summaryrefslogtreecommitdiffstats
path: root/tdeprint/droptionview.cpp
blob: cb8057f66ef1b217a92734ccd1a118a7dbc362dd (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 *  This file is part of the KDE libraries
 *  Copyright (c) 2001 Michael Goffioul <[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 version 2 as published by the Free Software Foundation.
 *
 *  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 "droptionview.h"
#include "driver.h"
#include "driveritem.h"

#include <math.h>
#include <tqlineedit.h>
#include <tqslider.h>
#include <tqlabel.h>
#include <tdelistbox.h>
#include <tqvbuttongroup.h>
#include <tqradiobutton.h>
#include <tqwidgetstack.h>
#include <tqlayout.h>
#include <tqapplication.h>

#include <kcursor.h>
#include <kdialog.h>
#include <tdelocale.h>

OptionBaseView::OptionBaseView(TQWidget *parent, const char *name)
: TQWidget(parent,name)
{
	blockSS = false;
}

void OptionBaseView::setOption(DrBase*)
{
}

void OptionBaseView::setValue(const TQString&)
{
}

//******************************************************************************************************

OptionNumericView::OptionNumericView(TQWidget *parent, const char *name)
: OptionBaseView(parent,name)
{
	m_edit = new TQLineEdit(this);
	m_slider = new TQSlider(TQt::Horizontal,this);
	m_slider->setTickmarks(TQSlider::Below);
	TQLabel	*lab = new TQLabel(i18n("Value:"),this);
	m_minval = new TQLabel(this);
	m_maxval = new TQLabel(this);

	m_integer = true;

	TQVBoxLayout	*main_ = new TQVBoxLayout(this, 0, 10);
	TQHBoxLayout	*sub_ = new TQHBoxLayout(0, 0, 10);
	TQHBoxLayout	*sub2_ = new TQHBoxLayout(0, 0, 5);
	main_->addStretch(1);
	main_->addLayout(sub_,0);
	main_->addLayout(sub2_,0);
	main_->addStretch(1);
	sub_->addWidget(lab,0);
	sub_->addWidget(m_edit,0);
	sub_->addStretch(1);
	sub2_->addWidget(m_minval,0);
	sub2_->addWidget(m_slider,1);
	sub2_->addWidget(m_maxval,0);

	connect(m_slider,TQT_SIGNAL(valueChanged(int)),TQT_SLOT(slotSliderChanged(int)));
	connect(m_edit,TQT_SIGNAL(textChanged(const TQString&)),TQT_SLOT(slotEditChanged(const TQString&)));
}

void OptionNumericView::setOption(DrBase *opt)
{
	if (opt->type() != DrBase::Integer && opt->type() != DrBase::Float)
		return;

	blockSS = true;
	if (opt->type() == DrBase::Integer)
	{
		m_integer = true;
		int	min_ = opt->get("minval").toInt();
		int	max_ = opt->get("maxval").toInt();
		m_slider->setRange(min_,max_);
		m_slider->setSteps(1,TQMAX((max_-min_)/20,1));
		m_minval->setText(TQString::number(min_));
		m_maxval->setText(TQString::number(max_));
	}
	else
	{
		m_integer = false;
		int	min_ = (int)rint(opt->get("minval").toFloat()*1000);
		int	max_ = (int)rint(opt->get("maxval").toFloat()*1000);
		m_slider->setRange(min_,max_);
		m_slider->setSteps(1,TQMAX((max_-min_)/20,1));
		m_minval->setText(opt->get("minval"));
		m_maxval->setText(opt->get("maxval"));
	}
	m_slider->update();
	blockSS = false;

	setValue(opt->valueText());
}

void OptionNumericView::setValue(const TQString& val)
{
	m_edit->setText(val);
}

void OptionNumericView::slotSliderChanged(int value)
{
	if (blockSS) return;

	TQString	txt;
	if (m_integer)
		txt = TQString::number(value);
	else
		txt = TQString::number(float(value)/1000.0,'f',3);
	blockSS = true;
	m_edit->setText(txt);
	blockSS = false;
	emit valueChanged(txt);
}

void OptionNumericView::slotEditChanged(const TQString& txt)
{
	if (blockSS) return;

	bool	ok(false);
	int	val(0);
	if (m_integer)
		val = txt.toInt(&ok);
	else
		val = (int)rint(txt.toFloat(&ok)*1000);
	if (ok)
	{
		blockSS = true;
		m_slider->setValue(val);
		blockSS = false;
		emit valueChanged(txt);
	}
	else
	{
		m_edit->selectAll();
		TQApplication::beep();
	}
}

//******************************************************************************************************

OptionStringView::OptionStringView(TQWidget *parent, const char *name)
: OptionBaseView(parent,name)
{
	m_edit = new TQLineEdit(this);
	TQLabel	*lab = new TQLabel(i18n("String value:"),this);

	TQVBoxLayout	*main_ = new TQVBoxLayout(this, 0, 5);
	main_->addStretch(1);
	main_->addWidget(lab,0);
	main_->addWidget(m_edit,0);
	main_->addStretch(1);

	connect(m_edit,TQT_SIGNAL(textChanged(const TQString&)),TQT_SIGNAL(valueChanged(const TQString&)));
}

void OptionStringView::setOption(DrBase *opt)
{
	if (opt->type() == DrBase::String)
		m_edit->setText(opt->valueText());
}

void OptionStringView::setValue(const TQString& val)
{
	m_edit->setText(val);
}

//******************************************************************************************************

OptionListView::OptionListView(TQWidget *parent, const char *name)
: OptionBaseView(parent,name)
{
	m_list = new TDEListBox(this);

	TQVBoxLayout	*main_ = new TQVBoxLayout(this, 0, 10);
	main_->addWidget(m_list);

	connect(m_list,TQT_SIGNAL(selectionChanged()),TQT_SLOT(slotSelectionChanged()));
}

void OptionListView::setOption(DrBase *opt)
{
	if (opt->type() == DrBase::List)
	{
		blockSS = true;
		m_list->clear();
		m_choices.clear();
		TQPtrListIterator<DrBase>	it(*(((DrListOption*)opt)->choices()));
		for (;it.current();++it)
		{
			m_list->insertItem(it.current()->get("text"));
			m_choices.append(it.current()->name());
		}
		blockSS = false;
		setValue(opt->valueText());
	}
}

void OptionListView::setValue(const TQString& val)
{
	m_list->setCurrentItem(m_choices.findIndex(val));
}

void OptionListView::slotSelectionChanged()
{
	if (blockSS) return;

	TQString	s = m_choices[m_list->currentItem()];
	emit valueChanged(s);
}

//******************************************************************************************************

OptionBooleanView::OptionBooleanView(TQWidget *parent, const char *name)
: OptionBaseView(parent,name)
{
	m_group = new TQVButtonGroup(this);
	m_group->setFrameStyle(TQFrame::NoFrame);

	TQRadioButton	*btn = new TQRadioButton(m_group);
	btn->setCursor(KCursor::handCursor());
	btn = new TQRadioButton(m_group);
	btn->setCursor(KCursor::handCursor());

	TQVBoxLayout	*main_ = new TQVBoxLayout(this, 0, 10);
	main_->addWidget(m_group);

	connect(m_group,TQT_SIGNAL(clicked(int)),TQT_SLOT(slotSelected(int)));
}

void OptionBooleanView::setOption(DrBase *opt)
{
	if (opt->type() == DrBase::Boolean)
	{
		TQPtrListIterator<DrBase>	it(*(((DrBooleanOption*)opt)->choices()));
		m_choices.clear();
		static_cast<TQButton*>(m_group->find(0))->setText(it.toFirst()->get("text"));
		m_choices.append(it.toFirst()->name());
		static_cast<TQButton*>(m_group->find(1))->setText(it.toLast()->get("text"));
		m_choices.append(it.toLast()->name());
		setValue(opt->valueText());
	}
}

void OptionBooleanView::setValue(const TQString& val)
{
	int	ID = m_choices.findIndex(val);
	m_group->setButton(ID);
}

void OptionBooleanView::slotSelected(int ID)
{
	TQString	s = m_choices[ID];
	emit valueChanged(s);
}

//******************************************************************************************************

DrOptionView::DrOptionView(TQWidget *parent, const char *name)
: TQGroupBox(parent,name)
{
	m_stack = new TQWidgetStack(this);

	OptionBaseView	*w = new OptionListView(m_stack);
	connect(w,TQT_SIGNAL(valueChanged(const TQString&)),TQT_SLOT(slotValueChanged(const TQString&)));
	m_stack->addWidget(w,DrBase::List);

	w = new OptionStringView(m_stack);
	connect(w,TQT_SIGNAL(valueChanged(const TQString&)),TQT_SLOT(slotValueChanged(const TQString&)));
	m_stack->addWidget(w,DrBase::String);

	w = new OptionNumericView(m_stack);
	connect(w,TQT_SIGNAL(valueChanged(const TQString&)),TQT_SLOT(slotValueChanged(const TQString&)));
	m_stack->addWidget(w,DrBase::Integer);

	w = new OptionBooleanView(m_stack);
	connect(w,TQT_SIGNAL(valueChanged(const TQString&)),TQT_SLOT(slotValueChanged(const TQString&)));
	m_stack->addWidget(w,DrBase::Boolean);

	w = new OptionBaseView(m_stack);
	connect(w,TQT_SIGNAL(valueChanged(const TQString&)),TQT_SLOT(slotValueChanged(const TQString&)));
	m_stack->addWidget(w,0);	// empty widget

	m_stack->raiseWidget(w);
	setTitle(i18n("No Option Selected"));

	setColumnLayout(0, TQt::Vertical );
	layout()->setSpacing( KDialog::spacingHint() );
	layout()->setMargin( KDialog::marginHint() );
	TQVBoxLayout	*main_ = new TQVBoxLayout(TQT_TQLAYOUT(layout()), KDialog::marginHint());
	main_->addWidget(m_stack);

	m_item = 0;
	m_block = false;
	m_allowfixed = true;
}

void DrOptionView::slotItemSelected(TQListViewItem *i)
{
	m_item = (DriverItem*)i;
	if (m_item && !m_item->drItem()->isOption())
		m_item = 0;
	int	ID(0);
	if (m_item)
		if (m_item->drItem()->type() == DrBase::Float) ID = DrBase::Integer;
		else ID = m_item->drItem()->type();

	OptionBaseView	*w = (OptionBaseView*)m_stack->widget(ID);
	if (w)
	{
		m_block = true;
		bool 	enabled(true);
		if (m_item)
		{
			w->setOption((m_item ? m_item->drItem() : 0));
			setTitle(m_item->drItem()->get("text"));
			enabled = ((m_item->drItem()->get("fixed") != "1") || m_allowfixed);
		}
		else
			setTitle(i18n("No Option Selected"));
		m_stack->raiseWidget(w);
		w->setEnabled(enabled);
		m_block = false;
	}
}

void DrOptionView::slotValueChanged(const TQString& val)
{
	if (m_item && m_item->drItem() && !m_block)
	{
		m_item->drItem()->setValueText(val);
		m_item->updateText();
		emit changed();
	}
}

#include "droptionview.moc"