summaryrefslogtreecommitdiffstats
path: root/lib/kopainter/koIconChooser.cc
blob: 4604bbbd38550496910850563e568514ba677097 (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
473
474
475
476
477
478
479
480
481
482
/* This file is part of the KDE project
  Copyright (c) 1999 Carsten Pfeiffer (pfeiffer@kde.org)
  Copyright (c) 2002 Igor Jansen (rm@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 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.
*/

#include <kdebug.h>
#include <koIconChooser.h>
#include <kglobal.h>

#include <tqpainter.h>
#include <tqcursor.h>
#include <tqapplication.h>
#include <tqhbox.h>
#include <tqlayout.h>
#include <kdebug.h>

KoPixmapWidget::KoPixmapWidget(const TQPixmap &aPixmap, TQWidget *parent, const char *name):
TQFrame(parent, name, WType_Popup)
{
  kdDebug() << "Popup created: " << name << "\n";
  setFrameStyle(TQFrame::WinPanel | TQFrame::Raised);
  mPixmap = aPixmap;
  int w = mPixmap.width() + 2 * lineWidth();
  int h = mPixmap.height() + 2 * lineWidth();
  resize(w, h);

}

KoPixmapWidget::~KoPixmapWidget()
{
}

// paint the centered pixmap; don't overpaint the frame
void KoPixmapWidget::paintEvent(TQPaintEvent *e)
{
  TQFrame::paintEvent(e);
  TQPainter p(this);
  p.setClipRect(e->rect());
  p.drawPixmap(lineWidth(), lineWidth(), mPixmap);
}


void KoPixmapWidget::mouseReleaseEvent(TQMouseEvent *)
{
	hide();
}


KoIconChooser::KoIconChooser(TQSize aIconSize, TQWidget *parent, const char *name, bool sort):
TQGridView(parent, name)
{
  TQGridView::setBackgroundColor(TQt::white);

  mMargin = 2;
  setCellWidth(aIconSize.width() + 2 * mMargin);
  setCellHeight(aIconSize.height() + 2 * mMargin);

  mIconList.clear();
  mPixmapWidget = 0L;
  mNCols = 0;
  mCurRow = 0;
  mCurCol = 0;
  mItemCount = 0;
  mItemWidth = aIconSize.width();
  mItemHeight = aIconSize.height();
  mMouseButtonDown = false;
  mDragEnabled = false;
  mSort = sort;
}

KoIconChooser::~KoIconChooser()
{
  mIconList.clear();
  if(mPixmapWidget)
    delete mPixmapWidget;
}

void KoIconChooser::addItem(KoIconItem *item)
{
  TQ_INT32 n = mItemCount;
  KoIconItem *current = currentItem();

  Q_ASSERT(item);

  int i;

  if (mSort)
  {
    i = sortInsertionIndex(item);
  }
  else
  {
    i = mItemCount;
  }

  mIconList.insert(i, item);
  mItemCount++;
  calculateCells();

  if (mSort)
  {
    setCurrentItem(current);
    updateContents();
  }
  else
  {
    updateCell(n / numCols(), n - (n / numCols()) * numCols());
  }
}

bool KoIconChooser::removeItem(KoIconItem *item)
{
  int index = mIconList.find(item);
  bool ok = mIconList.remove(item);
  if( ok )
  {
    mItemCount--;
    // select same cell as deleted cell, or new last cell if we deleted
    // last cell.
    setCurrentItem(itemAt(kMin(index, mItemCount - 1)));
    calculateCells();
  }
  return ok;
}

void KoIconChooser::clear()
{
  mItemCount = 0;
  mIconList.clear();
  calculateCells();
}

// return 0L if there is no current item
KoIconItem *KoIconChooser::currentItem()
{
  return itemAt(mCurRow, mCurCol);
}

// sets the current item to item
// does NOT emit selected()  (should it?)
void KoIconChooser::setCurrentItem(KoIconItem *item)
{
  int index = mIconList.find(item);

  // item is available
  if(index != -1 && mNCols > 0)
  {
    int oldRow = mCurRow;
    int oldCol = mCurCol;

    mCurRow = index / mNCols;
    mCurCol = index % mNCols;

    // tqrepaint the old and the new item
    repaintCell(oldRow, oldCol);
    repaintCell(mCurRow, mCurCol);

    ensureCellVisible(mCurRow, mCurCol);
  }
}

// eventually select the item, clicked on
void KoIconChooser::mousePressEvent(TQMouseEvent *e)
{
  TQGridView::mousePressEvent(e);
}

void KoIconChooser::mouseMoveEvent(TQMouseEvent *e)
{
  if(mMouseButtonDown && mDragEnabled )
  {
#if 0
    if(mPixmapWidget)
    {
      delete mPixmapWidget;
      mPixmapWidget = 0L;
    }
#endif
    if( ( mDragStartPos - e->pos() ).manhattanLength() > TQApplication::startDragDistance() )
      startDrag();
  }
}

void
KoIconChooser::startDrag()
{
  mMouseButtonDown = false;
}

void KoIconChooser::mouseReleaseEvent(TQMouseEvent * e)
{
  mMouseButtonDown = true;
  if(e->button() == Qt::LeftButton)
  {
    TQPoint p = e->pos();
    mDragStartPos = p;
    int x = contentsX() + p.x();
    int y = contentsY() + p.y();
    TQSize gridExtent = gridSize();

    if (x < gridExtent.width() && y < gridExtent.height())
    {
      int row = rowAt(y);
      int col = columnAt(x);

      KoIconItem *item = itemAt(row, col);
      if(item)
      {
        const TQPixmap &pix = item->pixmap();
        if(pix.width() > mItemWidth || pix.height() > mItemHeight)
          showFullPixmap(pix, p);

        setCurrentItem(item);
        emit selected( item );
      }
    }
  }
}

// FIXME: implement keyboard navigation
void KoIconChooser::keyPressEvent(TQKeyEvent *e)
{
  TQGridView::keyPressEvent(e);
}

// recalculate the number of items that fit into one row
// set the current item again after calculating the new grid
void KoIconChooser::resizeEvent(TQResizeEvent *e)
{
  TQGridView::resizeEvent(e);

  KoIconItem *item = currentItem();
  int oldNCols = mNCols;

  if (cellWidth() != 0)
  {
    mNCols = e -> size().width() / cellWidth();
  }

  if(mNCols != oldNCols)
  {
    setNumCols(mNCols);
    calculateCells();
    setCurrentItem(item);
    updateContents();
  }
}

// paint one cell
// mark the current item and center items smaller than the cellSize
// TODO: scale down big pixmaps and paint the size as text into the pixmap
void KoIconChooser::paintCell(TQPainter *p, int row, int col)
{
  KoIconItem *item = itemAt(row, col);

  if(item)
  {
    const TQPixmap &pix = item->pixmap();

    int x = mMargin;
    int y = mMargin;
    int pw = pix.width();
    int ph = pix.height();
    int cw = cellWidth();
    int ch = cellHeight();

    // center small pixmaps
    if(pw < mItemWidth)
      x = (cw - pw) / 2;
    if(ph < mItemHeight)
      y = (cw - ph) / 2;

    if((!item->hasValidThumb()) || (pw <= mItemWidth && ph <= mItemHeight))
      p->drawPixmap(x, y, pix, 0, 0, mItemWidth, mItemHeight);
    else
    {
      const TQPixmap &thumbpix = item->thumbPixmap();
      x = mMargin;
      y = mMargin;
      pw = thumbpix.width();
      ph = thumbpix.height();
      cw = cellWidth();
      ch = cellHeight();

      // center small pixmaps
      if(pw < mItemWidth)
        x = (cw - pw) / 2;
      if(ph < mItemHeight)
        y = (cw - ph) / 2;
      p->drawPixmap(x, y, thumbpix, 0, 0, mItemWidth, mItemHeight);
    }

    // highlight current item
    if(row == mCurRow && col == mCurCol)
    {
      p->setPen(blue);
      p->drawRect(0, 0, cw, ch);
    }
    else
    {
      p->setPen(gray);
      //p->drawRect(0, 0, cw, ch);
      p->drawLine(cw-1, 0, cw-1, ch-1);
      p->drawLine(0, ch-1, cw-1, ch-1);
    }
  }
  else
  {
    // empty cell
    p->fillRect(0, 0, cellWidth(), cellHeight(), TQBrush(TQt::white));
  }
}

// return the pointer of the item at (row,col) - beware, resizing disturbs
// rows and cols!
// return 0L if item is not found
KoIconItem *KoIconChooser::itemAt(int row, int col) 
{
  return itemAt(cellIndex(row, col));
}

// return the pointer of the item at position index
// return 0L if item is not found
KoIconItem *KoIconChooser::itemAt(int index)
{
  if(index < 0)
    return 0L;
  return mIconList.count() > uint(index) ? mIconList.at(index) : 0;
}

// return the index of a cell, given row and column position
// maps directly to the position in the itemlist
// return -1 on failure
int KoIconChooser::cellIndex(int row, int col)
{
  if(row < 0 || col < 0)
    return -1;
  else
    return((row * mNCols) + col);
}

// calculate the grid and set the number of rows and columns
// reorder all items approrpriately
void KoIconChooser::calculateCells()
{
  if(mNCols == 0)
    return;
  bool update = isUpdatesEnabled();
  int rows = mItemCount / mNCols;
  setUpdatesEnabled(false);
  if((rows * mNCols) < mItemCount)
    rows++;
  setNumRows(rows);
  setUpdatesEnabled(update);
  updateContents();
}

// show the full pixmap of a large item in an extra widget
void KoIconChooser::showFullPixmap(const TQPixmap &pix, const TQPoint &/*p*/)
{
  //delete mPixmapWidget;
  mPixmapWidget = new KoPixmapWidget(pix, this);

  // center widget under mouse cursor
  TQPoint p = TQCursor::pos();
  int w = mPixmapWidget->width();
  int h = mPixmapWidget->height();
  mPixmapWidget->move(p.x() - w / 2, p.y() - h / 2);
  mPixmapWidget->show();
}

int KoIconChooser::sortInsertionIndex(const KoIconItem *item)
{
  int index = 0;

  if (!mIconList.isEmpty())
  {
    // Binary insertion
    int first = 0;
    int last = mIconList.count() - 1;
    
    while (first != last)
    {
      int middle = (first + last) / 2;
    
      if (item -> compare(mIconList.at(middle)) < 0)
      {
        last = middle - 1;

        if (last < first)
        {
          last = first;
        }
      }
      else
      {
        first = middle + 1;

        if (first > last)
        {
          first = last;
        }
      }
    }

    if (item -> compare(mIconList.at(first)) < 0)
    {
      index = first;
    }
    else
    {
      index = first + 1;
    }
  }

  return index;
}

KoPatternChooser::KoPatternChooser( const TQPtrList<KoIconItem> &list, TQWidget *parent, const char *name )
 : TQWidget( parent, name )
{
    // only serves as beautifier for the iconchooser
    //frame = new TQHBox( this );
    //frame->setFrameStyle( TQFrame::Panel | TQFrame::Sunken );
    chooser = new KoIconChooser( TQSize(30,30), this, "pattern chooser" );

	TQObject::connect( chooser, TQT_SIGNAL(selected( KoIconItem * ) ),
					            this, TQT_SIGNAL( selected( KoIconItem * )));

	TQPtrListIterator<KoIconItem> itr( list );
	for( itr.toFirst(); itr.current(); ++itr )
		chooser->addItem( itr.current() );

	TQVBoxLayout *mainLayout = new TQVBoxLayout( this, 1, -1, "main tqlayout" );
	mainLayout->addWidget( chooser, 10 );
}


KoPatternChooser::~KoPatternChooser()
{
  delete chooser;
  //delete frame;
}

// set the active pattern in the chooser - does NOT emit selected() (should it?)
void KoPatternChooser::setCurrentPattern( KoIconItem *pattern )
{
    chooser->setCurrentItem( pattern );
}

void KoPatternChooser::addPattern( KoIconItem *pattern )
{
    chooser->addItem( pattern );
}

// return the active pattern
KoIconItem *KoPatternChooser::currentPattern()
{
    return chooser->currentItem();
}

void KoPatternChooser::removePattern( KoIconItem *pattern )
{
	chooser->removeItem( pattern );
}

#include "koIconChooser.moc"