From d8e758317969d971df3ffffb89b072797649348f Mon Sep 17 00:00:00 2001 From: Le Tan Date: Mon, 22 Jul 2019 20:37:59 +0800 Subject: [PATCH] adapt to Qt 5.6.1 1. Remove dependency on QWebEngineView --- src/dialog/vcopytextashtmldialog.cpp | 8 +- src/dialog/vcopytextashtmldialog.h | 4 +- src/dialog/vexportdialog.cpp | 1500 -------------------------- src/dialog/vexportdialog.h | 516 --------- src/dialog/vsettingsdialog.cpp | 25 +- src/dialog/vtipsdialog.cpp | 10 +- src/dialog/vtipsdialog.h | 4 +- src/dialog/vupdater.cpp | 183 ---- src/dialog/vupdater.h | 41 - src/pegparser.cpp | 4 +- src/src.pro | 14 +- src/utils/vutils.cpp | 19 +- src/utils/vutils.h | 4 +- src/veditarea.cpp | 2 +- src/vexporter.cpp | 1053 ------------------ src/vexporter.h | 213 ---- src/vfile.cpp | 2 +- src/vfilelist.cpp | 26 +- src/vmainwindow.cpp | 96 +- src/vmainwindow.h | 11 +- src/vmathjaxpreviewhelper.cpp | 49 +- src/vmathjaxpreviewhelper.h | 8 +- src/vmathjaxwebdocument.cpp | 54 - src/vmathjaxwebdocument.h | 69 -- src/vmdeditor.cpp | 42 +- src/vmdtab.cpp | 91 +- src/vmdtab.h | 12 +- src/vnote.cpp | 2 +- src/vpreviewpage.cpp | 34 - src/vpreviewpage.h | 18 - src/vsearchconfig.h | 2 +- src/vwebview.cpp | 13 +- src/vwebview.h | 8 +- 33 files changed, 170 insertions(+), 3967 deletions(-) delete mode 100644 src/dialog/vexportdialog.cpp delete mode 100644 src/dialog/vexportdialog.h delete mode 100644 src/dialog/vupdater.cpp delete mode 100644 src/dialog/vupdater.h delete mode 100644 src/vexporter.cpp delete mode 100644 src/vexporter.h delete mode 100644 src/vmathjaxwebdocument.cpp delete mode 100644 src/vmathjaxwebdocument.h delete mode 100644 src/vpreviewpage.cpp delete mode 100644 src/vpreviewpage.h diff --git a/src/dialog/vcopytextashtmldialog.cpp b/src/dialog/vcopytextashtmldialog.cpp index a4fd62bc..4e677f12 100644 --- a/src/dialog/vcopytextashtmldialog.cpp +++ b/src/dialog/vcopytextashtmldialog.cpp @@ -1,7 +1,6 @@ #include "vcopytextashtmldialog.h" #include -#include #include #include #include @@ -31,8 +30,8 @@ void VCopyTextAsHtmlDialog::setupUI() m_textEdit->setProperty("LineEdit", true); m_htmlLabel = new QLabel(tr("HTML:")); - m_htmlViewer = VUtils::getWebEngineView(g_config->getBaseBackground()); - m_htmlViewer->setContextMenuPolicy(Qt::NoContextMenu); + m_htmlViewer = VUtils::getTextBrowser(g_config->getBaseBackground()); + // m_htmlViewer->setContextMenuPolicy(Qt::NoContextMenu); m_htmlViewer->setMinimumSize(600, 400); m_infoLabel = new QLabel(tr("Converting text to HTML ...")); @@ -66,7 +65,8 @@ void VCopyTextAsHtmlDialog::setConvertedHtml(const QUrl &p_baseUrl, const QString &p_html) { QString html = p_html; - m_htmlViewer->setHtml("" + html + "", p_baseUrl); + m_htmlViewer->setHtml("" + html + ""); + m_htmlViewer->setSource(p_baseUrl); setHtmlVisible(true); g_webUtils->alterHtmlAsTarget(p_baseUrl, html, m_copyTarget); diff --git a/src/dialog/vcopytextashtmldialog.h b/src/dialog/vcopytextashtmldialog.h index a4bad341..3f33fb6d 100644 --- a/src/dialog/vcopytextashtmldialog.h +++ b/src/dialog/vcopytextashtmldialog.h @@ -6,7 +6,7 @@ class QPlainTextEdit; -class QWebEngineView; +class QTextBrowser; class QDialogButtonBox; class VWaitingWidget; class QLabel; @@ -32,7 +32,7 @@ private: QLabel *m_htmlLabel; - QWebEngineView *m_htmlViewer; + QTextBrowser *m_htmlViewer; QLabel *m_infoLabel; diff --git a/src/dialog/vexportdialog.cpp b/src/dialog/vexportdialog.cpp deleted file mode 100644 index 9c782fd7..00000000 --- a/src/dialog/vexportdialog.cpp +++ /dev/null @@ -1,1500 +0,0 @@ -#include "vexportdialog.h" - -#include -#include -#include -#include -#include -#include -#include - -#ifndef QT_NO_PRINTER -#include -#include -#endif - -#include "utils/vutils.h" -#include "utils/vclipboardutils.h" -#include "vlineedit.h" -#include "vnotebook.h" -#include "vfile.h" -#include "vdirectory.h" -#include "vcart.h" -#include "vconfigmanager.h" -#include "vnotefile.h" -#include "vnote.h" -#include "vexporter.h" -#include "vlineedit.h" -#include "utils/vprocessutils.h" - -extern VConfigManager *g_config; - -extern VNote *g_vnote; - -QString VExportDialog::s_lastOutputFolder; - -ExportOption VExportDialog::s_opt; - -#define LOGERR(x) do { QString msg = (x); \ - VUtils::addErrMsg(p_errMsg, msg); \ - appendLogLine(msg); \ - } while (0) - -VExportDialog::VExportDialog(VNotebook *p_notebook, - VDirectory *p_directory, - VFile *p_file, - VCart *p_cart, - MarkdownConverterType p_renderer, - QWidget *p_parent) - : QDialog(p_parent), - m_notebook(p_notebook), - m_directory(p_directory), - m_file(p_file), - m_cart(p_cart), - m_pageLayout(QPageLayout(QPageSize(QPageSize::A4), - QPageLayout::Portrait, - QMarginsF(10, 16, 10, 10), - QPageLayout::Millimeter)), - m_inExport(false), - m_askedToStop(false) -{ - if (s_lastOutputFolder.isEmpty()) { - s_lastOutputFolder = g_config->getExportFolderPath(); - } - - setupUI(); - - m_exporter = new VExporter(this); - connect(m_exporter, &VExporter::outputLog, - this, [this](const QString &p_log) { - appendLogLine(p_log); - }); - - initUIFields(p_renderer); - - handleInputChanged(); -} - -void VExportDialog::setupUI() -{ - // Notes to export. - m_srcCB = VUtils::getComboBox(); - m_srcCB->setToolTip(tr("Choose notes to export")); - m_srcCB->setSizeAdjustPolicy(QComboBox::AdjustToContents); - connect(m_srcCB, SIGNAL(currentIndexChanged(int)), - this, SLOT(handleCurrentSrcChanged(int))); - - // Target format. - m_formatCB = VUtils::getComboBox(); - m_formatCB->setToolTip(tr("Choose target format to export as")); - m_formatCB->setSizeAdjustPolicy(QComboBox::AdjustToContents); - connect(m_formatCB, SIGNAL(currentIndexChanged(int)), - this, SLOT(handleCurrentFormatChanged(int))); - - // Markdown renderer. - m_rendererCB = VUtils::getComboBox(); - m_rendererCB->setToolTip(tr("Choose converter to render Markdown")); - m_rendererCB->setSizeAdjustPolicy(QComboBox::AdjustToContents); - - // Markdown rendering background. - m_renderBgCB = VUtils::getComboBox(); - m_renderBgCB->setToolTip(tr("Choose rendering background color for Markdown")); - - // Markdown rendering style. - m_renderStyleCB = VUtils::getComboBox(); - m_renderStyleCB->setToolTip(tr("Choose rendering style for Markdown")); - - // Markdown rendering code block style. - m_renderCodeBlockStyleCB = VUtils::getComboBox(); - m_renderCodeBlockStyleCB->setToolTip(tr("Choose rendering code block style for Markdown")); - - // Output directory. - m_outputEdit = new VLineEdit(s_lastOutputFolder); - connect(m_outputEdit, &QLineEdit::textChanged, - this, &VExportDialog::handleInputChanged); - QPushButton *browseBtn = new QPushButton(tr("&Browse")); - connect(browseBtn, &QPushButton::clicked, - this, &VExportDialog::handleOutputBrowseBtnClicked); - QHBoxLayout *outputLayout = new QHBoxLayout(); - outputLayout->addWidget(m_outputEdit); - outputLayout->addWidget(browseBtn); - - m_basicBox = new QGroupBox(tr("Information")); - - m_settingBox = new QGroupBox(tr("Advanced Settings")); - - m_consoleEdit = new QPlainTextEdit(); - m_consoleEdit->setReadOnly(true); - m_consoleEdit->setLineWrapMode(QPlainTextEdit::WidgetWidth); - m_consoleEdit->setProperty("LineEdit", true); - m_consoleEdit->setPlaceholderText(tr("Output logs will be shown here")); - - // Ok is the default button. - m_btnBox = new QDialogButtonBox(QDialogButtonBox::Close); - m_exportBtn = m_btnBox->addButton(tr("Export"), QDialogButtonBox::ActionRole); - m_openBtn = m_btnBox->addButton(tr("Open Directory"), QDialogButtonBox::ActionRole); - m_openBtn->setToolTip(tr("Open output directory")); - m_copyBtn = m_btnBox->addButton(tr("Copy Content"), QDialogButtonBox::ActionRole); - m_copyBtn->setToolTip(tr("Copy the content of the exported file")); - m_copyBtn->setEnabled(false); - connect(m_btnBox, &QDialogButtonBox::rejected, - this, [this]() { - if (m_inExport) { - // Just cancel the export. Do not exit. - m_askedToStop = true; - m_exporter->setAskedToStop(true); - appendLogLine(tr("Cancelling the export...")); - } else { - QDialog::reject(); - } - }); - - m_exportBtn->setProperty("SpecialBtn", true); - connect(m_exportBtn, &QPushButton::clicked, - this, &VExportDialog::startExport); - - connect(m_openBtn, &QPushButton::clicked, - this, [this]() { - QUrl url = QUrl::fromLocalFile(getOutputDirectory()); - QDesktopServices::openUrl(url); - }); - - connect(m_copyBtn, &QPushButton::clicked, - this, [this]() { - if (m_exportedFile.isEmpty()) { - return; - } - - bool ret = false; - QString content = VUtils::readFileFromDisk(m_exportedFile); - if (!content.isNull()) { - QMimeData *data = new QMimeData(); - data->setText(content); - VClipboardUtils::setMimeDataToClipboard(QApplication::clipboard(), data, QClipboard::Clipboard); - ret = true; - } - - if (ret) { - appendLogLine(tr("Copied content of file %1").arg(m_exportedFile)); - } else { - appendLogLine(tr("Fail to copy content of file %1").arg(m_exportedFile)); - } - }); - - // Progress bar. - m_proBar = new QProgressBar(); - m_proBar->setRange(0, 0); - m_proBar->hide(); - - QHBoxLayout *btnLayout = new QHBoxLayout(); - btnLayout->addWidget(m_proBar); - btnLayout->addWidget(m_btnBox); - - QFormLayout *basicLayout = new QFormLayout(); - basicLayout->addRow(tr("Notes to export:"), m_srcCB); - basicLayout->addRow(tr("Target format:"), m_formatCB); - basicLayout->addRow(tr("Markdown renderer:"), m_rendererCB); - basicLayout->addRow(tr("Markdown rendering background:"), m_renderBgCB); - basicLayout->addRow(tr("Markdown rendering style:"), m_renderStyleCB); - basicLayout->addRow(tr("Markdown rendering code block style:"), m_renderCodeBlockStyleCB); - basicLayout->addRow(tr("Output directory:"), outputLayout); - - m_basicBox->setLayout(basicLayout); - - // Settings box. - m_generalSettings = setupGeneralAdvancedSettings(); - m_htmlSettings = setupHTMLAdvancedSettings(); - m_pdfSettings = setupPDFAdvancedSettings(); - m_customSettings = setupCustomAdvancedSettings(); - - QVBoxLayout *advLayout = new QVBoxLayout(); - advLayout->addWidget(m_generalSettings); - advLayout->addWidget(m_htmlSettings); - advLayout->addWidget(m_pdfSettings); - advLayout->addWidget(m_customSettings); - - m_settingBox->setLayout(advLayout); - - QVBoxLayout *mainLayout = new QVBoxLayout(); - mainLayout->addWidget(m_basicBox); - mainLayout->addWidget(m_settingBox); - mainLayout->addWidget(m_consoleEdit); - mainLayout->addLayout(btnLayout); - - setLayout(mainLayout); - - setWindowTitle(tr("Export")); -} - -QWidget *VExportDialog::setupPDFAdvancedSettings() -{ - // Page layout settings. - m_layoutLabel = new QLabel(); - QPushButton *layoutBtn = new QPushButton(tr("Settings")); - -#ifndef QT_NO_PRINTER - connect(layoutBtn, &QPushButton::clicked, - this, &VExportDialog::handleLayoutBtnClicked); -#else - layoutBtn->hide(); -#endif - - updatePageLayoutLabel(); - - // Enable table of contents. - m_tableOfContentsCB = new QCheckBox(tr("Enable Table Of Contents")); - m_tableOfContentsCB->setToolTip(tr("Add a table of contents to the document")); - - // Use wkhtmltopdf. - m_wkhtmltopdfCB = new QCheckBox(tr("Use wkhtmltopdf")); - m_wkhtmltopdfCB->setToolTip(tr("Use wkhtmltopdf tool to generate PDF (wkhtmltopdf needed to be installed)")); - connect(m_wkhtmltopdfCB, &QCheckBox::stateChanged, - this, [this](int p_state) { - bool checked = p_state == Qt::Checked; - m_wkPathEdit->setEnabled(checked); - m_wkPathBrowseBtn->setEnabled(checked); - m_wkBackgroundCB->setEnabled(checked); - m_wkPageNumberCB->setEnabled(checked); - m_wkExtraArgsEdit->setEnabled(checked); - }); - - QPushButton *wkBtn = new QPushButton(tr("Download wkhtmltopdf")); - connect(wkBtn, &QPushButton::clicked, - this, [this]() { - QString url("https://wkhtmltopdf.org/downloads.html"); - QDesktopServices::openUrl(QUrl(url)); - }); - - // wkhtmltopdf Path. - m_wkPathEdit = new VLineEdit(); - m_wkPathEdit->setToolTip(tr("Tell VNote where to find wkhtmltopdf tool")); - m_wkPathEdit->setEnabled(false); - - m_wkPathBrowseBtn = new QPushButton(tr("&Browse")); - connect(m_wkPathBrowseBtn, &QPushButton::clicked, - this, &VExportDialog::handleWkPathBrowseBtnClicked); - m_wkPathBrowseBtn->setEnabled(false); - - m_wkTitleEdit = new VLineEdit(); - m_wkTitleEdit->setPlaceholderText(tr("Empty to use the name of the first source file")); - m_wkTitleEdit->setToolTip(tr("Title of the generated PDF file")); - m_wkTitleEdit->setEnabled(false); - - m_wkTargetFileNameEdit = new VLineEdit(); - m_wkTargetFileNameEdit->setPlaceholderText(tr("Empty to use the name of the first source file")); - m_wkTargetFileNameEdit->setToolTip(tr("Name of the generated PDF file")); - QValidator *validator = new QRegExpValidator(QRegExp(VUtils::c_fileNameRegExp), - m_wkTargetFileNameEdit); - m_wkTargetFileNameEdit->setValidator(validator); - m_wkTargetFileNameEdit->setEnabled(false); - - // wkhtmltopdf enable background. - m_wkBackgroundCB = new QCheckBox(tr("Enable background")); - m_wkBackgroundCB->setToolTip(tr("Enable background when printing")); - m_wkBackgroundCB->setEnabled(false); - - // wkhtmltopdf page number. - m_wkPageNumberCB = VUtils::getComboBox(); - m_wkPageNumberCB->setToolTip(tr("Append page number as footer")); - m_wkPageNumberCB->setEnabled(false); - - // wkhtmltopdf extra argumnets. - m_wkExtraArgsEdit = new VLineEdit(); - m_wkExtraArgsEdit->setToolTip(tr("Additional global options passed to wkhtmltopdf")); - m_wkExtraArgsEdit->setPlaceholderText(tr("Use \" to enclose options containing spaces")); - m_wkExtraArgsEdit->setEnabled(false); - - QGridLayout *advLayout = new QGridLayout(); - advLayout->addWidget(new QLabel(tr("Page layout:")), 0, 0); - advLayout->addWidget(m_layoutLabel, 0, 1); - advLayout->addWidget(layoutBtn, 0, 2); - advLayout->addWidget(m_tableOfContentsCB, 0, 4, 1, 2); - - advLayout->addWidget(m_wkhtmltopdfCB, 1, 1, 1, 2); - advLayout->addWidget(wkBtn, 1, 4, 1, 2); - - advLayout->addWidget(new QLabel(tr("wkhtmltopdf path:")), 2, 0); - advLayout->addWidget(m_wkPathEdit, 2, 1, 1, 4); - advLayout->addWidget(m_wkPathBrowseBtn, 2, 5); - - advLayout->addWidget(new QLabel(tr("Title:")), 3, 0); - advLayout->addWidget(m_wkTitleEdit, 3, 1, 1, 2); - - advLayout->addWidget(new QLabel(tr("Output file name:")), 3, 3); - advLayout->addWidget(m_wkTargetFileNameEdit, 3, 4, 1, 2); - - advLayout->addWidget(new QLabel(tr("Page number:")), 4, 0); - advLayout->addWidget(m_wkPageNumberCB, 4, 1, 1, 2); - advLayout->addWidget(m_wkBackgroundCB, 4, 4, 1, 2); - - advLayout->addWidget(new QLabel(tr("Additional options:")), 5, 0); - advLayout->addWidget(m_wkExtraArgsEdit, 5, 1, 1, 5); - - advLayout->setContentsMargins(0, 0, 0, 0); - - QWidget *wid = new QWidget(); - wid->setLayout(advLayout); - - return wid; -} - -QWidget *VExportDialog::setupHTMLAdvancedSettings() -{ - // Embed CSS styles. - m_embedStyleCB = new QCheckBox(tr("Embed CSS styles"), this); - m_embedStyleCB->setToolTip(tr("Embed CSS styles in HTML file")); - - // Embed images as data URI. - m_embedImagesCB = new QCheckBox(tr("Embed images"), this); - m_embedImagesCB->setToolTip(tr("Embed images as data URI")); - - // Complete HTML. - m_completeHTMLCB = new QCheckBox(tr("Complete page"), this); - m_completeHTMLCB->setToolTip(tr("Export the whole web page along with pictures " - "which may not keep the HTML link structure of " - "the original page")); - connect(m_completeHTMLCB, &QCheckBox::stateChanged, - this, [this](int p_state) { - bool checked = p_state == Qt::Checked; - m_embedImagesCB->setEnabled(checked); - if (!checked) { - m_embedImagesCB->setChecked(false); - } - }); - - // Mime HTML. - m_mimeHTMLCB = new QCheckBox(tr("MIME HTML"), this); - m_mimeHTMLCB->setToolTip(tr("Export as a complete web page in MIME HTML format")); - connect(m_mimeHTMLCB, &QCheckBox::stateChanged, - this, [this](int p_state) { - bool checked = p_state == Qt::Checked; - m_embedStyleCB->setEnabled(!checked); - m_completeHTMLCB->setEnabled(!checked); - }); - - // Outline panel. - m_outlinePanelCB = new QCheckBox(tr("Enable outline panel"), this); - m_outlinePanelCB->setToolTip(tr("Add an outline panel in HTML file")); - - QGridLayout *advLayout = new QGridLayout(); - advLayout->addWidget(m_embedStyleCB, 0, 1, 1, 2); - advLayout->addWidget(m_completeHTMLCB, 0, 4, 1, 2); - advLayout->addWidget(m_embedImagesCB, 1, 1, 1, 2); - advLayout->addWidget(m_mimeHTMLCB, 1, 4, 1, 2); - advLayout->addWidget(m_outlinePanelCB, 2, 1, 1, 2); - - advLayout->setContentsMargins(0, 0, 0, 0); - - QWidget *wid = new QWidget(); - wid->setLayout(advLayout); - - return wid; -} - -QWidget *VExportDialog::setupGeneralAdvancedSettings() -{ - // Include subfolders. - m_subfolderCB = new QCheckBox(tr("Process subfolders")); - m_subfolderCB->setToolTip(tr("Process subfolders recursively")); - - QFormLayout *advLayout = new QFormLayout(); - advLayout->addRow(m_subfolderCB); - - advLayout->setContentsMargins(0, 0, 0, 0); - - QWidget *wid = new QWidget(); - wid->setLayout(advLayout); - - return wid; -} - -void VExportDialog::initUIFields(MarkdownConverterType p_renderer) -{ - // Notes to export. - if (m_file) { - m_srcCB->addItem(tr("Current Note (%1)").arg(m_file->getName()), - (int)ExportSource::CurrentNote); - } - - if (m_directory) { - m_srcCB->addItem(tr("Current Folder (%1)").arg(m_directory->getName()), - (int)ExportSource::CurrentFolder); - } - - if (m_notebook) { - m_srcCB->addItem(tr("Current Notebook (%1)").arg(m_notebook->getName()), - (int)ExportSource::CurrentNotebook); - } - - if (m_cart && m_cart->count() > 0) { - m_srcCB->addItem(tr("Cart (%1)").arg(m_cart->count()), - (int)ExportSource::Cart); - } - - m_subfolderCB->setChecked(s_opt.m_processSubfolders); - - // Export format. - m_formatCB->addItem(tr("Markdown"), (int)ExportFormat::Markdown); - m_formatCB->addItem(tr("HTML"), (int)ExportFormat::HTML); - m_formatCB->addItem(tr("PDF"), (int)ExportFormat::PDF); - m_formatCB->addItem(tr("PDF (All In One)"), (int)ExportFormat::OnePDF); - m_formatCB->addItem(tr("Custom"), (int)ExportFormat::Custom); - m_formatCB->setCurrentIndex(m_formatCB->findData((int)s_opt.m_format)); - - // Markdown renderer. - m_rendererCB->addItem(tr("Hoedown"), MarkdownConverterType::Hoedown); - m_rendererCB->addItem(tr("Marked"), MarkdownConverterType::Marked); - m_rendererCB->addItem(tr("Markdown-it"), MarkdownConverterType::MarkdownIt); - m_rendererCB->addItem(tr("Showdown"), MarkdownConverterType::Showdown); - m_rendererCB->setCurrentIndex(m_rendererCB->findData(p_renderer)); - - // Markdown rendering background. - m_renderBgCB->addItem(tr("System"), "System"); - m_renderBgCB->addItem(tr("Transparent"), "Transparent"); - const QVector &bgColors = g_config->getCustomColors(); - for (int i = 0; i < bgColors.size(); ++i) { - m_renderBgCB->addItem(bgColors[i].m_name, bgColors[i].m_name); - } - - if (s_opt.m_renderBg.isEmpty()) { - s_opt.m_renderBg = g_config->getCurRenderBackgroundColor(); - } - - m_renderBgCB->setCurrentIndex(m_renderBgCB->findData(s_opt.m_renderBg)); - - // Markdown rendering style. - QList styles = g_config->getCssStyles(); - for (auto const &style : styles) { - m_renderStyleCB->addItem(style, style); - } - - m_renderStyleCB->setCurrentIndex( - m_renderStyleCB->findData(g_config->getCssStyle())); - - // Markdown rendering code block style. - QList cbStyles = g_config->getCodeBlockCssStyles(); - for (auto const &style : cbStyles) { - m_renderCodeBlockStyleCB->addItem(style, style); - } - - m_renderCodeBlockStyleCB->setCurrentIndex( - m_renderCodeBlockStyleCB->findData(g_config->getCodeBlockCssStyle())); - - m_embedStyleCB->setChecked(s_opt.m_htmlOpt.m_embedCssStyle); - - m_completeHTMLCB->setChecked(s_opt.m_htmlOpt.m_completeHTML); - - m_embedImagesCB->setChecked(s_opt.m_htmlOpt.m_embedImages); - - m_mimeHTMLCB->setChecked(s_opt.m_htmlOpt.m_mimeHTML); - - m_outlinePanelCB->setChecked(s_opt.m_htmlOpt.m_outlinePanel); - - m_tableOfContentsCB->setChecked(s_opt.m_pdfOpt.m_enableTableOfContents); - - m_wkhtmltopdfCB->setChecked(s_opt.m_pdfOpt.m_wkhtmltopdf); - - // wkhtmltopdf path. - m_wkPathEdit->setText(g_config->getWkhtmltopdfPath()); - - m_wkBackgroundCB->setChecked(s_opt.m_pdfOpt.m_wkEnableBackground); - - // wkhtmltopdf page number. - m_wkPageNumberCB->addItem(tr("None"), (int)ExportPageNumber::None); - m_wkPageNumberCB->addItem(tr("Left"), (int)ExportPageNumber::Left); - m_wkPageNumberCB->addItem(tr("Center"), (int)ExportPageNumber::Center); - m_wkPageNumberCB->addItem(tr("Right"), (int)ExportPageNumber::Right); - m_wkPageNumberCB->setCurrentIndex(m_wkPageNumberCB->findData((int)s_opt.m_pdfOpt.m_wkPageNumber)); - - m_wkExtraArgsEdit->setText(g_config->getWkhtmltopdfArgs()); - - // Custom export. - // Read from config every time. - ExportCustomOption customOpt(g_config->getCustomExport()); - - m_customSrcFormatCB->addItem(tr("Markdown"), (int)ExportCustomOption::SourceFormat::Markdown); - m_customSrcFormatCB->addItem(tr("HTML"), (int)ExportCustomOption::SourceFormat::HTML); - m_customSrcFormatCB->setCurrentIndex(m_customSrcFormatCB->findData((int)customOpt.m_srcFormat)); - - m_customSuffixEdit->setText(customOpt.m_outputSuffix); - - m_customCmdEdit->setPlainText(customOpt.m_cmd); - - m_customAllInOneCB->setChecked(s_opt.m_customOpt.m_allInOne); - - m_customPdfLikeCB->setChecked(s_opt.m_customOpt.m_pdfLike); - - m_customFolderSepEdit->setText(s_opt.m_customOpt.m_folderSep); -} - -bool VExportDialog::checkWkhtmltopdfExecutable(const QString &p_file) -{ - QStringList args; - args << "--version"; - QByteArray out, err; - int exitCode = -1; - int ret = VProcessUtils::startProcess(p_file, args, exitCode, out, err); - switch (ret) { - case -2: - appendLogLine(tr("Fail to start wkhtmltopdf (%1).").arg(p_file)); - break; - - case -1: - appendLogLine(tr("wkhtmltopdf crashed (%1).").arg(p_file)); - break; - - case 0: - appendLogLine(tr("Use %1 (%2).").arg(p_file).arg(exitCode)); - break; - - default: - Q_ASSERT(false); - break; - } - - return ret == 0; -} - -void VExportDialog::startExport() -{ - if (m_inExport) { - return; - } - - m_exportBtn->setEnabled(false); - m_proBar->show(); - m_askedToStop = false; - m_exporter->setAskedToStop(false); - m_inExport = true; - m_exportedFile.clear(); - m_copyBtn->setEnabled(false); - - QString outputFolder = QDir::cleanPath(QDir(getOutputDirectory()).absolutePath()); - - QString renderStyle = m_renderStyleCB->currentData().toString(); - QString renderCodeBlockStyle = m_renderCodeBlockStyleCB->currentData().toString(); - - s_opt = ExportOption(currentSource(), - currentFormat(), - (MarkdownConverterType)m_rendererCB->currentData().toInt(), - m_renderBgCB->currentData().toString(), - renderStyle, - renderCodeBlockStyle, - m_subfolderCB->isChecked(), - ExportPDFOption(&m_pageLayout, - m_wkhtmltopdfCB->isChecked(), - QDir::toNativeSeparators(m_wkPathEdit->text()), - m_wkBackgroundCB->isChecked(), - m_tableOfContentsCB->isChecked(), - m_wkTitleEdit->text(), - m_wkTargetFileNameEdit->text(), - (ExportPageNumber) - m_wkPageNumberCB->currentData().toInt(), - m_wkExtraArgsEdit->text()), - ExportHTMLOption(m_embedStyleCB->isChecked(), - m_completeHTMLCB->isChecked(), - m_embedImagesCB->isChecked(), - m_mimeHTMLCB->isChecked(), - m_outlinePanelCB->isChecked()), - ExportCustomOption((ExportCustomOption::SourceFormat) - m_customSrcFormatCB->currentData().toInt(), - m_customSuffixEdit->text(), - m_customCmdEdit->toPlainText(), - g_config->getCssStyleUrl(renderStyle), - g_config->getCodeBlockCssStyleUrl(renderCodeBlockStyle), - m_customAllInOneCB->isChecked(), - m_customPdfLikeCB->isChecked(), - m_customFolderSepEdit->text(), - m_customTargetFileNameEdit->text())); - - m_consoleEdit->clear(); - appendLogLine(tr("Export to %1.").arg(outputFolder)); - - if (s_opt.m_format == ExportFormat::PDF - || s_opt.m_format == ExportFormat::OnePDF - || s_opt.m_format == ExportFormat::HTML) { - if (s_opt.m_format != ExportFormat::OnePDF) { - s_opt.m_pdfOpt.m_wkTitle.clear(); - } - - if ((s_opt.m_format == ExportFormat::PDF - && s_opt.m_pdfOpt.m_wkhtmltopdf) - || s_opt.m_format == ExportFormat::OnePDF) { - g_config->setWkhtmltopdfPath(s_opt.m_pdfOpt.m_wkPath); - g_config->setWkhtmltopdfArgs(s_opt.m_pdfOpt.m_wkExtraArgs); - - if (!checkWkhtmltopdfExecutable(s_opt.m_pdfOpt.m_wkPath)) { - m_inExport = false; - m_exportBtn->setEnabled(true); - m_proBar->hide(); - return; - } - } - - m_exporter->prepareExport(s_opt); - } else if (s_opt.m_format == ExportFormat::Custom) { - const ExportCustomOption &opt = s_opt.m_customOpt; - if (opt.m_srcFormat == ExportCustomOption::HTML) { - m_exporter->prepareExport(s_opt); - } - - // Save it to config. - g_config->setCustomExport(opt.toConfig()); - - if (opt.m_outputSuffix.isEmpty() - || opt.m_cmd.isEmpty() - || (opt.m_allInOne && opt.m_folderSep.isEmpty())) { - appendLogLine(tr("Invalid configurations for custom export.")); - m_inExport = false; - m_exportBtn->setEnabled(true); - m_proBar->hide(); - return; - } - } - - int ret = 0; - QString msg; - - if (s_opt.m_format == ExportFormat::OnePDF) { - QList files; - // Output HTMLs to a tmp folder. - QTemporaryDir tmpDir; - if (!tmpDir.isValid()) { - goto exit; - } - - ret = outputAsHTML(tmpDir.path(), &msg, &files); - if (m_askedToStop) { - ret = 0; - goto exit; - } - - Q_ASSERT(ret == files.size()); - if (!files.isEmpty()) { - ret = doExportPDFAllInOne(files, s_opt, outputFolder, &msg); - } - } else if (s_opt.m_format == ExportFormat::Custom - && s_opt.m_customOpt.m_allInOne) { - QList files; - QTemporaryDir tmpDir; - if (!tmpDir.isValid()) { - goto exit; - } - - if (s_opt.m_customOpt.m_srcFormat == ExportCustomOption::HTML) { - // Output HTMLs to a tmp folder. - ret = outputAsHTML(tmpDir.path(), &msg, &files); - if (m_askedToStop) { - ret = 0; - goto exit; - } - - Q_ASSERT(ret == files.size()); - } else { - // Collect all markdown files. - files = collectFiles(&msg); - } - - if (!files.isEmpty()) { - ret = doExportCustomAllInOne(files, s_opt, outputFolder, &msg); - } - } else { - switch (s_opt.m_source) { - case ExportSource::CurrentNote: - { - QStringList files; - ret = doExport(m_file, s_opt, outputFolder, &msg, &files); - if (ret == 1 && s_opt.m_format == ExportFormat::HTML) { - Q_ASSERT(files.size() == 1); - m_exportedFile = files.first(); - } - - break; - } - - case ExportSource::CurrentFolder: - ret = doExport(m_directory, s_opt, outputFolder, &msg); - break; - - case ExportSource::CurrentNotebook: - ret = doExport(m_notebook, s_opt, outputFolder, &msg); - break; - - case ExportSource::Cart: - ret = doExport(m_cart, s_opt, outputFolder, &msg); - break; - - default: - break; - } - } - -exit: - - if (m_askedToStop) { - appendLogLine(tr("User cancelled the export. Aborted!")); - m_askedToStop = false; - m_exporter->setAskedToStop(false); - } - - if (!msg.isEmpty()) { - VUtils::showMessage(QMessageBox::Warning, - tr("Warning"), - tr("Errors found during export."), - msg, - QMessageBox::Ok, - QMessageBox::Ok, - this); - } - - appendLogLine(tr("%1 notes exported.").arg(ret)); - - m_inExport = false; - m_exportBtn->setEnabled(true); - m_proBar->hide(); - - m_copyBtn->setEnabled(!m_exportedFile.isEmpty()); -} - -QString VExportDialog::getOutputDirectory() const -{ - return m_outputEdit->text(); -} - -void VExportDialog::handleOutputBrowseBtnClicked() -{ - QString initPath = getOutputDirectory(); - if (!QFileInfo::exists(initPath)) { - initPath = g_config->getDocumentPathOrHomePath(); - } - - QString dirPath = QFileDialog::getExistingDirectory(this, - tr("Select Output Directory To Export To"), - initPath, - QFileDialog::ShowDirsOnly - | QFileDialog::DontResolveSymlinks); - - if (!dirPath.isEmpty()) { - m_outputEdit->setText(dirPath); - s_lastOutputFolder = dirPath; - } -} - -void VExportDialog::handleWkPathBrowseBtnClicked() -{ - QString initPath = m_wkPathEdit->text(); - if (!QFileInfo::exists(initPath)) { - initPath = QCoreApplication::applicationDirPath(); - } - -#if defined(Q_OS_WIN) - QString filter = tr("Executable (*.exe)"); -#else - QString filter; -#endif - - QString filePath = QFileDialog::getOpenFileName(this, - tr("Select wkhtmltopdf Executable"), - initPath, - filter); - - if (!filePath.isEmpty()) { - m_wkPathEdit->setText(filePath); - } -} - -void VExportDialog::handleInputChanged() -{ - // Source. - bool sourceOk = true; - if (m_srcCB->count() == 0) { - sourceOk = false; - } - - // Output folder. - bool pathOk = true; - QString path = getOutputDirectory(); - if (path.isEmpty() || !VUtils::checkPathLegal(path)) { - pathOk = false; - } - - m_exportBtn->setEnabled(sourceOk && pathOk); - m_openBtn->setEnabled(pathOk); -} - -void VExportDialog::appendLogLine(const QString &p_text) -{ - m_consoleEdit->appendPlainText(">>> " + p_text); - m_consoleEdit->ensureCursorVisible(); - QCoreApplication::sendPostedEvents(); -} - -int VExportDialog::doExport(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg, - QList *p_outputFiles) -{ - Q_ASSERT(p_file); - - appendLogLine(tr("Exporting note %1.").arg(p_file->fetchPath())); - - int ret = 0; - switch (p_opt.m_format) { - case ExportFormat::Markdown: - ret = doExportMarkdown(p_file, p_opt, p_outputFolder, p_errMsg, p_outputFiles); - break; - - case ExportFormat::PDF: - V_FALLTHROUGH; - case ExportFormat::OnePDF: - ret = doExportPDF(p_file, p_opt, p_outputFolder, p_errMsg, p_outputFiles); - break; - - case ExportFormat::HTML: - ret = doExportHTML(p_file, p_opt, p_outputFolder, p_errMsg, p_outputFiles); - break; - - case ExportFormat::Custom: - ret = doExportCustom(p_file, p_opt, p_outputFolder, p_errMsg, p_outputFiles); - break; - - default: - break; - } - - return ret; -} - -int VExportDialog::doExport(VDirectory *p_directory, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg, - QList *p_outputFiles) -{ - Q_ASSERT(p_directory); - - bool opened = p_directory->isOpened(); - if (!opened && !p_directory->open()) { - LOGERR(tr("Fail to open folder %1.").arg(p_directory->fetchRelativePath())); - return 0; - } - - int ret = 0; - - QString folderName = VUtils::getDirNameWithSequence(p_outputFolder, - p_directory->getName()); - QString outputPath = QDir(p_outputFolder).filePath(folderName); - if (!VUtils::makePath(outputPath)) { - LOGERR(tr("Fail to create directory %1.").arg(outputPath)); - goto exit; - } - - // Export child notes. - for (auto const & file : p_directory->getFiles()) { - if (!checkUserAction()) { - goto exit; - } - - ret += doExport(file, p_opt, outputPath, p_errMsg, p_outputFiles); - } - - // Export subfolders. - if (p_opt.m_processSubfolders) { - for (auto const & dir : p_directory->getSubDirs()) { - if (!checkUserAction()) { - goto exit; - } - - ret += doExport(dir, p_opt, outputPath, p_errMsg, p_outputFiles); - } - } - -exit: - if (!opened) { - p_directory->close(); - } - - return ret; -} - -int VExportDialog::doExport(VNotebook *p_notebook, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg, - QList *p_outputFiles) -{ - Q_ASSERT(p_notebook); - - bool opened = p_notebook->isOpened(); - if (!opened && !p_notebook->open()) { - LOGERR(tr("Fail to open notebook %1.").arg(p_notebook->getName())); - return 0; - } - - int ret = 0; - - QString folderName = VUtils::getDirNameWithSequence(p_outputFolder, - p_notebook->getName()); - QString outputPath = QDir(p_outputFolder).filePath(folderName); - if (!VUtils::makePath(outputPath)) { - LOGERR(tr("Fail to create directory %1.").arg(outputPath)); - goto exit; - } - - // Export subfolder. - for (auto const & dir : p_notebook->getRootDir()->getSubDirs()) { - if (!checkUserAction()) { - goto exit; - } - - ret += doExport(dir, p_opt, outputPath, p_errMsg, p_outputFiles); - } - -exit: - if (!opened) { - p_notebook->close(); - } - - return ret; -} - -int VExportDialog::doExport(VCart *p_cart, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg, - QList *p_outputFiles) -{ - Q_UNUSED(p_cart); - Q_ASSERT(p_cart); - int ret = 0; - - QVector files = m_cart->getFiles(); - for (auto const & it : files) { - VFile *file = g_vnote->getFile(it); - if (!file) { - LOGERR(tr("Fail to open file %1.").arg(it)); - continue; - } - - ret += doExport(file, p_opt, p_outputFolder, p_errMsg, p_outputFiles); - } - - return ret; -} - -int VExportDialog::doExportMarkdown(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg, - QList *p_outputFiles) -{ - Q_UNUSED(p_opt); - - QString srcFilePath(p_file->fetchPath()); - - if (p_file->getDocType() != DocType::Markdown) { - LOGERR(tr("Skip exporting non-Markdown file %1 as Markdown.").arg(srcFilePath)); - return 0; - } - - // Export it to a folder with the same name. - QString name = VUtils::getDirNameWithSequence(p_outputFolder, p_file->getName()); - QString outputPath = QDir(p_outputFolder).filePath(name); - if (!VUtils::makePath(outputPath)) { - LOGERR(tr("Fail to create directory %1.").arg(outputPath)); - return 0; - } - - // Copy the note file. - QString destPath = QDir(outputPath).filePath(p_file->getName()); - if (!VUtils::copyFile(srcFilePath, destPath, false)) { - LOGERR(tr("Fail to copy the note file %1.").arg(srcFilePath)); - return 0; - } - - // Copy images. - int ret = 1; - int nrImageCopied = 0; - QVector images = VUtils::fetchImagesFromMarkdownFile(p_file, - ImageLink::LocalRelativeInternal); - if (!VNoteFile::copyInternalImages(images, - outputPath, - false, - &nrImageCopied, - p_errMsg)) { - ret = 0; - appendLogLine(tr("Fail to copy images of note %1.").arg(srcFilePath)); - } - - // Copy attachments. - if (p_file->getType() == FileType::Note) { - VNoteFile *noteFile = static_cast(p_file); - QString attaFolder = noteFile->getAttachmentFolder(); - if (!attaFolder.isEmpty()) { - QString attaFolderPath; - attaFolderPath = noteFile->fetchAttachmentFolderPath(); - QString relativePath = QDir(noteFile->fetchBasePath()).relativeFilePath(attaFolderPath); - QString folderPath = QDir(outputPath).filePath(relativePath); - - // Copy attaFolder to folderPath. - if (!VUtils::copyDirectory(attaFolderPath, folderPath, false)) { - LOGERR(tr("Fail to copy attachments folder %1 to %2.") - .arg(attaFolderPath).arg(folderPath)); - ret = 0; - } - } - } - - if (ret) { - if (p_outputFiles) { - p_outputFiles->append(destPath); - } - - appendLogLine(tr("Note %1 exported to %2.").arg(srcFilePath).arg(outputPath)); - } - - return ret; -} - -int VExportDialog::doExportPDF(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg, - QList *p_outputFiles) -{ - Q_UNUSED(p_opt); - - QString srcFilePath(p_file->fetchPath()); - - if (p_file->getDocType() != DocType::Markdown) { - LOGERR(tr("Skip exporting non-Markdown file %1 as PDF.").arg(srcFilePath)); - return 0; - } - - if (!VUtils::makePath(p_outputFolder)) { - LOGERR(tr("Fail to create directory %1.").arg(p_outputFolder)); - return 0; - } - - // Get output file. - QString suffix = ".pdf"; - QString name = VUtils::getFileNameWithSequence(p_outputFolder, - QFileInfo(p_file->getName()).completeBaseName() + suffix); - QString outputPath = QDir(p_outputFolder).filePath(name); - - if (m_exporter->exportPDF(p_file, p_opt, outputPath, p_errMsg)) { - if (p_outputFiles) { - p_outputFiles->append(outputPath); - } - - appendLogLine(tr("Note %1 exported to %2.").arg(srcFilePath).arg(outputPath)); - return 1; - } else { - appendLogLine(tr("Fail to export note %1.").arg(srcFilePath)); - return 0; - } -} - -int VExportDialog::doExportHTML(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg, - QList *p_outputFiles) -{ - Q_UNUSED(p_opt); - - QString srcFilePath(p_file->fetchPath()); - - if (p_file->getDocType() != DocType::Markdown) { - LOGERR(tr("Skip exporting non-Markdown file %1 as HTML.").arg(srcFilePath)); - return 0; - } - - if (!VUtils::makePath(p_outputFolder)) { - LOGERR(tr("Fail to create directory %1.").arg(p_outputFolder)); - return 0; - } - - // Get output file. - QString suffix = p_opt.m_htmlOpt.m_mimeHTML ? ".mht" : ".html"; - QString name = VUtils::getFileNameWithSequence(p_outputFolder, - QFileInfo(p_file->getName()).completeBaseName() + suffix); - QString outputPath = QDir(p_outputFolder).filePath(name); - - if (m_exporter->exportHTML(p_file, p_opt, outputPath, p_errMsg)) { - if (p_outputFiles) { - p_outputFiles->append(outputPath); - } - - appendLogLine(tr("Note %1 exported to %2.").arg(srcFilePath).arg(outputPath)); - return 1; - } else { - appendLogLine(tr("Fail to export note %1.").arg(srcFilePath)); - return 0; - } -} - -int VExportDialog::doExportCustom(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg, - QList *p_outputFiles) -{ - Q_UNUSED(p_opt); - - QString srcFilePath(p_file->fetchPath()); - - if (p_file->getDocType() != DocType::Markdown) { - LOGERR(tr("Skip exporting non-Markdown file %1.").arg(srcFilePath)); - return 0; - } - - if (!VUtils::makePath(p_outputFolder)) { - LOGERR(tr("Fail to create directory %1.").arg(p_outputFolder)); - return 0; - } - - // Get output file. - QString suffix = "." + p_opt.m_customOpt.m_outputSuffix; - QString name = VUtils::getFileNameWithSequence(p_outputFolder, - QFileInfo(p_file->getName()).completeBaseName() + suffix); - QString outputPath = QDir(p_outputFolder).filePath(name); - - if (m_exporter->exportCustom(p_file, p_opt, outputPath, p_errMsg)) { - if (p_outputFiles) { - p_outputFiles->append(outputPath); - } - - appendLogLine(tr("Note %1 exported to %2.").arg(srcFilePath).arg(outputPath)); - return 1; - } else { - appendLogLine(tr("Fail to export note %1.").arg(srcFilePath)); - return 0; - } -} - -bool VExportDialog::checkUserAction() -{ - if (m_askedToStop) { - return false; - } - - QCoreApplication::processEvents(); - - return true; -} - -void VExportDialog::handleLayoutBtnClicked() -{ -#ifndef QT_NO_PRINTER - QPrinter printer; - printer.setPageLayout(m_pageLayout); - - QPageSetupDialog dlg(&printer, this); - if (dlg.exec() != QDialog::Accepted) { - return; - } - - m_pageLayout.setUnits(QPageLayout::Millimeter); - m_pageLayout.setPageSize(printer.pageLayout().pageSize()); - m_pageLayout.setMargins(printer.pageLayout().margins(QPageLayout::Millimeter)); - m_pageLayout.setOrientation(printer.pageLayout().orientation()); - - updatePageLayoutLabel(); -#endif -} - -void VExportDialog::updatePageLayoutLabel() -{ - m_layoutLabel->setText(QString("%1, %2").arg(m_pageLayout.pageSize().name()) - .arg(m_pageLayout.orientation() == QPageLayout::Portrait ? - tr("Portrait") : tr("Landscape"))); -} - -void VExportDialog::handleCurrentFormatChanged(int p_index) -{ - bool pdfEnabled = false; - bool htmlEnabled = false; - bool pdfTitleNameEnabled = false; - bool customEnabled = false; - - if (p_index >= 0) { - switch (currentFormat()) { - case ExportFormat::PDF: - pdfEnabled = true; - m_wkhtmltopdfCB->setEnabled(true); - break; - - case ExportFormat::HTML: - htmlEnabled = true; - break; - - case ExportFormat::OnePDF: - pdfEnabled = true; - pdfTitleNameEnabled = true; - m_wkhtmltopdfCB->setChecked(true); - m_wkhtmltopdfCB->setEnabled(false); - break; - - case ExportFormat::Custom: - customEnabled = true; - break; - - default: - break; - } - } - - m_pdfSettings->setVisible(pdfEnabled); - m_htmlSettings->setVisible(htmlEnabled); - m_customSettings->setVisible(customEnabled); - - m_wkTitleEdit->setEnabled(pdfTitleNameEnabled); - m_wkTargetFileNameEdit->setEnabled(pdfTitleNameEnabled); - - QTimer::singleShot(100, [this]() { - resize(size().width(), minimumSizeHint().height()); - }); -} - -void VExportDialog::handleCurrentSrcChanged(int p_index) -{ - bool subfolderEnabled = false; - - if (p_index >= 0) { - switch (currentSource()) { - case ExportSource::CurrentFolder: - subfolderEnabled = true; - break; - - default: - break; - } - } - - m_subfolderCB->setVisible(subfolderEnabled); -} - -int VExportDialog::doExportPDFAllInOne(const QList &p_files, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg) -{ - if (p_files.isEmpty()) { - return 0; - } - - if (!VUtils::makePath(p_outputFolder)) { - LOGERR(tr("Fail to create directory %1.").arg(p_outputFolder)); - return 0; - } - - // Get output file. - const QString suffix = ".pdf"; - QString name = p_opt.m_pdfOpt.m_wkTargetFileName; - if (name.isEmpty()) { - name = VUtils::getFileNameWithSequence(p_outputFolder, - QFileInfo(p_files.first()).completeBaseName() + suffix); - } else if (!name.endsWith(suffix)) { - name += suffix; - } - - QString outputPath = QDir(p_outputFolder).filePath(name); - - int ret = m_exporter->exportPDFInOne(p_files, p_opt, outputPath, p_errMsg); - if (ret > 0) { - appendLogLine(tr("%1 notes exported to %2.").arg(ret).arg(outputPath)); - } else { - appendLogLine(tr("Fail to export %1 notes in one PDF.").arg(p_files.size())); - } - - return ret; -} - -int VExportDialog::doExportCustomAllInOne(const QList &p_files, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg) -{ - if (p_files.isEmpty()) { - return 0; - } - - if (!VUtils::makePath(p_outputFolder)) { - LOGERR(tr("Fail to create directory %1.").arg(p_outputFolder)); - return 0; - } - - // Get output file. - QString suffix = "." + p_opt.m_customOpt.m_outputSuffix; - QString name = p_opt.m_customOpt.m_targetFileName; - if (name.isEmpty()) { - name = VUtils::getFileNameWithSequence(p_outputFolder, - QFileInfo(p_files.first()).completeBaseName() + suffix); - } else if (!name.endsWith(suffix)) { - name += suffix; - } - - QString outputPath = QDir(p_outputFolder).filePath(name); - - int ret = m_exporter->exportCustomInOne(p_files, p_opt, outputPath, p_errMsg); - if (ret > 0) { - appendLogLine(tr("%1 notes exported to %2.").arg(ret).arg(outputPath)); - } else { - appendLogLine(tr("Fail to export %1 notes in one.").arg(p_files.size())); - } - - return ret; -} - -QWidget *VExportDialog::setupCustomAdvancedSettings() -{ - // Source format. - m_customSrcFormatCB = VUtils::getComboBox(this); - m_customSrcFormatCB->setToolTip(tr("Choose format of the input")); - - // Output suffix. - m_customSuffixEdit = new VLineEdit(this); - m_customSuffixEdit->setPlaceholderText(tr("Without the preceding dot")); - m_customSuffixEdit->setToolTip(tr("Suffix of the output file without the preceding dot")); - QValidator *validator = new QRegExpValidator(QRegExp(VUtils::c_fileNameRegExp), - m_customSuffixEdit); - m_customSuffixEdit->setValidator(validator); - - QLabel *tipsLabel = new QLabel(tr("%0 for the input file; " - "%1 for the output file; " - "%2 for the rendering CSS style file; " - "%3 for the input file directory; " - "%4 for the rendering code block CSS style file."), - this); - tipsLabel->setWordWrap(true); - - // Enable All In One. - m_customAllInOneCB = new QCheckBox(tr("Enable All In One"), this); - m_customAllInOneCB->setToolTip(tr("Pass a list of input files to the custom command")); - connect(m_customAllInOneCB, &QCheckBox::stateChanged, - this, [this](int p_state) { - bool checked = p_state == Qt::Checked; - m_customFolderSepEdit->setEnabled(checked); - m_customTargetFileNameEdit->setEnabled(checked); - }); - - // Enable PDF-like. - m_customPdfLikeCB = new QCheckBox(tr("PDF-Like"), this); - m_customPdfLikeCB->setToolTip(tr("Treat the exported file as PDF, such as wrapping line")); - - // Input directory separator. - m_customFolderSepEdit = new VLineEdit(this); - m_customFolderSepEdit->setPlaceholderText(tr("Separator to concatenate input files directories")); - m_customFolderSepEdit->setToolTip(tr("Separator to concatenate input files directories")); - m_customFolderSepEdit->setEnabled(false); - - // Target file name for all in one. - m_customTargetFileNameEdit = new VLineEdit(this); - m_customTargetFileNameEdit->setPlaceholderText(tr("Empty to use the name of the first source file")); - m_customTargetFileNameEdit->setToolTip(tr("Name of the generated All-In-One file")); - validator = new QRegExpValidator(QRegExp(VUtils::c_fileNameRegExp), - m_customTargetFileNameEdit); - m_customTargetFileNameEdit->setValidator(validator); - m_customTargetFileNameEdit->setEnabled(false); - - // Cmd edit. - m_customCmdEdit = new QPlainTextEdit(this); - m_customCmdEdit->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); - QString cmdExamp("pandoc --resource-path=.:\"%3\" --css=\"%2\" --css=\"%4\" -s -o \"%1\" \"%0\""); - m_customCmdEdit->setPlaceholderText(cmdExamp); - m_customCmdEdit->setToolTip(tr("Custom command to be executed")); - m_customCmdEdit->setProperty("LineEdit", true); - - QGridLayout *advLayout = new QGridLayout(); - advLayout->addWidget(new QLabel(tr("Source format:")), 0, 0); - advLayout->addWidget(m_customSrcFormatCB, 0, 1, 1, 2); - - advLayout->addWidget(new QLabel(tr("Output suffix:")), 0, 3); - advLayout->addWidget(m_customSuffixEdit, 0, 4, 1, 2); - - advLayout->addWidget(m_customAllInOneCB, 1, 1, 1, 2); - advLayout->addWidget(m_customPdfLikeCB, 1, 4, 1, 2); - - advLayout->addWidget(new QLabel(tr("Output file name:")), 2, 0); - advLayout->addWidget(m_customTargetFileNameEdit, 2, 1, 1, 2); - - advLayout->addWidget(new QLabel(tr("Input directories separator:")), 2, 3); - advLayout->addWidget(m_customFolderSepEdit, 2, 4, 1, 2); - - advLayout->addWidget(tipsLabel, 3, 0, 1, 6); - - advLayout->addWidget(m_customCmdEdit, 4, 0, 1, 6); - - advLayout->setContentsMargins(0, 0, 0, 0); - - QWidget *wid = new QWidget(); - wid->setLayout(advLayout); - - m_customCmdEdit->setMaximumHeight(m_customSrcFormatCB->height() * 3); - - return wid; -} - -int VExportDialog::outputAsHTML(const QString &p_outputFolder, - QString *p_errMsg, - QList *p_outputFiles) -{ - int ret = 0; - ExportFormat fmt = s_opt.m_format; - s_opt.m_format = ExportFormat::HTML; - switch (s_opt.m_source) { - case ExportSource::CurrentNote: - ret = doExport(m_file, s_opt, p_outputFolder, p_errMsg, p_outputFiles); - break; - - case ExportSource::CurrentFolder: - ret = doExport(m_directory, s_opt, p_outputFolder, p_errMsg, p_outputFiles); - break; - - case ExportSource::CurrentNotebook: - ret = doExport(m_notebook, s_opt, p_outputFolder, p_errMsg, p_outputFiles); - break; - - case ExportSource::Cart: - ret = doExport(m_cart, s_opt, p_outputFolder, p_errMsg, p_outputFiles); - break; - - default: - break; - } - - s_opt.m_format = fmt; - - return ret; -} - -QList VExportDialog::collectFiles(QString *p_errMsg) -{ - Q_UNUSED(p_errMsg); - - QList files; - switch (s_opt.m_source) { - case ExportSource::CurrentNote: - files.append(m_file->fetchPath()); - break; - - case ExportSource::CurrentFolder: - files = m_directory->collectFiles(); - break; - - case ExportSource::CurrentNotebook: - files = m_notebook->collectFiles(); - break; - - case ExportSource::Cart: - files = m_cart->getFiles().toList(); - break; - - default: - break; - } - - return files; -} diff --git a/src/dialog/vexportdialog.h b/src/dialog/vexportdialog.h deleted file mode 100644 index 48a0afec..00000000 --- a/src/dialog/vexportdialog.h +++ /dev/null @@ -1,516 +0,0 @@ -#ifndef VEXPORTDIALOG_H -#define VEXPORTDIALOG_H - -#include -#include -#include -#include - -#include "vconstants.h" - -class QLabel; -class VLineEdit; -class QDialogButtonBox; -class QPushButton; -class QGroupBox; -class QPlainTextEdit; -class VNotebook; -class VDirectory; -class VFile; -class VCart; -class VExporter; -class QCheckBox; -class VLineEdit; -class QProgressBar; - - -enum class ExportSource -{ - CurrentNote = 0, - CurrentFolder, - CurrentNotebook, - Cart -}; - - -enum class ExportFormat -{ - Markdown = 0, - HTML, - PDF, - OnePDF, - Custom -}; - - -enum class ExportPageNumber -{ - None = 0, - Left, - Center, - Right -}; - - -struct ExportHTMLOption -{ - ExportHTMLOption() - : m_embedCssStyle(true), - m_completeHTML(true), - m_embedImages(true), - m_mimeHTML(false), - m_outlinePanel(true) - { - } - - ExportHTMLOption(bool p_embedCssStyle, - bool p_completeHTML, - bool p_embedImages, - bool p_mimeHTML, - bool p_outlinePanel) - : m_embedCssStyle(p_embedCssStyle), - m_completeHTML(p_completeHTML), - m_embedImages(p_embedImages), - m_mimeHTML(p_mimeHTML), - m_outlinePanel(p_outlinePanel) - { - } - - bool m_embedCssStyle; - bool m_completeHTML; - bool m_embedImages; - bool m_mimeHTML; - bool m_outlinePanel; -}; - - -struct ExportPDFOption -{ - ExportPDFOption() - : m_layout(NULL), - m_wkhtmltopdf(false), - m_wkEnableBackground(true), - m_enableTableOfContents(false), - m_wkPageNumber(ExportPageNumber::None) - { - } - - ExportPDFOption(QPageLayout *p_layout, - bool p_wkhtmltopdf, - const QString &p_wkPath, - bool p_wkEnableBackground, - bool p_enableTableOfContents, - const QString &p_wkTitle, - const QString &p_wkTargetFileName, - ExportPageNumber p_wkPageNumber, - const QString &p_wkExtraArgs) - : m_layout(p_layout), - m_wkhtmltopdf(p_wkhtmltopdf), - m_wkPath(p_wkPath), - m_wkEnableBackground(p_wkEnableBackground), - m_enableTableOfContents(p_enableTableOfContents), - m_wkTitle(p_wkTitle), - m_wkTargetFileName(p_wkTargetFileName), - m_wkPageNumber(p_wkPageNumber), - m_wkExtraArgs(p_wkExtraArgs) - { - } - - QPageLayout *m_layout; - bool m_wkhtmltopdf; - QString m_wkPath; - bool m_wkEnableBackground; - bool m_enableTableOfContents;; - QString m_wkTitle; - QString m_wkTargetFileName; - ExportPageNumber m_wkPageNumber; - QString m_wkExtraArgs; -}; - - -struct ExportCustomOption -{ -#if defined(Q_OS_WIN) - #define DEFAULT_SEP ";" -#else - #define DEFAULT_SEP ":" -#endif - - enum SourceFormat - { - Markdown = 0, - HTML - }; - - ExportCustomOption() - : m_srcFormat(SourceFormat::Markdown), - m_allInOne(false), - m_pdfLike(false), - m_folderSep(DEFAULT_SEP) - { - } - - ExportCustomOption(const QStringList &p_config) - : m_srcFormat(SourceFormat::Markdown), - m_allInOne(false), - m_pdfLike(false), - m_folderSep(DEFAULT_SEP) - { - if (p_config.size() < 3) { - return; - } - - if (p_config.at(0).trimmed() != "0") { - m_srcFormat = SourceFormat::HTML; - } - - m_outputSuffix = p_config.at(1).trimmed(); - - m_cmd = p_config.at(2).trimmed(); - } - - ExportCustomOption(ExportCustomOption::SourceFormat p_srcFormat, - const QString &p_outputSuffix, - const QString &p_cmd, - const QString &p_cssUrl, - const QString &p_codeBlockCssUrl, - bool p_allInOne, - bool p_pdfLike, - const QString &p_folderSep, - const QString &p_targetFileName) - : m_srcFormat(p_srcFormat), - m_outputSuffix(p_outputSuffix), - m_cssUrl(p_cssUrl), - m_codeBlockCssUrl(p_codeBlockCssUrl), - m_allInOne(p_allInOne), - m_pdfLike(p_pdfLike), - m_folderSep(p_folderSep), - m_targetFileName(p_targetFileName) - { - QStringList cmds = p_cmd.split('\n'); - if (!cmds.isEmpty()) { - m_cmd = cmds.first(); - } - } - - QStringList toConfig() const - { - QStringList config; - config << QString::number((int)m_srcFormat); - config << m_outputSuffix; - config << m_cmd; - - return config; - } - - SourceFormat m_srcFormat; - QString m_outputSuffix; - QString m_cmd; - - QString m_cssUrl; - QString m_codeBlockCssUrl; - - bool m_allInOne; - bool m_pdfLike; - - QString m_folderSep; - QString m_targetFileName; -}; - - -struct ExportOption -{ - ExportOption() - : m_source(ExportSource::CurrentNote), - m_format(ExportFormat::Markdown), - m_renderer(MarkdownConverterType::MarkdownIt), - m_processSubfolders(true) - { - } - - ExportOption(ExportSource p_source, - ExportFormat p_format, - MarkdownConverterType p_renderer, - const QString &p_renderBg, - const QString &p_renderStyle, - const QString &p_renderCodeBlockStyle, - bool p_processSubfolders, - const ExportPDFOption &p_pdfOpt, - const ExportHTMLOption &p_htmlOpt, - const ExportCustomOption &p_customOpt) - : m_source(p_source), - m_format(p_format), - m_renderer(p_renderer), - m_renderBg(p_renderBg), - m_renderStyle(p_renderStyle), - m_renderCodeBlockStyle(p_renderCodeBlockStyle), - m_processSubfolders(p_processSubfolders), - m_pdfOpt(p_pdfOpt), - m_htmlOpt(p_htmlOpt), - m_customOpt(p_customOpt) - { - } - - ExportSource m_source; - ExportFormat m_format; - MarkdownConverterType m_renderer; - - // Background name. - QString m_renderBg; - - QString m_renderStyle; - QString m_renderCodeBlockStyle; - - // Whether process subfolders recursively when source is CurrentFolder. - bool m_processSubfolders; - - ExportPDFOption m_pdfOpt; - - ExportHTMLOption m_htmlOpt; - - ExportCustomOption m_customOpt; -}; - - -class VExportDialog : public QDialog -{ - Q_OBJECT -public: - VExportDialog(VNotebook *p_notebook, - VDirectory *p_directory, - VFile *p_file, - VCart *p_cart, - MarkdownConverterType p_renderer, - QWidget *p_parent = nullptr); - -private slots: - void startExport(); - - void handleOutputBrowseBtnClicked(); - - void handleWkPathBrowseBtnClicked(); - - void handleInputChanged(); - - void handleLayoutBtnClicked(); - - void handleCurrentFormatChanged(int p_index); - - void handleCurrentSrcChanged(int p_index); - -private: - void setupUI(); - - QWidget *setupPDFAdvancedSettings(); - - QWidget *setupHTMLAdvancedSettings(); - - QWidget *setupGeneralAdvancedSettings(); - - QWidget *setupCustomAdvancedSettings(); - - void initUIFields(MarkdownConverterType p_renderer); - - QString getOutputDirectory() const; - - void appendLogLine(const QString &p_text); - - // Return number of files exported. - int doExport(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg = NULL, - QList *p_outputFiles = NULL); - - int doExport(VDirectory *p_directory, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg = NULL, - QList *p_outputFiles = NULL); - - int doExport(VNotebook *p_notebook, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg = NULL, - QList *p_outputFiles = NULL); - - int doExport(VCart *p_cart, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg = NULL, - QList *p_outputFiles = NULL); - - int doExportMarkdown(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg = NULL, - QList *p_outputFiles = NULL); - - int doExportPDF(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg = NULL, - QList *p_outputFiles = NULL); - - int doExportHTML(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg = NULL, - QList *p_outputFiles = NULL); - - int doExportPDFAllInOne(const QList &p_files, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg = NULL); - - int doExportCustomAllInOne(const QList &p_files, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg = NULL); - - int doExportCustom(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFolder, - QString *p_errMsg = NULL, - QList *p_outputFiles = NULL); - - // Return false if we could not continue. - bool checkUserAction(); - - void updatePageLayoutLabel(); - - bool checkWkhtmltopdfExecutable(const QString &p_file); - - ExportSource currentSource() const; - - ExportFormat currentFormat() const; - - int outputAsHTML(const QString &p_outputFolder, - QString *p_errMsg = NULL, - QList *p_outputFiles = NULL); - - // Collect files to be handled. - QList collectFiles(QString *p_errMsg = NULL); - - QComboBox *m_srcCB; - - QComboBox *m_formatCB; - - QComboBox *m_rendererCB; - - QComboBox *m_renderBgCB; - - QComboBox *m_renderStyleCB; - - QComboBox *m_renderCodeBlockStyleCB; - - VLineEdit *m_outputEdit; - - QGroupBox *m_basicBox; - - QGroupBox *m_settingBox; - - QWidget *m_pdfSettings; - - QWidget *m_htmlSettings; - - QWidget *m_generalSettings; - - QWidget *m_customSettings; - - QPlainTextEdit *m_consoleEdit; - - QDialogButtonBox *m_btnBox; - - QPushButton *m_openBtn; - - QPushButton *m_exportBtn; - - QPushButton *m_copyBtn; - - QLabel *m_layoutLabel; - - QCheckBox *m_wkhtmltopdfCB; - - VLineEdit *m_wkPathEdit; - - QPushButton *m_wkPathBrowseBtn; - - VLineEdit *m_wkTitleEdit; - - VLineEdit *m_wkTargetFileNameEdit; - - QCheckBox *m_wkBackgroundCB; - - QCheckBox *m_tableOfContentsCB; - - QComboBox *m_wkPageNumberCB; - - VLineEdit *m_wkExtraArgsEdit; - - QCheckBox *m_embedStyleCB; - - QCheckBox *m_completeHTMLCB; - - QCheckBox *m_embedImagesCB; - - QCheckBox *m_mimeHTMLCB; - - QCheckBox *m_outlinePanelCB; - - QCheckBox *m_subfolderCB; - - QComboBox *m_customSrcFormatCB; - - VLineEdit *m_customSuffixEdit; - - QCheckBox *m_customAllInOneCB; - - QCheckBox *m_customPdfLikeCB; - - QPlainTextEdit *m_customCmdEdit; - - VLineEdit *m_customFolderSepEdit; - - VLineEdit *m_customTargetFileNameEdit; - - VNotebook *m_notebook; - - VDirectory *m_directory; - - VFile *m_file; - - VCart *m_cart; - - QProgressBar *m_proBar; - - QPageLayout m_pageLayout; - - // Whether we are exporting files. - bool m_inExport; - - // Asked to stop exporting by user. - bool m_askedToStop; - - // Exporter used to export PDF and HTML. - VExporter *m_exporter; - - // Last exproted file path. - QString m_exportedFile; - - // Last output folder path. - static QString s_lastOutputFolder; - - static ExportOption s_opt; -}; - -inline ExportSource VExportDialog::currentSource() const -{ - return (ExportSource)m_srcCB->currentData().toInt(); -} - -inline ExportFormat VExportDialog::currentFormat() const -{ - return (ExportFormat)m_formatCB->currentData().toInt(); -} -#endif // VEXPORTDIALOG_H diff --git a/src/dialog/vsettingsdialog.cpp b/src/dialog/vsettingsdialog.cpp index 8f80e5a0..0b9bf2ba 100644 --- a/src/dialog/vsettingsdialog.cpp +++ b/src/dialog/vsettingsdialog.cpp @@ -62,7 +62,7 @@ VSettingsDialog::VSettingsDialog(QWidget *p_parent) // Add tabs. addTab(new VGeneralTab(), tr("General")); addTab(new VLookTab(), tr("Appearance")); - addTab(new VReadEditTab(this), tr("Read/Edit")); + addTab(new VReadEditTab(this), tr("Edit")); addTab(new VNoteManagementTab(), tr("Note Management")); addTab(new VMarkdownTab(), tr("Markdown")); addTab(new VMiscTab(), tr("Misc")); @@ -684,7 +684,9 @@ VReadEditTab::VReadEditTab(VSettingsDialog *p_dlg, QWidget *p_parent) : QWidget(p_parent), m_settingsDlg(p_dlg) { - m_readBox = new QGroupBox(tr("Read Mode (For Markdown Only)")); + m_readBox = new QGroupBox(tr("Read Mode (For Markdown Only)"), this); + m_readBox->setVisible(false); + m_editBox = new QGroupBox(tr("Edit Mode")); // Web Zoom Factor. @@ -771,7 +773,7 @@ VReadEditTab::VReadEditTab(VSettingsDialog *p_dlg, QWidget *p_parent) m_keyModeCB->setCurrentIndex(0); QVBoxLayout *mainLayout = new QVBoxLayout(); - mainLayout->addWidget(m_readBox); + // mainLayout->addWidget(m_readBox); mainLayout->addWidget(m_editBox); setLayout(mainLayout); } @@ -1211,10 +1213,11 @@ VMarkdownTab::VMarkdownTab(QWidget *p_parent) : QWidget(p_parent) { // Default note open mode. - m_openModeCombo = VUtils::getComboBox(); + m_openModeCombo = VUtils::getComboBox(this); m_openModeCombo->setToolTip(tr("Default mode to open a file")); m_openModeCombo->addItem(tr("Read Mode"), (int)OpenFileMode::Read); m_openModeCombo->addItem(tr("Edit Mode"), (int)OpenFileMode::Edit); + m_openModeCombo->setVisible(false); // Heading sequence. m_headingSequenceTypeCombo = VUtils::getComboBox(); @@ -1256,20 +1259,22 @@ VMarkdownTab::VMarkdownTab(QWidget *p_parent) colorColumnLabel->setToolTip(m_colorColumnEdit->toolTip()); // MathJax. - m_mathjaxConfigEdit = new VLineEdit(); + m_mathjaxConfigEdit = new VLineEdit(this); m_mathjaxConfigEdit->setToolTip(tr("Location of MathJax JavaScript and its configuration " "(restart VNote to make it work in in-place preview)")); m_mathjaxConfigEdit->setPlaceholderText(tr("Need to prepend \"file://\" to local path")); + m_mathjaxConfigEdit->setVisible(false); // PlantUML. m_plantUMLModeCombo = VUtils::getComboBox(); m_plantUMLModeCombo->setToolTip(tr("Enable PlantUML support in Markdown")); m_plantUMLModeCombo->addItem(tr("Disabled"), PlantUMLMode::DisablePlantUML); - m_plantUMLModeCombo->addItem(tr("Online Service"), PlantUMLMode::OnlinePlantUML); + // m_plantUMLModeCombo->addItem(tr("Online Service"), PlantUMLMode::OnlinePlantUML); m_plantUMLModeCombo->addItem(tr("Local JAR"), PlantUMLMode::LocalPlantUML); - m_plantUMLServerEdit = new VLineEdit(); + m_plantUMLServerEdit = new VLineEdit(this); m_plantUMLServerEdit->setToolTip(tr("Server address for online PlantUML")); + m_plantUMLServerEdit->setVisible(false); m_plantUMLJarEdit = new VLineEdit(); m_plantUMLJarEdit->setToolTip(tr("Location to the PlantUML JAR executable for local PlantUML")); @@ -1349,12 +1354,12 @@ VMarkdownTab::VMarkdownTab(QWidget *p_parent) graphvizLayout->addWidget(graphvizTestBtn); QFormLayout *mainLayout = new QFormLayout(); - mainLayout->addRow(tr("Open mode:"), m_openModeCombo); + // mainLayout->addRow(tr("Open mode:"), m_openModeCombo); mainLayout->addRow(tr("Heading sequence:"), headingSequenceLayout); mainLayout->addRow(colorColumnLabel, m_colorColumnEdit); - mainLayout->addRow(tr("MathJax configuration:"), m_mathjaxConfigEdit); + // mainLayout->addRow(tr("MathJax configuration:"), m_mathjaxConfigEdit); mainLayout->addRow(tr("PlantUML:"), m_plantUMLModeCombo); - mainLayout->addRow(tr("PlantUML server:"), m_plantUMLServerEdit); + // mainLayout->addRow(tr("PlantUML server:"), m_plantUMLServerEdit); mainLayout->addRow(tr("PlantUML JAR:"), plantUMLLayout); mainLayout->addRow(m_graphvizCB); mainLayout->addRow(tr("Graphviz executable:"), graphvizLayout); diff --git a/src/dialog/vtipsdialog.cpp b/src/dialog/vtipsdialog.cpp index 9e093664..ae2f0f40 100644 --- a/src/dialog/vtipsdialog.cpp +++ b/src/dialog/vtipsdialog.cpp @@ -1,7 +1,6 @@ #include "vtipsdialog.h" #include -#include #include "vconfigmanager.h" #include "vmarkdownconverter.h" @@ -23,8 +22,8 @@ VTipsDialog::VTipsDialog(const QString &p_tipFile, void VTipsDialog::setupUI(const QString &p_actionText) { - m_viewer = VUtils::getWebEngineView(g_config->getBaseBackground()); - m_viewer->setContextMenuPolicy(Qt::NoContextMenu); + m_viewer = VUtils::getTextBrowser(g_config->getBaseBackground()); + // m_viewer->setContextMenuPolicy(Qt::NoContextMenu); m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok); connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept); @@ -59,9 +58,10 @@ void VTipsDialog::readFile(const QString &p_tipFile) QString html = mdConverter.generateHtml(content, g_config->getMarkdownExtensions(), toc); - html = VUtils::generateSimpleHtmlTemplate(html); + // html = VUtils::generateSimpleHtmlTemplate(html); // Add a base URL to enable it to access local style files. - m_viewer->setHtml(html, QUrl("qrc:/resources")); + m_viewer->setHtml(html); + // m_viewer->setSource(QUrl("qrc:/resources")); } void VTipsDialog::showEvent(QShowEvent *p_event) diff --git a/src/dialog/vtipsdialog.h b/src/dialog/vtipsdialog.h index 2c652e0c..719b0314 100644 --- a/src/dialog/vtipsdialog.h +++ b/src/dialog/vtipsdialog.h @@ -6,9 +6,9 @@ #include class QDialogButtonBox; -class QWebEngineView; class QPushButton; class QShowEvent; +class QTextBrowser; typedef std::function TipsDialogFunc; @@ -29,7 +29,7 @@ private: void readFile(const QString &p_tipFile); - QWebEngineView *m_viewer; + QTextBrowser *m_viewer; QDialogButtonBox *m_btnBox; diff --git a/src/dialog/vupdater.cpp b/src/dialog/vupdater.cpp deleted file mode 100644 index ad308a00..00000000 --- a/src/dialog/vupdater.cpp +++ /dev/null @@ -1,183 +0,0 @@ -#include "vupdater.h" - -#include -#include -#include -#include -#include - -#include "vconfigmanager.h" -#include "vdownloader.h" -#include "vmarkdownconverter.h" -#include "utils/vutils.h" -#include "vnote.h" - -extern VConfigManager *g_config; - -VUpdater::VUpdater(QWidget *p_parent) - : QDialog(p_parent) -{ - setupUI(); -} - -void VUpdater::setupUI() -{ - QImage img(":/resources/icons/vnote_update.svg"); - QSize imgSize(128, 128); - QLabel *imgLabel = new QLabel(); - imgLabel->setPixmap(QPixmap::fromImage(img.scaled(imgSize))); - - m_versionLabel = new QLabel(tr("Current Version: v%1") - .arg(g_config->c_version)); - - m_proLabel = new QLabel(tr("Checking for updates...")); - m_proLabel->setOpenExternalLinks(true); - m_proBar = new QProgressBar(); - m_proBar->setTextVisible(false); - - m_descriptionWV = VUtils::getWebEngineView(g_config->getBaseBackground()); - m_descriptionWV->setContextMenuPolicy(Qt::NoContextMenu); - m_descriptionWV->setHtml(VUtils::generateSimpleHtmlTemplate(VNote::s_sloganTemplate), - QUrl("qrc:/resources")); - - m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok); - m_btnBox->button(QDialogButtonBox::Ok)->setProperty("SpecialBtn", true); - connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept); - - QVBoxLayout *verLayout = new QVBoxLayout(); - verLayout->addStretch(); - verLayout->addWidget(m_versionLabel); - verLayout->addStretch(); - verLayout->addWidget(m_proLabel); - verLayout->addWidget(m_proBar); - - QHBoxLayout *topLayout = new QHBoxLayout(); - topLayout->addWidget(imgLabel); - topLayout->addLayout(verLayout); - topLayout->addStretch(); - - QVBoxLayout *mainLayout = new QVBoxLayout(); - mainLayout->addLayout(topLayout); - mainLayout->addWidget(m_descriptionWV, 1); - mainLayout->addWidget(m_btnBox); - - m_proLabel->hide(); - m_proBar->hide(); - - setLayout(mainLayout); - setWindowTitle(tr("VNote Update")); -} - -void VUpdater::showEvent(QShowEvent *p_event) -{ - QDialog::showEvent(p_event); - - QTimer *timer = new QTimer(this); - timer->setSingleShot(true); - timer->setInterval(1000); - connect(timer, &QTimer::timeout, - this, [this]() { - this->checkUpdates(); - }); - - timer->start(); -} - -void VUpdater::checkUpdates() -{ - // Change UI. - m_proLabel->setText(tr("Checking for updates...")); - m_proLabel->show(); - - m_proBar->setEnabled(true); - m_proBar->setMinimum(0); - m_proBar->setMaximum(100); - m_proBar->reset(); - m_proBar->show(); - - QString url("https://api.github.com/repos/tamlok/vnote/releases/latest"); - VDownloader *downloader = new VDownloader(this); - connect(downloader, &VDownloader::downloadFinished, - this, &VUpdater::parseResult); - downloader->download(url); - - m_proBar->setValue(20); -} - -// Return if @p_latestVersion is newer than p_curVersion. -// They are both in format xx.xx.xx.xx -bool isNewerVersion(const QString &p_curVersion, const QString &p_latestVersion) -{ - QStringList curList = p_curVersion.split('.', QString::SkipEmptyParts); - QStringList latestList = p_latestVersion.split('.', QString::SkipEmptyParts); - - int i = 0; - for (; i < curList.size() && i < latestList.size(); ++i) { - int a = curList[i].toInt(); - int b = latestList[i].toInt(); - - if (a > b) { - return false; - } else if (a < b) { - return true; - } - } - - - if (i < curList.size()) { - // 1.2.1 vs 1.2 - return false; - } else if (i < latestList.size()) { - // 1.2 vs 1.2.1 - return true; - } else { - // 1.2 vs 1.2 - return false; - } -} - -void VUpdater::parseResult(const QByteArray &p_data) -{ - m_proBar->setValue(40); - QJsonDocument jsonDoc = QJsonDocument::fromJson(p_data); - QJsonObject json = jsonDoc.object(); - - if (jsonDoc.isNull() || json.empty()) { - m_proBar->setEnabled(false); - m_proLabel->setText(tr(":( Fail to check for updates.\n" - "Please try it later.")); - - return; - } - - m_proBar->setValue(100); - - QString tag = json["tag_name"].toString(); - if (tag.startsWith('v') && tag.size() > 3) { - tag = tag.right(tag.size() - 1); - } - - QString releaseName = json["name"].toString(); - QString releaseUrl = json["html_url"].toString(); - QString body = json["body"].toString(); - - m_versionLabel->setText(tr("Current Version: v%1\nLatest Version: v%2") - .arg(g_config->c_version).arg(tag)); - if (isNewerVersion(g_config->c_version, tag)) { - m_proLabel->setText(tr("Updates Available!
" - "Please visit GitHub Releases to download the latest version.") - .arg(releaseUrl)); - } else { - m_proLabel->setText(tr("VNote is already the latest version.")); - } - - QString mdText = "# " + releaseName + "\n" + body; - VMarkdownConverter mdConverter; - QString toc; - QString html = mdConverter.generateHtml(mdText, - g_config->getMarkdownExtensions(), - toc); - html = VUtils::generateSimpleHtmlTemplate(html); - m_descriptionWV->setHtml(html, QUrl("qrc:/resources")); - m_proBar->hide(); -} diff --git a/src/dialog/vupdater.h b/src/dialog/vupdater.h deleted file mode 100644 index dcff3bcc..00000000 --- a/src/dialog/vupdater.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef VUPDATER_H -#define VUPDATER_H - -#include -#include - -class QLabel; -class QDialogButtonBox; -class QWebEngineView; -class QProgressBar; -class QShowEvent; - -class VUpdater : public QDialog -{ - Q_OBJECT -public: - VUpdater(QWidget *p_parent = 0); - -protected: - void showEvent(QShowEvent *p_event) Q_DECL_OVERRIDE; - -private slots: - // Calling to Github api got responses. - void parseResult(const QByteArray &p_data); - -private: - void setupUI(); - - // Fetch the latest release info from Github. - void checkUpdates(); - - QLabel *m_versionLabel; - QWebEngineView *m_descriptionWV; - QDialogButtonBox *m_btnBox; - - // Progress label and bar. - QLabel *m_proLabel; - QProgressBar *m_proBar; -}; - -#endif // VUPDATER_H diff --git a/src/pegparser.cpp b/src/pegparser.cpp index 0b93fa08..bd3a008b 100644 --- a/src/pegparser.cpp +++ b/src/pegparser.cpp @@ -463,7 +463,7 @@ static QSharedPointer tryFixUnicodeData(const char *p_data) int cp; int nr = utf8CodePoint(ch, cp); if (nr == -1) { - return NULL; + return QSharedPointer(); } if (cp > MAX_CODE_POINT) { @@ -478,7 +478,7 @@ static QSharedPointer tryFixUnicodeData(const char *p_data) } if (!needFix) { - return NULL; + return QSharedPointer(); } // Replace those chars with two one-byte chars. diff --git a/src/src.pro b/src/src.pro index 631d98e0..99b7a28d 100644 --- a/src/src.pro +++ b/src/src.pro @@ -4,7 +4,7 @@ # #------------------------------------------------- -QT += core gui webenginewidgets webchannel network svg printsupport +QT += core gui network svg printsupport greaterThan(QT_MAJOR_VERSION, 4): QT += widgets @@ -39,7 +39,6 @@ SOURCES += main.cpp\ vedit.cpp \ vdocument.cpp \ utils/vutils.cpp \ - vpreviewpage.cpp \ vstyleparser.cpp \ dialog/vnewnotebookdialog.cpp \ vmarkdownconverter.cpp \ @@ -69,7 +68,6 @@ SOURCES += main.cpp\ vnavigationmode.cpp \ vorphanfile.cpp \ vcodeblockhighlighthelper.cpp \ - vwebview.cpp \ vmdtab.cpp \ vhtmltab.cpp \ utils/vvim.cpp \ @@ -77,7 +75,6 @@ SOURCES += main.cpp\ vvimindicator.cpp \ vbuttonwithwidget.cpp \ vtabindicator.cpp \ - dialog/vupdater.cpp \ dialog/vorphanfileinfodialog.cpp \ vtextblockdata.cpp \ utils/vpreviewutils.cpp \ @@ -122,8 +119,6 @@ SOURCES += main.cpp\ vsimplesearchinput.cpp \ vstyleditemdelegate.cpp \ vtreewidget.cpp \ - dialog/vexportdialog.cpp \ - vexporter.cpp \ vsearcher.cpp \ vsearch.cpp \ vsearchresulttree.cpp \ @@ -140,7 +135,6 @@ SOURCES += main.cpp\ vgraphvizhelper.cpp \ vlivepreviewhelper.cpp \ vmathjaxpreviewhelper.cpp \ - vmathjaxwebdocument.cpp \ vmathjaxinplacepreviewhelper.cpp \ vhistorylist.cpp \ vexplorer.cpp \ @@ -175,7 +169,6 @@ HEADERS += vmainwindow.h \ vconstants.h \ vdocument.h \ utils/vutils.h \ - vpreviewpage.h \ vstyleparser.h \ dialog/vnewnotebookdialog.h \ vmarkdownconverter.h \ @@ -205,7 +198,6 @@ HEADERS += vmainwindow.h \ vnavigationmode.h \ vorphanfile.h \ vcodeblockhighlighthelper.h \ - vwebview.h \ vmdtab.h \ vhtmltab.h \ utils/vvim.h \ @@ -214,7 +206,6 @@ HEADERS += vmainwindow.h \ vbuttonwithwidget.h \ vedittabinfo.h \ vtabindicator.h \ - dialog/vupdater.h \ dialog/vorphanfileinfodialog.h \ vtextblockdata.h \ utils/vpreviewutils.h \ @@ -259,8 +250,6 @@ HEADERS += vmainwindow.h \ vsimplesearchinput.h \ vstyleditemdelegate.h \ vtreewidget.h \ - dialog/vexportdialog.h \ - vexporter.h \ vwordcountinfo.h \ vsearcher.h \ vsearch.h \ @@ -281,7 +270,6 @@ HEADERS += vmainwindow.h \ vgraphvizhelper.h \ vlivepreviewhelper.h \ vmathjaxpreviewhelper.h \ - vmathjaxwebdocument.h \ vmathjaxinplacepreviewhelper.h \ markdownitoption.h \ vhistorylist.h \ diff --git a/src/utils/vutils.cpp b/src/utils/vutils.cpp index c2522985..a658128c 100644 --- a/src/utils/vutils.cpp +++ b/src/utils/vutils.cpp @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -32,11 +31,11 @@ #include #include #include +#include #include "vorphanfile.h" #include "vnote.h" #include "vnotebook.h" -#include "vpreviewpage.h" #include "pegparser.h" #include "widgets/vcombobox.h" @@ -1463,20 +1462,10 @@ void VUtils::setDynamicProperty(QWidget *p_widget, const char *p_prop, bool p_va p_widget->style()->polish(p_widget); } -QWebEngineView *VUtils::getWebEngineView(const QColor &p_background, QWidget *p_parent) +QTextBrowser *VUtils::getTextBrowser(const QColor &p_background, QWidget *p_parent) { - QWebEngineView *viewer = new QWebEngineView(p_parent); - VPreviewPage *page = new VPreviewPage(viewer); - - // Setting the background to Qt::transparent will force GrayScale antialiasing. - if (p_background.isValid() && p_background != Qt::transparent) { - page->setBackgroundColor(p_background); - } - - viewer->setPage(page); - viewer->setZoomFactor(g_config->getWebZoomFactor()); - - return viewer; + QTextBrowser *browser = new QTextBrowser(p_parent); + return browser; } QString VUtils::getFileNameWithLocale(const QString &p_name, const QString &p_locale) diff --git a/src/utils/vutils.h b/src/utils/vutils.h index 3c457646..214d65d1 100644 --- a/src/utils/vutils.h +++ b/src/utils/vutils.h @@ -19,11 +19,11 @@ class VOrphanFile; class VNotebook; class QWidget; class QComboBox; -class QWebEngineView; class QAction; class QTreeWidgetItem; class QFormLayout; class QTemporaryFile; +class QTextBrowser; #if !defined(V_ASSERT) #define V_ASSERT(cond) ((!(cond)) ? qt_assert(#cond, __FILE__, __LINE__) : qt_noop()) @@ -314,7 +314,7 @@ public: // Create and return a QComboBox. static QComboBox *getComboBox(QWidget *p_parent = nullptr); - static QWebEngineView *getWebEngineView(const QColor &p_background, QWidget *p_parent = nullptr); + static QTextBrowser *getTextBrowser(const QColor &p_background, QWidget *p_parent = nullptr); static void setDynamicProperty(QWidget *p_widget, const char *p_prop, bool p_val = true); diff --git a/src/veditarea.cpp b/src/veditarea.cpp index 9ad1864e..e48e2e83 100644 --- a/src/veditarea.cpp +++ b/src/veditarea.cpp @@ -292,7 +292,7 @@ int VEditArea::openFileInWindow(int windowIndex, VFile *p_file, OpenFileMode p_m { Q_ASSERT(windowIndex < splitter->count()); VEditWindow *win = getWindow(windowIndex); - return win->openFile(p_file, p_mode); + return win->openFile(p_file, OpenFileMode::Edit); } void VEditArea::setCurrentTab(int windowIndex, int tabIndex, bool setFocus) diff --git a/src/vexporter.cpp b/src/vexporter.cpp deleted file mode 100644 index e368cf88..00000000 --- a/src/vexporter.cpp +++ /dev/null @@ -1,1053 +0,0 @@ -#include "vexporter.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "vconfigmanager.h" -#include "vfile.h" -#include "vwebview.h" -#include "utils/vutils.h" -#include "vpreviewpage.h" -#include "vconstants.h" -#include "vmarkdownconverter.h" -#include "vdocument.h" -#include "utils/vwebutils.h" - -extern VConfigManager *g_config; - -extern VWebUtils *g_webUtils; - -VExporter::VExporter(QWidget *p_parent) - : QObject(p_parent), - m_webViewer(NULL), - m_state(ExportState::Idle), - m_askedToStop(false) -{ -} - -static QString marginToStrMM(qreal p_margin) -{ - return QString("%1mm").arg(p_margin); -} - -void VExporter::prepareExport(const ExportOption &p_opt) -{ - bool isPdf = p_opt.m_format == ExportFormat::PDF - || p_opt.m_format == ExportFormat::OnePDF - || (p_opt.m_format == ExportFormat::Custom - && p_opt.m_customOpt.m_pdfLike); - bool extraToc = isPdf - && !p_opt.m_pdfOpt.m_wkhtmltopdf - && p_opt.m_pdfOpt.m_enableTableOfContents; - - m_htmlTemplate = VUtils::generateHtmlTemplate(p_opt.m_renderer, - p_opt.m_renderBg, - p_opt.m_renderStyle, - p_opt.m_renderCodeBlockStyle, - isPdf, - isPdf && p_opt.m_pdfOpt.m_wkhtmltopdf, - extraToc); - - bool outline = p_opt.m_htmlOpt.m_outlinePanel - && !isPdf - && (p_opt.m_format == ExportFormat::HTML - || p_opt.m_format == ExportFormat::Custom); - m_exportHtmlTemplate = VUtils::generateExportHtmlTemplate(p_opt.m_renderBg, - isPdf && p_opt.m_pdfOpt.m_wkhtmltopdf, - outline); - - m_pageLayout = *(p_opt.m_pdfOpt.m_layout); - - prepareWKArguments(p_opt.m_pdfOpt); -} - -void VExporter::prepareWKArguments(const ExportPDFOption &p_opt) -{ - m_wkArgs.clear(); - m_wkArgs << "--page-size" << m_pageLayout.pageSize().key(); - m_wkArgs << "--orientation" - << (m_pageLayout.orientation() == QPageLayout::Portrait ? "Portrait" : "Landscape"); - - QMarginsF marginsMM = m_pageLayout.margins(QPageLayout::Millimeter); - m_wkArgs << "--margin-bottom" << marginToStrMM(marginsMM.bottom()); - m_wkArgs << "--margin-left" << marginToStrMM(marginsMM.left()); - m_wkArgs << "--margin-right" << marginToStrMM(marginsMM.right()); - m_wkArgs << "--margin-top" << marginToStrMM(marginsMM.top()); - - QString footer; - switch (p_opt.m_wkPageNumber) { - case ExportPageNumber::Left: - footer = "--footer-left"; - break; - - case ExportPageNumber::Center: - footer = "--footer-center"; - break; - - case ExportPageNumber::Right: - footer = "--footer-right"; - break; - - default: - break; - } - - if (!footer.isEmpty()) { - m_wkArgs << footer << "[page]" - << "--footer-spacing" << QString::number(marginsMM.bottom() / 3, 'f', 2); - } - - // Title. - if (!p_opt.m_wkTitle.isEmpty()) { - m_wkArgs << "--title" << p_opt.m_wkTitle; - } - - m_wkArgs << "--encoding" << "utf-8"; - m_wkArgs << (p_opt.m_wkEnableBackground ? "--background" : "--no-background"); - - // Delay for MathJax. - if (p_opt.m_wkhtmltopdf) { - m_wkArgs << "--javascript-delay" << "10000"; - } - - // Append additional global option. - if (!p_opt.m_wkExtraArgs.isEmpty()) { - m_wkArgs.append(VUtils::parseCombinedArgString(p_opt.m_wkExtraArgs)); - } - - // TOC option. - if (p_opt.m_enableTableOfContents) { - m_wkArgs << "toc" << "--toc-text-size-shrink" << "1.0"; - } -} - -bool VExporter::exportPDF(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFile, - QString *p_errMsg) -{ - return exportViaWebView(p_file, p_opt, p_outputFile, p_errMsg); -} - -bool VExporter::exportHTML(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFile, - QString *p_errMsg) -{ - return exportViaWebView(p_file, p_opt, p_outputFile, p_errMsg); -} - -static void replaceArgument(QString &p_cmd, const QString &p_arg, const QString &p_val) -{ - if (p_val.startsWith("\"")) { - // Check if the arg has been already surrounded by ". - int pos = 0; - while (pos < p_cmd.size()) { - int idx = p_cmd.indexOf(p_arg, pos); - if (idx == -1) { - break; - } - - int len = p_arg.size(); - int nidx = idx; - if (idx > 0 && p_cmd[idx - 1] == '"') { - --nidx; - len += 1; - } - - if (idx + p_arg.size() < p_cmd.size() - && p_cmd[idx + p_arg.size()] == '"') { - len += 1; - } - - p_cmd.replace(nidx, len, p_val); - pos = nidx + p_val.size() - len; - } - } else { - p_cmd.replace(p_arg, p_val); - } -} - -static QString evaluateCommand(const ExportCustomOption &p_opt, - const QString &p_input, - const QString &p_inputFolder, - const QString &p_output) -{ - QString cmd(p_opt.m_cmd); - replaceArgument(cmd, "%0", p_input); - replaceArgument(cmd, "%1", p_output); - replaceArgument(cmd, "%2", QDir::toNativeSeparators(p_opt.m_cssUrl)); - replaceArgument(cmd, "%3", p_inputFolder); - replaceArgument(cmd, "%4", QDir::toNativeSeparators(p_opt.m_codeBlockCssUrl)); - - return cmd; -} - -bool VExporter::exportCustom(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFile, - QString *p_errMsg) -{ - const ExportCustomOption &customOpt = p_opt.m_customOpt; - if (customOpt.m_srcFormat == ExportCustomOption::Markdown) { - // Use Markdown file as input. - QList files; - files.append(QDir::toNativeSeparators(p_file->fetchPath())); - return convertFilesViaCustom(files, p_outputFile, customOpt, p_errMsg); - } else { - return exportViaWebView(p_file, p_opt, p_outputFile, p_errMsg); - } -} - -void VExporter::initWebViewer(VFile *p_file, const ExportOption &p_opt) -{ - Q_ASSERT(!m_webViewer); - - m_webViewer = new VWebView(p_file, static_cast(parent())); - m_webViewer->hide(); - - VPreviewPage *page = new VPreviewPage(m_webViewer); - m_webViewer->setPage(page); - connect(page, &VPreviewPage::loadFinished, - this, &VExporter::handleLoadFinished); - connect(page->profile(), &QWebEngineProfile::downloadRequested, - this, &VExporter::handleDownloadRequested); - - m_webDocument = new VDocument(p_file, m_webViewer); - connect(m_webDocument, &VDocument::logicsFinished, - this, &VExporter::handleLogicsFinished); - - QWebChannel *channel = new QWebChannel(m_webViewer); - channel->registerObject(QStringLiteral("content"), m_webDocument); - page->setWebChannel(channel); - - // Need to generate HTML using Hoedown. - if (p_opt.m_renderer == MarkdownConverterType::Hoedown) { - VMarkdownConverter mdConverter; - QString toc; - QString html = mdConverter.generateHtml(p_file->getContent(), - g_config->getMarkdownExtensions(), - toc); - bool isPdf = p_opt.m_format == ExportFormat::PDF - || p_opt.m_format == ExportFormat::OnePDF; - bool extraToc = isPdf - && !p_opt.m_pdfOpt.m_wkhtmltopdf - && p_opt.m_pdfOpt.m_enableTableOfContents; - if (extraToc && !toc.isEmpty()) { - // Add toc to html. - QString div = "
" + toc + "
\n"; - html = div + html; - } - - m_webDocument->setHtml(html); - } - - m_baseUrl = p_file->getBaseUrl(); - m_webViewer->setHtml(m_htmlTemplate, m_baseUrl); -} - -void VExporter::handleLogicsFinished() -{ - Q_ASSERT(!(m_noteState & NoteState::WebLogicsReady)); - m_noteState = NoteState(m_noteState | NoteState::WebLogicsReady); -} - -void VExporter::handleLoadFinished(bool p_ok) -{ - Q_ASSERT(!(m_noteState & NoteState::WebLoadFinished)); - m_noteState = NoteState(m_noteState | NoteState::WebLoadFinished); - - if (!p_ok) { - m_noteState = NoteState(m_noteState | NoteState::Failed); - } -} - -void VExporter::clearWebViewer() -{ - // m_webDocument will be freeed by QObject. - delete m_webViewer; - m_webViewer = NULL; - m_webDocument = NULL; - m_baseUrl.clear(); -} - -bool VExporter::exportToPDF(VWebView *p_webViewer, - const QString &p_filePath, - const QPageLayout &p_layout) -{ - int pdfPrinted = 0; - p_webViewer->page()->printToPdf([&, this](const QByteArray &p_result) { - if (p_result.isEmpty() || this->m_state == ExportState::Cancelled) { - pdfPrinted = -1; - return; - } - - V_ASSERT(!p_filePath.isEmpty()); - - if (!VUtils::writeFileToDisk(p_filePath, p_result)) { - pdfPrinted = -1; - return; - } - - pdfPrinted = 1; - }, p_layout); - - while (pdfPrinted == 0) { - VUtils::sleepWait(100); - - if (m_state == ExportState::Cancelled) { - break; - } - } - - return pdfPrinted == 1; -} - -bool VExporter::exportToPDFViaWK(VDocument *p_webDocument, - const ExportPDFOption &p_opt, - const QString &p_filePath, - QString *p_errMsg) -{ - int pdfExported = 0; - - connect(p_webDocument, &VDocument::htmlContentFinished, - this, [&, this](const QString &p_headContent, - const QString &p_styleContent, - const QString &p_bodyContent) { - if (p_bodyContent.isEmpty() || this->m_state == ExportState::Cancelled) { - pdfExported = -1; - return; - } - - Q_ASSERT(!p_filePath.isEmpty()); - - // Save HTML to a temp dir. - QTemporaryDir tmpDir; - if (!tmpDir.isValid()) { - pdfExported = -1; - return; - } - - QString htmlPath = tmpDir.filePath("vnote_tmp.html"); - QString title = p_webDocument->getFile()->getName(); - title = QFileInfo(title).completeBaseName(); - if (!outputToHTMLFile(htmlPath, - title, - p_headContent, - p_styleContent, - p_bodyContent, - true, - true, - false)) { - pdfExported = -1; - return; - } - - // Convert via wkhtmltopdf. - QList files; - files.append(htmlPath); - if (!htmlsToPDFViaWK(files, p_filePath, p_opt, p_errMsg)) { - pdfExported = -1; - } else { - pdfExported = 1; - } - }); - - p_webDocument->getHtmlContentAsync(); - - while (pdfExported == 0) { - VUtils::sleepWait(100); - - if (m_state == ExportState::Cancelled) { - break; - } - } - - return pdfExported == 1; -} - -bool VExporter::exportToCustom(VDocument *p_webDocument, - const ExportCustomOption &p_opt, - const QString &p_filePath, - QString *p_errMsg) -{ - int exported = 0; - - connect(p_webDocument, &VDocument::htmlContentFinished, - this, [&, this](const QString &p_headContent, - const QString &p_styleContent, - const QString &p_bodyContent) { - if (p_bodyContent.isEmpty() || this->m_state == ExportState::Cancelled) { - exported = -1; - return; - } - - Q_ASSERT(!p_filePath.isEmpty()); - - // Save HTML to a temp dir. - QTemporaryDir tmpDir; - if (!tmpDir.isValid()) { - exported = -1; - return; - } - - QString htmlPath = tmpDir.filePath("vnote_tmp.html"); - QString title = p_webDocument->getFile()->getName(); - title = QFileInfo(title).completeBaseName(); - if (!outputToHTMLFile(htmlPath, - title, - p_headContent, - p_styleContent, - p_bodyContent, - true, - true, - false)) { - exported = -1; - return; - } - - // Convert via custom command. - QList files; - files.append(htmlPath); - if (!convertFilesViaCustom(files, p_filePath, p_opt, p_errMsg)) { - exported = -1; - } else { - exported = 1; - } - }); - - p_webDocument->getHtmlContentAsync(); - - while (exported == 0) { - VUtils::sleepWait(100); - - if (m_state == ExportState::Cancelled) { - break; - } - } - - return exported == 1; -} - -bool VExporter::exportViaWebView(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFile, - QString *p_errMsg) -{ - Q_UNUSED(p_errMsg); - - bool ret = false; - - bool isOpened = p_file->isOpened(); - if (!isOpened && !p_file->open()) { - goto exit; - } - - Q_ASSERT(m_state == ExportState::Idle); - m_state = ExportState::Busy; - - clearNoteState(); - - initWebViewer(p_file, p_opt); - - while (!isNoteStateReady()) { - VUtils::sleepWait(100); - - if (m_state == ExportState::Cancelled) { - goto exit; - } - - if (isNoteStateFailed()) { - m_state = ExportState::Failed; - goto exit; - } - - if (m_askedToStop) { - m_state = ExportState::Cancelled; - goto exit; - } - } - - // Wait to ensure Web side is really ready. - VUtils::sleepWait(200); - - if (m_state == ExportState::Cancelled) { - goto exit; - } - - { - - bool exportRet = false; - switch (p_opt.m_format) { - case ExportFormat::PDF: - V_FALLTHROUGH; - case ExportFormat::OnePDF: - if (p_opt.m_pdfOpt.m_wkhtmltopdf) { - exportRet = exportToPDFViaWK(m_webDocument, - p_opt.m_pdfOpt, - p_outputFile, - p_errMsg); - } else { - exportRet = exportToPDF(m_webViewer, - p_outputFile, - m_pageLayout); - } - - break; - - case ExportFormat::HTML: - if (p_opt.m_htmlOpt.m_mimeHTML) { - exportRet = exportToMHTML(m_webViewer, - p_opt.m_htmlOpt, - p_outputFile); - } else { - exportRet = exportToHTML(m_webDocument, - p_opt.m_htmlOpt, - p_outputFile); - } - - break; - - case ExportFormat::Custom: - exportRet = exportToCustom(m_webDocument, - p_opt.m_customOpt, - p_outputFile, - p_errMsg); - break; - - default: - break; - } - - clearNoteState(); - - if (!isOpened) { - p_file->close(); - } - - if (exportRet) { - m_state = ExportState::Successful; - } else { - m_state = ExportState::Failed; - } - - } - -exit: - clearWebViewer(); - - if (m_state == ExportState::Successful) { - ret = true; - } - - m_state = ExportState::Idle; - - return ret; -} - -bool VExporter::exportToHTML(VDocument *p_webDocument, - const ExportHTMLOption &p_opt, - const QString &p_filePath) -{ - int htmlExported = 0; - - connect(p_webDocument, &VDocument::htmlContentFinished, - this, [&, this](const QString &p_headContent, - const QString &p_styleContent, - const QString &p_bodyContent) { - if (p_bodyContent.isEmpty() || this->m_state == ExportState::Cancelled) { - htmlExported = -1; - return; - } - - Q_ASSERT(!p_filePath.isEmpty()); - - QString title = p_webDocument->getFile()->getName(); - title = QFileInfo(title).completeBaseName(); - if (!outputToHTMLFile(p_filePath, - title, - p_headContent, - p_styleContent, - p_bodyContent, - p_opt.m_embedCssStyle, - p_opt.m_completeHTML, - p_opt.m_embedImages)) { - htmlExported = -1; - return; - } - - htmlExported = 1; - }); - - p_webDocument->getHtmlContentAsync(); - - while (htmlExported == 0) { - VUtils::sleepWait(100); - - if (m_state == ExportState::Cancelled) { - break; - } - } - - return htmlExported == 1; -} - -bool VExporter::fixStyleResources(const QString &p_folder, - QString &p_html) -{ - bool altered = false; - QRegExp reg("\\burl\\(\"((file|qrc):[^\"\\)]+)\"\\);"); - - int pos = 0; - while (pos < p_html.size()) { - int idx = p_html.indexOf(reg, pos); - if (idx == -1) { - break; - } - - QString targetFile = g_webUtils->copyResource(QUrl(reg.cap(1)), p_folder); - if (targetFile.isEmpty()) { - pos = idx + reg.matchedLength(); - } else { - // Replace the url string in html. - QString newUrl = QString("url(\"%1\");").arg(getResourceRelativePath(targetFile)); - p_html.replace(idx, reg.matchedLength(), newUrl); - pos = idx + newUrl.size(); - altered = true; - } - } - - return altered; -} - -bool VExporter::embedStyleResources(QString &p_html) -{ - bool altered = false; - QRegExp reg("\\burl\\(\"((file|qrc):[^\"\\)]+)\"\\);"); - - int pos = 0; - while (pos < p_html.size()) { - int idx = p_html.indexOf(reg, pos); - if (idx == -1) { - break; - } - - QString dataURI = g_webUtils->dataURI(QUrl(reg.cap(1)), false); - if (dataURI.isEmpty()) { - pos = idx + reg.matchedLength(); - } else { - // Replace the url string in html. - QString newUrl = QString("url('%1');").arg(dataURI); - p_html.replace(idx, reg.matchedLength(), newUrl); - pos = idx + newUrl.size(); - altered = true; - } - } - - return altered; -} - -bool VExporter::fixBodyResources(const QUrl &p_baseUrl, - const QString &p_folder, - QString &p_html) -{ - bool altered = false; - if (p_baseUrl.isEmpty()) { - return altered; - } - - QRegExp reg("]*)src=\"([^\"]+)\"([^>]*)>"); - - int pos = 0; - while (pos < p_html.size()) { - int idx = p_html.indexOf(reg, pos); - if (idx == -1) { - break; - } - - if (reg.cap(2).isEmpty()) { - pos = idx + reg.matchedLength(); - continue; - } - - QUrl srcUrl(p_baseUrl.resolved(reg.cap(2))); - QString targetFile = g_webUtils->copyResource(srcUrl, p_folder); - if (targetFile.isEmpty()) { - pos = idx + reg.matchedLength(); - } else { - // Replace the url string in html. - QString newUrl = QString("").arg(reg.cap(1)) - .arg(getResourceRelativePath(targetFile)) - .arg(reg.cap(3)); - p_html.replace(idx, reg.matchedLength(), newUrl); - pos = idx + newUrl.size(); - altered = true; - } - } - - return altered; -} - -bool VExporter::embedBodyResources(const QUrl &p_baseUrl, QString &p_html) -{ - bool altered = false; - if (p_baseUrl.isEmpty()) { - return altered; - } - - QRegExp reg("]*)src=\"([^\"]+)\"([^>]*)>"); - - int pos = 0; - while (pos < p_html.size()) { - int idx = p_html.indexOf(reg, pos); - if (idx == -1) { - break; - } - - if (reg.cap(2).isEmpty()) { - pos = idx + reg.matchedLength(); - continue; - } - - QUrl srcUrl(p_baseUrl.resolved(reg.cap(2))); - QString dataURI = g_webUtils->dataURI(srcUrl); - if (dataURI.isEmpty()) { - pos = idx + reg.matchedLength(); - } else { - // Replace the url string in html. - QString newUrl = QString("").arg(reg.cap(1)) - .arg(dataURI) - .arg(reg.cap(3)); - p_html.replace(idx, reg.matchedLength(), newUrl); - pos = idx + newUrl.size(); - altered = true; - } - } - - return altered; -} - -QString VExporter::getResourceRelativePath(const QString &p_file) -{ - int idx = p_file.lastIndexOf('/'); - int idx2 = p_file.lastIndexOf('/', idx - 1); - Q_ASSERT(idx > 0 && idx2 < idx); - return "." + p_file.mid(idx2); -} - -bool VExporter::exportToMHTML(VWebView *p_webViewer, - const ExportHTMLOption &p_opt, - const QString &p_filePath) -{ - Q_UNUSED(p_opt); - - m_downloadState = QWebEngineDownloadItem::DownloadRequested; - - p_webViewer->page()->save(p_filePath, QWebEngineDownloadItem::MimeHtmlSaveFormat); - - while (m_downloadState == QWebEngineDownloadItem::DownloadRequested - || m_downloadState == QWebEngineDownloadItem::DownloadInProgress) { - VUtils::sleepWait(100); - } - - return m_downloadState == QWebEngineDownloadItem::DownloadCompleted; -} - -void VExporter::handleDownloadRequested(QWebEngineDownloadItem *p_item) -{ - if (p_item->savePageFormat() == QWebEngineDownloadItem::MimeHtmlSaveFormat) { - connect(p_item, &QWebEngineDownloadItem::stateChanged, - this, [this](QWebEngineDownloadItem::DownloadState p_state) { - m_downloadState = p_state; - }); - } -} - -static QString combineArgs(QStringList &p_args) -{ - QString str; - for (const QString &arg : p_args) { - QString tmp; - if (arg.contains(' ')) { - tmp = '"' + arg + '"'; - } else { - tmp = arg; - } - - if (str.isEmpty()) { - str = tmp; - } else { - str = str + ' ' + tmp; - } - } - - return str; -} - -bool VExporter::htmlsToPDFViaWK(const QList &p_htmlFiles, - const QString &p_filePath, - const ExportPDFOption &p_opt, - QString *p_errMsg) -{ - // Note: system's locale settings (Language for non-Unicode programs) is important to wkhtmltopdf. - // Input file could be encoded via QUrl::fromLocalFile(p_htmlFile).toString(QUrl::EncodeUnicode) to - // handle non-ASCII path. - - QStringList args(m_wkArgs); - - for (auto const & it : p_htmlFiles) { - args << QDir::toNativeSeparators(it); - } - - args << QDir::toNativeSeparators(p_filePath); - - QString cmd = p_opt.m_wkPath + " " + combineArgs(args); - emit outputLog(cmd); - qDebug() << "wkhtmltopdf cmd:" << cmd; - int ret = startProcess(p_opt.m_wkPath, args); - qDebug() << "wkhtmltopdf returned" << ret; - if (m_askedToStop) { - return ret == 0; - } - - switch (ret) { - case -2: - VUtils::addErrMsg(p_errMsg, tr("Fail to start wkhtmltopdf (%1).").arg(cmd)); - break; - - case -1: - VUtils::addErrMsg(p_errMsg, tr("wkhtmltopdf crashed (%1).").arg(cmd)); - break; - - default: - break; - } - - return ret == 0; -} - -bool VExporter::convertFilesViaCustom(const QList &p_files, - const QString &p_filePath, - const ExportCustomOption &p_opt, - QString *p_errMsg) -{ - QString input; - QString inputFolder; - for (auto const & it : p_files) { - if (!input.isEmpty()) { - input += " "; - } - - if (!inputFolder.isEmpty()) { - inputFolder += p_opt.m_folderSep; - } - - QString tmp = QDir::toNativeSeparators(it); - input += ("\"" + tmp + "\""); - inputFolder += ("\"" + VUtils::basePathFromPath(tmp) + "\""); - } - - QString output = QDir::toNativeSeparators(p_filePath); - QString cmd = evaluateCommand(p_opt, - input, - inputFolder, - output); - emit outputLog(cmd); - qDebug() << "custom cmd:" << cmd; - int ret = startProcess(cmd); - qDebug() << "custom cmd returned" << ret; - if (m_askedToStop) { - return ret == 0; - } - - switch (ret) { - case -2: - VUtils::addErrMsg(p_errMsg, tr("Fail to start custom command (%1).").arg(cmd)); - break; - - case -1: - VUtils::addErrMsg(p_errMsg, tr("Custom command crashed (%1).").arg(cmd)); - break; - - default: - break; - } - - return ret == 0; -} - -int VExporter::exportPDFInOne(const QList &p_htmlFiles, - const ExportOption &p_opt, - const QString &p_outputFile, - QString *p_errMsg) -{ - if (!htmlsToPDFViaWK(p_htmlFiles, p_outputFile, p_opt.m_pdfOpt, p_errMsg)) { - return 0; - } - - return p_htmlFiles.size(); -} - -int VExporter::startProcess(const QString &p_program, const QStringList &p_args) -{ - int ret = 0; - QScopedPointer process(new QProcess(this)); - process->start(p_program, p_args); - bool finished = false; - bool started = false; - while (true) { - QProcess::ProcessError err = process->error(); - if (err == QProcess::FailedToStart - || err == QProcess::Crashed) { - emit outputLog(tr("QProcess error %1.").arg(err)); - if (err == QProcess::FailedToStart) { - ret = -2; - } else { - ret = -1; - } - - break; - } - - if (started) { - if (process->state() == QProcess::NotRunning) { - finished = true; - } - } else { - if (process->state() != QProcess::NotRunning) { - started = true; - } - } - - if (process->waitForFinished(500)) { - // Finished. - finished = true; - } - - QByteArray outBa = process->readAllStandardOutput(); - QByteArray errBa = process->readAllStandardError(); - QString msg; - if (!outBa.isEmpty()) { - msg += QString::fromLocal8Bit(outBa); - } - - if (!errBa.isEmpty()) { - msg += QString::fromLocal8Bit(errBa); - } - - if (!msg.isEmpty()) { - emit outputLog(msg); - } - - if (finished) { - QProcess::ExitStatus sta = process->exitStatus(); - if (sta == QProcess::CrashExit) { - ret = -1; - break; - } - - ret = process->exitCode(); - break; - } - - QCoreApplication::processEvents(); - - if (m_askedToStop) { - process->kill(); - ret = -1; - break; - } - } - - return ret; -} - -int VExporter::startProcess(const QString &p_cmd) -{ - QStringList args = VUtils::parseCombinedArgString(p_cmd); - if (args.isEmpty()) { - return -2; - } - - return startProcess(args.first(), args.mid(1)); -} - -bool VExporter::outputToHTMLFile(const QString &p_file, - const QString &p_title, - const QString &p_headContent, - const QString &p_styleContent, - const QString &p_bodyContent, - bool p_embedCssStyle, - bool p_completeHTML, - bool p_embedImages) -{ - QFile file(p_file); - if (!file.open(QFile::WriteOnly)) { - return false; - } - - QString resFolder = QFileInfo(p_file).completeBaseName() + "_files"; - QString resFolderPath = QDir(VUtils::basePathFromPath(p_file)).filePath(resFolder); - - qDebug() << "HTML files folder" << resFolderPath; - - QString html(m_exportHtmlTemplate); - if (!p_title.isEmpty()) { - html.replace(HtmlHolder::c_headTitleHolder, - "" + VUtils::escapeHtml(p_title) + ""); - } - - if (!p_styleContent.isEmpty() && p_embedCssStyle) { - QString content(p_styleContent); - embedStyleResources(content); - html.replace(HtmlHolder::c_styleHolder, content); - } - - if (!p_headContent.isEmpty()) { - html.replace(HtmlHolder::c_headHolder, p_headContent); - } - - if (p_completeHTML) { - QString content(p_bodyContent); - if (p_embedImages) { - embedBodyResources(m_baseUrl, content); - } else { - fixBodyResources(m_baseUrl, resFolderPath, content); - } - - html.replace(HtmlHolder::c_bodyHolder, content); - } else { - html.replace(HtmlHolder::c_bodyHolder, p_bodyContent); - } - - file.write(html.toUtf8()); - file.close(); - - // Delete empty resource folder. - QDir dir(resFolderPath); - if (dir.isEmpty()) { - dir.cdUp(); - dir.rmdir(resFolder); - } - - return true; -} - -int VExporter::exportCustomInOne(const QList &p_files, - const ExportOption &p_opt, - const QString &p_outputFile, - QString *p_errMsg) -{ - if (!convertFilesViaCustom(p_files, p_outputFile, p_opt.m_customOpt, p_errMsg)) { - return 0; - } - - return p_files.size(); -} diff --git a/src/vexporter.h b/src/vexporter.h deleted file mode 100644 index d8743224..00000000 --- a/src/vexporter.h +++ /dev/null @@ -1,213 +0,0 @@ -#ifndef VEXPORTER_H -#define VEXPORTER_H - -#include -#include -#include -#include -#include - -#include "dialog/vexportdialog.h" - -class QWidget; -class VWebView; -class VDocument; - -class VExporter : public QObject -{ - Q_OBJECT -public: - explicit VExporter(QWidget *p_parent = nullptr); - - void prepareExport(const ExportOption &p_opt); - - bool exportPDF(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFile, - QString *p_errMsg = NULL); - - bool exportHTML(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFile, - QString *p_errMsg = NULL); - - bool exportCustom(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFile, - QString *p_errMsg = NULL); - - int exportPDFInOne(const QList &p_htmlFiles, - const ExportOption &p_opt, - const QString &p_outputFile, - QString *p_errMsg = NULL); - - int exportCustomInOne(const QList &p_files, - const ExportOption &p_opt, - const QString &p_outputFile, - QString *p_errMsg = NULL); - - void setAskedToStop(bool p_askedToStop); - -signals: - // Request to output log. - void outputLog(const QString &p_log); - -private slots: - void handleLogicsFinished(); - - void handleLoadFinished(bool p_ok); - - void handleDownloadRequested(QWebEngineDownloadItem *p_item); - -private: - enum class ExportState - { - Idle = 0, - Cancelled, - Busy, - Failed, - Successful - }; - - - enum NoteState - { - NotReady = 0, - WebLogicsReady = 0x1, - WebLoadFinished = 0x2, - Ready = 0x3, - Failed = 0x4 - }; - - - void initWebViewer(VFile *p_file, const ExportOption &p_opt); - - void clearWebViewer(); - - void clearNoteState(); - - bool isNoteStateReady() const; - - bool isNoteStateFailed() const; - - bool exportViaWebView(VFile *p_file, - const ExportOption &p_opt, - const QString &p_outputFile, - QString *p_errMsg = NULL); - - bool exportToPDF(VWebView *p_webViewer, - const QString &p_filePath, - const QPageLayout &p_layout); - - bool exportToPDFViaWK(VDocument *p_webDocument, - const ExportPDFOption &p_opt, - const QString &p_filePath, - QString *p_errMsg = NULL); - - bool exportToCustom(VDocument *p_webDocument, - const ExportCustomOption &p_opt, - const QString &p_filePath, - QString *p_errMsg = NULL); - - bool exportToHTML(VDocument *p_webDocument, - const ExportHTMLOption &p_opt, - const QString &p_filePath); - - bool exportToMHTML(VWebView *p_webViewer, - const ExportHTMLOption &p_opt, - const QString &p_filePath); - - bool htmlsToPDFViaWK(const QList &p_htmlFiles, - const QString &p_filePath, - const ExportPDFOption &p_opt, - QString *p_errMsg = NULL); - - bool convertFilesViaCustom(const QList &p_files, - const QString &p_filePath, - const ExportCustomOption &p_opt, - QString *p_errMsg = NULL); - - void prepareWKArguments(const ExportPDFOption &p_opt); - - int startProcess(const QString &p_program, const QStringList &p_args); - - int startProcess(const QString &p_cmd); - - // @p_embedImages: embed as data URI. - bool outputToHTMLFile(const QString &p_file, - const QString &p_title, - const QString &p_headContent, - const QString &p_styleContent, - const QString &p_bodyContent, - bool p_embedCssStyle, - bool p_completeHTML, - bool p_embedImages); - - // Fix @p_html's resources like url("...") with "file" or "qrc" schema. - // Copy the resource to @p_folder and fix the url string. - static bool fixStyleResources(const QString &p_folder, - QString &p_html); - - // Fix @p_html's resources like url("...") with "file" or "qrc" schema. - // Embed the image data in data URIs. - static bool embedStyleResources(QString &p_html); - - // Fix @p_html's resources like . - // Copy the resource to @p_folder and fix the url string. - static bool fixBodyResources(const QUrl &p_baseUrl, - const QString &p_folder, - QString &p_html); - - // Embed @p_html's resources like . - static bool embedBodyResources(const QUrl &p_baseUrl, QString &p_html); - - static QString getResourceRelativePath(const QString &p_file); - - QPageLayout m_pageLayout; - - // Will be allocated and free for each conversion. - VWebView *m_webViewer; - - VDocument *m_webDocument; - - // Base URL of VWebView. - QUrl m_baseUrl; - - QString m_htmlTemplate; - - // Template to hold the export HTML result. - QString m_exportHtmlTemplate; - - NoteState m_noteState; - - ExportState m_state; - - // Download state used for MIME HTML. - QWebEngineDownloadItem::DownloadState m_downloadState; - - // Arguments for wkhtmltopdf. - QStringList m_wkArgs; - - bool m_askedToStop; -}; - -inline void VExporter::clearNoteState() -{ - m_noteState = NoteState::NotReady; -} - -inline bool VExporter::isNoteStateReady() const -{ - return m_noteState == NoteState::Ready; -} - -inline bool VExporter::isNoteStateFailed() const -{ - return m_noteState & NoteState::Failed; -} - -inline void VExporter::setAskedToStop(bool p_askedToStop) -{ - m_askedToStop = p_askedToStop; -} -#endif // VEXPORTER_H diff --git a/src/vfile.cpp b/src/vfile.cpp index da896309..874bc4cc 100644 --- a/src/vfile.cpp +++ b/src/vfile.cpp @@ -124,7 +124,7 @@ bool VFile::isChangedOutside(bool &p_missing) const p_missing = false; QDateTime lm = QFileInfo(fetchPath()).lastModified(); - return lm.toSecsSinceEpoch() != m_lastModified.toSecsSinceEpoch(); + return lm.toMSecsSinceEpoch() != m_lastModified.toMSecsSinceEpoch(); } bool VFile::reload() diff --git a/src/vfilelist.cpp b/src/vfilelist.cpp index ba15b8d0..7f4b166e 100644 --- a/src/vfilelist.cpp +++ b/src/vfilelist.cpp @@ -561,31 +561,18 @@ void VFileList::contextMenuRequested(QPoint pos) VNoteFile *file = getVFile(item); if (file) { if (file->getDocType() == DocType::Markdown) { - QAction *openInReadAct = new QAction(VIconUtils::menuIcon(":/resources/icons/reading.svg"), - tr("&Open In Read Mode"), + QAction *openAct = new QAction(VIconUtils::menuIcon(":/resources/icons/editing.svg"), + tr("Open"), &menu); - openInReadAct->setToolTip(tr("Open current note in read mode")); - connect(openInReadAct, &QAction::triggered, - this, [this]() { - QListWidgetItem *item = fileList->currentItem(); - if (item) { - emit fileClicked(getVFile(item), OpenFileMode::Read, true); - } - }); - menu.addAction(openInReadAct); - - QAction *openInEditAct = new QAction(VIconUtils::menuIcon(":/resources/icons/editing.svg"), - tr("Open In &Edit Mode"), - &menu); - openInEditAct->setToolTip(tr("Open current note in edit mode")); - connect(openInEditAct, &QAction::triggered, + openAct->setToolTip(tr("Open and edit current note")); + connect(openAct, &QAction::triggered, this, [this]() { QListWidgetItem *item = fileList->currentItem(); if (item) { emit fileClicked(getVFile(item), OpenFileMode::Edit, true); } }); - menu.addAction(openInEditAct); + menu.addAction(openAct); } menu.addMenu(getOpenWithMenu()); @@ -824,7 +811,8 @@ void VFileList::activateItem(QListWidgetItem *p_item, bool p_restoreFocus) // Qt seems not to update the QListWidget correctly. Manually force it to repaint. fileList->update(); - emit fileClicked(getVFile(p_item), g_config->getNoteOpenMode()); + // emit fileClicked(getVFile(p_item), g_config->getNoteOpenMode()); + emit fileClicked(getVFile(p_item), OpenFileMode::Edit); if (p_restoreFocus) { fileList->setFocus(); diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index 9533b7a7..74e78a3f 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include "vmainwindow.h" #include "vdirectorytree.h" @@ -18,13 +17,11 @@ #include "dialog/vsettingsdialog.h" #include "vcaptain.h" #include "vedittab.h" -#include "vwebview.h" -#include "vexporter.h" #include "vmdtab.h" #include "vvimindicator.h" #include "vvimcmdlineedit.h" #include "vtabindicator.h" -#include "dialog/vupdater.h" +// #include "dialog/vupdater.h" #include "vorphanfile.h" #include "dialog/vorphanfileinfodialog.h" #include "vsingleinstanceguard.h" @@ -39,7 +36,6 @@ #include "utils/viconutils.h" #include "dialog/vtipsdialog.h" #include "vcart.h" -#include "dialog/vexportdialog.h" #include "vsearcher.h" #include "vuniversalentry.h" #include "vsearchue.h" @@ -755,22 +751,16 @@ QToolBar *VMainWindow::initFileToolBar(QSize p_iconSize) connect(deleteNoteAct, &QAction::triggered, this, &VMainWindow::deleteCurNote); - m_editReadAct = new QAction(this); - connect(m_editReadAct, &QAction::triggered, - this, &VMainWindow::toggleEditReadMode); - - m_discardExitAct = new QAction(VIconUtils::menuIcon(":/resources/icons/discard_exit.svg"), - tr("Discard Changes And Read"), + m_discardAct = new QAction(VIconUtils::menuIcon(":/resources/icons/discard_exit.svg"), + tr("Discard Changes"), this); - VUtils::fixTextWithCaptainShortcut(m_discardExitAct, "DiscardAndRead"); - m_discardExitAct->setStatusTip(tr("Discard changes and exit edit mode")); - connect(m_discardExitAct, &QAction::triggered, + VUtils::fixTextWithCaptainShortcut(m_discardAct, "DiscardAndRead"); + m_discardAct->setStatusTip(tr("Discard changes")); + connect(m_discardAct, &QAction::triggered, this, [this]() { m_editArea->readFile(true); }); - updateEditReadAct(NULL); - saveNoteAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/save_note.svg"), tr("Save"), this); saveNoteAct->setStatusTip(tr("Save changes to current note")); @@ -788,8 +778,7 @@ QToolBar *VMainWindow::initFileToolBar(QSize p_iconSize) newNoteAct->setEnabled(false); noteInfoAct->setEnabled(false); deleteNoteAct->setEnabled(false); - m_editReadAct->setEnabled(false); - m_discardExitAct->setEnabled(false); + m_discardAct->setEnabled(false); saveNoteAct->setEnabled(false); m_fileToolBar->addWidget(m_avatarBtn); @@ -797,8 +786,7 @@ QToolBar *VMainWindow::initFileToolBar(QSize p_iconSize) m_fileToolBar->addAction(newNoteAct); m_fileToolBar->addAction(deleteNoteAct); m_fileToolBar->addAction(noteInfoAct); - m_fileToolBar->addAction(m_editReadAct); - m_fileToolBar->addAction(m_discardExitAct); + m_fileToolBar->addAction(m_discardAct); m_fileToolBar->addAction(saveNoteAct); return m_fileToolBar; @@ -861,6 +849,7 @@ void VMainWindow::initHelpMenu() QDesktopServices::openUrl(url); }); + /* QAction *updateAct = new QAction(tr("Check For &Updates"), this); updateAct->setToolTip(tr("Check for updates of VNote")); connect(updateAct, &QAction::triggered, @@ -868,6 +857,7 @@ void VMainWindow::initHelpMenu() VUpdater updater(this); updater.exec(); }); + */ QAction *starAct = new QAction(tr("Star VNote on &GitHub"), this); starAct->setToolTip(tr("Give a star to VNote on GitHub project")); @@ -901,7 +891,7 @@ void VMainWindow::initHelpMenu() helpMenu->addAction(mdGuideAct); helpMenu->addAction(docAct); helpMenu->addAction(donateAct); - helpMenu->addAction(updateAct); + // helpMenu->addAction(updateAct); helpMenu->addAction(starAct); helpMenu->addAction(feedbackAct); @@ -918,6 +908,7 @@ void VMainWindow::initMarkdownMenu() QMenu *markdownMenu = menuBar()->addMenu(tr("&Markdown")); markdownMenu->setToolTipsVisible(true); + /* initConverterMenu(markdownMenu); initMarkdownitOptionMenu(markdownMenu); @@ -969,6 +960,7 @@ void VMainWindow::initMarkdownMenu() }); markdownMenu->addAction(lineNumberAct); lineNumberAct->setChecked(g_config->getEnableCodeBlockLineNumber()); + */ QAction *previewImageAct = new QAction(tr("In-Place Preview"), this); previewImageAct->setToolTip(tr("Enable in-place preview (images, diagrams, and formulas) in edit mode (re-open current tabs to make it work)")); @@ -1051,6 +1043,7 @@ void VMainWindow::initFileMenu() fileMenu->addSeparator(); // Export as PDF. + /* m_exportAct = new QAction(tr("E&xport"), this); m_exportAct->setToolTip(tr("Export notes")); VUtils::fixTextWithCaptainShortcut(m_exportAct, "Export"); @@ -1058,8 +1051,10 @@ void VMainWindow::initFileMenu() this, &VMainWindow::handleExportAct); fileMenu->addAction(m_exportAct); + */ // Print. + /* m_printAct = new QAction(VIconUtils::menuIcon(":/resources/icons/print.svg"), tr("&Print"), this); m_printAct->setToolTip(tr("Print current note")); @@ -1070,6 +1065,7 @@ void VMainWindow::initFileMenu() fileMenu->addAction(m_printAct); fileMenu->addSeparator(); + */ // Themes. initThemeMenu(fileMenu); @@ -2080,14 +2076,14 @@ void VMainWindow::updateActionsStateFromTab(const VEditTab *p_tab) && file->getType() == FileType::Orphan && dynamic_cast(file)->isSystemFile(); - m_printAct->setEnabled(file && file->getDocType() == DocType::Markdown); - updateEditReadAct(p_tab); + m_discardAct->setEnabled(file && editMode && p_tab->isModified()); saveNoteAct->setEnabled(file && editMode && file->isModifiable()); deleteNoteAct->setEnabled(file && file->getType() == FileType::Note); noteInfoAct->setEnabled(file && !systemFile); + m_attachmentBtn->setEnabled(file && file->getType() == FileType::Note); m_headingBtn->setEnabled(file && editMode); @@ -2569,6 +2565,7 @@ void VMainWindow::shortcutsHelp() void VMainWindow::printNote() { + /* if (m_printer || !m_curFile || m_curFile->getDocType() != DocType::Markdown) { @@ -2600,6 +2597,7 @@ void VMainWindow::printNote() delete m_printer; m_printer = NULL; } + */ } QAction *VMainWindow::newAction(const QIcon &p_icon, @@ -2887,7 +2885,7 @@ bool VMainWindow::discardAndReadByCaptain(void *p_target, void *p_data) Q_UNUSED(p_data); VMainWindow *obj = static_cast(p_target); if (obj->m_curTab) { - obj->m_discardExitAct->trigger(); + obj->m_discardAct->trigger(); obj->m_curTab->setFocus(); return false; @@ -3267,56 +3265,8 @@ void VMainWindow::toggleEditReadMode() } } -void VMainWindow::updateEditReadAct(const VEditTab *p_tab) -{ - static QIcon editIcon = VIconUtils::toolButtonIcon(":/resources/icons/edit_note.svg"); - static QString editText; - static QIcon readIcon = VIconUtils::toolButtonIcon(":/resources/icons/save_exit.svg"); - static QString readText; - - if (editText.isEmpty()) { - QString keySeq = g_config->getShortcutKeySequence("EditReadNote"); - QKeySequence seq(keySeq); - if (!seq.isEmpty()) { - QString shortcutText = VUtils::getShortcutText(keySeq); - editText = tr("Edit\t%1").arg(shortcutText); - readText = tr("Save Changes And Read\t%1").arg(shortcutText); - - m_editReadAct->setShortcut(seq); - } else { - editText = tr("Edit"); - readText = tr("Save Changes And Read"); - } - } - - if (!p_tab || !p_tab->isEditMode()) { - // Edit. - m_editReadAct->setIcon(editIcon); - m_editReadAct->setText(editText); - m_editReadAct->setStatusTip(tr("Edit current note")); - - m_discardExitAct->setEnabled(false); - } else { - // Read. - m_editReadAct->setIcon(readIcon); - m_editReadAct->setText(readText); - m_editReadAct->setStatusTip(tr("Save changes and exit edit mode")); - - m_discardExitAct->setEnabled(true); - } - - m_editReadAct->setEnabled(p_tab); -} - void VMainWindow::handleExportAct() { - VExportDialog dialog(m_notebookSelector->currentNotebook(), - m_dirTree->currentDirectory(), - m_curFile, - m_cart, - g_config->getMdConverterType(), - this); - dialog.exec(); } VNotebook *VMainWindow::getCurrentNotebook() const @@ -3601,6 +3551,7 @@ void VMainWindow::setupFileListSplitOut(bool p_enabled) void VMainWindow::collectUserStat() const { + /* // One request per day. auto lastCheckDate = g_config->getLastUserTrackDate(); if (lastCheckDate != QDate::currentDate()) { @@ -3620,6 +3571,7 @@ void VMainWindow::collectUserStat() const } QTimer::singleShot(30 * 60 * 1000, this, SLOT(collectUserStat())); + */ } void VMainWindow::promptForVNoteRestart() diff --git a/src/vmainwindow.h b/src/vmainwindow.h index 2da51331..48dfbe43 100644 --- a/src/vmainwindow.h +++ b/src/vmainwindow.h @@ -319,8 +319,6 @@ private: void initThemeMenu(QMenu *p_emnu); - void updateEditReadAct(const VEditTab *p_tab); - void initUniversalEntry(); void setMenuBarVisible(bool p_visible); @@ -421,21 +419,14 @@ private: QAction *deleteNoteAct; - // Toggle read and edit note. - QAction *m_editReadAct; - QAction *saveNoteAct; - QAction *m_discardExitAct; + QAction *m_discardAct; QAction *expandViewAct; QAction *m_importNoteAct; - QAction *m_printAct; - - QAction *m_exportAct; - QAction *m_findReplaceAct; QAction *m_findNextAct; diff --git a/src/vmathjaxpreviewhelper.cpp b/src/vmathjaxpreviewhelper.cpp index 127a4827..06bf371f 100644 --- a/src/vmathjaxpreviewhelper.cpp +++ b/src/vmathjaxpreviewhelper.cpp @@ -1,10 +1,10 @@ #include "vmathjaxpreviewhelper.h" -#include -#include +// #include +// #include #include "utils/vutils.h" -#include "vmathjaxwebdocument.h" +// #include "vmathjaxwebdocument.h" #include "vconfigmanager.h" extern VConfigManager *g_config; @@ -14,7 +14,7 @@ VMathJaxPreviewHelper::VMathJaxPreviewHelper(QWidget *p_parentWidget, QObject *p m_parentWidget(p_parentWidget), m_initialized(false), m_nextID(0), - m_webView(NULL), + // m_webView(NULL), m_webReady(false) { } @@ -30,6 +30,7 @@ void VMathJaxPreviewHelper::doInit() m_initialized = true; + /* m_webView = new QWebEngineView(m_parentWidget); connect(m_webView, &QWebEngineView::loadFinished, this, [this]() { @@ -79,6 +80,7 @@ void VMathJaxPreviewHelper::doInit() QUrl baseUrl(QUrl::fromLocalFile(g_config->getDocumentPathOrHomePath() + QDir::separator())); m_webView->setHtml(VUtils::generateMathJaxPreviewTemplate(), baseUrl); m_webView->setEnabled(true); + */ } void VMathJaxPreviewHelper::previewMathJax(int p_identifier, @@ -88,18 +90,7 @@ void VMathJaxPreviewHelper::previewMathJax(int p_identifier, { init(); - if (!m_webReady) { - auto func = std::bind(&VMathJaxWebDocument::previewMathJax, - m_webDoc, - p_identifier, - p_id, - p_timeStamp, - p_text, - false); - m_pendingFunc.append(func); - } else { - m_webDoc->previewMathJax(p_identifier, p_id, p_timeStamp, p_text, false); - } + emit mathjaxPreviewResultReady(p_identifier, p_id, p_timeStamp, "png", ""); } void VMathJaxPreviewHelper::previewMathJaxFromHtml(int p_identifier, @@ -109,18 +100,7 @@ void VMathJaxPreviewHelper::previewMathJaxFromHtml(int p_identifier, { init(); - if (!m_webReady) { - auto func = std::bind(&VMathJaxWebDocument::previewMathJax, - m_webDoc, - p_identifier, - p_id, - p_timeStamp, - p_html, - true); - m_pendingFunc.append(func); - } else { - m_webDoc->previewMathJax(p_identifier, p_id, p_timeStamp, p_html, true); - } + emit mathjaxPreviewResultReady(p_identifier, p_id, p_timeStamp, "png", ""); } void VMathJaxPreviewHelper::previewDiagram(int p_identifier, @@ -131,16 +111,5 @@ void VMathJaxPreviewHelper::previewDiagram(int p_identifier, { init(); - if (!m_webReady) { - auto func = std::bind(&VMathJaxWebDocument::previewDiagram, - m_webDoc, - p_identifier, - p_id, - p_timeStamp, - p_lang, - p_text); - m_pendingFunc.append(func); - } else { - m_webDoc->previewDiagram(p_identifier, p_id, p_timeStamp, p_lang, p_text); - } + emit diagramPreviewResultReady(p_identifier, p_id, p_timeStamp, "png", ""); } diff --git a/src/vmathjaxpreviewhelper.h b/src/vmathjaxpreviewhelper.h index 04e0b1ca..0a6d423e 100644 --- a/src/vmathjaxpreviewhelper.h +++ b/src/vmathjaxpreviewhelper.h @@ -7,8 +7,8 @@ #include "vconstants.h" -class QWebEngineView; -class VMathJaxWebDocument; +// class QWebEngineView; +// class VMathJaxWebDocument; class QWidget; typedef std::function PendingFunc; @@ -67,9 +67,9 @@ private: int m_nextID; - QWebEngineView *m_webView; + // QWebEngineView *m_webView; - VMathJaxWebDocument *m_webDoc; + // VMathJaxWebDocument *m_webDoc; bool m_webReady; diff --git a/src/vmathjaxwebdocument.cpp b/src/vmathjaxwebdocument.cpp deleted file mode 100644 index 6f2d87ce..00000000 --- a/src/vmathjaxwebdocument.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "vmathjaxwebdocument.h" - -#include - -VMathJaxWebDocument::VMathJaxWebDocument(QObject *p_parent) - : QObject(p_parent) -{ -} - -void VMathJaxWebDocument::previewMathJax(int p_identifier, - int p_id, - TimeStamp p_timeStamp, - const QString &p_text, - bool p_isHtml) -{ - emit requestPreviewMathJax(p_identifier, p_id, p_timeStamp, p_text, p_isHtml); -} - -void VMathJaxWebDocument::mathjaxResultReady(int p_identifier, - int p_id, - unsigned long long p_timeStamp, - const QString &p_format, - const QString &p_data) -{ - emit mathjaxPreviewResultReady(p_identifier, p_id, p_timeStamp, p_format, p_data); -} - -void VMathJaxWebDocument::diagramResultReady(int p_identifier, - int p_id, - unsigned long long p_timeStamp, - const QString &p_format, - const QString &p_data) -{ - emit diagramPreviewResultReady(p_identifier, p_id, p_timeStamp, p_format, p_data); -} - -void VMathJaxWebDocument::previewDiagram(int p_identifier, - int p_id, - TimeStamp p_timeStamp, - const QString &p_lang, - const QString &p_text) -{ - emit requestPreviewDiagram(p_identifier, - p_id, - p_timeStamp, - p_lang, - p_text); -} - -void VMathJaxWebDocument::setLog(const QString &p_log) -{ - qDebug() << "JS:" << p_log; -} - diff --git a/src/vmathjaxwebdocument.h b/src/vmathjaxwebdocument.h deleted file mode 100644 index 7216dfdd..00000000 --- a/src/vmathjaxwebdocument.h +++ /dev/null @@ -1,69 +0,0 @@ -#ifndef VMATHJAXWEBDOCUMENT_H -#define VMATHJAXWEBDOCUMENT_H - -#include - -#include "vconstants.h" - -class VMathJaxWebDocument : public QObject -{ - Q_OBJECT -public: - explicit VMathJaxWebDocument(QObject *p_parent = nullptr); - - void previewMathJax(int p_identifier, - int p_id, - TimeStamp p_timeStamp, - const QString &p_text, - bool p_isHtml); - - void previewDiagram(int p_identifier, - int p_id, - TimeStamp p_timeStamp, - const QString &p_lang, - const QString &p_text); - -public slots: - // Will be called in the HTML side - - void mathjaxResultReady(int p_identifier, - int p_id, - unsigned long long p_timeStamp, - const QString &p_format, - const QString &p_data); - - void diagramResultReady(int p_identifier, - int p_id, - unsigned long long p_timeStamp, - const QString &p_format, - const QString &p_data); - - void setLog(const QString &p_log); - -signals: - void requestPreviewMathJax(int p_identifier, - int p_id, - unsigned long long p_timeStamp, - const QString &p_text, - bool p_isHtml); - - void requestPreviewDiagram(int p_identifier, - int p_id, - unsigned long long p_timeStamp, - const QString &p_lang, - const QString &p_text); - - void mathjaxPreviewResultReady(int p_identifier, - int p_id, - TimeStamp p_timeStamp, - const QString &p_format, - const QString &p_data); - - void diagramPreviewResultReady(int p_identifier, - int p_id, - TimeStamp p_timeStamp, - const QString &p_format, - const QString &p_data); -}; - -#endif // VMATHJAXWEBDOCUMENT_H diff --git a/src/vmdeditor.cpp b/src/vmdeditor.cpp index 48482bad..fddc5ea3 100644 --- a/src/vmdeditor.cpp +++ b/src/vmdeditor.cpp @@ -360,9 +360,11 @@ void VMdEditor::contextMenuEvent(QContextMenuEvent *p_event) pasteAct = getActionByObjectName(actions, "edit-paste"); } + /* if (copyAct && copyAct->isEnabled()) { initCopyAsMenu(copyAct, menu.data()); } + */ if (pasteAct && pasteAct->isEnabled()) { QClipboard *clipboard = QApplication::clipboard(); @@ -371,9 +373,11 @@ void VMdEditor::contextMenuEvent(QContextMenuEvent *p_event) initPasteAsBlockQuoteMenu(pasteAct, menu.data()); } + /* if (mimeData->hasHtml()) { initPasteAfterParseMenu(pasteAct, menu.data()); } + */ QAction *pptAct = new QAction(tr("Paste As Plain Text"), menu.data()); VUtils::fixTextWithShortcut(pptAct, "PastePlainText"); @@ -387,44 +391,6 @@ void VMdEditor::contextMenuEvent(QContextMenuEvent *p_event) if (!textCursor().hasSelection()) { initLinkAndPreviewMenu(firstAct, menu.data(), p_event->pos()); - QAction *saveExitAct = new QAction(VIconUtils::menuIcon(":/resources/icons/save_exit.svg"), - tr("&Save Changes And Read"), - menu.data()); - saveExitAct->setToolTip(tr("Save changes and exit edit mode")); - connect(saveExitAct, &QAction::triggered, - this, [this]() { - emit m_object->saveAndRead(); - }); - - QAction *discardExitAct = new QAction(VIconUtils::menuIcon(":/resources/icons/discard_exit.svg"), - tr("&Discard Changes And Read"), - menu.data()); - discardExitAct->setToolTip(tr("Discard changes and exit edit mode")); - connect(discardExitAct, &QAction::triggered, - this, [this]() { - emit m_object->discardAndRead(); - }); - - VMdTab *mdtab = dynamic_cast(m_editTab); - if (mdtab) { - QAction *toggleLivePreviewAct = new QAction(tr("Live Preview For Graphs"), menu.data()); - toggleLivePreviewAct->setToolTip(tr("Toggle live preview panel for graphs")); - VUtils::fixTextWithCaptainShortcut(toggleLivePreviewAct, "LivePreview"); - connect(toggleLivePreviewAct, &QAction::triggered, - this, [mdtab]() { - mdtab->toggleLivePreview(); - }); - - menu->insertAction(firstAct, toggleLivePreviewAct); - menu->insertAction(toggleLivePreviewAct, discardExitAct); - menu->insertAction(discardExitAct, saveExitAct); - menu->insertSeparator(toggleLivePreviewAct); - } else { - menu->insertAction(firstAct, discardExitAct); - menu->insertAction(discardExitAct, saveExitAct); - menu->insertSeparator(discardExitAct); - } - if (firstAct) { menu->insertSeparator(firstAct); } diff --git a/src/vmdtab.cpp b/src/vmdtab.cpp index 800a3716..a10543f9 100644 --- a/src/vmdtab.cpp +++ b/src/vmdtab.cpp @@ -1,13 +1,13 @@ #include -#include +// #include #include #include -#include +// #include #include "vmdtab.h" #include "vdocument.h" #include "vnote.h" #include "utils/vutils.h" -#include "vpreviewpage.h" +// #include "vpreviewpage.h" #include "pegmarkdownhighlighter.h" #include "vconfigmanager.h" #include "vmarkdownconverter.h" @@ -34,7 +34,7 @@ VMdTab::VMdTab(VFile *p_file, VEditArea *p_editArea, OpenFileMode p_mode, QWidget *p_parent) : VEditTab(p_file, p_editArea, p_parent), m_editor(NULL), - m_webViewer(NULL), + // m_webViewer(NULL), m_document(NULL), m_mdConType(g_config->getMdConverterType()), m_enableHeadingSequence(false), @@ -79,6 +79,8 @@ VMdTab::VMdTab(VFile *p_file, VEditArea *p_editArea, m_livePreviewTimer->setInterval(500); connect(m_livePreviewTimer, &QTimer::timeout, this, [this]() { + Q_ASSERT(false); + /* QString text = m_webViewer->selectedText().trimmed(); if (text.isEmpty()) { return; @@ -92,6 +94,7 @@ VMdTab::VMdTab(VFile *p_file, VEditArea *p_editArea, info.m_startPos, info.m_endPos); } + */ }); QTimer::singleShot(50, this, [this, p_mode]() { @@ -121,6 +124,7 @@ void VMdTab::setupUI() void VMdTab::showFileReadMode() { + Q_ASSERT(false); m_isEditMode = false; // Will recover the header when web side is ready. @@ -288,13 +292,11 @@ bool VMdTab::closeFile(bool p_forced) Q_ASSERT(m_editor); m_editor->reloadFile(); m_editor->endEdit(); - - showFileReadMode(); - } else { - readFile(); } - return !m_isEditMode; + readFile(); + + return !isModified(); } void VMdTab::editFile() @@ -353,7 +355,7 @@ void VMdTab::readFile(bool p_discard) m_editor->endEdit(); } - showFileReadMode(); + showFileEditMode(); } bool VMdTab::saveFile() @@ -434,6 +436,7 @@ void VMdTab::discardAndRead() void VMdTab::setupMarkdownViewer() { + /* m_webViewer = new VWebView(m_file, this); connect(m_webViewer, &VWebView::editNote, this, &VMdTab::editFile); @@ -455,10 +458,12 @@ void VMdTab::setupMarkdownViewer() // Avoid white flash before loading content. // Setting Qt::transparent will force GrayScale antialias rendering. page->setBackgroundColor(g_config->getBaseBackground()); + */ - m_document = new VDocument(m_file, m_webViewer); + m_document = new VDocument(m_file, this); m_documentID = m_document->registerIdentifier(); + /* QWebChannel *channel = new QWebChannel(m_webViewer); channel->registerObject(QStringLiteral("content"), m_document); connect(m_document, &VDocument::tocChanged, @@ -515,6 +520,7 @@ void VMdTab::setupMarkdownViewer() m_file->getBaseUrl()); m_splitter->addWidget(m_webViewer); + */ } void VMdTab::setupMarkdownEditor() @@ -766,8 +772,9 @@ void VMdTab::nextMatch(const QString &p_text, uint p_options, bool p_forward) void VMdTab::findTextInWebView(const QString &p_text, uint p_options, bool /* p_peek */, bool p_forward) { - V_ASSERT(m_webViewer); + V_ASSERT(false); + /* QWebEnginePage::FindFlags flags; if (p_options & FindOption::CaseSensitive) { flags |= QWebEnginePage::FindCaseSensitively; @@ -778,6 +785,7 @@ void VMdTab::findTextInWebView(const QString &p_text, uint p_options, } m_webViewer->findText(p_text, flags); + */ } QString VMdTab::getSelectedText() const @@ -787,15 +795,19 @@ QString VMdTab::getSelectedText() const QTextCursor cursor = m_editor->textCursor(); return cursor.selectedText(); } else { - return m_webViewer->selectedText(); + Q_ASSERT(false); + // return m_webViewer->selectedText(); + return QString(); } } void VMdTab::clearSearchedWordHighlight() { + /* if (m_webViewer) { m_webViewer->findText(""); } + */ if (m_editor) { m_editor->clearSearchedWordHighlight(); @@ -804,8 +816,7 @@ void VMdTab::clearSearchedWordHighlight() void VMdTab::handleWebKeyPressed(int p_key, bool p_ctrl, bool p_shift, bool p_meta) { - V_ASSERT(m_webViewer); - + Q_ASSERT(false); #if defined(Q_OS_MACOS) || defined(Q_OS_MAC) bool macCtrl = p_meta; #else @@ -841,7 +852,7 @@ void VMdTab::handleWebKeyPressed(int p_key, bool p_ctrl, bool p_shift, bool p_me case 48: if (p_ctrl || macCtrl) { // Recover zoom. - m_webViewer->setZoomFactor(1); + // m_webViewer->setZoomFactor(1); } break; @@ -898,6 +909,8 @@ void VMdTab::zoom(bool p_zoomIn, qreal p_step) void VMdTab::zoomWebPage(bool p_zoomIn, qreal p_step) { + Q_ASSERT(false); + /* V_ASSERT(m_webViewer); qreal curFactor = m_webViewer->zoomFactor(); @@ -909,11 +922,7 @@ void VMdTab::zoomWebPage(bool p_zoomIn, qreal p_step) } m_webViewer->setZoomFactor(newFactor); -} - -VWebView *VMdTab::getWebViewer() const -{ - return m_webViewer; + */ } MarkdownConverterType VMdTab::getMarkdownConverterType() const @@ -925,7 +934,8 @@ void VMdTab::focusChild() { switch (m_mode) { case Mode::Read: - m_webViewer->setFocus(); + Q_ASSERT(false); + // m_webViewer->setFocus(); break; case Mode::Edit: @@ -933,10 +943,11 @@ void VMdTab::focusChild() break; case Mode::EditPreview: + Q_ASSERT(false); if (m_editor->isVisible()) { m_editor->setFocus(); } else { - m_webViewer->setFocus(); + // m_webViewer->setFocus(); } break; @@ -1158,7 +1169,7 @@ void VMdTab::reload() // Reload web viewer. m_ready &= ~TabReady::ReadMode; - m_webViewer->reload(); + // m_webViewer->reload(); if (!m_isEditMode) { VUtils::sleepWait(500); @@ -1287,8 +1298,10 @@ void VMdTab::handleFileOrDirectoryChange(bool p_isFile, UpdateAction p_act) { // Reload the web view with new base URL. m_headerFromEditMode = m_currentHeader; + /* m_webViewer->setHtml(VUtils::generateHtmlTemplate(m_mdConType), m_file->getBaseUrl()); + */ if (m_editor) { m_editor->updateInitAndInsertedImages(p_isFile, p_act); @@ -1436,7 +1449,8 @@ bool VMdTab::executeVimCommandInWebView(const QString &p_cmd) msg = tr("Quit"); } else if (p_cmd == "nohlsearch" || p_cmd == "noh") { // :nohlsearch, clear highlight search. - m_webViewer->findText(""); + Q_ASSERT(false); + // m_webViewer->findText(""); } else { validCommand = false; } @@ -1450,6 +1464,7 @@ bool VMdTab::executeVimCommandInWebView(const QString &p_cmd) return validCommand; } +/* void VMdTab::handleDownloadRequested(QWebEngineDownloadItem *p_item) { connect(p_item, &QWebEngineDownloadItem::stateChanged, @@ -1473,6 +1488,7 @@ void VMdTab::handleDownloadRequested(QWebEngineDownloadItem *p_item) void VMdTab::handleSavePageRequested() { + Q_ASSERT(false); static QString lastPath = g_config->getDocumentPathOrHomePath(); QStringList filters; @@ -1500,8 +1516,9 @@ void VMdTab::handleSavePageRequested() emit statusMessage(tr("Saving page to %1").arg(fileName)); - m_webViewer->page()->save(fileName, format); + // m_webViewer->page()->save(fileName, format); } +*/ VWordCountInfo VMdTab::fetchWordCountInfo(bool p_editMode) const { @@ -1527,7 +1544,8 @@ void VMdTab::setCurrentMode(Mode p_mode) return; } - qreal factor = m_webViewer->zoomFactor(); + // qreal factor = m_webViewer->zoomFactor(); + qreal factor = 1.0; if (m_mode == Mode::Read) { m_readWebViewState->m_zoomFactor = factor; } else if (m_mode == Mode::EditPreview) { @@ -1539,12 +1557,13 @@ void VMdTab::setCurrentMode(Mode p_mode) switch (p_mode) { case Mode::Read: + Q_ASSERT(false); if (m_editor) { m_editor->hide(); } - m_webViewer->setInPreview(false); - m_webViewer->show(); + // m_webViewer->setInPreview(false); + // m_webViewer->show(); // Fix the bug introduced by 051088be31dbffa3c04e2d382af15beec40d5fdb // which replace QStackedLayout with QSplitter. @@ -1554,7 +1573,7 @@ void VMdTab::setCurrentMode(Mode p_mode) m_readWebViewState.reset(new WebViewState()); m_readWebViewState->m_zoomFactor = factor; } else if (factor != m_readWebViewState->m_zoomFactor) { - m_webViewer->setZoomFactor(m_readWebViewState->m_zoomFactor); + // m_webViewer->setZoomFactor(m_readWebViewState->m_zoomFactor); } m_document->setPreviewEnabled(false); @@ -1562,7 +1581,7 @@ void VMdTab::setCurrentMode(Mode p_mode) case Mode::Edit: m_document->muteWebView(true); - m_webViewer->hide(); + // m_webViewer->hide(); m_editor->show(); QCoreApplication::sendPostedEvents(); @@ -1571,9 +1590,10 @@ void VMdTab::setCurrentMode(Mode p_mode) case Mode::EditPreview: Q_ASSERT(m_editor); + Q_ASSERT(false); m_document->muteWebView(true); - m_webViewer->setInPreview(true); - m_webViewer->show(); + // m_webViewer->setInPreview(true); + // m_webViewer->show(); m_editor->show(); QCoreApplication::sendPostedEvents(); @@ -1597,7 +1617,7 @@ void VMdTab::setCurrentMode(Mode p_mode) newSizes.append(b); m_splitter->setSizes(newSizes); } else if (factor != m_previewWebViewState->m_zoomFactor) { - m_webViewer->setZoomFactor(m_previewWebViewState->m_zoomFactor); + // m_webViewer->setZoomFactor(m_previewWebViewState->m_zoomFactor); } m_document->setPreviewEnabled(true); @@ -1650,9 +1670,10 @@ bool VMdTab::expandRestorePreviewArea() return false; } + Q_ASSERT(false); if (m_editor->isVisible()) { m_editor->hide(); - m_webViewer->setFocus(); + // m_webViewer->setFocus(); } else { m_editor->show(); m_editor->setFocus(); diff --git a/src/vmdtab.h b/src/vmdtab.h index 7de0d442..51b8724e 100644 --- a/src/vmdtab.h +++ b/src/vmdtab.h @@ -9,12 +9,12 @@ #include "vmarkdownconverter.h" #include "vconfigmanager.h" -class VWebView; +// class VWebView; class VDocument; class VMdEditor; class VInsertSelector; class QTimer; -class QWebEngineDownloadItem; +// class QWebEngineDownloadItem; class QSplitter; class VLivePreviewHelper; class VMathJaxInplacePreviewHelper; @@ -69,7 +69,7 @@ public: void clearSearchedWordHighlight() Q_DECL_OVERRIDE; - VWebView *getWebViewer() const; + // VWebView *getWebViewer() const; VMdEditor *getEditor() const; @@ -153,10 +153,10 @@ private slots: void restoreFromTabInfo(); // Handle download request from web page. - void handleDownloadRequested(QWebEngineDownloadItem *p_item); + // void handleDownloadRequested(QWebEngineDownloadItem *p_item); // Handle save page request. - void handleSavePageRequested(); + // void handleSavePageRequested(); // Selection changed in web. void handleWebSelectionChanged(); @@ -247,7 +247,7 @@ private: bool previewExpanded() const; VMdEditor *m_editor; - VWebView *m_webViewer; + // VWebView *m_webViewer; VDocument *m_document; MarkdownConverterType m_mdConType; diff --git a/src/vnote.cpp b/src/vnote.cpp index 09b90bb2..1b292690 100644 --- a/src/vnote.cpp +++ b/src/vnote.cpp @@ -232,7 +232,7 @@ QString VNote::getNavigationLabelStyle(const QString &p_str, bool p_small) const font.setBold(true); QFontMetrics fm(font); pxWidth = fm.width(p_str); - pxHeight = fm.capHeight() + 5; + pxHeight = fm.height() + 5; lastLen = p_str.size(); } diff --git a/src/vpreviewpage.cpp b/src/vpreviewpage.cpp deleted file mode 100644 index 81e67ede..00000000 --- a/src/vpreviewpage.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "vpreviewpage.h" - -#include - -#include "vmainwindow.h" - -extern VMainWindow *g_mainWin; - -VPreviewPage::VPreviewPage(QWidget *parent) : QWebEnginePage(parent) -{ - -} - -bool VPreviewPage::acceptNavigationRequest(const QUrl &p_url, - QWebEnginePage::NavigationType p_type, - bool p_isMainFrame) -{ - Q_UNUSED(p_type); - - if (p_url.isLocalFile()) { - QString filePath = p_url.toLocalFile(); - if (g_mainWin->tryOpenInternalFile(filePath)) { - return false; - } - } else if (!p_isMainFrame) { - return true; - } else if (p_url.scheme() == "data") { - // Qt 5.12 will trigger this when calling QWebEngineView.setHtml(). - return true; - } - - QDesktopServices::openUrl(p_url); - return false; -} diff --git a/src/vpreviewpage.h b/src/vpreviewpage.h deleted file mode 100644 index eca8a25c..00000000 --- a/src/vpreviewpage.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef VPREVIEWPAGE_H -#define VPREVIEWPAGE_H - -#include - -class VPreviewPage : public QWebEnginePage -{ - Q_OBJECT -public: - explicit VPreviewPage(QWidget *parent = 0); - -protected: - bool acceptNavigationRequest(const QUrl &p_url, - NavigationType p_type, - bool p_isMainFrame); -}; - -#endif // VPREVIEWPAGE_H diff --git a/src/vsearchconfig.h b/src/vsearchconfig.h index 25668218..49b42fd7 100644 --- a/src/vsearchconfig.h +++ b/src/vsearchconfig.h @@ -505,7 +505,7 @@ struct VSearchResultItem VSearchResultItem::MatchType p_matchType, const QString &p_text, const QString &p_path, - const QSharedPointer &p_config = nullptr) + const QSharedPointer &p_config = QSharedPointer()) : m_type(p_type), m_matchType(p_matchType), m_text(p_text), diff --git a/src/vwebview.cpp b/src/vwebview.cpp index 2524ba1e..bd059fd4 100644 --- a/src/vwebview.cpp +++ b/src/vwebview.cpp @@ -27,7 +27,7 @@ extern VWebUtils *g_webUtils; static const QString c_ClipboardPropertyMark = "CopiedImageURLAltered"; VWebView::VWebView(VFile *p_file, QWidget *p_parent) - : QWebEngineView(p_parent), + : QTextBrowser(p_parent), m_file(p_file), m_copyImageUrlActionHooked(false), m_afterCopyImage(false), @@ -481,3 +481,14 @@ void VWebView::initPreviewTunnelMenu(QAction *p_before, QMenu *p_menu) p_menu->insertMenu(p_before, subMenu); } + +QString VWebView::selectedText() const +{ + return QString(); +} + +void VWebView::setHtml(const QString &p_html, const QUrl &p_baseUrl) +{ + QTextBrowser::setHtml(p_html); + setSource(p_baseUrl); +} diff --git a/src/vwebview.h b/src/vwebview.h index 08988bc6..06b2c5a4 100644 --- a/src/vwebview.h +++ b/src/vwebview.h @@ -1,14 +1,14 @@ #ifndef VWEBVIEW_H #define VWEBVIEW_H -#include +#include #include #include class VFile; class QMenu; -class VWebView : public QWebEngineView +class VWebView : public QTextBrowser { Q_OBJECT public: @@ -17,6 +17,10 @@ public: void setInPreview(bool p_preview); + QString selectedText() const; + + void setHtml(const QString &p_html, const QUrl &p_baseUrl = QUrl()); + signals: void editNote();