F:/KPlato/koffice/libs/koproperty/editors/fontedit.cpp

Aller à la documentation de ce fichier.
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>
00026 #include <QPainter>
00027 #include <QLayout>
00028 #include <QVariant>
00029 #include <QFont>
00030 #include <QFontMetrics>
00031 #include <QLabel>
00032 #include <QToolTip>
00033 #include <QEvent>
00034 #include <QKeyEvent>
00035 #include <QFrame>
00036 #include <QResizeEvent>
00037 
00038 #include <kdeversion.h>
00039 #include <kfontrequester.h>
00040 #include <kacceleratormanager.h>
00041 #include <klocale.h>
00042 
00045 class FontEditRequester : public KFontRequester
00046 {
00047         public:
00048                 FontEditRequester(QWidget* parent)
00049                         : KFontRequester(parent)
00050                 {
00051                         QPalette pal = label()->palette();
00052                         pal.setColor(QPalette::Window, palette().color(QPalette::Active, QPalette::Base));
00053                         label()->setPalette(pal);
00054                         label()->setMinimumWidth(0);
00055                         label()->setFrameShape(QFrame::Box);
00056                         label()->setIndent(-1);
00057                         label()->setFocusPolicy(Qt::ClickFocus);
00058                         KAcceleratorManager::setNoAccel(label());
00059                         layout()->removeWidget(label());
00060                         layout()->removeWidget(button());//->reparent(this, 0, QPoint(0,0));
00061                         delete layout();
00062                         button()->setText(i18n("..."));
00063                         button()->setToolTip( i18n("Change font"));
00064                         button()->setFocusPolicy(Qt::NoFocus);
00065                         button()->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00066                         QFontMetrics fm(button()->font());
00067                         button()->setFixedWidth(fm.width(button()->text()+' '));
00068                 }
00069                 virtual void resizeEvent(QResizeEvent *e)
00070                 {
00071                         KFontRequester::resizeEvent(e);
00072                         label()->move(0,0);
00073                         label()->resize(e->size()-QSize(button()->width(),-1));
00074                         button()->move(label()->width(),0);
00075                         button()->setFixedSize(button()->width(), height());
00076                 }
00077 };
00078 
00079 using namespace KoProperty;
00080 
00081 FontEdit::FontEdit(Property *property, QWidget *parent)
00082  : Widget(property, parent)
00083 {
00084         m_edit = new FontEditRequester(this);
00085         m_edit->setMinimumHeight(5);
00086         setEditor(m_edit);
00087         setFocusWidget(m_edit->label());
00088         connect(m_edit, SIGNAL(fontSelected(const QFont& )), this, SLOT(slotValueChanged(const QFont&)));
00089 }
00090 
00091 FontEdit::~FontEdit()
00092 {}
00093 
00094 QVariant
00095 FontEdit::value() const
00096 {
00097         return m_edit->font();
00098 }
00099 
00100 static QString sampleText(const QVariant &value)
00101 {
00102         QFontInfo fi(value.value<QFont>());
00103         return fi.family() + (fi.bold() ? ' ' + i18n("Bold") : QString()) +
00104                 (fi.italic() ? ' ' + i18n("Italic") : QString::null) +
00105                 ' ' + QString::number(fi.pointSize());
00106 }
00107 
00108 void
00109 FontEdit::setValue(const QVariant &value, bool emitChange)
00110 {
00111         m_edit->blockSignals(true);
00112         m_edit->setFont(value.value<QFont>());
00113         m_edit->blockSignals(false);
00114         m_edit->setSampleText(sampleText(value));
00115         if (emitChange)
00116                 emit valueChanged(this);
00117 }
00118 
00119 void
00120 FontEdit::drawViewer(QPainter *p, const QColorGroup &, const QRect &r, const QVariant &value)
00121 {
00122         p->eraseRect(r);
00123         p->setFont(value.value<QFont>());
00124         QRect r2(r);
00125         r2.setLeft(r2.left()+KPROPEDITOR_ITEM_MARGIN);
00126         r2.setBottom(r2.bottom()+1);
00127         p->drawText(r2, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine, sampleText(value));
00128 }
00129 
00130 void
00131 FontEdit::slotValueChanged(const QFont &)
00132 {
00133         emit valueChanged(this);
00134 }
00135 
00136 bool
00137 FontEdit::eventFilter(QObject* watched, QEvent* e)
00138 {
00139         if(e->type() == QEvent::KeyPress) {
00140                 QKeyEvent* ev = static_cast<QKeyEvent*>(e);
00141                 if(ev->key() == Qt::Key_Space) {
00142                         m_edit->button()->animateClick();
00143                         return true;
00144                 }
00145         }
00146         return Widget::eventFilter(watched, e);
00147 }
00148 
00149 void
00150 FontEdit::setReadOnlyInternal(bool readOnly)
00151 {
00152         setVisibleFlag(!readOnly);
00153 }
00154 
00155 #include "fontedit.moc"

Généré le Wed Nov 22 23:41:06 2006 pour KPlato par  doxygen 1.5.1-p1