fontedit.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
3 Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
4 Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
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 "editoritem.h"
24
25#include <tqpushbutton.h>
26#include <tqpainter.h>
27#include <tqlayout.h>
28#include <tqvariant.h>
29#include <tqfont.h>
30#include <tqfontmetrics.h>
31#include <tqlabel.h>
32#include <tqtooltip.h>
33
34#include <tdeversion.h>
35#include <tdefontrequester.h>
36#include <tdeaccelmanager.h>
37#include <tdelocale.h>
38
41
42namespace KoProperty {
43
44class FontEditRequester : public TDEFontRequester
45{
46 public:
47 FontEditRequester(TQWidget* parent)
48 : TDEFontRequester(parent)
49 {
50 label()->setPaletteBackgroundColor(palette().active().base());
51 label()->setMinimumWidth(0);
52 label()->setFrameShape(TQFrame::Box);
53 label()->setIndent(-1);
54#if TDE_VERSION >= TDE_MAKE_VERSION(3,4,0)
55 label()->setFocusPolicy(TQWidget::ClickFocus);
56 TDEAcceleratorManager::setNoAccel(label());
57#endif
58 layout()->remove(label());
59 layout()->remove(button());//->reparent(this, 0, TQPoint(0,0));
60 delete layout();
61 button()->setText(i18n("..."));
62 TQToolTip::add(button(), i18n("Change font"));
63 button()->setFocusPolicy(TQWidget::NoFocus);
64 button()->setSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Fixed);
65 TQFontMetrics fm(button()->font());
66 button()->setFixedWidth(fm.width(button()->text()+' '));
67 }
68 virtual void resizeEvent(TQResizeEvent *e)
69 {
70 TDEFontRequester::resizeEvent(e);
71 label()->move(0,0);
72 label()->resize(e->size()-TQSize(button()->width(),-1));
73 button()->move(label()->width(),0);
74 button()->setFixedSize(button()->width(), height());
75 }
76};
77
78}
79
80using namespace KoProperty;
81
82FontEdit::FontEdit(Property *property, TQWidget *parent, const char *name)
83 : Widget(property, parent, name)
84{
85 m_edit = new FontEditRequester(this);
86 m_edit->setMinimumHeight(5);
87 setEditor(m_edit);
88 setFocusWidget(m_edit->label());
89 connect(m_edit, TQ_SIGNAL(fontSelected(const TQFont& )), this, TQ_SLOT(slotValueChanged(const TQFont&)));
90}
91
92FontEdit::~FontEdit()
93{}
94
95TQVariant
96FontEdit::value() const
97{
98 return m_edit->font();
99}
100
101static TQString sampleText(const TQVariant &value)
102{
103 TQFontInfo fi(value.toFont());
104 return fi.family() + (fi.bold() ? " " + i18n("Bold") : TQString()) +
105 (fi.italic() ? " " + i18n("Italic") : TQString()) +
106 " " + TQString::number(fi.pointSize());
107}
108
109void
110FontEdit::setValue(const TQVariant &value, bool emitChange)
111{
112 m_edit->blockSignals(true);
113 m_edit->setFont(value.toFont());
114 m_edit->blockSignals(false);
115 m_edit->setSampleText(sampleText(value));
116 if (emitChange)
117 emit valueChanged(this);
118}
119
120void
121FontEdit::drawViewer(TQPainter *p, const TQColorGroup &, const TQRect &r, const TQVariant &value)
122{
123 p->eraseRect(r);
124 p->setFont(value.toFont());
125 TQRect r2(r);
126 r2.setLeft(r2.left()+KPROPEDITOR_ITEM_MARGIN);
127 r2.setBottom(r2.bottom()+1);
128 p->drawText(r2, TQt::AlignLeft | TQt::AlignVCenter | TQt::SingleLine, sampleText(value));
129}
130
131void
132FontEdit::slotValueChanged(const TQFont &)
133{
134 emit valueChanged(this);
135}
136
137bool
138FontEdit::eventFilter(TQObject* watched, TQEvent* e)
139{
140 if(e->type() == TQEvent::KeyPress) {
141 TQKeyEvent* ev = static_cast<TQKeyEvent*>(e);
142 if(ev->key() == Key_Space) {
143 m_edit->button()->animateClick();
144 return true;
145 }
146 }
147 return Widget::eventFilter(watched, e);
148}
149
150void
151FontEdit::setReadOnlyInternal(bool readOnly)
152{
153 setVisibleFlag(!readOnly);
154}
155
156#include "fontedit.moc"
The base class representing a single property.
Definition property.h:177
The base class for all item editors used in Editor.
Definition widget.h:37
virtual bool eventFilter(TQObject *watched, TQEvent *e)
Definition widget.cpp:108
TQPushButton * button() const
TQFont font() const
TQLabel * label() const
const char * name(StdAction id)
Namespace for a set of classes implementing generic properties framework.