Changeset View
Standalone View
thumbnail/textcreator.cpp
Show All 21 Lines | |||||
22 | 22 | | |||
23 | #include <QFile> | 23 | #include <QFile> | ||
24 | #include <QFontDatabase> | 24 | #include <QFontDatabase> | ||
25 | #include <QPixmap> | 25 | #include <QPixmap> | ||
26 | #include <QImage> | 26 | #include <QImage> | ||
27 | #include <QPainter> | 27 | #include <QPainter> | ||
28 | #include <QPalette> | 28 | #include <QPalette> | ||
29 | #include <QTextCodec> | 29 | #include <QTextCodec> | ||
30 | #include <QTextDocument> | ||||
30 | 31 | | |||
32 | #include <KSyntaxHighlighting/SyntaxHighlighter> | ||||
33 | #include <KSyntaxHighlighting/Theme> | ||||
34 | #include <KSyntaxHighlighting/Definition> | ||||
31 | #include <KDesktopFile> | 35 | #include <KDesktopFile> | ||
32 | 36 | | |||
33 | // TODO Fix or remove kencodingprober code | 37 | // TODO Fix or remove kencodingprober code | ||
34 | // #include <kencodingprober.h> | 38 | // #include <kencodingprober.h> | ||
35 | 39 | | |||
36 | extern "C" | 40 | extern "C" | ||
37 | { | 41 | { | ||
38 | Q_DECL_EXPORT ThumbCreator *new_creator() | 42 | Q_DECL_EXPORT ThumbCreator *new_creator() | ||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | 73 | { | |||
91 | 95 | | |||
92 | // this font is supposed to look good at small sizes | 96 | // this font is supposed to look good at small sizes | ||
93 | QFont font = QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont); | 97 | QFont font = QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont); | ||
94 | 98 | | |||
95 | font.setPixelSize( qMax(7, qMin( 10, ( pixmapSize.height() - 2 * yborder ) / 16 ) ) ); | 99 | font.setPixelSize( qMax(7, qMin( 10, ( pixmapSize.height() - 2 * yborder ) / 16 ) ) ); | ||
96 | QFontMetrics fm( font ); | 100 | QFontMetrics fm( font ); | ||
97 | 101 | | |||
98 | // calculate a better border so that the text is centered | 102 | // calculate a better border so that the text is centered | ||
99 | int canvasWidth = pixmapSize.width() - 2*xborder; | 103 | const QSizeF canvasSize(pixmapSize.width() - 2 * xborder, pixmapSize.height() - 2 * yborder); | ||
100 | int canvasHeight = pixmapSize.height() - 2*yborder; | 104 | const int numLines = (int) (canvasSize.height() / fm.height()); | ||
101 | int numLines = (int) ( canvasHeight / fm.height() ); | | |||
102 | 105 | | |||
103 | // assumes an average line length of <= 120 chars | 106 | // assumes an average line length of <= 120 chars | ||
104 | const int bytesToRead = 120 * numLines; | 107 | const int bytesToRead = 120 * numLines; | ||
105 | 108 | | |||
106 | // create text-preview | 109 | // create text-preview | ||
107 | QFile file( path ); | 110 | QFile file( path ); | ||
108 | if ( file.open( QIODevice::ReadOnly )) | 111 | if ( file.open( QIODevice::ReadOnly )) | ||
109 | { | 112 | { | ||
Show All 21 Lines | 133 | if ( trimmedLine.contains( '\t' ) || trimmedLine.contains( " " ) ) { | |||
131 | font.setFamily( QFontDatabase::systemFont(QFontDatabase::FixedFont).family()); | 134 | font.setFamily( QFontDatabase::systemFont(QFontDatabase::FixedFont).family()); | ||
132 | break; | 135 | break; | ||
133 | } | 136 | } | ||
134 | } | 137 | } | ||
135 | 138 | | |||
136 | #if 0 | 139 | #if 0 | ||
137 | QPalette palette; | 140 | QPalette palette; | ||
138 | QColor bgColor = palette.color( QPalette::Base ); | 141 | QColor bgColor = palette.color( QPalette::Base ); | ||
139 | QColor fgColor = palette.color( QPalette::Text ); | | |||
140 | if ( qGray( bgColor.rgb() ) > qGray( fgColor.rgb() ) ) { | 142 | if ( qGray( bgColor.rgb() ) > qGray( fgColor.rgb() ) ) { | ||
141 | bgColor = bgColor.darker( 103 ); | 143 | bgColor = bgColor.darker( 103 ); | ||
142 | } else { | 144 | } else { | ||
143 | bgColor = bgColor.lighter( 103 ); | 145 | bgColor = bgColor.lighter( 103 ); | ||
144 | } | 146 | } | ||
145 | #else | 147 | #else | ||
146 | QColor bgColor = QColor ( 245, 245, 245 ); // light-grey background | 148 | QColor bgColor = QColor ( 245, 245, 245 ); // light-grey background | ||
147 | QColor fgColor = Qt::black; | | |||
148 | #endif | 149 | #endif | ||
149 | m_pixmap.fill( bgColor ); | 150 | m_pixmap.fill( bgColor ); | ||
150 | 151 | | |||
151 | QPainter painter( &m_pixmap ); | 152 | QPainter painter( &m_pixmap ); | ||
152 | painter.setFont( font ); | 153 | | ||
153 | painter.setPen( fgColor ); | 154 | QTextDocument textDocument(text); | ||
155 | | ||||
156 | // QTextDocument only supports one margin value for all borders, | ||||
157 | // so we do a page-in-page behind its back, and do our own borders | ||||
158 | textDocument.setDocumentMargin(0); | ||||
159 | textDocument.setPageSize(canvasSize); | ||||
160 | textDocument.setDefaultFont(font); | ||||
154 | 161 | | |||
155 | QTextOption textOption( Qt::AlignTop | Qt::AlignLeft ); | 162 | QTextOption textOption( Qt::AlignTop | Qt::AlignLeft ); | ||
156 | textOption.setTabStop( 8 * painter.fontMetrics().width( ' ' ) ); | 163 | textOption.setTabStop( 8 * painter.fontMetrics().width( ' ' ) ); | ||
157 | textOption.setWrapMode( QTextOption::WrapAtWordBoundaryOrAnywhere ); | 164 | textOption.setWrapMode( QTextOption::WrapAtWordBoundaryOrAnywhere ); | ||
158 | painter.drawText( QRect( xborder, yborder, canvasWidth, canvasHeight ), text, textOption ); | 165 | textDocument.setDefaultTextOption(textOption); | ||
166 | | ||||
167 | KSyntaxHighlighting::SyntaxHighlighter syntaxHighlighter(&textDocument); | ||||
168 | syntaxHighlighter.setDefinition(m_highlightingRepository.definitionForFileName(path)); | ||||
169 | const auto highlightingTheme = m_highlightingRepository.defaultTheme(KSyntaxHighlighting::Repository::LightTheme); | ||||
cfeck: KSyntaxHighlighting::Theme also provides a background color. Can that be used instead of the… | |||||
Good idea. Hm, highlightingTheme.backgroundColor(KSyntaxHighlighting::Theme::Normal) returns black for me, and there is a comment for Theme::backgroundColor(TextStyle) const saying kossebau: Good idea. Hm, `highlightingTheme.backgroundColor(KSyntaxHighlighting::Theme::Normal)` returns… | |||||
@vkrause Can you help here and tell what one is intended to use? kossebau: @vkrause Can you help here and tell what one is intended to use? | |||||
I think default background color here refers to what your palette gives you. The background color defined by highlighting is typically only used for small text blocks like for alerts ("TODO", "HACK", etc). vkrause: I think default background color here refers to what your palette gives you. The background… | |||||
@vkrause Thanks for the comment. With that in mind and coffee in blood and in daylight I get the SH api dox a bit better. I might think about doing a proposal for improvement for the next person hitting that without coffee perhaps ;) @cfeck From what I have seen, I would then stay with the hardcoded background color. Actually I wonder why it it not pure #fff as one has(?) with any rich textdocument usually. But not the intention of this very patch, so I leave that out. kossebau: @vkrause Thanks for the comment. With that in mind and coffee in blood and in daylight I get… | |||||
@cfeck: I would do the remove of the qpalette in a separate commit then later. kossebau: @cfeck: I would do the remove of the qpalette in a separate commit then later. | |||||
I saw this review request just now: Using a hardcoded theme is not a good idea. I suggest to use a solution based on the background color. We have the following in our example codeeditor: setTheme((palette().color(QPalette::Base).lightness() < 128) ? m_repository.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme) : m_repository.defaultTheme(KSyntaxHighlighting::Repository::LightTheme)); Could a similar approach be used here as well? dhaumann: I saw this review request just now: Using a hardcoded theme is not a good idea. I suggest to… | |||||
For the thumbnail the code is rendering on a paper-like canvas, which is hard-coded as well (see lines above with QColor ( 245, 245, 245 ); // light-grey background). To simulate a print-out, for what I guess or would have done myself. kossebau: For the thumbnail the code is rendering on a paper-like canvas, which is hard-coded as well… | |||||
Ah, I thought in a dark color theme the preview is also dark - which does not seem to be the case... dhaumann: Ah, I thought in a dark color theme the preview is also dark - which does not seem to be the… | |||||
170 | syntaxHighlighter.setTheme(highlightingTheme); | ||||
171 | | ||||
172 | // draw page-in-page, with clipping as needed | ||||
173 | painter.translate(xborder, yborder); | ||||
174 | textDocument.drawContents(&painter, QRectF(QPointF(0, 0), canvasSize)); | ||||
175 | | ||||
159 | painter.end(); | 176 | painter.end(); | ||
160 | 177 | | |||
161 | img = m_pixmap.toImage(); | 178 | img = m_pixmap.toImage(); | ||
162 | } | 179 | } | ||
163 | 180 | | |||
164 | file.close(); | 181 | file.close(); | ||
165 | } | 182 | } | ||
166 | return ok; | 183 | return ok; | ||
167 | } | 184 | } | ||
168 | 185 | | |||
169 | ThumbCreator::Flags TextCreator::flags() const | 186 | ThumbCreator::Flags TextCreator::flags() const | ||
170 | { | 187 | { | ||
171 | return (Flags)BlendIcon; | 188 | return (Flags)BlendIcon; | ||
172 | } | 189 | } | ||
173 | 190 | |
KSyntaxHighlighting::Theme also provides a background color. Can that be used instead of the hardcoded 245? I cannot remember reason why I disabled (or never enabled) the QPalette code; I suggest to remove it.