~ubuntu-branches/ubuntu/trusty/hedgewars/trusty-proposed

« back to all changes in this revision

Viewing changes to QTfrontend/ui/widget/itemNum.cpp

  • Committer: Package Import Robot
  • Author(s): Dmitry E. Oboukhov
  • Date: 2011-11-20 18:31:17 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20111120183117-pjhz1n2pvnmxa246
Tags: 0.9.17-1
* [Paul Wise]
 * Mention the homing bee in the package description (Closes: #577092)
 * Also install the hwengine desktop file (LP: #811770)
 * Depend on ttf-dejavu-core since it is smaller
 * Depend on ttf-wqy-zenhei instead of embedding a copy of it
* [Dmitry E. Oboukhov]
 * New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
void ItemNum::wheelEvent ( QWheelEvent * event )
 
64
{
 
65
    if (nonInteractive) return;
 
66
    if (!enabled)
 
67
    {
 
68
        event->ignore();
 
69
        return;
 
70
    }
 
71
    event->accept();
 
72
 
 
73
    bool up = (event->delta() > 0); // positive delta is up, negative is down
 
74
 
 
75
    // negative delta on horizontal wheel is not left, but right
 
76
    if (event->orientation() == Qt::Horizontal)
 
77
        up = !up;
 
78
 
 
79
    if(up)
 
80
    {
 
81
        if((infinityState && numItems <= maxItems) || (!infinityState && numItems < maxItems))
 
82
            incItems();
 
83
    }
 
84
    else
 
85
    {
 
86
        if(numItems > minItems)
 
87
            decItems();
 
88
    }
 
89
  repaint();
 
90
}
 
91
 
 
92
QSize ItemNum::sizeHint () const
 
93
{
 
94
  return QSize((maxItems+1)*12, 32);
 
95
}
 
96
 
 
97
void ItemNum::paintEvent(QPaintEvent* event)
 
98
{
 
99
  Q_UNUSED(event);
 
100
 
 
101
  QPainter painter(this);
 
102
 
 
103
  if (numItems==maxItems+1) {
 
104
    QRect target(0, 0, 100, 32);
 
105
    if (enabled) {
 
106
        painter.drawImage(target, QImage(":/res/infinity.png"));
 
107
    } else {
 
108
        painter.drawImage(target, QImage(":/res/infinitygrey.png"));
 
109
    }
 
110
  } else {
 
111
    for(int i=0; i<numItems; i++) {
 
112
      QRect target(11 * i, i % 2, 25, 35);
 
113
      if (enabled) {
 
114
        painter.drawImage(target, m_im);
 
115
      } else {
 
116
        painter.drawImage(target, m_img);
 
117
      }
 
118
    }
 
119
  }
 
120
}
 
121
 
 
122
unsigned char ItemNum::getItemsNum() const
 
123
{
 
124
  return numItems;
 
125
}
 
126
 
 
127
void ItemNum::setItemsNum(const unsigned char num)
 
128
{
 
129
  numItems=num;
 
130
  repaint();
 
131
}
 
132
 
 
133
void ItemNum::setInfinityState(bool value)
 
134
{
 
135
  infinityState=value;
 
136
}
 
137
 
 
138
void ItemNum::setEnabled(bool value)
 
139
{
 
140
  enabled=value;
 
141
  repaint();
 
142
}