QTfrontend/itemNum.cpp
branchhedgeroid
changeset 6224 42b256eca362
parent 6055 88cfcd9161d3
parent 6223 cc3eb9b7230f
child 6226 3106add9a5bf
equal deleted inserted replaced
6055:88cfcd9161d3 6224:42b256eca362
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2006-2011 Igor Ulyanov <iulyanov@gmail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17  */
       
    18 
       
    19 #include "itemNum.h"
       
    20 #include "hwform.h"
       
    21 
       
    22 #include <QMouseEvent>
       
    23 #include <QPainter>
       
    24 
       
    25 ItemNum::ItemNum(const QImage& im, const QImage& img, QWidget * parent, unsigned char min, unsigned char max) :
       
    26   QFrame(parent), m_im(im), m_img(img), infinityState(false), nonInteractive(false), minItems(min), maxItems(max),
       
    27   numItems(min+2 >= max ? min : min+2)
       
    28 {
       
    29     enabled = true;
       
    30     if(frontendEffects) setAttribute(Qt::WA_PaintOnScreen, true);
       
    31 }
       
    32 
       
    33 ItemNum::~ItemNum()
       
    34 {
       
    35 }
       
    36 
       
    37 void ItemNum::mousePressEvent ( QMouseEvent * event )
       
    38 {
       
    39   if(nonInteractive) return;
       
    40   if(event->button()==Qt::LeftButton && enabled) {
       
    41     event->accept();
       
    42     if((infinityState && numItems <= maxItems) || (!infinityState && numItems < maxItems)) {
       
    43       incItems();
       
    44     } else {
       
    45       numItems = minItems+1;
       
    46       // appears there's an emit in there
       
    47       decItems();
       
    48     }
       
    49   } else if (event->button()==Qt::RightButton && enabled) {
       
    50     event->accept();
       
    51     if(numItems > minItems) {
       
    52       decItems();
       
    53     } else {
       
    54       numItems = maxItems+(infinityState?0:-1);
       
    55       incItems();
       
    56     }
       
    57   } else {
       
    58     event->ignore();
       
    59     return;
       
    60   }
       
    61   repaint();
       
    62 }
       
    63 
       
    64 QSize ItemNum::sizeHint () const
       
    65 {
       
    66   return QSize((maxItems+1)*12, 32);
       
    67 }
       
    68 
       
    69 void ItemNum::paintEvent(QPaintEvent* event)
       
    70 {
       
    71   Q_UNUSED(event);
       
    72 
       
    73   QPainter painter(this);
       
    74 
       
    75   if (numItems==maxItems+1) {
       
    76     QRect target(0, 0, 100, 32);
       
    77     if (enabled) {
       
    78         painter.drawImage(target, QImage(":/res/infinity.png"));
       
    79     } else {
       
    80         painter.drawImage(target, QImage(":/res/infinitygrey.png"));
       
    81     }
       
    82   } else {
       
    83     for(int i=0; i<numItems; i++) {
       
    84       QRect target(11 * i, i % 2, 25, 35);
       
    85       if (enabled) {
       
    86         painter.drawImage(target, m_im);
       
    87       } else {
       
    88         painter.drawImage(target, m_img);
       
    89       }
       
    90     }
       
    91   }
       
    92 }
       
    93 
       
    94 unsigned char ItemNum::getItemsNum() const
       
    95 {
       
    96   return numItems;
       
    97 }
       
    98 
       
    99 void ItemNum::setItemsNum(const unsigned char num)
       
   100 {
       
   101   numItems=num;
       
   102   repaint();
       
   103 }
       
   104 
       
   105 void ItemNum::setInfinityState(bool value)
       
   106 {
       
   107   infinityState=value;
       
   108 }
       
   109 
       
   110 void ItemNum::setEnabled(bool value)
       
   111 {
       
   112   enabled=value;
       
   113   repaint();
       
   114 }