Renamed PanelContent class to PanelContentStack and simplified the general 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 <QDebug>
24 #include <QPropertyAnimation>
25 #include <QSignalTransition>
26 #include <QStackedWidget>
27 #include <QStateMachine>
28
29 #include "panelbar.h"
30 #include "panelcontentstack.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     resize(PANEL_BAR_TABBED_WIDTH + PANEL_WIDTH, PANEL_HEIGHT);
42     move(PANEL_CLOSED_X, PANEL_TOP_PADDING);
43
44     // --- TABS ---
45     m_panelTabBar = new PanelTabBar(this);
46
47     connect(m_panelTabBar, SIGNAL(currentChanged(int)),
48             this, SLOT(setCurrentIndex(int)));
49
50     connect(m_panelTabBar, SIGNAL(tabCloseRequested(int)),
51              this, SLOT(closePanel()));
52
53     connect(this, SIGNAL(panelClosed()),
54             m_panelTabBar, SLOT(deselectTabs()));
55
56     // --- BAR ---
57     m_panelBar = new PanelBar(this);
58     m_panelBar->move(PANEL_TAB_WIDTH, 0);
59
60     // --- PANEL CONTENT ---
61     m_panelContentStack = new PanelContentStack(this);
62     m_panelContentStack->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     const int APPEND_INDEX = -1;
98
99     return insertTab(APPEND_INDEX, widget, icon);
100 }
101
102 void TabbedPanel::closePanel()
103 {
104     qDebug() << __PRETTY_FUNCTION__;
105
106     if(m_isOpen)
107         emit toggleState();
108 }
109
110 int TabbedPanel::insertTab(int index, QWidget *widget, const QIcon& icon)
111 {
112     qDebug() << __PRETTY_FUNCTION__;
113
114     index = m_panelContentStack->insertWidget(index, widget);
115     m_panelTabBar->insertTab(index, icon);
116
117     return index;
118 }
119
120 void TabbedPanel::removeTab(int index)
121 {
122     qDebug() << __PRETTY_FUNCTION__;
123
124     if(QWidget *widget = m_panelContentStack->widget(index)) {
125         m_panelContentStack->removeWidget(widget);
126         m_panelTabBar->removeTab(index);
127     }
128 }
129
130 void TabbedPanel::resizePanel(const QSize &size)
131 {
132     qDebug() << __PRETTY_FUNCTION__;
133
134     resize(PANEL_BAR_TABBED_WIDTH + PANEL_WIDTH,
135            size.height() - PANEL_TOP_PADDING - PANEL_BOTTOM_PADDING);
136
137     if (!m_isOpen)
138         move(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH, PANEL_TOP_PADDING);
139     else
140         move(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH - PANEL_WIDTH, PANEL_TOP_PADDING);
141
142     m_panelBar->resizeBar(size);
143
144     m_panelContentStack->resizePanelContentStack(size);
145
146     QPoint closedPosition(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH, PANEL_TOP_PADDING);
147     m_panelStateClosed->assignProperty(this, "pos", closedPosition);
148
149     QPoint openedPosition(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH - PANEL_WIDTH,
150                           PANEL_TOP_PADDING);
151     m_panelStateOpened->assignProperty(this, "pos", openedPosition);
152 }
153
154 void TabbedPanel::showPanel(QWidget *widget)
155 {
156     qDebug() << __PRETTY_FUNCTION__;
157
158     m_panelTabBar->selectTab(m_panelContentStack->indexOf(widget));
159 }
160
161 void TabbedPanel::setCurrentIndex(int index)
162 {
163     qDebug() << __PRETTY_FUNCTION__;
164
165     if ((index < m_panelContentStack->count()) && (index >= 0)) {
166         m_panelContentStack->setCurrentIndex(index);
167
168         if(!m_isOpen)
169             emit toggleState();
170
171         emit currentChanged(index);
172     }
173 }
174
175 void TabbedPanel::stateChanged()
176 {
177     qDebug() << __PRETTY_FUNCTION__;
178
179     if(!m_isOpen) {
180         m_isOpen = true;
181         emit panelOpened();
182     } else {
183         m_isOpen = false;
184         emit panelClosed();
185     }
186 }