diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,2 +1,3 @@ ecm_optional_add_subdirectory(codeeditor) ecm_optional_add_subdirectory(codepdfprinter) +ecm_optional_add_subdirectory(minimaltest) diff --git a/examples/minimal/CMakeLists.txt b/examples/minimal/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/examples/minimal/CMakeLists.txt @@ -0,0 +1,4 @@ +if(Qt5Widgets_FOUND) + add_executable(minimal main.cpp) + target_link_libraries(minimal Qt5::Widgets KF5SyntaxHighlighting) +endif() diff --git a/examples/minimal/main.cpp b/examples/minimal/main.cpp new file mode 100644 --- /dev/null +++ b/examples/minimal/main.cpp @@ -0,0 +1,67 @@ +/* + Copyright (C) 2020 Aleix Pol Gonzalez + + 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 +#include +#include +#include + +#include +#include +#include +#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 highlight.")); + parser.process(app); + + KSyntaxHighlighting::Repository repository; + for (const QString &file : parser.positionalArguments()) { + const auto url = QUrl::fromUserInput(file, {}, QUrl::UserInputResolutionOption::AssumeLocalFile); + QFile f(url.toLocalFile()); + if (!f.open(QIODevice::ReadOnly)) + continue; + + auto view = new QPlainTextEdit(); + view->setPlainText(QString::fromUtf8(f.readAll())); + view->resize(500, 500); + + auto highlighter = new KSyntaxHighlighting::SyntaxHighlighter(view->document()); + highlighter->setTheme((view->palette().color(QPalette::Base).lightness() < 128) + ? repository.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme) + : repository.defaultTheme(KSyntaxHighlighting::Repository::LightTheme)); + + const auto def = repository.definitionForFileName(url.toLocalFile()); + highlighter->setDefinition(def); + + view->show(); + } + + return app.exec(); +}