Source Code (Use browser search to find items of interest.)

Class Index

kimageshop'IconChooser (./koffice/kimageshop/ui/iconchooser.h:38)

class IconChooser: public QTableView
{
  Q_OBJECT

public:
  IconChooser( QWidget *parent, QSize iconSize, const char *name );
  virtual ~IconChooser();


  void		addItem( IconItem * );
  bool 		removeItem( IconItem * );
  IconItem *	currentItem();
  void 		setCurrentItem( IconItem * );
  void 		clear();
  void 		setAutoDelete( bool b )       { iconList.setAutoDelete( b );  }
  bool 		autoDelete() 		const { return iconList.autoDelete(); }
  void 		setBackgroundColor( const QColor& color ) const;
  const QColor& backgroundColor()	const { return bgColor;		      }


protected:
  int 		count() 		const { return itemCount; }
  IconItem *	itemAt( int row, int col );
  IconItem *	itemAt( int index );
  int 		cellIndex( int row, int col );
  void 		calculateCells();
  void 		showFullPixmap( const QPixmap&, const QPoint& );
  virtual void 	keyPressEvent( QKeyEvent * );
  virtual void	mousePressEvent( QMouseEvent * );
  virtual void 	mouseReleaseEvent( QMouseEvent * );
  virtual void	paintCell( QPainter *, int, int );
  virtual void 	resizeEvent( QResizeEvent * );

  int 		nCols;
  int 		curRow;
  int 		curCol;
  int 		itemWidth;
  int 		itemHeight;
  int 		margin;
  QColor 	bgColor;
  QList<IconItem> iconList;
  PixmapWidget 	*pw;

private:
  int 		itemCount;


signals:
  void 		selected( IconItem * );

};



kimageshop'IconChooser::IconChooser() (./koffice/kimageshop/ui/iconchooser.cc:32)

IconChooser::IconChooser( QWidget *parent, QSize iconSize, const char *name )
  : QTableView( parent, name )
{
  setTableFlags( Tbl_autoVScrollBar | Tbl_clipCellPainting | Tbl_cutCellsH |
		 Tbl_snapToGrid );
  QTableView::setBackgroundColor(white);

  margin = 2; // a cell is 2 * two pixels larger than an item
  setCellWidth( iconSize.width() + 2*margin );
  setCellHeight( iconSize.height() + 2*margin );
  updateTableSize();

  iconList.clear();
  pw           = 0L;
  nCols        = 0;
  curRow       = 0;
  curCol       = 0;
  itemCount    = 0;
  itemWidth    = iconSize.width();
  itemHeight   = iconSize.height();
  bgColor      = colorGroup().background();
}



kimageshop'IconChooser::~IconChooser() (./koffice/kimageshop/ui/iconchooser.cc:56)

IconChooser::~IconChooser()
{
  iconList.clear();
  if ( pw )
    delete pw;
}



kimageshop'IconChooser::addItem() (./koffice/kimageshop/ui/iconchooser.cc:64)

void IconChooser::addItem( IconItem *item )
{
  ASSERT( item != 0L );
  iconList.insert( itemCount++, item );
  calculateCells();
}



kimageshop'IconChooser::removeItem() (./koffice/kimageshop/ui/iconchooser.cc:72)

bool IconChooser::removeItem( IconItem *item )
{
  bool ok = iconList.remove( item );
  if ( ok ) {
    itemCount--;
    calculateCells();
  }

  return ok;
}



kimageshop'IconChooser::clear() (./koffice/kimageshop/ui/iconchooser.cc:84)

void IconChooser::clear()
{
  itemCount = 0;
  iconList.clear();
  calculateCells();
}


// return the pointer of the item at (row,col) - beware, resizing disturbs
// rows and cols!
// return 0L if item is not found

kimageshop'IconChooser::itemAt() (./koffice/kimageshop/ui/iconchooser.cc:95)

IconItem* IconChooser::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

kimageshop'IconChooser::itemAt() (./koffice/kimageshop/ui/iconchooser.cc:103)

IconItem* IconChooser::itemAt( int index )
{
  if ( index == -1 || index >= itemCount )
    return 0L;
  else
    return iconList.at( index );
}


// return 0L if there is no current item

kimageshop'IconChooser::currentItem() (./koffice/kimageshop/ui/iconchooser.cc:113)

IconItem* IconChooser::currentItem()
{
  return itemAt( curRow, curCol );
}


// sets the current item to item
// does NOT emit selected()  (should it?)

kimageshop'IconChooser::setCurrentItem() (./koffice/kimageshop/ui/iconchooser.cc:121)

void IconChooser::setCurrentItem( IconItem *item )
{
  int index = iconList.find( item );

  if ( index != -1 && nCols > 0 ) { // item is available
    int oldRow = curRow;
    int oldCol = curCol;

    curRow = index / nCols;
    curCol = index - (curRow * nCols);

    // repaint the old and the new item
    updateCell( oldRow, oldCol, true );
    updateCell( curRow, curCol, true );
  }
}


// calculate the grid and set the number of rows and columns
// reorder all items approrpriately

kimageshop'IconChooser::calculateCells() (./koffice/kimageshop/ui/iconchooser.cc:141)

void IconChooser::calculateCells()
{
  if ( nCols == 0 )
    return;

  bool update = autoUpdate();
  setAutoUpdate( false );

  int rows = itemCount / nCols;
  if ( (rows * nCols) < itemCount )
    rows++;

  setNumRows( rows );

  setAutoUpdate( update );
  repaint();
}



// recalculate the number of items that fit into one row
// set the current item again after calculating the new grid

kimageshop'IconChooser::resizeEvent() (./koffice/kimageshop/ui/iconchooser.cc:163)

void IconChooser::resizeEvent ( QResizeEvent *e )
{
  QTableView::resizeEvent( e );
  ASSERT( cellWidth() > 0 );

  IconItem *item = currentItem();
  int oldNCols = nCols;
  nCols = viewWidth() / cellWidth();

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


// 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

kimageshop'IconChooser::paintCell() (./koffice/kimageshop/ui/iconchooser.cc:183)

void IconChooser::paintCell( QPainter *p, int row, int col )
{
  IconItem *item = itemAt( row, col );

  if ( item ) {
    const QPixmap& pix = item->pixmap();
    int x  = margin; 		int y  = margin;
    int pw = pix.width(); 	int ph = pix.height();
    int cw = cellWidth(); 	int ch = cellHeight();

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

    p->drawPixmap( x, y, pix, 0, 0, itemWidth, itemHeight );

    if ( row == curRow && col == curCol )  // highlight current item
      {
	p->setPen(blue);
	p->drawRect( 0, 0, cw, ch );
      }
    else
      {
	p->setPen(gray);
	p->drawRect( 0, 0, cw+1, ch+1 );
      }
  }

  else { // empty cell
    p->fillRect( 0, 0, cellWidth(), cellHeight(), QBrush( white ) );
  }
}


// return the index of a cell, given row and column position
// maps directly to the position in the itemlist
// return -1 on failure

kimageshop'IconChooser::cellIndex() (./koffice/kimageshop/ui/iconchooser.cc:222)

int IconChooser::cellIndex( int row, int col )
{
  if ( row < 0 || col < 0 )
    return -1;

  return ( (row * nCols) + col );
}


// eventually select the item, clicked on

kimageshop'IconChooser::mousePressEvent() (./koffice/kimageshop/ui/iconchooser.cc:232)

void IconChooser::mousePressEvent( QMouseEvent *e )
{
  QTableView::mousePressEvent( e );

  if ( e->button() == LeftButton ) {
    QPoint p = e->pos();

    int row = findRow( p.y() );
    int col = findCol( p.x() );

    IconItem *item = itemAt( row, col );
    if ( item ) {
      const QPixmap& pix = item->pixmap();
      if ( pix.width() > itemWidth || pix.height() > itemHeight )
	showFullPixmap( pix, p );

      int oldRow = curRow;
      int oldCol = curCol;

      curRow = row;
      curCol = col;

      updateCell( oldRow, oldCol, true );
      updateCell( curRow, curCol, true );

      emit selected( item );
    }
  }
}


// when a big item is shown in full size, delete it on mouseRelease

kimageshop'IconChooser::mouseReleaseEvent() (./koffice/kimageshop/ui/iconchooser.cc:264)

void IconChooser::mouseReleaseEvent( QMouseEvent * )
{
  if ( pw ) {
    delete pw;
    pw = 0L;
  }
}


// show the full pixmap of a large item in an extra widget

kimageshop'IconChooser::showFullPixmap() (./koffice/kimageshop/ui/iconchooser.cc:274)

void IconChooser::showFullPixmap( const QPixmap& pix, const QPoint& )
{
  pw = new PixmapWidget( pix, 0L );
}


// FIXME: implement keyboard navigation

kimageshop'IconChooser::keyPressEvent() (./koffice/kimageshop/ui/iconchooser.cc:281)

void IconChooser::keyPressEvent( QKeyEvent *e )
{
  QTableView::keyPressEvent( e ); // for now...
}





/////////////////////////////
/////
// helper class PixmapWidget
//