booledit.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) 2006 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 "booledit.h"
23#include "../property.h"
24
25#include <kiconloader.h>
26#include <tdelocale.h>
27#include <kcombobox.h>
28#include <kdebug.h>
29
30#include <tqtoolbutton.h>
31#include <tqpainter.h>
32#include <tqvariant.h>
33#include <tqlayout.h>
34#include <tqbitmap.h>
35
36using namespace KoProperty;
37
38BoolEdit::BoolEdit(Property *property, TQWidget *parent, const char *name)
39 : Widget(property, parent, name)
40 , m_yesIcon( SmallIcon("button_ok") )
41 , m_noIcon( SmallIcon("button_no") )
42{
43 m_toggle = new TQToolButton(this);
44 m_toggle->setToggleButton( true );
45 m_toggle->setFocusPolicy(TQWidget::WheelFocus);
46 m_toggle->setUsesTextLabel(true);
47 m_toggle->setTextPosition(TQToolButton::Right);
48 m_toggle->setSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Fixed);
49 //we're not using layout to because of problems with button size
50 m_toggle->move(0, 0);
51 m_toggle->resize(width(), height());
52 setFocusWidget(m_toggle);
53 connect(m_toggle, TQ_SIGNAL(stateChanged(int)), this, TQ_SLOT(slotValueChanged(int)));
54}
55
56BoolEdit::~BoolEdit()
57{
58}
59
60TQVariant
61BoolEdit::value() const
62{
63 return TQVariant(m_toggle->isOn());
64}
65
66void
67BoolEdit::setValue(const TQVariant &value, bool emitChange)
68{
69 m_toggle->blockSignals(true);
70 m_toggle->setOn(value.toBool());
71 setState( value.toBool() ? TQButton::On : TQButton::Off );
72 m_toggle->blockSignals(false);
73 if (emitChange)
74 emit valueChanged(this);
75}
76
77void
78BoolEdit::slotValueChanged(int state)
79{
80 setState(state);
81 emit valueChanged(this);
82}
83
84static void drawViewerInternal(TQPainter *p, const TQRect &r, const TQVariant &value,
85 const TQPixmap& yesIcon, const TQPixmap& noIcon, const TQString& nullText)
86{
87 p->eraseRect(r);
88 TQRect r2(r);
89 r2.moveLeft(TDEIcon::SizeSmall + 6);
90
91 if(value.isNull() && !nullText.isEmpty()) {
92 p->drawText(r2, TQt::AlignVCenter | TQt::AlignLeft, nullText);
93 }
94 else if(value.toBool()) {
95 p->drawPixmap(3, (r.height()-1-TDEIcon::SizeSmall)/2, yesIcon);
96 p->drawText(r2, TQt::AlignVCenter | TQt::AlignLeft, i18n("Yes"));
97 }
98 else {
99 p->drawPixmap(3, (r.height()-1-TDEIcon::SizeSmall)/2, noIcon);
100 p->drawText(r2, TQt::AlignVCenter | TQt::AlignLeft, i18n("No"));
101 }
102}
103
104void
105BoolEdit::drawViewer(TQPainter *p, const TQColorGroup &cg, const TQRect &r, const TQVariant &value)
106{
107 Q_UNUSED(cg);
108 drawViewerInternal(p, r, value, m_yesIcon, m_noIcon, "");
109}
110
111void
112BoolEdit::setState(int state)
113{
114 if(TQButton::On == state) {
115 m_toggle->setIconSet(TQIconSet(m_yesIcon));
116 m_toggle->setTextLabel(i18n("Yes"));
117 }
118 else if (TQButton::Off == state) {
119 m_toggle->setIconSet(TQIconSet(m_noIcon));
120 m_toggle->setTextLabel(i18n("No"));
121 }
122}
123
124void
125BoolEdit::resizeEvent(TQResizeEvent *ev)
126{
127 m_toggle->resize(ev->size());
128}
129
130bool
131BoolEdit::eventFilter(TQObject* watched, TQEvent* e)
132{
133 if(e->type() == TQEvent::KeyPress) {
134 TQKeyEvent* ev = static_cast<TQKeyEvent*>(e);
135 const int k = ev->key();
136 if(k == TQt::Key_Space || k == TQt::Key_Enter || k == TQt::Key_Return) {
137 if (m_toggle)
138 m_toggle->toggle();
139 return true;
140 }
141 }
142 return Widget::eventFilter(watched, e);
143}
144
145void
146BoolEdit::setReadOnlyInternal(bool readOnly)
147{
148 setVisibleFlag(!readOnly);
149}
150
151//--------------------------------------------------
152
153ThreeStateBoolEdit::ThreeStateBoolEdit(Property *property, TQWidget *parent, const char *name)
154 : ComboBox(property, parent, name)
155 , m_yesIcon( SmallIcon("button_ok") )
156 , m_noIcon( SmallIcon("button_no") )
157{
158 m_edit->insertItem( m_yesIcon, i18n("Yes") );
159 m_edit->insertItem( m_noIcon, i18n("No") );
160 TQVariant thirdState = property ? property->option("3rdState") : TQVariant();
161 TQPixmap nullIcon( m_yesIcon.size() ); //transparent pixmap of appropriate size
162 nullIcon.setMask(TQBitmap(m_yesIcon.size(), true));
163 m_edit->insertItem( nullIcon, thirdState.toString().isEmpty() ? i18n("None") : thirdState.toString() );
164}
165
166ThreeStateBoolEdit::~ThreeStateBoolEdit()
167{
168}
169
170TQVariant
171ThreeStateBoolEdit::value() const
172{
173 // list items: true, false, NULL
174 const int idx = m_edit->currentItem();
175 if (idx==0)
176 return TQVariant(true);
177 else
178 return idx==1 ? TQVariant(false) : TQVariant();
179}
180
181void
182ThreeStateBoolEdit::setProperty(Property *prop)
183{
184 m_setValueEnabled = false; //setValue() couldn't be called before fillBox()
186 m_setValueEnabled = true;
187 if(prop)
188 setValue(prop->value(), false); //now the value can be set
189}
190
191void
192ThreeStateBoolEdit::setValue(const TQVariant &value, bool emitChange)
193{
194 if (!m_setValueEnabled)
195 return;
196
197 if (value.isNull())
198 m_edit->setCurrentItem(2);
199 else
200 m_edit->setCurrentItem(value.toBool() ? 0 : 1);
201
202 if (emitChange)
203 emit valueChanged(this);
204}
205
206void
207ThreeStateBoolEdit::drawViewer(TQPainter *p, const TQColorGroup &cg, const TQRect &r, const TQVariant &value)
208{
209 Q_UNUSED(cg);
210 drawViewerInternal(p, r, value, m_yesIcon, m_noIcon, m_edit->text(2));
211}
212
213#include "booledit.moc"
The base class representing a single property.
Definition property.h:177
TQVariant value() const
Definition property.cpp:314
The base class for all item editors used in Editor.
Definition widget.h:37
virtual void setProperty(Property *property)
Definition widget.cpp:78
virtual bool eventFilter(TQObject *watched, TQEvent *e)
Definition widget.cpp:108
const char * name(StdAction id)
Namespace for a set of classes implementing generic properties framework.