Merge branch 'master' into new_panels
[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 <QPropertyAnimation>
26 #include <QSignalTransition>
27 #include <QStackedWidget>
28 #include <QStateMachine>
29
30 #include "panelbar.h"
31 #include "panelcontent.h"
32 #include "paneltab.h"
33 #include "userinfo.h"
34
35 #include "tabbedpanel.h"
36
37 TabbedPanel::TabbedPanel(QWidget *parent)
38     : QWidget(parent),
39       m_isOpen(false),
40       m_activeTab(-1)
41 {
42     qDebug() << __PRETTY_FUNCTION__;
43
44     this->resize(PANEL_BAR_TABBED_WIDTH + PANEL_WIDTH, PANEL_HEIGHT);
45     this->move(PANEL_CLOSED_X, PANEL_TOP_PADDING);
46
47     // --- TABS ---
48     m_panelWidgetStack = new QStackedWidget(this);
49
50     m_tabButtonGroup = new QButtonGroup(this);
51
52     connect(m_tabButtonGroup, SIGNAL(buttonPressed(int)),
53             this, SLOT(setActiveTab(int)));
54
55     // --- BAR ---
56     m_panelBar = new PanelBar(this);
57     m_panelBar->move(PANEL_TAB_WIDTH, 0);
58
59     // --- PANEL CONTENT ---
60     m_panelContent = new PanelContent(this);
61     m_panelContent->setContentWidget(m_panelWidgetStack);
62     m_panelContent->move(PANEL_TAB_WIDTH + PANEL_BAR_WIDTH, 0);
63
64     // --- PANEL ANIMATION ---
65     m_panelStateMachine = new QStateMachine(this);
66
67     m_panelStateClosed = new QState(m_panelStateMachine);
68     m_panelStateOpened = new QState(m_panelStateMachine);
69
70     m_panelAnimation = new QPropertyAnimation(this, "pos", this);
71
72     m_panelStateMachine->setInitialState(m_panelStateClosed);
73
74     m_panelTransitionOpen = m_panelStateClosed->addTransition(this, SIGNAL(toggleState()),
75                                                               m_panelStateOpened);
76     m_panelTransitionOpen->addAnimation(m_panelAnimation);
77
78     m_panelTransitionClose = m_panelStateOpened->addTransition(this, SIGNAL(toggleState()),
79                                                                m_panelStateClosed);
80     m_panelTransitionClose->addAnimation(m_panelAnimation);
81
82     connect(m_panelAnimation, SIGNAL(finished()),
83             this, SLOT(stateChanged()));
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::resizePanel(const QSize &size)
153 {
154     qDebug() << __PRETTY_FUNCTION__;
155
156     this->resize(PANEL_BAR_TABBED_WIDTH + PANEL_WIDTH,
157                  size.height() - PANEL_TOP_PADDING - PANEL_BOTTOM_PADDING);
158
159     if(!m_isOpen)
160         this->move(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH, PANEL_TOP_PADDING);
161     else
162         this->move(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH - PANEL_WIDTH,
163                    PANEL_TOP_PADDING);
164
165     m_panelBar->resizeBar(size);
166
167     m_panelContent->resizePanelContent(size);
168
169     m_panelStateClosed->assignProperty(this, "pos",
170                         QPoint(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH, PANEL_TOP_PADDING));
171     m_panelStateOpened->assignProperty(this, "pos",
172                         QPoint(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH - PANEL_WIDTH,
173                                PANEL_TOP_PADDING));
174 }
175
176 void TabbedPanel::setActiveTab(int index)
177 {
178     qDebug() << __PRETTY_FUNCTION__;
179
180     if(m_activeTab == -1) {
181         m_activeTab = index;
182         m_panelWidgetStack->setCurrentIndex(index);
183         emit tabChanged();
184         emit toggleState();
185     } else if(m_activeTab == index) {
186         m_activeTab = -1;
187         emit toggleState();
188     } else {
189         m_activeTab = index;
190         m_panelWidgetStack->setCurrentIndex(index);
191         emit tabChanged();
192     }
193 }
194
195 void TabbedPanel::stateChanged()
196 {
197     qDebug() << __PRETTY_FUNCTION__;
198
199     if(!m_isOpen) {
200         m_isOpen = true;
201         emit panelOpened();
202     } else {
203         m_isOpen = false;
204
205         m_tabButtonGroup->setExclusive(false);
206         m_tabButtonGroup->button(m_tabButtonGroup->checkedId())->setChecked(false);
207         m_tabButtonGroup->setExclusive(true);
208
209         emit panelClosed();
210     }
211 }