Reviewed new files
[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 ///< @todo TAB_WIDTH not global? move into paintEvent()
30 const int TAB_WIDTH = 66;
31 const int TAB_WIDTH_ACTIVE = 74;
32 const int TAB_HEIGHT = 66;
33
34 PanelTab::PanelTab(QWidget *parent)
35     : QToolButton(parent),
36       m_tabActive(false),
37       m_tabSelected(false)
38 {
39     qDebug() << __PRETTY_FUNCTION__;
40
41     ///< @todo magic, use enum, check also .h todo comment, fix everywhere
42     m_tabPixmaps[0].load(":/res/images/tab_inactive.png");
43     m_tabPixmaps[1].load(":/res/images/tab_inactive2.png");
44     m_tabPixmaps[2].load(":/res/images/tab_active.png");
45
46 //    m_tabRect.setRect(TAB_WIDTH_ACTIVE - TAB_WIDTH, 0, TAB_WIDTH, TAB_HEIGHT);
47
48     setCheckable(true);
49
50     setFixedSize(TAB_WIDTH_ACTIVE, TAB_HEIGHT);
51 }
52
53 void PanelTab::mouseMoveEvent(QMouseEvent *event)
54 {
55     qDebug() << __PRETTY_FUNCTION__;
56
57     if(m_tabSelected) {
58         if(!rect().contains(event->pos()))
59             setDown(false);
60         else
61             setDown(true);
62     }
63 }
64
65 void PanelTab::mousePressEvent(QMouseEvent *event)
66 {
67     qDebug() << __PRETTY_FUNCTION__;
68
69     if(event->button() == Qt::LeftButton) {
70         setDown(true);
71         m_tabSelected = true;
72     }
73 }
74
75 void PanelTab::mouseReleaseEvent(QMouseEvent *event)
76 {
77     qDebug() << __PRETTY_FUNCTION__;
78
79     ///< @todo mouse button is not checked, add check here or remove from mousePressEvent
80
81     if(this->rect().contains(event->pos())) {
82         click();
83
84         if(isChecked()) {
85             setChecked(false);
86 //            m_tabRect.setRect(TAB_WIDTH_ACTIVE - TAB_WIDTH, 0, TAB_WIDTH, TAB_HEIGHT);
87         } else {
88             setChecked(true);
89 //            m_tabRect.setRect(0, 0, TAB_WIDTH_ACTIVE, TAB_HEIGHT);
90         }
91     }
92
93     setDown(false);
94     m_tabSelected = false;
95 }
96
97 void PanelTab::paintEvent(QPaintEvent *)
98 {
99     qDebug() << __PRETTY_FUNCTION__;
100
101     QPainter painter(this);
102
103     if(isChecked()) {
104         m_tabRect.setRect(0, 0, TAB_WIDTH_ACTIVE, TAB_HEIGHT);
105         painter.drawPixmap(m_tabRect, m_tabPixmaps[2]);
106     } else {
107         m_tabRect.setRect(TAB_WIDTH_ACTIVE - TAB_WIDTH, 0, TAB_WIDTH, TAB_HEIGHT);
108         painter.drawPixmap(m_tabRect, m_tabPixmaps[0]);
109     }
110
111     if(isDown())
112         icon().paint(&painter, m_tabRect, Qt::AlignCenter, QIcon::Selected);
113     else if(isChecked())
114         icon().paint(&painter, m_tabRect, Qt::AlignCenter, QIcon::Normal);
115     else
116         icon().paint(&painter, m_tabRect, Qt::AlignCenter, QIcon::Disabled);
117 }
118
119 ///< @todo setActive() method is not used
120 void PanelTab::setActive(bool state)
121 {
122     qDebug() << __PRETTY_FUNCTION__;
123
124     if(state)
125         setChecked(true);
126     else
127         setChecked(false);
128 }