Made a few corrections according to Sami's suggestions
[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 PanelTabBar::PanelTabBar(QWidget *parent)
30     : QWidget(parent),
31       m_activeTab(-1)
32 {
33     qDebug() << __PRETTY_FUNCTION__;
34
35     m_tabButtonGroup = new QButtonGroup(this);
36
37     connect(m_tabButtonGroup, SIGNAL(buttonPressed(int)),
38                 this, SLOT(setCurrentIndex(int)));
39 }
40
41 int PanelTabBar::addTab(const QIcon& icon)
42 {
43     qDebug() << __PRETTY_FUNCTION__;
44
45     const int APPEND_INDEX = -1;
46
47     return insertTab(APPEND_INDEX, icon);
48 }
49
50 void PanelTabBar::deselectTabs()
51 {
52     qDebug() << __PRETTY_FUNCTION__;
53
54     m_tabButtonGroup->setExclusive(false);
55     m_tabButtonGroup->button(m_tabButtonGroup->checkedId())->setChecked(false);
56     m_tabButtonGroup->setExclusive(true);
57 }
58
59 int PanelTabBar::insertTab(int index, const QIcon& icon)
60 {
61     qDebug() << __PRETTY_FUNCTION__;
62
63     int verticalStartPoint = 8;
64
65     m_tabButtonGroup->addButton(new PanelTab(this), index);
66     m_tabButtonGroup->button(index)->setIcon(icon);
67
68     // [BEGIN]: Purkkaa (to be removed ASAP!!!)
69     if(index > 0)
70         verticalStartPoint += 65 * index;
71
72     m_tabButtonGroup->button(index)->move(0, verticalStartPoint);
73     // [END]: Purkkaa
74
75     return index;
76 }
77
78 void PanelTabBar::removeTab(int index)
79 {
80     qDebug() << __PRETTY_FUNCTION__;
81
82     if(QAbstractButton *tab = m_tabButtonGroup->button(index)) {
83         m_tabButtonGroup->removeButton(tab);
84         delete tab;
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 = -1;
102         emit tabCloseRequested(index);
103     } else {
104         m_activeTab = index;
105         emit currentChanged(index);
106     }
107 }