Merge branch 'new_panels' of https://vcs.maemo.org/git/situare 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 <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     ///< @todo Do not use this, REMOVE ALL OCCURENCES IN ALL FILES!
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_panelTabBar = new PanelTabBar(this);
49
50     connect(m_panelTabBar, SIGNAL(currentChanged(int)),
51             this, SLOT(showTab(int)));
52
53     connect(m_panelTabBar, SIGNAL(tabCloseRequested(int)),
54              this, SLOT(closePanel()));
55
56     connect(this, SIGNAL(panelClosed()),
57             m_panelTabBar, SLOT(deselectTabs()));
58
59     // --- BAR ---
60     m_panelBar = new PanelBar(this);
61     m_panelBar->move(PANEL_TAB_WIDTH, 0);
62
63     // --- PANEL CONTENT ---
64     m_panelContent = new PanelContent(this);
65     m_panelContent->setContentWidget(m_panelWidgetStack);
66     m_panelContent->move(PANEL_TAB_WIDTH + PANEL_BAR_WIDTH, 0);
67
68     // --- PANEL ANIMATION ---
69     m_panelStateMachine = new QStateMachine(this);
70
71     m_panelStateClosed = new QState(m_panelStateMachine);
72     m_panelStateOpened = new QState(m_panelStateMachine);
73
74     m_panelAnimation = new QPropertyAnimation(this, "pos", this);
75
76     m_panelStateMachine->setInitialState(m_panelStateClosed);
77
78     m_panelTransitionOpen = m_panelStateClosed->addTransition(this, SIGNAL(toggleState()),
79                                                               m_panelStateOpened);
80     m_panelTransitionOpen->addAnimation(m_panelAnimation);
81
82     m_panelTransitionClose = m_panelStateOpened->addTransition(this, SIGNAL(toggleState()),
83                                                                m_panelStateClosed);
84     m_panelTransitionClose->addAnimation(m_panelAnimation);
85
86     connect(m_panelAnimation, SIGNAL(finished()),
87             this, SLOT(stateChanged()));
88
89     m_panelStateClosed->assignProperty(this, "pos",
90                                        QPoint(PANEL_CLOSED_X, PANEL_TOP_PADDING));
91     m_panelStateOpened->assignProperty(this, "pos",
92                                        QPoint(PANEL_OPENED_X, PANEL_TOP_PADDING));
93
94     m_panelStateMachine->start();
95 }
96
97 int TabbedPanel::addTab(QWidget *widget, const QIcon& icon)
98 {
99     qDebug() << __PRETTY_FUNCTION__;
100
101     ///< @todo magic
102     return insertTab(-1, widget, icon);
103 }
104
105 int TabbedPanel::insertTab(int index, QWidget *widget, const QIcon& icon)
106 {
107     qDebug() << __PRETTY_FUNCTION__;
108
109     ///< @todo callers responsibility to call with right parameters
110     if(!widget)
111         return -1;
112
113     index = m_panelWidgetStack->insertWidget(index, widget);
114     m_panelTabBar->insertTab(index, icon);
115
116     return index;
117 }
118
119 void TabbedPanel::removeTab(int index)
120 {
121     qDebug() << __PRETTY_FUNCTION__;
122
123     if(QWidget *widget = m_panelWidgetStack->widget(index)) {
124         m_panelWidgetStack->removeWidget(widget);
125         m_panelTabBar->removeTab(index);
126     }
127 }
128
129 ///< @todo sort alphabetically (other methods too)
130 void TabbedPanel::closePanel()
131 {
132     qDebug() << __PRETTY_FUNCTION__;
133
134     if(m_isOpen)
135         emit toggleState();
136 }
137
138 void TabbedPanel::openPanel()
139 {
140     qDebug() << __PRETTY_FUNCTION__;
141
142     if(!m_isOpen)
143         emit toggleState();
144 }
145
146 void TabbedPanel::showTab(int index)
147 {
148     qDebug() << __PRETTY_FUNCTION__;
149
150 ///< @todo if ((first) && (second)) {
151     if (index < m_panelWidgetStack->count() && index >= 0) {
152         m_panelWidgetStack->setCurrentIndex(index);
153         openPanel();
154         emit currentChanged(index);
155     }
156 }
157
158 void TabbedPanel::resizePanel(const QSize &size)
159 {
160     qDebug() << __PRETTY_FUNCTION__;
161
162     this->resize(PANEL_BAR_TABBED_WIDTH + PANEL_WIDTH,
163                  size.height() - PANEL_TOP_PADDING - PANEL_BOTTOM_PADDING);
164
165     if (!m_isOpen) {
166         this->move(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH, PANEL_TOP_PADDING);
167     } else {
168         this->move(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH - PANEL_WIDTH,
169                    PANEL_TOP_PADDING);
170     }
171
172     m_panelBar->resizeBar(size);
173
174     m_panelContent->resizePanelContent(size);
175
176     ///< @todo alignment
177     m_panelStateClosed->assignProperty(this, "pos",
178                         QPoint(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH, PANEL_TOP_PADDING));
179     m_panelStateOpened->assignProperty(this, "pos",
180                         QPoint(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH - PANEL_WIDTH,
181                                PANEL_TOP_PADDING));
182 }
183
184 void TabbedPanel::stateChanged()
185 {
186     qDebug() << __PRETTY_FUNCTION__;
187
188     if(!m_isOpen) {
189         m_isOpen = true;
190         emit panelOpened();
191     } else {
192         m_isOpen = false;
193         emit panelClosed();
194     }
195 }