00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <kdebug.h>
00022 #include <koIconChooser.h>
00023 #include <kglobal.h>
00024
00025 #include <qpainter.h>
00026 #include <qcursor.h>
00027 #include <qapplication.h>
00028 #include <qhbox.h>
00029 #include <qlayout.h>
00030 #include <kdebug.h>
00031
00032 KoPixmapWidget::KoPixmapWidget(const QPixmap &aPixmap, QWidget *parent, const char *name):
00033 QFrame(parent, name, WStyle_Customize | WStyle_NoBorder)
00034 {
00035 setFrameStyle(QFrame::WinPanel | QFrame::Raised);
00036 mPixmap = aPixmap;
00037 int w = mPixmap.width() + 2 * lineWidth();
00038 int h = mPixmap.height() + 2 * lineWidth();
00039 resize(w, h);
00040
00041
00042 QPoint p = QCursor::pos();
00043 move(p.x() - w / 2, p.y() - h / 2);
00044 show();
00045 }
00046
00047 KoPixmapWidget::~KoPixmapWidget()
00048 {
00049 }
00050
00051
00052 void KoPixmapWidget::paintEvent(QPaintEvent *e)
00053 {
00054 QFrame::paintEvent(e);
00055 QPainter p(this);
00056 p.setClipRect(e->rect());
00057 p.drawPixmap(lineWidth(), lineWidth(), mPixmap);
00058 }
00059
00060
00061
00062 KoIconChooser::KoIconChooser(QSize aIconSize, QWidget *parent, const char *name):
00063 QGridView(parent, name)
00064 {
00065 QGridView::setBackgroundColor(Qt::white);
00066
00067 mMargin = 2;
00068 setCellWidth(aIconSize.width() + 2 * mMargin);
00069 setCellHeight(aIconSize.height() + 2 * mMargin);
00070
00071 mIconList.clear();
00072 mPixmapWidget = 0L;
00073 nCols = 0;
00074 mCurRow = 0;
00075 mCurCol = 0;
00076 mItemCount = 0;
00077 mItemWidth = aIconSize.width();
00078 mItemHeight = aIconSize.height();
00079 mMouseButtonDown = false;
00080 mDragEnabled = false;
00081 }
00082
00083 KoIconChooser::~KoIconChooser()
00084 {
00085 mIconList.clear();
00086 if(mPixmapWidget)
00087 delete mPixmapWidget;
00088 }
00089
00090 void KoIconChooser::addItem(KoIconItem *item)
00091 {
00092 Q_INT32 n = mItemCount;
00093
00094 Q_ASSERT(item);
00095 mIconList.insert(mItemCount++, item);
00096 calculateCells();
00097 updateCell(n / numCols(), n - (n / numCols()) * numCols());
00098 }
00099
00100 bool KoIconChooser::removeItem(KoIconItem *item)
00101 {
00102 int index = mIconList.find(item);
00103 bool ok = mIconList.remove(item);
00104 if( ok )
00105 {
00106 mItemCount--;
00107
00108
00109 setCurrentItem(itemAt(kMin(index, mItemCount - 1)));
00110 calculateCells();
00111 }
00112 return ok;
00113 }
00114
00115 void KoIconChooser::clear()
00116 {
00117 mItemCount = 0;
00118 mIconList.clear();
00119 calculateCells();
00120 }
00121
00122
00123 KoIconItem *KoIconChooser::currentItem()
00124 {
00125 return itemAt(mCurRow, mCurCol);
00126 }
00127
00128
00129
00130 void KoIconChooser::setCurrentItem(KoIconItem *item)
00131 {
00132 int index = mIconList.find(item);
00133
00134
00135 if(index != -1 && nCols > 0)
00136 {
00137 int oldRow = mCurRow;
00138 int oldCol = mCurCol;
00139
00140 mCurRow = index / nCols;
00141 mCurCol = index % nCols;
00142
00143
00144 updateCell(oldRow, oldCol);
00145 updateCell(mCurRow, mCurCol);
00146 }
00147 }
00148
00149
00150 void KoIconChooser::mousePressEvent(QMouseEvent *e)
00151 {
00152 QGridView::mousePressEvent(e);
00153 mMouseButtonDown = true;
00154 if(e->button() == LeftButton)
00155 {
00156 QPoint p = e->pos();
00157 mDragStartPos = p;
00158 int row = rowAt(contentsY() + p.y());
00159 int col = columnAt(contentsX() + p.x());
00160
00161 KoIconItem *item = itemAt(row, col);
00162 if(item)
00163 {
00164 const QPixmap &pix = item->pixmap();
00165 if(pix.width() > mItemWidth || pix.height() > mItemHeight)
00166 showFullPixmap(pix, p);
00167
00168 int oldRow = mCurRow;
00169 int oldCol = mCurCol;
00170
00171 mCurRow = row;
00172 mCurCol = col;
00173
00174 updateCell(oldRow, oldCol);
00175 updateCell(mCurRow, mCurCol);
00176
00177 emit selected( item );
00178 }
00179 }
00180 }
00181
00182 void KoIconChooser::mouseMoveEvent(QMouseEvent *e)
00183 {
00184 if(mMouseButtonDown && mDragEnabled )
00185 {
00186 if(mPixmapWidget)
00187 {
00188 delete mPixmapWidget;
00189 mPixmapWidget = 0L;
00190 }
00191 if( ( mDragStartPos - e->pos() ).manhattanLength() > QApplication::startDragDistance() )
00192 startDrag();
00193 }
00194 }
00195
00196 void
00197 KoIconChooser::startDrag()
00198 {
00199 mMouseButtonDown = false;
00200 }
00201
00202
00203 void KoIconChooser::mouseReleaseEvent(QMouseEvent * )
00204 {
00205 mMouseButtonDown = false;
00206 if(mPixmapWidget)
00207 {
00208 delete mPixmapWidget;
00209 mPixmapWidget = 0L;
00210 }
00211 }
00212
00213
00214 void KoIconChooser::keyPressEvent(QKeyEvent *e)
00215 {
00216 QGridView::keyPressEvent(e);
00217 }
00218
00219
00220
00221 void KoIconChooser::resizeEvent(QResizeEvent *e)
00222 {
00223 QGridView::resizeEvent(e);
00224
00225 KoIconItem *item = currentItem();
00226 int oldNCols = nCols;
00227 nCols = numCols();
00228
00229 if(nCols != oldNCols)
00230 {
00231 setNumCols(nCols);
00232 calculateCells();
00233 setCurrentItem(item);
00234 }
00235 }
00236
00237
00238
00239
00240 void KoIconChooser::paintCell(QPainter *p, int row, int col)
00241 {
00242 KoIconItem *item = itemAt(row, col);
00243
00244 if(item)
00245 {
00246 const QPixmap &pix = item->pixmap();
00247
00248 int x = mMargin;
00249 int y = mMargin;
00250 int pw = pix.width();
00251 int ph = pix.height();
00252 int cw = cellWidth();
00253 int ch = cellHeight();
00254
00255
00256 if(pw < mItemWidth)
00257 x = (cw - pw) / 2;
00258 if(ph < mItemHeight)
00259 y = (cw - ph) / 2;
00260
00261 if((!item->hasValidThumb()) || (pw <= mItemWidth && ph <= mItemHeight))
00262 p->drawPixmap(x, y, pix, 0, 0, mItemWidth, mItemHeight);
00263 else
00264 {
00265 const QPixmap &thumbpix = item->thumbPixmap();
00266 x = mMargin;
00267 y = mMargin;
00268 pw = thumbpix.width();
00269 ph = thumbpix.height();
00270 cw = cellWidth();
00271 ch = cellHeight();
00272
00273
00274 if(pw < mItemWidth)
00275 x = (cw - pw) / 2;
00276 if(ph < mItemHeight)
00277 y = (cw - ph) / 2;
00278 p->drawPixmap(x, y, thumbpix, 0, 0, mItemWidth, mItemHeight);
00279 }
00280
00281
00282 if(row == mCurRow && col == mCurCol)
00283 {
00284 p->setPen(blue);
00285 p->drawRect(0, 0, cw, ch);
00286 }
00287 else
00288 {
00289 p->setPen(gray);
00290
00291 p->drawLine(cw-1, 0, cw-1, ch-1);
00292 p->drawLine(0, ch-1, cw-1, ch-1);
00293 }
00294 }
00295 else
00296 {
00297
00298 p->fillRect(0, 0, cellWidth(), cellHeight(), QBrush(Qt::white));
00299 }
00300 }
00301
00302
00303
00304
00305 KoIconItem *KoIconChooser::itemAt(int row, int col)
00306 {
00307 return itemAt(cellIndex(row, col));
00308 }
00309
00310
00311
00312 KoIconItem *KoIconChooser::itemAt(int index)
00313 {
00314 return mIconList.count() > index ? mIconList.at(index) : 0;
00315 }
00316
00317
00318
00319
00320 int KoIconChooser::cellIndex(int row, int col)
00321 {
00322 if(row < 0 || col < 0)
00323 return -1;
00324 else
00325 return((row * nCols) + col);
00326 }
00327
00328
00329
00330 void KoIconChooser::calculateCells()
00331 {
00332 if(nCols == 0)
00333 return;
00334 bool update = isUpdatesEnabled();
00335 int rows = mItemCount / nCols;
00336 setUpdatesEnabled(false);
00337 if((rows * nCols) < mItemCount)
00338 rows++;
00339 setNumRows(rows);
00340 setUpdatesEnabled(update);
00341 repaint();
00342 }
00343
00344
00345 void KoIconChooser::showFullPixmap(const QPixmap &pix, const QPoint &)
00346 {
00347 mPixmapWidget = new KoPixmapWidget(pix, 0L);
00348 }
00349
00350 KoPatternChooser::KoPatternChooser( const QPtrList<KoIconItem> &list, QWidget *parent, const char *name )
00351 : QWidget( parent, name )
00352 {
00353
00354
00355
00356 chooser = new KoIconChooser( QSize(30,30), this, "pattern chooser" );
00357
00358 QObject::connect( chooser, SIGNAL(selected( KoIconItem * ) ),
00359 this, SIGNAL( selected( KoIconItem * )));
00360
00361 QPtrListIterator<KoIconItem> itr( list );
00362 for( itr.toFirst(); itr.current(); ++itr )
00363 chooser->addItem( itr.current() );
00364
00365 QVBoxLayout *mainLayout = new QVBoxLayout( this, 1, -1, "main layout" );
00366 mainLayout->addWidget( chooser, 10 );
00367 }
00368
00369
00370 KoPatternChooser::~KoPatternChooser()
00371 {
00372 delete chooser;
00373
00374 }
00375
00376
00377 void KoPatternChooser::setCurrentPattern( KoIconItem *pattern )
00378 {
00379 chooser->setCurrentItem( pattern );
00380 }
00381
00382 void KoPatternChooser::addPattern( KoIconItem *pattern )
00383 {
00384 chooser->addItem( pattern );
00385 }
00386
00387
00388 KoIconItem *KoPatternChooser::currentPattern()
00389 {
00390 return chooser->currentItem();
00391 }
00392
00393 #include "koIconChooser.moc"