Fixed the last remaining magic numbers
[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 "paneltab.h"
26
27 #include "paneltabbar.h"
28
29 const int APPEND_INDEX = -1;
30 const int NO_ACTIVE_TABS = -1;
31
32 PanelTabBar::PanelTabBar(QWidget *parent)
33     : QWidget(parent),
34       m_activeTab(NO_ACTIVE_TABS)
35 {
36     qDebug() << __PRETTY_FUNCTION__;
37
38     m_tabButtonGroup = new QButtonGroup(this);
39
40     connect(m_tabButtonGroup, SIGNAL(buttonPressed(int)),
41                 this, SLOT(setCurrentIndex(int)));
42 }
43
44 int PanelTabBar::addTab(const QIcon& icon)
45 {
46     qDebug() << __PRETTY_FUNCTION__;
47
48     return insertTab(APPEND_INDEX, icon);
49 }
50
51 void PanelTabBar::deselectTabs()
52 {
53     qDebug() << __PRETTY_FUNCTION__;
54
55     m_tabButtonGroup->setExclusive(false);
56     m_tabButtonGroup->button(m_tabButtonGroup->checkedId())->setChecked(false);
57     m_tabButtonGroup->setExclusive(true);
58 }
59
60 int PanelTabBar::insertTab(int index, const QIcon& icon)
61 {
62     qDebug() << __PRETTY_FUNCTION__;
63
64     if (index == APPEND_INDEX)
65         m_tabButtonGroup->addButton(new PanelTab(this));
66     else
67         m_tabButtonGroup->addButton(new PanelTab(this), index);
68
69     m_tabButtonGroup->button(index)->setIcon(icon);
70
71     setUpTabLayout();
72
73     return index;
74 }
75
76 void PanelTabBar::removeTab(int index)
77 {
78     qDebug() << __PRETTY_FUNCTION__;
79
80     if (QAbstractButton *tab = m_tabButtonGroup->button(index)) {
81         m_tabButtonGroup->removeButton(tab);
82         delete tab;
83
84         setUpTabLayout();
85     }
86 }
87
88 void PanelTabBar::selectTab(int index)
89 {
90     qDebug() << __PRETTY_FUNCTION__;
91
92     if (!m_tabButtonGroup->button(index)->isChecked())
93         m_tabButtonGroup->button(index)->click();
94 }
95
96 void PanelTabBar::setCurrentIndex(int index)
97 {
98     qDebug() << __PRETTY_FUNCTION__;
99
100     if (m_activeTab == index) {
101         m_activeTab = NO_ACTIVE_TABS;
102         emit tabCloseRequested(index);
103     } else {
104         m_activeTab = index;
105         emit currentChanged(index);
106     }
107 }
108
109 void PanelTabBar::setUpTabLayout()
110 {
111     qDebug() << __PRETTY_FUNCTION__;
112
113     const int TAB_BUTTON_STEPPING = 65;
114
115     QList<QAbstractButton *> tabList = m_tabButtonGroup->buttons();
116
117     for (int i = 0; i < tabList.size(); i ++) {
118         tabList.at(i)->move(0, i * TAB_BUTTON_STEPPING);
119     }
120 }