KTextEditor

variablelistview.cpp
1/*
2 SPDX-FileCopyrightText: 2011-2018 Dominik Haumann <dhaumann@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "variablelistview.h"
8#include "variableeditor.h"
9#include "variableitem.h"
10
11#include <QRegularExpression>
12#include <QResizeEvent>
13
14VariableListView::VariableListView(const QString &variableLine, QWidget *parent)
15 : QScrollArea(parent)
16{
17 setBackgroundRole(QPalette::Base);
18
19 setWidget(new QWidget(this));
20
21 parseVariables(variableLine);
22}
23
24void VariableListView::parseVariables(const QString &line)
25{
26 QString tmp = line.trimmed();
27 if (tmp.startsWith(QLatin1String("kate:"))) {
28 tmp.remove(0, 5);
29 }
30
31 QStringList variables = tmp.split(QLatin1Char(';'), Qt::SkipEmptyParts);
32
33 const QRegularExpression sep(QStringLiteral("\\s+"));
34 for (int i = 0; i < variables.size(); ++i) {
35 QStringList pair = variables[i].split(sep, Qt::SkipEmptyParts);
36 if (pair.size() < 2) {
37 continue;
38 }
39 if (pair.size() > 2) { // e.g. fonts have spaces in the value. Hence, join all value items again
40 QString key = pair[0];
41 pair.removeAt(0);
42 QString value = pair.join(QLatin1Char(' '));
43 pair.clear();
44 pair << key << value;
45 }
46
47 m_variables[pair[0]] = pair[1];
48 }
49}
50
51void VariableListView::addItem(VariableItem *item)
52{
53 // overwrite default value when variable exists in modeline
54 auto it = m_variables.find(item->variable());
55 if (it != m_variables.end()) {
56 item->setValueByString(it->second);
57 item->setActive(true);
58 }
59
60 VariableEditor *editor = item->createEditor(widget());
61 editor->setBackgroundRole((m_editors.size() % 2) ? QPalette::AlternateBase : QPalette::Base);
62
63 m_editors.push_back(editor);
64 m_items.push_back(item);
65
66 connect(editor, &VariableEditor::valueChanged, this, &VariableListView::changed);
67}
68
69void VariableListView::resizeEvent(QResizeEvent *event)
70{
72
73 // calculate sum of all editor heights
74 int listHeight = 0;
75 for (QWidget *w : std::as_const(m_editors)) {
76 listHeight += w->sizeHint().height();
77 }
78
79 // resize scroll area widget
80 QWidget *top = widget();
81 top->resize(event->size().width(), listHeight);
82
83 // set client geometries correctly
84 int h = 0;
85 for (QWidget *w : std::as_const(m_editors)) {
86 w->setGeometry(0, h, top->width(), w->sizeHint().height());
87 h += w->sizeHint().height();
88 }
89}
90
91void VariableListView::hideEvent(QHideEvent *event)
92{
93 if (!event->spontaneous()) {
94 Q_EMIT aboutToHide();
95 }
97}
98
99QString VariableListView::variableLine()
100{
101 for (size_t i = 0; i < m_items.size(); ++i) {
102 VariableItem *item = m_items[i];
103
104 if (item->isActive()) {
105 m_variables[item->variable()] = item->valueAsString();
106 } else {
107 m_variables.erase(item->variable());
108 }
109 }
110
111 QString line;
112 for (const auto &var : m_variables) {
113 if (!line.isEmpty()) {
114 line += QLatin1Char(' ');
115 }
116 line += var.first + QLatin1Char(' ') + var.second + QLatin1Char(';');
117 }
118 line.prepend(QLatin1String("kate: "));
119
120 return line;
121}
122
123#include "moc_variablelistview.cpp"
void clear()
void removeAt(qsizetype i)
qsizetype size() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool event(QEvent *e) override
virtual void resizeEvent(QResizeEvent *) override
QWidget * widget() const const
QString first(qsizetype n) const const
bool isEmpty() const const
QString & prepend(QChar ch)
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString trimmed() const const
QString join(QChar separator) const const
SkipEmptyParts
virtual void hideEvent(QHideEvent *event)
void setBackgroundRole(QPalette::ColorRole role)
void resize(const QSize &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:11:27 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.