@@ -13,6 +13,15 @@ class QMenu;
1313#define QUITIFNULL (VIEW ) if (nullptr ==(VIEW)) { return ; }
1414#define CURRENTIFNULL (VIEW ) if (nullptr ==VIEW) { VIEW = currentZimView ();}
1515
16+ namespace
17+ {
18+
19+ QAction* getAction (KiwixApp::Actions action) {
20+ return KiwixApp::instance ()->getAction (action);
21+ }
22+
23+ } // unnamed namespace
24+
1625TabBar::TabBar (QWidget *parent) :
1726 QTabBar(parent)
1827{
@@ -24,78 +33,31 @@ TabBar::TabBar(QWidget *parent) :
2433 setMovable (true );
2534 setIconSize (QSize (30 , 30 ));
2635 connect (this , &QTabBar::currentChanged, this , &TabBar::onCurrentChanged, Qt::QueuedConnection);
27- auto app = KiwixApp::instance ();
2836
29- connect (app->getAction (KiwixApp::NextTabAction), &QAction::triggered, this , &TabBar::moveToNextTab);
30- connect (app->getAction (KiwixApp::PreviousTabAction), &QAction::triggered, this , &TabBar::moveToPreviousTab);
31- connect (app->getAction (KiwixApp::NewTabAction), &QAction::triggered,
32- this , [=]() {
33- this ->createNewTab (true , false );
34- auto topWidget = KiwixApp::instance ()->getMainWindow ()->getTopWidget ();
35- topWidget->getSearchBar ().setFocus (Qt::MouseFocusReason);
36- topWidget->getSearchBar ().clear ();
37- topWidget->getSearchBar ().clearSuggestions ();
38- topWidget->getSearchBar ().hideSuggestions ();
39- });
40- connect (app->getAction (KiwixApp::CloseTabAction), &QAction::triggered,
37+ connect (getAction (KiwixApp::NextTabAction), &QAction::triggered, this , &TabBar::moveToNextTab);
38+ connect (getAction (KiwixApp::PreviousTabAction), &QAction::triggered, this , &TabBar::moveToPreviousTab);
39+ connect (getAction (KiwixApp::CloseCurrentTabAction), &QAction::triggered,
4140 this , [=]() {
42- auto index = currentIndex ();
43- if (index < 0 )
44- return ;
45-
46- // library tab cannot be closed
47- QWidget *w = mp_stackedWidget->widget (index);
48- if (qobject_cast<ContentManagerView*>(w)) {
49- return ;
50- }
51- this ->closeTab (index);
41+ this ->closeTab (currentIndex ());
5242 });
53- connect (app-> getAction (KiwixApp::OpenHomePageAction), &QAction::triggered,
43+ connect (getAction (KiwixApp::OpenHomePageAction), &QAction::triggered,
5444 this , [=]() {
5545 auto current = this ->currentWebView ();
5646 QUITIFNULL (current);
5747 current->setUrl (" zim://" + current->zimId () + " .zim/" );
5848 });
59- connect (app->getAction (KiwixApp::SettingAction), &QAction::triggered,
60- this , [=]() {
61- SettingsView* view = KiwixApp::instance ()->getSettingsManager ()->getView ();
62- for (int i = 0 ; i < mp_stackedWidget->count (); i++) {
63- if (mp_stackedWidget->widget (i) == view) {
64- setCurrentIndex (i);
65- return ;
66- }
67- }
68- int index = currentIndex () + 1 ;
69- mp_stackedWidget->insertWidget (index, view);
70- emit tabDisplayed (TabType::SettingsTab);
71- insertTab (index,QIcon (" :/icons/settings.svg" ), gt (" settings" ));
72- QToolButton *tb = new QToolButton (this );
73- tb->setDefaultAction (KiwixApp::instance ()->getAction (KiwixApp::CloseTabAction));
74- setTabButton (index, QTabBar::RightSide, tb);
75- setCurrentIndex (index);
76- });
49+ connect (getAction (KiwixApp::SettingAction), &QAction::triggered,
50+ this , &TabBar::openOrSwitchToSettingsTab);
7751
7852 for (int i = 0 ; i <= 9 ; i++) {
7953 QAction *a = new QAction (this );
80- a->setData (QVariant::fromValue (i));
81- QKeySequence ks (Qt::ALT | (Qt::Key_0 + i));
82- a->setShortcut (ks);
54+ a->setShortcut (QKeySequence (Qt::ALT | (Qt::Key_0 + i)));
8355 addAction (a);
8456 connect (a, &QAction::triggered, this , [=](){
85- QAction *a = qobject_cast<QAction*>(QObject::sender ());
86- if (!a)
87- return ;
88-
89- bool ok;
90- int tab_n = a->data ().toInt (&ok);
91- if (tab_n==0 )
92- tab_n=10 ;
93- if (!ok)
94- return ;
95- if (tab_n >= count ())
96- return ;
97-
98- setCurrentIndex (tab_n-1 );
57+ const int tabIndex = i == 0 ? 9 : i - 1 ;
58+ if (tabIndex < realTabCount ()) {
59+ setCurrentIndex (tabIndex);
60+ }
9961 });
10062 }
10163
@@ -104,6 +66,23 @@ TabBar::TabBar(QWidget *parent) :
10466 this , SLOT (onTabMoved (int ,int )), Qt::DirectConnection);
10567}
10668
69+ void TabBar::openOrSwitchToSettingsTab ()
70+ {
71+ SettingsView* view = KiwixApp::instance ()->getSettingsManager ()->getView ();
72+ for (int i = 0 ; i < mp_stackedWidget->count (); i++) {
73+ if (mp_stackedWidget->widget (i) == view) {
74+ setCurrentIndex (i);
75+ return ;
76+ }
77+ }
78+ int index = currentIndex () + 1 ;
79+ mp_stackedWidget->insertWidget (index, view);
80+ emit tabDisplayed (TabType::SettingsTab);
81+ insertTab (index,QIcon (" :/icons/settings.svg" ), gt (" settings" ));
82+ setCloseTabButton (index);
83+ setCurrentIndex (index);
84+ }
85+
10786void TabBar::setStackedWidget (QStackedWidget *widget) {
10887 mp_stackedWidget = widget;
10988 connect (this , &QTabBar::currentChanged,
@@ -119,10 +98,10 @@ void TabBar::setContentManagerView(ContentManagerView* view)
11998 setTabButton (idx, RightSide, nullptr );
12099}
121100
122- void TabBar::setNewTabButton ()
101+ void TabBar::setNewTabButton (QAction* newTabAction )
123102{
124103 QToolButton *tb = new QToolButton ();
125- tb->setDefaultAction (KiwixApp::instance ()-> getAction (KiwixApp::NewTabAction) );
104+ tb->setDefaultAction (newTabAction );
126105 tb->setIcon (QIcon (" :/icons/new-tab-icon.svg" ));
127106 int idx = addTab (" " );
128107 setTabEnabled (idx, false );
@@ -131,12 +110,12 @@ void TabBar::setNewTabButton()
131110 setTabButton (idx, QTabBar::RightSide, Q_NULLPTR);
132111}
133112
113+ // Returns the count of real tabs with content (excluding the last pseudo-tab
114+ // that acts as a button for creating new empty tabs; BTW what is the use for
115+ // such empty tabs?)
134116int TabBar::realTabCount () const
135117{
136- // The last tab is "+" in TabBar, but that isn't a real tab which displays any content hence the real count is tab count - 1
137- if (count () < 1 )
138- return 0 ;
139- return count () - 1 ;
118+ return count () < 1 ? 0 : count () - 1 ;
140119}
141120
142121void TabBar::moveToNextTab ()
@@ -151,20 +130,32 @@ void TabBar::moveToPreviousTab()
151130 setCurrentIndex (index <= 0 ? realTabCount () - 1 : index - 1 );
152131}
153132
154- ZimView* TabBar::createNewTab ( bool setCurrent, bool adjacentToCurrentTab )
133+ void TabBar::setCloseTabButton ( int index )
155134{
156- auto tab = new ZimView (this , this );
157- int index;
158- if (adjacentToCurrentTab) {
159- index = currentIndex () + 1 ;
160- } else {
161- index = realTabCount (); // for New Tab Button
162- }
163- mp_stackedWidget->insertWidget (index, tab);
164- index = insertTab (index, " " );
135+ Q_ASSERT (index > 0 && index < realTabCount ());
136+
165137 QToolButton *tb = new QToolButton (this );
166- tb->setDefaultAction (KiwixApp::instance ()->getAction (KiwixApp::CloseTabAction));
138+ QAction *a = new QAction (QIcon (" :/icons/close.svg" ), gt (" close-tab" ), tb);
139+ a->setToolTip (getAction (KiwixApp::CloseCurrentTabAction)->toolTip ());
140+ tb->setDefaultAction (a);
167141 setTabButton (index, QTabBar::RightSide, tb);
142+ connect (tb, &QToolButton::triggered, this , [=]() {
143+ for ( int i = 0 ; i < realTabCount (); ++i ) {
144+ if ( tb == tabButton (i, QTabBar::RightSide) ) {
145+ closeTab (i);
146+ return ;
147+ }
148+ }
149+ });
150+ }
151+
152+ ZimView* TabBar::createNewTab (bool setCurrent, bool nextToCurrentTab)
153+ {
154+ auto tab = new ZimView (this , this );
155+ const int index = nextToCurrentTab ? currentIndex () + 1 : realTabCount ();
156+ mp_stackedWidget->insertWidget (index, tab);
157+ insertTab (index, " " );
158+ setCloseTabButton (index);
168159 if (setCurrent) {
169160 setCurrentIndex (index);
170161 }
@@ -265,48 +256,37 @@ void TabBar::triggerWebPageAction(QWebEnginePage::WebAction action, ZimView *wid
265256
266257void TabBar::closeTabsByZimId (const QString &id)
267258{
268- // the last tab is + button, skip it
269- for (int i = count () - 2 ; i >= 0 ; i--) {
259+ // 0th tab is always (unless this comment becomes outdated by the time you
260+ // read it) the library tab, so iteration could start from 1, however we
261+ // shouldn't try to save CPU cycles at the cost of the code breaking
262+ // should this comment indeed become outdated ;)
263+ for (int i = 0 ; i < realTabCount () ; ++i ) {
270264 auto *zv = qobject_cast<ZimView*>(mp_stackedWidget->widget (i));
271- if (!zv)
272- continue ;
273- if (zv->getWebView ()->zimId () == id) {
265+ if (zv && zv->getWebView ()->zimId () == id) {
274266 closeTab (i);
275267 }
276268 }
277269}
278270
279271void TabBar::closeTab (int index)
280272{
281- // the last tab is + button, cannot be closed
282- if (index == this ->realTabCount ())
273+ // The first and last tabs (i.e. the library tab and the + (new tab) button)
274+ // cannot be closed
275+ if (index <= 0 || index >= this ->realTabCount ())
283276 return ;
284277
285- setSelectionBehaviorOnRemove (index);
286-
287- QWidget *view = mp_stackedWidget->widget (index);
288-
289- // library tab cannot be closed
290- if (qobject_cast<ContentManagerView*>(view)) {
291- return ;
278+ if ( index == currentIndex () ) {
279+ setCurrentIndex (index + 1 == realTabCount () ? index - 1 : index + 1 );
292280 }
293281
282+ QWidget *view = mp_stackedWidget->widget (index);
294283 mp_stackedWidget->removeWidget (view);
295284 view->setParent (nullptr );
296285 removeTab (index);
297286 view->close ();
298287 view->deleteLater ();
299288}
300289
301- void TabBar::setSelectionBehaviorOnRemove (int index)
302- {
303- if (index == count () - 2 ) {
304- setCurrentIndex (index - 1 );
305- } else {
306- setCurrentIndex (index + 1 );
307- }
308- }
309-
310290void TabBar::onCurrentChanged (int index)
311291{
312292 if (index == -1 )
0 commit comments