00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
#include "qinputbox.h"
00021
00022
#include <qlineedit.h>
00023
#include <qlabel.h>
00024
#include <qpushbutton.h>
00025
#include <qlayout.h>
00026
#include <qregexp.h>
00027
#include <klocale.h>
00028
00029 QInputBox::QInputBox(
QWidget *parent,
const char *name)
00030 :
QDialog(parent, name, true)
00031 {
00032 count_ = 0;
00033 initialized_ =
false;
00034 edits_.setAutoDelete(
false);
00035 labels_.setAutoDelete(
false);
00036 init(1);
00037 }
00038
00039 QInputBox::QInputBox(
int numlines,
QWidget *parent,
const char *name)
00040 :
QDialog(parent, name, true)
00041 {
00042 count_ = 0;
00043 initialized_ =
false;
00044 edits_.setAutoDelete(
false);
00045 labels_.setAutoDelete(
false);
00046 init(numlines);
00047 }
00048
00049 QInputBox::~QInputBox()
00050 {
00051 }
00052
00053
QSize QInputBox::sizeHint()
const
00054
{
00055
QSize s1(count_ > 0 ? labels_.getFirst()->sizeHint() :
QSize(0,0)), s2(count_ > 0 ? edits_.getFirst()->sizeHint() :
QSize(0,0)), s3(okbtn_->sizeHint()),s4(cancelbtn_->sizeHint());
00056
return QSize(QMAX(s1.width(),s3.width()+s4.width())+120, count_*(s1.height()+s2.height()+13)+s3.height()+20);
00057 }
00058
00059
QString QInputBox::text(
int index)
00060 {
00061
if (index >= 0 && index < count_)
00062 {
00063
return edits_.at(index)->text();
00064 }
00065
else return QString(
"");
00066 }
00067
00068
void QInputBox::setText(
const QString& txt,
int index)
00069 {
00070
if (index >= 0 && index < count_)
00071 {
00072 edits_.at(index)->setText(txt);
00073 edits_.at(index)->selectAll();
00074 }
00075 }
00076
00077
void QInputBox::setMessage(
const QString& msg,
int index)
00078 {
00079
if (index >= 0 && index < count_)
00080 {
00081 labels_.at(index)->setText(msg);
00082 resize(sizeHint());
00083 }
00084 }
00085
00086
QString QInputBox::inputBox(
QWidget *parent,
const QString& caption,
const QString& msg,
const QString& txt,
bool *ok)
00087 {
00088 QInputBox dlg(parent);
00089 dlg.setMessage(msg);
00090 dlg.setCaption(caption);
00091 dlg.setText(txt);
00092
QString result(
"");
00093
if (ok) *ok =
false;
00094
if (dlg.exec())
00095 {
00096 result = dlg.text();
00097
if (ok) *ok =
true;
00098 }
00099
return result;
00100 }
00101
00102
QStringList QInputBox::inputBox(
QWidget *parent,
const QString& caption,
const QStringList& msgs,
int numlines,
bool *ok)
00103 {
00104
if (numlines <= 0)
return QStringList();
00105
00106 QInputBox dlg(numlines, parent);
00107 QStringList::ConstIterator it;
00108
int i = 0;
00109
for (it=msgs.begin(); it!=msgs.end() && i<numlines; i++, ++it)
00110 dlg.setMessage(*it, i);
00111 dlg.setCaption(caption);
00112
QStringList res;
00113
if (ok) *ok =
false;
00114
if (dlg.exec())
00115 {
00116
for (i=0;i<numlines;i++)
00117 res.append(dlg.text(i));
00118
if (ok) *ok =
true;
00119 }
00120
return res;
00121 }
00122
00123
void QInputBox::init(
int numlines)
00124 {
00125
if (initialized_ || numlines <= 0)
return;
00126 initialized_ =
true;
00127
00128
QVBoxLayout *main_ =
new QVBoxLayout(
this, 10, 0);
00129 count_ = numlines;
00130
for (
int i=0;i<numlines;i++)
00131 {
00132
QLineEdit *edit_ =
new QLineEdit(
this);
00133 edits_.append(edit_);
00134
QLabel *label_ =
new QLabel(i18n(
"Input value:"),
this);
00135 labels_.append(label_);
00136
00137 main_->addWidget(label_);
00138 main_->addSpacing(3);
00139 main_->addWidget(edit_);
00140
00141 main_->addSpacing(10);
00142 }
00143
00144 okbtn_ =
new KPushButton(KStdGuiItem::ok(),
this);
00145 connect(okbtn_, SIGNAL(clicked()), SLOT(accept()));
00146 okbtn_->setDefault(
true);
00147
00148 cancelbtn_ =
new KPushButton(KStdGuiItem::cancel(),
this);
00149 connect(cancelbtn_, SIGNAL(clicked()), SLOT(reject()));
00150
00151
QHBoxLayout *btnlayout_ =
new QHBoxLayout(0, 0, 10);
00152
00153 main_->addLayout(btnlayout_);
00154 btnlayout_->addStretch(1);
00155 btnlayout_->addWidget(okbtn_);
00156 btnlayout_->addWidget(cancelbtn_);
00157
00158 edits_.first()->setFocus();
00159
00160 resize(sizeHint());
00161 }