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