2 Copyright 2013-2015 Mats Sjöberg
4 This file is part of the Pumpa programme.
6 Pumpa is free software: you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Pumpa is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Pumpa. If not, see <http://www.gnu.org/licenses/>.
20 #include "tabwidget.h"
24 //------------------------------------------------------------------------------
26 TabWidget::TabWidget(QWidget
* parent
) : QTabWidget(parent
) {
27 m_sMap
= new QSignalMapper(this);
28 connect(m_sMap
, SIGNAL(mapped(int)), this, SLOT(highlightTab(int)));
29 setTabsClosable(true);
31 connect(tabBar(), SIGNAL(tabCloseRequested(int)),
32 this, SLOT(closeTab(int)));
35 //------------------------------------------------------------------------------
37 int TabWidget::addTab(QWidget
* page
, const QString
& label
,
38 bool highlight
, bool closable
) {
39 int index
= QTabWidget::addTab(page
, label
);
42 addHighlightConnection(page
, index
);
45 tabBar()->setTabButton(index
, QTabBar::LeftSide
, 0);
46 tabBar()->setTabButton(index
, QTabBar::RightSide
, 0);
48 m_okToClose
.insert(index
);
54 //------------------------------------------------------------------------------
56 QWidget
* TabWidget::closeCurrentTab() {
57 QWidget
* w
= currentWidget();
58 closeTab(currentIndex());
62 //------------------------------------------------------------------------------
64 void TabWidget::closeTab(int index
) {
65 if (!closable(index
)) {
66 qDebug() << "[ERROR] Tried to close unclosable tab" << index
;
73 //------------------------------------------------------------------------------
75 void TabWidget::highlightTab(int index
) {
77 index
= currentIndex();
78 tabBar()->setTabTextColor(index
, Qt::red
);
81 //------------------------------------------------------------------------------
83 void TabWidget::deHighlightTab(int index
) {
85 index
= currentIndex();
87 tabBar()->setTabTextColor(index
, pal
.color(foregroundRole()));
90 //------------------------------------------------------------------------------
92 void TabWidget::addHighlightConnection(QWidget
* page
, int index
) {
93 m_sMap
->setMapping(page
, index
);
94 connect(page
, SIGNAL(highlightMe()), m_sMap
, SLOT(map()));
97 //------------------------------------------------------------------------------
99 void TabWidget::keyPressEvent(QKeyEvent
* event
) {
100 int keyn
= event
->key() - Qt::Key_0
;
101 if ((event
->modifiers() & Qt::AltModifier
) && // alt
102 (keyn
>= 1 && keyn
<= 9 && keyn
<= count())) { // + 0 .. 9
103 setCurrentIndex(keyn
-1);
105 QTabWidget::keyPressEvent(event
);