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

Class Index

kblackbox'KBBGraphic (./kdegames/kblackbox/kbbgfx.h:53)

class KBBGraphic : public QWidget
{
  Q_OBJECT
public:
  KBBGraphic(  QPixmap** p=0, QWidget* parent=0, const char* name=0 );
  ~KBBGraphic();

  friend class KBBGame;
  
  void setSize( int w, int h );
  RectOnArray *getGraphicBoard();
  int numC();
  int numR();
  int width();
  int height();
  int wHint();
  int hHint();
  void setCellWidth( int w );
  void setCellHeight( int h );
  void setNumRows( int rows );
  void setNumCols( int cols );

public slots:
  void setInputAccepted( bool b );
  void updateElement( int col, int row );

signals:
  void sizeChanged();
  void inputAt( int, int, int );
  void endMouseClicked();

protected:
  void paintEvent( QPaintEvent* );
  void mousePressEvent( QMouseEvent* );
  void mouseMoveEvent( QMouseEvent* );
  void keyPressEvent( QKeyEvent* );
  void focusInEvent( QFocusEvent* );
  void focusOutEvent( QFocusEvent* );
  void resizeEvent( QResizeEvent* e );

private:
  void paintCell( QPainter* p, int row, int col );
  void paintCellDefault( QPainter*, int row, int col );
  void paintCellPixmap( QPainter*, int row, int col );
  void scalePixmaps( int w, int h );
  RectOnArray *graphicBoard;
  int curRow;
  int curCol;
  bool inputAccepted;
  int minW;
  int minH;
  int cellW;
  int cellH;
  int numCols;
  int numRows;
  QPixmap **pix;
  QPixmap **pixScaled;
  QPixmap *drawBuffer;
};

kblackbox'KBBGraphic::KBBGraphic() (./kdegames/kblackbox/kbbgfx.cpp:25)

KBBGraphic::KBBGraphic( QPixmap **p, QWidget* parent, const char* name )
    : QWidget( parent, name )
{
  int i;

  curRow = curCol = 0;
  setFocusPolicy( NoFocus );
  setBackgroundColor( gray );
  setCellWidth( CELLW );		// set width of cell in pixels
  setCellHeight( CELLH );		// set height of cell in pixels
  setMouseTracking( FALSE );
  
  pix = p;
  if (pix == NULL) pixScaled = NULL;
  else {
    pixScaled = new QPixmap * [NROFTYPES];
    for (i = 0; i < NROFTYPES; i++) {
       pixScaled[i] = new QPixmap;
    }
  }
  graphicBoard = NULL;
  drawBuffer = NULL;
}

/*
   Destructor: deallocates memory for contents
*/


kblackbox'KBBGraphic::~KBBGraphic() (./kdegames/kblackbox/kbbgfx.cpp:53)

KBBGraphic::~KBBGraphic()
{
  int i;

  if (pix != NULL) {
    for (i = 0; i < NROFTYPES; i++) {
      if (pix[i] != NULL) delete pix[i];
    }
    delete pix;
  }
  if (pixScaled != NULL) {
    for (i = 0; i < NROFTYPES; i++) {
      if (pixScaled[i] != NULL) delete pixScaled[i];
    }
    delete pixScaled;
  }
  if (graphicBoard != NULL) delete graphicBoard;
  if (drawBuffer != NULL) delete drawBuffer;
}

/*
   Sets the size of the table
*/


kblackbox'KBBGraphic::setSize() (./kdegames/kblackbox/kbbgfx.cpp:77)

void KBBGraphic::setSize( int w, int h )
{
  if ((w != numCols) || (h != numRows)) {
    if (graphicBoard != NULL) delete graphicBoard;
    graphicBoard = new RectOnArray( w, h );
    graphicBoard->fill( OUTERBBG );
    setNumCols( w );
    setNumRows( h );
    setCellWidth( CELLW );
    setCellHeight( CELLH );
    minW = cellW * numRows;
    minH = cellH * numCols;
    emit(sizeChanged());
  }
}


kblackbox'KBBGraphic::setCellWidth() (./kdegames/kblackbox/kbbgfx.cpp:93)

void KBBGraphic::setCellWidth( int w )
{
  cellW = w;
}


kblackbox'KBBGraphic::setCellHeight() (./kdegames/kblackbox/kbbgfx.cpp:98)

void KBBGraphic::setCellHeight( int h )
{
  cellH = h;
}


kblackbox'KBBGraphic::setNumRows() (./kdegames/kblackbox/kbbgfx.cpp:103)

void KBBGraphic::setNumRows( int rows )
{
  numRows = rows;
}


kblackbox'KBBGraphic::setNumCols() (./kdegames/kblackbox/kbbgfx.cpp:108)

void KBBGraphic::setNumCols( int cols )
{
  numCols = cols;
}

/*
   Scales all pixmaps to desired size.
*/


kblackbox'KBBGraphic::scalePixmaps() (./kdegames/kblackbox/kbbgfx.cpp:117)

void KBBGraphic::scalePixmaps( int w, int h )
{
  int i, w0, h0;
  QWMatrix wm;

  w0 = pix[0]->width();
  h0 = pix[0]->height();
  wm.scale( (float) w / (float) w0, (float) h / (float) h0 );
  for (i = 0; i < NROFTYPES; i++) {
    *pixScaled[i] = pix[i]->xForm( wm );
  }
}

/*
   Returns the sizes of the table
*/


kblackbox'KBBGraphic::numC() (./kdegames/kblackbox/kbbgfx.cpp:134)

int KBBGraphic::numC() { return numCols; }

kblackbox'KBBGraphic::numR() (./kdegames/kblackbox/kbbgfx.cpp:135)

int KBBGraphic::numR() { return numRows; }

kblackbox'KBBGraphic::width() (./kdegames/kblackbox/kbbgfx.cpp:136)

int KBBGraphic::width() { return cellW * numRows; }

kblackbox'KBBGraphic::height() (./kdegames/kblackbox/kbbgfx.cpp:137)

int KBBGraphic::height() { return cellH * numCols; }

kblackbox'KBBGraphic::wHint() (./kdegames/kblackbox/kbbgfx.cpp:138)

int KBBGraphic::wHint() { return minW; }

kblackbox'KBBGraphic::hHint() (./kdegames/kblackbox/kbbgfx.cpp:139)

int KBBGraphic::hHint() { return minH; }

/*
   Returns a pointer to graphicBoard
*/


kblackbox'KBBGraphic::getGraphicBoard() (./kdegames/kblackbox/kbbgfx.cpp:145)

RectOnArray *KBBGraphic::getGraphicBoard() { return graphicBoard; }

/*
   Handles cell painting for the KBBGraphic widget.
*/


kblackbox'KBBGraphic::paintCell() (./kdegames/kblackbox/kbbgfx.cpp:151)

void KBBGraphic::paintCell( QPainter* p, int row, int col )
{
  if (pix == NULL) paintCellDefault( p, row, col );
  else paintCellPixmap( p, row, col );
}


kblackbox'KBBGraphic::paintCellPixmap() (./kdegames/kblackbox/kbbgfx.cpp:157)

void KBBGraphic::paintCellPixmap( QPainter* p, int row, int col )
{
  int w = cellW;
  int h = cellH;
  int x2 = w - 1;
  int y2 = h - 1;
  int type;
  QPixmap pm;

  //  debug( "%d", p->viewport().width() );

  switch (type = graphicBoard->get( col, row )) {
  case MARK1BBG: pm = *pixScaled[MARK1BBG]; break;
  case OUTERBBG: pm = *pixScaled[OUTERBBG]; break;  
  case INNERBBG: pm = *pixScaled[INNERBBG]; break;
  case LASERBBG: pm = *pixScaled[LASERBBG]; break;
  case LFIREBBG: pm = *pixScaled[LFIREBBG]; break;
  case FBALLBBG: pm = *pixScaled[FBALLBBG]; break;
  case TBALLBBG: pm = *pixScaled[TBALLBBG]; break;
  case WBALLBBG: pm = *pixScaled[WBALLBBG]; break;
  default: pm = *pixScaled[OUTERBBG];
  }
  //  debug( "%d %d", pm.width(), w );
  p->drawPixmap( 0, 0, pm );
  //  bitBlt( this, col * w, row * h, &pm );

  p->setPen( black );

  if (type == INNERBBG) {
    p->drawLine( x2, 0, x2, y2 );		// draw vertical line on right
    p->drawLine( 0, y2, x2, y2 );		// draw horiz. line at bottom
    p->drawLine( 0, 0, x2, 0 );
    p->drawLine( 0, 0, 0, y2 );
  }
   
  /*
     Extra drawings for boxes aroud lasers. 
  */
  QString s;
  switch (type) {
  case RLASERBBG:
    s.sprintf( "%c", 'R' );
    p->drawText( 1, 1, x2-1, y2-1, AlignCenter, s );
    break;
  case HLASERBBG:
    s.sprintf( "%c", 'H' );
    p->drawText( 1, 1, x2-1, y2-1, AlignCenter, s );
    break;
  }
  if (type < 0) {
    s.sprintf( "%d", -type );
    p->drawText( 1, 1, x2-1, y2-1, AlignCenter, s );
  }

  /*
     Draw extra frame inside if this is the current cell.
  */
  p->setPen( yellow );
  if ( (row == curRow) && (col == curCol) ) {	// if we are on current cell,
    if ( hasFocus() ) {
      p->drawRect( 0, 0, x2, y2 );
    }
    else {					// we don't have focus, so
      p->setPen( DotLine );		        // use dashed line to
      p->drawRect( 0, 0, x2, y2 );
      p->setPen( SolidLine );		        // restore to normal
    }
  }
}


kblackbox'KBBGraphic::paintCellDefault() (./kdegames/kblackbox/kbbgfx.cpp:227)

void KBBGraphic::paintCellDefault( QPainter* p, int row, int col )
{
  int w = cellW;
  int h = cellH;
  int x2 = w - 1;
  int y2 = h - 1;
  int type;
  QColor color;

  switch (type = graphicBoard->get( col, row )) {
  case MARK1BBG: color = darkRed; break;
  case OUTERBBG: color = white; break;  
  case INNERBBG: color = gray; break;
  case LASERBBG: color = darkGreen; break;
  case LFIREBBG: color = green; break;
  case FBALLBBG: color = red; break;
  case TBALLBBG: color = blue; break;
  case WBALLBBG: color = cyan; break;
  default: color = white;
  }
  p->fillRect( 0, 0, x2, y2, color );

  p->setPen( black );
  p->drawLine( x2, 0, x2, y2 );		// draw vertical line on right
  p->drawLine( 0, y2, x2, y2 );		// draw horiz. line at bottom
  
  /*
     Extra drawings for boxes aroud lasers. 
  */
  QString s;
  switch (type) {
  case RLASERBBG:
    s.sprintf( "%c", 'R' );
    p->drawText( 1, 1, x2-1, y2-1, AlignCenter, s );
    break;
  case HLASERBBG:
    s.sprintf( "%c", 'H' );
    p->drawText( 1, 1, x2-1, y2-1, AlignCenter, s );
    break;
  }
  if (type < 0) {
    s.sprintf( "%d", -type );
    p->drawText( 1, 1, x2-1, y2-1, AlignCenter, s );
  }

  /*
     Draw extra frame inside if this is the current cell.
  */
  if ( (row == curRow) && (col == curCol) ) {	// if we are on current cell,
    if ( hasFocus() ) {
      p->drawEllipse( 1, 1, x2-2, y2-2 );	// draw ellipse
    }
    else {					// we don't have focus, so
      p->setPen( DotLine );		        // use dashed line to
      p->drawEllipse( 1, 1, x2-2, y2-2 );	// draw ellipse
      p->setPen( SolidLine );		        // restore to normal
    }
  }
}

/*
   Xperimantal...
*/


kblackbox'KBBGraphic::paintEvent() (./kdegames/kblackbox/kbbgfx.cpp:291)

void KBBGraphic::paintEvent( QPaintEvent* )
{
  int i, j;
  QPainter paint( drawBuffer );

  //  debug( "%d", drawBuffer->width() );
  for (i = 0; i < numRows; i++) {
    for (j = 0; j < numCols; j++) {
      paint.setViewport( j * cellW, i * cellH, width(), height() );
      paintCell( &paint, i, j );
    }
  }
  bitBlt( this, 0, 0, drawBuffer );
}

/*
   Resize event of the KBBGraphic widget.
*/


kblackbox'KBBGraphic::resizeEvent() (./kdegames/kblackbox/kbbgfx.cpp:310)

void KBBGraphic::resizeEvent( QResizeEvent*  )
{
  int w = QWidget::width();
  int h = QWidget::height();
  int wNew, hNew;

  //  debug("%d %d %d %d", w, h, minW, minH );
  if (w > minW) {
    wNew = w / numC();
  } else {
    wNew = CELLW;
  }
  if (h > minH) {
    hNew = h / numR();
  } else {
    hNew = CELLH;
  }
  if (pix != NULL) scalePixmaps( wNew, hNew );
  setCellWidth( wNew );
  setCellHeight( hNew );

  if (drawBuffer != NULL) delete drawBuffer;
  drawBuffer = new QPixmap( cellW * numRows, cellH * numCols );
}

/*
   Handles mouse press events for the KBBGraphic widget.
*/

kblackbox'KBBGraphic::mousePressEvent() (./kdegames/kblackbox/kbbgfx.cpp:338)

void KBBGraphic::mousePressEvent( QMouseEvent* e )
{
  if (inputAccepted) {
    /*
     * Middle click finishes the game.
     */
    if (e->button() == MidButton) {
      emit endMouseClicked();
      return;
    }
    int oldRow = curRow;
    int oldCol = curCol;
    QPoint pos = e->pos();		// extract pointer position
    curRow = pos.y() / cellH;
    curCol = pos.x() / cellW;
    //debug("%d %d %d ", e->state(), LeftButton, e->state()&LeftButton);
    updateElement( oldCol, oldRow );
    emit inputAt( curCol, curRow, e->button() ); 
  }
}


/*
   Handles mouse move events for the KBBGraphic widget.
*/


kblackbox'KBBGraphic::mouseMoveEvent() (./kdegames/kblackbox/kbbgfx.cpp:364)

void KBBGraphic::mouseMoveEvent( QMouseEvent* e ) {
  if (inputAccepted) {
    int oldRow = curRow;
    int oldCol = curCol;
    QPoint pos = e->pos();                 // extract pointer position
    int movRow = pos.y() / cellH;
    int movCol = pos.x() / cellW;
    // debug("%d %d", movRow,curRow);
    if ( (curRow != movRow) 			// if current cell has moved,
	|| (curCol != movCol) ) {
      curRow = movRow;
      curCol = movCol;
      updateElement( oldCol, oldRow );
      emit inputAt( curCol, curRow, e->state() ); 
    }
  }
}

/*
   Handles key press events for the KBBGraphic widget.
*/


kblackbox'KBBGraphic::keyPressEvent() (./kdegames/kblackbox/kbbgfx.cpp:386)

void KBBGraphic::keyPressEvent( QKeyEvent* e )
{
  if (inputAccepted) {
    int oldRow = curRow;
    int oldCol = curCol;
    switch( e->key() ) {	       	// Look at the key code
    case Key_Left:		       	// If 'left arrow'-key, 
      if( curCol > 0 ) {	       	// and cr't not in leftmost col
	curCol--;     			// set cr't to next left column
      }
      break;
    case Key_Right:		       	// Correspondingly...
      if( curCol < numCols-1 ) {
	curCol++;
      }
      break;
    case Key_Up:
      if( curRow > 0 ) {
	curRow--;
      } 
      break;
    case Key_Down:
      if( curRow < numRows-1 ) {
	curRow++;
      }
      break;
    case Key_Return:
    case Key_Enter:
      emit inputAt( curCol, curRow, LeftButton ); 
      break;
    default:				// If not an interesting key,
      e->ignore();			// we don't accept the event
      return;	
    }
    updateElement( oldCol, oldRow );
    updateElement( curCol, curRow );
  }
}

/*
   Handles focus reception events for the KBBGraphic widget.
*/


kblackbox'KBBGraphic::focusInEvent() (./kdegames/kblackbox/kbbgfx.cpp:429)

void KBBGraphic::focusInEvent( QFocusEvent* )
{
  repaint( FALSE );
}    


/*
   Handles focus loss events for the KBBGraphic widget.
*/


kblackbox'KBBGraphic::focusOutEvent() (./kdegames/kblackbox/kbbgfx.cpp:439)

void KBBGraphic::focusOutEvent( QFocusEvent* )
{
  repaint( FALSE );
}    

/*
   Sets whether user input is processed or not.
*/


kblackbox'KBBGraphic::setInputAccepted() (./kdegames/kblackbox/kbbgfx.cpp:448)

void KBBGraphic::setInputAccepted( bool b )
{
  inputAccepted = b;
  if (b) setFocusPolicy( StrongFocus );
  else setFocusPolicy( NoFocus );
}    

/*
   Updates the cell at (col,row).
*/


kblackbox'KBBGraphic::updateElement() (./kdegames/kblackbox/kbbgfx.cpp:459)

void KBBGraphic::updateElement( int col, int row )
{
  QPainter paint( this );

  paint.setViewport( col * cellW, row * cellH, width(), height() );
  paintCell( &paint, row, col );
}