xref: /glogg/src/tabbedcrawlerwidget.cpp (revision 9c01a031dacba0f21dec76878d718ccaef72fde3)
1 /*
2  * Copyright (C) 2014 Nicolas Bonnefon and other contributors
3  *
4  * This file is part of glogg.
5  *
6  * glogg is free software: you can redistribute it and/or modify
7  * it 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.
10  *
11  * glogg 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 glogg.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "tabbedcrawlerwidget.h"
21 
22 #include <QKeyEvent>
23 
24 #include "log.h"
25 
26 TabbedCrawlerWidget::TabbedCrawlerWidget() : QTabWidget(), myTabBar_()
27 {
28 #ifdef WIN32
29     myTabBar_.setStyleSheet( "QTabBar::tab {\
30             height: 20px; "
31             "}" );
32 #else
33     // On GTK style, it looks better with a smaller font
34     myTabBar_.setStyleSheet(
35             "QTabBar::tab {"
36             " height: 20px; "
37             " font-size: 9pt; "
38             "} "
39             "QTabBar::close-button {\
40             height: 6px; width: 6px; }" );
41 #endif
42     setTabBar( &myTabBar_ );
43     myTabBar_.hide();
44 }
45 
46 // I know hiding non-virtual functions from the base class is bad form
47 // and I do it here out of pure laziness: I don't want to encapsulate
48 // QTabBar with all signals and all just to implement this very simple logic.
49 // Maybe one day that should be done better...
50 
51 int TabbedCrawlerWidget::addTab( QWidget* page, const QString& label )
52 {
53     int index = QTabWidget::addTab( page, label );
54 
55     LOG(logDEBUG) << "addTab, count = " << count();
56 
57     if ( count() > 1 )
58         myTabBar_.show();
59 
60     return index;
61 }
62 
63 void TabbedCrawlerWidget::removeTab( int index )
64 {
65     QTabWidget::removeTab( index );
66 
67     if ( count() <= 1 )
68         myTabBar_.hide();
69 }
70 
71 void TabbedCrawlerWidget::keyPressEvent( QKeyEvent* event )
72 {
73     LOG(logDEBUG) << "TabbedCrawlerWidget::keyPressEvent";
74 
75     char c = ( event->text() )[0].toLatin1();
76     Qt::KeyboardModifiers mod = event->modifiers();
77 
78     // Ctrl + tab
79     if ( mod == Qt::ControlModifier && c == '\t' ) {
80         setCurrentIndex( ( currentIndex() + 1 ) % count() );
81     }
82     // Ctrl + shift + tab
83     else if ( mod == ( Qt::ControlModifier | Qt::ShiftModifier ) && c == '\t' ) {
84         setCurrentIndex( ( currentIndex() + 1 ) % count() );
85     }
86     // Ctrl + numbers
87     else if ( mod == Qt::ControlModifier && ( c >= '1' && c <= '8' ) ) {
88         LOG(logDEBUG) << "number " << c;
89         int new_index = c - '0';
90         if ( new_index <= count() )
91             setCurrentIndex( new_index - 1 );
92     }
93     // Ctrl + 9
94     else if ( mod == Qt::ControlModifier && c == '9' ) {
95         LOG(logDEBUG) << "last";
96         setCurrentIndex( count() - 1 );
97     }
98     else if ( mod == Qt::ControlModifier && (c == 'q' || c == 'w') ) {
99         emit tabCloseRequested( currentIndex() );
100     }
101     else {
102         QTabWidget::keyPressEvent( event );
103     }
104 }
105