QTfrontend/hedgehogerWidget.cpp
changeset 612 333d095319de
parent 579 94db15de0392
child 624 e7673b036db5
equal deleted inserted replaced
611:8cf6d27cec86 612:333d095319de
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  */
    17  */
    18 
    18 
    19 #include "hedgehogerWidget.h"
    19 #include "hedgehogerWidget.h"
    20 
    20 
    21 #include <QMouseEvent>
       
    22 #include <QPainter>
       
    23 
       
    24 #include "frameTeam.h"
    21 #include "frameTeam.h"
    25 
    22 
    26 CHedgehogerWidget::CHedgehogerWidget(QWidget * parent) :
    23 CHedgehogerWidget::CHedgehogerWidget(const QImage& im, QWidget * parent) :
    27   QWidget(parent), nonInteractive(false)
    24     ItemNum(im, parent)
    28 {
    25 {
    29   if(parent) {
    26   if(parent) {
    30     pOurFrameTeams=dynamic_cast<FrameTeams*>(parent->parentWidget());
    27     pOurFrameTeams=dynamic_cast<FrameTeams*>(parent->parentWidget());
    31   }
    28   }
    32   if(pOurFrameTeams->overallHedgehogs+4>pOurFrameTeams->maxHedgehogsPerGame) {
    29   if(pOurFrameTeams->overallHedgehogs+4>pOurFrameTeams->maxHedgehogsPerGame) {
    33     numHedgehogs=pOurFrameTeams->maxHedgehogsPerGame-pOurFrameTeams->overallHedgehogs;
    30     numItems=pOurFrameTeams->maxHedgehogsPerGame-pOurFrameTeams->overallHedgehogs;
    34   } else numHedgehogs=4;
    31   } else numItems=4;
    35   pOurFrameTeams->overallHedgehogs+=numHedgehogs;
    32   pOurFrameTeams->overallHedgehogs+=numItems;
       
    33 }
       
    34 
       
    35 void CHedgehogerWidget::incItems() 
       
    36 {
       
    37     if(numItems < 8 && pOurFrameTeams->overallHedgehogs<18) {
       
    38       numItems++;
       
    39       pOurFrameTeams->overallHedgehogs++;
       
    40       emit hedgehogsNumChanged();
       
    41     }
       
    42 }
       
    43 
       
    44 void CHedgehogerWidget::decItems()
       
    45 {
       
    46     if(numItems > 1) {
       
    47       numItems--;
       
    48       pOurFrameTeams->overallHedgehogs--;
       
    49       emit hedgehogsNumChanged();
       
    50     }
    36 }
    51 }
    37 
    52 
    38 CHedgehogerWidget::~CHedgehogerWidget()
    53 CHedgehogerWidget::~CHedgehogerWidget()
    39 {
    54 {
    40   pOurFrameTeams->overallHedgehogs-=numHedgehogs;
    55   pOurFrameTeams->overallHedgehogs-=numItems;
    41 }
    56 }
    42 
    57 
    43 void CHedgehogerWidget::setNonInteractive()
    58 void CHedgehogerWidget::setNonInteractive()
    44 {
    59 {
    45   nonInteractive=true;
    60   nonInteractive=true;
    46 }
    61 }
    47 
    62 
    48 void CHedgehogerWidget::mousePressEvent ( QMouseEvent * event )
       
    49 {
       
    50   if(nonInteractive) return;
       
    51   if(event->button()==Qt::LeftButton) {
       
    52     event->accept();
       
    53     if(numHedgehogs < 8 && pOurFrameTeams->overallHedgehogs<18) {
       
    54       numHedgehogs++;
       
    55       pOurFrameTeams->overallHedgehogs++;
       
    56       emit hedgehogsNumChanged();
       
    57     }
       
    58   } else if (event->button()==Qt::RightButton) {
       
    59     event->accept();
       
    60     if(numHedgehogs > 1) {
       
    61       numHedgehogs--;
       
    62       pOurFrameTeams->overallHedgehogs--;
       
    63       emit hedgehogsNumChanged();
       
    64     }
       
    65   } else {
       
    66     event->ignore();
       
    67     return;
       
    68   }
       
    69   repaint();
       
    70 }
       
    71 
       
    72 void CHedgehogerWidget::setHHNum(unsigned int num)
    63 void CHedgehogerWidget::setHHNum(unsigned int num)
    73 {
    64 {
    74   unsigned int diff=numHedgehogs-num;
    65   unsigned int diff=numItems-num;
    75   numHedgehogs=num;
    66   numItems=num;
    76   pOurFrameTeams->overallHedgehogs+=diff;
    67   pOurFrameTeams->overallHedgehogs+=diff;
    77   repaint();
    68   repaint();
    78 }
    69 }
    79 
    70 
    80 void CHedgehogerWidget::paintEvent(QPaintEvent* event)
       
    81 {
       
    82   QImage image(":/res/hh25x25.png");
       
    83 
       
    84   QPainter painter(this);
       
    85 
       
    86   for(int i=0; i<numHedgehogs; i++) {
       
    87     QRect target(11 * i, i % 2, 25, 25);
       
    88     painter.drawImage(target, image);
       
    89   }
       
    90 }
       
    91 
       
    92 unsigned char CHedgehogerWidget::getHedgehogsNum() const
    71 unsigned char CHedgehogerWidget::getHedgehogsNum() const
    93 {
    72 {
    94   return numHedgehogs;
    73   return numItems;
    95 }
    74 }