diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ include(KDECMakeSettings) include(ECMMarkNonGuiExecutable) include(ECMAddQch) +include(ECMOptionalAddSubdirectory) ecm_setup_version(PROJECT @@ -39,9 +40,10 @@ if(KSYNTAXHIGHLIGHTING_USE_GUI) find_package(Qt5 ${REQUIRED_QT_VERSION} NO_MODULE REQUIRED COMPONENTS Gui) endif() -find_package(Qt5 ${REQUIRED_QT_VERSION} NO_MODULE QUIET OPTIONAL_COMPONENTS Widgets XmlPatterns) +find_package(Qt5 ${REQUIRED_QT_VERSION} NO_MODULE QUIET OPTIONAL_COMPONENTS PrintSupport Widgets XmlPatterns) set_package_properties(Qt5 PROPERTIES URL "http://qt-project.org/") set_package_properties(Qt5Widgets PROPERTIES PURPOSE "Example application.") +set_package_properties(Qt5PrintSupport PROPERTIES PURPOSE "Example application.") set_package_properties(Qt5XmlPatterns PROPERTIES PURPOSE "Compile-time validation of syntax definition files.") find_package(Perl REQUIRED) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -2,3 +2,5 @@ add_executable(codeeditor codeeditor.cpp main.cpp) target_link_libraries(codeeditor Qt5::Widgets KF5SyntaxHighlighting) endif() + +ecm_optional_add_subdirectory(codepdfprinter) diff --git a/examples/codepdfprinter/CMakeLists.txt b/examples/codepdfprinter/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/examples/codepdfprinter/CMakeLists.txt @@ -0,0 +1,4 @@ +if(Qt5Widgets_FOUND AND Qt5PrintSupport_FOUND) + add_executable(codepdfprinter codepdfprinter.cpp main.cpp) + target_link_libraries(codepdfprinter Qt5::Widgets Qt5::PrintSupport KF5SyntaxHighlighting) +endif() diff --git a/examples/codepdfprinter/codepdfprinter.h b/examples/codepdfprinter/codepdfprinter.h new file mode 100644 --- /dev/null +++ b/examples/codepdfprinter/codepdfprinter.h @@ -0,0 +1,52 @@ +/* + Copyright 2019 Friedrich W. H. Kossebau + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#ifndef CODEPDFPRINTER_H +#define CODEPDFPRINTER_H + +#include + +#include + +namespace KSyntaxHighlighting { +class SyntaxHighlighter; +} + +class CodePdfPrinter +{ +public: + explicit CodePdfPrinter(); + ~CodePdfPrinter(); + +public: + bool openSourceFile(const QString &fileName); + void printPdfFile(const QString &fileName); + +private: + QTextDocument m_document; + + KSyntaxHighlighting::Repository m_repository; + KSyntaxHighlighting::SyntaxHighlighter *m_highlighter; +}; + +#endif diff --git a/examples/codepdfprinter/codepdfprinter.cpp b/examples/codepdfprinter/codepdfprinter.cpp new file mode 100644 --- /dev/null +++ b/examples/codepdfprinter/codepdfprinter.cpp @@ -0,0 +1,81 @@ +/* + Copyright 2019 Friedrich W. H. Kossebau + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include "codepdfprinter.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + + + +CodePdfPrinter::CodePdfPrinter() + : m_highlighter(new KSyntaxHighlighting::SyntaxHighlighter(&m_document)) +{ + const auto font = QFontDatabase::systemFont(QFontDatabase::FixedFont); + const QFontMetrics fontMetrics(font); + m_document.setDefaultFont(font); + + QTextOption textOption(Qt::AlignTop | Qt::AlignLeft ); + textOption.setTabStop(8 * fontMetrics.width(QLatin1Char(' '))); + textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); + m_document.setDefaultTextOption(textOption); + + // light theme for "printing" on white PDF "paper" + const auto theme = m_repository.defaultTheme(KSyntaxHighlighting::Repository::LightTheme); + m_highlighter->setTheme(theme); +} + +CodePdfPrinter::~CodePdfPrinter() = default; + +bool CodePdfPrinter::openSourceFile(const QString &fileName) +{ + QFile f(fileName); + if (!f.open(QFile::ReadOnly)) { + qWarning() << "Failed to open" << fileName << ":" << f.errorString(); + return false; + } + + const auto def = m_repository.definitionForFileName(fileName); + m_highlighter->setDefinition(def); + + m_document.setPlainText(QString::fromUtf8(f.readAll())); + return true; +} + +void CodePdfPrinter::printPdfFile(const QString &fileName) +{ + QPrinter printer(QPrinter::PrinterResolution); + printer.setOutputFormat(QPrinter::PdfFormat); + printer.setPaperSize(QPrinter::A4); + printer.setOutputFileName(fileName); + + m_document.print(&printer); +} diff --git a/examples/codepdfprinter/main.cpp b/examples/codepdfprinter/main.cpp new file mode 100644 --- /dev/null +++ b/examples/codepdfprinter/main.cpp @@ -0,0 +1,49 @@ +/* + Copyright 2019 Friedrich W. H. Kossebau + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include "codepdfprinter.h" + +#include +#include + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + QCommandLineParser parser; + parser.addHelpOption(); + parser.addPositionalArgument(QStringLiteral("source"), QStringLiteral("The source file to print.")); + parser.addPositionalArgument(QStringLiteral("pdf"), QStringLiteral("The PDF file to print to.")); + parser.process(app); + + if (parser.positionalArguments().size() < 2) { + parser.showHelp(); + } + + CodePdfPrinter printer; + if (printer.openSourceFile(parser.positionalArguments().at(0))) { + printer.printPdfFile(parser.positionalArguments().at(1)); + } + + return 0; +}