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 44 Lines • ▼ Show 20 Line(s) | 86 | else | |||
83 | pixmapSize.setWidth( height * 3 / 4 ); | 87 | pixmapSize.setWidth( height * 3 / 4 ); | ||
84 | 88 | | |||
85 | if ( pixmapSize != m_pixmap.size() ) | 89 | if ( pixmapSize != m_pixmap.size() ) | ||
86 | m_pixmap = QPixmap( pixmapSize ); | 90 | m_pixmap = QPixmap( pixmapSize ); | ||
87 | 91 | | |||
88 | // one pixel for the rectangle, the rest. whitespace | 92 | // one pixel for the rectangle, the rest. whitespace | ||
89 | int xborder = 1 + pixmapSize.width()/16; // minimum x-border | 93 | int xborder = 1 + pixmapSize.width()/16; // minimum x-border | ||
90 | int yborder = 1 + pixmapSize.height()/16; // minimum y-border | 94 | int yborder = 1 + pixmapSize.height()/16; // minimum y-border | ||
95 | const int margin = qMax(xborder, yborder); | ||||
96 | | ||||
97 | const int canvasHeight = pixmapSize.height() - 2 * margin; | ||||
91 | 98 | | |||
92 | // this font is supposed to look good at small sizes | 99 | // this font is supposed to look good at small sizes | ||
93 | QFont font = QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont); | 100 | QFont font = QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont); | ||
94 | 101 | | |||
95 | font.setPixelSize( qMax(7, qMin( 10, ( pixmapSize.height() - 2 * yborder ) / 16 ) ) ); | 102 | font.setPixelSize(qMax(7, qMin(10, canvasHeight / 16))); | ||
96 | QFontMetrics fm( font ); | 103 | QFontMetrics fm( font ); | ||
97 | 104 | | |||
98 | // calculate a better border so that the text is centered | 105 | // calculate a better border so that the text is centered | ||
99 | int canvasWidth = pixmapSize.width() - 2*xborder; | | |||
100 | int canvasHeight = pixmapSize.height() - 2*yborder; | | |||
101 | int numLines = (int) ( canvasHeight / fm.height() ); | 106 | int numLines = (int) ( canvasHeight / fm.height() ); | ||
102 | 107 | | |||
103 | // assumes an average line length of <= 120 chars | 108 | // assumes an average line length of <= 120 chars | ||
104 | const int bytesToRead = 120 * numLines; | 109 | const int bytesToRead = 120 * numLines; | ||
105 | 110 | | |||
106 | // create text-preview | 111 | // create text-preview | ||
107 | QFile file( path ); | 112 | QFile file( path ); | ||
108 | if ( file.open( QIODevice::ReadOnly )) | 113 | if ( file.open( QIODevice::ReadOnly )) | ||
Show All 22 Lines | 135 | if ( trimmedLine.contains( '\t' ) || trimmedLine.contains( " " ) ) { | |||
131 | font.setFamily( QFontDatabase::systemFont(QFontDatabase::FixedFont).family()); | 136 | font.setFamily( QFontDatabase::systemFont(QFontDatabase::FixedFont).family()); | ||
132 | break; | 137 | break; | ||
133 | } | 138 | } | ||
134 | } | 139 | } | ||
135 | 140 | | |||
136 | #if 0 | 141 | #if 0 | ||
137 | QPalette palette; | 142 | QPalette palette; | ||
138 | QColor bgColor = palette.color( QPalette::Base ); | 143 | QColor bgColor = palette.color( QPalette::Base ); | ||
139 | QColor fgColor = palette.color( QPalette::Text ); | | |||
140 | if ( qGray( bgColor.rgb() ) > qGray( fgColor.rgb() ) ) { | 144 | if ( qGray( bgColor.rgb() ) > qGray( fgColor.rgb() ) ) { | ||
141 | bgColor = bgColor.darker( 103 ); | 145 | bgColor = bgColor.darker( 103 ); | ||
142 | } else { | 146 | } else { | ||
143 | bgColor = bgColor.lighter( 103 ); | 147 | bgColor = bgColor.lighter( 103 ); | ||
144 | } | 148 | } | ||
145 | #else | 149 | #else | ||
146 | QColor bgColor = QColor ( 245, 245, 245 ); // light-grey background | 150 | QColor bgColor = QColor ( 245, 245, 245 ); // light-grey background | ||
147 | QColor fgColor = Qt::black; | | |||
148 | #endif | 151 | #endif | ||
149 | m_pixmap.fill( bgColor ); | 152 | m_pixmap.fill( bgColor ); | ||
150 | 153 | | |||
151 | QPainter painter( &m_pixmap ); | 154 | QPainter painter( &m_pixmap ); | ||
152 | painter.setFont( font ); | 155 | | ||
153 | painter.setPen( fgColor ); | 156 | QTextDocument textDocument(text); | ||
157 | | ||||
158 | textDocument.setDocumentMargin(margin); | ||||
159 | textDocument.setPageSize(m_pixmap.size()); | ||||
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 | syntaxHighlighter.setTheme(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 | | ||||
171 | textDocument.drawContents(&painter); | ||||
172 | | ||||
159 | painter.end(); | 173 | painter.end(); | ||
160 | 174 | | |||
161 | img = m_pixmap.toImage(); | 175 | img = m_pixmap.toImage(); | ||
162 | } | 176 | } | ||
163 | 177 | | |||
164 | file.close(); | 178 | file.close(); | ||
165 | } | 179 | } | ||
166 | return ok; | 180 | return ok; | ||
167 | } | 181 | } | ||
168 | 182 | | |||
169 | ThumbCreator::Flags TextCreator::flags() const | 183 | ThumbCreator::Flags TextCreator::flags() const | ||
170 | { | 184 | { | ||
171 | return (Flags)BlendIcon; | 185 | return (Flags)BlendIcon; | ||
172 | } | 186 | } | ||
173 | 187 | |
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.