File indexing completed on 2024-04-21 04:41:00

0001 /* This file is part of the KDE project
0002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
0003    Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
0004    Copyright (C) 2005-2017 Jarosław Staniek <staniek@kde.org>
0005 
0006    This library is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU Library General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 
0011    This library is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014    Library General Public License for more details.
0015 
0016    You should have received a copy of the GNU Library General Public License
0017    along with this library; see the file COPYING.LIB.  If not, write to
0018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #include "fontedit.h"
0023 #include "fontedit_p.h"
0024 #include "utils.h"
0025 #include "kproperty_debug.h"
0026 #include "KPropertyUtils_p.h"
0027 
0028 #include <QApplication>
0029 #include <QPushButton>
0030 #include <QModelIndex>
0031 #include <QStyleOptionViewItem>
0032 #include <QFontDatabase>
0033 #include <QHBoxLayout>
0034 #include <QPushButton>
0035 #include <QFontDialog>
0036 
0037 KPropertyFontEditRequester::KPropertyFontEditRequester(QWidget *parent)
0038         : QWidget(parent)
0039         , m_paletteChangedEnabled(true)
0040 {
0041     setBackgroundRole(QPalette::Base);
0042     QHBoxLayout *lyr = new QHBoxLayout(this);
0043     lyr->setContentsMargins(0,0,0,0);
0044     lyr->setSpacing( 1 );
0045     lyr->addStretch(1);
0046     m_button = new QPushButton(this);
0047     setFocusProxy(m_button);
0048     KPropertyUtils::setupDotDotDotButton(m_button,
0049         tr("Click to select a font"),
0050         tr("Selects font"));
0051     connect( m_button, SIGNAL( clicked() ), SLOT( slotSelectFontClicked() ) );
0052     lyr->addWidget(m_button);
0053     setValue(qApp->font());
0054 }
0055 
0056 QFont KPropertyFontEditRequester::value() const
0057 {
0058     return m_font;
0059 }
0060 
0061 void KPropertyFontEditRequester::setValue(const QFont &value)
0062 {
0063     //kprDebug() << QFontDatabase().families();
0064     m_font = value;
0065 }
0066 
0067 void KPropertyFontEditRequester::slotSelectFontClicked()
0068 {
0069     QFontDialog::FontDialogOptions dialogOptions;
0070     if (!KPropertyUtilsPrivate::shouldUseNativeDialogs()) { // switch to Qt's dialog outside of KDE desktops
0071         dialogOptions |= QFontDialog::DontUseNativeDialog;
0072     }
0073     bool ok;
0074     const QFont font = QFontDialog::getFont(&ok, m_font, parentWidget(), QString(), dialogOptions);
0075     if (ok) {
0076         m_font = font;
0077         setValue(m_font);
0078         emit commitData(this);
0079     }
0080 }
0081 
0082 bool KPropertyFontEditRequester::event( QEvent * event )
0083 {
0084     return QWidget::event(event);
0085 }
0086 
0087 // -----------
0088 
0089 KPropertyFontDelegate::KPropertyFontDelegate()
0090 {
0091 }
0092 
0093 QWidget * KPropertyFontDelegate::createEditor( int type, QWidget *parent,
0094     const QStyleOptionViewItem & option, const QModelIndex & index ) const
0095 {
0096     Q_UNUSED(type);
0097     Q_UNUSED(option);
0098     Q_UNUSED(index);
0099     return new KPropertyFontEditRequester(parent);
0100 }
0101 
0102 void KPropertyFontDelegate::paint( QPainter * painter,
0103     const QStyleOptionViewItem & option, const QModelIndex & index ) const
0104 {
0105     const KPropertyUtilsPrivate::PainterSaver saver(painter);
0106     const QFont origFont( painter->font() );
0107     QFont f( index.data(Qt::EditRole).value<QFont>() );
0108     if (option.font.pointSize() > 0)
0109         f.setPointSize(option.font.pointSize());
0110     else if (option.font.pixelSize() > 0)
0111         f.setPixelSize(option.font.pixelSize());
0112     painter->setFont( f );
0113     QRect rect( option.rect );
0114     rect.setLeft( rect.left() + 1 );
0115     const QString txt( QObject::tr("Abc", "Font sample for property editor item, typically \"Abc\"") );
0116     painter->drawText( rect, Qt::AlignLeft | Qt::AlignVCenter,
0117         QObject::tr("Abc", "Font sample for property editor item, typically \"Abc\"") );
0118 
0119     rect.setLeft(rect.left() + 5 + painter->fontMetrics().width( txt ));
0120     painter->setFont(origFont);
0121     painter->drawText(rect, Qt::AlignLeft | Qt::AlignVCenter, valueToString(index.data(Qt::EditRole), QLocale()));
0122 
0123 }
0124 
0125 QString KPropertyFontDelegate::valueToString(const QVariant& value, const QLocale &locale) const
0126 {
0127     const QFont f(value.value<QFont>());
0128     qreal size = f.pointSizeF();
0129     QString unit;
0130     if (size == -1) {
0131         size = f.pixelSize();
0132         unit = QLatin1String("px");
0133     }
0134     else {
0135         unit = QLatin1String("pt");
0136     }
0137     QStringList list;
0138     list << f.family();
0139     const bool translate = locale.language() == QLocale::C;
0140     list << (translate ? QObject::tr("%1%2", "<fontsize><unit>, e.g. 12pt").arg(size).arg(unit)
0141                       : QString::fromLatin1("%1%2").arg(size).arg(unit));
0142     if (f.bold()) {
0143         list << (translate ? QObject::tr("bold", "bold font") : QLatin1String("bold"));
0144     }
0145     if (f.italic()) {
0146         list << (translate ? QObject::tr("italic", "italic font") : QLatin1String("italic"));
0147     }
0148     if (f.strikeOut()) {
0149         list << (translate ? QObject::tr("strikeout", "strikeout font") : QLatin1String("strikeout"));
0150     }
0151     if (f.underline()) {
0152         list << (translate ? QObject::tr("underline", "underline font") : QLatin1String("underline"));
0153     }
0154     return QLocale::c().createSeparatedList(list); // yes, C locale, we just want ',' separator.
0155 }