2321ceb10a3f7b21f2d6c4ed13f1cacd4b1a47bf
[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 <QDebug>
24 #include <QPropertyAnimation>
25 #include <QSignalTransition>
26 #include <QStackedWidget>
27 #include <QStateMachine>
28
29 #include "panelbar.h"
30 #include "panelcontent.h"
31 #include "paneltabbar.h"
32
33 #include "tabbedpanel.h"
34
35 TabbedPanel::TabbedPanel(QWidget *parent)
36     : QWidget(parent),
37       m_isOpen(false)
38 {
39     qDebug() << __PRETTY_FUNCTION__;
40
41     this->resize(PANEL_BAR_TABBED_WIDTH + PANEL_WIDTH, PANEL_HEIGHT);
42     this->move(PANEL_CLOSED_X, PANEL_TOP_PADDING);
43
44     // --- TABS ---
45     m_panelWidgetStack = new QStackedWidget(this);
46
47     m_panelTabBar = new PanelTabBar(this);
48
49     connect(m_panelTabBar, SIGNAL(currentChanged(int)),
50             this, SLOT(showTab(int)));
51
52     connect(m_panelTabBar, SIGNAL(tabCloseRequested(int)),
53              this, SLOT(closePanel()));
54
55     connect(this, SIGNAL(panelClosed()),
56             m_panelTabBar, SLOT(deselectTabs()));
57
58     // --- BAR ---
59     m_panelBar = new PanelBar(this);
60     m_panelBar->move(PANEL_TAB_WIDTH, 0);
61
62     // --- PANEL CONTENT ---
63     m_panelContent = new PanelContent(this);
64     m_panelContent->setContentWidget(m_panelWidgetStack);
65     m_panelContent->move(PANEL_TAB_WIDTH + PANEL_BAR_WIDTH, 0);
66
67     // --- PANEL ANIMATION ---
68     m_panelStateMachine = new QStateMachine(this);
69
70     m_panelStateClosed = new QState(m_panelStateMachine);
71     m_panelStateOpened = new QState(m_panelStateMachine);
72
73     m_panelAnimation = new QPropertyAnimation(this, "pos", this);
74
75     m_panelStateMachine->setInitialState(m_panelStateClosed);
76
77     m_panelTransitionOpen = m_panelStateClosed->addTransition(this, SIGNAL(toggleState()),
78                                                               m_panelStateOpened);
79     m_panelTransitionOpen->addAnimation(m_panelAnimation);
80
81     m_panelTransitionClose = m_panelStateOpened->addTransition(this, SIGNAL(toggleState()),
82                                                                m_panelStateClosed);
83     m_panelTransitionClose->addAnimation(m_panelAnimation);
84
85     connect(m_panelAnimation, SIGNAL(finished()),
86             this, SLOT(stateChanged()));
87
88     m_panelStateClosed->assignProperty(this, "pos",
89                                        QPoint(PANEL_CLOSED_X, PANEL_TOP_PADDING));
90     m_panelStateOpened->assignProperty(this, "pos",
91                                        QPoint(PANEL_OPENED_X, PANEL_TOP_PADDING));
92
93     m_panelStateMachine->start();
94 }
95
96 int TabbedPanel::addTab(QWidget *widget, const QIcon& icon)
97 {
98     qDebug() << __PRETTY_FUNCTION__;
99
100     return insertTab(-1, widget, icon);
101 }
102
103 int TabbedPanel::insertTab(int index, QWidget *widget, const QIcon& icon)
104 {
105     qDebug() << __PRETTY_FUNCTION__;
106
107     if(!widget)
108         return -1;
109
110     index = m_panelWidgetStack->insertWidget(index, widget);
111     m_panelTabBar->insertTab(index, icon);
112
113     return index;
114 }
115
116 void TabbedPanel::removeTab(int index)
117 {
118     qDebug() << __PRETTY_FUNCTION__;
119
120     if(QWidget *widget = m_panelWidgetStack->widget(index)) {
121         m_panelWidgetStack->removeWidget(widget);
122         m_panelTabBar->removeTab(index);
123     }
124 }
125
126 void TabbedPanel::closePanel()
127 {
128     qDebug() << __PRETTY_FUNCTION__;
129
130     if(m_isOpen)
131         emit toggleState();
132 }
133
134 void TabbedPanel::openPanel()
135 {
136     qDebug() << __PRETTY_FUNCTION__;
137
138     if(!m_isOpen)
139         emit toggleState();
140 }
141
142 void TabbedPanel::showTab(int index)
143 {
144     qDebug() << __PRETTY_FUNCTION__;
145
146     if (index < m_panelWidgetStack->count() && index >= 0) {
147         m_panelWidgetStack->setCurrentIndex(index);
148         openPanel();
149         emit currentChanged(index);
150     }
151 }
152
153 void TabbedPanel::resizePanel(const QSize &size)
154 {
155     qDebug() << __PRETTY_FUNCTION__;
156
157     this->resize(PANEL_BAR_TABBED_WIDTH + PANEL_WIDTH,
158                  size.height() - PANEL_TOP_PADDING - PANEL_BOTTOM_PADDING);
159
160     if(!m_isOpen)
161         this->move(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH, PANEL_TOP_PADDING);
162     else
163         this->move(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH - PANEL_WIDTH,
164                    PANEL_TOP_PADDING);
165
166     m_panelBar->resizeBar(size);
167
168     m_panelContent->resizePanelContent(size);
169
170     m_panelStateClosed->assignProperty(this, "pos",
171                         QPoint(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH, PANEL_TOP_PADDING));
172     m_panelStateOpened->assignProperty(this, "pos",
173                         QPoint(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH - PANEL_WIDTH,
174                                PANEL_TOP_PADDING));
175 }
176
177 void TabbedPanel::stateChanged()
178 {
179     qDebug() << __PRETTY_FUNCTION__;
180
181     if(!m_isOpen) {
182         m_isOpen = true;
183         emit panelOpened();
184     } else {
185         m_isOpen = false;
186         emit panelClosed();
187     }
188 }