KTextEditor

expandingwidgetmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2007 David Nolden <david.nolden.kdevelop@art-master.de>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "expandingwidgetmodel.h"
8
9#include <QApplication>
10#include <QBrush>
11#include <QModelIndex>
12#include <QTextEdit>
13#include <QTreeView>
14
15#include <KColorUtils>
16#include <ktexteditor/codecompletionmodel.h>
17
18#include "katepartdebug.h"
19
20using namespace KTextEditor;
21
22inline QModelIndex firstColumn(const QModelIndex &index)
23{
24 return index.sibling(index.row(), 0);
25}
26
27ExpandingWidgetModel::ExpandingWidgetModel(QWidget *parent)
28 : QAbstractItemModel(parent)
29{
30}
31
32ExpandingWidgetModel::~ExpandingWidgetModel()
33{
34}
35
36static QColor doAlternate(const QColor &color)
37{
38 QColor background = QApplication::palette().window().color();
39 return KColorUtils::mix(color, background, 0.15);
40}
41
43{
44 int matchQuality = contextMatchQuality(index.sibling(index.row(), 0));
45
46 if (matchQuality > 0) {
47 bool alternate = index.row() & 1;
48
49 QColor badMatchColor(0xff00aa44); // Blue-ish green
50 QColor goodMatchColor(0xff00ff00); // Green
51
52 QColor background = treeView()->palette().light().color();
53
54 QColor totalColor = KColorUtils::mix(badMatchColor, goodMatchColor, ((float)matchQuality) / 10.0);
55
56 if (alternate) {
57 totalColor = doAlternate(totalColor);
58 }
59
60 const qreal dynamicTint = 0.2;
61 const qreal minimumTint = 0.2;
62 qreal tintStrength = (dynamicTint * matchQuality) / 10;
63 if (tintStrength != 0.0) {
64 tintStrength += minimumTint; // Some minimum tinting strength, else it's not visible any more
65 }
66
67 return KColorUtils::tint(background, totalColor, tintStrength).rgb();
68 } else {
69 return 0;
70 }
71}
72
73QVariant ExpandingWidgetModel::data(const QModelIndex &index, int role) const
74{
75 switch (role) {
76 case Qt::BackgroundRole: {
77 if (index.column() == 0) {
78 // Highlight by match-quality
79 uint color = matchColor(index);
80 if (color) {
81 return QBrush(color);
82 }
83 }
84 }
85 }
86 return QVariant();
87}
88
89QList<QVariant> mergeCustomHighlighting(int leftSize, const QList<QVariant> &left, int rightSize, const QList<QVariant> &right)
90{
91 QList<QVariant> ret = left;
92 if (left.isEmpty()) {
93 ret << QVariant(0);
94 ret << QVariant(leftSize);
96 }
97
98 if (right.isEmpty()) {
99 ret << QVariant(leftSize);
100 ret << QVariant(rightSize);
102 } else {
103 QList<QVariant>::const_iterator it = right.constBegin();
104 while (it != right.constEnd()) {
105 {
107 for (int a = 0; a < 2; a++) {
108 ++testIt;
109 if (testIt == right.constEnd()) {
110 qCWarning(LOG_KTE) << "Length of input is not multiple of 3";
111 break;
112 }
113 }
114 }
115
116 ret << QVariant((*it).toInt() + leftSize);
117 ++it;
118 ret << QVariant((*it).toInt());
119 ++it;
120 ret << *it;
121 if (!(*it).value<QTextFormat>().isValid()) {
122 qCDebug(LOG_KTE) << "Text-format is invalid";
123 }
124 ++it;
125 }
126 }
127 return ret;
128}
129
130// It is assumed that between each two strings, one space is inserted
131QList<QVariant> mergeCustomHighlighting(QStringList strings, QList<QVariantList> highlights, int grapBetweenStrings)
132{
133 if (strings.isEmpty()) {
134 qCWarning(LOG_KTE) << "List of strings is empty";
135 return QList<QVariant>();
136 }
137
138 if (highlights.isEmpty()) {
139 qCWarning(LOG_KTE) << "List of highlightings is empty";
140 return QList<QVariant>();
141 }
142
143 if (strings.count() != highlights.count()) {
144 qCWarning(LOG_KTE) << "Length of string-list is " << strings.count() << " while count of highlightings is " << highlights.count() << ", should be same";
145 return QList<QVariant>();
146 }
147
148 // Merge them together
149 QString totalString = strings[0];
150 QVariantList totalHighlighting = highlights[0];
151
152 strings.pop_front();
153 highlights.pop_front();
154
155 while (!strings.isEmpty()) {
156 totalHighlighting = mergeCustomHighlighting(totalString.length(), totalHighlighting, strings[0].length(), highlights[0]);
157 totalString += strings[0];
158
159 for (int a = 0; a < grapBetweenStrings; a++) {
160 totalString += QLatin1Char(' ');
161 }
162
163 strings.pop_front();
164 highlights.pop_front();
165 }
166 // Combine the custom-highlightings
167 return totalHighlighting;
168}
virtual int contextMatchQuality(const QModelIndex &index) const =0
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Does not request data from index, this only returns local data like highlighting for expanded rows an...
uint matchColor(const QModelIndex &index) const
Returns the match-color for the given index, or zero if match-quality could not be computed.
KGUIADDONS_EXPORT QColor mix(const QColor &c1, const QColor &c2, qreal bias=0.5)
KGUIADDONS_EXPORT QColor tint(const QColor &base, const QColor &color, qreal amount=0.3)
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
const QColor & color() const const
QRgb rgb() const const
QPalette palette()
qsizetype count() const const
bool isEmpty() const const
void pop_front()
int column() const const
int row() const const
QModelIndex sibling(int row, int column) const const
const QBrush & window() const const
qsizetype length() const const
BackgroundRole
QTextStream & right(QTextStream &stream)
bool isValid() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:17:27 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.