From 5d0f1bd096acd88fde4f752fa2031eb3e8ec37cf Mon Sep 17 00:00:00 2001 From: Alexander Sashnov Date: Wed, 15 Dec 2021 16:50:56 +0700 Subject: [PATCH] Code clean up: illiminate KiwixApp::SideBarType property Switching sidebar between reading list and library is only MainWindow responsibility. KiwixApp and Tabbar are not involved in it anymore. --- src/kiwixapp.cpp | 51 ++++++++++------------------------------------ src/kiwixapp.h | 12 +---------- src/mainwindow.cpp | 51 +++++++++++++++++++++++++++++++++++++--------- src/mainwindow.h | 11 ++++++---- src/tabbar.cpp | 6 ------ 5 files changed, 60 insertions(+), 71 deletions(-) diff --git a/src/kiwixapp.cpp b/src/kiwixapp.cpp index b9161fa..70c7f26 100644 --- a/src/kiwixapp.cpp +++ b/src/kiwixapp.cpp @@ -84,8 +84,6 @@ void KiwixApp::init() mp_tabWidget = mp_mainWindow->getTabBar(); mp_tabWidget->setContentManagerView(mp_manager->getView()); mp_tabWidget->setNewTabButton(); - mp_mainWindow->getSideContentManager()->setContentManager(mp_manager); - setSideBar(CONTENTMANAGER_BAR); postInit(); mp_errorDialog = new QErrorMessage(mp_mainWindow); setActivationWindow(mp_mainWindow); @@ -213,35 +211,6 @@ void KiwixApp::openUrl(const QUrl &url, bool newTab) { mp_tabWidget->openUrl(url, newTab); } -void KiwixApp::setSideBar(KiwixApp::SideBarType type) -{ - auto sideDockWidget = mp_mainWindow->getSideDockWidget(); - switch(type) { - case CONTENTMANAGER_BAR: - case READINGLIST_BAR: - sideDockWidget->setCurrentIndex(type); - sideDockWidget->show(); - break; - case NONE: - sideDockWidget->hide(); - break; - } - m_currentSideType = type; - emit(currentSideTypeChanged(type)); -} - -void KiwixApp::toggleSideBar(KiwixApp::SideBarType type) { - if (m_currentSideType == type) { - setSideBar(NONE); - return; - } - - if (m_currentSideType == CONTENTMANAGER_BAR) { - return; - } - setSideBar(type); -} - void KiwixApp::openRandomUrl(bool newTab) { auto zimId = mp_tabWidget->currentZimId(); @@ -299,6 +268,14 @@ bool KiwixApp::isCurrentArticleBookmarked() #define CREATE_ACTION_ICON_SHORTCUT(ID, ICON, TEXT, SHORTCUT) \ mpa_actions[ID] = new QAction(QIcon(":/icons/" ICON ".svg"), TEXT); \ SET_SHORTCUT(ID, TEXT, SHORTCUT) +#define CREATE_ACTION_ONOFF_ICON_SHORTCUT(ID, ON_ICON, OFF_ICON, TEXT, SHORTCUT) \ + CREATE_ACTION(ID, TEXT); \ + SET_SHORTCUT(ID, TEXT, SHORTCUT); \ + mpa_actions[ID]->setCheckable(true); \ + { QIcon icon; \ + icon.addPixmap(QPixmap(":/icons/" ON_ICON ".svg"), QIcon::Normal, QIcon::On); \ + icon.addPixmap(QPixmap(":/icons/" OFF_ICON, ".svg"), QIcon::Normal, QIcon::Off); \ + mpa_actions[ID]->setIcon(icon); } #define SET_SHORTCUTS(ID, TEXT, SHORTCUTS) \ mpa_actions[ID]->setShortcuts(SHORTCUTS); \ mpa_actions[ID]->setToolTip(TEXT + " (" + SHORTCUTS.first().toString() + ")" ) @@ -384,14 +361,7 @@ void KiwixApp::createAction() CREATE_ACTION_SHORTCUT(ToggleTOCAction, gt("table-of-content"), QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_1)); HIDE_ACTION(ToggleTOCAction); - CREATE_ACTION_ICON_SHORTCUT(ToggleReadingListAction, "reading-list" ,gt("reading-list"), QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_2)); - connect(mpa_actions[ToggleReadingListAction], &QAction::triggered, - this, [=]() { toggleSideBar(READINGLIST_BAR); }); - connect(this, &KiwixApp::currentSideTypeChanged, - this, [=](SideBarType type) { - mpa_actions[ToggleReadingListAction]->setIcon( - QIcon((type == READINGLIST_BAR) ? ":/icons/reading-list-active.svg" : ":/icons/reading-list.svg")); - }); + CREATE_ACTION_ONOFF_ICON_SHORTCUT(ToggleReadingListAction, "reading-list-active", "reading-list", gt("reading-list"), QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_2)); CREATE_ACTION_SHORTCUTS(ZoomInAction, gt("zoom-in"), QList({QKeySequence::ZoomIn, QKeySequence(Qt::CTRL+Qt::Key_Equal)})); @@ -425,7 +395,8 @@ void KiwixApp::postInit() { mp_mainWindow->getTopWidget(), &TopWidget::handleWebActionEnabledChanged); connect(mp_tabWidget, &TabBar::currentTitleChanged, this, [=](const QString& title) { emit currentTitleChanged(title); }); - connect(mp_tabWidget, &TabBar::libraryPageDisplayed, this, &KiwixApp::disableItemsOnLibraryPage); + connect(mp_tabWidget, &TabBar::libraryPageDisplayed, + this, &KiwixApp::disableItemsOnLibraryPage); emit(m_library.booksChanged()); connect(&m_library, &Library::booksChanged, this, &KiwixApp::updateNameMapper); disableItemsOnLibraryPage(true); diff --git a/src/kiwixapp.h b/src/kiwixapp.h index 31d65c7..d7a727a 100644 --- a/src/kiwixapp.h +++ b/src/kiwixapp.h @@ -23,7 +23,6 @@ class KiwixApp : public QtSingleApplication { Q_OBJECT - Q_PROPERTY(SideBarType currentSideType MEMBER m_currentSideType NOTIFY currentSideTypeChanged) public: enum Actions { @@ -59,11 +58,6 @@ public: ExitAction, MAX_ACTION }; - enum SideBarType { - CONTENTMANAGER_BAR, - READINGLIST_BAR, - NONE - }; KiwixApp(int& argc, char *argv[]); virtual ~KiwixApp(); @@ -77,27 +71,24 @@ public: KProfile* getProfile() { return &m_profile; } Library* getLibrary() { return &m_library; } MainWindow* getMainWindow() { return mp_mainWindow; } + ContentManager* getContentManager() { return mp_manager; } kiwix::Downloader* getDownloader() { return mp_downloader; } TabBar* getTabWidget() { return mp_tabWidget; } QAction* getAction(Actions action); QString getLibraryDirectory() { return m_libraryDirectory; }; kiwix::Server* getLocalServer() { return &m_server; } SettingsManager* getSettingsManager() { return &m_settingsManager; }; - SideBarType getSideType() { return m_currentSideType; } QString getText(const QString &key) { return m_translation.getText(key); }; bool isCurrentArticleBookmarked(); signals: void currentTitleChanged(const QString& title); - void currentSideTypeChanged(SideBarType type); public slots: void openZimFile(const QString& zimfile=""); void openUrl(const QString& url, bool newTab=true); void openUrl(const QUrl& url, bool newTab=true); - void setSideBar(SideBarType type); - void toggleSideBar(KiwixApp::SideBarType type); void printPage(); void disableItemsOnLibraryPage(bool displayed); void updateNameMapper(); @@ -117,7 +108,6 @@ private: ContentManager* mp_manager; MainWindow* mp_mainWindow; TabBar* mp_tabWidget; - SideBarType m_currentSideType; QErrorMessage* mp_errorDialog; kiwix::UpdatableNameMapper m_nameMapper; kiwix::Server m_server; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index e94bc2b..fbf2f45 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -19,29 +19,38 @@ MainWindow::MainWindow(QWidget *parent) : { QWidget::setAttribute(Qt::WA_AlwaysShowToolTips); mp_ui->setupUi(this); + mp_ui->tabBar->setExpanding(false); mp_ui->tabBar->setStackedWidget(mp_ui->mainView); + auto app = KiwixApp::instance(); + connect(app->getAction(KiwixApp::ExitAction), &QAction::triggered, this, &QMainWindow::close); connect(app->getAction(KiwixApp::ToggleFullscreenAction), &QAction::triggered, this, &MainWindow::toggleFullScreen); + connect(app->getAction(KiwixApp::ToggleReadingListAction), &QAction::toggled, + this, &MainWindow::when_ReadingList_toggled); connect(app->getAction(KiwixApp::AboutAction), &QAction::triggered, mp_about, &QDialog::show); connect(app->getAction(KiwixApp::DonateAction), &QAction::triggered, this, [=]() { QDesktopServices::openUrl(QUrl("https://donate.kiwix.org")); }); connect(app->getAction(KiwixApp::KiwixServeAction), &QAction::triggered, mp_localKiwixServer, &QDialog::show); + connect(app, &KiwixApp::currentTitleChanged, this, [=](const QString& title) { if (!title.isEmpty() && !title.startsWith("zim://")) setWindowTitle(title + " - Kiwix"); else setWindowTitle(gt("window-title")); }); + addAction(app->getAction(KiwixApp::OpenHomePageAction)); + #if !SYSTEMTITLEBAR setWindowFlags(Qt::Window | Qt::CustomizeWindowHint); #endif + #ifdef Q_OS_WIN QWindow *window = windowHandle(); if (!window) { @@ -52,6 +61,11 @@ MainWindow::MainWindow(QWidget *parent) : connect(mp_ui->tabBar, &QTabBar::currentChanged, mp_ui->mainToolBar, &TopWidget::updateBackForwardButtons); + connect(mp_ui->tabBar, &TabBar::libraryPageDisplayed, + this, &MainWindow::when_libraryPageDisplayed); + + mp_ui->contentmanagerside->setContentManager(app->getContentManager()); + mp_ui->sideBar->setCurrentWidget(mp_ui->contentmanagerside); } MainWindow::~MainWindow() @@ -66,6 +80,33 @@ void MainWindow::toggleFullScreen() { showFullScreen(); } +void MainWindow::when_ReadingList_toggled(bool state) +{ + if (state) { + mp_ui->sideBar->setCurrentWidget(mp_ui->readinglistbar); + mp_ui->sideBar->show(); + } + else { + mp_ui->sideBar->hide(); + } +} + +void MainWindow::when_libraryPageDisplayed(bool showed) +{ + auto app = KiwixApp::instance(); + + // When library sidebar appeared, or hidden, reading list is always hidden. + app->getAction(KiwixApp::ToggleReadingListAction)->setChecked(false); + + if (showed) { + mp_ui->sideBar->setCurrentWidget(mp_ui->contentmanagerside); + mp_ui->sideBar->show(); + } + else { + mp_ui->sideBar->hide(); + } +} + void MainWindow::keyPressEvent(QKeyEvent *event) { auto key = event->key(); @@ -87,13 +128,3 @@ TopWidget *MainWindow::getTopWidget() { return mp_ui->mainToolBar; } - -QStackedWidget *MainWindow::getSideDockWidget() -{ - return mp_ui->sideBar; -} - -ContentManagerSide *MainWindow::getSideContentManager() -{ - return mp_ui->contentmanagerside; -} diff --git a/src/mainwindow.h b/src/mainwindow.h index 1296806..6c669e1 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -24,12 +24,15 @@ public: TabBar* getTabBar(); TopWidget* getTopWidget(); - QStackedWidget* getSideDockWidget(); - ContentManagerSide* getSideContentManager(); -protected slots: - void toggleFullScreen(); +protected: void keyPressEvent(QKeyEvent *event); + +private slots: + void toggleFullScreen(); + void when_ReadingList_toggled(bool state); + void when_libraryPageDisplayed(bool showed); + private: Ui::MainWindow *mp_ui; About *mp_about; diff --git a/src/tabbar.cpp b/src/tabbar.cpp index 576b389..45a5f79 100644 --- a/src/tabbar.cpp +++ b/src/tabbar.cpp @@ -64,7 +64,6 @@ TabBar::TabBar(QWidget *parent) : int index = currentIndex() + 1; mp_stackedWidget->insertWidget(index, view); insertTab(index,QIcon(":/icons/settings.svg"), gt("settings")); - KiwixApp::instance()->setSideBar(KiwixApp::SideBarType::NONE); QToolButton *tb = new QToolButton(this); tb->setDefaultAction(KiwixApp::instance()->getAction(KiwixApp::CloseTabAction)); setTabButton(index, QTabBar::RightSide, tb); @@ -296,22 +295,17 @@ void TabBar::onCurrentChanged(int index) emit webActionEnabledChanged(QWebEnginePage::Back, false); emit webActionEnabledChanged(QWebEnginePage::Forward, false); emit libraryPageDisplayed(false); - KiwixApp::instance()->setSideBar(KiwixApp::NONE); QTimer::singleShot(0, [=](){emit currentTitleChanged("");}); } else if (auto zv = qobject_cast(w)) { auto view = zv->getWebView(); emit webActionEnabledChanged(QWebEnginePage::Back, view->isWebActionEnabled(QWebEnginePage::Back)); emit webActionEnabledChanged(QWebEnginePage::Forward, view->isWebActionEnabled(QWebEnginePage::Forward)); emit libraryPageDisplayed(false); - if (KiwixApp::instance()->getSideType() == KiwixApp::CONTENTMANAGER_BAR) { - KiwixApp::instance()->setSideBar(KiwixApp::NONE); - } QTimer::singleShot(0, [=](){emit currentTitleChanged(view->title());}); } else if (qobject_cast(w)) { emit webActionEnabledChanged(QWebEnginePage::Back, false); emit webActionEnabledChanged(QWebEnginePage::Forward, false); emit libraryPageDisplayed(true); - KiwixApp::instance()->setSideBar(KiwixApp::CONTENTMANAGER_BAR); QTimer::singleShot(0, [=](){emit currentTitleChanged("");}); } else {