Merge branch 'master' of https://vcs.maemo.org/git/situare
[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 "panelcommon.h"
28 #include "paneltab.h"
29
30 const int TAB_HEIGHT = 66;
31 const int TAB_WIDTH_ACTIVE = PANEL_TAB_BAR_WIDTH;
32
33 PanelTab::PanelTab(QWidget *parent)
34     : QToolButton(parent),
35       m_tabActive(false),
36       m_tabSelected(false)
37 {
38     qDebug() << __PRETTY_FUNCTION__;
39
40     m_tabActiveImage.load(":/res/images/tab_active.png");
41     m_tabInactiveImage.load(":/res/images/tab_inactive.png");
42     m_tabInactiveImage2.load(":/res/images/tab_inactive2.png");
43
44     setCheckable(true);
45
46     setFixedSize(TAB_WIDTH_ACTIVE, TAB_HEIGHT);
47 }
48
49 void PanelTab::mouseMoveEvent(QMouseEvent *event)
50 {
51     qDebug() << __PRETTY_FUNCTION__;
52
53     if (m_tabSelected) {
54         if (!rect().contains(event->pos()))
55             setDown(false);
56         else
57             setDown(true);
58     }
59 }
60
61 void PanelTab::mousePressEvent(QMouseEvent *event)
62 {
63     qDebug() << __PRETTY_FUNCTION__;
64
65     if (event->button() == Qt::LeftButton) {
66         setDown(true);
67         m_tabSelected = true;
68     }
69 }
70
71 void PanelTab::mouseReleaseEvent(QMouseEvent *event)
72 {
73     qDebug() << __PRETTY_FUNCTION__;
74
75     if (event->button() == Qt::LeftButton) {
76         if (rect().contains(event->pos())) {
77             click();
78
79             if (isChecked())
80                 setChecked(false);
81             else
82                 setChecked(true);
83         }
84
85         setDown(false);
86         m_tabSelected = false;
87     }
88 }
89
90 void PanelTab::paintEvent(QPaintEvent *event)
91 {
92     qDebug() << __PRETTY_FUNCTION__;
93
94     Q_UNUSED(event);
95
96     const int TAB_WIDTH = 66;
97     const int TAB_RECT_X = 0;
98     const int TAB_RECT_Y = 0;
99
100     QPainter painter(this);
101
102     if (isChecked()) {
103         m_tabRect.setRect(TAB_RECT_X, TAB_RECT_Y, TAB_WIDTH_ACTIVE, TAB_HEIGHT);
104         painter.drawPixmap(m_tabRect, m_tabActiveImage);
105     } else {
106         m_tabRect.setRect(TAB_WIDTH_ACTIVE - TAB_WIDTH, TAB_RECT_Y, TAB_WIDTH, TAB_HEIGHT);
107         painter.drawPixmap(m_tabRect, m_tabInactiveImage);
108     }
109
110     if (isDown())
111         icon().paint(&painter, m_tabRect, Qt::AlignCenter, QIcon::Selected);
112     else if (!isEnabled())
113         icon().paint(&painter, m_tabRect, Qt::AlignCenter, QIcon::Disabled);
114     else
115         icon().paint(&painter, m_tabRect, Qt::AlignCenter, QIcon::Normal);
116 }