Changed a constant name and reviewed the previous changes
[situare] / src / ui / paneltab.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Pekka Nissinen - pekka.nissinen@ixonos.com
6
7     Situare is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     version 2 as published by the Free Software Foundation.
10
11     Situare is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with Situare; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19     USA.
20 */
21
22 #include <QDebug>
23 #include <QMouseEvent>
24 #include <QPainter>
25 #include <QRect>
26
27 #include "paneltab.h"
28
29 const int TAB_HEIGHT = 66;
30 const int TAB_WIDTH_ACTIVE = PANEL_TAB_BAR_WIDTH;
31
32 PanelTab::PanelTab(QWidget *parent)
33     : QToolButton(parent),
34       m_tabActive(false),
35       m_tabSelected(false)
36 {
37     qDebug() << __PRETTY_FUNCTION__;
38
39     m_tabActiveImage.load(":/res/images/tab_active.png");
40     m_tabInactiveImage.load(":/res/images/tab_inactive.png");
41     m_tabInactiveImage2.load(":/res/images/tab_inactive2.png");
42
43     setCheckable(true);
44
45     setFixedSize(TAB_WIDTH_ACTIVE, TAB_HEIGHT);
46 }
47
48 void PanelTab::mouseMoveEvent(QMouseEvent *event)
49 {
50     qDebug() << __PRETTY_FUNCTION__;
51
52     if (m_tabSelected) {
53         if (!rect().contains(event->pos()))
54             setDown(false);
55         else
56             setDown(true);
57     }
58 }
59
60 void PanelTab::mousePressEvent(QMouseEvent *event)
61 {
62     qDebug() << __PRETTY_FUNCTION__;
63
64     if (event->button() == Qt::LeftButton) {
65         setDown(true);
66         m_tabSelected = true;
67     }
68 }
69
70 void PanelTab::mouseReleaseEvent(QMouseEvent *event)
71 {
72     qDebug() << __PRETTY_FUNCTION__;
73
74     if (event->button() == Qt::LeftButton) {
75         if (rect().contains(event->pos())) {
76             click();
77
78             if (isChecked())
79                 setChecked(false);
80             else
81                 setChecked(true);
82         }
83
84         setDown(false);
85         m_tabSelected = false;
86     }
87 }
88
89 void PanelTab::paintEvent(QPaintEvent *event)
90 {
91     qDebug() << __PRETTY_FUNCTION__;
92
93     Q_UNUSED(event);
94
95     const int TAB_WIDTH = 66;
96     const int TAB_RECT_X = 0;
97     const int TAB_RECT_Y = 0;
98
99     QPainter painter(this);
100
101     if (isChecked()) {
102         m_tabRect.setRect(TAB_RECT_X, TAB_RECT_Y, TAB_WIDTH_ACTIVE, TAB_HEIGHT);
103         painter.drawPixmap(m_tabRect, m_tabActiveImage);
104     } else {
105         m_tabRect.setRect(TAB_WIDTH_ACTIVE - TAB_WIDTH, TAB_RECT_Y, TAB_WIDTH, TAB_HEIGHT);
106         painter.drawPixmap(m_tabRect, m_tabInactiveImage);
107     }
108
109     if (isDown())
110         icon().paint(&painter, m_tabRect, Qt::AlignCenter, QIcon::Selected);
111     else if (isChecked())
112         icon().paint(&painter, m_tabRect, Qt::AlignCenter, QIcon::Normal);
113     else
114         icon().paint(&painter, m_tabRect, Qt::AlignCenter, QIcon::Disabled);
115 }