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 { 28*9c01a031SNicolas 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 71f88e59a4SNicolas Bonnefon void TabbedCrawlerWidget::keyPressEvent( QKeyEvent* event ) 72f88e59a4SNicolas Bonnefon { 73f88e59a4SNicolas Bonnefon LOG(logDEBUG) << "TabbedCrawlerWidget::keyPressEvent"; 74f88e59a4SNicolas Bonnefon 75f88e59a4SNicolas Bonnefon char c = ( event->text() )[0].toLatin1(); 76f88e59a4SNicolas Bonnefon Qt::KeyboardModifiers mod = event->modifiers(); 77f88e59a4SNicolas Bonnefon 78f88e59a4SNicolas Bonnefon // Ctrl + tab 79f88e59a4SNicolas Bonnefon if ( mod == Qt::ControlModifier && c == '\t' ) { 80f88e59a4SNicolas Bonnefon setCurrentIndex( ( currentIndex() + 1 ) % count() ); 81f88e59a4SNicolas Bonnefon } 82f88e59a4SNicolas Bonnefon // Ctrl + shift + tab 83f88e59a4SNicolas Bonnefon else if ( mod == ( Qt::ControlModifier | Qt::ShiftModifier ) && c == '\t' ) { 84f88e59a4SNicolas Bonnefon setCurrentIndex( ( currentIndex() + 1 ) % count() ); 85f88e59a4SNicolas Bonnefon } 86d73b7073SNicolas Bonnefon // Ctrl + numbers 87d73b7073SNicolas Bonnefon else if ( mod == Qt::ControlModifier && ( c >= '1' && c <= '8' ) ) { 88f88e59a4SNicolas Bonnefon LOG(logDEBUG) << "number " << c; 89f88e59a4SNicolas Bonnefon int new_index = c - '0'; 90f88e59a4SNicolas Bonnefon if ( new_index <= count() ) 91f88e59a4SNicolas Bonnefon setCurrentIndex( new_index - 1 ); 92f88e59a4SNicolas Bonnefon } 93d73b7073SNicolas Bonnefon // Ctrl + 9 94d73b7073SNicolas Bonnefon else if ( mod == Qt::ControlModifier && c == '9' ) { 95f88e59a4SNicolas Bonnefon LOG(logDEBUG) << "last"; 96f88e59a4SNicolas Bonnefon setCurrentIndex( count() - 1 ); 97f88e59a4SNicolas Bonnefon } 98d73b7073SNicolas Bonnefon else if ( mod == Qt::ControlModifier && (c == 'q' || c == 'w') ) { 99d73b7073SNicolas Bonnefon emit tabCloseRequested( currentIndex() ); 100d73b7073SNicolas Bonnefon } 101f88e59a4SNicolas Bonnefon else { 102f88e59a4SNicolas Bonnefon QTabWidget::keyPressEvent( event ); 103f88e59a4SNicolas Bonnefon } 104f88e59a4SNicolas Bonnefon } 105