droptionview.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <kdeprint@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 <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 <kdialog.h>
00037 #include <klocale.h>
00038 
00039 OptionBaseView::OptionBaseView(QWidget *parent, const char *name)
00040 : QWidget(parent,name)
00041 {
00042     blockSS = false;
00043 }
00044 
00045 void OptionBaseView::setOption(DrBase*)
00046 {
00047 }
00048 
00049 void OptionBaseView::setValue(const QString&)
00050 {
00051 }
00052 
00053 //******************************************************************************************************
00054 
00055 OptionNumericView::OptionNumericView(QWidget *parent, const char *name)
00056 : OptionBaseView(parent,name)
00057 {
00058     m_edit = new QLineEdit(this);
00059     m_slider = new QSlider(Qt::Horizontal,this);
00060     m_slider->setTickmarks(QSlider::Below);
00061     QLabel  *lab = new QLabel(i18n("Value:"),this);
00062     m_minval = new QLabel(this);
00063     m_maxval = new QLabel(this);
00064 
00065     m_integer = true;
00066 
00067     QVBoxLayout *main_ = new QVBoxLayout(this, 0, 10);
00068     QHBoxLayout *sub_ = new QHBoxLayout(0, 0, 10);
00069     QHBoxLayout *sub2_ = new QHBoxLayout(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,SIGNAL(valueChanged(int)),SLOT(slotSliderChanged(int)));
00082     connect(m_edit,SIGNAL(textChanged(const QString&)),SLOT(slotEditChanged(const QString&)));
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,QMAX((max_-min_)/20,1));
00098         m_minval->setText(QString::number(min_));
00099         m_maxval->setText(QString::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,QMAX((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 QString& 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 = QString::number(value);
00129     else
00130         txt = QString::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 QString& 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         QApplication::beep();
00158     }
00159 }
00160 
00161 //******************************************************************************************************
00162 
00163 OptionStringView::OptionStringView(QWidget *parent, const char *name)
00164 : OptionBaseView(parent,name)
00165 {
00166     m_edit = new QLineEdit(this);
00167     QLabel  *lab = new QLabel(i18n("String value:"),this);
00168 
00169     QVBoxLayout *main_ = new QVBoxLayout(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,SIGNAL(textChanged(const QString&)),SIGNAL(valueChanged(const QString&)));
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 QString& val)
00185 {
00186     m_edit->setText(val);
00187 }
00188 
00189 //******************************************************************************************************
00190 
00191 OptionListView::OptionListView(QWidget *parent, const char *name)
00192 : OptionBaseView(parent,name)
00193 {
00194     m_list = new KListBox(this);
00195 
00196     QVBoxLayout *main_ = new QVBoxLayout(this, 0, 10);
00197     main_->addWidget(m_list);
00198 
00199     connect(m_list,SIGNAL(selectionChanged()),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         QPtrListIterator<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 QString& 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(QWidget *parent, const char *name)
00236 : OptionBaseView(parent,name)
00237 {
00238     m_group = new QVButtonGroup(this);
00239     m_group->setFrameStyle(QFrame::NoFrame);
00240 
00241     QRadioButton    *btn = new QRadioButton(m_group);
00242     btn->setCursor(KCursor::handCursor());
00243     btn = new QRadioButton(m_group);
00244     btn->setCursor(KCursor::handCursor());
00245 
00246     QVBoxLayout *main_ = new QVBoxLayout(this, 0, 10);
00247     main_->addWidget(m_group);
00248 
00249     connect(m_group,SIGNAL(clicked(int)),SLOT(slotSelected(int)));
00250 }
00251 
00252 void OptionBooleanView::setOption(DrBase *opt)
00253 {
00254     if (opt->type() == DrBase::Boolean)
00255     {
00256         QPtrListIterator<DrBase>    it(*(((DrBooleanOption*)opt)->choices()));
00257         m_choices.clear();
00258         m_group->find(0)->setText(it.toFirst()->get("text"));
00259         m_choices.append(it.toFirst()->name());
00260         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 QString& val)
00267 {
00268     int ID = m_choices.findIndex(val);
00269     m_group->setButton(ID);
00270 }
00271 
00272 void OptionBooleanView::slotSelected(int ID)
00273 {
00274     QString s = m_choices[ID];
00275     emit valueChanged(s);
00276 }
00277 
00278 //******************************************************************************************************
00279 
00280 DrOptionView::DrOptionView(QWidget *parent, const char *name)
00281 : QGroupBox(parent,name)
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);    // 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     QVBoxLayout *main_ = new QVBoxLayout(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(QListViewItem *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 QString& 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"
KDE Home | KDE Accessibility Home | Description of Access Keys