widget.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
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "widget.h"
22#include "property.h"
23#include "editoritem.h"
24#include "editor.h"
25
26#include <tqpainter.h>
27#include <tqvariant.h>
28
29#include <tdelistview.h>
30#include <kdebug.h>
31
32using namespace KoProperty;
33
34namespace KoProperty {
35class WidgetPrivate
36{
37 public:
38 WidgetPrivate()
39 : property(0)
40 , editor(0)
41 , leaveTheSpaceForRevertButton(false)
42 , hasBorders(true)
43 , readOnly(false)
44 , visibleFlag(true)
45 {
46 }
47 ~WidgetPrivate() {}
48
49 Property *property;
50 TQWidget *editor;
51 bool leaveTheSpaceForRevertButton : 1;
52 bool hasBorders : 1;
53 bool readOnly : 1;
54 bool visibleFlag : 1;
55};
56}
57
58Widget::Widget(Property *property, TQWidget *parent, const char *name)
59 : TQWidget(parent, name)
60{
61 d = new WidgetPrivate();
62 d->property = property;
63}
64
65Widget::~Widget()
66{
67 delete d;
68 d = 0;
69}
70
73{
74 return d ? d->property : 0; //for sanity
75}
76
77void
79{
80 d->property = property;
81 if(property)
82 setValue(property->value(), false);
83 //if(property->type() == ValueFromList)
84 // setValueList(property->valueList());
85}
86
87void
88Widget::drawViewer(TQPainter *p, const TQColorGroup &, const TQRect &r, const TQVariant &value)
89{
90 p->eraseRect(r);
91 TQRect rect(r);
92 rect.setLeft(rect.left()+KPROPEDITOR_ITEM_MARGIN);
93// if (d->hasBorders)
94// rect.setTop(rect.top()+1); //+1 to have the same vertical position as editor
95// else
96// rect.setHeight(rect.height()-1); //don't place over listviews's border
97 p->drawText(rect, TQt::AlignLeft | TQt::AlignVCenter | TQt::SingleLine, value.toString());
98}
99
100void
102{
103 if(d->property)
104 d->property->resetValue();
105}
106
107bool
108Widget::eventFilter(TQObject*, TQEvent* e)
109{
110 if(e->type() == TQEvent::KeyPress)
111 {
112 TQKeyEvent* ev = static_cast<TQKeyEvent*>(e);
113 if(ev->key() == Key_Escape)
114 {
115 emit rejectInput(this);
116 return true;
117 }
118 else if((ev->key() == Key_Return) || (ev->key() == Key_Enter))
119 {
120 // should apply when autosync == false
121 emit acceptInput(this);
122 return true;
123 }
124 else {
125 Editor *list = static_cast<KoProperty::Editor*>(parentWidget()->parentWidget());
126 if (!list)
127 return false; //for sanity
128 return list->handleKeyPress(ev);
129 }
130
131 /* moved in Editor
132 if (item) {
133 if(ev->key() == Key_Up && ev->state() != ControlButton)
134 {
135 if(item->itemAbove())
136 list->setCurrentItem(item->itemAbove());
137 return true;
138 }
139 else if(ev->key() == Key_Down && ev->state() != ControlButton)
140 {
141 if(item->itemBelow())
142 list->setCurrentItem(item->itemBelow());
143 return true;
144 }
145 }*/
146 }
147
148 return false;
149}
150
151void
152Widget::setFocusWidget(TQWidget*focusProxy)
153{
154 if (focusProxy) {
155 if (focusProxy->focusPolicy() != TQWidget::NoFocus)
156 setFocusProxy(focusProxy);
157 focusProxy->installEventFilter(this);
158 }
159 else if (this->focusProxy()) {
160 this->focusProxy()->removeEventFilter(this);
161 setFocusProxy(0);
162 }
163}
164
165bool
167{
168 return d->leaveTheSpaceForRevertButton;
169}
170
171void
172Widget::setLeavesTheSpaceForRevertButton(bool set)
173{
174 d->leaveTheSpaceForRevertButton = set;
175}
176
177void
178Widget::setHasBorders(bool set)
179{
180 d->hasBorders = set;
181}
182
183bool
185{
186 return d->hasBorders;
187}
188
189void
190Widget::setEditor(TQWidget* editor)
191{
192 d->editor = editor;
193 if (!d->editor)
194 return;
195 d->editor->setSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Fixed);
196 d->editor->move(0,0);
197}
198
199void
200Widget::resizeEvent(TQResizeEvent *e)
201{
202 TQWidget::resizeEvent(e);
203 if (d->editor)
204 d->editor->resize(size());
205}
206
207bool
209{
210 return d->readOnly;
211}
212
213void
215{
216 d->readOnly = readOnly;
217 setReadOnlyInternal(readOnly);
218}
219
220bool
221Widget::visibleFlag() const
222{
223 return d->visibleFlag;
224}
225
226void
228{
229 d->visibleFlag = visible;
230}
231
232#include "widget.moc"
A listview to edit properties Editor widgets use property options using Property::option(const char *...
Definition editor.h:64
The base class representing a single property.
Definition property.h:177
TQVariant value() const
Definition property.cpp:314
virtual void drawViewer(TQPainter *p, const TQColorGroup &cg, const TQRect &r, const TQVariant &value)
Definition widget.cpp:88
bool hasBorders() const
Definition widget.cpp:184
virtual void setValue(const TQVariant &value, bool emitChange=true)=0
virtual Property * property() const
Definition widget.cpp:72
void setVisibleFlag(bool visible)
Definition widget.cpp:227
virtual void setProperty(Property *property)
Definition widget.cpp:78
virtual void undo()
Definition widget.cpp:101
bool isReadOnly() const
Definition widget.cpp:208
void setReadOnly(bool readOnly)
Definition widget.cpp:214
bool leavesTheSpaceForRevertButton() const
Definition widget.cpp:166
virtual bool eventFilter(TQObject *watched, TQEvent *e)
Definition widget.cpp:108
virtual TQVariant value() const =0
void setFocusWidget(TQWidget *focusProxy)
Definition widget.cpp:152
virtual void setReadOnlyInternal(bool readOnly)=0
const char * name(StdAction id)
Namespace for a set of classes implementing generic properties framework.