File: confmodel.cpp

package info (click to toggle)
kde-config-systemd 1.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,560 kB
  • ctags: 688
  • sloc: cpp: 3,306; makefile: 9; sh: 2
file content (137 lines) | stat: -rw-r--r-- 4,432 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
/*******************************************************************************
 * 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 "confmodel.h"

#include <QFont>

#include <KLocalizedString>

ConfModel::ConfModel(QObject *parent, QList<confOption> *confOptList)
 : QAbstractTableModel(parent)
 , m_optList(confOptList)
{
}

int ConfModel::rowCount(const QModelIndex &) const
{
  return m_optList->size();
}

int ConfModel::columnCount(const QModelIndex &) const
{
  return 3;
}

QVariant ConfModel::headerData(int section, Qt::Orientation orientation, int role) const
{
  if (orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0)
    return i18n("Item");
  if (orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 1)
    return i18n("Value");
  return QVariant();
}

QVariant ConfModel::data(const QModelIndex &index, int role) const
{

  if (!index.isValid() || index.row() >= m_optList->size()) {
    return QVariant();
  }
  const confOption *opt = &m_optList->at(index.row());

  if (role == Qt::DisplayRole)
  {
    if (index.column() == 0)
    {
      if (opt->realName == "RuntimeDirectorySize")
        return i18nc("configuration name (unit)", "%1 (%)",
                     opt->realName);
      else if (opt->type == SIZE)
        return i18nc("configuration name (unit)", "%1 (MB)",
                     opt->realName);
      else if (opt->type == TIME)
        return i18nc("configuration name (unit)", "%1 (%2)",
                     opt->realName,
                     opt->getTimeUnit());
      else
        return opt->realName;
    }
    else if (index.column() == 1)
      return opt->getValueAsString();
    else if (index.column() == 2)
      return opt->getFilename();
  }
  else if (role == Qt::UserRole && index.column() == 1)
  {
    // Holds the type, used in the delegate to set different
    // types of editor widgets
    return opt->type;
  }
  else if (role == Qt::UserRole+1 && index.column() == 1)
  {
    // Holds the uniqueName, used in createEditor in the delegate,
    // to set a pointer to the correct confOption.
    return opt->uniqueName;
  }
  else if (role == Qt::UserRole+2 && index.column() == 1)
  {
    // Holds a QVariantMap, used in the delegate to save/retrieve selected
    // values in comboboxes for type MULTILIST
    return QVariant(opt->getValue().toMap());
  }
  else if (role == Qt::FontRole && !opt->isDefault())
  {
    // Set font to bold if value is not default
    QFont newfont;
    newfont.setBold(true);
    return newfont;
  }
  else if (role == Qt::ToolTipRole)
  {
    return opt->toolTip;
  }

  return QVariant();
}

bool ConfModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
  if (!value.isValid())
    return false;

  if (role == Qt::DisplayRole && index.column() == 1)
  {
    (*m_optList)[index.row()].setValue(value);
  }
  else if (role == Qt::UserRole+2 && index.column() == 1)
  {
    (*m_optList)[index.row()].setValue(value);
  }

  emit dataChanged(index, index);
  return true;
}

Qt::ItemFlags ConfModel::flags ( const QModelIndex & index ) const
{
  Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
  if (index.column() == 1) {
    return flags | Qt::ItemIsEditable;
  }
  return flags;
}