xref: /glogg/src/tabbedcrawlerwidget.cpp (revision 4006f1be24514f0a91d23b4b75207a1bd5461297)
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>
23*4006f1beSNicolas Bonnefon #include <QLabel>
24f88e59a4SNicolas Bonnefon 
25333b2889SNicolas Bonnefon #include "log.h"
26333b2889SNicolas Bonnefon 
27333b2889SNicolas Bonnefon TabbedCrawlerWidget::TabbedCrawlerWidget() : QTabWidget(), myTabBar_()
28333b2889SNicolas Bonnefon {
299c01a031SNicolas Bonnefon #ifdef WIN32
30333b2889SNicolas Bonnefon     myTabBar_.setStyleSheet( "QTabBar::tab {\
31333b2889SNicolas Bonnefon             height: 20px; "
32*4006f1beSNicolas Bonnefon             "} "
33*4006f1beSNicolas Bonnefon             "QTabBar::close-button {\
34*4006f1beSNicolas Bonnefon               height: 6px; width: 6px;\
35*4006f1beSNicolas Bonnefon               subcontrol-origin: padding;\
36*4006f1beSNicolas Bonnefon               subcontrol-position: left;\
37*4006f1beSNicolas Bonnefon              }" );
38333b2889SNicolas Bonnefon #else
39333b2889SNicolas Bonnefon     // On GTK style, it looks better with a smaller font
40333b2889SNicolas Bonnefon     myTabBar_.setStyleSheet(
41333b2889SNicolas Bonnefon             "QTabBar::tab {"
42333b2889SNicolas Bonnefon             " height: 20px; "
43333b2889SNicolas Bonnefon             " font-size: 9pt; "
44333b2889SNicolas Bonnefon             "} "
45333b2889SNicolas Bonnefon             "QTabBar::close-button {\
46*4006f1beSNicolas Bonnefon               height: 6px; width: 6px;\
47*4006f1beSNicolas Bonnefon               subcontrol-origin: padding;\
48*4006f1beSNicolas Bonnefon               subcontrol-position: left;\
49*4006f1beSNicolas Bonnefon              }" );
50333b2889SNicolas Bonnefon #endif
51333b2889SNicolas Bonnefon     setTabBar( &myTabBar_ );
52333b2889SNicolas Bonnefon     myTabBar_.hide();
53333b2889SNicolas Bonnefon }
54333b2889SNicolas Bonnefon 
55333b2889SNicolas Bonnefon // I know hiding non-virtual functions from the base class is bad form
56333b2889SNicolas Bonnefon // and I do it here out of pure laziness: I don't want to encapsulate
57333b2889SNicolas Bonnefon // QTabBar with all signals and all just to implement this very simple logic.
58333b2889SNicolas Bonnefon // Maybe one day that should be done better...
59333b2889SNicolas Bonnefon 
60333b2889SNicolas Bonnefon int TabbedCrawlerWidget::addTab( QWidget* page, const QString& label )
61333b2889SNicolas Bonnefon {
62333b2889SNicolas Bonnefon     int index = QTabWidget::addTab( page, label );
63333b2889SNicolas Bonnefon 
64*4006f1beSNicolas Bonnefon     // Display the icon
65*4006f1beSNicolas Bonnefon     QLabel* icon_label = new QLabel();
66*4006f1beSNicolas Bonnefon     icon_label->setPixmap( QPixmap( QString::fromUtf8( "newdata_icon.png" ) ) );
67*4006f1beSNicolas Bonnefon     icon_label->setAlignment( Qt::AlignCenter );
68*4006f1beSNicolas Bonnefon     myTabBar_.setTabButton( index, QTabBar::RightSide, icon_label );
69*4006f1beSNicolas Bonnefon 
70333b2889SNicolas Bonnefon     LOG(logDEBUG) << "addTab, count = " << count();
71333b2889SNicolas Bonnefon 
72333b2889SNicolas Bonnefon     if ( count() > 1 )
73333b2889SNicolas Bonnefon         myTabBar_.show();
74333b2889SNicolas Bonnefon 
75333b2889SNicolas Bonnefon     return index;
76333b2889SNicolas Bonnefon }
77333b2889SNicolas Bonnefon 
78333b2889SNicolas Bonnefon void TabbedCrawlerWidget::removeTab( int index )
79333b2889SNicolas Bonnefon {
80333b2889SNicolas Bonnefon     QTabWidget::removeTab( index );
81333b2889SNicolas Bonnefon 
82333b2889SNicolas Bonnefon     if ( count() <= 1 )
83333b2889SNicolas Bonnefon         myTabBar_.hide();
84333b2889SNicolas Bonnefon }
85f88e59a4SNicolas Bonnefon 
86045bfe08SGustav Andersson void TabbedCrawlerWidget::mouseReleaseEvent( QMouseEvent *event)
87045bfe08SGustav Andersson {
88045bfe08SGustav Andersson     LOG(logDEBUG) << "TabbedCrawlerWidget::mouseReleaseEvent";
89045bfe08SGustav Andersson 
90045bfe08SGustav Andersson     if (event->button() == Qt::MidButton)
91045bfe08SGustav Andersson     {
92045bfe08SGustav Andersson         int tab = this->myTabBar_.tabAt( event->pos() );
93045bfe08SGustav Andersson         if (-1 != tab)
94045bfe08SGustav Andersson         {
95045bfe08SGustav Andersson             emit tabCloseRequested( tab );
96045bfe08SGustav Andersson         }
97045bfe08SGustav Andersson     }
98045bfe08SGustav Andersson }
99045bfe08SGustav Andersson 
100f88e59a4SNicolas Bonnefon void TabbedCrawlerWidget::keyPressEvent( QKeyEvent* event )
101f88e59a4SNicolas Bonnefon {
102f88e59a4SNicolas Bonnefon     LOG(logDEBUG) << "TabbedCrawlerWidget::keyPressEvent";
103f88e59a4SNicolas Bonnefon 
104f88e59a4SNicolas Bonnefon     char c = ( event->text() )[0].toLatin1();
105f88e59a4SNicolas Bonnefon     Qt::KeyboardModifiers mod = event->modifiers();
106f88e59a4SNicolas Bonnefon 
107f88e59a4SNicolas Bonnefon     // Ctrl + tab
108f88e59a4SNicolas Bonnefon     if ( mod == Qt::ControlModifier && c == '\t' ) {
109f88e59a4SNicolas Bonnefon         setCurrentIndex( ( currentIndex() + 1 ) % count() );
110f88e59a4SNicolas Bonnefon     }
111f88e59a4SNicolas Bonnefon     // Ctrl + shift + tab
112f88e59a4SNicolas Bonnefon     else if ( mod == ( Qt::ControlModifier | Qt::ShiftModifier ) && c == '\t' ) {
113f88e59a4SNicolas Bonnefon         setCurrentIndex( ( currentIndex() + 1 ) % count() );
114f88e59a4SNicolas Bonnefon     }
115d73b7073SNicolas Bonnefon     // Ctrl + numbers
116d73b7073SNicolas Bonnefon     else if ( mod == Qt::ControlModifier && ( c >= '1' && c <= '8' ) ) {
117f88e59a4SNicolas Bonnefon         LOG(logDEBUG) << "number " << c;
118f88e59a4SNicolas Bonnefon         int new_index = c - '0';
119f88e59a4SNicolas Bonnefon         if ( new_index <= count() )
120f88e59a4SNicolas Bonnefon             setCurrentIndex( new_index - 1 );
121f88e59a4SNicolas Bonnefon     }
122d73b7073SNicolas Bonnefon     // Ctrl + 9
123d73b7073SNicolas Bonnefon     else if ( mod == Qt::ControlModifier && c == '9' ) {
124f88e59a4SNicolas Bonnefon         LOG(logDEBUG) << "last";
125f88e59a4SNicolas Bonnefon         setCurrentIndex( count() - 1 );
126f88e59a4SNicolas Bonnefon     }
127d73b7073SNicolas Bonnefon     else if ( mod == Qt::ControlModifier && (c == 'q' || c == 'w') ) {
128045bfe08SGustav Andersson         LOG(logDEBUG) << "Close tab " << currentIndex();
129d73b7073SNicolas Bonnefon         emit tabCloseRequested( currentIndex() );
130d73b7073SNicolas Bonnefon     }
131f88e59a4SNicolas Bonnefon     else {
132f88e59a4SNicolas Bonnefon         QTabWidget::keyPressEvent( event );
133f88e59a4SNicolas Bonnefon     }
134f88e59a4SNicolas Bonnefon }
135*4006f1beSNicolas Bonnefon 
136*4006f1beSNicolas Bonnefon void TabbedCrawlerWidget::setTabDataStatus( int index, DataStatus status )
137*4006f1beSNicolas Bonnefon {
138*4006f1beSNicolas Bonnefon     QLabel* icon_label = dynamic_cast<QLabel*>(
139*4006f1beSNicolas Bonnefon             myTabBar_.tabButton( index, QTabBar::RightSide ) );
140*4006f1beSNicolas Bonnefon 
141*4006f1beSNicolas Bonnefon     if ( icon_label ) {
142*4006f1beSNicolas Bonnefon         QString icon_file_name;
143*4006f1beSNicolas Bonnefon         switch ( status ) {
144*4006f1beSNicolas Bonnefon             case DataStatus::OLD_DATA:
145*4006f1beSNicolas Bonnefon                 icon_file_name = QString::fromUtf8( "olddata_icon16.png" );
146*4006f1beSNicolas Bonnefon                 break;
147*4006f1beSNicolas Bonnefon             case DataStatus::NEW_DATA:
148*4006f1beSNicolas Bonnefon                 icon_file_name = QString::fromUtf8( "olddata_icon16.png" );
149*4006f1beSNicolas Bonnefon                 break;
150*4006f1beSNicolas Bonnefon             case DataStatus::NEW_FILTERED_DATA:
151*4006f1beSNicolas Bonnefon                 icon_file_name = QString::fromUtf8( "olddata_icon16.png" );
152*4006f1beSNicolas Bonnefon                 break;
153*4006f1beSNicolas Bonnefon         }
154*4006f1beSNicolas Bonnefon         icon_label->setPixmap ( QPixmap( icon_file_name ) );
155*4006f1beSNicolas Bonnefon     }
156*4006f1beSNicolas Bonnefon }
157