summaryrefslogtreecommitdiffstats
path: root/kregexpeditor/concwidget.cpp
blob: d70e735b606895b93c15e2625ec2d654053785c7 (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
/*
 *  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.
 **/
#include "concregexp.h"
#include "concwidget.h"
#include "dragaccepter.h"

#include <tqpainter.h>

ConcWidget::ConcWidget(RegExpEditorWindow* editorWindow, TQWidget *tqparent,
                       const char *name)
  :MultiContainerWidget(editorWindow, tqparent, name == 0 ? "concwidget" : name)
{
  init();
  DragAccepter *accepter = new DragAccepter(editorWindow, this);
  accepter->show();
  _tqchildren.append(accepter);
}


ConcWidget::ConcWidget(RegExpEditorWindow* editorWindow, RegExpWidget *child,
                       TQWidget *tqparent, const char *name)
  :MultiContainerWidget(editorWindow, tqparent, name == 0 ? "concwidget" : name)
{
  init();
  DragAccepter *accepter = new DragAccepter(editorWindow, this);
  _tqchildren.append(accepter);
  child->reparent(this, TQPoint(0,0), false);
  addNewChild(accepter, child);
}

ConcWidget::ConcWidget( RegExpEditorWindow* editorWindow, ConcWidget* origConc,
                    unsigned int start, unsigned int end)
  :MultiContainerWidget(editorWindow, 0, "Splitted ConcWidget")
{
  init();
  _tqchildren.prepend( new DragAccepter(editorWindow, this) );
  for (unsigned int i = end; i >= start; i--) {
    RegExpWidget* child = origConc->_tqchildren.take( i );
    _tqchildren.prepend( child );
    child->reparent( this, TQPoint(0,0), false);
  }
  _tqchildren.prepend( new DragAccepter(editorWindow, this) );
}

ConcWidget::ConcWidget( ConcRegExp* regexp, RegExpEditorWindow* editorWindow,
            TQWidget* tqparent, const char* name )
  :MultiContainerWidget( editorWindow, tqparent, name == 0 ? "concwidget" : name )
{
  init();
  DragAccepter *accepter = new DragAccepter(editorWindow, this);
  _tqchildren.append(accepter);

  RegExpList list = regexp->tqchildren();
  for ( RegExpListIt it(list); *it; ++it ) {
    RegExpWidget* child = WidgetFactory::createWidget( *it, editorWindow, this );
    append( child );
  }
}

void ConcWidget::init()
{
  _maxSelectedHeight = 0;
}


TQSize ConcWidget::tqsizeHint() const
{
  int tqchildrenWidth = 0;
  int tqchildrenHeight = 0;
  TQPtrListIterator<RegExpWidget> it(_tqchildren);

  for ( ; *it; ++it) {
    TQSize thisChildSize = (*it)->tqsizeHint();
    tqchildrenWidth += thisChildSize.width();
    tqchildrenHeight = TQMAX(tqchildrenHeight, thisChildSize.height());
  }

  return TQSize(tqchildrenWidth, tqchildrenHeight);
}

void ConcWidget::paintEvent( TQPaintEvent *e)
{
  Q_ASSERT( dynamic_cast<DragAccepter*>(_tqchildren.at(0)) );
  // if this fails, then I should check the location of the show()
  Q_ASSERT( _tqchildren.count() == 1 ||
          ( _tqchildren.count() >=3 &&
            dynamic_cast<DragAccepter*>(_tqchildren.at(_tqchildren.count()-1)) ) );

  if ( _tqchildren.count() == 1) {
    // There is only an accepter, lets give it all the space.
    _tqchildren.at(0)->setGeometry( 0, 0, size().width(), size().height() );
  }
  else {
    TQSize myReqSize = tqsizeHint();
    TQSize mySize(TQMAX(myReqSize.width(), size().width()),
                 TQMAX(myReqSize.height(), size().height()));

    // If the widget needs less space than it can get then this space should
    // be given to the leftmost and rightmost accepter. So lets calculate
    // this extra space.
    int extra = 0;
    if (size().width() > myReqSize.width()) {
      extra = (size().width() - myReqSize.width())/2;
    }

    TQPainter painter( this );

    drawPossibleSelection( painter, mySize );

    int lastHeight = 0;
    int offset = 0;

    for (unsigned int i = 1; i < _tqchildren.count(); i += 2 ) {
      DragAccepter* accepter = dynamic_cast<DragAccepter*>(_tqchildren.at(i-1));
      if (!accepter)
        continue;
      RegExpWidget* child = _tqchildren.at(i);

      TQSize childSize = child->tqsizeHint();
      TQSize curChildSize = child->size();

      //----------------------------- first place the accepter
      int x = offset;
      int w = accepter->tqsizeHint().width();
      if ( i == 1 ) w+= extra;
      int h = TQMAX( lastHeight, childSize.height() );
      int y = (mySize.height() - h)/2;
      accepter->setGeometry( x, y, w, h );

      offset += w;
      lastHeight = childSize.height();


      //------------------------------ Draw Accepter selection
      if ( accepter->isSelected() ) {
        y = (mySize.height()-_maxSelectedHeight)/2;
        h = _maxSelectedHeight;
        painter.fillRect( x, y, w, h, TQBrush( gray ) );
      }

      //-------------------------------------- place the child
      x = offset;
      h = childSize.height();
      w = childSize.width();
      y = (mySize.height() - h)/2;
      child->setGeometry( x, y, w, h );
      if ( childSize != curChildSize ) {
        // I resized the child, so give it a chance to relect thus.
        child->update();
      }

      offset += w;

      //------------------------------ Draw Accepter selection
      if ( child->isSelected() ) {
        y = (mySize.height()-_maxSelectedHeight)/2;
        h = _maxSelectedHeight;
        painter.fillRect( x, y, w, h, TQBrush( gray ) );
      }
    }

    //---------------------- Finally place the last accepter.
    DragAccepter* accepter =
      dynamic_cast<DragAccepter*>(_tqchildren.at(_tqchildren.count()-1));
    // dynamic_cast is ASSERTed at top
    int x = offset;
    int h = lastHeight;
    int w = accepter->tqsizeHint().width() + extra;
    int y = (mySize.height()-h)/2;
    accepter->setGeometry( x, y, w, h );
  }
  MultiContainerWidget::paintEvent( e );
}

void ConcWidget::mousePressEvent ( TQMouseEvent* event )
{
  if ( event->button() == Qt::RightButton ) {
    _editorWindow->showRMBMenu( _editorWindow->hasSelection() );
  }
  else {
    RegExpWidget::mousePressEvent( event );
  }
}



void ConcWidget::sizeAccepter( DragAccepter* accepter, int height, int totHeight )
{
  if (accepter->height() != height ) {
    accepter->resize( accepter->width(), height );
  }

  if (accepter->y() != (totHeight - height)/2) {
    accepter->move( accepter->x(), (totHeight - height)/2 );
  }
}

RegExp* ConcWidget::regExp() const
{
  TQPtrListIterator<RegExpWidget> it( _tqchildren );
  ++it; // start with the second element.

  if ( _tqchildren.count() == 3 ) {
    // Exactly one child (and two drag accepters)
    return (*it)->regExp();
  }
  else {
    ConcRegExp *regexp = new ConcRegExp( isSelected() );

    for ( ; *it; it+=2 ) {
      regexp->addRegExp( (*it)->regExp() );
    }
    return regexp;
  }
}

bool ConcWidget::updateSelection(bool parentSelected)
{
  bool isSel = _isSelected;
  bool changed =  MultiContainerWidget::updateSelection( parentSelected );

  _maxSelectedHeight = 0;

  TQPtrListIterator<RegExpWidget> it(_tqchildren);
  ++it; // Skip past the first DragAccepter
	for ( ; *it; it +=2  ) {
    if ( (*it)->isSelected() ) {
      _maxSelectedHeight = TQMAX( _maxSelectedHeight, (*it)->tqsizeHint().height() );
    }
  }

  changed = changed || isSel != _isSelected;
  if ( changed ) {
    tqrepaint();
  }

  return changed;
}

void ConcWidget::getSelectionIndexes( int* start, int* end )
{
  *start = -1;
  *end = -1;

  // Start with element at index 1, and skip every second element, as we
  // know they are dragAccepters.
	for ( unsigned int index = 1; index< _tqchildren.count(); index += 2 ) {
    RegExpWidget* child = _tqchildren.at(index);

    if ( child->isSelected() ) {
      // The child is selected at topmost level.
      if ( *start == -1 ) {
        *start = index;
      }
    }
    else if ( *start != -1 ) {
      // Found the end of the selection.
      *end = index -2;
      break;
    }
  }

  if ( *start != -1 && *end == -1 )
    *end = _tqchildren.count() -2;
}

void ConcWidget::applyRegExpToSelection( RegExpType type )
{
  int start, end;
  getSelectionIndexes( &start, &end );

  if ( start == -1 ) {
    // No item selected at top level

    TQPtrListIterator<RegExpWidget> it(_tqchildren);
    ++it; // Skip past the first DragAccepter
    for ( ; *it ; it += 2 ) {
      if ( (*it)->hasSelection() ) {
        (*it)->applyRegExpToSelection( type );
        break;
      }
    }
  }
  else {
    // Apply RegExp to selection.
    RegExpWidget* newElm = WidgetFactory::createWidget( _editorWindow, this, type );

    if ( newElm ) {
      ConcWidget* subSequence = new ConcWidget(_editorWindow, this, start, end);
      newElm->setConcChild( subSequence );


      subSequence->resize(0,0); // see note (1)
      subSequence->reparent( newElm, TQPoint(0,0), false);
      _tqchildren.insert( start, newElm );
      newElm->show();
    }
  }
}

bool ConcWidget::isSelected() const
{
  // A ConcWidget should be considered selected when all its elements has been selected
  // otherwise empty ConcWidgets may be left behind when for example selection is deleted.
  bool allSelected = true;
  TQPtrListIterator<RegExpWidget> it(_tqchildren);
  ++it; // Skip past first DragAccepter.
  for ( ; *it && allSelected; it += 2 ) {
    allSelected = allSelected && (*it)->isSelected();
  }

  return allSelected;
}

RegExp* ConcWidget::selection() const
{
  if ( isSelected() )
    return regExp();

  bool foundAny = false;
  bool foundMoreThanOne = false;
  RegExp* regexp = 0;

  TQPtrListIterator<RegExpWidget> it(_tqchildren);
  ++it; // Skip past the first DragAccepter
  for ( ; (*it) ; it += 2 ) {
    if ( (*it)->hasSelection() ) {
      if (!foundAny) {
        regexp = (*it)->selection();
        foundAny = true;
      }
      else if ( !foundMoreThanOne ) {
        ConcRegExp* reg = new ConcRegExp( isSelected() );
        reg->addRegExp( regexp );
        reg->addRegExp( (*it)->selection() );
        regexp = reg;
        foundMoreThanOne = true;
      }
      else {
        dynamic_cast<ConcRegExp*>(regexp)->addRegExp( (*it)->selection() );
      }
    }
  }

  Q_ASSERT( foundAny );
  return regexp;
}

void ConcWidget::addNewConcChild(DragAccepter *accepter, ConcWidget *other)
{
  for ( unsigned int i=0; i<_tqchildren.count(); i+= 2 ) {
    RegExpWidget *ch = _tqchildren.at( i );
    if ( ch == accepter ) {
      // Move all the element from the `child' ConcWidget to this one.
      // Do not copy the first one as this is a dragAccepter, and we place the widgets
      // after this drag accepter.
      // We must take them in pairs to avoid breaking the invariant for paintEvent,
      // namely that every second element is a dragAccepter
      for ( unsigned int j = other->_tqchildren.count()-1; j > 0 ; j-=2 ) {
        RegExpWidget* newChildA = other->_tqchildren.take(j);
        newChildA->reparent( this, TQPoint(0,0), false);
        _tqchildren.insert( i+1, newChildA );
        RegExpWidget* newChildB = other->_tqchildren.take(j-1);
        newChildB->reparent( this, TQPoint(0,0), false);
        _tqchildren.insert( i+1, newChildB );
        newChildA->show();
        newChildB->show();
      }
      delete other;
      return;
    }
  }
  qFatal("accepter not found");
}

bool ConcWidget::validateSelection() const
{
  bool cont = true;
  TQPtrListIterator<RegExpWidget> it(_tqchildren);
  ++it; // skip past the DragAccepter.
  for ( ; *it && cont; it += 2 ) {
    cont = (*it)->validateSelection();
  }
  return cont;
}