lib

fontedit.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
00003    Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
00004    Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "fontedit.h"
00023 #include "editoritem.h"
00024 
00025 #include <qpushbutton.h>
00026 #include <qpainter.h>
00027 #include <qlayout.h>
00028 #include <qvariant.h>
00029 #include <qfont.h>
00030 #include <qfontmetrics.h>
00031 #include <qlabel.h>
00032 #include <qtooltip.h>
00033 
00034 #include <kdeversion.h>
00035 #include <kfontrequester.h>
00036 #include <kaccelmanager.h>
00037 
00038 #ifdef QT_ONLY
00039 
00040 #else
00041 #include <klocale.h>
00042 #endif
00043 
00046 class FontEditRequester : public KFontRequester
00047 {
00048     public:
00049         FontEditRequester(QWidget* parent)
00050             : KFontRequester(parent)
00051         {
00052             label()->setPaletteBackgroundColor(palette().active().base());
00053             label()->setMinimumWidth(0);
00054             label()->setFrameShape(QFrame::Box);
00055             label()->setIndent(-1);
00056 #if KDE_VERSION >= KDE_MAKE_VERSION(3,4,0) 
00057             label()->setFocusPolicy(ClickFocus);
00058             KAcceleratorManager::setNoAccel(label());
00059 #endif
00060             layout()->remove(label());
00061             layout()->remove(button());//->reparent(this, 0, QPoint(0,0));
00062             delete layout();
00063             button()->setText(i18n("..."));
00064             QToolTip::add(button(), i18n("Change font"));
00065             button()->setFocusPolicy(NoFocus);
00066             button()->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00067             QFontMetrics fm(button()->font());
00068             button()->setFixedWidth(fm.width(button()->text()+" "));
00069         }
00070         virtual void resizeEvent(QResizeEvent *e)
00071         {
00072             KFontRequester::resizeEvent(e);
00073             label()->move(0,0);
00074             label()->resize(e->size()-QSize(button()->width(),-1));
00075             button()->move(label()->width(),0);
00076             button()->setFixedSize(button()->width(), height());
00077         }
00078 };
00079 
00080 using namespace KoProperty;
00081 
00082 FontEdit::FontEdit(Property *property, QWidget *parent, const char *name)
00083  : Widget(property, parent, name)
00084 {
00085     m_edit = new FontEditRequester(this);
00086     m_edit->setMinimumHeight(5);
00087     setEditor(m_edit);
00088     setFocusWidget(m_edit->label());
00089     connect(m_edit, SIGNAL(fontSelected(const QFont& )), this, SLOT(slotValueChanged(const QFont&)));
00090 }
00091 
00092 FontEdit::~FontEdit()
00093 {}
00094 
00095 QVariant
00096 FontEdit::value() const
00097 {
00098     return m_edit->font();
00099 }
00100 
00101 static QString sampleText(const QVariant &value)
00102 {
00103     QFontInfo fi(value.toFont());
00104     return fi.family() + (fi.bold() ? " " + i18n("Bold") : QString("")) +
00105         (fi.italic() ? " " + i18n("Italic") : QString::null) +
00106         " " + QString::number(fi.pointSize());
00107 }
00108 
00109 void
00110 FontEdit::setValue(const QVariant &value, bool emitChange)
00111 {
00112     m_edit->blockSignals(true);
00113     m_edit->setFont(value.toFont());
00114     m_edit->blockSignals(false);
00115     m_edit->setSampleText(sampleText(value));
00116     if (emitChange)
00117         emit valueChanged(this);
00118 }
00119 
00120 void
00121 FontEdit::drawViewer(QPainter *p, const QColorGroup &, const QRect &r, const QVariant &value)
00122 {
00123     p->eraseRect(r);
00124     p->setFont(value.toFont());
00125     QRect r2(r);
00126     r2.setLeft(r2.left()+KPROPEDITOR_ITEM_MARGIN);
00127     r2.setBottom(r2.bottom()+1);
00128     p->drawText(r2, Qt::AlignLeft | Qt::AlignVCenter | Qt::SingleLine, sampleText(value));
00129 }
00130 
00131 void
00132 FontEdit::slotValueChanged(const QFont &)
00133 {
00134     emit valueChanged(this);
00135 }
00136 
00137 bool
00138 FontEdit::eventFilter(QObject* watched, QEvent* e)
00139 {
00140     if(e->type() == QEvent::KeyPress) {
00141         QKeyEvent* ev = static_cast<QKeyEvent*>(e);
00142         if(ev->key() == Key_Space) {
00143             m_edit->button()->animateClick();
00144             return true;
00145         }
00146     }
00147     return Widget::eventFilter(watched, e);
00148 }
00149 
00150 void
00151 FontEdit::setReadOnlyInternal(bool readOnly)
00152 {
00153     setVisibleFlag(!readOnly);
00154 }
00155 
00156 #include "fontedit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys