diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,7 @@ Solid Bookmarks GuiAddons + SyntaxHighlighting ) # As this is the check used for linkage, only require it in the same location... if (UNIX) diff --git a/thumbnail/CMakeLists.txt b/thumbnail/CMakeLists.txt --- a/thumbnail/CMakeLists.txt +++ b/thumbnail/CMakeLists.txt @@ -94,6 +94,7 @@ target_link_libraries(textthumbnail Qt5::Gui KF5::KIOWidgets + KF5::SyntaxHighlighting ) install(TARGETS textthumbnail DESTINATION ${KDE_INSTALL_PLUGINDIR}) diff --git a/thumbnail/textcreator.h b/thumbnail/textcreator.h --- a/thumbnail/textcreator.h +++ b/thumbnail/textcreator.h @@ -22,6 +22,7 @@ #include #include +#include class TextCreator : public ThumbCreator { @@ -35,6 +36,8 @@ char *m_data; int m_dataSize; QPixmap m_pixmap; + + KSyntaxHighlighting::Repository m_highlightingRepository; }; #endif diff --git a/thumbnail/textcreator.cpp b/thumbnail/textcreator.cpp --- a/thumbnail/textcreator.cpp +++ b/thumbnail/textcreator.cpp @@ -27,7 +27,11 @@ #include #include #include +#include +#include +#include +#include #include // TODO Fix or remove kencodingprober code @@ -136,26 +140,37 @@ #if 0 QPalette palette; QColor bgColor = palette.color( QPalette::Base ); - QColor fgColor = palette.color( QPalette::Text ); if ( qGray( bgColor.rgb() ) > qGray( fgColor.rgb() ) ) { bgColor = bgColor.darker( 103 ); } else { bgColor = bgColor.lighter( 103 ); } #else QColor bgColor = QColor ( 245, 245, 245 ); // light-grey background - QColor fgColor = Qt::black; #endif m_pixmap.fill( bgColor ); QPainter painter( &m_pixmap ); - painter.setFont( font ); - painter.setPen( fgColor ); + + QTextDocument textDocument(text); + + textDocument.setDocumentMargin(0); + textDocument.setPageSize(QSizeF(canvasWidth, canvasHeight)); + textDocument.setDefaultFont(font); QTextOption textOption( Qt::AlignTop | Qt::AlignLeft ); textOption.setTabStop( 8 * painter.fontMetrics().width( ' ' ) ); textOption.setWrapMode( QTextOption::WrapAtWordBoundaryOrAnywhere ); - painter.drawText( QRect( xborder, yborder, canvasWidth, canvasHeight ), text, textOption ); + textDocument.setDefaultTextOption(textOption); + + KSyntaxHighlighting::SyntaxHighlighter syntaxHighlighter(&textDocument); + syntaxHighlighter.setDefinition(m_highlightingRepository.definitionForFileName(path)); + const auto highlightingTheme = m_highlightingRepository.defaultTheme(KSyntaxHighlighting::Repository::LightTheme); + syntaxHighlighter.setTheme(highlightingTheme); + + painter.translate(xborder, yborder); + textDocument.drawContents(&painter); + painter.end(); img = m_pixmap.toImage();