File: confdelegate.cpp

package info (click to toggle)
kde-config-systemd 1.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,560 kB
  • ctags: 688
  • sloc: cpp: 3,306; makefile: 9; sh: 2
file content (213 lines) | stat: -rw-r--r-- 7,244 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*******************************************************************************
 * Copyright (C) 2013-2015 Ragnar Thomsen <rthomsen6@gmail.com>                *
 *                                                                             *
 * This program is free software: you can redistribute it and/or modify it     *
 * under the terms of the GNU General Public License as published by the Free  *
 * Software Foundation, either version 2 of the License, or (at your option)   *
 * any later version.                                                          *
 *                                                                             *
 * This program is distributed in the hope that it will be useful, but WITHOUT *
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for    *
 * more details.                                                               *
 *                                                                             *
 * You should have received a copy of the GNU General Public License along     *
 * with this program. If not, see <http://www.gnu.org/licenses/>.              *
 *******************************************************************************/

#include "confdelegate.h"

#include <QDebug>
#include <QSpinBox>
#include <QComboBox>
#include <QLineEdit>
#include <QStandardItemModel>

ConfDelegate::ConfDelegate(QObject *parent, const QList<confOption> *confOptList)
    : QStyledItemDelegate(parent)
    , m_optList(confOptList)
{
}

QWidget *ConfDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &/* option */,
    const QModelIndex &index) const
{
  // Creates the editor component for an item in qtableview

  // qDebug() << "Creating editor";

  QString uniqueName = index.data(Qt::UserRole+1).toString();
  int confIndex = m_optList->indexOf(confOption(uniqueName));
  if (confIndex == -1) {
    qWarning() << "Config option" << uniqueName << "not found.";
    return NULL;
  }
  const confOption *opt = &m_optList->at(confIndex);

  if (index.data(Qt::UserRole) == BOOL)
  {
    QComboBox *editor = new QComboBox(parent);
    QStringList list = (QStringList() << "true" << "false");
    editor->addItems(list);
    return editor;
  }
  else if (index.data(Qt::UserRole) == TIME ||
           index.data(Qt::UserRole) == INTEGER ||
           index.data(Qt::UserRole) == RESLIMIT ||
           index.data(Qt::UserRole) == SIZE)
  {
    QSpinBox *editor = new QSpinBox(parent);
    editor->setMinimum(opt->minVal);
    editor->setMaximum(opt->maxVal);
    return editor;
  }
  else if (index.data(Qt::UserRole) == LIST)
  {
    QComboBox *editor = new QComboBox(parent);

    // Populate combobox
    editor->addItems(opt->possibleVals);

    // editor->setFrame(false);
    return editor;
  }
  else if (index.data(Qt::UserRole) == MULTILIST)
  {
    QComboBox *editor = new QComboBox(parent);

    // Populate combobox
    QStandardItemModel *multiListModel = new QStandardItemModel();
    editor->setModel(multiListModel);
    for (int i = 0; i < opt->possibleVals.count(); ++i)
    {
      QStandardItem* item;
      item = new QStandardItem(opt->possibleVals.at(i));
      item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
      item->setData(Qt::Checked, Qt::CheckStateRole);

      multiListModel->appendRow(item);
    }
    // editor->setFrame(false);
    return editor;
  }
  else
  {
    QLineEdit *editor  = new QLineEdit(parent);
    editor->setFrame(false);
    return editor;
  }
}

void ConfDelegate::setEditorData(QWidget *editor,
                                    const QModelIndex &index) const
{
  // Set the value in the editor (spinbox, lineedit, etc)

  // qDebug() << "Setting editor data";

  if (index.data(Qt::UserRole) == BOOL)
  {
    QString value = index.model()->data(index, Qt::DisplayRole).toString().toLower();
    if (value == "true" || value == "on" || value == "yes")
      value = "true";
    else if (value == "false" || value == "off" || value == "no")
      value = "false";
    QComboBox *cmb = static_cast<QComboBox*>(editor);
    cmb->setCurrentIndex(cmb->findText(value));
  }
  else if (index.data(Qt::UserRole) == TIME ||
           index.data(Qt::UserRole) == INTEGER ||
           index.data(Qt::UserRole) == RESLIMIT ||
           index.data(Qt::UserRole) == SIZE)
  {
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    int value = index.model()->data(index, Qt::DisplayRole).toInt();
    spinBox->setValue(value);
  }
  else if (index.data(Qt::UserRole) == LIST)
  {
    QString value = index.model()->data(index, Qt::DisplayRole).toString();
    QComboBox *cmb = static_cast<QComboBox*>(editor);
    cmb->setCurrentIndex(cmb->findText(value));
  }
  else if (index.data(Qt::UserRole) == MULTILIST)
  {
    QComboBox *cmb = static_cast<QComboBox*>(editor);
    QVariantMap map = index.data(Qt::UserRole+2).toMap();

    for(QVariantMap::const_iterator iter = map.constBegin(); iter != map.constEnd(); ++iter)
    {
      // qDebug() << iter.key() << " = " << iter.value();
      if (iter.value() == true)
        cmb->setItemData(cmb->findText(iter.key()), Qt::Checked, Qt::CheckStateRole);
      else
        cmb->setItemData(cmb->findText(iter.key()), Qt::Unchecked, Qt::CheckStateRole);
    }
  }
  else
  {
    QString value = index.model()->data(index, Qt::DisplayRole).toString();
    QLineEdit *le = static_cast<QLineEdit*>(editor);
    le->setText(value);
  }
}

void ConfDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex &index) const
{
  // Set data in model from the editor

  // qDebug() << "Setting model data (index " << index << ")";
  
  QVariant value;
  
  if (index.data(Qt::UserRole) == BOOL)
  {
    QComboBox *cmb = static_cast<QComboBox*>(editor);
    value = cmb->currentText();
  }
  else if (index.data(Qt::UserRole) == TIME ||
           index.data(Qt::UserRole) == INTEGER ||
           index.data(Qt::UserRole) == RESLIMIT ||
           index.data(Qt::UserRole) == SIZE)
  {
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    spinBox->interpretText();
    value = spinBox->value();
  }
  else if (index.data(Qt::UserRole) == LIST)
  {
    QComboBox *cmb = static_cast<QComboBox*>(editor);
    value = cmb->currentText();
  }
  else if (index.data(Qt::UserRole) == MULTILIST)
  {
    QComboBox *cmb = static_cast<QComboBox*>(editor);

    QVariantMap map;
    for(int i = 0; i < cmb->count(); i++)
    {
      if (cmb->itemData(i, Qt::CheckStateRole) == Qt::Checked)
        map[cmb->itemText(i)] = true;
      else
        map[cmb->itemText(i)] = false;
    }
    model->setData(index, QVariant(map), Qt::UserRole+2);
    return;
  }
  else
  {
    QLineEdit *le = static_cast<QLineEdit*>(editor);
    value = le->text();
  }
  // qDebug() << "before" << model->data(index, Qt::DisplayRole);
  model->setData(index, value, Qt::DisplayRole);
  // qDebug() << "after" << model->data(index, Qt::DisplayRole);
}

void ConfDelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}