1333b2889SNicolas Bonnefon /* 2*45ef183cSNicolas Bonnefon * Copyright (C) 2014, 2015 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> 234006f1beSNicolas Bonnefon #include <QLabel> 24f88e59a4SNicolas Bonnefon 25*45ef183cSNicolas Bonnefon #include "crawlerwidget.h" 26*45ef183cSNicolas Bonnefon 27333b2889SNicolas Bonnefon #include "log.h" 28333b2889SNicolas Bonnefon 29333b2889SNicolas Bonnefon TabbedCrawlerWidget::TabbedCrawlerWidget() : QTabWidget(), myTabBar_() 30333b2889SNicolas Bonnefon { 319c01a031SNicolas Bonnefon #ifdef WIN32 32333b2889SNicolas Bonnefon myTabBar_.setStyleSheet( "QTabBar::tab {\ 33333b2889SNicolas Bonnefon height: 20px; " 344006f1beSNicolas Bonnefon "} " 354006f1beSNicolas Bonnefon "QTabBar::close-button {\ 364006f1beSNicolas Bonnefon height: 6px; width: 6px;\ 374006f1beSNicolas Bonnefon subcontrol-origin: padding;\ 384006f1beSNicolas Bonnefon subcontrol-position: left;\ 394006f1beSNicolas Bonnefon }" ); 40333b2889SNicolas Bonnefon #else 41333b2889SNicolas Bonnefon // On GTK style, it looks better with a smaller font 42333b2889SNicolas Bonnefon myTabBar_.setStyleSheet( 43333b2889SNicolas Bonnefon "QTabBar::tab {" 44333b2889SNicolas Bonnefon " height: 20px; " 45333b2889SNicolas Bonnefon " font-size: 9pt; " 46333b2889SNicolas Bonnefon "} " 47333b2889SNicolas Bonnefon "QTabBar::close-button {\ 484006f1beSNicolas Bonnefon height: 6px; width: 6px;\ 494006f1beSNicolas Bonnefon subcontrol-origin: padding;\ 504006f1beSNicolas Bonnefon subcontrol-position: left;\ 514006f1beSNicolas Bonnefon }" ); 52333b2889SNicolas Bonnefon #endif 53333b2889SNicolas Bonnefon setTabBar( &myTabBar_ ); 54333b2889SNicolas Bonnefon myTabBar_.hide(); 55333b2889SNicolas Bonnefon } 56333b2889SNicolas Bonnefon 57333b2889SNicolas Bonnefon // I know hiding non-virtual functions from the base class is bad form 58333b2889SNicolas Bonnefon // and I do it here out of pure laziness: I don't want to encapsulate 59333b2889SNicolas Bonnefon // QTabBar with all signals and all just to implement this very simple logic. 60333b2889SNicolas Bonnefon // Maybe one day that should be done better... 61333b2889SNicolas Bonnefon 62333b2889SNicolas Bonnefon int TabbedCrawlerWidget::addTab( QWidget* page, const QString& label ) 63333b2889SNicolas Bonnefon { 64333b2889SNicolas Bonnefon int index = QTabWidget::addTab( page, label ); 65333b2889SNicolas Bonnefon 66*45ef183cSNicolas Bonnefon if ( auto crawler = dynamic_cast<CrawlerWidget*>( page ) ) { 67*45ef183cSNicolas Bonnefon // Mmmmhhhh... new Qt5 signal syntax create tight coupling between 68*45ef183cSNicolas Bonnefon // us and the sender, baaaaad.... 69*45ef183cSNicolas Bonnefon 70*45ef183cSNicolas Bonnefon // Listen for a changing data status: 71*45ef183cSNicolas Bonnefon connect( crawler, &CrawlerWidget::dataStatusChanged, 72*45ef183cSNicolas Bonnefon [ this, index ]( DataStatus status ) { setTabDataStatus( index, status ); } ); 73*45ef183cSNicolas Bonnefon } 74*45ef183cSNicolas Bonnefon 754006f1beSNicolas Bonnefon // Display the icon 764006f1beSNicolas Bonnefon QLabel* icon_label = new QLabel(); 77*45ef183cSNicolas Bonnefon icon_label->setPixmap( QPixmap( QString::fromUtf8( "olddata_icon.png" ) ) ); 784006f1beSNicolas Bonnefon icon_label->setAlignment( Qt::AlignCenter ); 794006f1beSNicolas Bonnefon myTabBar_.setTabButton( index, QTabBar::RightSide, icon_label ); 804006f1beSNicolas Bonnefon 81333b2889SNicolas Bonnefon LOG(logDEBUG) << "addTab, count = " << count(); 82333b2889SNicolas Bonnefon 83333b2889SNicolas Bonnefon if ( count() > 1 ) 84333b2889SNicolas Bonnefon myTabBar_.show(); 85333b2889SNicolas Bonnefon 86333b2889SNicolas Bonnefon return index; 87333b2889SNicolas Bonnefon } 88333b2889SNicolas Bonnefon 89333b2889SNicolas Bonnefon void TabbedCrawlerWidget::removeTab( int index ) 90333b2889SNicolas Bonnefon { 91333b2889SNicolas Bonnefon QTabWidget::removeTab( index ); 92333b2889SNicolas Bonnefon 93333b2889SNicolas Bonnefon if ( count() <= 1 ) 94333b2889SNicolas Bonnefon myTabBar_.hide(); 95333b2889SNicolas Bonnefon } 96f88e59a4SNicolas Bonnefon 97045bfe08SGustav Andersson void TabbedCrawlerWidget::mouseReleaseEvent( QMouseEvent *event) 98045bfe08SGustav Andersson { 99045bfe08SGustav Andersson LOG(logDEBUG) << "TabbedCrawlerWidget::mouseReleaseEvent"; 100045bfe08SGustav Andersson 101045bfe08SGustav Andersson if (event->button() == Qt::MidButton) 102045bfe08SGustav Andersson { 103045bfe08SGustav Andersson int tab = this->myTabBar_.tabAt( event->pos() ); 104045bfe08SGustav Andersson if (-1 != tab) 105045bfe08SGustav Andersson { 106045bfe08SGustav Andersson emit tabCloseRequested( tab ); 107045bfe08SGustav Andersson } 108045bfe08SGustav Andersson } 109045bfe08SGustav Andersson } 110045bfe08SGustav Andersson 111f88e59a4SNicolas Bonnefon void TabbedCrawlerWidget::keyPressEvent( QKeyEvent* event ) 112f88e59a4SNicolas Bonnefon { 113f88e59a4SNicolas Bonnefon LOG(logDEBUG) << "TabbedCrawlerWidget::keyPressEvent"; 114f88e59a4SNicolas Bonnefon 115f88e59a4SNicolas Bonnefon char c = ( event->text() )[0].toLatin1(); 116f88e59a4SNicolas Bonnefon Qt::KeyboardModifiers mod = event->modifiers(); 117f88e59a4SNicolas Bonnefon 118f88e59a4SNicolas Bonnefon // Ctrl + tab 119f88e59a4SNicolas Bonnefon if ( mod == Qt::ControlModifier && c == '\t' ) { 120f88e59a4SNicolas Bonnefon setCurrentIndex( ( currentIndex() + 1 ) % count() ); 121f88e59a4SNicolas Bonnefon } 122f88e59a4SNicolas Bonnefon // Ctrl + shift + tab 123f88e59a4SNicolas Bonnefon else if ( mod == ( Qt::ControlModifier | Qt::ShiftModifier ) && c == '\t' ) { 124f88e59a4SNicolas Bonnefon setCurrentIndex( ( currentIndex() + 1 ) % count() ); 125f88e59a4SNicolas Bonnefon } 126d73b7073SNicolas Bonnefon // Ctrl + numbers 127d73b7073SNicolas Bonnefon else if ( mod == Qt::ControlModifier && ( c >= '1' && c <= '8' ) ) { 128f88e59a4SNicolas Bonnefon LOG(logDEBUG) << "number " << c; 129f88e59a4SNicolas Bonnefon int new_index = c - '0'; 130f88e59a4SNicolas Bonnefon if ( new_index <= count() ) 131f88e59a4SNicolas Bonnefon setCurrentIndex( new_index - 1 ); 132f88e59a4SNicolas Bonnefon } 133d73b7073SNicolas Bonnefon // Ctrl + 9 134d73b7073SNicolas Bonnefon else if ( mod == Qt::ControlModifier && c == '9' ) { 135f88e59a4SNicolas Bonnefon LOG(logDEBUG) << "last"; 136f88e59a4SNicolas Bonnefon setCurrentIndex( count() - 1 ); 137f88e59a4SNicolas Bonnefon } 138d73b7073SNicolas Bonnefon else if ( mod == Qt::ControlModifier && (c == 'q' || c == 'w') ) { 139045bfe08SGustav Andersson LOG(logDEBUG) << "Close tab " << currentIndex(); 140d73b7073SNicolas Bonnefon emit tabCloseRequested( currentIndex() ); 141d73b7073SNicolas Bonnefon } 142f88e59a4SNicolas Bonnefon else { 143f88e59a4SNicolas Bonnefon QTabWidget::keyPressEvent( event ); 144f88e59a4SNicolas Bonnefon } 145f88e59a4SNicolas Bonnefon } 1464006f1beSNicolas Bonnefon 1474006f1beSNicolas Bonnefon void TabbedCrawlerWidget::setTabDataStatus( int index, DataStatus status ) 1484006f1beSNicolas Bonnefon { 149*45ef183cSNicolas Bonnefon LOG(logDEBUG) << "TabbedCrawlerWidget::setTabDataStatus"; 150*45ef183cSNicolas Bonnefon 1514006f1beSNicolas Bonnefon QLabel* icon_label = dynamic_cast<QLabel*>( 1524006f1beSNicolas Bonnefon myTabBar_.tabButton( index, QTabBar::RightSide ) ); 1534006f1beSNicolas Bonnefon 1544006f1beSNicolas Bonnefon if ( icon_label ) { 1554006f1beSNicolas Bonnefon QString icon_file_name; 1564006f1beSNicolas Bonnefon switch ( status ) { 1574006f1beSNicolas Bonnefon case DataStatus::OLD_DATA: 158*45ef183cSNicolas Bonnefon icon_file_name = QString::fromUtf8( "olddata_icon.png" ); 1594006f1beSNicolas Bonnefon break; 1604006f1beSNicolas Bonnefon case DataStatus::NEW_DATA: 161*45ef183cSNicolas Bonnefon icon_file_name = QString::fromUtf8( "newdata_icon.png" ); 1624006f1beSNicolas Bonnefon break; 1634006f1beSNicolas Bonnefon case DataStatus::NEW_FILTERED_DATA: 164*45ef183cSNicolas Bonnefon icon_file_name = QString::fromUtf8( "newfiltered_icon.png" ); 1654006f1beSNicolas Bonnefon break; 1664006f1beSNicolas Bonnefon } 1674006f1beSNicolas Bonnefon icon_label->setPixmap ( QPixmap( icon_file_name ) ); 1684006f1beSNicolas Bonnefon } 1694006f1beSNicolas Bonnefon } 170