TabbedPanel now emits panelOpened/Closed signals after the animation has stopped.
[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 "panelbar.h"
29 #include "panelcontent.h"
30 #include "paneltab.h"
31 #include "userinfo.h"
32
33 #include "tabbedpanel.h"
34
35 TabbedPanel::TabbedPanel(QWidget *parent)
36     : QWidget(parent),
37       m_isOpen(false),
38       m_activeTab(-1)
39 {
40     qDebug() << __PRETTY_FUNCTION__;
41
42     this->resize(PANEL_BAR_TABBED_WIDTH + PANEL_WIDTH, PANEL_HEIGHT);
43     this->move(PANEL_CLOSED_X, PANEL_TOP_PADDING);
44
45     // --- TABS ---
46     m_panelWidgetStack = new QStackedWidget(this);
47
48     m_tabButtonGroup = new QButtonGroup(this);
49
50     connect(m_tabButtonGroup, SIGNAL(buttonPressed(int)),
51             this, SLOT(setActiveTab(int)));
52
53     // --- BAR ---
54     m_panelBar = new PanelBar(this);
55     m_panelBar->move(PANEL_TAB_WIDTH, 0);
56
57     // --- PANEL CONTENT ---
58     m_panelContent = new PanelContent(this);
59     m_panelContent->setContentWidget(m_panelWidgetStack);
60     m_panelContent->move(PANEL_TAB_WIDTH + PANEL_BAR_WIDTH, 0);
61
62     // --- PANEL ANIMATION ---
63     m_panelStateMachine = new QStateMachine(this);
64
65     m_panelStateClosed = new QState(m_panelStateMachine);
66     m_panelStateOpened = new QState(m_panelStateMachine);
67
68     m_panelAnimation = new QPropertyAnimation(this, "pos", this);
69
70     m_panelStateMachine->setInitialState(m_panelStateClosed);
71
72     m_panelTransitionOpen = m_panelStateClosed->addTransition(this, SIGNAL(toggleState()),
73                                                               m_panelStateOpened);
74     m_panelTransitionOpen->addAnimation(m_panelAnimation);
75
76     m_panelTransitionClose = m_panelStateOpened->addTransition(this, SIGNAL(toggleState()),
77                                                                m_panelStateClosed);
78     m_panelTransitionClose->addAnimation(m_panelAnimation);
79
80     connect(m_panelAnimation, SIGNAL(finished()),
81             this, SLOT(stateChanged()));
82
83     m_panelStateClosed->assignProperty(this, "pos",
84                                        QPoint(PANEL_CLOSED_X, PANEL_TOP_PADDING));
85     m_panelStateOpened->assignProperty(this, "pos",
86                                        QPoint(PANEL_OPENED_X, PANEL_TOP_PADDING));
87
88     m_panelStateMachine->start();
89 }
90
91 int TabbedPanel::addTab(QWidget *widget, const QIcon& icon)
92 {
93     qDebug() << __PRETTY_FUNCTION__;
94
95     return insertTab(-1, widget, icon);
96 }
97
98 int TabbedPanel::insertTab(int index, QWidget *widget, const QIcon& icon)
99 {
100     qDebug() << __PRETTY_FUNCTION__;
101
102     if(!widget)
103         return -1;
104
105     int verticalStartPoint = 8;
106
107     index = m_panelWidgetStack->insertWidget(index, widget);
108     m_tabButtonGroup->addButton(new PanelTab(this), index);
109     m_tabButtonGroup->button(index)->setIcon(icon);
110
111     // [BEGIN]: Purkkaa (to be removed ASAP!!!)
112     if(index > 0)
113         verticalStartPoint += 65 * index;
114
115     m_tabButtonGroup->button(index)->move(0, verticalStartPoint);
116     // [END]: Purkkaa
117
118     return index;
119 }
120
121 void TabbedPanel::removeTab(int index)
122 {
123     qDebug() << __PRETTY_FUNCTION__;
124
125     if(QWidget *widget = m_panelWidgetStack->widget(index)) {
126         m_panelWidgetStack->removeWidget(widget);
127
128         QAbstractButton *tab = m_tabButtonGroup->button(index);
129         m_tabButtonGroup->removeButton(tab);
130         delete tab;
131     }
132 }
133
134 void TabbedPanel::closePanel()
135 {
136     qDebug() << __PRETTY_FUNCTION__;
137
138     if(m_isOpen)
139         emit toggleState();
140 }
141
142 void TabbedPanel::openPanel()
143 {
144     qDebug() << __PRETTY_FUNCTION__;
145
146     if(!m_isOpen)
147         emit toggleState();
148 }
149
150 void TabbedPanel::setActiveTab(int index)
151 {
152     qDebug() << __PRETTY_FUNCTION__;
153
154     if(m_activeTab == -1) {
155         m_activeTab = index;
156         m_panelWidgetStack->setCurrentIndex(index);
157         emit toggleState();
158     } else if(m_activeTab == index) {
159         m_activeTab = -1;
160         emit toggleState();
161     } else {
162         m_activeTab = index;
163         m_panelWidgetStack->setCurrentIndex(index);
164     }
165 }
166
167 void TabbedPanel::resizePanel(const QSize &size)
168 {
169     qDebug() << __PRETTY_FUNCTION__;
170
171     this->resize(PANEL_BAR_TABBED_WIDTH + PANEL_WIDTH,
172                  size.height() - PANEL_TOP_PADDING - PANEL_BOTTOM_PADDING);
173
174     if(!m_isOpen)
175         this->move(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH, PANEL_TOP_PADDING);
176     else
177         this->move(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH - PANEL_WIDTH,
178                    PANEL_TOP_PADDING);
179
180     m_panelBar->resizeBar(size);
181
182     m_panelContent->resizePanelContent(size);
183
184     m_panelStateClosed->assignProperty(this, "pos",
185                         QPoint(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH, PANEL_TOP_PADDING));
186     m_panelStateOpened->assignProperty(this, "pos",
187                         QPoint(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH - PANEL_WIDTH,
188                                PANEL_TOP_PADDING));
189 }
190
191 void TabbedPanel::stateChanged()
192 {
193     qDebug() << __PRETTY_FUNCTION__;
194
195     if(!m_isOpen) {
196         m_isOpen = true;
197         emit panelOpened();
198     } else {
199         m_isOpen = false;
200
201         m_tabButtonGroup->setExclusive(false);
202         m_tabButtonGroup->button(m_tabButtonGroup->checkedId())->setChecked(false);
203         m_tabButtonGroup->setExclusive(true);
204
205         emit panelClosed();
206     }
207 }