2 // Copyright (C) 1998 Matthias Hoelzer <hoelzer@kde.org>
3 // Copyright (C) 2002-2005 David Faure <faure@kde.org>
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.
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.
25 #include <QtCore/QFile>
26 #include <QtGui/QDesktopWidget>
27 #include <QtCore/QTextStream>
28 #include <QtGui/QTextCursor>
29 #include <QtGui/QLabel>
33 #include <kmessagebox.h>
34 #include <kinputdialog.h>
35 #include <kpassworddialog.h>
36 #include <kcombobox.h>
38 #include <kapplication.h>
39 #include <kcmdlineargs.h>
40 #include <ktextedit.h>
44 #include "klistboxdialog.h"
45 #include "progressdialog.h"
48 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
52 void Widgets::handleXGeometry(QWidget
* dlg
)
56 KCmdLineArgs
*args
= KCmdLineArgs::parsedArgs("kde");
57 if (args
&& args
->isSet("geometry"))
58 geometry
= args
->getOption("geometry");
59 if ( ! geometry
.isEmpty()) {
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;
73 bool Widgets::inputBox(QWidget
*parent
, const QString
& title
, const QString
& text
, const QString
& init
, QString
&result
)
76 QString str
= KInputDialog::getText( title
, text
, init
, &ok
, parent
, 0, 0, QString() );
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
);
89 handleXGeometry(&dlg
);
91 bool retcode
= (dlg
.exec() == QDialog::Accepted
);
93 result
= dlg
.password();
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);
113 if (!f
.open(QIODevice::ReadOnly
))
115 kError() << i18n("kdialog: could not open file ") << file
<< endl
;
121 edit
->append(s
.readLine());
123 edit
->setTextCursor(QTextCursor(edit
->document()));
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);
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
);
167 result
= edit
->toPlainText().toLocal8Bit();
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
);
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
);
195 result
= combo
.currentText();
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
);
217 result
= args
[ box
.currentItem()*2 ];
222 bool Widgets::checkList(QWidget
*parent
, const QString
& title
, const QString
& text
, const QStringList
& args
, bool separateOutput
, QStringList
&result
)
224 QStringList entries
, tags
;
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
);
254 if (separateOutput
) {
255 for (int i
=0; i
<table
.count(); i
++)
256 if (table
.item(i
)->isSelected())
257 result
.append(tags
[i
]);
259 for (int i
=0; i
<table
.count(); i
++)
260 if (table
.item(i
)->isSelected())
261 rs
+= QLatin1String("\"") + tags
[i
] + QLatin1String("\" ");
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
);
295 result
= tags
[ table
.currentRow() ];
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
);
306 return dlg
.wasCancelled();