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 | |
14 | VariableListView::VariableListView(const QString &variableLine, QWidget *parent) |
15 | : QScrollArea(parent) |
16 | { |
17 | setBackgroundRole(QPalette::Base); |
18 | |
19 | setWidget(new QWidget(this)); |
20 | |
21 | parseVariables(line: variableLine); |
22 | } |
23 | |
24 | void VariableListView::parseVariables(const QString &line) |
25 | { |
26 | QString tmp = line.trimmed(); |
27 | if (tmp.startsWith(s: QLatin1String("kate:" ))) { |
28 | tmp.remove(i: 0, len: 5); |
29 | } |
30 | |
31 | QStringList variables = tmp.split(sep: QLatin1Char(';'), behavior: 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, behavior: 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(i: 0); |
42 | QString value = pair.join(sep: QLatin1Char(' ')); |
43 | pair.clear(); |
44 | pair << key << value; |
45 | } |
46 | |
47 | m_variables[pair[0]] = pair[1]; |
48 | } |
49 | } |
50 | |
51 | void VariableListView::addItem(VariableItem *item) |
52 | { |
53 | // overwrite default value when variable exists in modeline |
54 | auto it = m_variables.find(x: item->variable()); |
55 | if (it != m_variables.end()) { |
56 | item->setValueByString(it->second); |
57 | item->setActive(true); |
58 | } |
59 | |
60 | VariableEditor *editor = item->createEditor(parent: widget()); |
61 | editor->setBackgroundRole((m_editors.size() % 2) ? QPalette::AlternateBase : QPalette::Base); |
62 | |
63 | m_editors.push_back(x: editor); |
64 | m_items.push_back(x: item); |
65 | |
66 | connect(sender: editor, signal: &VariableEditor::valueChanged, context: this, slot: &VariableListView::changed); |
67 | } |
68 | |
69 | void VariableListView::resizeEvent(QResizeEvent *event) |
70 | { |
71 | QScrollArea::resizeEvent(event); |
72 | |
73 | // calculate sum of all editor heights |
74 | int listHeight = 0; |
75 | for (QWidget *w : std::as_const(t&: m_editors)) { |
76 | listHeight += w->sizeHint().height(); |
77 | } |
78 | |
79 | // resize scroll area widget |
80 | QWidget *top = widget(); |
81 | top->resize(w: event->size().width(), h: listHeight); |
82 | |
83 | // set client geometries correctly |
84 | int h = 0; |
85 | for (QWidget *w : std::as_const(t&: m_editors)) { |
86 | w->setGeometry(ax: 0, ay: h, aw: top->width(), ah: w->sizeHint().height()); |
87 | h += w->sizeHint().height(); |
88 | } |
89 | } |
90 | |
91 | void VariableListView::hideEvent(QHideEvent *event) |
92 | { |
93 | if (!event->spontaneous()) { |
94 | Q_EMIT aboutToHide(); |
95 | } |
96 | QScrollArea::hideEvent(event); |
97 | } |
98 | |
99 | QString 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(x: 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(s: QLatin1String("kate: " )); |
119 | |
120 | return line; |
121 | } |
122 | |
123 | #include "moc_variablelistview.cpp" |
124 | |