xref: /glogg/src/tabbedcrawlerwidget.cpp (revision f88e59a4cd2ebac34960e4c840b1cd76e3dc3250)
1333b2889SNicolas Bonnefon /*
2333b2889SNicolas Bonnefon  * Copyright (C) 2014 Nicolas Bonnefon and other contributors
3333b2889SNicolas Bonnefon  *
4333b2889SNicolas Bonnefon  * This file is part of glogg.
5333b2889SNicolas Bonnefon  *
6333b2889SNicolas Bonnefon  * glogg is free software: you can redistribute it and/or modify
7333b2889SNicolas Bonnefon  * it under the terms of the GNU General Public License as published by
8333b2889SNicolas Bonnefon  * the Free Software Foundation, either version 3 of the License, or
9333b2889SNicolas Bonnefon  * (at your option) any later version.
10333b2889SNicolas Bonnefon  *
11333b2889SNicolas Bonnefon  * glogg is distributed in the hope that it will be useful,
12333b2889SNicolas Bonnefon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13333b2889SNicolas Bonnefon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14333b2889SNicolas Bonnefon  * GNU General Public License for more details.
15333b2889SNicolas Bonnefon  *
16333b2889SNicolas Bonnefon  * You should have received a copy of the GNU General Public License
17333b2889SNicolas Bonnefon  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18333b2889SNicolas Bonnefon  */
19333b2889SNicolas Bonnefon 
20333b2889SNicolas Bonnefon #include "tabbedcrawlerwidget.h"
21333b2889SNicolas Bonnefon 
22*f88e59a4SNicolas Bonnefon #include <QKeyEvent>
23*f88e59a4SNicolas Bonnefon 
24333b2889SNicolas Bonnefon #include "log.h"
25333b2889SNicolas Bonnefon 
26333b2889SNicolas Bonnefon TabbedCrawlerWidget::TabbedCrawlerWidget() : QTabWidget(), myTabBar_()
27333b2889SNicolas Bonnefon {
28333b2889SNicolas Bonnefon #if WIN32
29333b2889SNicolas Bonnefon     myTabBar_.setStyleSheet( "QTabBar::tab {\
30333b2889SNicolas Bonnefon             height: 20px; "
31333b2889SNicolas Bonnefon             "}" );
32333b2889SNicolas Bonnefon #else
33333b2889SNicolas Bonnefon     // On GTK style, it looks better with a smaller font
34333b2889SNicolas Bonnefon     myTabBar_.setStyleSheet(
35333b2889SNicolas Bonnefon             "QTabBar::tab {"
36333b2889SNicolas Bonnefon             " height: 20px; "
37333b2889SNicolas Bonnefon             " font-size: 9pt; "
38333b2889SNicolas Bonnefon             "} "
39333b2889SNicolas Bonnefon             "QTabBar::close-button {\
40333b2889SNicolas Bonnefon             height: 6px; width: 6px; }" );
41333b2889SNicolas Bonnefon #endif
42333b2889SNicolas Bonnefon     setTabBar( &myTabBar_ );
43333b2889SNicolas Bonnefon     myTabBar_.hide();
44333b2889SNicolas Bonnefon }
45333b2889SNicolas Bonnefon 
46333b2889SNicolas Bonnefon // I know hiding non-virtual functions from the base class is bad form
47333b2889SNicolas Bonnefon // and I do it here out of pure laziness: I don't want to encapsulate
48333b2889SNicolas Bonnefon // QTabBar with all signals and all just to implement this very simple logic.
49333b2889SNicolas Bonnefon // Maybe one day that should be done better...
50333b2889SNicolas Bonnefon 
51333b2889SNicolas Bonnefon int TabbedCrawlerWidget::addTab( QWidget* page, const QString& label )
52333b2889SNicolas Bonnefon {
53333b2889SNicolas Bonnefon     int index = QTabWidget::addTab( page, label );
54333b2889SNicolas Bonnefon 
55333b2889SNicolas Bonnefon     LOG(logDEBUG) << "addTab, count = " << count();
56333b2889SNicolas Bonnefon 
57333b2889SNicolas Bonnefon     if ( count() > 1 )
58333b2889SNicolas Bonnefon         myTabBar_.show();
59333b2889SNicolas Bonnefon 
60333b2889SNicolas Bonnefon     return index;
61333b2889SNicolas Bonnefon }
62333b2889SNicolas Bonnefon 
63333b2889SNicolas Bonnefon void TabbedCrawlerWidget::removeTab( int index )
64333b2889SNicolas Bonnefon {
65333b2889SNicolas Bonnefon     QTabWidget::removeTab( index );
66333b2889SNicolas Bonnefon 
67333b2889SNicolas Bonnefon     if ( count() <= 1 )
68333b2889SNicolas Bonnefon         myTabBar_.hide();
69333b2889SNicolas Bonnefon }
70*f88e59a4SNicolas Bonnefon 
71*f88e59a4SNicolas Bonnefon void TabbedCrawlerWidget::keyPressEvent( QKeyEvent* event )
72*f88e59a4SNicolas Bonnefon {
73*f88e59a4SNicolas Bonnefon     LOG(logDEBUG) << "TabbedCrawlerWidget::keyPressEvent";
74*f88e59a4SNicolas Bonnefon 
75*f88e59a4SNicolas Bonnefon     char c = ( event->text() )[0].toLatin1();
76*f88e59a4SNicolas Bonnefon     Qt::KeyboardModifiers mod = event->modifiers();
77*f88e59a4SNicolas Bonnefon 
78*f88e59a4SNicolas Bonnefon     // Ctrl + tab
79*f88e59a4SNicolas Bonnefon     if ( mod == Qt::ControlModifier && c == '\t' ) {
80*f88e59a4SNicolas Bonnefon         setCurrentIndex( ( currentIndex() + 1 ) % count() );
81*f88e59a4SNicolas Bonnefon     }
82*f88e59a4SNicolas Bonnefon     // Ctrl + shift + tab
83*f88e59a4SNicolas Bonnefon     else if ( mod == ( Qt::ControlModifier | Qt::ShiftModifier ) && c == '\t' ) {
84*f88e59a4SNicolas Bonnefon         LOG(logDEBUG) << "previous...";
85*f88e59a4SNicolas Bonnefon         setCurrentIndex( ( currentIndex() + 1 ) % count() );
86*f88e59a4SNicolas Bonnefon     }
87*f88e59a4SNicolas Bonnefon     // Alt + numbers
88*f88e59a4SNicolas Bonnefon     else if ( mod == Qt::AltModifier && ( c >= '1' && c <= '8' ) ) {
89*f88e59a4SNicolas Bonnefon         LOG(logDEBUG) << "number " << c;
90*f88e59a4SNicolas Bonnefon         int new_index = c - '0';
91*f88e59a4SNicolas Bonnefon         if ( new_index <= count() )
92*f88e59a4SNicolas Bonnefon             setCurrentIndex( new_index - 1 );
93*f88e59a4SNicolas Bonnefon     }
94*f88e59a4SNicolas Bonnefon     // Alt + 9
95*f88e59a4SNicolas Bonnefon     else if ( mod == Qt::AltModifier && c == '9' ) {
96*f88e59a4SNicolas Bonnefon         LOG(logDEBUG) << "last";
97*f88e59a4SNicolas Bonnefon         setCurrentIndex( count() - 1 );
98*f88e59a4SNicolas Bonnefon     }
99*f88e59a4SNicolas Bonnefon     else {
100*f88e59a4SNicolas Bonnefon         QTabWidget::keyPressEvent( event );
101*f88e59a4SNicolas Bonnefon     }
102*f88e59a4SNicolas Bonnefon }
103