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

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
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.7.3
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