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

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
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
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