summaryrefslogtreecommitdiffstats
path: root/tdeui/kinputdialog.cpp
blob: 61b30cce1113d80312f4319608c6bc00e388d473 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
  Copyright (C) 2003 Nadeem Hasan <[email protected]>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#ifdef USE_QT4
#undef Status
#endif // USE_QT4

#include <tqlayout.h>
#include <tqlabel.h>
#include <tqvalidator.h>
#include <tqwhatsthis.h>

#include <klineedit.h>
#include <knuminput.h>
#include <kcombobox.h>
#include <klistbox.h>
#include <ktextedit.h>

#include "kinputdialog.h"

class KInputDialogPrivate
{
  public:
    KInputDialogPrivate();

    TQLabel *m_label;
    KLineEdit *m_lineEdit;
    KIntSpinBox *m_intSpinBox;
    KDoubleSpinBox *m_doubleSpinBox;
    KComboBox *m_comboBox;
    KListBox *m_listBox;
    KTextEdit *m_textEdit;
};

KInputDialogPrivate::KInputDialogPrivate()
    : m_label( 0L ), m_lineEdit( 0L ), m_intSpinBox( 0L ),
      m_doubleSpinBox( 0L ), m_comboBox( 0L )
{
}

KInputDialog::KInputDialog( const TQString &caption, const TQString &label,
    const TQString &value, TQWidget *parent, const char *name,
    TQValidator *validator, const TQString &mask )
    : KDialogBase( parent, name, true, caption, Ok|Cancel|User1, Ok, true,
    KStdGuiItem::clear() ),
    d( new KInputDialogPrivate() )
{
  TQFrame *frame = makeMainWidget();
  TQVBoxLayout *layout = new TQVBoxLayout( frame, 0, spacingHint() );

  d->m_label = new TQLabel( label, frame );
  layout->addWidget( d->m_label );

  d->m_lineEdit = new KLineEdit( value, frame );
  layout->addWidget( d->m_lineEdit );

  d->m_lineEdit->setFocus();
  d->m_label->setBuddy( d->m_lineEdit );

  layout->addStretch();

  if ( validator )
    d->m_lineEdit->setValidator( validator );

  if ( !mask.isEmpty() )
    d->m_lineEdit->setInputMask( mask );

  connect( d->m_lineEdit, TQT_SIGNAL( textChanged( const TQString & ) ),
      TQT_SLOT( slotEditTextChanged( const TQString & ) ) );
  connect( this, TQT_SIGNAL( user1Clicked() ), d->m_lineEdit, TQT_SLOT( clear() ) );

  slotEditTextChanged( value );
  setMinimumWidth( 350 );
}

KInputDialog::KInputDialog( const TQString &caption, const TQString &label,
    const TQString &value, TQWidget *parent, const char *name )
    : KDialogBase( parent, name, true, caption, Ok|Cancel|User1, Ok, false,
    KStdGuiItem::clear() ),
    d( new KInputDialogPrivate() )
{
  TQFrame *frame = makeMainWidget();
  TQVBoxLayout *layout = new TQVBoxLayout( frame, 0, spacingHint() );

  d->m_label = new TQLabel( label, frame );
  layout->addWidget( d->m_label );

  d->m_textEdit = new KTextEdit( frame );
  d->m_textEdit->setTextFormat( PlainText );
  d->m_textEdit->setText( value );
  layout->addWidget( d->m_textEdit, 10 );

  d->m_textEdit->setFocus();
  d->m_label->setBuddy( d->m_textEdit );

  connect( this, TQT_SIGNAL( user1Clicked() ), d->m_textEdit, TQT_SLOT( clear() ) );

  setMinimumWidth( 400 );
}

KInputDialog::KInputDialog( const TQString &caption, const TQString &label,
    int value, int minValue, int maxValue, int step, int base,
    TQWidget *parent, const char *name )
    : KDialogBase( parent, name, true, caption, Ok|Cancel, Ok, true ),
    d( new KInputDialogPrivate() )
{
  TQFrame *frame = makeMainWidget();
  TQVBoxLayout *layout = new TQVBoxLayout( frame, 0, spacingHint() );

  d->m_label = new TQLabel( label, frame );
  layout->addWidget( d->m_label );

  d->m_intSpinBox = new KIntSpinBox( minValue, maxValue, step, value,
      base, frame );
  layout->addWidget( d->m_intSpinBox );

  layout->addStretch();

  d->m_intSpinBox->setFocus();
  setMinimumWidth( 300 );
}

KInputDialog::KInputDialog( const TQString &caption, const TQString &label,
    double value, double minValue, double maxValue, double step, int decimals,
    TQWidget *parent, const char *name )
    : KDialogBase( parent, name, true, caption, Ok|Cancel, Ok, true ),
    d( new KInputDialogPrivate() )
{
  TQFrame *frame = makeMainWidget();
  TQVBoxLayout *layout = new TQVBoxLayout( frame, 0, spacingHint() );

  d->m_label = new TQLabel( label, frame );
  layout->addWidget( d->m_label );

  d->m_doubleSpinBox = new KDoubleSpinBox( minValue, maxValue, step, value,
      decimals, frame );
  layout->addWidget( d->m_doubleSpinBox );

  layout->addStretch();

  d->m_doubleSpinBox->setFocus();
  setMinimumWidth( 300 );
}

KInputDialog::KInputDialog( const TQString &caption, const TQString &label,
    const TQStringList &list, int current, bool editable, TQWidget *parent,
    const char *name )
    : KDialogBase( parent, name, true, caption, Ok|Cancel|User1, Ok, true,
    KStdGuiItem::clear() ),
    d( new KInputDialogPrivate() )
{
  showButton( User1, editable );

  TQFrame *frame = makeMainWidget();
  TQVBoxLayout *layout = new TQVBoxLayout( frame, 0, spacingHint() );

  d->m_label = new TQLabel( label, frame );
  layout->addWidget( d->m_label );

  if ( editable )
  {
    d->m_comboBox = new KComboBox( editable, frame );
    d->m_comboBox->insertStringList( list );
    d->m_comboBox->setCurrentItem( current );
    layout->addWidget( d->m_comboBox );

    connect( d->m_comboBox, TQT_SIGNAL( textChanged( const TQString & ) ),
      TQT_SLOT( slotUpdateButtons( const TQString & ) ) );
    connect( this, TQT_SIGNAL( user1Clicked() ),
      d->m_comboBox, TQT_SLOT( clearEdit() ) );
    slotUpdateButtons( d->m_comboBox->currentText() );
    d->m_comboBox->setFocus();
  } else {
    d->m_listBox = new KListBox( frame );
    d->m_listBox->insertStringList( list );
    d->m_listBox->setSelected( current, true );
    d->m_listBox->ensureCurrentVisible();
    layout->addWidget( d->m_listBox, 10 );
    connect( d->m_listBox, TQT_SIGNAL( doubleClicked( TQListBoxItem * ) ),
      TQT_SLOT( slotOk() ) );
    connect( d->m_listBox, TQT_SIGNAL( returnPressed( TQListBoxItem * ) ),
      TQT_SLOT( slotOk() ) );

    d->m_listBox->setFocus();
  }

  layout->addStretch();

  setMinimumWidth( 320 );
}

KInputDialog::KInputDialog( const TQString &caption, const TQString &label,
    const TQStringList &list, const TQStringList &select, bool multiple,
    TQWidget *parent, const char *name )
    : KDialogBase( parent, name, true, caption, Ok|Cancel, Ok, true ),
    d( new KInputDialogPrivate() )
{
  TQFrame *frame = makeMainWidget();
  TQVBoxLayout *layout = new TQVBoxLayout( frame, 0, spacingHint() );

  d->m_label = new TQLabel( label, frame );
  layout->addWidget( d->m_label );

  d->m_listBox = new KListBox( frame );
  d->m_listBox->insertStringList( list );
  layout->addWidget( d->m_listBox );

  TQListBoxItem *item;

  if ( multiple )
  {
    d->m_listBox->setSelectionMode( TQListBox::Extended );

    for ( TQStringList::ConstIterator it=select.begin(); it!=select.end(); ++it )
    {
      item = d->m_listBox->findItem( *it, CaseSensitive|ExactMatch );
      if ( item )
        d->m_listBox->setSelected( item, true );
    }
  }
  else
  {
    connect( d->m_listBox, TQT_SIGNAL( doubleClicked( TQListBoxItem * ) ),
      TQT_SLOT( slotOk() ) );
    connect( d->m_listBox, TQT_SIGNAL( returnPressed( TQListBoxItem * ) ),
      TQT_SLOT( slotOk() ) );

    TQString text = select.first();
    item = d->m_listBox->findItem( text, CaseSensitive|ExactMatch );
    if ( item )
      d->m_listBox->setSelected( item, true );
  }

  d->m_listBox->ensureCurrentVisible();
  d->m_listBox->setFocus();

  layout->addStretch();

  setMinimumWidth( 320 );
}

KInputDialog::~KInputDialog()
{
  delete d;
}

TQString KInputDialog::getText( const TQString &caption, const TQString &label,
    const TQString &value, bool *ok, TQWidget *parent, const char *name,
    TQValidator *validator, const TQString &mask )
{
  return text( caption, label, value, ok, parent, name, validator, mask,
               TQString::null );
}

TQString KInputDialog::text( const TQString &caption,
    const TQString &label, const TQString &value, bool *ok, TQWidget *parent,
    const char *name, TQValidator *validator, const TQString &mask,
    const TQString &whatsThis )
{
  KInputDialog dlg( caption, label, value, parent, name, validator, mask );

  if( !whatsThis.isEmpty() )
    TQWhatsThis::add( dlg.lineEdit(), whatsThis );

  bool _ok = ( dlg.exec() == Accepted );

  if ( ok )
    *ok = _ok;

  TQString result;
  if ( _ok )
    result = dlg.lineEdit()->text();

  // A validator may explicitly allow leading and trailing whitespace
  if ( !validator )
    result = result.stripWhiteSpace();

  return result;
}

TQString KInputDialog::getMultiLineText( const TQString &caption,
    const TQString &label, const TQString &value, bool *ok,
    TQWidget *parent, const char *name )
{
  KInputDialog dlg( caption, label, value, parent, name );

  bool _ok = ( dlg.exec() == Accepted );

  if ( ok )
    *ok = _ok;

  TQString result;
  if ( _ok )
    result = dlg.textEdit()->text();

  return result;
}

int KInputDialog::getInteger( const TQString &caption, const TQString &label,
    int value, int minValue, int maxValue, int step, int base, bool *ok,
    TQWidget *parent, const char *name )
{
  KInputDialog dlg( caption, label, value, minValue,
    maxValue, step, base, parent, name );

  bool _ok = ( dlg.exec() == Accepted );

  if ( ok )
    *ok = _ok;

  int result=0;
  if ( _ok )
    result = dlg.intSpinBox()->value();

  return result;
}

int KInputDialog::getInteger( const TQString &caption, const TQString &label,
    int value, int minValue, int maxValue, int step, bool *ok,
    TQWidget *parent, const char *name )
{
  return getInteger( caption, label, value, minValue, maxValue, step,
    10, ok, parent, name );
}

double KInputDialog::getDouble( const TQString &caption, const TQString &label,
    double value, double minValue, double maxValue, double step, int decimals,
    bool *ok, TQWidget *parent, const char *name )
{
  KInputDialog dlg( caption, label, value, minValue,
    maxValue, step, decimals, parent, name );

  bool _ok = ( dlg.exec() == Accepted );

  if ( ok )
    *ok = _ok;

  double result=0;
  if ( _ok )
    result = dlg.doubleSpinBox()->value();

  return result;
}

double KInputDialog::getDouble( const TQString &caption, const TQString &label,
    double value, double minValue, double maxValue, int decimals,
    bool *ok, TQWidget *parent, const char *name )
{
  return getDouble( caption, label, value, minValue, maxValue, 0.1, decimals,
    ok, parent, name );
}

TQString KInputDialog::getItem( const TQString &caption, const TQString &label,
    const TQStringList &list, int current, bool editable, bool *ok,
    TQWidget *parent, const char *name )
{
  KInputDialog dlg( caption, label, list, current,
    editable, parent, name );
  if ( !editable)
  {
      connect( dlg.listBox(),  TQT_SIGNAL(doubleClicked ( TQListBoxItem *)), &dlg, TQT_SLOT( slotOk()));
  }
  bool _ok = ( dlg.exec() == Accepted );

  if ( ok )
    *ok = _ok;

  TQString result;
  if ( _ok )
    if ( editable )
      result = dlg.comboBox()->currentText();
    else
      result = dlg.listBox()->currentText();

  return result;
}

TQStringList KInputDialog::getItemList( const TQString &caption,
    const TQString &label, const TQStringList &list, const TQStringList &select,
    bool multiple, bool *ok, TQWidget *parent, const char *name )
{
  KInputDialog dlg( caption, label, list, select,
    multiple, parent, name );

  bool _ok = ( dlg.exec() == Accepted );

  if ( ok )
    *ok = _ok;

  TQStringList result;
  if ( _ok )
  {
    for (const TQListBoxItem* i = dlg.listBox()->firstItem(); i != 0; i = i->next() )
      if ( i->isSelected() )
        result.append( i->text() );
  }

  return result;
}

void KInputDialog::slotEditTextChanged( const TQString &text )
{
  bool on;
  if ( lineEdit()->validator() ) {
    TQString str = lineEdit()->text();
    int index = lineEdit()->cursorPosition();
    on = ( lineEdit()->validator()->validate( str, index )
      == TQValidator::Acceptable );
  } else {
    on = !text.stripWhiteSpace().isEmpty();
  }

  enableButton( Ok, on );
  enableButton( User1, !text.isEmpty() );
}

void KInputDialog::slotUpdateButtons( const TQString &text )
{
  enableButton( Ok, !text.isEmpty() );
  enableButton( User1, !text.isEmpty() );
}

KLineEdit *KInputDialog::lineEdit() const
{
  return d->m_lineEdit;
}

KIntSpinBox *KInputDialog::intSpinBox() const
{
  return d->m_intSpinBox;
}

KDoubleSpinBox *KInputDialog::doubleSpinBox() const
{
  return d->m_doubleSpinBox;
}

KComboBox *KInputDialog::comboBox() const
{
  return d->m_comboBox;
}

KListBox *KInputDialog::listBox() const
{
  return d->m_listBox;
}

KTextEdit *KInputDialog::textEdit() const
{
  return d->m_textEdit;
}

#include "kinputdialog.moc"

/* vim: set ai et sw=2 ts=2
*/