xref: /glogg/src/tabbedcrawlerwidget.cpp (revision 4006f1be24514f0a91d23b4b75207a1bd5461297)
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 #include <QLabel>
24 
25 #include "log.h"
26 
27 TabbedCrawlerWidget::TabbedCrawlerWidget() : QTabWidget(), myTabBar_()
28 {
29 #ifdef WIN32
30     myTabBar_.setStyleSheet( "QTabBar::tab {\
31             height: 20px; "
32             "} "
33             "QTabBar::close-button {\
34               height: 6px; width: 6px;\
35               subcontrol-origin: padding;\
36               subcontrol-position: left;\
37              }" );
38 #else
39     // On GTK style, it looks better with a smaller font
40     myTabBar_.setStyleSheet(
41             "QTabBar::tab {"
42             " height: 20px; "
43             " font-size: 9pt; "
44             "} "
45             "QTabBar::close-button {\
46               height: 6px; width: 6px;\
47               subcontrol-origin: padding;\
48               subcontrol-position: left;\
49              }" );
50 #endif
51     setTabBar( &myTabBar_ );
52     myTabBar_.hide();
53 }
54 
55 // I know hiding non-virtual functions from the base class is bad form
56 // and I do it here out of pure laziness: I don't want to encapsulate
57 // QTabBar with all signals and all just to implement this very simple logic.
58 // Maybe one day that should be done better...
59 
60 int TabbedCrawlerWidget::addTab( QWidget* page, const QString& label )
61 {
62     int index = QTabWidget::addTab( page, label );
63 
64     // Display the icon
65     QLabel* icon_label = new QLabel();
66     icon_label->setPixmap( QPixmap( QString::fromUtf8( "newdata_icon.png" ) ) );
67     icon_label->setAlignment( Qt::AlignCenter );
68     myTabBar_.setTabButton( index, QTabBar::RightSide, icon_label );
69 
70     LOG(logDEBUG) << "addTab, count = " << count();
71 
72     if ( count() > 1 )
73         myTabBar_.show();
74 
75     return index;
76 }
77 
78 void TabbedCrawlerWidget::removeTab( int index )
79 {
80     QTabWidget::removeTab( index );
81 
82     if ( count() <= 1 )
83         myTabBar_.hide();
84 }
85 
86 void TabbedCrawlerWidget::mouseReleaseEvent( QMouseEvent *event)
87 {
88     LOG(logDEBUG) << "TabbedCrawlerWidget::mouseReleaseEvent";
89 
90     if (event->button() == Qt::MidButton)
91     {
92         int tab = this->myTabBar_.tabAt( event->pos() );
93         if (-1 != tab)
94         {
95             emit tabCloseRequested( tab );
96         }
97     }
98 }
99 
100 void TabbedCrawlerWidget::keyPressEvent( QKeyEvent* event )
101 {
102     LOG(logDEBUG) << "TabbedCrawlerWidget::keyPressEvent";
103 
104     char c = ( event->text() )[0].toLatin1();
105     Qt::KeyboardModifiers mod = event->modifiers();
106 
107     // Ctrl + tab
108     if ( mod == Qt::ControlModifier && c == '\t' ) {
109         setCurrentIndex( ( currentIndex() + 1 ) % count() );
110     }
111     // Ctrl + shift + tab
112     else if ( mod == ( Qt::ControlModifier | Qt::ShiftModifier ) && c == '\t' ) {
113         setCurrentIndex( ( currentIndex() + 1 ) % count() );
114     }
115     // Ctrl + numbers
116     else if ( mod == Qt::ControlModifier && ( c >= '1' && c <= '8' ) ) {
117         LOG(logDEBUG) << "number " << c;
118         int new_index = c - '0';
119         if ( new_index <= count() )
120             setCurrentIndex( new_index - 1 );
121     }
122     // Ctrl + 9
123     else if ( mod == Qt::ControlModifier && c == '9' ) {
124         LOG(logDEBUG) << "last";
125         setCurrentIndex( count() - 1 );
126     }
127     else if ( mod == Qt::ControlModifier && (c == 'q' || c == 'w') ) {
128         LOG(logDEBUG) << "Close tab " << currentIndex();
129         emit tabCloseRequested( currentIndex() );
130     }
131     else {
132         QTabWidget::keyPressEvent( event );
133     }
134 }
135 
136 void TabbedCrawlerWidget::setTabDataStatus( int index, DataStatus status )
137 {
138     QLabel* icon_label = dynamic_cast<QLabel*>(
139             myTabBar_.tabButton( index, QTabBar::RightSide ) );
140 
141     if ( icon_label ) {
142         QString icon_file_name;
143         switch ( status ) {
144             case DataStatus::OLD_DATA:
145                 icon_file_name = QString::fromUtf8( "olddata_icon16.png" );
146                 break;
147             case DataStatus::NEW_DATA:
148                 icon_file_name = QString::fromUtf8( "olddata_icon16.png" );
149                 break;
150             case DataStatus::NEW_FILTERED_DATA:
151                 icon_file_name = QString::fromUtf8( "olddata_icon16.png" );
152                 break;
153         }
154         icon_label->setPixmap ( QPixmap( icon_file_name ) );
155     }
156 }
157