summaryrefslogtreecommitdiffstats
path: root/lib/koproperty/widget.cpp
blob: 2028e75cdb1e6b691c2ae41995756a0cb7a31d0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* This file is part of the KDE project
   Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
   Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "widget.h"
#include "property.h"
#include "editoritem.h"
#include "editor.h"

#include <tqpainter.h>
#include <tqvariant.h>

#include <tdelistview.h>
#include <kdebug.h>

using namespace KoProperty;

namespace KoProperty {
class WidgetPrivate
{
	public:
		WidgetPrivate()
		: property(0)
		, editor(0)
		, leaveTheSpaceForRevertButton(false)
		, hasBorders(true)
		, readOnly(false)
		, visibleFlag(true)
		{
		}
		~WidgetPrivate() {}

		Property *property;
		TQWidget *editor;
		bool leaveTheSpaceForRevertButton : 1;
		bool hasBorders : 1;
		bool readOnly : 1;
		bool visibleFlag : 1;
};
}

Widget::Widget(Property *property, TQWidget *parent, const char *name)
 : TQWidget(parent, name)
{
	d = new WidgetPrivate();
	d->property = property;
}

Widget::~Widget()
{
	delete d;
	d = 0;
}

Property*
Widget::property() const
{
	return d ? d->property : 0; //for sanity
}

void
Widget::setProperty(Property *property)
{
	d->property = property;
	if(property)
		setValue(property->value(), false);
	//if(property->type() == ValueFromList)
	//	setValueList(property->valueList());
}

void
Widget::drawViewer(TQPainter *p, const TQColorGroup &, const TQRect &r, const TQVariant &value)
{
	p->eraseRect(r);
	TQRect rect(r);
	rect.setLeft(rect.left()+KPROPEDITOR_ITEM_MARGIN);
//	if (d->hasBorders)
//		rect.setTop(rect.top()+1); //+1 to have the same vertical position as editor
//	else
//		rect.setHeight(rect.height()-1); //don't place over listviews's border
	p->drawText(rect, TQt::AlignLeft | TQt::AlignVCenter | TQt::SingleLine, value.toString());
}

void
Widget::undo()
{
	if(d->property)
		d->property->resetValue();
}

bool
Widget::eventFilter(TQObject*, TQEvent* e)
{
	if(e->type() == TQEvent::KeyPress)
	{
		TQKeyEvent* ev = TQT_TQKEYEVENT(e);
		if(ev->key() == Key_Escape)
		{
			emit rejectInput(this);
			return true;
		}
		else if((ev->key() == Key_Return) || (ev->key() == Key_Enter))
		{
			// should apply when autosync == false
			emit acceptInput(this);
			return true;
		}
		else {
			Editor *list = static_cast<KoProperty::Editor*>(parentWidget()->parentWidget());
			if (!list)
				return false; //for sanity
			return list->handleKeyPress(ev);
		}

		/* moved in Editor
		if (item) {
			if(ev->key() == Key_Up && ev->state() != ControlButton)
			{
				if(item->itemAbove())
					list->setCurrentItem(item->itemAbove());
				return true;
			}
			else if(ev->key() == Key_Down && ev->state() != ControlButton)
			{
				if(item->itemBelow())
					list->setCurrentItem(item->itemBelow());
				return true;
			}
		}*/
	}

	return false;
}

void
Widget::setFocusWidget(TQWidget*focusProxy)
{
	if (focusProxy) {
		if (focusProxy->focusPolicy() != TQ_NoFocus)
			setFocusProxy(focusProxy);
		focusProxy->installEventFilter(this);
	}
	else if (this->focusProxy()) {
		this->focusProxy()->removeEventFilter(this);
		setFocusProxy(0);
	}
}

bool
Widget::leavesTheSpaceForRevertButton() const
{
	return d->leaveTheSpaceForRevertButton;
}

void
Widget::setLeavesTheSpaceForRevertButton(bool set)
{
	d->leaveTheSpaceForRevertButton = set;
}

void
Widget::setHasBorders(bool set)
{
	d->hasBorders = set;
}

bool
Widget::hasBorders() const
{
	return d->hasBorders;
}

void
Widget::setEditor(TQWidget* editor)
{
	d->editor = editor;
	if (!d->editor)
		return;
	d->editor->setSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Fixed);
	d->editor->move(0,0);
}

void
Widget::resizeEvent(TQResizeEvent *e)
{
	TQWidget::resizeEvent(e);
	if (d->editor)
		d->editor->resize(size());
}

bool
Widget::isReadOnly() const
{
	return d->readOnly;
}

void
Widget::setReadOnly(bool readOnly)
{
	d->readOnly = readOnly;
	setReadOnlyInternal(readOnly);
}

bool 
Widget::visibleFlag() const
{
	return d->visibleFlag;
}

void
Widget::setVisibleFlag(bool visible)
{
	d->visibleFlag = visible;
}

#include "widget.moc"