lib
fontedit.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 #include <klocale.h>
00038
00041
00042 namespace KoProperty {
00043
00044 class FontEditRequester : public KFontRequester
00045 {
00046 public:
00047 FontEditRequester(QWidget* parent)
00048 : KFontRequester(parent)
00049 {
00050 label()->setPaletteBackgroundColor(palette().active().base());
00051 label()->setMinimumWidth(0);
00052 label()->setFrameShape(QFrame::Box);
00053 label()->setIndent(-1);
00054 #if KDE_VERSION >= KDE_MAKE_VERSION(3,4,0)
00055 label()->setFocusPolicy(ClickFocus);
00056 KAcceleratorManager::setNoAccel(label());
00057 #endif
00058 layout()->remove(label());
00059 layout()->remove(button());
00060 delete layout();
00061 button()->setText(i18n("..."));
00062 QToolTip::add(button(), i18n("Change font"));
00063 button()->setFocusPolicy(NoFocus);
00064 button()->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00065 QFontMetrics fm(button()->font());
00066 button()->setFixedWidth(fm.width(button()->text()+' '));
00067 }
00068 virtual void resizeEvent(QResizeEvent *e)
00069 {
00070 KFontRequester::resizeEvent(e);
00071 label()->move(0,0);
00072 label()->resize(e->size()-QSize(button()->width(),-1));
00073 button()->move(label()->width(),0);
00074 button()->setFixedSize(button()->width(), height());
00075 }
00076 };
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"
|