Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / apps / kdialog / widgets.cpp
blob33a1d3550615f6ae6a96f599ef8d4d3d04694ad5
1 //
2 // Copyright (C) 1998 Matthias Hoelzer <hoelzer@kde.org>
3 // Copyright (C) 2002-2005 David Faure <faure@kde.org>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the7 implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 // Own
22 #include "widgets.h"
24 // Qt
25 #include <QtCore/QFile>
26 #include <QtGui/QDesktopWidget>
27 #include <QtCore/QTextStream>
28 #include <QtGui/QTextCursor>
29 #include <QtGui/QLabel>
31 // KDE
32 #include <klocale.h>
33 #include <kmessagebox.h>
34 #include <kinputdialog.h>
35 #include <kpassworddialog.h>
36 #include <kcombobox.h>
37 #include <kdebug.h>
38 #include <kapplication.h>
39 #include <kcmdlineargs.h>
40 #include <ktextedit.h>
41 #include <kvbox.h>
43 // Local
44 #include "klistboxdialog.h"
45 #include "progressdialog.h"
48 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
49 #include <netwm.h>
50 #endif
52 void Widgets::handleXGeometry(QWidget * dlg)
54 #ifdef Q_WS_X11
55 QString geometry;
56 KCmdLineArgs *args = KCmdLineArgs::parsedArgs("kde");
57 if (args && args->isSet("geometry"))
58 geometry = args->getOption("geometry");
59 if ( ! geometry.isEmpty()) {
60 int x, y;
61 int w, h;
62 int m = XParseGeometry( geometry.toLatin1(), &x, &y, (unsigned int*)&w, (unsigned int*)&h);
63 if ( (m & XNegative) )
64 x = KApplication::desktop()->width() + x - w;
65 if ( (m & YNegative) )
66 y = KApplication::desktop()->height() + y - h;
67 dlg->setGeometry(x, y, w, h);
68 // kDebug() << "x: " << x << " y: " << y << " w: " << w << " h: " << h;
70 #endif
73 bool Widgets::inputBox(QWidget *parent, const QString& title, const QString& text, const QString& init, QString &result)
75 bool ok;
76 QString str = KInputDialog::getText( title, text, init, &ok, parent, 0, 0, QString() );
77 if ( ok )
78 result = str;
79 return ok;
82 bool Widgets::passwordBox(QWidget *parent, const QString& title, const QString& text, QString &result)
84 KPasswordDialog dlg( parent );
85 kapp->setTopWidget( &dlg );
86 dlg.setCaption(title);
87 dlg.setPrompt(text);
89 handleXGeometry(&dlg);
91 bool retcode = (dlg.exec() == QDialog::Accepted);
92 if ( retcode )
93 result = dlg.password();
94 return retcode;
97 int Widgets::textBox(QWidget *parent, int width, int height, const QString& title, const QString& file)
99 // KTextBox dlg(parent, 0, true, width, height, file);
100 KDialog dlg( parent );
101 dlg.setCaption( title );
102 dlg.setButtons( KDialog::Ok );
103 dlg.setModal( true );
105 kapp->setTopWidget( &dlg );
106 KVBox* vbox = new KVBox(&dlg);
107 dlg.setMainWidget(vbox);
109 KTextEdit *edit = new KTextEdit( vbox );
110 edit->setReadOnly(true);
112 QFile f(file);
113 if (!f.open(QIODevice::ReadOnly))
115 kError() << i18n("kdialog: could not open file ") << file << endl;
116 return -1;
118 QTextStream s(&f);
120 while (!s.atEnd())
121 edit->append(s.readLine());
123 edit->setTextCursor(QTextCursor(edit->document()));
125 f.close();
127 if ( width > 0 && height > 0 )
128 dlg.setInitialSize( QSize( width, height ) );
130 handleXGeometry(&dlg);
131 dlg.setCaption(title);
132 return (dlg.exec() == KDialog::Accepted) ? 0 : 1;
135 int Widgets::textInputBox(QWidget *parent, int width, int height, const QString& title, const QStringList& args, QString &result)
137 // KTextBox dlg(parent, 0, true, width, height, file);
138 KDialog dlg( parent );
139 dlg.setCaption( title );
140 dlg.setButtons( KDialog::Ok );
141 dlg.setModal( true );
143 kapp->setTopWidget( &dlg );
144 KVBox* vbox = new KVBox(&dlg);
146 dlg.setMainWidget(vbox);
148 if( args.count() > 0 )
150 QLabel *label = new QLabel(vbox);
151 label->setText(args[0]);
154 KTextEdit *edit = new KTextEdit( vbox );
155 edit->setReadOnly(false);
156 edit->setFocus();
158 if( args.count() > 1 )
159 edit->insertPlainText( args[1] );
161 if ( width > 0 && height > 0 )
162 dlg.setInitialSize( QSize( width, height ) );
164 handleXGeometry(&dlg);
165 dlg.setCaption(title);
166 dlg.exec();
167 result = edit->toPlainText().toLocal8Bit();
168 return 0;
171 bool Widgets::comboBox(QWidget *parent, const QString& title, const QString& text, const QStringList& args,
172 const QString& defaultEntry, QString &result)
174 KDialog dlg( parent );
175 kapp->setTopWidget( &dlg );
176 dlg.setCaption( title );
177 dlg.setButtons( KDialog::Ok|KDialog::Cancel );
178 dlg.setModal( true );
179 dlg.setDefaultButton( KDialog::Ok );
181 KVBox* vbox = new KVBox( &dlg );
182 dlg.setMainWidget( vbox );
184 QLabel label (vbox);
185 label.setText (text);
186 KComboBox combo (vbox);
187 combo.insertItems (0, args);
188 combo.setCurrentIndex( combo.findText( defaultEntry ) );
190 handleXGeometry(&dlg);
192 bool retcode = (dlg.exec() == QDialog::Accepted);
194 if (retcode)
195 result = combo.currentText();
197 return retcode;
200 bool Widgets::listBox(QWidget *parent, const QString& title, const QString& text, const QStringList& args,
201 const QString& defaultEntry, QString &result)
203 KListBoxDialog box(text,parent);
205 kapp->setTopWidget( &box );
206 box.setCaption(title);
208 for (int i = 0; i+1<args.count(); i += 2) {
209 box.insertItem(args[i+1]);
211 box.setCurrentItem( defaultEntry );
213 handleXGeometry(&box);
215 bool retcode = (box.exec() == QDialog::Accepted);
216 if ( retcode )
217 result = args[ box.currentItem()*2 ];
218 return retcode;
222 bool Widgets::checkList(QWidget *parent, const QString& title, const QString& text, const QStringList& args, bool separateOutput, QStringList &result)
224 QStringList entries, tags;
225 QString rs;
227 result.clear();
229 KListBoxDialog box(text,parent);
231 QListWidget &table = box.getTable();
233 kapp->setTopWidget( &box );
234 box.setCaption(title);
236 for (int i=0; i+2<args.count(); i += 3) {
237 tags.append(args[i]);
238 entries.append(args[i+1]);
241 table.addItems(entries);
242 table.setSelectionMode(QListWidget::MultiSelection);
243 table.setCurrentItem(0); // This is to circumvent a Qt bug
245 for (int i=0; i+2<args.count(); i += 3) {
246 table.item( i/3 )->setSelected( args[i+2] == QLatin1String("on") );
249 handleXGeometry(&box);
251 bool retcode = (box.exec() == QDialog::Accepted);
253 if ( retcode ) {
254 if (separateOutput) {
255 for (int i=0; i<table.count(); i++)
256 if (table.item(i)->isSelected())
257 result.append(tags[i]);
258 } else {
259 for (int i=0; i<table.count(); i++)
260 if (table.item(i)->isSelected())
261 rs += QLatin1String("\"") + tags[i] + QLatin1String("\" ");
262 result.append(rs);
265 return retcode;
269 bool Widgets::radioBox(QWidget *parent, const QString& title, const QString& text, const QStringList& args, QString &result)
271 QStringList entries, tags;
273 KListBoxDialog box(text,parent);
275 QListWidget &table = box.getTable();
277 kapp->setTopWidget( &box );
278 box.setCaption(title);
280 for (int i=0; i+2<args.count(); i += 3) {
281 tags.append(args[i]);
282 entries.append(args[i+1]);
285 table.addItems(entries);
287 for (int i=0; i+2<args.count(); i += 3) {
288 table.item( i/3 )->setSelected( args[i+2] == QLatin1String("on") );
291 handleXGeometry(&box);
293 bool retcode = (box.exec() == QDialog::Accepted);
294 if ( retcode )
295 result = tags[ table.currentRow() ];
296 return retcode;
299 bool Widgets::progressBar(QWidget *parent, const QString& title, const QString& text, int totalSteps)
301 ProgressDialog dlg( parent, title, text, totalSteps );
302 kapp->setTopWidget( &dlg );
303 dlg.setCaption( title );
304 handleXGeometry(&dlg);
305 dlg.exec();
306 return dlg.wasCancelled();