xref: /glogg/src/tabbedcrawlerwidget.cpp (revision 045bfe08ca98f6c980126a984e7f90156172a17a)
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::mouseReleaseEvent( QMouseEvent *event)
72 {
73     LOG(logDEBUG) << "TabbedCrawlerWidget::mouseReleaseEvent";
74 
75     if (event->button() == Qt::MidButton)
76     {
77         int tab = this->myTabBar_.tabAt( event->pos() );
78         if (-1 != tab)
79         {
80             emit tabCloseRequested( tab );
81         }
82     }
83 }
84 
85 void TabbedCrawlerWidget::keyPressEvent( QKeyEvent* event )
86 {
87     LOG(logDEBUG) << "TabbedCrawlerWidget::keyPressEvent";
88 
89     char c = ( event->text() )[0].toLatin1();
90     Qt::KeyboardModifiers mod = event->modifiers();
91 
92     // Ctrl + tab
93     if ( mod == Qt::ControlModifier && c == '\t' ) {
94         setCurrentIndex( ( currentIndex() + 1 ) % count() );
95     }
96     // Ctrl + shift + tab
97     else if ( mod == ( Qt::ControlModifier | Qt::ShiftModifier ) && c == '\t' ) {
98         setCurrentIndex( ( currentIndex() + 1 ) % count() );
99     }
100     // Ctrl + numbers
101     else if ( mod == Qt::ControlModifier && ( c >= '1' && c <= '8' ) ) {
102         LOG(logDEBUG) << "number " << c;
103         int new_index = c - '0';
104         if ( new_index <= count() )
105             setCurrentIndex( new_index - 1 );
106     }
107     // Ctrl + 9
108     else if ( mod == Qt::ControlModifier && c == '9' ) {
109         LOG(logDEBUG) << "last";
110         setCurrentIndex( count() - 1 );
111     }
112     else if ( mod == Qt::ControlModifier && (c == 'q' || c == 'w') ) {
113         LOG(logDEBUG) << "Close tab " << currentIndex();
114         emit tabCloseRequested( currentIndex() );
115     }
116     else {
117         QTabWidget::keyPressEvent( event );
118     }
119 }
120