22#include <koIconChooser.h>
27#include <tqapplication.h>
32KoPixmapWidget::KoPixmapWidget(
const TQPixmap &aPixmap, TQWidget *parent,
const char *name):
33TQFrame(parent,
name, WType_Popup)
35 kdDebug() <<
"Popup created: " <<
name <<
"\n";
36 setFrameStyle(TQFrame::WinPanel | TQFrame::Raised);
38 int w = mPixmap.width() + 2 * lineWidth();
39 int h = mPixmap.height() + 2 * lineWidth();
44KoPixmapWidget::~KoPixmapWidget()
49void KoPixmapWidget::paintEvent(TQPaintEvent *e)
51 TQFrame::paintEvent(e);
53 p.setClipRect(e->rect());
54 p.drawPixmap(lineWidth(), lineWidth(), mPixmap);
58void KoPixmapWidget::mouseReleaseEvent(TQMouseEvent *)
64KoIconChooser::KoIconChooser(TQSize aIconSize, TQWidget *parent,
const char *name,
bool sort):
65TQGridView(parent,
name)
67 TQGridView::setBackgroundColor(TQt::white);
70 setCellWidth(aIconSize.width() + 2 * mMargin);
71 setCellHeight(aIconSize.height() + 2 * mMargin);
79 mItemWidth = aIconSize.width();
80 mItemHeight = aIconSize.height();
81 mMouseButtonDown =
false;
86KoIconChooser::~KoIconChooser()
93void KoIconChooser::addItem(KoIconItem *item)
95 TQ_INT32 n = mItemCount;
96 KoIconItem *current = currentItem();
104 i = sortInsertionIndex(item);
111 mIconList.insert(i, item);
117 setCurrentItem(current);
122 updateCell(n / numCols(), n - (n / numCols()) * numCols());
126bool KoIconChooser::removeItem(KoIconItem *item)
128 int index = mIconList.find(item);
129 bool ok = mIconList.remove(item);
135 setCurrentItem(itemAt(kMin(index, mItemCount - 1)));
141void KoIconChooser::clear()
149KoIconItem *KoIconChooser::currentItem()
151 return itemAt(mCurRow, mCurCol);
156void KoIconChooser::setCurrentItem(KoIconItem *item)
158 int index = mIconList.find(item);
161 if(index != -1 && mNCols > 0)
163 int oldRow = mCurRow;
164 int oldCol = mCurCol;
166 mCurRow = index / mNCols;
167 mCurCol = index % mNCols;
170 repaintCell(oldRow, oldCol);
171 repaintCell(mCurRow, mCurCol);
173 ensureCellVisible(mCurRow, mCurCol);
178void KoIconChooser::mousePressEvent(TQMouseEvent *e)
180 TQGridView::mousePressEvent(e);
183void KoIconChooser::mouseMoveEvent(TQMouseEvent *e)
185 if(mMouseButtonDown && mDragEnabled )
190 delete mPixmapWidget;
194 if( ( mDragStartPos - e->pos() ).manhattanLength() > TQApplication::startDragDistance() )
200KoIconChooser::startDrag()
202 mMouseButtonDown =
false;
205void KoIconChooser::mouseReleaseEvent(TQMouseEvent * e)
207 mMouseButtonDown =
true;
208 if(e->button() == TQt::LeftButton)
210 TQPoint p = e->pos();
212 int x = contentsX() + p.x();
213 int y = contentsY() + p.y();
214 TQSize gridExtent = gridSize();
216 if (x < gridExtent.width() && y < gridExtent.height())
219 int col = columnAt(x);
221 KoIconItem *item = itemAt(row, col);
224 const TQPixmap &pix = item->pixmap();
225 if(pix.width() > mItemWidth || pix.height() > mItemHeight)
226 showFullPixmap(pix, p);
228 setCurrentItem(item);
229 emit selected( item );
236void KoIconChooser::keyPressEvent(TQKeyEvent *e)
238 TQGridView::keyPressEvent(e);
243void KoIconChooser::resizeEvent(TQResizeEvent *e)
245 TQGridView::resizeEvent(e);
247 KoIconItem *item = currentItem();
248 int oldNCols = mNCols;
250 if (cellWidth() != 0)
252 mNCols = e -> size().width() / cellWidth();
255 if(mNCols != oldNCols)
259 setCurrentItem(item);
267void KoIconChooser::paintCell(TQPainter *p,
int row,
int col)
269 KoIconItem *item = itemAt(row, col);
273 const TQPixmap &pix = item->pixmap();
277 int pw = pix.width();
278 int ph = pix.height();
279 int cw = cellWidth();
280 int ch = cellHeight();
288 if((!item->hasValidThumb()) || (pw <= mItemWidth && ph <= mItemHeight))
289 p->drawPixmap(x, y, pix, 0, 0, mItemWidth, mItemHeight);
292 const TQPixmap &thumbpix = item->thumbPixmap();
295 pw = thumbpix.width();
296 ph = thumbpix.height();
305 p->drawPixmap(x, y, thumbpix, 0, 0, mItemWidth, mItemHeight);
309 if(row == mCurRow && col == mCurCol)
312 p->drawRect(0, 0, cw, ch);
318 p->drawLine(cw-1, 0, cw-1, ch-1);
319 p->drawLine(0, ch-1, cw-1, ch-1);
325 p->fillRect(0, 0, cellWidth(), cellHeight(), TQBrush(TQt::white));
332KoIconItem *KoIconChooser::itemAt(
int row,
int col)
334 return itemAt(cellIndex(row, col));
339KoIconItem *KoIconChooser::itemAt(
int index)
343 return mIconList.count() > uint(index) ? mIconList.at(index) : 0;
349int KoIconChooser::cellIndex(
int row,
int col)
351 if(row < 0 || col < 0)
354 return((row * mNCols) + col);
359void KoIconChooser::calculateCells()
363 bool update = isUpdatesEnabled();
364 int rows = mItemCount / mNCols;
365 setUpdatesEnabled(
false);
366 if((rows * mNCols) < mItemCount)
369 setUpdatesEnabled(update);
374void KoIconChooser::showFullPixmap(
const TQPixmap &pix,
const TQPoint &)
377 mPixmapWidget =
new KoPixmapWidget(pix,
this);
380 TQPoint p = TQCursor::pos();
381 int w = mPixmapWidget->width();
382 int h = mPixmapWidget->height();
383 mPixmapWidget->move(p.x() - w / 2, p.y() - h / 2);
384 mPixmapWidget->show();
387int KoIconChooser::sortInsertionIndex(
const KoIconItem *item)
391 if (!mIconList.isEmpty())
395 int last = mIconList.count() - 1;
397 while (first != last)
399 int middle = (first + last) / 2;
401 if (item -> compare(mIconList.at(middle)) < 0)
421 if (item -> compare(mIconList.at(first)) < 0)
434KoPatternChooser::KoPatternChooser(
const TQPtrList<KoIconItem> &list, TQWidget *parent,
const char *name )
435 : TQWidget( parent,
name )
440 chooser =
new KoIconChooser( TQSize(30,30),
this,
"pattern chooser" );
442 TQObject::connect( chooser, TQ_SIGNAL(selected( KoIconItem * ) ),
443 this, TQ_SIGNAL( selected( KoIconItem * )));
445 TQPtrListIterator<KoIconItem> itr( list );
446 for( itr.toFirst(); itr.current(); ++itr )
447 chooser->addItem( itr.current() );
449 TQVBoxLayout *mainLayout =
new TQVBoxLayout(
this, 1, -1,
"main layout" );
450 mainLayout->addWidget( chooser, 10 );
454KoPatternChooser::~KoPatternChooser()
461void KoPatternChooser::setCurrentPattern( KoIconItem *pattern )
463 chooser->setCurrentItem( pattern );
466void KoPatternChooser::addPattern( KoIconItem *pattern )
468 chooser->addItem( pattern );
472KoIconItem *KoPatternChooser::currentPattern()
474 return chooser->currentItem();
477void KoPatternChooser::removePattern( KoIconItem *pattern )
479 chooser->removeItem( pattern );
482#include "koIconChooser.moc"
const char * name(StdAction id)