Connect OpenFileAction.

This commit is contained in:
Matthieu Gautier 2018-07-25 17:30:32 +02:00
parent 52f6b03da2
commit 2ec1beb103
3 changed files with 22 additions and 14 deletions

View File

@ -4,6 +4,7 @@
#include <QFontDatabase>
#include <QStyleFactory>
#include <QFile>
#include <QFileDialog>
#include <QAction>
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);

View File

@ -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();

View File

@ -1,7 +1,6 @@
#include "kiwixapp.h"
#include <QCommandLineParser>
#include <QFileDialog>
#include <iostream>
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);
}
return a.exec();
}