From 2ec1beb10308c3920f4f6960d0cf98248f23fabb Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 25 Jul 2018 17:30:32 +0200 Subject: [PATCH] Connect OpenFileAction. --- src/kiwixapp.cpp | 19 ++++++++++++++++--- src/kiwixapp.h | 5 ++++- src/main.cpp | 12 ++---------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/kiwixapp.cpp b/src/kiwixapp.cpp index e18a784..edf1c10 100644 --- a/src/kiwixapp.cpp +++ b/src/kiwixapp.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include KiwixApp::KiwixApp(int& argc, char *argv[]) @@ -75,11 +76,22 @@ KiwixApp *KiwixApp::instance() void KiwixApp::openZimFile(const QString &zimfile) { + QString _zimfile = zimfile; + if (_zimfile.isEmpty()) { + _zimfile = QFileDialog::getOpenFileName( + getMainWindow(), + "Open Zim", + QString(), + "ZimFile (*.zim*)"); + } + if (_zimfile.isEmpty()) { + return; + } QString zimId; try { - zimId = m_library.openBook(zimfile); + zimId = m_library.openBook(_zimfile); } catch (const std::exception& e) { - showMessage("Cannot open " + zimfile + ": \n" + e.what()); + showMessage("Cannot open " + _zimfile + ": \n" + e.what()); return; } openUrl(QUrl("zim://"+zimId+"/")); @@ -137,7 +149,8 @@ void KiwixApp::createAction() CREATE_ACTION(OpenFileAction, "Open file"); SET_SHORTCUT(OpenFileAction, QKeySequence::Open); - DISABLE_ACTION(OpenFileAction); + connect(mpa_actions[OpenFileAction], &QAction::triggered, + this, [=]() { openZimFile(); }); CREATE_ACTION(OpenRecentAction, "Open recent"); HIDE_ACTION(OpenRecentAction); diff --git a/src/kiwixapp.h b/src/kiwixapp.h index 5653ec7..a4214b9 100644 --- a/src/kiwixapp.h +++ b/src/kiwixapp.h @@ -13,6 +13,7 @@ class KiwixApp : public QApplication { + Q_OBJECT public: enum Actions { KiwixServeAction, @@ -51,7 +52,6 @@ public: virtual ~KiwixApp(); static KiwixApp* instance(); - void openZimFile(const QString& zimfile); void openUrl(const QUrl& url, bool newTab=true); void showMessage(const QString& message); @@ -63,6 +63,9 @@ public: TabWidget* getTabWidget() { return mp_tabWidget; } QAction* getAction(Actions action); +public slots: + void openZimFile(const QString& zimfile=""); + protected: void createAction(); diff --git a/src/main.cpp b/src/main.cpp index 1dc9ea5..defba0b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,6 @@ #include "kiwixapp.h" #include -#include #include int main(int argc, char *argv[]) @@ -15,16 +14,9 @@ int main(int argc, char *argv[]) parser.process(a); QString zimfile; auto positionalArguments = parser.positionalArguments(); - if (positionalArguments.size() < 1){ - zimfile = QFileDialog::getOpenFileName(a.getMainWindow(), - "Open Zim", - QString(), - "ZimFile (*.zim*)"); - } else { + if (positionalArguments.size() >= 1){ zimfile = parser.positionalArguments().at(0); } - if (zimfile.size()) { - a.openZimFile(zimfile); - } + a.openZimFile(zimfile); return a.exec(); }