KProperty

fontedit.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2004 Cedric Pasteur <[email protected]>
3  Copyright (C) 2004 Alexander Dymo <[email protected]>
4  Copyright (C) 2005-2017 Jarosław Staniek <[email protected]>
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 
37 KPropertyFontEditRequester::KPropertyFontEditRequester(QWidget *parent)
38  : QWidget(parent)
39  , m_paletteChangedEnabled(true)
40 {
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 
56 QFont KPropertyFontEditRequester::value() const
57 {
58  return m_font;
59 }
60 
61 void KPropertyFontEditRequester::setValue(const QFont &value)
62 {
63  //kprDebug() << QFontDatabase().families();
64  m_font = value;
65 }
66 
67 void KPropertyFontEditRequester::slotSelectFontClicked()
68 {
69  QFontDialog::FontDialogOptions dialogOptions;
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 
82 bool KPropertyFontEditRequester::event( QEvent * event )
83 {
84  return QWidget::event(event);
85 }
86 
87 // -----------
88 
89 KPropertyFontDelegate::KPropertyFontDelegate()
90 {
91 }
92 
93 QWidget * 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 
102 void 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\"") );
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 
125 QString 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 }
void setContentsMargins(int left, int top, int right, int bottom)
const QFont & font() const const
T value() const const
QFont getFont(bool *ok, QWidget *parent)
QString tr(const char *sourceText, const char *disambiguation, int n)
AlignLeft
QSize size() const const
QLocale c()
void setFont(const QFont &font)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
int left() const const
EditRole
void setFocusProxy(QWidget *w)
void drawText(const QPointF &position, const QString &text)
QLocale::Language language() const const
QString createSeparatedList(const QStringList &list) const const
QLocale locale() const const
QRect rect() const const
const QFont & font() const const
QVariant data(int role) const const
void addStretch(int stretch)
QFontMetrics fontMetrics() const const
QWidget * parentWidget() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
typedef FontDialogOptions
QString fromLatin1(const char *str, int size)
int width(const QString &text, int len) const const
void setBackgroundRole(QPalette::ColorRole role)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
virtual bool event(QEvent *event) override
void setLeft(int x)
void setSpacing(int spacing)
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 23:10:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.