KTextEditor

katerenderrange.cpp
1/*
2 SPDX-FileCopyrightText: 2003-2005 Hamish Rodda <rodda@kde.org>
3 SPDX-FileCopyrightText: 2007 Mirko Stocker <me@misto.ch>
4 SPDX-FileCopyrightText: 2008 David Nolden <david.nolden.kdevelop@art-master.de>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "katerenderrange.h"
10
11#include <KColorUtils>
12
13void mergeAttributes(KTextEditor::Attribute::Ptr base, KTextEditor::Attribute::Ptr add)
14{
15 if (!add) {
16 return;
17 }
18
19 bool hadBg = base->hasProperty(KTextEditor::Attribute::BackgroundBrush);
20 bool hasBg = add->hasProperty(KTextEditor::Attribute::BackgroundBrush);
21
22 bool hadFg = base->hasProperty(KTextEditor::Attribute::ForegroundBrush);
23 bool hasFg = add->hasProperty(KTextEditor::Attribute::ForegroundBrush);
24
25 if (((!hadBg || !hasBg) && (!hadFg || !hasFg))) {
26 // Nothing to blend
27 *base += *add;
28 return;
29 }
30
31 // We eventually have to blend
32
33 QBrush baseBgBrush;
34 QBrush baseFgBrush;
35
36 if (hadBg) {
37 baseBgBrush = base->background();
38 }
39
40 if (hadFg) {
41 baseFgBrush = base->foreground();
42 }
43
44 *base += *add;
45
46 if (hadBg && hasBg) {
47 QBrush bg = add->background();
48 if (!bg.isOpaque()) {
49 QColor mixWithColor = bg.color();
50 mixWithColor.setAlpha(255);
51 bg.setColor(KColorUtils::mix(baseBgBrush.color(), mixWithColor, bg.color().alphaF()));
52 base->setBackground(bg);
53 }
54 }
55 if (hadFg && hasFg) {
56 QBrush fg = add->foreground();
57 if (!fg.isOpaque()) {
58 QColor mixWithColor = fg.color();
59 mixWithColor.setAlpha(255);
60 fg.setColor(KColorUtils::mix(baseFgBrush.color(), mixWithColor, fg.color().alphaF()));
61 base->setForeground(fg);
62 }
63 }
64}
65
66void NormalRenderRange::addRange(KTextEditor::Range range, KTextEditor::Attribute::Ptr attribute)
67{
68 m_ranges.emplace_back(range, std::move(attribute));
69}
70
71KTextEditor::Cursor NormalRenderRange::nextBoundary() const
72{
73 return m_nextBoundary;
74}
75
76bool NormalRenderRange::advanceTo(const KTextEditor::Cursor pos)
77{
78 size_t index = m_currentRange;
79 while (index < m_ranges.size()) {
80 const auto &p = m_ranges[index];
81 const auto &r = p.first;
82 if (r.end() <= pos) {
83 ++index;
84 } else {
85 bool ret = index != m_currentRange;
86 m_currentRange = index;
87
88 if (r.start() > pos) {
89 m_nextBoundary = r.start();
90 } else {
91 m_nextBoundary = r.end();
92 }
93 if (r.contains(pos)) {
94 m_currentAttribute = p.second;
95 } else {
96 m_currentAttribute.reset();
97 }
98
99 return ret;
100 }
101 }
102
103 m_nextBoundary = KTextEditor::Cursor(INT_MAX, INT_MAX);
104 m_currentAttribute.reset();
105 return false;
106}
107
108KTextEditor::Attribute::Ptr NormalRenderRange::currentAttribute() const
109{
110 return m_currentAttribute;
111}
112
113KTextEditor::Cursor RenderRangeVector::nextBoundary() const
114{
115 KTextEditor::Cursor ret = m_currentPos;
116 bool first = true;
117 for (auto &r : m_ranges) {
118 if (first) {
119 ret = r.nextBoundary();
120 first = false;
121
122 } else {
123 KTextEditor::Cursor nb = r.nextBoundary();
124 if (ret > nb) {
125 ret = nb;
126 }
127 }
128 }
129 return ret;
130}
131
132NormalRenderRange &RenderRangeVector::pushNewRange()
133{
134 m_ranges.push_back(NormalRenderRange());
135 return m_ranges.back();
136}
137
138void RenderRangeVector::advanceTo(const KTextEditor::Cursor pos)
139{
140 for (auto &r : m_ranges) {
141 r.advanceTo(pos);
142 }
143}
144
145bool RenderRangeVector::hasAttribute() const
146{
147 for (auto &r : m_ranges) {
148 if (r.currentAttribute()) {
149 return true;
150 }
151 }
152
153 return false;
154}
155
156KTextEditor::Attribute::Ptr RenderRangeVector::generateAttribute() const
157{
159 bool ownsAttribute = false;
160
161 for (auto &r : m_ranges) {
162 if (KTextEditor::Attribute::Ptr a2 = r.currentAttribute()) {
163 if (!a) {
164 a = a2;
165 } else {
166 if (!ownsAttribute) {
167 // Make an own copy of the attribute..
168 ownsAttribute = true;
169 a = new KTextEditor::Attribute(*a);
170 }
171 mergeAttributes(a, a2);
172 }
173 }
174 }
175
176 return a;
177}
A class which provides customized text decorations.
Definition attribute.h:51
The Cursor represents a position in a Document.
Definition cursor.h:75
static constexpr Cursor start() noexcept
Returns a cursor representing the start of any document - i.e., line 0, column 0.
Definition cursor.h:120
An object representing a section of text, from one Cursor to another.
KGUIADDONS_EXPORT QColor mix(const QColor &c1, const QColor &c2, qreal bias=0.5)
KGuiItem add()
const QColor & color() const const
bool isOpaque() const const
void setColor(Qt::GlobalColor color)
float alphaF() const const
void setAlpha(int alpha)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.