Skip to content

Commit 0432a9d

Browse files
committed
Fix tab data icon for dark theme (#129)
1 parent e3b04f6 commit 0432a9d

File tree

2 files changed

+89
-38
lines changed

2 files changed

+89
-38
lines changed

src/ui/include/tabbedcrawlerwidget.h

+34-30
Original file line numberDiff line numberDiff line change
@@ -20,55 +20,59 @@
2020
#ifndef TABBEDCRAWLERWIDGET_H
2121
#define TABBEDCRAWLERWIDGET_H
2222

23-
#include <QTabWidget>
2423
#include <QTabBar>
24+
#include <QTabWidget>
2525

2626
#include "data/loadingstatus.h"
2727

2828
// This class represents glogg's main widget, a tabbed
2929
// group of CrawlerWidgets.
3030
// This is a very slightly customised QTabWidget, with
3131
// a particular style.
32-
class TabbedCrawlerWidget : public QTabWidget
33-
{
34-
Q_OBJECT
35-
public:
36-
TabbedCrawlerWidget();
32+
class TabbedCrawlerWidget : public QTabWidget {
33+
Q_OBJECT
34+
public:
35+
TabbedCrawlerWidget();
36+
37+
template <typename T>
38+
int addCrawler( T* crawler, const QString& file_name )
39+
{
40+
const auto index = QTabWidget::addTab( crawler, QString{} );
3741

38-
template<typename T>
39-
int addCrawler( T* crawler, const QString& file_name )
40-
{
41-
const auto index = QTabWidget::addTab( crawler, QString{});
42+
connect( crawler, &T::dataStatusChanged,
43+
[this, index]( DataStatus status ) { setTabDataStatus( index, status ); } );
4244

43-
connect( crawler, &T::dataStatusChanged,
44-
[this, index]( DataStatus status ) { setTabDataStatus( index, status ); } );
45+
addTabBarItem( index, file_name );
4546

46-
addTabBarItem( index, file_name );
47+
return index;
48+
}
4749

48-
return index;
49-
}
50+
void removeCrawler( int index );
5051

51-
void removeCrawler( int index );
52+
// Set the data status (icon) for the tab number 'index'
53+
void setTabDataStatus( int index, DataStatus status );
5254

53-
// Set the data status (icon) for the tab number 'index'
54-
void setTabDataStatus( int index, DataStatus status );
55+
protected:
56+
void keyPressEvent( QKeyEvent* event ) override;
57+
void mouseReleaseEvent( QMouseEvent* event ) override;
58+
void changeEvent( QEvent* event ) override;
5559

56-
protected:
57-
void keyPressEvent( QKeyEvent* event ) override;
58-
void mouseReleaseEvent( QMouseEvent *event) override;
60+
private:
61+
void addTabBarItem( int index, const QString& file_name );
62+
QString tabPathAt( int index ) const;
5963

60-
private:
61-
void addTabBarItem( int index, const QString& file_name);
64+
void loadIcons();
65+
void updateIcon( int index );
6266

63-
private slots:
64-
void showContextMenu(const QPoint &);
67+
private slots:
68+
void showContextMenu( const QPoint& );
6569

66-
private:
67-
const QIcon olddata_icon_;
68-
const QIcon newdata_icon_;
69-
const QIcon newfiltered_icon_;
70+
private:
71+
QIcon olddata_icon_;
72+
QIcon newdata_icon_;
73+
QIcon newfiltered_icon_;
7074

71-
QTabBar myTabBar_;
75+
QTabBar myTabBar_;
7276
};
7377

7478
#endif

src/ui/src/tabbedcrawlerwidget.cpp

+55-8
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,23 @@
3030

3131
#include "crawlerwidget.h"
3232

33+
#include "iconloader.h"
3334
#include "log.h"
3435
#include "openfilehelper.h"
3536
#include "tabnamemapping.h"
3637

38+
namespace {
39+
constexpr QLatin1String PathKey = QLatin1String( "path", 4 );
40+
constexpr QLatin1String StatusKey = QLatin1String( "status", 6 );
41+
} // namespace
42+
3743
TabbedCrawlerWidget::TabbedCrawlerWidget()
3844
: QTabWidget()
39-
, olddata_icon_( ":/images/olddata_icon.png" )
4045
, newdata_icon_( ":/images/newdata_icon.png" )
4146
, newfiltered_icon_( ":/images/newfiltered_icon.png" )
4247
, myTabBar_()
4348
{
49+
4450
#ifdef Q_OS_WIN
4551
myTabBar_.setStyleSheet( "QTabBar::tab {\
4652
height: 20px; "
@@ -68,6 +74,27 @@ TabbedCrawlerWidget::TabbedCrawlerWidget()
6874
myTabBar_.setContextMenuPolicy( Qt::CustomContextMenu );
6975
connect( &myTabBar_, &QWidget::customContextMenuRequested, this,
7076
&TabbedCrawlerWidget::showContextMenu );
77+
78+
QTimer::singleShot( 0, [this] { loadIcons(); } );
79+
}
80+
81+
void TabbedCrawlerWidget::loadIcons()
82+
{
83+
IconLoader iconLoader{ this };
84+
85+
olddata_icon_ = iconLoader.load( "olddata_icon" );
86+
for ( int tab = 0; tab < count(); ++tab ) {
87+
updateIcon( tab );
88+
}
89+
}
90+
91+
void TabbedCrawlerWidget::changeEvent( QEvent* event )
92+
{
93+
if ( event->type() == QEvent::StyleChange ) {
94+
QTimer::singleShot( 0, [this] { loadIcons(); } );
95+
}
96+
97+
QWidget::changeEvent( event );
7198
}
7299

73100
void TabbedCrawlerWidget::addTabBarItem( int index, const QString& file_name )
@@ -77,7 +104,12 @@ void TabbedCrawlerWidget::addTabBarItem( int index, const QString& file_name )
77104

78105
myTabBar_.setTabText( index, tabName.isEmpty() ? tab_label : tabName );
79106
myTabBar_.setTabToolTip( index, QDir::toNativeSeparators( file_name ) );
80-
myTabBar_.setTabData( index, file_name );
107+
108+
QVariantMap tabData;
109+
tabData[ PathKey ] = file_name;
110+
tabData[ StatusKey ] = static_cast<int>( DataStatus::OLD_DATA );
111+
112+
myTabBar_.setTabData( index, tabData );
81113

82114
// Display the icon
83115
auto icon_label = std::make_unique<QLabel>();
@@ -115,6 +147,11 @@ void TabbedCrawlerWidget::mouseReleaseEvent( QMouseEvent* event )
115147
event->ignore();
116148
}
117149

150+
QString TabbedCrawlerWidget::tabPathAt( int index ) const
151+
{
152+
return myTabBar_.tabData( index ).toMap()[ PathKey ].toString();
153+
}
154+
118155
void TabbedCrawlerWidget::showContextMenu( const QPoint& point )
119156
{
120157
int tab = myTabBar_.tabAt( point );
@@ -181,7 +218,7 @@ void TabbedCrawlerWidget::showContextMenu( const QPoint& point )
181218
auto newName = QInputDialog::getText( this, "Rename tab", "Tab name", QLineEdit::Normal,
182219
myTabBar_.tabText( tab ), &isNameEntered );
183220
if ( isNameEntered ) {
184-
const auto tabPath = myTabBar_.tabData( tab ).toString();
221+
const auto tabPath = tabPathAt( tab );
185222
TabNameMapping::getSynced().setTabName( tabPath, newName ).save();
186223

187224
if ( newName.isEmpty() ) {
@@ -194,7 +231,7 @@ void TabbedCrawlerWidget::showContextMenu( const QPoint& point )
194231
} );
195232

196233
connect( resetTabName, &QAction::triggered, [this, tab] {
197-
const auto tabPath = myTabBar_.tabData( tab ).toString();
234+
const auto tabPath = tabPathAt( tab );
198235
TabNameMapping::getSynced().setTabName( tabPath, "" ).save();
199236
myTabBar_.setTabText( tab, QFileInfo( tabPath ).fileName() );
200237
} );
@@ -242,15 +279,14 @@ void TabbedCrawlerWidget::keyPressEvent( QKeyEvent* event )
242279
}
243280
}
244281

245-
void TabbedCrawlerWidget::setTabDataStatus( int index, DataStatus status )
282+
void TabbedCrawlerWidget::updateIcon( int index )
246283
{
247-
LOG( logDEBUG ) << "TabbedCrawlerWidget::setTabDataStatus " << index;
248-
284+
auto tabData = myTabBar_.tabData( index ).toMap();
249285
auto* icon_label = qobject_cast<QLabel*>( myTabBar_.tabButton( index, QTabBar::RightSide ) );
250286

251287
if ( icon_label ) {
252288
const QIcon* icon;
253-
switch ( status ) {
289+
switch ( static_cast<DataStatus>( tabData[ StatusKey ].toInt() ) ) {
254290
case DataStatus::OLD_DATA:
255291
icon = &olddata_icon_;
256292
break;
@@ -267,3 +303,14 @@ void TabbedCrawlerWidget::setTabDataStatus( int index, DataStatus status )
267303
icon_label->setPixmap( icon->pixmap( 12, 12 ) );
268304
}
269305
}
306+
307+
void TabbedCrawlerWidget::setTabDataStatus( int index, DataStatus status )
308+
{
309+
LOG( logDEBUG ) << "TabbedCrawlerWidget::setTabDataStatus " << index;
310+
311+
auto tabData = myTabBar_.tabData( index ).toMap();
312+
tabData[ StatusKey ] = static_cast<int>( status );
313+
myTabBar_.setTabData( index, tabData );
314+
315+
updateIcon( index );
316+
}

0 commit comments

Comments
 (0)