• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

kinputdialog.cpp

Go to the documentation of this file.
00001 /*
00002   Copyright (C) 2003 Nadeem Hasan <nhasan@kde.org>
00003 
00004   This library is free software; you can redistribute it and/or
00005   modify it under the terms of the GNU Library General Public
00006   License as published by the Free Software Foundation; either
00007   version 2 of the License, or (at your option) any later version.
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 "kinputdialog.h"
00021 #include "kinputdialog_p.h"
00022 
00023 #include <QtGui/QDoubleValidator>
00024 #include <QtGui/QLabel>
00025 #include <QtGui/QLayout>
00026 
00027 #include <kcombobox.h>
00028 #include <kcompletion.h>
00029 #include <kguiitem.h>
00030 #include <klineedit.h>
00031 #include <klistwidget.h>
00032 #include <knuminput.h>
00033 #include <kstandardguiitem.h>
00034 #include <ktextedit.h>
00035 
00036 KInputDialogHelper::KInputDialogHelper( const QString &caption, const QString &label,
00037                                         const QString &value, QWidget *parent,
00038                                         QValidator *validator, const QString &mask )
00039     : KDialog(parent),
00040       m_label(0), m_lineEdit(0), m_intSpinBox(0),
00041       m_doubleSpinBox(0), m_comboBox(0)
00042 {
00043     setCaption(caption);
00044     setButtons(Ok | Cancel);
00045     setDefaultButton(Ok);
00046     showButtonSeparator(true);
00047     setModal(true);
00048 
00049     QWidget *frame = new QWidget(this);
00050     QVBoxLayout *layout = new QVBoxLayout(frame);
00051     layout->setMargin(0);
00052     layout->setSpacing(spacingHint());
00053 
00054     m_label = new QLabel(label, frame);
00055     m_label->setWordWrap(true);
00056     layout->addWidget(m_label);
00057 
00058     m_lineEdit = new KLineEdit(value, frame);
00059     m_lineEdit->setClearButtonShown(true);
00060     layout->addWidget(m_lineEdit);
00061 
00062     m_lineEdit->setFocus();
00063     m_label->setBuddy(m_lineEdit);
00064 
00065     layout->addStretch();
00066 
00067     if (validator)
00068         m_lineEdit->setValidator(validator);
00069 
00070     if (!mask.isEmpty())
00071         m_lineEdit->setInputMask(mask);
00072 
00073     connect(m_lineEdit, SIGNAL(textChanged(const QString&)),
00074             SLOT(slotEditTextChanged(const QString&)));
00075 
00076     setMainWidget(frame);
00077     slotEditTextChanged(value);
00078     setMinimumWidth(350);
00079 }
00080 
00081 KInputDialogHelper::KInputDialogHelper( const QString &caption, const QString &label,
00082                                         const QString &value, QWidget *parent )
00083     : KDialog(parent),
00084       m_label(0), m_lineEdit(0), m_intSpinBox(0),
00085       m_doubleSpinBox(0), m_comboBox(0)
00086 {
00087     setCaption(caption);
00088     setButtons(Ok | Cancel | User1);
00089     setButtonGuiItem(User1, KStandardGuiItem::clear());
00090     setDefaultButton(Ok);
00091     showButtonSeparator(false);
00092     setModal(true);
00093     QWidget *frame = new QWidget(this);
00094     QVBoxLayout *layout = new QVBoxLayout(frame);
00095     layout->setMargin(0);
00096     layout->setSpacing(spacingHint());
00097 
00098     m_label = new QLabel(label, frame);
00099     m_label->setWordWrap(true);
00100     layout->addWidget(m_label);
00101 
00102     m_textEdit = new KTextEdit(frame);
00103     m_textEdit->insertPlainText(value);
00104     layout->addWidget(m_textEdit, 10);
00105 
00106     m_textEdit->setFocus();
00107     m_label->setBuddy(m_textEdit);
00108 
00109     connect(this, SIGNAL(user1Clicked()), m_textEdit, SLOT(clear()));
00110     connect(this, SIGNAL(user1Clicked()), m_textEdit, SLOT(setFocus()));
00111     setMainWidget(frame);
00112     setMinimumWidth(400);
00113 }
00114 
00115 KInputDialogHelper::KInputDialogHelper( const QString &caption, const QString &label,
00116                                         int value, int minValue, int maxValue, int step, int base,
00117                                         QWidget *parent )
00118     : KDialog(parent),
00119       m_label(0), m_lineEdit(0), m_intSpinBox(0),
00120       m_doubleSpinBox(0), m_comboBox(0)
00121 {
00122     setCaption(caption);
00123     setButtons(Ok | Cancel);
00124     showButtonSeparator(true);
00125     setModal(true);
00126 
00127     QWidget *frame = new QWidget(this);
00128     QVBoxLayout *layout = new QVBoxLayout(frame);
00129     layout->setSpacing(spacingHint());
00130 
00131     m_label = new QLabel(label, frame);
00132     m_label->setWordWrap(true);
00133     layout->addWidget(m_label);
00134 
00135     m_intSpinBox = new KIntSpinBox(minValue, maxValue, step, value, frame, base);
00136     layout->addWidget(m_intSpinBox);
00137 
00138     layout->addStretch();
00139     layout->setMargin(0);
00140 
00141     m_intSpinBox->setFocus();
00142     setMainWidget(frame);
00143     setMinimumWidth(300);
00144 }
00145 
00146 KInputDialogHelper::KInputDialogHelper( const QString &caption, const QString &label,
00147                                         double value, double minValue, double maxValue, double step, int decimals,
00148                                         QWidget *parent )
00149     : KDialog( parent ),
00150       m_label(0), m_lineEdit(0), m_intSpinBox(0),
00151       m_doubleSpinBox(0), m_comboBox(0)
00152 {
00153     setCaption(caption);
00154     setButtons(Ok | Cancel);
00155     showButtonSeparator(true);
00156     setModal(true);
00157 
00158     QWidget *frame = new QWidget(this);
00159     QVBoxLayout *layout = new QVBoxLayout(frame);
00160     layout->setSpacing(spacingHint());
00161 
00162     m_label = new QLabel(label, frame);
00163     m_label->setWordWrap(true);
00164     layout->addWidget(m_label);
00165 
00166     m_doubleSpinBox = new QDoubleSpinBox(frame);
00167     m_doubleSpinBox->setRange(minValue, maxValue);
00168     m_doubleSpinBox->setSingleStep(step);
00169     m_doubleSpinBox->setValue(value);
00170     m_doubleSpinBox->setDecimals(decimals);
00171 
00172     layout->addWidget(m_doubleSpinBox);
00173 
00174     layout->addStretch();
00175     layout->setMargin(0);
00176 
00177     m_doubleSpinBox->setFocus();
00178     setMainWidget(frame);
00179     setMinimumWidth(300);
00180 }
00181 
00182 KInputDialogHelper::KInputDialogHelper( const QString &caption, const QString &label,
00183                                         const QStringList &list, int current, bool editable, QWidget *parent )
00184     : KDialog(parent),
00185       m_label(0), m_lineEdit(0), m_intSpinBox(0),
00186       m_doubleSpinBox(0), m_comboBox(0)
00187 {
00188     setCaption(caption);
00189     setButtons(Ok | Cancel);
00190     setDefaultButton(Ok);
00191     showButtonSeparator(true);
00192     setModal(true);
00193 
00194     QWidget *frame = new QWidget(this);
00195     QVBoxLayout *layout = new QVBoxLayout(frame);
00196     layout->setSpacing(spacingHint());
00197 
00198     m_label = new QLabel(label, frame);
00199     m_label->setWordWrap(true);
00200     layout->addWidget(m_label);
00201 
00202     if (editable) {
00203         m_comboBox = new KComboBox(editable, frame);
00204         m_lineEdit = new KLineEdit(frame);
00205         m_lineEdit->setClearButtonShown(true);
00206         m_comboBox->setLineEdit(m_lineEdit);
00207         m_comboBox->insertItems(0, list);
00208         m_comboBox->setCurrentIndex(current);
00209         layout->addWidget(m_comboBox);
00210 
00211         connect(m_comboBox, SIGNAL(editTextChanged(const QString&)),
00212                 SLOT(slotUpdateButtons(const QString&)));
00213         slotUpdateButtons(m_comboBox->currentText());
00214         m_comboBox->setFocus();
00215     } else {
00216         m_listBox = new KListWidget(frame);
00217         m_listBox->addItems(list);
00218         m_listBox->setCurrentRow(current);
00219         layout->addWidget(m_listBox, 10);
00220         connect(m_listBox, SIGNAL(executed(QListWidgetItem*)),
00221                 SLOT(accept()));
00222         m_listBox->setFocus();
00223     }
00224 
00225     layout->addStretch();
00226     layout->setMargin(0);
00227     setMainWidget(frame);
00228     setMinimumWidth(320);
00229 }
00230 
00231 KInputDialogHelper::KInputDialogHelper( const QString &caption, const QString &label,
00232                                         const QStringList &list, const QStringList &select, bool multiple,
00233                                         QWidget *parent )
00234     : KDialog( parent ),
00235       m_label(0), m_lineEdit(0), m_intSpinBox(0),
00236       m_doubleSpinBox(0), m_comboBox(0)
00237 {
00238     setCaption(caption);
00239     setButtons(Ok | Cancel);
00240     showButtonSeparator(true);
00241     setModal(true);
00242 
00243     QWidget *frame = new QWidget(this);
00244     QVBoxLayout *layout = new QVBoxLayout(frame);
00245     layout->setSpacing(spacingHint());
00246 
00247     m_label = new QLabel(label, frame);
00248     m_label->setWordWrap(true); 
00249    layout->addWidget(m_label);
00250 
00251     m_listBox = new KListWidget(frame);
00252     m_listBox->addItems(list);
00253     layout->addWidget(m_listBox);
00254 
00255     if (multiple) {
00256         m_listBox->setSelectionMode(QAbstractItemView::ExtendedSelection);
00257 
00258         for (QStringList::ConstIterator it = select.begin(); it != select.end(); ++it) {
00259             const QList<QListWidgetItem*> matches = m_listBox->findItems(*it, Qt::MatchCaseSensitive|Qt::MatchExactly);
00260             if (!matches.isEmpty())
00261                 m_listBox->setCurrentItem(matches.first());
00262         }
00263     } else {
00264         connect(m_listBox, SIGNAL(executed(QListWidgetItem*)), SLOT(accept()));
00265 
00266         if (!select.isEmpty()) {
00267             QString text = select.first();
00268 
00269             const QList<QListWidgetItem*> matches = m_listBox->findItems(text, Qt::MatchCaseSensitive|Qt::MatchExactly);
00270             if (!matches.isEmpty())
00271                 m_listBox->setCurrentItem(matches.first());
00272         }
00273     }
00274 
00275     m_listBox->setFocus();
00276 
00277     layout->addStretch();
00278     layout->setMargin(0);
00279     setMainWidget(frame);
00280     setMinimumWidth(320);
00281 }
00282 
00283 KInputDialogHelper::~KInputDialogHelper()
00284 {
00285 }
00286 
00287 void KInputDialogHelper::slotEditTextChanged( const QString &text )
00288 {
00289     bool on;
00290 
00291     if (m_lineEdit->validator()) {
00292         QString str = m_lineEdit->text();
00293         int index = m_lineEdit->cursorPosition();
00294         on = (m_lineEdit->validator()->validate(str, index) == QValidator::Acceptable);
00295     } else {
00296         on = !text.trimmed().isEmpty();
00297     }
00298 
00299     enableButton(Ok, on);
00300 }
00301 
00302 void KInputDialogHelper::slotUpdateButtons( const QString &text )
00303 {
00304     enableButton(Ok, !text.isEmpty());
00305 }
00306 
00307 KLineEdit *KInputDialogHelper::lineEdit() const
00308 {
00309     return m_lineEdit;
00310 }
00311 
00312 KIntSpinBox *KInputDialogHelper::intSpinBox() const
00313 {
00314     return m_intSpinBox;
00315 }
00316 
00317 QDoubleSpinBox *KInputDialogHelper::doubleSpinBox() const
00318 {
00319     return m_doubleSpinBox;
00320 }
00321 
00322 KComboBox *KInputDialogHelper::comboBox() const
00323 {
00324     return m_comboBox;
00325 }
00326 
00327 KListWidget *KInputDialogHelper::listBox() const
00328 {
00329     return m_listBox;
00330 }
00331 
00332 KTextEdit *KInputDialogHelper::textEdit() const
00333 {
00334     return m_textEdit;
00335 }
00336 
00337 
00338 // KInputDialog namespace
00339 
00340 namespace KInputDialog {
00341 
00342 QString getText( const QString &caption,
00343                  const QString &label, const QString &value, bool *ok, QWidget *parent,
00344                  QValidator *validator, const QString &mask,
00345                  const QString &whatsThis,const QStringList &completionList )
00346 {
00347     KInputDialogHelper dlg(caption, label, value, parent, validator, mask);
00348 
00349     if (!whatsThis.isEmpty())
00350         dlg.lineEdit()->setWhatsThis(whatsThis);
00351 
00352     if (!completionList.isEmpty()) {
00353         KCompletion *comp=dlg.lineEdit()->completionObject();
00354         for (QStringList::const_iterator it = completionList.constBegin(); it != completionList.constEnd(); ++it)
00355             comp->addItem(*it);
00356     }
00357 
00358     bool _ok = (dlg.exec() == KDialog::Accepted);
00359 
00360     if (ok)
00361         *ok = _ok;
00362 
00363     QString result;
00364     if (_ok)
00365         result = dlg.lineEdit()->text();
00366 
00367     // A validator may explicitly allow leading and trailing whitespace
00368     if (!validator)
00369         result = result.trimmed();
00370 
00371     return result;
00372 }
00373 
00374 QString getMultiLineText( const QString &caption,
00375                           const QString &label, const QString &value, bool *ok,
00376                           QWidget *parent )
00377 {
00378     KInputDialogHelper dlg(caption, label, value, parent);
00379 
00380     bool _ok = (dlg.exec() == KDialog::Accepted);
00381 
00382     if (ok)
00383         *ok = _ok;
00384 
00385     QString result;
00386     if (_ok)
00387         result = dlg.textEdit()->toPlainText();
00388 
00389     return result;
00390 }
00391 
00392 int getInteger( const QString &caption, const QString &label,
00393                 int value, int minValue, int maxValue, int step, int base, bool *ok,
00394                 QWidget *parent )
00395 {
00396     KInputDialogHelper dlg(caption, label, value, minValue, maxValue, step, base, parent);
00397 
00398     bool _ok = (dlg.exec() == KDialog::Accepted);
00399 
00400     if (ok)
00401         *ok = _ok;
00402 
00403     int result = 0;
00404     if (_ok)
00405         result = dlg.intSpinBox()->value();
00406 
00407     return result;
00408 }
00409 
00410 int getInteger( const QString &caption, const QString &label,
00411                 int value, int minValue, int maxValue, int step, bool *ok,
00412                 QWidget *parent )
00413 {
00414     return getInteger(caption, label, value, minValue, maxValue, step, 10, ok, parent);
00415 }
00416 
00417 double getDouble( const QString &caption, const QString &label,
00418                   double value, double minValue, double maxValue, double step, int decimals,
00419                   bool *ok, QWidget *parent )
00420 {
00421     KInputDialogHelper dlg(caption, label, value, minValue, maxValue, step, decimals, parent);
00422 
00423     bool _ok = (dlg.exec() == KDialog::Accepted);
00424 
00425     if (ok)
00426         *ok = _ok;
00427 
00428     double result = 0;
00429     if (_ok)
00430         result = dlg.doubleSpinBox()->value();
00431 
00432     return result;
00433 }
00434 
00435 double getDouble( const QString &caption, const QString &label,
00436                   double value, double minValue, double maxValue, int decimals,
00437                   bool *ok, QWidget *parent )
00438 {
00439     return getDouble(caption, label, value, minValue, maxValue, 0.1, decimals, ok, parent);
00440 }
00441 
00442 QString getItem( const QString &caption, const QString &label,
00443                  const QStringList &list, int current, bool editable, bool *ok,
00444                  QWidget *parent )
00445 {
00446     KInputDialogHelper dlg(caption, label, list, current, editable, parent);
00447 
00448     if (!editable)
00449         dlg.connect(dlg.listBox(), SIGNAL(executed(QListWidgetItem*)), &dlg, SLOT(accept()));
00450 
00451     bool _ok = (dlg.exec() == KDialog::Accepted);
00452 
00453     if (ok)
00454         *ok = _ok;
00455 
00456     QString result;
00457     if (_ok) {
00458         if (editable)
00459             result = dlg.comboBox()->currentText();
00460         else if( dlg.listBox()->currentItem())
00461             result = dlg.listBox()->currentItem()->text();
00462     }
00463 
00464     return result;
00465 }
00466 
00467 QStringList getItemList( const QString &caption,
00468                          const QString &label, const QStringList &list, const QStringList &select,
00469                          bool multiple, bool *ok, QWidget *parent )
00470 {
00471     KInputDialogHelper dlg(caption, label, list, select, multiple, parent);
00472 
00473     bool _ok = (dlg.exec() == KDialog::Accepted);
00474 
00475     if (ok)
00476         *ok = _ok;
00477 
00478     QStringList result;
00479     if (_ok) {
00480         for (int i=0 ; i < dlg.listBox()->count() ; i++) {
00481 
00482             QListWidgetItem* item = dlg.listBox()->item(i);
00483 
00484             if (item->isSelected())
00485                 result.append(item->text());
00486         }
00487     }
00488 
00489     return result;
00490 }
00491 
00492 }
00493 
00494 #include "kinputdialog_p.moc"
00495 
00496 /* vim: set ai et sw=2 ts=2
00497 */

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal