droptionview.cpp
00001 /* 00002 * This file is part of the KDE libraries 00003 * Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License version 2 as published by the Free Software Foundation. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Library General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Library General Public License 00015 * along with this library; see the file COPYING.LIB. If not, write to 00016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 * Boston, MA 02110-1301, USA. 00018 **/ 00019 00020 #include "droptionview.h" 00021 #include "driver.h" 00022 #include "driveritem.h" 00023 00024 #include <math.h> 00025 #include <tqlineedit.h> 00026 #include <tqslider.h> 00027 #include <tqlabel.h> 00028 #include <tdelistbox.h> 00029 #include <tqvbuttongroup.h> 00030 #include <tqradiobutton.h> 00031 #include <tqwidgetstack.h> 00032 #include <tqlayout.h> 00033 #include <tqapplication.h> 00034 00035 #include <kcursor.h> 00036 #include <kdialog.h> 00037 #include <tdelocale.h> 00038 00039 OptionBaseView::OptionBaseView(TQWidget *parent, const char *name) 00040 : TQWidget(parent,name) 00041 { 00042 blockSS = false; 00043 } 00044 00045 void OptionBaseView::setOption(DrBase*) 00046 { 00047 } 00048 00049 void OptionBaseView::setValue(const TQString&) 00050 { 00051 } 00052 00053 //****************************************************************************************************** 00054 00055 OptionNumericView::OptionNumericView(TQWidget *parent, const char *name) 00056 : OptionBaseView(parent,name) 00057 { 00058 m_edit = new TQLineEdit(this); 00059 m_slider = new TQSlider(Qt::Horizontal,this); 00060 m_slider->setTickmarks(TQSlider::Below); 00061 TQLabel *lab = new TQLabel(i18n("Value:"),this); 00062 m_minval = new TQLabel(this); 00063 m_maxval = new TQLabel(this); 00064 00065 m_integer = true; 00066 00067 TQVBoxLayout *main_ = new TQVBoxLayout(this, 0, 10); 00068 TQHBoxLayout *sub_ = new TQHBoxLayout(0, 0, 10); 00069 TQHBoxLayout *sub2_ = new TQHBoxLayout(0, 0, 5); 00070 main_->addStretch(1); 00071 main_->addLayout(sub_,0); 00072 main_->addLayout(sub2_,0); 00073 main_->addStretch(1); 00074 sub_->addWidget(lab,0); 00075 sub_->addWidget(m_edit,0); 00076 sub_->addStretch(1); 00077 sub2_->addWidget(m_minval,0); 00078 sub2_->addWidget(m_slider,1); 00079 sub2_->addWidget(m_maxval,0); 00080 00081 connect(m_slider,TQT_SIGNAL(valueChanged(int)),TQT_SLOT(slotSliderChanged(int))); 00082 connect(m_edit,TQT_SIGNAL(textChanged(const TQString&)),TQT_SLOT(slotEditChanged(const TQString&))); 00083 } 00084 00085 void OptionNumericView::setOption(DrBase *opt) 00086 { 00087 if (opt->type() != DrBase::Integer && opt->type() != DrBase::Float) 00088 return; 00089 00090 blockSS = true; 00091 if (opt->type() == DrBase::Integer) 00092 { 00093 m_integer = true; 00094 int min_ = opt->get("minval").toInt(); 00095 int max_ = opt->get("maxval").toInt(); 00096 m_slider->setRange(min_,max_); 00097 m_slider->setSteps(1,TQMAX((max_-min_)/20,1)); 00098 m_minval->setText(TQString::number(min_)); 00099 m_maxval->setText(TQString::number(max_)); 00100 } 00101 else 00102 { 00103 m_integer = false; 00104 int min_ = (int)rint(opt->get("minval").toFloat()*1000); 00105 int max_ = (int)rint(opt->get("maxval").toFloat()*1000); 00106 m_slider->setRange(min_,max_); 00107 m_slider->setSteps(1,TQMAX((max_-min_)/20,1)); 00108 m_minval->setText(opt->get("minval")); 00109 m_maxval->setText(opt->get("maxval")); 00110 } 00111 m_slider->update(); 00112 blockSS = false; 00113 00114 setValue(opt->valueText()); 00115 } 00116 00117 void OptionNumericView::setValue(const TQString& val) 00118 { 00119 m_edit->setText(val); 00120 } 00121 00122 void OptionNumericView::slotSliderChanged(int value) 00123 { 00124 if (blockSS) return; 00125 00126 QString txt; 00127 if (m_integer) 00128 txt = TQString::number(value); 00129 else 00130 txt = TQString::number(float(value)/1000.0,'f',3); 00131 blockSS = true; 00132 m_edit->setText(txt); 00133 blockSS = false; 00134 emit valueChanged(txt); 00135 } 00136 00137 void OptionNumericView::slotEditChanged(const TQString& txt) 00138 { 00139 if (blockSS) return; 00140 00141 bool ok(false); 00142 int val(0); 00143 if (m_integer) 00144 val = txt.toInt(&ok); 00145 else 00146 val = (int)rint(txt.toFloat(&ok)*1000); 00147 if (ok) 00148 { 00149 blockSS = true; 00150 m_slider->setValue(val); 00151 blockSS = false; 00152 emit valueChanged(txt); 00153 } 00154 else 00155 { 00156 m_edit->selectAll(); 00157 TQApplication::beep(); 00158 } 00159 } 00160 00161 //****************************************************************************************************** 00162 00163 OptionStringView::OptionStringView(TQWidget *parent, const char *name) 00164 : OptionBaseView(parent,name) 00165 { 00166 m_edit = new TQLineEdit(this); 00167 TQLabel *lab = new TQLabel(i18n("String value:"),this); 00168 00169 TQVBoxLayout *main_ = new TQVBoxLayout(this, 0, 5); 00170 main_->addStretch(1); 00171 main_->addWidget(lab,0); 00172 main_->addWidget(m_edit,0); 00173 main_->addStretch(1); 00174 00175 connect(m_edit,TQT_SIGNAL(textChanged(const TQString&)),TQT_SIGNAL(valueChanged(const TQString&))); 00176 } 00177 00178 void OptionStringView::setOption(DrBase *opt) 00179 { 00180 if (opt->type() == DrBase::String) 00181 m_edit->setText(opt->valueText()); 00182 } 00183 00184 void OptionStringView::setValue(const TQString& val) 00185 { 00186 m_edit->setText(val); 00187 } 00188 00189 //****************************************************************************************************** 00190 00191 OptionListView::OptionListView(TQWidget *parent, const char *name) 00192 : OptionBaseView(parent,name) 00193 { 00194 m_list = new TDEListBox(this); 00195 00196 TQVBoxLayout *main_ = new TQVBoxLayout(this, 0, 10); 00197 main_->addWidget(m_list); 00198 00199 connect(m_list,TQT_SIGNAL(selectionChanged()),TQT_SLOT(slotSelectionChanged())); 00200 } 00201 00202 void OptionListView::setOption(DrBase *opt) 00203 { 00204 if (opt->type() == DrBase::List) 00205 { 00206 blockSS = true; 00207 m_list->clear(); 00208 m_choices.clear(); 00209 TQPtrListIterator<DrBase> it(*(((DrListOption*)opt)->choices())); 00210 for (;it.current();++it) 00211 { 00212 m_list->insertItem(it.current()->get("text")); 00213 m_choices.append(it.current()->name()); 00214 } 00215 blockSS = false; 00216 setValue(opt->valueText()); 00217 } 00218 } 00219 00220 void OptionListView::setValue(const TQString& val) 00221 { 00222 m_list->setCurrentItem(m_choices.findIndex(val)); 00223 } 00224 00225 void OptionListView::slotSelectionChanged() 00226 { 00227 if (blockSS) return; 00228 00229 QString s = m_choices[m_list->currentItem()]; 00230 emit valueChanged(s); 00231 } 00232 00233 //****************************************************************************************************** 00234 00235 OptionBooleanView::OptionBooleanView(TQWidget *parent, const char *name) 00236 : OptionBaseView(parent,name) 00237 { 00238 m_group = new TQVButtonGroup(this); 00239 m_group->setFrameStyle(TQFrame::NoFrame); 00240 00241 TQRadioButton *btn = new TQRadioButton(m_group); 00242 btn->setCursor(KCursor::handCursor()); 00243 btn = new TQRadioButton(m_group); 00244 btn->setCursor(KCursor::handCursor()); 00245 00246 TQVBoxLayout *main_ = new TQVBoxLayout(this, 0, 10); 00247 main_->addWidget(m_group); 00248 00249 connect(m_group,TQT_SIGNAL(clicked(int)),TQT_SLOT(slotSelected(int))); 00250 } 00251 00252 void OptionBooleanView::setOption(DrBase *opt) 00253 { 00254 if (opt->type() == DrBase::Boolean) 00255 { 00256 TQPtrListIterator<DrBase> it(*(((DrBooleanOption*)opt)->choices())); 00257 m_choices.clear(); 00258 static_cast<TQButton*>(m_group->find(0))->setText(it.toFirst()->get("text")); 00259 m_choices.append(it.toFirst()->name()); 00260 static_cast<TQButton*>(m_group->find(1))->setText(it.toLast()->get("text")); 00261 m_choices.append(it.toLast()->name()); 00262 setValue(opt->valueText()); 00263 } 00264 } 00265 00266 void OptionBooleanView::setValue(const TQString& val) 00267 { 00268 int ID = m_choices.findIndex(val); 00269 m_group->setButton(ID); 00270 } 00271 00272 void OptionBooleanView::slotSelected(int ID) 00273 { 00274 TQString s = m_choices[ID]; 00275 emit valueChanged(s); 00276 } 00277 00278 //****************************************************************************************************** 00279 00280 DrOptionView::DrOptionView(TQWidget *parent, const char *name) 00281 : TQGroupBox(parent,name) 00282 { 00283 m_stack = new TQWidgetStack(this); 00284 00285 OptionBaseView *w = new OptionListView(m_stack); 00286 connect(w,TQT_SIGNAL(valueChanged(const TQString&)),TQT_SLOT(slotValueChanged(const TQString&))); 00287 m_stack->addWidget(w,DrBase::List); 00288 00289 w = new OptionStringView(m_stack); 00290 connect(w,TQT_SIGNAL(valueChanged(const TQString&)),TQT_SLOT(slotValueChanged(const TQString&))); 00291 m_stack->addWidget(w,DrBase::String); 00292 00293 w = new OptionNumericView(m_stack); 00294 connect(w,TQT_SIGNAL(valueChanged(const TQString&)),TQT_SLOT(slotValueChanged(const TQString&))); 00295 m_stack->addWidget(w,DrBase::Integer); 00296 00297 w = new OptionBooleanView(m_stack); 00298 connect(w,TQT_SIGNAL(valueChanged(const TQString&)),TQT_SLOT(slotValueChanged(const TQString&))); 00299 m_stack->addWidget(w,DrBase::Boolean); 00300 00301 w = new OptionBaseView(m_stack); 00302 connect(w,TQT_SIGNAL(valueChanged(const TQString&)),TQT_SLOT(slotValueChanged(const TQString&))); 00303 m_stack->addWidget(w,0); // empty widget 00304 00305 m_stack->raiseWidget(w); 00306 setTitle(i18n("No Option Selected")); 00307 00308 setColumnLayout(0, Qt::Vertical ); 00309 layout()->setSpacing( KDialog::spacingHint() ); 00310 layout()->setMargin( KDialog::marginHint() ); 00311 TQVBoxLayout *main_ = new TQVBoxLayout(TQT_TQLAYOUT(layout()), KDialog::marginHint()); 00312 main_->addWidget(m_stack); 00313 00314 m_item = 0; 00315 m_block = false; 00316 m_allowfixed = true; 00317 } 00318 00319 void DrOptionView::slotItemSelected(TQListViewItem *i) 00320 { 00321 m_item = (DriverItem*)i; 00322 if (m_item && !m_item->drItem()->isOption()) 00323 m_item = 0; 00324 int ID(0); 00325 if (m_item) 00326 if (m_item->drItem()->type() == DrBase::Float) ID = DrBase::Integer; 00327 else ID = m_item->drItem()->type(); 00328 00329 OptionBaseView *w = (OptionBaseView*)m_stack->widget(ID); 00330 if (w) 00331 { 00332 m_block = true; 00333 bool enabled(true); 00334 if (m_item) 00335 { 00336 w->setOption((m_item ? m_item->drItem() : 0)); 00337 setTitle(m_item->drItem()->get("text")); 00338 enabled = ((m_item->drItem()->get("fixed") != "1") || m_allowfixed); 00339 } 00340 else 00341 setTitle(i18n("No Option Selected")); 00342 m_stack->raiseWidget(w); 00343 w->setEnabled(enabled); 00344 m_block = false; 00345 } 00346 } 00347 00348 void DrOptionView::slotValueChanged(const TQString& val) 00349 { 00350 if (m_item && m_item->drItem() && !m_block) 00351 { 00352 m_item->drItem()->setValueText(val); 00353 m_item->updateText(); 00354 emit changed(); 00355 } 00356 } 00357 00358 #include "droptionview.moc"