lib

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 <tdefontrequester.h>
35#include <tdeaccelmanager.h>
36#include <tdelocale.h>
37
40
41namespace KoProperty {
42
43class FontEditRequester : public TDEFontRequester
44{
45 public:
46 FontEditRequester(TQWidget* parent)
47 : TDEFontRequester(parent)
48 {
49 label()->setPaletteBackgroundColor(palette().active().base());
50 label()->setMinimumWidth(0);
51 label()->setFrameShape(TQFrame::Box);
52 label()->setIndent(-1);
53 label()->setFocusPolicy(TQWidget::ClickFocus);
54 TDEAcceleratorManager::setNoAccel(label());
55 layout()->remove(label());
56 layout()->remove(button());//->reparent(this, 0, TQPoint(0,0));
57 delete layout();
58 button()->setText(i18n("..."));
59 TQToolTip::add(button(), i18n("Change font"));
60 button()->setFocusPolicy(TQWidget::NoFocus);
61 button()->setSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Fixed);
62 TQFontMetrics fm(button()->font());
63 button()->setFixedWidth(fm.width(button()->text()+' '));
64 }
65 virtual void resizeEvent(TQResizeEvent *e)
66 {
67 TDEFontRequester::resizeEvent(e);
68 label()->move(0,0);
69 label()->resize(e->size()-TQSize(button()->width(),-1));
70 button()->move(label()->width(),0);
71 button()->setFixedSize(button()->width(), height());
72 }
73};
74
75}
76
77using namespace KoProperty;
78
79FontEdit::FontEdit(Property *property, TQWidget *parent, const char *name)
80 : Widget(property, parent, name)
81{
82 m_edit = new FontEditRequester(this);
83 m_edit->setMinimumHeight(5);
84 setEditor(m_edit);
85 setFocusWidget(m_edit->label());
86 connect(m_edit, TQ_SIGNAL(fontSelected(const TQFont& )), this, TQ_SLOT(slotValueChanged(const TQFont&)));
87}
88
89FontEdit::~FontEdit()
90{}
91
92TQVariant
93FontEdit::value() const
94{
95 return m_edit->font();
96}
97
98static TQString sampleText(const TQVariant &value)
99{
100 TQFontInfo fi(value.toFont());
101 return fi.family() + (fi.bold() ? " " + i18n("Bold") : TQString()) +
102 (fi.italic() ? " " + i18n("Italic") : TQString()) +
103 " " + TQString::number(fi.pointSize());
104}
105
106void
107FontEdit::setValue(const TQVariant &value, bool emitChange)
108{
109 m_edit->blockSignals(true);
110 m_edit->setFont(value.toFont());
111 m_edit->blockSignals(false);
112 m_edit->setSampleText(sampleText(value));
113 if (emitChange)
114 emit valueChanged(this);
115}
116
117void
118FontEdit::drawViewer(TQPainter *p, const TQColorGroup &, const TQRect &r, const TQVariant &value)
119{
120 p->eraseRect(r);
121 p->setFont(value.toFont());
122 TQRect r2(r);
123 r2.setLeft(r2.left()+KPROPEDITOR_ITEM_MARGIN);
124 r2.setBottom(r2.bottom()+1);
125 p->drawText(r2, TQt::AlignLeft | TQt::AlignVCenter | TQt::SingleLine, sampleText(value));
126}
127
128void
129FontEdit::slotValueChanged(const TQFont &)
130{
131 emit valueChanged(this);
132}
133
134bool
135FontEdit::eventFilter(TQObject* watched, TQEvent* e)
136{
137 if(e->type() == TQEvent::KeyPress) {
138 TQKeyEvent* ev = static_cast<TQKeyEvent*>(e);
139 if(ev->key() == Key_Space) {
140 m_edit->button()->animateClick();
141 return true;
142 }
143 }
144 return Widget::eventFilter(watched, e);
145}
146
147void
148FontEdit::setReadOnlyInternal(bool readOnly)
149{
150 setVisibleFlag(!readOnly);
151}
152
153#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
void setVisibleFlag(bool visible)
Definition widget.cpp:227
virtual bool eventFilter(TQObject *watched, TQEvent *e)
Definition widget.cpp:108
virtual TQVariant value() const =0
TQPushButton * button() const
TDEFontRequester(TQWidget *parent=0L, const char *name=0L, bool onlyFixed=false)
TQFont font() const
TQLabel * label() const
const char * name(StdAction id)
Namespace for a set of classes implementing generic properties framework.