KProperty

fontedit.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
3 Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
4 Copyright (C) 2005-2017 Jarosław Staniek <staniek@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20*/
21
22#include "fontedit.h"
23#include "fontedit_p.h"
24#include "utils.h"
25#include "kproperty_debug.h"
26#include "KPropertyUtils_p.h"
27
28#include <QApplication>
29#include <QPushButton>
30#include <QModelIndex>
31#include <QStyleOptionViewItem>
32#include <QFontDatabase>
33#include <QHBoxLayout>
34#include <QPushButton>
35#include <QFontDialog>
36
37KPropertyFontEditRequester::KPropertyFontEditRequester(QWidget *parent)
38 : QWidget(parent)
39 , m_paletteChangedEnabled(true)
40{
41 setBackgroundRole(QPalette::Base);
42 QHBoxLayout *lyr = new QHBoxLayout(this);
43 lyr->setContentsMargins(0,0,0,0);
44 lyr->setSpacing( 1 );
45 lyr->addStretch(1);
46 m_button = new QPushButton(this);
47 setFocusProxy(m_button);
48 KPropertyUtils::setupDotDotDotButton(m_button,
49 tr("Click to select a font"),
50 tr("Selects font"));
51 connect( m_button, SIGNAL( clicked() ), SLOT( slotSelectFontClicked() ) );
52 lyr->addWidget(m_button);
53 setValue(qApp->font());
54}
55
56QFont KPropertyFontEditRequester::value() const
57{
58 return m_font;
59}
60
61void KPropertyFontEditRequester::setValue(const QFont &value)
62{
63 //kprDebug() << QFontDatabase().families();
64 m_font = value;
65}
66
67void KPropertyFontEditRequester::slotSelectFontClicked()
68{
70 if (!KPropertyUtilsPrivate::shouldUseNativeDialogs()) { // switch to Qt's dialog outside of KDE desktops
71 dialogOptions |= QFontDialog::DontUseNativeDialog;
72 }
73 bool ok;
74 const QFont font = QFontDialog::getFont(&ok, m_font, parentWidget(), QString(), dialogOptions);
75 if (ok) {
76 m_font = font;
77 setValue(m_font);
78 emit commitData(this);
79 }
80}
81
82bool KPropertyFontEditRequester::event( QEvent * event )
83{
84 return QWidget::event(event);
85}
86
87// -----------
88
89KPropertyFontDelegate::KPropertyFontDelegate()
90{
91}
92
93QWidget * KPropertyFontDelegate::createEditor( int type, QWidget *parent,
94 const QStyleOptionViewItem & option, const QModelIndex & index ) const
95{
96 Q_UNUSED(type);
97 Q_UNUSED(option);
98 Q_UNUSED(index);
99 return new KPropertyFontEditRequester(parent);
100}
101
102void KPropertyFontDelegate::paint( QPainter * painter,
103 const QStyleOptionViewItem & option, const QModelIndex & index ) const
104{
105 const KPropertyUtilsPrivate::PainterSaver saver(painter);
106 const QFont origFont( painter->font() );
107 QFont f( index.data(Qt::EditRole).value<QFont>() );
108 if (option.font.pointSize() > 0)
109 f.setPointSize(option.font.pointSize());
110 else if (option.font.pixelSize() > 0)
111 f.setPixelSize(option.font.pixelSize());
112 painter->setFont( f );
113 QRect rect( option.rect );
114 rect.setLeft( rect.left() + 1 );
115 const QString txt( QObject::tr("Abc", "Font sample for property editor item, typically \"Abc\"") );
116 painter->drawText( rect, Qt::AlignLeft | Qt::AlignVCenter,
117 QObject::tr("Abc", "Font sample for property editor item, typically \"Abc\"") );
118
119 rect.setLeft(rect.left() + 5 + painter->fontMetrics().width( txt ));
120 painter->setFont(origFont);
121 painter->drawText(rect, Qt::AlignLeft | Qt::AlignVCenter, valueToString(index.data(Qt::EditRole), QLocale()));
122
123}
124
125QString KPropertyFontDelegate::valueToString(const QVariant& value, const QLocale &locale) const
126{
127 const QFont f(value.value<QFont>());
128 qreal size = f.pointSizeF();
129 QString unit;
130 if (size == -1) {
131 size = f.pixelSize();
132 unit = QLatin1String("px");
133 }
134 else {
135 unit = QLatin1String("pt");
136 }
138 list << f.family();
139 const bool translate = locale.language() == QLocale::C;
140 list << (translate ? QObject::tr("%1%2", "<fontsize><unit>, e.g. 12pt").arg(size).arg(unit)
141 : QString::fromLatin1("%1%2").arg(size).arg(unit));
142 if (f.bold()) {
143 list << (translate ? QObject::tr("bold", "bold font") : QLatin1String("bold"));
144 }
145 if (f.italic()) {
146 list << (translate ? QObject::tr("italic", "italic font") : QLatin1String("italic"));
147 }
148 if (f.strikeOut()) {
149 list << (translate ? QObject::tr("strikeout", "strikeout font") : QLatin1String("strikeout"));
150 }
151 if (f.underline()) {
152 list << (translate ? QObject::tr("underline", "underline font") : QLatin1String("underline"));
153 }
154 return QLocale::c().createSeparatedList(list); // yes, C locale, we just want ',' separator.
155}
KIOCORE_EXPORT QStringList list(const QString &fileClass)
void addStretch(int stretch)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
virtual void setSpacing(int spacing) override
QFont getFont(bool *ok, QWidget *parent)
void setContentsMargins(const QMargins &margins)
QLocale c()
QString createSeparatedList(const QStringList &list) const const
Language language() const const
QVariant data(int role) const const
QString tr(const char *sourceText, const char *disambiguation, int n)
void drawText(const QPoint &position, const QString &text)
const QFont & font() const const
QFontMetrics fontMetrics() const const
void setFont(const QFont &font)
QString arg(Args &&... args) const const
QString fromLatin1(QByteArrayView str)
AlignLeft
EditRole
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
T value() const const
virtual bool event(QEvent *event) override
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:11:18 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.