From 871c53743fc6507909cb1d895303f8c0e6b5619e Mon Sep 17 00:00:00 2001 From: Le Tan Date: Sat, 20 May 2017 13:47:52 +0800 Subject: [PATCH] add print action item Print support of QWebEngineView is incomplete in Qt5.7. Need to figure out another way to print documents. Just hide the action Item. --- src/resources/icons/print.svg | 14 +++++++++ src/src.pro | 2 +- src/vedittab.cpp | 6 ++++ src/vedittab.h | 2 ++ src/vmainwindow.cpp | 55 ++++++++++++++++++++++++++++++----- src/vmainwindow.h | 2 ++ src/vnote.qrc | 1 + 7 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 src/resources/icons/print.svg diff --git a/src/resources/icons/print.svg b/src/resources/icons/print.svg new file mode 100644 index 00000000..ff2f1c33 --- /dev/null +++ b/src/resources/icons/print.svg @@ -0,0 +1,14 @@ + + + + + + + + + + + diff --git a/src/src.pro b/src/src.pro index 323e2bab..7c6d578c 100644 --- a/src/src.pro +++ b/src/src.pro @@ -4,7 +4,7 @@ # #------------------------------------------------- -QT += core gui webenginewidgets webchannel network svg +QT += core gui webenginewidgets webchannel network svg printsupport greaterThan(QT_MAJOR_VERSION, 4): QT += widgets diff --git a/src/vedittab.cpp b/src/vedittab.cpp index d61a7e93..ecd68664 100644 --- a/src/vedittab.cpp +++ b/src/vedittab.cpp @@ -727,3 +727,9 @@ void VEditTab::zoomWebPage(bool p_zoomIn, qreal p_step) } webPreviewer->setZoomFactor(newFactor); } + +VWebView *VEditTab::getWebViewer() const +{ + return webPreviewer; +} + diff --git a/src/vedittab.h b/src/vedittab.h index 21846be1..a1054df3 100644 --- a/src/vedittab.h +++ b/src/vedittab.h @@ -49,6 +49,8 @@ public: QString getSelectedText() const; void clearSearchedWordHighlight(); + VWebView *getWebViewer() const; + public slots: // Enter edit mode void editFile(); diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index 5d1c4581..d216ebc8 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -1,5 +1,8 @@ #include #include +#include +#include +#include #include "vmainwindow.h" #include "vdirectorytree.h" #include "vnote.h" @@ -13,6 +16,8 @@ #include "dialog/vfindreplacedialog.h" #include "dialog/vsettingsdialog.h" #include "vcaptain.h" +#include "vedittab.h" +#include "vwebview.h" extern VConfigManager vconfig; @@ -438,30 +443,42 @@ void VMainWindow::initFileMenu() // Import notes from files. m_importNoteAct = new QAction(QIcon(":/resources/icons/import_note.svg"), - tr("&Import Notes From Files"), this); + tr("&Import Notes From Files"), this); m_importNoteAct->setToolTip(tr("Import notes from files into current directory")); connect(m_importNoteAct, &QAction::triggered, this, &VMainWindow::importNoteFromFile); m_importNoteAct->setEnabled(false); + fileMenu->addAction(m_importNoteAct); + + fileMenu->addSeparator(); + + // Print. + m_printAct = new QAction(QIcon(":/resources/icons/print.svg"), + tr("&Print"), this); + m_printAct->setToolTip(tr("Print current note")); + connect(m_printAct, &QAction::triggered, + this, &VMainWindow::printNote); + m_printAct->setEnabled(false); + // Settings. QAction *settingsAct = new QAction(QIcon(":/resources/icons/settings.svg"), - tr("Settings"), this); + tr("&Settings"), this); settingsAct->setToolTip(tr("View and change settings for VNote")); connect(settingsAct, &QAction::triggered, this, &VMainWindow::viewSettings); + fileMenu->addAction(settingsAct); + + fileMenu->addSeparator(); + // Exit. - QAction *exitAct = new QAction(tr("Exit"), this); + QAction *exitAct = new QAction(tr("&Exit"), this); exitAct->setToolTip(tr("Exit VNote")); exitAct->setShortcut(QKeySequence("Ctrl+Q")); connect(exitAct, &QAction::triggered, this, &VMainWindow::close); - fileMenu->addAction(m_importNoteAct); - fileMenu->addSeparator(); - fileMenu->addAction(settingsAct); - fileMenu->addSeparator(); fileMenu->addAction(exitAct); } @@ -998,6 +1015,8 @@ void VMainWindow::setEditorStyle(QAction *p_action) void VMainWindow::updateActionStateFromTabStatusChange(const VFile *p_file, bool p_editMode) { + m_printAct->setEnabled(p_file && p_file->getDocType() == DocType::Markdown); + editNoteAct->setVisible(p_file && p_file->isModifiable() && !p_editMode); discardExitAct->setVisible(p_file && p_editMode); saveExitAct->setVisible(p_file && p_editMode); @@ -1352,3 +1371,25 @@ void VMainWindow::shortcutHelp() VFile *file = vnote->getOrphanFile(docName); editArea->openFile(file, OpenFileMode::Read); } + +void VMainWindow::printNote() +{ + QPrinter printer; + QPrintDialog dialog(&printer, this); + dialog.setWindowTitle(tr("Print Note")); + + V_ASSERT(m_curTab); + + VWebView *webView = m_curTab->getWebViewer(); + + V_ASSERT(webView); + + if (webView->hasSelection()) { + dialog.addEnabledOption(QAbstractPrintDialog::PrintSelection); + } + + if (dialog.exec() != QDialog::Accepted) { + return; + } +} + diff --git a/src/vmainwindow.h b/src/vmainwindow.h index 7b61d452..117167df 100644 --- a/src/vmainwindow.h +++ b/src/vmainwindow.h @@ -80,6 +80,7 @@ private slots: void enableImagePreviewConstraint(bool p_checked); void enableImageConstraint(bool p_checked); void enableImageCaption(bool p_checked); + void printNote(); protected: void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE; @@ -153,6 +154,7 @@ private: QAction *discardExitAct; QAction *expandViewAct; QAction *m_importNoteAct; + QAction *m_printAct; QAction *m_insertImageAct; QAction *m_findReplaceAct; diff --git a/src/vnote.qrc b/src/vnote.qrc index 4fe9f615..4fd24960 100644 --- a/src/vnote.qrc +++ b/src/vnote.qrc @@ -104,5 +104,6 @@ utils/showdown/showdown.min.js resources/showdown.js utils/showdown/showdown-headinganchor.js + resources/icons/print.svg