Git commit 31c9b0bec3386c8c66416e33ec30f47d7ba623d7 by Dawid Wrobel, on behalf of Dawid Wróbel. Committed on 28/04/2021 at 00:08. Pushed by wrobelda into branch 'master'.
Revisit Check Printing plugin - Use QTextEdit instead of QWebkit/WebEngine - embed the default template using Qt Resources - allow overriding the default template with a custom one - fix settings dialog's window title - move tempaltes to their own subfolder - update the manual M +14 -10 doc/details-settings.docbook M +3 -7 kmymoney/plugins/checkprinting/CMakeLists.txt M +14 -26 kmymoney/plugins/checkprinting/checkprinting.cpp M +3 -0 kmymoney/plugins/checkprinting/checkprinting.qrc M +75 -19 kmymoney/plugins/checkprinting/kcm_checkprinting.cpp M +14 -13 kmymoney/plugins/checkprinting/kcm_checkprinting.h M +6 -1 kmymoney/plugins/checkprinting/pluginsettings.kcfg M +1 -0 kmymoney/plugins/checkprinting/pluginsettings.kcfgc M +7 -7 kmymoney/plugins/checkprinting/pluginsettingsdecl.ui R +0 -0 kmymoney/plugins/checkprinting/templates/check-3-part-template.html [from: kmymoney/plugins/checkprinting/check-3-part-template.html - 100% similarity] R +0 -0 kmymoney/plugins/checkprinting/templates/check_template.html [from: kmymoney/plugins/checkprinting/check_template.html - 100% similarity] R +0 -0 kmymoney/plugins/checkprinting/templates/check_template_green_linen.html [from: kmymoney/plugins/checkprinting/check_template_green_linen.html - 100% similarity] https://invent.kde.org/office/kmymoney/commit/31c9b0bec3386c8c66416e33ec30f47d7ba623d7 diff --git a/doc/details-settings.docbook b/doc/details-settings.docbook index 3a69cc7ca..fd9b52586 100644 --- a/doc/details-settings.docbook +++ b/doc/details-settings.docbook @@ -908,23 +908,27 @@ of accounts.</para> is included with the source of &kmymoney;, and should always be enabled. It allows printing of a check based on the data from a selected transaction, with the layout controlled by an &HTML; template. You need to use a - template which is matched to your pre-printed checks. When you click on the - <guibutton>configure</guibutton> (&configicon;) button, the <guilabel>Check - printing</guilabel> configuration dialog is displayed. At the top is a text box - for the path to the selected template. To the right of that is a button which - brings up a file chooser, to select an alternate template file. + template which is matched to your pre-printed checks. A default, built-in + template is provided. You may also choose a custom template. To do so, click on + the <guibutton>configure</guibutton> (&configicon;) button to get the + <guilabel>Check printing</guilabel> configuration dialog displayed. At the top + you can check the box to use a custom template. To the right of that is a + button which brings up a file chooser, to select an alternate template file. </para> <para> A template file is an &HTML; file, in which specific strings are used to reference the fields from the transaction and from the current account and - institution to show where they will be printed on the check. &kmymoney; is - shipped with some sample template files. You should either be able to use one - of them, or modify one to suit your needs. When the default template is - selected, the configuration dialog will show you the folder which contains - the sample files. + institution to show where they will be printed on the check. &kmymoney; + ships with a default, built-in template. At <ulink url="https://invent.kde.org/office/kmymoney/-/blob/master/kmymoney/plugins/checkprinting/templates/">this</ulink> + address you can find the HTML source file of default template, as well as some + additional templates. You should either be able to use one of them, or modify + one to suit your needs. Note that only a <emphasis>subset</emphasis> of the + HTML standard is supported — the detailed documentation on that can be found + <ulink url="https://doc.qt.io/qt-5/richtext-html-subset.html">here</ulink>. </para> + <!-- info from source code on substitution variables // data about the user checkHTML.replace("$OWNER_NAME", file->user().name()); diff --git a/kmymoney/plugins/checkprinting/CMakeLists.txt b/kmymoney/plugins/checkprinting/CMakeLists.txt index 1a3c92e7f..9dce1e4a6 100644 --- a/kmymoney/plugins/checkprinting/CMakeLists.txt +++ b/kmymoney/plugins/checkprinting/CMakeLists.txt @@ -36,12 +36,6 @@ else(ENABLE_WEBENGINE) target_link_libraries(checkprinting KF5::WebKit) endif(ENABLE_WEBENGINE) -install(FILES check_template.html - DESTINATION "${DATA_INSTALL_DIR}/checkprinting") - -install(FILES check_template_green_linen.html - DESTINATION "${DATA_INSTALL_DIR}/checkprinting") - # the KCM module set(kcm_checkprinting_PART_SRCS @@ -64,7 +58,9 @@ set_target_properties(kcm_checkprinting PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}") -target_link_libraries(kcm_checkprinting +target_link_libraries( + kcm_checkprinting + kmm_url Qt5::PrintSupport KF5::I18n KF5::ConfigWidgets diff --git a/kmymoney/plugins/checkprinting/checkprinting.cpp b/kmymoney/plugins/checkprinting/checkprinting.cpp index d42f63d0c..6688f5815 100644 --- a/kmymoney/plugins/checkprinting/checkprinting.cpp +++ b/kmymoney/plugins/checkprinting/checkprinting.cpp @@ -12,11 +12,7 @@ #include <QAction> #include <QFile> #include <QDialog> -#ifdef ENABLE_WEBENGINE -#include <QWebEngineView> -#else -#include <KWebView> -#endif +#include <QTextDocument> #include <QStandardPaths> // KDE includes @@ -76,31 +72,27 @@ struct CheckPrinting::Private { void readCheckTemplate() { - QString checkTemplateHTMLPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "checkprinting/check_template.html"); - if (PluginSettings::checkTemplateFile().isEmpty()) { - PluginSettings::setCheckTemplateFile(checkTemplateHTMLPath); - PluginSettings::self()->save(); - } + QFile *checkTemplateHTMLFile = new QFile(PluginSettings::checkTemplateFile()); - QFile checkTemplateHTMLFile(PluginSettings::checkTemplateFile()); - checkTemplateHTMLFile.open(QIODevice::ReadOnly); + if (!PluginSettings::useCustomCheckTemplate || PluginSettings::checkTemplateFile().isEmpty() || !checkTemplateHTMLFile->exists()) + checkTemplateHTMLFile = new QFile(PluginSettings::defaultCheckTemplateFileValue()); + if (!(checkTemplateHTMLFile->open(QIODevice::ReadOnly))) + qDebug() << "Failed to open the template from" << checkTemplateHTMLFile->fileName(); + else + qDebug() << "Template successfully opened from" << checkTemplateHTMLFile->fileName(); - QTextStream stream(&checkTemplateHTMLFile); + QTextStream stream(checkTemplateHTMLFile); m_checkTemplateHTML = stream.readAll(); - checkTemplateHTMLFile.close(); + checkTemplateHTMLFile->close(); } void printCheck(const QString& accountId, const QString& transactionId) { MyMoneyMoneyToWordsConverter converter; -#ifdef ENABLE_WEBENGINE - auto htmlPart = new QWebEngineView(); -#else - auto htmlPart = new KWebView(); -#endif + auto htmlPart = new QTextDocument(); const auto file = MyMoneyFile::instance(); const auto transaction = file->transaction(transactionId); @@ -155,15 +147,10 @@ struct CheckPrinting::Private { } // print the check - htmlPart->setHtml(checkHTML, QUrl("file://")); + htmlPart->setHtml(checkHTML); auto printer = KMyMoneyPrinter::startPrint(); - if (printer != nullptr) { -#ifdef ENABLE_WEBENGINE - htmlPart->page()->print(printer, [=] (bool) {}); -#else + if (printer != nullptr) htmlPart->print(printer); -#endif - } // mark the transaction as printed markAsPrinted(transactionId); @@ -183,6 +170,7 @@ CheckPrinting::CheckPrinting(QObject *parent, const KPluginMetaData &metaData, c // Tell the host application to load my GUI component const auto rcFileName = QLatin1String("checkprinting.rc"); + setXMLFile(rcFileName); // For ease announce that we have been loaded. diff --git a/kmymoney/plugins/checkprinting/checkprinting.qrc b/kmymoney/plugins/checkprinting/checkprinting.qrc index 0fb65cb71..167cba652 100644 --- a/kmymoney/plugins/checkprinting/checkprinting.qrc +++ b/kmymoney/plugins/checkprinting/checkprinting.qrc @@ -2,4 +2,7 @@ <qresource prefix="/kxmlgui5/checkprinting"> <file>checkprinting.rc</file> </qresource> + <qresource prefix="/plugins/checkprinting/"> + <file>templates/check_template.html</file> + </qresource> </RCC> diff --git a/kmymoney/plugins/checkprinting/kcm_checkprinting.cpp b/kmymoney/plugins/checkprinting/kcm_checkprinting.cpp index 15d4b9a5e..4633d8a0d 100644 --- a/kmymoney/plugins/checkprinting/kcm_checkprinting.cpp +++ b/kmymoney/plugins/checkprinting/kcm_checkprinting.cpp @@ -9,46 +9,101 @@ // Qt includes #include <QFrame> -#ifdef ENABLE_WEBENGINE -#include <QWebEngineView> -#else -#include <KWebView> -#endif +#include <QTextEdit> // KDE includes #include <KPluginFactory> #include <KAboutData> +// ---------------------------------------------------------------------------- +// Project Includes + #include "pluginsettings.h" +#include "kcm_checkprinting.h" -PluginSettingsWidget::PluginSettingsWidget(QWidget* parent) : +PluginSettingsWidget::PluginSettingsWidget(QWidget *parent) + : QWidget(parent) { setupUi(this); -#ifdef ENABLE_WEBENGINE - m_checkTemplatePreviewHTMLPart = new QWebEngineView(m_previewFrame); -#else - m_checkTemplatePreviewHTMLPart = new KWebView(m_previewFrame); -#endif + m_checkTemplatePreviewHTMLPart = new QTextEdit(m_previewFrame); QVBoxLayout *layout = new QVBoxLayout; m_previewFrame->setLayout(layout); layout->addWidget(m_checkTemplatePreviewHTMLPart); - connect(kcfg_checkTemplateFile, &KUrlRequester::urlSelected, this, &PluginSettingsWidget::urlSelected); - connect(kcfg_checkTemplateFile, QOverload<const QString&>::of(&KUrlRequester::returnPressed), this, &PluginSettingsWidget::returnPressed); + if (PluginSettings::checkTemplateFile().isEmpty()) { + restoreDefaultSettings(); + } + + connect(kcfg_checkTemplateFile, &KUrlRequester::textChanged, this, &PluginSettingsWidget::textChanged); + connect(kcfg_checkTemplateFile, + &KUrlRequester::urlSelected, + this, + QOverload<const QUrl &>::of(&PluginSettingsWidget::urlSelected)); + connect(kcfg_checkTemplateFile, QOverload<const QString &>::of(&KUrlRequester::returnPressed), this, + QOverload<const QString &>::of(&PluginSettingsWidget::urlSelected)); + connect(kcfg_useCustomCheckTemplate, SIGNAL(toggled(bool)), kcfg_checkTemplateFile, SLOT(setEnabled(bool))); + connect(kcfg_useCustomCheckTemplate, + &QCheckBox::toggled, + this, + QOverload<>::of(&PluginSettingsWidget::urlSelected)); +} + +void PluginSettingsWidget::urlSelected() +{ + if (kcfg_useCustomCheckTemplate->checkState() == Qt::Unchecked + || PluginSettings::checkTemplateFile().isEmpty() + || kcfg_checkTemplateFile->url().isEmpty()) { + urlSelected(QUrl::fromUserInput(PluginSettings::defaultCheckTemplateFileValue())); + } + else { + urlSelected(kcfg_checkTemplateFile->url()); + } +} + +void PluginSettingsWidget::urlSelected(const QString &url) +{ + urlSelected(QUrl::fromUserInput(url)); } void PluginSettingsWidget::urlSelected(const QUrl &url) { - if (!url.isEmpty()) - m_checkTemplatePreviewHTMLPart->load(url); + if (!url.isEmpty()) { + m_checkTemplatePreviewHTMLPart->clear(); + QFile file(url.toLocalFile()); + + if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { + QTextStream stream(&file); + m_checkTemplatePreviewHTMLPart->setHtml(stream.readAll()); + file.close(); + } + } + else { + urlSelected(); + } +} + +void PluginSettingsWidget::textChanged(const QString& text) +{ + // conceal the default "qrc:/" value to avoid confusing regular users + if (text == PluginSettings::defaultCheckTemplateFileValue()) { + kcfg_checkTemplateFile->setText(""); + } +} + +void PluginSettingsWidget::restoreDefaultSettings() const +{ + PluginSettings::setUseCustomCheckTemplate(false); + PluginSettings::setCheckTemplateFile(PluginSettings::defaultCheckTemplateFileValue()); + PluginSettings::self()->save(); } -void PluginSettingsWidget::returnPressed(const QString& url) +PluginSettingsWidget::~PluginSettingsWidget() { - if (!url.isEmpty()) - m_checkTemplatePreviewHTMLPart->load(QUrl::fromUserInput(url)); + if (kcfg_checkTemplateFile->url().isEmpty()) { + restoreDefaultSettings(); + } } KCMCheckPrinting::KCMCheckPrinting(QWidget *parent, const QVariantList& args) @@ -60,7 +115,8 @@ KCMCheckPrinting::KCMCheckPrinting(QWidget *parent, const QVariantList& args) setLayout(layout); layout->addWidget(w); load(); - w->urlSelected(QUrl::fromUserInput(PluginSettings::checkTemplateFile())); + w->urlSelected(PluginSettings::useCustomCheckTemplate() ? PluginSettings::checkTemplateFile() + : PluginSettings::defaultCheckTemplateFileValue()); } KCMCheckPrinting::~KCMCheckPrinting() diff --git a/kmymoney/plugins/checkprinting/kcm_checkprinting.h b/kmymoney/plugins/checkprinting/kcm_checkprinting.h index 18f58c23c..c8cf7e8f0 100644 --- a/kmymoney/plugins/checkprinting/kcm_checkprinting.h +++ b/kmymoney/plugins/checkprinting/kcm_checkprinting.h @@ -1,39 +1,40 @@ /* SPDX-FileCopyrightText: 2009 Cristian Onet onet.cristian @gmail.com + SPDX-FileCopyrightText: 2021 Dawid Wróbel <[email protected]> SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL */ #ifndef KCM_CHECKPRINTING_H #define KCM_CHECKPRINTING_H #include <config-kmymoney.h> +#include "ui_pluginsettingsdecl.h" + +// Override QUrl +#include <misc/kmmurl.h> #include <KCModule> #include <QWidget> -#include "ui_pluginsettingsdecl.h" -#ifdef ENABLE_WEBENGINE -class QWebEngineView; -#else -class KWebView; -#endif +class QTextEdit; +class QTextDocument; class PluginSettingsWidget : public QWidget, public Ui::PluginSettingsDecl { - Q_OBJECT +Q_OBJECT public: explicit PluginSettingsWidget(QWidget* parent = 0); + ~PluginSettingsWidget(); public Q_SLOTS: + void urlSelected(); void urlSelected(const QUrl &url); - void returnPressed(const QString& url); + void urlSelected(const QString& url); + void textChanged(const QString& text); private: -#ifdef ENABLE_WEBENGINE - QWebEngineView *m_checkTemplatePreviewHTMLPart; -#else - KWebView *m_checkTemplatePreviewHTMLPart; -#endif + QTextEdit *m_checkTemplatePreviewHTMLPart; + void restoreDefaultSettings() const; }; class KCMCheckPrinting : public KCModule diff --git a/kmymoney/plugins/checkprinting/pluginsettings.kcfg b/kmymoney/plugins/checkprinting/pluginsettings.kcfg index cab0d10d1..173a48460 100644 --- a/kmymoney/plugins/checkprinting/pluginsettings.kcfg +++ b/kmymoney/plugins/checkprinting/pluginsettings.kcfg @@ -6,8 +6,13 @@ <kcfgfile name="kmymoney/checkprintingrc"/> <group name="General"> - <entry name="checkTemplateFile" type="String"> + <entry name="useCustomCheckTemplate" type="Bool"> + <label>Use custom check template</label> + <default>true</default> + </entry> + <entry name="checkTemplateFile" type="Path"> <label>The check template file which defines the way the printed check will look.</label> + <default>:/plugins/checkprinting/templates/check_template.html</default> </entry> <entry name="printedChecks" type="StringList"> <label>A list containing the id's of already printed checks.</label> diff --git a/kmymoney/plugins/checkprinting/pluginsettings.kcfgc b/kmymoney/plugins/checkprinting/pluginsettings.kcfgc index fb06ac35c..89a56d786 100644 --- a/kmymoney/plugins/checkprinting/pluginsettings.kcfgc +++ b/kmymoney/plugins/checkprinting/pluginsettings.kcfgc @@ -3,4 +3,5 @@ File=pluginsettings.kcfg ClassName=PluginSettings Singleton=true Mutators=true +DefaultValueGetters=true # will create the necessary code for setting those variables diff --git a/kmymoney/plugins/checkprinting/pluginsettingsdecl.ui b/kmymoney/plugins/checkprinting/pluginsettingsdecl.ui index dd4a51e34..09d548e62 100644 --- a/kmymoney/plugins/checkprinting/pluginsettingsdecl.ui +++ b/kmymoney/plugins/checkprinting/pluginsettingsdecl.ui @@ -17,7 +17,7 @@ </sizepolicy> </property> <property name="windowTitle"> - <string>Icalendar export settings</string> + <string>Checking settings</string> </property> <layout class="QVBoxLayout" name="verticalLayout_2"> <item> @@ -25,18 +25,15 @@ <item> <layout class="QHBoxLayout" name="horizontalLayout"> <item> - <widget class="QLabel" name="check_template_file"> + <widget class="QCheckBox" name="kcfg_useCustomCheckTemplate"> <property name="sizePolicy"> - <sizepolicy hsizetype="Fixed" vsizetype="Fixed"> + <sizepolicy hsizetype="Minimum" vsizetype="Fixed"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="text"> - <string>Check template file</string> - </property> - <property name="wordWrap"> - <bool>false</bool> + <string>Use custom check template</string> </property> </widget> </item> @@ -45,6 +42,9 @@ <property name="kcfg_property" stdset="0"> <cstring>text</cstring> </property> + <property name="enabled"> + <bool>false</bool> + </property> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Fixed"> <horstretch>0</horstretch> diff --git a/kmymoney/plugins/checkprinting/check-3-part-template.html b/kmymoney/plugins/checkprinting/templates/check-3-part-template.html similarity index 100% rename from kmymoney/plugins/checkprinting/check-3-part-template.html rename to kmymoney/plugins/checkprinting/templates/check-3-part-template.html diff --git a/kmymoney/plugins/checkprinting/check_template.html b/kmymoney/plugins/checkprinting/templates/check_template.html similarity index 100% rename from kmymoney/plugins/checkprinting/check_template.html rename to kmymoney/plugins/checkprinting/templates/check_template.html diff --git a/kmymoney/plugins/checkprinting/check_template_green_linen.html b/kmymoney/plugins/checkprinting/templates/check_template_green_linen.html similarity index 100% rename from kmymoney/plugins/checkprinting/check_template_green_linen.html rename to kmymoney/plugins/checkprinting/templates/check_template_green_linen.html
