summaryrefslogtreecommitdiffstats
path: root/kregexpeditor/repeatwidget.cpp
blob: 085c36b7ca66b7971b5acb5c52a53acf3395b637 (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
/*
 *  Copyright (c) 2002-2003 Jesper K. Pedersen <blackie@kde.org>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  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 TQT_ONLY
  #include "compat.h"
#else
  #include <tdelocale.h>
  #include <kdialogbase.h>
  #include "repeatwidget.moc"
#endif

#include "repeatwidget.h"
#include "concwidget.h"
#include "repeatregexp.h"
#include <tqpainter.h>
#include <tqcursor.h>
#include <tqspinbox.h>
#include <tqradiobutton.h>
#include <tqlayout.h>
#include <tqgrid.h>
#include <tqvbuttongroup.h>
#include "kwidgetstreamer.h"

RepeatWidget::RepeatWidget(RegExpEditorWindow* editorWindow, TQWidget *parent,
                           const char *name)
  : SingleContainerWidget(editorWindow, parent, name ? name : "RepeatWidget")
{
  _child = new ConcWidget(editorWindow, this);
  init();
}

RepeatWidget::RepeatWidget( RepeatRegExp* regexp, RegExpEditorWindow* editorWindow,
                            TQWidget* parent, const char* name )
  :SingleContainerWidget( editorWindow, parent, name )
{
  init();
  RegExpWidget* child = WidgetFactory::createWidget( regexp->child(), editorWindow, this );
  if ( ! (_child = dynamic_cast<ConcWidget*>( child ) ) )
    _child = new ConcWidget( editorWindow, child, this );

  if ( regexp->max() == -1 ) {
    if ( regexp->min() == 0 ) {
      _content->set( RepeatRangeWindow::ANY, regexp->min(), regexp->max() );
    }
    else {
      _content->set( RepeatRangeWindow::ATLEAST, regexp->min(), regexp->max() );
    }
  }
  else {
    if ( regexp->min() == 0 ) {
      _content->set( RepeatRangeWindow::ATMOST, regexp->min(), regexp->max() );
    }
    else if ( regexp->min() == regexp->max() ) {
      _content->set( RepeatRangeWindow::EXACTLY, regexp->min(), regexp->max() );
    }
    else {
      _content->set( RepeatRangeWindow::MINMAX, regexp->min(), regexp->max() );
    }
  }
}

void RepeatWidget::init()
{
  _configWindow = new KDialogBase( this, "_configWindow", true,
                                   i18n("Number of Times to Repeat Content"),
                                   KDialogBase::Ok | KDialogBase::Cancel);
  _content = new RepeatRangeWindow( _configWindow );
  _configWindow->setMainWidget( _content );
  connect( _configWindow, TQT_SIGNAL( cancelClicked() ), this, TQT_SLOT( slotConfigCanceled() ) );
  connect(_configWindow, TQT_SIGNAL(finished()), this, TQT_SLOT(slotConfigWindowClosed()));
}


TQSize RepeatWidget::sizeHint() const
{
  // TODO: Merge with LookAheadWidget::sizeHint
  TQFontMetrics metrics = fontMetrics();
  _textSize = metrics.size( 0, _content->text() );

  _childSize = _child->sizeHint();

  int height = _textSize.height() + bdSize + _childSize.height() + bdSize + 2*pw;
  int width  = 2 * pw + TQMAX(_childSize.width(), 4*bdSize + _textSize.width());
  return TQSize(width,height);
}

void RepeatWidget::paintEvent( TQPaintEvent *e )
{
  // TODO: Merge with LookAheadWidget::paintEvent
  TQSize mySize = sizeHint();
  TQPainter painter(this);

  drawPossibleSelection( painter, mySize );

  // move the child to its position and resize it.
  _child->move(pw,_textSize.height()+bdSize);
  TQSize curChildSize = _child->size();
  TQSize newChildSize = TQSize(mySize.width() - 2*pw, _childSize.height());
  if ( curChildSize != newChildSize ) {
    _child->resize(newChildSize);
    // I resized the child, so give it a chance to relect thus.
    _child->update();
  }

  // Draw the border and the text.
  int startY = _textSize.height()/2;

  // Top lines and text
  painter.drawLine(pw,startY,bdSize,startY);
  painter.drawText(pw+2*bdSize,0,_textSize.width(), _textSize.height(),0,
                   _content->text());
  int offset = pw + 3*bdSize + _textSize.width();
  painter.drawLine(offset,startY,mySize.width()-pw,startY);

  // horizontal lines
  painter.drawLine(0,startY,0,mySize.height()-pw);
  painter.drawLine(mySize.width()-pw,startY,mySize.width()-pw, mySize.height()-pw);

  // buttom line
  painter.drawLine(0, mySize.height()-pw, mySize.width()-pw, mySize.height()-pw);

  SingleContainerWidget::paintEvent(e);
}

RegExp* RepeatWidget::regExp() const
{
	return new RepeatRegExp( isSelected(), _content->min(), _content->max(), _child->regExp() );
}

void RepeatWidget::slotConfigWindowClosed()
{
  _editorWindow->updateContent( 0 );
  update();
}

void RepeatWidget::slotConfigCanceled()
{
  TQDataStream stream( _backup, IO_ReadOnly );
  KWidgetStreamer streamer;
  streamer.fromStream( stream, TQT_TQOBJECT(_content) );
  repaint();
}

int RepeatWidget::edit()
{
  _configWindow->move(TQCursor::pos() - TQPoint(_configWindow->sizeHint().width()/2,
                                              _configWindow->sizeHint().height()/2)  );
  TQDataStream stream( _backup, IO_WriteOnly );
  KWidgetStreamer streamer;
  streamer.toStream( TQT_TQOBJECT(_content), stream );

  return _configWindow->exec();
}

//--------------------------------------------------------------------------------
RepeatRangeWindow::RepeatRangeWindow( TQWidget* parent, const char* name )
  : TQVBox( parent, name ? name : "RepeatRangeWindow" )
{
  setSpacing( 6 );

  _group = new TQVButtonGroup( i18n("Times to Match"), this, "groupbox" );

  // Any number of times
  TQRadioButton* radioBut = new TQRadioButton(i18n("Any number of times (including zero times)"),
                                            _group,
                                            "RepeatRangeWindow::choice any times");

  _group->insert(radioBut, ANY);

  TQWidget* container = new TQWidget( _group );
  TQHBoxLayout* lay = new TQHBoxLayout( container );
  TQGrid* grid = new TQGrid( 3, container );
  grid->setSpacing( 5 );

  lay->addWidget( grid );
  lay->addStretch( 1 );

  createLine( grid, i18n( "At least" ), &_leastTimes, ATLEAST );
  createLine( grid, i18n( "At most" ), &_mostTimes, ATMOST );
  createLine( grid, i18n( "Exactly" ), &_exactlyTimes, EXACTLY );

  // from ___ to ___ times
  radioBut = new TQRadioButton(i18n( "From" ), grid, "RepeatRangeWindow::from");
  _group->insert( radioBut, MINMAX );

  _rangeFrom = new TQSpinBox( 1, 999, 1, grid);

  TQHBox* box = new TQHBox( grid );
  box->setSpacing( 5 );

  (void) new TQLabel(i18n( "to" ), box);
  _rangeTo = new TQSpinBox( 1, 999, 1, box );
  (void) new TQLabel( i18n( "time(s)" ), box );

  connect( _rangeFrom, TQT_SIGNAL( valueChanged( int ) ), this, TQT_SLOT( slotUpdateMaxVal( int ) ) );
  connect( _rangeTo, TQT_SIGNAL( valueChanged( int ) ), this, TQT_SLOT( slotUpdateMinVal( int ) ) );

  // set a default button.
  _group->setButton(ANY);
  slotItemChange( ANY );


  connect( _group, TQT_SIGNAL( clicked( int ) ), this, TQT_SLOT( slotItemChange( int ) ) );
}


void RepeatRangeWindow::createLine( TQWidget* parent, TQString text, TQSpinBox** spin, REPEATTYPE tp )
{

  TQRadioButton* radioBut = new TQRadioButton(text, parent);
  *spin = new TQSpinBox( 1, 999, 1, parent);
  (*spin)->setValue(1);

  (void) new TQLabel(i18n("time(s)"), parent);
  _group->insert(radioBut, tp);
}

void RepeatRangeWindow::slotItemChange( int which )
{
  _leastTimes->setEnabled( false );
  _mostTimes->setEnabled( false );
  _exactlyTimes->setEnabled( false );
  _rangeFrom->setEnabled( false );
  _rangeTo->setEnabled( false );

  switch ( which ) {
  case ANY: break;
  case ATLEAST: _leastTimes->setEnabled( true ); break;
  case ATMOST: _mostTimes->setEnabled( true ); break;
  case EXACTLY: _exactlyTimes->setEnabled( true ); break;
  case MINMAX:
    _rangeFrom->setEnabled( true );
    _rangeTo->setEnabled( true );
    break;
  }
}

void RepeatRangeWindow::slotUpdateMinVal( int maxVal )
{
  if ( _rangeFrom->value() > maxVal ) {
    _rangeFrom->setValue( maxVal );
  }
}

void RepeatRangeWindow::slotUpdateMaxVal( int minVal )
{
  if ( _rangeTo->value() < minVal ) {
    _rangeTo->setValue( minVal );
  }
}

TQString RepeatRangeWindow::text()
{
  switch ( _group->id(_group->selected()) ) {
  case ANY: return i18n("Repeated Any Number of Times");
  case ATLEAST: return i18n("Repeated at Least 1 Time", "Repeated at Least %n Times", _leastTimes->value() );
  case ATMOST: return i18n("Repeated at Most 1 Time", "Repeated at Most %n Times", _mostTimes->value() );
  case EXACTLY: return i18n("Repeated Exactly 1 Time", "Repeated Exactly %n Times", _exactlyTimes->value() );
  case MINMAX: return i18n("Repeated From %1 to %2 Times")
                 .arg( _rangeFrom->value() ).arg( _rangeTo->value() );
  }
  tqFatal("Fall through!");
  return TQString::fromLocal8Bit("");
}

int RepeatRangeWindow::min()
{
  switch ( _group->id(_group->selected()) ) {
  case ANY: return 0;
  case ATLEAST: return _leastTimes->value();
  case ATMOST: return 0;
  case EXACTLY: return _exactlyTimes->value();
  case MINMAX: return _rangeFrom->value();
  }
  tqFatal("Fall through!");
  return -1;
}

int RepeatRangeWindow::max()
{
  switch ( _group->id(_group->selected()) ) {
  case ANY: return -1;
  case ATLEAST: return -1;
  case ATMOST: return _mostTimes->value();
  case EXACTLY: return _exactlyTimes->value();
  case MINMAX:  return _rangeTo->value();
  }
  tqFatal("Fall through!");
  return -1;
}

void RepeatRangeWindow::set( REPEATTYPE tp, int min, int max )
{
  _group->setButton( tp );
  switch ( tp ) {
  case ANY:
    break;
  case ATLEAST:
    _leastTimes->setValue( min );
    break;
  case ATMOST:
    _mostTimes->setValue( max );
    break;
  case EXACTLY:
    _exactlyTimes->setValue( min );
    break;
  case MINMAX:
    _rangeFrom->setValue( min );
    _rangeTo->setValue( max );
    break;
  }
}