Renamed some of the old panel classes and re-organized the whole panel structure
[situare] / src / ui / tabbedpanel.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Kaj Wallin - kaj.wallin@ixonos.com
6         Pekka Nissinen - pekka.nissinen@ixonos.com
7
8     Situare is free software; you can redistribute it and/or
9     modify it under the terms of the GNU General Public License
10     version 2 as published by the Free Software Foundation.
11
12     Situare is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with Situare; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20     USA.
21 */
22
23 #include <QButtonGroup>
24 #include <QDebug>
25 #include <QPainter>
26 #include <QRegion>
27
28 #include "tabbedpanel.h"
29
30 #include "panelbar.h"
31 #include "panelbase.h"
32 #include "panelcontent.h"
33 #include "paneltab.h"
34 #include "userinfo.h"
35
36 enum Tab {USER_INFO, FRIEND_LIST, ROUTING};
37
38 TabbedPanel::TabbedPanel(QWidget *parent)
39     : QWidget(parent),
40       m_isOpen(false),
41       m_activeTab(-1)
42 {
43     qDebug() << __PRETTY_FUNCTION__;
44
45     this->resize(PANEL_BAR_TABBED_WIDTH + PANEL_WIDTH, PANEL_HEIGHT);
46     this->move(PANEL_CLOSED_X, PANEL_TOP_PADDING);
47
48     // --- TABS ---
49     m_panelWidgetStack = new QStackedWidget(this);
50
51     m_tabButtonGroup = new QButtonGroup(this);
52
53     connect(m_tabButtonGroup, SIGNAL(buttonPressed(int)),
54             this, SLOT(setActiveTab(int)));
55
56     // --- BAR ---
57     m_panelBar = new PanelBar(this);
58     m_panelBar->move(PANEL_TAB_WIDTH, 0);
59
60     // --- PANEL CONTENT ---
61     m_panelContent = new PanelContent(this);
62     m_panelContent->setContentWidget(m_panelWidgetStack);
63     m_panelContent->move(PANEL_TAB_WIDTH + PANEL_BAR_WIDTH, 0);
64
65     // --- STATE MACHINE ---
66     m_panelStateMachine = new QStateMachine(this);
67     m_panelStateClosed = new QState(m_panelStateMachine);
68     m_panelStateOpened = new QState(m_panelStateMachine);
69
70     m_panelStateMachine->setInitialState(m_panelStateClosed);
71
72     m_panelTransitionOpen = m_panelStateClosed->addTransition(this, SIGNAL(toggleState()),
73                                                               m_panelStateOpened);
74     m_panelTransitionOpen->addAnimation(new QPropertyAnimation(this, "pos", this));
75
76     m_panelTransitionClose = m_panelStateOpened->addTransition(this, SIGNAL(toggleState()),
77                                                                m_panelStateClosed);
78     m_panelTransitionClose->addAnimation(new QPropertyAnimation(this, "pos", this));
79
80     connect(m_panelStateClosed, SIGNAL(entered()),
81             this, SLOT(stateChangedToClosed()));
82     connect(m_panelStateOpened, SIGNAL(entered()),
83             this, SLOT(stateChangedToOpen()));
84
85     m_panelStateClosed->assignProperty(this, "pos",
86                                        QPoint(PANEL_CLOSED_X, PANEL_TOP_PADDING));
87     m_panelStateOpened->assignProperty(this, "pos",
88                                        QPoint(PANEL_OPENED_X, PANEL_TOP_PADDING));
89
90     m_panelStateMachine->start();
91 }
92
93 int TabbedPanel::addTab(QWidget *widget, const QIcon& icon)
94 {
95     qDebug() << __PRETTY_FUNCTION__;
96
97     return insertTab(-1, widget, icon);
98 }
99
100 int TabbedPanel::insertTab(int index, QWidget *widget, const QIcon& icon)
101 {
102     qDebug() << __PRETTY_FUNCTION__;
103
104     if(!widget)
105         return -1;
106
107     int verticalStartPoint = 8;
108
109     index = m_panelWidgetStack->insertWidget(index, widget);
110     m_tabButtonGroup->addButton(new PanelTab(this), index);
111     m_tabButtonGroup->button(index)->setIcon(icon);
112
113     // [BEGIN]: Purkkaa (to be removed ASAP!!!)
114     if(index > 0)
115         verticalStartPoint += 65 * index;
116
117     m_tabButtonGroup->button(index)->move(0, verticalStartPoint);
118     // [END]: Purkkaa
119
120     return index;
121 }
122
123 void TabbedPanel::removeTab(int index)
124 {
125     qDebug() << __PRETTY_FUNCTION__;
126
127     if(QWidget *widget = m_panelWidgetStack->widget(index)) {
128         m_panelWidgetStack->removeWidget(widget);
129
130         QAbstractButton *tab = m_tabButtonGroup->button(index);
131         m_tabButtonGroup->removeButton(tab);
132         delete tab;
133     }
134 }
135
136 void TabbedPanel::closePanel()
137 {
138     qDebug() << __PRETTY_FUNCTION__;
139
140     if(m_isOpen)
141         emit toggleState();
142 }
143
144 void TabbedPanel::openPanel()
145 {
146     qDebug() << __PRETTY_FUNCTION__;
147
148     if(!m_isOpen)
149         emit toggleState();
150 }
151
152 void TabbedPanel::setActiveTab(int index)
153 {
154     qDebug() << __PRETTY_FUNCTION__;
155
156     if(m_activeTab == -1) {
157         m_activeTab = index;
158         m_panelWidgetStack->setCurrentIndex(index);
159         emit toggleState();
160     } else if(m_activeTab == index) {
161         m_activeTab = -1;
162         emit toggleState();
163     } else {
164         m_activeTab = index;
165         m_panelWidgetStack->setCurrentIndex(index);
166     }
167 }
168
169 void TabbedPanel::resizePanel(const QSize &size)
170 {
171     qDebug() << __PRETTY_FUNCTION__;
172
173     this->resize(PANEL_BAR_TABBED_WIDTH + PANEL_WIDTH,
174                  size.height() - PANEL_TOP_PADDING - PANEL_BOTTOM_PADDING);
175
176     if(!m_isOpen)
177         this->move(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH, PANEL_TOP_PADDING);
178     else
179         this->move(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH - PANEL_WIDTH,
180                    PANEL_TOP_PADDING);
181
182     m_panelBar->resizeBar(size);
183
184     m_panelContent->resizePanelContent(size);
185
186     m_panelStateClosed->assignProperty(this, "pos",
187                         QPoint(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH, PANEL_TOP_PADDING));
188     m_panelStateOpened->assignProperty(this, "pos",
189                         QPoint(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH - PANEL_WIDTH,
190                                PANEL_TOP_PADDING));
191 }
192
193 void TabbedPanel::stateChangedToClosed()
194 {
195     qDebug() << __PRETTY_FUNCTION__;
196
197     m_isOpen = false;
198     emit panelClosed();
199 }
200
201 void TabbedPanel::stateChangedToOpen()
202 {
203     qDebug() << __PRETTY_FUNCTION__;
204
205     m_isOpen = true;
206     emit panelOpened();
207 }