File indexing completed on 2024-04-14 03:55:33

0001 /*
0002     SPDX-FileCopyrightText: 2011-2018 Dominik Haumann <dhaumann@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "variablelistview.h"
0008 #include "variableeditor.h"
0009 #include "variableitem.h"
0010 
0011 #include <QRegularExpression>
0012 #include <QResizeEvent>
0013 
0014 VariableListView::VariableListView(const QString &variableLine, QWidget *parent)
0015     : QScrollArea(parent)
0016 {
0017     setBackgroundRole(QPalette::Base);
0018 
0019     setWidget(new QWidget(this));
0020 
0021     parseVariables(variableLine);
0022 }
0023 
0024 void VariableListView::parseVariables(const QString &line)
0025 {
0026     QString tmp = line.trimmed();
0027     if (tmp.startsWith(QLatin1String("kate:"))) {
0028         tmp.remove(0, 5);
0029     }
0030 
0031     QStringList variables = tmp.split(QLatin1Char(';'), Qt::SkipEmptyParts);
0032 
0033     const QRegularExpression sep(QStringLiteral("\\s+"));
0034     for (int i = 0; i < variables.size(); ++i) {
0035         QStringList pair = variables[i].split(sep, Qt::SkipEmptyParts);
0036         if (pair.size() < 2) {
0037             continue;
0038         }
0039         if (pair.size() > 2) { // e.g. fonts have spaces in the value. Hence, join all value items again
0040             QString key = pair[0];
0041             pair.removeAt(0);
0042             QString value = pair.join(QLatin1Char(' '));
0043             pair.clear();
0044             pair << key << value;
0045         }
0046 
0047         m_variables[pair[0]] = pair[1];
0048     }
0049 }
0050 
0051 void VariableListView::addItem(VariableItem *item)
0052 {
0053     // overwrite default value when variable exists in modeline
0054     auto it = m_variables.find(item->variable());
0055     if (it != m_variables.end()) {
0056         item->setValueByString(it->second);
0057         item->setActive(true);
0058     }
0059 
0060     VariableEditor *editor = item->createEditor(widget());
0061     editor->setBackgroundRole((m_editors.size() % 2) ? QPalette::AlternateBase : QPalette::Base);
0062 
0063     m_editors.push_back(editor);
0064     m_items.push_back(item);
0065 
0066     connect(editor, &VariableEditor::valueChanged, this, &VariableListView::changed);
0067 }
0068 
0069 void VariableListView::resizeEvent(QResizeEvent *event)
0070 {
0071     QScrollArea::resizeEvent(event);
0072 
0073     // calculate sum of all editor heights
0074     int listHeight = 0;
0075     for (QWidget *w : std::as_const(m_editors)) {
0076         listHeight += w->sizeHint().height();
0077     }
0078 
0079     // resize scroll area widget
0080     QWidget *top = widget();
0081     top->resize(event->size().width(), listHeight);
0082 
0083     // set client geometries correctly
0084     int h = 0;
0085     for (QWidget *w : std::as_const(m_editors)) {
0086         w->setGeometry(0, h, top->width(), w->sizeHint().height());
0087         h += w->sizeHint().height();
0088     }
0089 }
0090 
0091 void VariableListView::hideEvent(QHideEvent *event)
0092 {
0093     if (!event->spontaneous()) {
0094         Q_EMIT aboutToHide();
0095     }
0096     QScrollArea::hideEvent(event);
0097 }
0098 
0099 QString VariableListView::variableLine()
0100 {
0101     for (size_t i = 0; i < m_items.size(); ++i) {
0102         VariableItem *item = m_items[i];
0103 
0104         if (item->isActive()) {
0105             m_variables[item->variable()] = item->valueAsString();
0106         } else {
0107             m_variables.erase(item->variable());
0108         }
0109     }
0110 
0111     QString line;
0112     for (const auto &var : m_variables) {
0113         if (!line.isEmpty()) {
0114             line += QLatin1Char(' ');
0115         }
0116         line += var.first + QLatin1Char(' ') + var.second + QLatin1Char(';');
0117     }
0118     line.prepend(QLatin1String("kate: "));
0119 
0120     return line;
0121 }
0122 
0123 #include "moc_variablelistview.cpp"