4decac696aa223601d10149902156e8985b943bd
[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) ///< @todo magic
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     const int TAB_BAR_TOP_SPACING = 8;
64
65     int verticalStartPoint = TAB_BAR_TOP_SPACING;
66
67     m_tabButtonGroup->addButton(new PanelTab(this), index);
68     m_tabButtonGroup->button(index)->setIcon(icon);
69
70     ///< @todo  [BEGIN]: Purkkaa (to be removed ASAP!!!)
71     if (index > 0)
72         verticalStartPoint += 65 * index;
73
74     m_tabButtonGroup->button(index)->move(0, verticalStartPoint);
75     // [END]: Purkkaa
76
77     return index;
78 }
79
80 void PanelTabBar::removeTab(int index)
81 {
82     qDebug() << __PRETTY_FUNCTION__;
83
84     if (QAbstractButton *tab = m_tabButtonGroup->button(index)) {
85         m_tabButtonGroup->removeButton(tab);
86         delete tab;
87     }
88 }
89
90 void PanelTabBar::selectTab(int index)
91 {
92     qDebug() << __PRETTY_FUNCTION__;
93
94     if (!m_tabButtonGroup->button(index)->isChecked())
95         m_tabButtonGroup->button(index)->click();
96 }
97
98 void PanelTabBar::setCurrentIndex(int index)
99 {
100     qDebug() << __PRETTY_FUNCTION__;
101
102     if (m_activeTab == index) {
103         ///< @todo magic
104         m_activeTab = -1;
105         emit tabCloseRequested(index);
106     } else {
107         m_activeTab = index;
108         emit currentChanged(index);
109     }
110 }