Skip to content

Commit 45f00a9

Browse files
authored
Merge branch 'kiwix:main' into main
2 parents 112ecb4 + 398a4fc commit 45f00a9

6 files changed

Lines changed: 112 additions & 114 deletions

File tree

src/kiwixapp.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ void KiwixApp::init()
7979
setDesktopFileName("kiwix.desktop");
8080
setStyleSheet(parseStyleFromFile(":/css/style.css"));
8181

82-
createAction();
82+
createActions();
8383
mp_mainWindow = new MainWindow;
8484
getTabWidget()->setContentManagerView(mp_manager->getView());
85-
getTabWidget()->setNewTabButton();
85+
const auto newTabAction = getAction(KiwixApp::NewTabAction);
86+
getTabWidget()->setNewTabButton(newTabAction);
87+
connect(newTabAction, &QAction::triggered, this, &KiwixApp::newTab);
8688
postInit();
8789
mp_errorDialog = new QErrorMessage(mp_mainWindow);
8890
setActivationWindow(mp_mainWindow);
@@ -128,6 +130,16 @@ KiwixApp::~KiwixApp()
128130
}
129131
}
130132

133+
void KiwixApp::newTab()
134+
{
135+
getTabWidget()->createNewTab(true, false);
136+
auto& searchBar = mp_mainWindow->getTopWidget()->getSearchBar();
137+
searchBar.setFocus(Qt::MouseFocusReason);
138+
searchBar.clear();
139+
searchBar.clearSuggestions();
140+
searchBar.hideSuggestions();
141+
}
142+
131143
QString KiwixApp::findLibraryDirectory()
132144
{
133145
// Check for library.xml in the same directory than the executable (portable kiwix-desktop)
@@ -340,7 +352,7 @@ void KiwixApp::setMonitorDir(const QString &dir) {
340352
#define HIDE_ACTION(ID) mpa_actions[ID]->setVisible(false)
341353
#define DISABLE_ACTION(ID) mpa_actions[ID]->setDisabled(true)
342354

343-
void KiwixApp::createAction()
355+
void KiwixApp::createActions()
344356
{
345357
CREATE_ACTION_ICON_SHORTCUT(KiwixServeAction, "share", gt("local-kiwix-server"), QKeySequence(Qt::CTRL | Qt::Key_I));
346358

@@ -370,8 +382,7 @@ void KiwixApp::createAction()
370382

371383
CREATE_ACTION_ICON_SHORTCUT(NewTabAction,"new-tab-icon", gt("new-tab"), QKeySequence::AddTab);
372384

373-
CREATE_ACTION_ICON_SHORTCUTS(CloseTabAction, "close", gt("close-tab"), QList<QKeySequence>({QKeySequence(Qt::CTRL | Qt::Key_F4), QKeySequence(Qt::CTRL | Qt::Key_W)}));
374-
mpa_actions[CloseTabAction]->setIconVisibleInMenu(false);
385+
CREATE_ACTION_SHORTCUTS(CloseCurrentTabAction, gt("close-tab"), QList<QKeySequence>({QKeySequence(Qt::CTRL | Qt::Key_F4), QKeySequence(Qt::CTRL | Qt::Key_W)}));
375386

376387
CREATE_ACTION_SHORTCUT(ReopenClosedTabAction, gt("reopen-closed-tab"), QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_T));
377388
HIDE_ACTION(ReopenClosedTabAction);
@@ -402,8 +413,11 @@ void KiwixApp::createAction()
402413
mpa_actions[FindInPageAction]->setShortcuts({QKeySequence::Find, Qt::Key_F3});
403414
connect(mpa_actions[FindInPageAction], &QAction::triggered,
404415
this, [=]() { getTabWidget()->openFindInPageBar(); });
405-
406-
CREATE_ACTION_ICON_SHORTCUT(ToggleFullscreenAction, "full-screen-enter", gt("set-fullscreen"), QKeySequence::FullScreen);
416+
417+
const auto fullScreenKeySeq = QKeySequence(QKeySequence::FullScreen).isEmpty()
418+
? Qt::Key_F11
419+
: QKeySequence::FullScreen;
420+
CREATE_ACTION_ICON_SHORTCUT(ToggleFullscreenAction, "full-screen-enter", gt("set-fullscreen"), fullScreenKeySeq);
407421
connect(mpa_actions[ToggleFullscreenAction], &QAction::toggled,
408422
this, [=](bool checked) {
409423
auto action = mpa_actions[ToggleFullscreenAction];

src/kiwixapp.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class KiwixApp : public QtSingleApplication
3434
OpenHomePageAction,
3535
PrintAction,
3636
NewTabAction,
37-
CloseTabAction,
37+
CloseCurrentTabAction,
3838
ReopenClosedTabAction,
3939
BrowseLibraryAction,
4040
OpenFileAction,
@@ -89,6 +89,7 @@ class KiwixApp : public QtSingleApplication
8989
QString parseStyleFromFile(QString filePath);
9090

9191
public slots:
92+
void newTab();
9293
void openZimFile(const QString& zimfile="");
9394
void openUrl(const QString& url, bool newTab=true);
9495
void openUrl(const QUrl& url, bool newTab=true);
@@ -98,7 +99,7 @@ public slots:
9899
void printVersions(std::ostream& out = std::cout);
99100

100101
protected:
101-
void createAction();
102+
void createActions();
102103
void postInit();
103104

104105
private:

src/mainmenu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ MainMenu::MainMenu(QWidget *parent) :
1919

2020
m_fileMenu.setTitle(gt("file"));
2121
m_fileMenu.ADD_ACTION(NewTabAction);
22-
m_fileMenu.ADD_ACTION(CloseTabAction);
22+
m_fileMenu.ADD_ACTION(CloseCurrentTabAction);
2323
m_fileMenu.ADD_ACTION(ReopenClosedTabAction);
2424
m_fileMenu.ADD_ACTION(BrowseLibraryAction);
2525
m_fileMenu.ADD_ACTION(OpenFileAction);

src/tabbar.cpp

Lines changed: 79 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1625
TabBar::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+
10786
void 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?)
134116
int 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

142121
void 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

266257
void 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

279271
void 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-
310290
void TabBar::onCurrentChanged(int index)
311291
{
312292
if (index == -1)

0 commit comments

Comments
 (0)