KTextEditor

katetextpreview.cpp
1/*
2 SPDX-FileCopyrightText: 2016 Dominik Haumann <dhaumann@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "katetextpreview.h"
8#include "kateconfig.h"
9#include "katedocument.h"
10#include "katelayoutcache.h"
11#include "katepartdebug.h"
12#include "katerenderer.h"
13#include "kateview.h"
14#include "kateviewinternal.h"
15
16#include <QPainter>
17
18#include <cmath>
19
20KateTextPreview::KateTextPreview(KTextEditor::ViewPrivate *view, QWidget *parent)
22 , m_view(view)
23 , m_line(0)
24 , m_showFoldedLines(false)
25 , m_center(true)
26 , m_scale(1.0)
27{
28}
29
30KTextEditor::ViewPrivate *KateTextPreview::view() const
31{
32 return m_view;
33}
34
36{
37 if (m_line != line) {
38 m_line = qMax(0.0, line);
39 update();
40 }
41}
42
43qreal KateTextPreview::line() const
44{
45 return m_line;
46}
47
49{
50 if (m_center != center) {
51 m_center = center;
52 update();
53 }
54}
55
56bool KateTextPreview::centerView() const
57{
58 return m_center;
59}
60
62{
63 if (m_scale <= 0.0) {
64 qCWarning(LOG_KTE) << "Negative scale factors are not supported, ignoring.";
65 return;
66 }
67
68 if (m_scale != factor) {
69 m_scale = factor;
70 update();
71 }
72}
73
74qreal KateTextPreview::scaleFactor() const
75{
76 return m_scale;
77}
78
80{
81 if (m_showFoldedLines != on) {
82 m_showFoldedLines = on;
83 update();
84 }
85}
86
87bool KateTextPreview::showFoldedLines() const
88{
89 return m_showFoldedLines;
90}
91
92void KateTextPreview::paintEvent(QPaintEvent *event)
93{
95
96 KateRenderer *const renderer = view()->renderer();
97 const int lastLine = showFoldedLines() ? view()->document()->lines() : view()->textFolding().visibleLines();
98
99 const QRectF r = contentsRect(); // already subtracted QFrame's frame width
100 const int xStart = 0;
101 const int xEnd = r.width() / m_scale;
102 const int lineHeight = qMax(1, renderer->lineHeight());
103 const int lineCount = ceil(static_cast<qreal>(r.height()) / (lineHeight * m_scale));
104 int startLine = qMax(0.0, m_line - (m_center ? (ceil(lineCount / 2.0)) : 0));
105 // at the very end of the document, make sure the preview is filled
106 if (qMax(0.0, m_line - (m_center ? (ceil(lineCount / 2.0)) : 0)) + lineCount - 1 > lastLine) {
107 m_line = qMax(0.0, lastLine - static_cast<qreal>(r.height()) / (lineHeight * m_scale) + floor(lineCount / 2.0) - 1);
108 startLine = qMax(0.0, m_line - (m_center ? (ceil(lineCount / 2.0)) : 0) + 1);
109 }
110 const int endLine = startLine + lineCount;
111
112 QPainter paint(this);
113 paint.setClipRect(r);
114 paint.fillRect(r, m_view->rendererConfig()->backgroundColor());
115
116 // renderer->setShowTabs(doc()->config()->showTabs());
117 // renderer->setShowTrailingSpaces(doc()->config()->showSpaces());
118
119 paint.scale(m_scale, m_scale);
120 paint.translate(r.topLeft());
121 if (m_center && m_line - ceil(lineCount / 2.0) > 0.0) {
122 paint.translate(0, -lineHeight * (m_line - static_cast<int>(m_line)));
123 }
124
125 for (int line = startLine; line <= endLine; ++line) {
126 // get real line, skip if invalid!
127 const int realLine = showFoldedLines() ? line : view()->textFolding().visibleLineToLine(line);
128 if (realLine < 0 || realLine >= renderer->doc()->lines()) {
129 continue;
130 }
131
132 // compute layout WITHOUT cache to not poison it + render it
133 KateLineLayout lineLayout(*renderer);
134 lineLayout.setLine(realLine, -1);
135 renderer->layoutLine(&lineLayout, -1 /* no wrap */, false /* no layout cache */);
136 renderer->paintTextLine(paint, &lineLayout, xStart, xEnd, QRectF{}, nullptr, KateRenderer::SkipDrawFirstInvisibleLineUnderlined);
137
138 // translate for next line
139 paint.translate(0, lineHeight);
140 }
141}
142
143#include "moc_katetextpreview.cpp"
virtual int lines() const =0
Get the count of lines of the document.
Handles all of the work of rendering the text (used for the views and printing)
@ SkipDrawFirstInvisibleLineUnderlined
Skip drawing the dashed underline at the start of a folded block of text?
KTextEditor::DocumentPrivate * doc() const
Returns the document to which this renderer is bound.
void layoutLine(KateLineLayout *line, int maxwidth=-1, bool cacheLayout=false) const
Text width & height calculation functions...
void paintTextLine(QPainter &paint, KateLineLayout *range, int xStart, int xEnd, const QRectF &textClipRect=QRectF(), const KTextEditor::Cursor *cursor=nullptr, PaintTextLineFlags flags=PaintTextLineFlags())
This is the ultimate function to perform painting of a text line.
void setShowFoldedLines(bool on)
Sets whether folded lines are hidden or not.
void setLine(qreal line)
Sets line as preview line.
void setCenterView(bool center)
Enabled/disable centering the view on the line set with setLine().
void setScaleFactor(qreal factor)
Sets the scale factor.
int visibleLines() const
Query number of visible lines.
int visibleLineToLine(int visibleLine) const
Convert a visible line number to a line number in the text buffer.
virtual bool event(QEvent *e) override
virtual void paintEvent(QPaintEvent *) override
qreal height() const const
QPointF topLeft() const const
qreal width() const const
FramelessWindowHint
QRect contentsRect() const const
void update()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:15:44 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.