Panel tab enabling/disabling when logged in/out reviewed.
[situare] / src / ui / paneltabbar.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Pekka Nissinen - pekka.nissinen@ixonos.com
6
7     Situare is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     version 2 as published by the Free Software Foundation.
10
11     Situare is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with Situare; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19     USA.
20 */
21
22 #include <QDebug>
23 #include <QButtonGroup>
24
25 #include "panelcommon.h"
26 #include "paneltab.h"
27
28 #include "paneltabbar.h"
29
30 const int APPEND_INDEX = -1;
31 const int NO_ACTIVE_TABS = -1;
32
33 PanelTabBar::PanelTabBar(QWidget *parent)
34     : QWidget(parent),
35       m_activeTab(NO_ACTIVE_TABS)
36 {
37     qDebug() << __PRETTY_FUNCTION__;
38
39     setFixedWidth(PANEL_TAB_BAR_WIDTH);
40
41     m_tabButtonGroup = new QButtonGroup(this);
42
43     connect(m_tabButtonGroup, SIGNAL(buttonPressed(int)),
44                 this, SLOT(setCurrentIndex(int)));
45 }
46
47 int PanelTabBar::addTab(const QIcon& icon)
48 {
49     qDebug() << __PRETTY_FUNCTION__;
50
51     return insertTab(APPEND_INDEX, icon);
52 }
53
54 void PanelTabBar::deselectTabs()
55 {
56     qDebug() << __PRETTY_FUNCTION__;
57
58     m_activeTab = NO_ACTIVE_TABS;
59
60     m_tabButtonGroup->setExclusive(false);
61     m_tabButtonGroup->button(m_tabButtonGroup->checkedId())->setChecked(false);
62     m_tabButtonGroup->setExclusive(true);
63 }
64
65 int PanelTabBar::insertTab(int index, const QIcon& icon)
66 {
67     qDebug() << __PRETTY_FUNCTION__;
68
69     if (index == APPEND_INDEX)
70         m_tabButtonGroup->addButton(new PanelTab(this));
71     else
72         m_tabButtonGroup->addButton(new PanelTab(this), index);
73
74     m_tabButtonGroup->button(index)->setIcon(icon);
75
76     setUpTabLayout();
77
78     return index;
79 }
80
81 void PanelTabBar::removeTab(int index)
82 {
83     qDebug() << __PRETTY_FUNCTION__;
84
85     if (QAbstractButton *tab = m_tabButtonGroup->button(index)) {
86         m_tabButtonGroup->removeButton(tab);
87         delete tab;
88
89         setUpTabLayout();
90     }
91 }
92
93 void PanelTabBar::selectTab(int index)
94 {
95     qDebug() << __PRETTY_FUNCTION__;
96
97     if (!m_tabButtonGroup->button(index)->isChecked())
98         m_tabButtonGroup->button(index)->click();
99 }
100
101 void PanelTabBar::setCurrentIndex(int index)
102 {
103     qDebug() << __PRETTY_FUNCTION__;
104
105     if (m_activeTab == index) {
106         m_activeTab = NO_ACTIVE_TABS;
107         emit tabCloseRequested(index);
108     } else {
109         m_activeTab = index;
110         emit currentChanged(index);
111     }
112 }
113
114 void PanelTabBar::setUpTabLayout()
115 {
116     qDebug() << __PRETTY_FUNCTION__;
117
118     const int TAB_BUTTON_STEPPING = 65;
119
120     QList<QAbstractButton *> tabList = m_tabButtonGroup->buttons();
121     int tabBarHeight = 0;
122     for (int i = 0; i < tabList.size(); i ++) {
123         tabList.at(i)->move(0, i * TAB_BUTTON_STEPPING);
124         tabBarHeight += TAB_BUTTON_STEPPING;
125     }
126
127     setFixedHeight(tabBarHeight);
128
129     emit sizeChangeRequested();
130 }
131
132 QButtonGroup * PanelTabBar::tabs() const
133 {
134     qDebug() << __PRETTY_FUNCTION__;
135
136     return m_tabButtonGroup;
137 }