summaryrefslogtreecommitdiffstats
path: root/kregexpeditor/KMultiFormListBox/tdemultiformlistbox-multivisible.cpp
blob: 09c505216c396ce9bd89547016710417e4e7c3de (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
/*
 *  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"
  #include <tqmessagebox.h>
#else
  #include <tdemessagebox.h>
  #include "tdemultiformlistbox-multivisible.moc"
#endif

#include "tdemultiformlistbox-multivisible.h"
#include "indexWindow.h"
#include "ccp.h"

#include <tqbitmap.h>

const int indexButtonWidth = 16;
const int indexButtonHeight = 12;
const uchar indexButtonBits[] = {
	0x00, 0x00, 0x00, 0x00, 0x0e, 0x02, 0x04, 0x02, 0x04, 0x02, 0xc4, 0x8a,
	0x24, 0x53, 0x14, 0x22, 0x14, 0x22, 0x24, 0x53, 0xce, 0x8a, 0x00, 0x00
};


KMultiFormListBoxMultiVisible::KMultiFormListBoxMultiVisible(KMultiFormListBoxFactory *fact, TQWidget *parent, const char *name)
  : TQScrollView(parent, name)
{
  factory = fact;

  // Initialize the element list
  elms = new WidgetList();

  // Initialize the clipper.
  enableClipper(true);
  resizeContents(50,50); // This is required for proper functionality
}



//----------------------------------------------------------------------
// This function returns a list of the elements in the KMultiFormListBox widget.
//----------------------------------------------------------------------
KMultiFormListBoxEntryList KMultiFormListBoxMultiVisible::elements()
{
  KMultiFormListBoxEntryList res;
  for (TQWidget *child = elms->first(); child; child=elms->next()) {
    if (strcmp(child->name(),"seperator") != 0) {
      res.append((KMultiFormListBoxEntry *) child);
    }
  }
  return res;
}


//----------------------------------------------------------------------
// This function is called whenever the KMultiFormListBox widget is resized. It is
// necessary to ensure that the content of the clipper is resized.
//----------------------------------------------------------------------
void KMultiFormListBoxMultiVisible::resizeEvent(TQResizeEvent *e)
{
  // The call of the super class ensures that the outer border is updated.
  TQScrollView::resizeEvent(e);

  updateClipperContent();
}

void KMultiFormListBoxMultiVisible::updateClipperContent()
{
  // Extract the current size of the clipper
  int ClipperWidth = clipper()->size().width();
  int ClipperHeight = clipper()->size().height();

  // Initialize the calculation of the size of the new clipper.
  int totalHeight = 0;
  int maxWidth = ClipperWidth;
  int count = 0;


  // calculate the required size.
  for (TQWidget *child = elms->first(); child; child=elms->next()) {
    maxWidth = TQMAX(maxWidth, child->sizeHint().width());
    if (strcmp(child->name(), "seperator") != 0) {
      totalHeight += child->sizeHint().height();
      count++;
    }
    else {
      totalHeight += child->size().height();
    }
  }

  // Calculate the extra height for the elements.
  int extra = 0;
  if (totalHeight < ClipperHeight && count != 0) {
    extra = (ClipperHeight - totalHeight) / count;
    totalHeight = ClipperHeight;
  }

  // Now place the elements in the clipper.
  int yPos = 0;
  for (TQWidget *child2 = elms->first(); child2; child2=elms->next()) {
    int h;
    if ( strcmp(child2->name(),"seperator") != 0) {
      h = child2->sizeHint().height();
      h += extra;
    }
    else {
      h = child2->size().height();
    }

    moveChild(child2, 0,yPos);

    child2->resize(maxWidth,h);
    yPos += h;
  }

  // Finally call the resize procedure for the clipper to ensure that the
  // new sizes is shown properly.
  resizeContents(maxWidth, totalHeight);
}


void KMultiFormListBoxMultiVisible::addElement()
{
  addElement(0);
}

void KMultiFormListBoxMultiVisible::addElement(KMultiFormListBoxEntry *after)
{
  KMultiFormListBoxEntry *elm = factory->create(viewport());
  insertElmIntoWidget(elm, after);
}

void KMultiFormListBoxMultiVisible::append(KMultiFormListBoxEntry *elm)
{
  elm->reparent(viewport(), 0, TQPoint(0,0), false);
  insertElmIntoWidget(elm, 0);
}

void KMultiFormListBoxMultiVisible::delElement(TQWidget *elm)
{
  int index = elms->find(elm);
  TQWidget *next = elms->at(index+1);
  if (strcmp(next->name(),"seperator") != 0) {
    elms->removeRef(next);
    removeChild(next);
  }

  elms->removeRef(elm);
  removeChild(elm);

  updateClipperContent();
}

void KMultiFormListBoxMultiVisible::delAnElement()
{
  delElement(elms->at(0));
}

void KMultiFormListBoxMultiVisible::insertElmIntoWidget(KMultiFormListBoxEntry *elm, KMultiFormListBoxEntry *after)
{
  // Bind the index button if it exists.
  if (elm->indexButton()) {
    elm->indexButton()->setPixmap(TQBitmap(indexButtonWidth, indexButtonHeight,
																					indexButtonBits, true));
    connect(elm->indexButton(), TQT_SIGNAL(clicked()), elm, TQT_SLOT(acceptIndexButton()));
    connect(elm, TQT_SIGNAL(gotoIndex(KMultiFormListBoxEntry *)),
            this, TQT_SLOT(showIndexList(KMultiFormListBoxEntry *)));
  }

  // Find the location to insert the new element.
  int index = elms->count();
  if (after) {
    index = elms->findRef(after);
  }

  // Now show the new element.
  elms->insert(index, elm);
  elm->show();
  addChild(elm,0,0); // updateClipperContent will place the child correctly.

  TQWidget *sep = factory->separator(viewport());
  if (sep != 0) {
    sep->setName("seperator");
    sep->show();
    addChild(sep,0,0); // updateClipperContent will place the child correctly.
    elms->insert(index+1, sep);
  }

  updateClipperContent();

  showWidget(elm); // scroll to show the new widget.

  // install cut'n'paste functionallity
  new CCP(this,elm);
}


//----------------------------------------------------------------------
// This function shows the list of available Idx elements.
//----------------------------------------------------------------------
void KMultiFormListBoxMultiVisible::showIndexList(KMultiFormListBoxEntry *elm)
{
  indexWindow *menu = new indexWindow();

  // Insert the elements into the menu item.
  for (TQWidget *child = elms->first(); child; child=elms->next()) {
    if ( strcmp(child->name(), "seperator") != 0) {
      TQString txt = ((KMultiFormListBoxEntry *) child)->idxString();
      menu->insertItem(txt);
    }
  }

  // Calculate the location of the window
  TQPoint start;
  int width;
  elm->indexWindowPos(&start, &width);

  // Show the window.
  int index = menu->exec(start,width);

  if (index != -1) {
    for (TQWidget *child = elms->first(); child; child=elms->next()) {
      if ( strcmp(child->name(), "seperator") != 0) {

        if (index == 0) {
          showWidget((KMultiFormListBoxEntry *) child);
          break;
        }
        index--;
      }
    }
  }
  delete menu;
}

//----------------------------------------------------------------------
// Scroll to the loaction of the given KMultiFormListBoxEntry element.
//----------------------------------------------------------------------
void KMultiFormListBoxMultiVisible::showWidget(KMultiFormListBoxEntry *elm)
{
  setContentsPos(childX(elm), childY(elm));
}


void KMultiFormListBoxMultiVisible::cut(KMultiFormListBoxEntry *elm)
{
	if (countElements(elms) == 1) {
		KMessageBox::information(this, i18n("Due to a bug, it is not possible to remove the last element."), i18n("Internal Error") );
		return;
	}

  TQDataStream stream(clipboard, IO_WriteOnly);
  factory->toStream( TQT_TQOBJECT(elm), stream );
  delElement(elm);
}

void KMultiFormListBoxMultiVisible::copy(KMultiFormListBoxEntry *elm)
{
  TQDataStream stream(clipboard, IO_WriteOnly);
  factory->toStream(TQT_TQOBJECT(elm), stream);
}

void KMultiFormListBoxMultiVisible::paste(KMultiFormListBoxEntry *oldElm)
{
  if (clipboard.size() == 0) {
    KMessageBox::information(this, i18n("There is no element on the clipboard to paste in."));
    return;
  }

  KMultiFormListBoxEntry *newElm = factory->create(viewport());
  TQDataStream stream( clipboard, IO_ReadOnly );
  factory->fromStream(stream, TQT_TQOBJECT(newElm));
  insertElmIntoWidget(newElm,oldElm);
}


int KMultiFormListBoxMultiVisible::countElements(WidgetList *elms)
{
  int count = 0;

  for (TQWidget *child = elms->first(); child; child=elms->next()) {
    if (dynamic_cast<const KMultiFormListBoxEntry *>(child))
      count++;
  }

  return count;
}