summaryrefslogtreecommitdiffstats
path: root/lib/koproperty/editors/spinbox.cpp
blob: 8c9dbab9cac66841e4d37d8af84113d19ad3bb05 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* This file is part of the KDE project
   Copyright (C) 2004 Cedric Pasteur <[email protected]>
   Copyright (C) 2004  Alexander Dymo <[email protected]>

   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 "spinbox.h"

#include "property.h"

#include <tqlayout.h>
#include <tqobjectlist.h>
#include <tqvariant.h>
#include <tqpainter.h>
#include <tqlineedit.h>

#include <kglobal.h>
#include <klocale.h>

using namespace KoProperty;

IntSpinBox::IntSpinBox(int lower, int upper, int step, int value, int base, IntEdit *parent, const char *name)
: KIntSpinBox(lower, upper, step, value, base, parent, name)
{
	editor()->setAlignment(TQt::AlignLeft);
	installEventFilter(editor());
	installEventFilter(this);
	TQObjectList *spinwidgets = queryList( TQSPINWIDGET_OBJECT_NAME_STRING, 0, false, true );
	TQSpinWidget* spin = static_cast<TQSpinWidget*>(TQT_TQWIDGET(spinwidgets->first()));
	if (spin)
		spin->installEventFilter(this);
	delete spinwidgets;
}

void IntSpinBox::setValue(const TQVariant &value)
{
	if (dynamic_cast<IntEdit*>(parentWidget()) && dynamic_cast<IntEdit*>(parentWidget())->isReadOnly())
		return;
	if (value.isNull())
		editor()->clear();
	else
		KIntSpinBox::setValue(value.toInt());
}

bool
IntSpinBox::eventFilter(TQObject *o, TQEvent *e)
{
	if(TQT_BASE_OBJECT(o) == editor())
	{
		if(e->type() == TQEvent::KeyPress)
		{
			TQKeyEvent* ev = TQT_TQKEYEVENT(e);
			if((ev->key()==Key_Up || ev->key()==Key_Down) && ev->state() !=ControlButton)
			{
				parentWidget()->eventFilter(o, e);
				return true;
			}
		}
	}
	if ((TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(editor()) || TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(this) || TQT_BASE_OBJECT(o->parent()) == TQT_BASE_OBJECT(this)) 
		&& e->type() == TQEvent::Wheel && static_cast<IntEdit*>(parentWidget())->isReadOnly())
	{
		return true; //avoid value changes for read-only widget
	}

	return KIntSpinBox::eventFilter(o, e);
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

IntEdit::IntEdit(Property *property, TQWidget *parent, const char *name)
 : Widget(property, parent, name)
{
	TQVariant minVal( property ? property->option("min") : 0 );
	TQVariant maxVal( property ? property->option("max") : TQVariant() );
	TQVariant minValueText( property ? property->option("minValueText") : TQVariant() );
	if (minVal.isNull())
		minVal = 0;
	if (maxVal.isNull())
		maxVal = INT_MAX;

	m_edit = new IntSpinBox(minVal.toInt(), maxVal.toInt(), 1, 0, 10, this);
	if (!minValueText.isNull())
		m_edit->setSpecialValueText(minValueText.toString());
	m_edit->setMinimumHeight(5);
	setEditor(m_edit);

	setLeavesTheSpaceForRevertButton(true);
	setFocusWidget(m_edit);
	connect(m_edit, TQT_SIGNAL(valueChanged(int)), this, TQT_SLOT(slotValueChanged(int)));
}

IntEdit::~IntEdit()
{}

TQVariant
IntEdit::value() const
{
	if (m_edit->cleanText().isEmpty())
		return TQVariant();
	return m_edit->value();
}

void
IntEdit::setValue(const TQVariant &value, bool emitChange)
{
	m_edit->blockSignals(true);
	m_edit->setValue(value);
	updateSpinWidgets();
	m_edit->blockSignals(false);
	if (emitChange)
		emit valueChanged(this);
}

void
IntEdit::drawViewer(TQPainter *p, const TQColorGroup &cg, const TQRect &r, const TQVariant &value)
{
	TQString valueText = value.toString();
	if (property() && property()->hasOptions()) {
		//replace min value with minValueText if defined
		TQVariant minValue( property()->option("min") );
		TQVariant minValueText( property()->option("minValueText") );
		if (!minValue.isNull() && !minValueText.isNull() && minValue.toInt() == value.toInt()) {
			valueText = minValueText.toString();
		}
	}

	Widget::drawViewer(p, cg, r, valueText);
//	p->eraseRect(r);
//	p->drawText(r, TQt::AlignLeft | TQt::AlignVCenter | TQt::SingleLine, valueText);
}

void
IntEdit::slotValueChanged(int)
{
	emit valueChanged(this);
}

void
IntEdit::updateSpinWidgets()
{
	TQObjectList *spinwidgets = queryList( TQSPINWIDGET_OBJECT_NAME_STRING, 0, false, true );
	TQSpinWidget* spin = static_cast<TQSpinWidget*>(TQT_TQWIDGET(spinwidgets->first()));
	if (spin) {
		spin->setUpEnabled(!isReadOnly());
		spin->setDownEnabled(!isReadOnly());
	}
	delete spinwidgets;
}

void
IntEdit::setReadOnlyInternal(bool readOnly)
{
	//disable editor and spin widget
	m_edit->editor()->setReadOnly(readOnly);
	updateSpinWidgets();
	if (readOnly)
		setLeavesTheSpaceForRevertButton(false);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

DoubleSpinBox::DoubleSpinBox (double lower, double upper, double step, double value, int precision, DoubleEdit *parent)
: KDoubleSpinBox(lower, upper, step, value, precision, parent)
{
	editor()->setAlignment(TQt::AlignLeft);
	installEventFilter(editor());
	installEventFilter(this);
	TQObjectList *spinwidgets = queryList( TQSPINWIDGET_OBJECT_NAME_STRING, 0, false, true );
	TQSpinWidget* spin = static_cast<TQSpinWidget*>(TQT_TQWIDGET(spinwidgets->first()));
	if (spin)
		spin->installEventFilter(this);
	delete spinwidgets;
}

bool
DoubleSpinBox::eventFilter(TQObject *o, TQEvent *e)
{
	if(TQT_BASE_OBJECT(o) == editor())
	{
		if(e->type() == TQEvent::KeyPress)
		{
			TQKeyEvent* ev = TQT_TQKEYEVENT(e);
			if((ev->key()==Key_Up || ev->key()==Key_Down) && ev->state()!=ControlButton)
			{
				parentWidget()->eventFilter(o, e);
				return true;
			}
		}
	}
	if ((TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(editor()) || TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(this) || TQT_BASE_OBJECT(o->parent()) == TQT_BASE_OBJECT(this)) 
		&& e->type() == TQEvent::Wheel && static_cast<IntEdit*>(parentWidget())->isReadOnly())
	{
		return true; //avoid value changes for read-only widget
	}

	return KDoubleSpinBox::eventFilter(o, e);
}


void DoubleSpinBox::setValue( const TQVariant& value )
{
	if (dynamic_cast<DoubleEdit*>(parentWidget()) && dynamic_cast<DoubleEdit*>(parentWidget())->isReadOnly())
		return;
	if (value.isNull())
		editor()->clear();
	else
		KDoubleSpinBox::setValue(value.toDouble());
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

DoubleEdit::DoubleEdit(Property *property, TQWidget *parent, const char *name)
 : Widget(property, parent, name)
{
	TQVariant minVal( property ? property->option("min") : 0 );
	TQVariant maxVal( property ? property->option("max") : TQVariant() );
	TQVariant step( property ? property->option("step") : TQVariant());
	TQVariant precision( property ? property->option("precision") : TQVariant());
	TQVariant minValueText( property ? property->option("minValueText") : TQVariant() );
	if (minVal.isNull())
		minVal = 0;
	if (maxVal.isNull())
		maxVal = (double)(INT_MAX/100);
	if(step.isNull())
		step = 0.1;
	if(precision.isNull())
		precision = 2;

	m_edit = new DoubleSpinBox(minVal.toDouble(), maxVal.toDouble(), step.toDouble(),
		 0, precision.toInt(), this);
	if (!minValueText.isNull())
		m_edit->setSpecialValueText(minValueText.toString());
	m_edit->setSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Expanding);
	m_edit->setMinimumHeight(5);
	setEditor(m_edit);

	setLeavesTheSpaceForRevertButton(true);
	setFocusWidget(m_edit);
	connect(m_edit, TQT_SIGNAL(valueChanged(double)), this, TQT_SLOT(slotValueChanged(double)));
}

DoubleEdit::~DoubleEdit()
{}

TQVariant
DoubleEdit::value() const
{
	if (m_edit->cleanText().isEmpty())
		return TQVariant();
	return m_edit->value();
}

void
DoubleEdit::setValue(const TQVariant &value, bool emitChange)
{
	m_edit->blockSignals(true);
	m_edit->setValue(value);
	updateSpinWidgets();
	m_edit->blockSignals(false);
	if (emitChange)
		emit valueChanged(this);
}

void
DoubleEdit::drawViewer(TQPainter *p, const TQColorGroup &cg, const TQRect &r, const TQVariant &value)
{
	TQString valueText;
	if (property() && property()->hasOptions()) {
		//replace min value with minValueText if defined
		TQVariant minValue( property()->option("min") );
		TQVariant minValueText( property()->option("minValueText") );
		if (!minValue.isNull() && !minValueText.isNull() && minValue.toString().toDouble() == value.toString().toDouble()) {
			valueText = minValueText.toString();
		}
	}
	if (valueText.isEmpty())
		valueText = TQString(value.toString()).replace('.', KGlobal::locale()->decimalSymbol());

	Widget::drawViewer(p, cg, r, valueText);
//	p->eraseRect(r);
//	p->drawText(r, TQt::AlignLeft | TQt::AlignVCenter | TQt::SingleLine, valueText);
}

void
DoubleEdit::slotValueChanged(double)
{
	emit valueChanged(this);
}

void
DoubleEdit::updateSpinWidgets()
{
	TQObjectList *spinwidgets = queryList( TQSPINWIDGET_OBJECT_NAME_STRING, 0, false, true );
	TQSpinWidget* spin = static_cast<TQSpinWidget*>(TQT_TQWIDGET(spinwidgets->first()));
	if (spin) {
		spin->setUpEnabled(!isReadOnly());
		spin->setDownEnabled(!isReadOnly());
	}
	delete spinwidgets;
}

void
DoubleEdit::setReadOnlyInternal(bool readOnly)
{
	//disable editor and spin widget
	m_edit->editor()->setReadOnly(readOnly);
	updateSpinWidgets();
	if (readOnly)
		setLeavesTheSpaceForRevertButton(false);
}

#include "spinbox.moc"