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

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • kdeui
  • dialogs
kinputdialog.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 Nadeem Hasan <nhasan@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "kinputdialog.h"
21 #include "kinputdialog_p.h"
22 
23 #include <QtGui/QDoubleValidator>
24 #include <QtGui/QLabel>
25 #include <QtGui/QLayout>
26 
27 #include <kcombobox.h>
28 #include <kcompletion.h>
29 #include <kguiitem.h>
30 #include <klineedit.h>
31 #include <klistwidget.h>
32 #include <knuminput.h>
33 #include <kstandardguiitem.h>
34 #include <ktextedit.h>
35 
36 KInputDialogHelper::KInputDialogHelper( const QString &caption, const QString &label,
37  const QString &value, QWidget *parent,
38  QValidator *validator, const QString &mask )
39  : KDialog(parent),
40  m_label(0), m_lineEdit(0), m_intSpinBox(0),
41  m_doubleSpinBox(0), m_comboBox(0), m_listBox(0)
42 {
43  setCaption(caption);
44  setButtons(Ok | Cancel);
45  setDefaultButton(Ok);
46  setModal(true);
47 
48  QWidget *frame = new QWidget(this);
49  QVBoxLayout *layout = new QVBoxLayout(frame);
50  layout->setMargin(0);
51 
52  m_label = new QLabel(label, frame);
53  m_label->setWordWrap(true);
54  layout->addWidget(m_label);
55 
56  m_lineEdit = new KLineEdit(value, frame);
57  m_lineEdit->setClearButtonShown(true);
58  layout->addWidget(m_lineEdit);
59 
60  m_lineEdit->setFocus();
61  m_label->setBuddy(m_lineEdit);
62 
63  layout->addStretch();
64 
65  if (validator)
66  m_lineEdit->setValidator(validator);
67 
68  if (!mask.isEmpty())
69  m_lineEdit->setInputMask(mask);
70 
71  connect(m_lineEdit, SIGNAL(textChanged(QString)),
72  SLOT(slotEditTextChanged(QString)));
73 
74  setMainWidget(frame);
75  slotEditTextChanged(value);
76  setMinimumWidth(350);
77 }
78 
79 KInputDialogHelper::KInputDialogHelper( const QString &caption, const QString &label,
80  const QString &value, QWidget *parent )
81  : KDialog(parent),
82  m_label(0), m_lineEdit(0), m_intSpinBox(0),
83  m_doubleSpinBox(0), m_comboBox(0)
84 {
85  setCaption(caption);
86  setButtons(Ok | Cancel | User1);
87  setButtonGuiItem(User1, KStandardGuiItem::clear());
88  setDefaultButton(Ok);
89  setModal(true);
90  QWidget *frame = new QWidget(this);
91  QVBoxLayout *layout = new QVBoxLayout(frame);
92  layout->setMargin(0);
93 
94  m_label = new QLabel(label, frame);
95  m_label->setWordWrap(true);
96  layout->addWidget(m_label);
97 
98  m_textEdit = new KTextEdit(frame);
99  m_textEdit->insertPlainText(value);
100  layout->addWidget(m_textEdit, 10);
101 
102  m_textEdit->setFocus();
103  m_label->setBuddy(m_textEdit);
104 
105  connect(this, SIGNAL(user1Clicked()), m_textEdit, SLOT(clear()));
106  connect(this, SIGNAL(user1Clicked()), m_textEdit, SLOT(setFocus()));
107  setMainWidget(frame);
108  setMinimumWidth(400);
109 }
110 
111 KInputDialogHelper::KInputDialogHelper( const QString &caption, const QString &label,
112  int value, int minValue, int maxValue, int step, int base,
113  QWidget *parent )
114  : KDialog(parent),
115  m_label(0), m_lineEdit(0), m_intSpinBox(0),
116  m_doubleSpinBox(0), m_comboBox(0), m_listBox(0)
117 {
118  setCaption(caption);
119  setButtons(Ok | Cancel);
120  setModal(true);
121 
122  QWidget *frame = new QWidget(this);
123  QVBoxLayout *layout = new QVBoxLayout(frame);
124 
125  m_label = new QLabel(label, frame);
126  m_label->setWordWrap(true);
127  layout->addWidget(m_label);
128 
129  m_intSpinBox = new KIntSpinBox(minValue, maxValue, step, value, frame, base);
130  layout->addWidget(m_intSpinBox);
131 
132  layout->addStretch();
133  layout->setMargin(0);
134 
135  m_intSpinBox->setFocus();
136  setMainWidget(frame);
137  setMinimumWidth(300);
138 }
139 
140 KInputDialogHelper::KInputDialogHelper( const QString &caption, const QString &label,
141  double value, double minValue, double maxValue, double step, int decimals,
142  QWidget *parent )
143  : KDialog( parent ),
144  m_label(0), m_lineEdit(0), m_intSpinBox(0),
145  m_doubleSpinBox(0), m_comboBox(0), m_listBox(0)
146 {
147  setCaption(caption);
148  setButtons(Ok | Cancel);
149  setModal(true);
150 
151  QWidget *frame = new QWidget(this);
152  QVBoxLayout *layout = new QVBoxLayout(frame);
153 
154  m_label = new QLabel(label, frame);
155  m_label->setWordWrap(true);
156  layout->addWidget(m_label);
157 
158  m_doubleSpinBox = new QDoubleSpinBox(frame);
159  m_doubleSpinBox->setRange(minValue, maxValue);
160  m_doubleSpinBox->setSingleStep(step);
161  m_doubleSpinBox->setValue(value);
162  m_doubleSpinBox->setDecimals(decimals);
163 
164  layout->addWidget(m_doubleSpinBox);
165 
166  layout->addStretch();
167  layout->setMargin(0);
168 
169  m_doubleSpinBox->setFocus();
170  setMainWidget(frame);
171  setMinimumWidth(300);
172 }
173 
174 KInputDialogHelper::KInputDialogHelper( const QString &caption, const QString &label,
175  const QStringList &list, int current, bool editable, QWidget *parent )
176  : KDialog(parent),
177  m_label(0), m_lineEdit(0), m_intSpinBox(0),
178  m_doubleSpinBox(0), m_comboBox(0), m_listBox(0)
179 {
180  setCaption(caption);
181  setButtons(Ok | Cancel);
182  setDefaultButton(Ok);
183  setModal(true);
184 
185  QWidget *frame = new QWidget(this);
186  QVBoxLayout *layout = new QVBoxLayout(frame);
187 
188  m_label = new QLabel(label, frame);
189  m_label->setWordWrap(true);
190  layout->addWidget(m_label);
191 
192  if (editable) {
193  m_comboBox = new KComboBox(editable, frame);
194  m_lineEdit = new KLineEdit(frame);
195  m_lineEdit->setClearButtonShown(true);
196  m_comboBox->setLineEdit(m_lineEdit);
197  m_comboBox->insertItems(0, list);
198  m_comboBox->setCurrentIndex(current);
199  layout->addWidget(m_comboBox);
200 
201  connect(m_comboBox, SIGNAL(editTextChanged(QString)),
202  SLOT(slotUpdateButtons(QString)));
203  slotUpdateButtons(m_comboBox->currentText());
204  m_comboBox->setFocus();
205  } else {
206  m_listBox = new KListWidget(frame);
207  m_listBox->addItems(list);
208  m_listBox->setCurrentRow(current);
209  layout->addWidget(m_listBox, 10);
210  connect(m_listBox, SIGNAL(executed(QListWidgetItem*)),
211  SLOT(accept()));
212  m_listBox->setFocus();
213  }
214 
215  layout->addStretch();
216  layout->setMargin(0);
217  setMainWidget(frame);
218  setMinimumWidth(320);
219 }
220 
221 KInputDialogHelper::KInputDialogHelper( const QString &caption, const QString &label,
222  const QStringList &list, const QStringList &select, bool multiple,
223  QWidget *parent )
224  : KDialog( parent ),
225  m_label(0), m_lineEdit(0), m_intSpinBox(0),
226  m_doubleSpinBox(0), m_comboBox(0), m_listBox(0)
227 {
228  setCaption(caption);
229  setButtons(Ok | Cancel);
230  setModal(true);
231 
232  QWidget *frame = new QWidget(this);
233  QVBoxLayout *layout = new QVBoxLayout(frame);
234 
235  m_label = new QLabel(label, frame);
236  m_label->setWordWrap(true);
237  layout->addWidget(m_label);
238 
239  m_listBox = new KListWidget(frame);
240  m_listBox->addItems(list);
241  layout->addWidget(m_listBox);
242 
243  if (multiple) {
244  m_listBox->setSelectionMode(QAbstractItemView::ExtendedSelection);
245 
246  for (QStringList::ConstIterator it = select.begin(); it != select.end(); ++it) {
247  const QList<QListWidgetItem*> matches = m_listBox->findItems(*it, Qt::MatchCaseSensitive|Qt::MatchExactly);
248  if (!matches.isEmpty())
249  m_listBox->setCurrentItem(matches.first());
250  }
251  } else {
252  connect(m_listBox, SIGNAL(executed(QListWidgetItem*)), SLOT(accept()));
253 
254  if (!select.isEmpty()) {
255  QString text = select.first();
256 
257  const QList<QListWidgetItem*> matches = m_listBox->findItems(text, Qt::MatchCaseSensitive|Qt::MatchExactly);
258  if (!matches.isEmpty())
259  m_listBox->setCurrentItem(matches.first());
260  }
261  }
262 
263  m_listBox->setFocus();
264 
265  layout->addStretch();
266  layout->setMargin(0);
267  setMainWidget(frame);
268  setMinimumWidth(320);
269 }
270 
271 KInputDialogHelper::~KInputDialogHelper()
272 {
273 }
274 
275 void KInputDialogHelper::slotEditTextChanged( const QString &text )
276 {
277  bool on;
278 
279  if (m_lineEdit->validator()) {
280  QString str = m_lineEdit->text();
281  int index = m_lineEdit->cursorPosition();
282  on = (m_lineEdit->validator()->validate(str, index) == QValidator::Acceptable);
283  } else {
284  on = !text.trimmed().isEmpty();
285  }
286 
287  enableButton(Ok, on);
288 }
289 
290 void KInputDialogHelper::slotUpdateButtons( const QString &text )
291 {
292  enableButton(Ok, !text.isEmpty());
293 }
294 
295 KLineEdit *KInputDialogHelper::lineEdit() const
296 {
297  return m_lineEdit;
298 }
299 
300 KIntSpinBox *KInputDialogHelper::intSpinBox() const
301 {
302  return m_intSpinBox;
303 }
304 
305 QDoubleSpinBox *KInputDialogHelper::doubleSpinBox() const
306 {
307  return m_doubleSpinBox;
308 }
309 
310 KComboBox *KInputDialogHelper::comboBox() const
311 {
312  return m_comboBox;
313 }
314 
315 KListWidget *KInputDialogHelper::listBox() const
316 {
317  return m_listBox;
318 }
319 
320 KTextEdit *KInputDialogHelper::textEdit() const
321 {
322  return m_textEdit;
323 }
324 
325 
326 // KInputDialog namespace
327 
328 namespace KInputDialog {
329 
330 QString getText( const QString &caption,
331  const QString &label, const QString &value, bool *ok, QWidget *parent,
332  QValidator *validator, const QString &mask,
333  const QString &whatsThis,const QStringList &completionList )
334 {
335  KInputDialogHelper dlg(caption, label, value, parent, validator, mask);
336 
337  if (!whatsThis.isEmpty())
338  dlg.lineEdit()->setWhatsThis(whatsThis);
339 
340  if (!completionList.isEmpty()) {
341  KCompletion *comp=dlg.lineEdit()->completionObject();
342  for (QStringList::const_iterator it = completionList.constBegin(); it != completionList.constEnd(); ++it)
343  comp->addItem(*it);
344  }
345 
346  bool _ok = (dlg.exec() == KDialog::Accepted);
347 
348  if (ok)
349  *ok = _ok;
350 
351  QString result;
352  if (_ok)
353  result = dlg.lineEdit()->text();
354 
355  // A validator may explicitly allow leading and trailing whitespace
356  if (!validator)
357  result = result.trimmed();
358 
359  return result;
360 }
361 
362 QString getMultiLineText( const QString &caption,
363  const QString &label, const QString &value, bool *ok,
364  QWidget *parent )
365 {
366  KInputDialogHelper dlg(caption, label, value, parent);
367  dlg.textEdit()->setAcceptRichText(false);
368  bool _ok = (dlg.exec() == KDialog::Accepted);
369 
370  if (ok)
371  *ok = _ok;
372 
373  QString result;
374  if (_ok)
375  result = dlg.textEdit()->toPlainText();
376 
377  return result;
378 }
379 
380 int getInteger( const QString &caption, const QString &label,
381  int value, int minValue, int maxValue, int step, int base, bool *ok,
382  QWidget *parent )
383 {
384  KInputDialogHelper dlg(caption, label, value, minValue, maxValue, step, base, parent);
385 
386  bool _ok = (dlg.exec() == KDialog::Accepted);
387 
388  if (ok)
389  *ok = _ok;
390 
391  int result = 0;
392  if (_ok)
393  result = dlg.intSpinBox()->value();
394 
395  return result;
396 }
397 
398 int getInteger( const QString &caption, const QString &label,
399  int value, int minValue, int maxValue, int step, bool *ok,
400  QWidget *parent )
401 {
402  return getInteger(caption, label, value, minValue, maxValue, step, 10, ok, parent);
403 }
404 
405 double getDouble( const QString &caption, const QString &label,
406  double value, double minValue, double maxValue, double step, int decimals,
407  bool *ok, QWidget *parent )
408 {
409  KInputDialogHelper dlg(caption, label, value, minValue, maxValue, step, decimals, parent);
410 
411  bool _ok = (dlg.exec() == KDialog::Accepted);
412 
413  if (ok)
414  *ok = _ok;
415 
416  double result = 0;
417  if (_ok)
418  result = dlg.doubleSpinBox()->value();
419 
420  return result;
421 }
422 
423 double getDouble( const QString &caption, const QString &label,
424  double value, double minValue, double maxValue, int decimals,
425  bool *ok, QWidget *parent )
426 {
427  return getDouble(caption, label, value, minValue, maxValue, 0.1, decimals, ok, parent);
428 }
429 
430 QString getItem( const QString &caption, const QString &label,
431  const QStringList &list, int current, bool editable, bool *ok,
432  QWidget *parent )
433 {
434  KInputDialogHelper dlg(caption, label, list, current, editable, parent);
435 
436  if (!editable)
437  dlg.connect(dlg.listBox(), SIGNAL(executed(QListWidgetItem*)), &dlg, SLOT(accept()));
438 
439  bool _ok = (dlg.exec() == KDialog::Accepted);
440 
441  if (ok)
442  *ok = _ok;
443 
444  QString result;
445  if (_ok) {
446  if (editable)
447  result = dlg.comboBox()->currentText();
448  else if( dlg.listBox()->currentItem())
449  result = dlg.listBox()->currentItem()->text();
450  }
451 
452  return result;
453 }
454 
455 QStringList getItemList( const QString &caption,
456  const QString &label, const QStringList &list, const QStringList &select,
457  bool multiple, bool *ok, QWidget *parent )
458 {
459  KInputDialogHelper dlg(caption, label, list, select, multiple, parent);
460 
461  bool _ok = (dlg.exec() == KDialog::Accepted);
462 
463  if (ok)
464  *ok = _ok;
465 
466  QStringList result;
467  if (_ok) {
468  for (int i=0 ; i < dlg.listBox()->count() ; i++) {
469 
470  QListWidgetItem* item = dlg.listBox()->item(i);
471 
472  if (item->isSelected())
473  result.append(item->text());
474  }
475  }
476 
477  return result;
478 }
479 
480 }
481 
482 #include "kinputdialog_p.moc"
483 
484 /* vim: set ai et sw=2 ts=2
485 */
knuminput.h
kcombobox.h
QWidget
QListWidgetItem::isSelected
bool isSelected() const
KStandardShortcut::label
QString label(StandardShortcut id)
Returns a localized label for user-visible display.
Definition: kstandardshortcut.cpp:267
KImageCache::clear
void clear()
Removes all entries from the cache.
Definition: kimagecache.cpp:164
kstandardguiitem.h
KInputDialog::getItem
QString getItem(const QString &caption, const QString &label, const QStringList &list, int current, bool editable, bool *ok, QWidget *parent)
Static convenience function to let the user select an item from a list.
Definition: kinputdialog.cpp:430
QListWidgetItem
KDialog
A dialog base class with standard buttons and predefined layouts.
Definition: kdialog.h:128
KStandardGuiItem::clear
KGuiItem clear()
Returns the 'Clear' gui item.
Definition: kstandardguiitem.cpp:169
KInputDialog::getDouble
double getDouble(const QString &caption, const QString &label, double value, double minValue, double maxValue, double step, int decimals, bool *ok, QWidget *parent)
Static convenience function to get a floating point number from the user.
Definition: kinputdialog.cpp:405
QList::const_iterator
KStandardGuiItem::Ok
Definition: kstandardguiitem.h:50
QDoubleSpinBox
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
KInputDialog::getInteger
int getInteger(const QString &caption, const QString &label, int value, int minValue, int maxValue, int step, int base, bool *ok, QWidget *parent)
Static convenience function to get an integer from the user.
Definition: kinputdialog.cpp:380
QList::append
void append(const T &value)
KInputDialog::getItemList
QStringList getItemList(const QString &caption, const QString &label, const QStringList &list, const QStringList &select, bool multiple, bool *ok, QWidget *parent)
Static convenience function to let the user select one or more items from a listbox.
Definition: kinputdialog.cpp:455
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
QString::trimmed
QString trimmed() const
KCompletion
A generic class for completing QStrings.
Definition: kcompletion.h:130
QVBoxLayout
QList::first
T & first()
QString
QList
QLayout::setMargin
void setMargin(int margin)
QStringList
KInputDialog::getText
QString getText(const QString &caption, const QString &label, const QString &value, bool *ok, QWidget *parent, QValidator *validator, const QString &mask, const QString &whatsThis, const QStringList &completionList)
Static convenience function to get a string from the user.
Definition: kinputdialog.cpp:330
kcompletion.h
QList::end
iterator end()
KStandardGuiItem::Cancel
Definition: kstandardguiitem.h:50
KStandardGuiItem::ok
KGuiItem ok()
Returns the 'Ok' gui item.
Definition: kstandardguiitem.cpp:107
kinputdialog.h
KInputDialog::getMultiLineText
QString getMultiLineText(const QString &caption, const QString &label, const QString &value, bool *ok, QWidget *parent)
Static convenience function to get a multiline string from the user.
Definition: kinputdialog.cpp:362
KLineEdit
An enhanced QLineEdit widget for inputting text.
Definition: klineedit.h:149
KIntSpinBox
A QSpinBox with support for arbitrary base numbers.
Definition: knuminput.h:717
QBoxLayout::addStretch
void addStretch(int stretch)
ktextedit.h
KStandardShortcut::whatsThis
QString whatsThis(StandardShortcut)
What's This button.
Definition: kstandardshortcut.cpp:276
QList::ConstIterator
typedef ConstIterator
KComboBox
An enhanced combo box.
Definition: kcombobox.h:148
KCompletion::addItem
void addItem(const QString &item)
Adds an item to the list of available completions.
Definition: kcompletion.cpp:146
klineedit.h
QList::constEnd
const_iterator constEnd() const
QList::constBegin
const_iterator constBegin() const
QLabel
KListWidget
A variant of QListWidget that honors KDE's system-wide settings.
Definition: klistwidget.h:40
kguiitem.h
klistwidget.h
QList::begin
iterator begin()
QListWidgetItem::text
QString text() const
KTextEdit
A KDE'ified QTextEdit.
Definition: ktextedit.h:90
QValidator
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:23:59 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • 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

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal