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

Class Index

kjumpingcube'KCubeWidget (./kdegames/kjumpingcube/kcubewidget.h:35)

class KCubeWidget : public QFrame , public Cube 
{
   Q_OBJECT
         
public:
   /** constructs a new KCubeWidget*/
   KCubeWidget(QWidget* parent=0,const char* name=0
                    ,Owner owner=Cube::Nobody,int value=1,int max=0);  
   virtual ~KCubeWidget();   
   
   virtual Owner setOwner(Owner newOwner); 
   virtual void setValue(int newValue);
   
   
   /** takes the information from a Cube */
   KCubeWidget& operator=(const Cube&);
   KCubeWidget& operator=(const KCubeWidget&);
   
   /** shows a tip e.g. blinks with the interval 500 and number times */
   void showHint(int interval=500,int number=5);
   /** stops showing a hint */
   void stopHint();
   
   /** 
   * animates the cube if possible (if feature is enabled)
   * In KCubeWidget this function does nothing, it's just for having
   * a public interface for all classes that inherits KCubeWidget 
   */
   virtual void animate(bool flag);
   
   /** 
   * sets the coordinates of the Cube in a Cubebox;
   * needed for identification when clicked.
   */ 
   void setCoordinates(int row,int column);
   /** returns the row */
   int row() const;
   /** returns the column */
   int column() const;
   
   /** enables or disables possibility to click a cube*/
   static void enableClicks(bool flag);
   static void setColor(Owner forWhom, QPalette newPalette); 
   static QPalette color(Owner forWhom);
   
public slots:
   /** resets the Cube to default values */
   virtual void reset();
   /** shows changed colors*/
   virtual void updateColors();
      
signals:
   void clicked(int row,int column);
   
protected:
   /** checks, if mouseclick was inside this cube*/
   virtual void mouseReleaseEvent(QMouseEvent*);
   
   /** refreshs the contents of the Cube */
   virtual void drawContents(QPainter*);
 
   
  
   int hintCounter;
   
protected slots:
   /** 
   * To this function the hintTimer is connected. 
   * It manage a periodical way of showing a hint.
   */
   virtual void hint(); 

private:
   int _row;
   int _column;
   
   QTimer *hintTimer;
   
   static bool _clicksAllowed;
   static QPalette color1;
   static QPalette color2;
};


kjumpingcube'KCubeWidget::enableClicks() (./kdegames/kjumpingcube/kcubewidget.cpp:36)

void KCubeWidget::enableClicks(bool flag)
{
   _clicksAllowed=flag;
}



kjumpingcube'KCubeWidget::setColor() (./kdegames/kjumpingcube/kcubewidget.cpp:42)

void KCubeWidget::setColor(Owner forWhom, QPalette newPalette)
{
   if(forWhom==One)
   {   
      color1=newPalette;
   }
   else if(forWhom==Two)
   {
      color2=newPalette;
   }
} 


kjumpingcube'KCubeWidget::color() (./kdegames/kjumpingcube/kcubewidget.cpp:54)

QPalette KCubeWidget::color(Owner forWhom)
{
   QPalette color;
   if(forWhom==One)
   {
      color=color1;
   }
   else if(forWhom==Two)
   {
      color=color2;
   }
   
   return color;
}


/* ****************************************************** **
**                 public functions                       **
** ****************************************************** */


kjumpingcube'KCubeWidget::KCubeWidget() (./kdegames/kjumpingcube/kcubewidget.cpp:74)

KCubeWidget::KCubeWidget(QWidget* parent,const char* name
                  ,Owner owner,int value,int max)
              : QFrame(parent,name),
                Cube(owner,value,max)
{
  setFrameStyle(Panel|Raised);
  //setLineWidth(2);
  setMinimumSize(20,20); 

  setCoordinates(0,0);
  
  //initialize hintTimer
  // will be automatically destroyed by the parent
  hintTimer = new QTimer(this);
  hintCounter=0;
  connect(hintTimer,SIGNAL(timeout()),SLOT(hint()));
  
  setPalette(kapp->palette());

  // show values
  repaint(false);
}


kjumpingcube'KCubeWidget::~KCubeWidget() (./kdegames/kjumpingcube/kcubewidget.cpp:97)

KCubeWidget::~KCubeWidget()
{
}



kjumpingcube'KCubeWidget::setValue() (./kdegames/kjumpingcube/kcubewidget.cpp:134)

void KCubeWidget::setValue(int newValue)
{
   Cube::setValue(newValue);
   update();
}



kjumpingcube'KCubeWidget::showHint() (./kdegames/kjumpingcube/kcubewidget.cpp:141)

void KCubeWidget::showHint(int interval,int number)
{
   if(hintTimer->isActive())
      return;
   
   hintCounter=2*number;
   hintTimer->start(interval);
}



kjumpingcube'KCubeWidget::animate() (./kdegames/kjumpingcube/kcubewidget.cpp:151)

void KCubeWidget::animate(bool )
{
}



kjumpingcube'KCubeWidget::setCoordinates() (./kdegames/kjumpingcube/kcubewidget.cpp:156)

void KCubeWidget::setCoordinates(int row,int column)
{
   _row=row;
   _column=column;
}


kjumpingcube'KCubeWidget::row() (./kdegames/kjumpingcube/kcubewidget.cpp:162)

int KCubeWidget::row() const
{
   return _row;
}


kjumpingcube'KCubeWidget::column() (./kdegames/kjumpingcube/kcubewidget.cpp:167)

int KCubeWidget::column() const
{
   return _column;
}




/* ****************************************************** **
**                   public slots                         **
** ****************************************************** */


kjumpingcube'KCubeWidget::reset() (./kdegames/kjumpingcube/kcubewidget.cpp:179)

void KCubeWidget::reset()
{
  setValue(1);
  setOwner(Nobody);
}



kjumpingcube'KCubeWidget::updateColors() (./kdegames/kjumpingcube/kcubewidget.cpp:186)

void KCubeWidget::updateColors()
{
  if(owner()==One)
    setPalette(color1);
  else if(owner()==Two)
    setPalette(color2);
  else if(owner()==Nobody)
     setPalette(kapp->palette());
}


kjumpingcube'KCubeWidget::stopHint() (./kdegames/kjumpingcube/kcubewidget.cpp:196)

void KCubeWidget::stopHint()
{
   if(hintTimer->isActive())
   {
      hintTimer->stop();
      setBackgroundMode(PaletteBackground);
   }

}



/* ****************************************************** **
**                   protected slots                      **
** ****************************************************** */


kjumpingcube'KCubeWidget::hint() (./kdegames/kjumpingcube/kcubewidget.cpp:212)

void KCubeWidget::hint()
{   
   hintCounter--;
   if(hintCounter%2==1)
   {
      setBackgroundMode(PaletteLight);
   }
   else
   {
      setBackgroundMode(PaletteBackground);
   }
   if(hintCounter==0)
   {
      stopHint();
   }
}



/* ****************************************************** **
**                   Event handler                        **
** ****************************************************** */


kjumpingcube'KCubeWidget::mouseReleaseEvent() (./kdegames/kjumpingcube/kcubewidget.cpp:235)

void KCubeWidget::mouseReleaseEvent(QMouseEvent *e)
{
  // only accept click if it was inside this cube
  if(e->x()< 0 || e->x() > this->width() || e->y() < 0 || e->y() > this->height())
    return;

  if(e->button() == LeftButton && _clicksAllowed)
  {
    stopHint();
    emit clicked(row(),column());
  }
}




kjumpingcube'KCubeWidget::drawContents() (./kdegames/kjumpingcube/kcubewidget.cpp:250)

void KCubeWidget::drawContents(QPainter *painter)
{
  QRect contents=contentsRect();
  QPixmap buffer(contents.size());
  buffer.fill(this,contents.topLeft());
  QPainter *p=new QPainter;
  p->begin(&buffer);
  int  h=contents.height();
  int  w=contents.width();
  int circleSize=(h<w?h:w)/7;
  int points=value();
  QBrush brush("black");
  QPen pen("black");
  p->setBrush(brush);
  p->setPen(pen);
  switch(points)
    {
    case 1:
      p->drawEllipse(w/2-circleSize/2,h/2-circleSize/2,circleSize,circleSize);
      break;

    case 3:
      p->drawEllipse(w/2-circleSize/2,h/2-circleSize/2,circleSize,circleSize);
    case 2:
      p->drawEllipse(w/4-circleSize/2,h/4-circleSize/2,circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,3*h/4-circleSize/2,
		     circleSize,circleSize);
      break;
      
    case 5:
      p->drawEllipse(w/2-circleSize/2,h/2-circleSize/2,circleSize,circleSize);
    case 4:
      p->drawEllipse(w/4-circleSize/2,h/4-circleSize/2,circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,h/4-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(w/4-circleSize/2,3*h/4-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,3*h/4-circleSize/2,
		     circleSize,circleSize);
      break;

    case 8:
      p->drawEllipse(w/2-circleSize/2,2*h/3-circleSize/2,
		     circleSize,circleSize);
    case 7:
      p->drawEllipse(w/2-circleSize/2,h/3-circleSize/2,
		     circleSize,circleSize);
    case 6:
      p->drawEllipse(w/4-circleSize/2,h/6-circleSize/2,circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(w/4-circleSize/2,3*h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,3*h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(w/4-circleSize/2,5*h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,5*h/6-circleSize/2,
		     circleSize,circleSize);
      break;
     
   
    case 9:
      p->drawEllipse(w/4-circleSize/2,h/6-circleSize/2,circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(w/4-circleSize/2,3*h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,3*h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(w/4-circleSize/2,5*h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,5*h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(w/2-circleSize/2,2*h/7-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(w/2-circleSize/2,5*h/7-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(w/2-circleSize/2,h/2-circleSize/2,
		     circleSize,circleSize);
      break; 

    default:
      debug("cube had value %d",points);
      QString s;
      s.sprintf("%d",points);
      p->drawText(w/2,h/2,s);
      break;
    }
   p->end();
   delete p;	
   
   painter->drawPixmap(contents.topLeft(),buffer);

}