00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
#include "droptionview.h"
00021
#include "driver.h"
00022
#include "driveritem.h"
00023
00024
#include <math.h>
00025
#include <qlineedit.h>
00026
#include <qslider.h>
00027
#include <qlabel.h>
00028
#include <klistbox.h>
00029
#include <qvbuttongroup.h>
00030
#include <qradiobutton.h>
00031
#include <qwidgetstack.h>
00032
#include <qlayout.h>
00033
#include <qapplication.h>
00034
00035
#include <kcursor.h>
00036
#include <klocale.h>
00037
00038 OptionBaseView::OptionBaseView(
QWidget *parent,
const char *name)
00039 :
QWidget(parent,name)
00040 {
00041 blockSS =
false;
00042 }
00043
00044
void OptionBaseView::setOption(DrBase*)
00045 {
00046 }
00047
00048
void OptionBaseView::setValue(
const QString&)
00049 {
00050 }
00051
00052
00053
00054 OptionNumericView::OptionNumericView(
QWidget *parent,
const char *name)
00055 : OptionBaseView(parent,name)
00056 {
00057 m_edit =
new QLineEdit(
this);
00058 m_slider =
new QSlider(Qt::Horizontal,
this);
00059 m_slider->setTickmarks(QSlider::Below);
00060
QLabel *lab =
new QLabel(i18n(
"Value:"),
this);
00061 m_minval =
new QLabel(
this);
00062 m_maxval =
new QLabel(
this);
00063
00064 m_integer =
true;
00065
00066
QVBoxLayout *main_ =
new QVBoxLayout(
this, 0, 10);
00067
QHBoxLayout *sub_ =
new QHBoxLayout(0, 0, 10);
00068
QHBoxLayout *sub2_ =
new QHBoxLayout(0, 0, 5);
00069 main_->addStretch(1);
00070 main_->addLayout(sub_,0);
00071 main_->addLayout(sub2_,0);
00072 main_->addStretch(1);
00073 sub_->addWidget(lab,0);
00074 sub_->addWidget(m_edit,0);
00075 sub_->addStretch(1);
00076 sub2_->addWidget(m_minval,0);
00077 sub2_->addWidget(m_slider,1);
00078 sub2_->addWidget(m_maxval,0);
00079
00080 connect(m_slider,SIGNAL(valueChanged(
int)),SLOT(slotSliderChanged(
int)));
00081 connect(m_edit,SIGNAL(textChanged(
const QString&)),SLOT(slotEditChanged(
const QString&)));
00082 }
00083
00084
void OptionNumericView::setOption(DrBase *opt)
00085 {
00086
if (opt->type() != DrBase::Integer && opt->type() != DrBase::Float)
00087
return;
00088
00089 blockSS =
true;
00090
if (opt->type() == DrBase::Integer)
00091 {
00092 m_integer =
true;
00093
int min_ = opt->get(
"minval").toInt();
00094
int max_ = opt->get(
"maxval").toInt();
00095 m_slider->setRange(min_,max_);
00096 m_slider->setSteps(1,QMAX((max_-min_)/20,1));
00097 m_minval->setText(QString::number(min_));
00098 m_maxval->setText(QString::number(max_));
00099 }
00100
else
00101 {
00102 m_integer =
false;
00103
int min_ = (
int)rint(opt->get(
"minval").toFloat()*1000);
00104
int max_ = (
int)rint(opt->get(
"maxval").toFloat()*1000);
00105 m_slider->setRange(min_,max_);
00106 m_slider->setSteps(1,QMAX((max_-min_)/20,1));
00107 m_minval->setText(opt->get(
"minval"));
00108 m_maxval->setText(opt->get(
"maxval"));
00109 }
00110 m_slider->update();
00111 blockSS =
false;
00112
00113 setValue(opt->valueText());
00114 }
00115
00116
void OptionNumericView::setValue(
const QString& val)
00117 {
00118 m_edit->setText(val);
00119 }
00120
00121
void OptionNumericView::slotSliderChanged(
int value)
00122 {
00123
if (blockSS)
return;
00124
00125
QString txt;
00126
if (m_integer)
00127 txt = QString::number(value);
00128
else
00129 txt = QString::number(
float(value)/1000.0,
'f',3);
00130 blockSS =
true;
00131 m_edit->setText(txt);
00132 blockSS =
false;
00133 emit valueChanged(txt);
00134 }
00135
00136
void OptionNumericView::slotEditChanged(
const QString& txt)
00137 {
00138
if (blockSS)
return;
00139
00140
bool ok(
false);
00141
int val(0);
00142
if (m_integer)
00143 val = txt.toInt(&ok);
00144
else
00145 val = (
int)rint(txt.toFloat(&ok)*1000);
00146
if (ok)
00147 {
00148 blockSS =
true;
00149 m_slider->setValue(val);
00150 blockSS =
false;
00151 emit valueChanged(txt);
00152 }
00153
else
00154 {
00155 m_edit->selectAll();
00156 QApplication::beep();
00157 }
00158 }
00159
00160
00161
00162 OptionStringView::OptionStringView(
QWidget *parent,
const char *name)
00163 : OptionBaseView(parent,name)
00164 {
00165 m_edit =
new QLineEdit(
this);
00166
QLabel *lab =
new QLabel(i18n(
"String value:"),
this);
00167
00168
QVBoxLayout *main_ =
new QVBoxLayout(
this, 0, 5);
00169 main_->addStretch(1);
00170 main_->addWidget(lab,0);
00171 main_->addWidget(m_edit,0);
00172 main_->addStretch(1);
00173
00174 connect(m_edit,SIGNAL(textChanged(
const QString&)),SIGNAL(valueChanged(
const QString&)));
00175 }
00176
00177
void OptionStringView::setOption(DrBase *opt)
00178 {
00179
if (opt->type() == DrBase::String)
00180 m_edit->setText(opt->valueText());
00181 }
00182
00183
void OptionStringView::setValue(
const QString& val)
00184 {
00185 m_edit->setText(val);
00186 }
00187
00188
00189
00190 OptionListView::OptionListView(
QWidget *parent,
const char *name)
00191 : OptionBaseView(parent,name)
00192 {
00193 m_list =
new KListBox(
this);
00194
00195
QVBoxLayout *main_ =
new QVBoxLayout(
this, 0, 10);
00196 main_->addWidget(m_list);
00197
00198 connect(m_list,SIGNAL(selectionChanged()),SLOT(slotSelectionChanged()));
00199 }
00200
00201
void OptionListView::setOption(DrBase *opt)
00202 {
00203
if (opt->type() == DrBase::List)
00204 {
00205 blockSS =
true;
00206 m_list->clear();
00207 m_choices.clear();
00208
QPtrListIterator<DrBase> it(*(((DrListOption*)opt)->choices()));
00209
for (;it.current();++it)
00210 {
00211 m_list->insertItem(it.current()->get(
"text"));
00212 m_choices.append(it.current()->name());
00213 }
00214 blockSS =
false;
00215 setValue(opt->valueText());
00216 }
00217 }
00218
00219
void OptionListView::setValue(
const QString& val)
00220 {
00221 m_list->setCurrentItem(m_choices.findIndex(val));
00222 }
00223
00224
void OptionListView::slotSelectionChanged()
00225 {
00226
if (blockSS)
return;
00227
00228
QString s = m_choices[m_list->currentItem()];
00229 emit valueChanged(s);
00230 }
00231
00232
00233
00234 OptionBooleanView::OptionBooleanView(
QWidget *parent,
const char *name)
00235 : OptionBaseView(parent,name)
00236 {
00237 m_group =
new QVButtonGroup(
this);
00238 m_group->setFrameStyle(QFrame::NoFrame);
00239
00240
QRadioButton *btn =
new QRadioButton(m_group);
00241 btn->setCursor(KCursor::handCursor());
00242 btn =
new QRadioButton(m_group);
00243 btn->setCursor(KCursor::handCursor());
00244
00245
QVBoxLayout *main_ =
new QVBoxLayout(
this, 0, 10);
00246 main_->addWidget(m_group);
00247
00248 connect(m_group,SIGNAL(clicked(
int)),SLOT(slotSelected(
int)));
00249 }
00250
00251
void OptionBooleanView::setOption(DrBase *opt)
00252 {
00253
if (opt->type() == DrBase::Boolean)
00254 {
00255
QPtrListIterator<DrBase> it(*(((DrBooleanOption*)opt)->choices()));
00256 m_choices.clear();
00257 m_group->find(0)->setText(it.toFirst()->get(
"text"));
00258 m_choices.append(it.toFirst()->name());
00259 m_group->find(1)->setText(it.toLast()->get(
"text"));
00260 m_choices.append(it.toLast()->name());
00261 setValue(opt->valueText());
00262 }
00263 }
00264
00265
void OptionBooleanView::setValue(
const QString& val)
00266 {
00267
int ID = m_choices.findIndex(val);
00268 m_group->setButton(ID);
00269 }
00270
00271
void OptionBooleanView::slotSelected(
int ID)
00272 {
00273
QString s = m_choices[ID];
00274 emit valueChanged(s);
00275 }
00276
00277
00278
00279 DrOptionView::DrOptionView(
QWidget *parent,
const char *name)
00280 :
QGroupBox(parent,name)
00281 {
00282
00283 m_stack =
new QWidgetStack(
this);
00284
00285 OptionBaseView *w =
new OptionListView(m_stack);
00286 connect(w,SIGNAL(valueChanged(
const QString&)),SLOT(slotValueChanged(
const QString&)));
00287 m_stack->addWidget(w,DrBase::List);
00288
00289 w =
new OptionStringView(m_stack);
00290 connect(w,SIGNAL(valueChanged(
const QString&)),SLOT(slotValueChanged(
const QString&)));
00291 m_stack->addWidget(w,DrBase::String);
00292
00293 w =
new OptionNumericView(m_stack);
00294 connect(w,SIGNAL(valueChanged(
const QString&)),SLOT(slotValueChanged(
const QString&)));
00295 m_stack->addWidget(w,DrBase::Integer);
00296
00297 w =
new OptionBooleanView(m_stack);
00298 connect(w,SIGNAL(valueChanged(
const QString&)),SLOT(slotValueChanged(
const QString&)));
00299 m_stack->addWidget(w,DrBase::Boolean);
00300
00301 w =
new OptionBaseView(m_stack);
00302 connect(w,SIGNAL(valueChanged(
const QString&)),SLOT(slotValueChanged(
const QString&)));
00303 m_stack->addWidget(w,0);
00304
00305 m_stack->raiseWidget(w);
00306 setTitle(i18n(
"No Option Selected"));
00307
00308
QVBoxLayout *main_ =
new QVBoxLayout(
this, 10, 10);
00309 main_->addSpacing(10);
00310 main_->addWidget(m_stack);
00311
00312 m_item = 0;
00313 m_block =
false;
00314 m_allowfixed =
true;
00315 }
00316
00317
void DrOptionView::slotItemSelected(
QListViewItem *i)
00318 {
00319 m_item = (DriverItem*)i;
00320
if (m_item && !m_item->drItem()->isOption())
00321 m_item = 0;
00322
int ID(0);
00323
if (m_item)
00324
if (m_item->drItem()->type() == DrBase::Float) ID = DrBase::Integer;
00325
else ID = m_item->drItem()->type();
00326
00327 OptionBaseView *w = (OptionBaseView*)m_stack->widget(ID);
00328
if (w)
00329 {
00330 m_block =
true;
00331
bool enabled(
true);
00332
if (m_item)
00333 {
00334 w->setOption((m_item ? m_item->drItem() : 0));
00335 setTitle(m_item->drItem()->get(
"text"));
00336 enabled = ((m_item->drItem()->get(
"fixed") !=
"1") || m_allowfixed);
00337 }
00338
else
00339 setTitle(i18n(
"No Option Selected"));
00340 m_stack->raiseWidget(w);
00341 w->setEnabled(enabled);
00342 m_block =
false;
00343 }
00344 }
00345
00346
void DrOptionView::slotValueChanged(
const QString& val)
00347 {
00348
if (m_item && m_item->drItem() && !m_block)
00349 {
00350 m_item->drItem()->setValueText(val);
00351 m_item->updateText();
00352 emit changed();
00353 }
00354 }
00355
00356
QSize DrOptionView::sizeHint()
const
00357
{
00358
return QSize(200,140);
00359 }
00360
#include "droptionview.moc"