30
30
31
31
#include " crawlerwidget.h"
32
32
33
+ #include " iconloader.h"
33
34
#include " log.h"
34
35
#include " openfilehelper.h"
35
36
#include " tabnamemapping.h"
36
37
38
+ namespace {
39
+ constexpr QLatin1String PathKey = QLatin1String( " path" , 4 );
40
+ constexpr QLatin1String StatusKey = QLatin1String( " status" , 6 );
41
+ } // namespace
42
+
37
43
TabbedCrawlerWidget::TabbedCrawlerWidget ()
38
44
: QTabWidget()
39
- , olddata_icon_( " :/images/olddata_icon.png" )
40
45
, newdata_icon_( " :/images/newdata_icon.png" )
41
46
, newfiltered_icon_( " :/images/newfiltered_icon.png" )
42
47
, myTabBar_()
43
48
{
49
+
44
50
#ifdef Q_OS_WIN
45
51
myTabBar_.setStyleSheet ( " QTabBar::tab {\
46
52
height: 20px; "
@@ -68,6 +74,27 @@ TabbedCrawlerWidget::TabbedCrawlerWidget()
68
74
myTabBar_.setContextMenuPolicy ( Qt::CustomContextMenu );
69
75
connect ( &myTabBar_, &QWidget::customContextMenuRequested, this ,
70
76
&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 );
71
98
}
72
99
73
100
void TabbedCrawlerWidget::addTabBarItem ( int index, const QString& file_name )
@@ -77,7 +104,12 @@ void TabbedCrawlerWidget::addTabBarItem( int index, const QString& file_name )
77
104
78
105
myTabBar_.setTabText ( index , tabName.isEmpty () ? tab_label : tabName );
79
106
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 );
81
113
82
114
// Display the icon
83
115
auto icon_label = std::make_unique<QLabel>();
@@ -115,6 +147,11 @@ void TabbedCrawlerWidget::mouseReleaseEvent( QMouseEvent* event )
115
147
event->ignore ();
116
148
}
117
149
150
+ QString TabbedCrawlerWidget::tabPathAt ( int index ) const
151
+ {
152
+ return myTabBar_.tabData ( index ).toMap ()[ PathKey ].toString ();
153
+ }
154
+
118
155
void TabbedCrawlerWidget::showContextMenu ( const QPoint& point )
119
156
{
120
157
int tab = myTabBar_.tabAt ( point );
@@ -181,7 +218,7 @@ void TabbedCrawlerWidget::showContextMenu( const QPoint& point )
181
218
auto newName = QInputDialog::getText ( this , " Rename tab" , " Tab name" , QLineEdit::Normal,
182
219
myTabBar_.tabText ( tab ), &isNameEntered );
183
220
if ( isNameEntered ) {
184
- const auto tabPath = myTabBar_. tabData ( tab ). toString ( );
221
+ const auto tabPath = tabPathAt ( tab );
185
222
TabNameMapping::getSynced ().setTabName ( tabPath, newName ).save ();
186
223
187
224
if ( newName.isEmpty () ) {
@@ -194,7 +231,7 @@ void TabbedCrawlerWidget::showContextMenu( const QPoint& point )
194
231
} );
195
232
196
233
connect ( resetTabName, &QAction::triggered, [this , tab] {
197
- const auto tabPath = myTabBar_. tabData ( tab ). toString ( );
234
+ const auto tabPath = tabPathAt ( tab );
198
235
TabNameMapping::getSynced ().setTabName ( tabPath, " " ).save ();
199
236
myTabBar_.setTabText ( tab, QFileInfo ( tabPath ).fileName () );
200
237
} );
@@ -242,15 +279,14 @@ void TabbedCrawlerWidget::keyPressEvent( QKeyEvent* event )
242
279
}
243
280
}
244
281
245
- void TabbedCrawlerWidget::setTabDataStatus ( int index, DataStatus status )
282
+ void TabbedCrawlerWidget::updateIcon ( int index )
246
283
{
247
- LOG ( logDEBUG ) << " TabbedCrawlerWidget::setTabDataStatus " << index ;
248
-
284
+ auto tabData = myTabBar_.tabData ( index ).toMap ();
249
285
auto * icon_label = qobject_cast<QLabel*>( myTabBar_.tabButton ( index , QTabBar::RightSide ) );
250
286
251
287
if ( icon_label ) {
252
288
const QIcon* icon;
253
- switch ( status ) {
289
+ switch ( static_cast <DataStatus>( tabData[ StatusKey ]. toInt () ) ) {
254
290
case DataStatus::OLD_DATA:
255
291
icon = &olddata_icon_;
256
292
break ;
@@ -267,3 +303,14 @@ void TabbedCrawlerWidget::setTabDataStatus( int index, DataStatus status )
267
303
icon_label->setPixmap ( icon->pixmap ( 12 , 12 ) );
268
304
}
269
305
}
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