Migration of QtMvvm from github
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
2.9 KiB

#include "settingsentrymodel.h"
#include <QtCore/QRegularExpression>
using namespace QtMvvm;
const QList<int> SettingsEntryModel::FilterRoles {
SettingsEntryModel::GroupRole,
SettingsEntryModel::TitleRole,
SettingsEntryModel::ToolTipRole,
SettingsEntryModel::SearchKeysRole
};
SettingsEntryModel::SettingsEntryModel(QObject *parent) :
QAbstractListModel(parent),
_entries()
{}
void SettingsEntryModel::setup(const SettingsElements::Section &section, SettingsViewModel *viewModel, InputViewFactory *factory)
{
beginResetModel();
_entries.clear();
_viewModel = viewModel;
auto rIndex = 0;
for(auto group : section.groups) {
for(auto entry : group.entries) {
auto url = factory->getDelegate(entry.type, entry.properties);
if(group.title.isEmpty()) // unnamed groups are presented first
_entries.insert(rIndex++, EntryInfo{entry, url});
else
_entries.append(EntryInfo{entry, url, group});
}
}
endResetModel();
}
int SettingsEntryModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
else
return _entries.size();
}
QVariant SettingsEntryModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
7 years ago
auto entry = _entries.value(index.row());
switch (role) {
case Qt::DisplayRole:
case TitleRole:
7 years ago
return entry.title;
case KeyRole:
7 years ago
return entry.key;
case TypeRole:
7 years ago
return QString::fromUtf8(entry.type);
case ToolTipRole:
7 years ago
return entry.tooltip;
case DelegateUrlRole:
7 years ago
return entry.delegateUrl;
case SettingsValueRole:
7 years ago
return _viewModel->loadValue(entry.key, entry.defaultValue);
case PropertiesRole:
7 years ago
return entry.properties;
case GroupRole:
7 years ago
return entry.group.title;
case SearchKeysRole:
7 years ago
return entry.searchKeys;
default:
return QVariant();
}
}
bool SettingsEntryModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid() || role != SettingsValueRole)
return false;
_viewModel->saveValue(_entries.value(index.row()).key, value);
emit dataChanged(index, index, {SettingsValueRole});
return true;
}
QHash<int, QByteArray> SettingsEntryModel::roleNames() const
{
return {
{GroupRole, "group"},
{KeyRole, "key"},
{TypeRole, "type"},
{TitleRole, "title"},
{ToolTipRole, "tooltip"},
{DelegateUrlRole, "delegateUrl"},
{SettingsValueRole, "settingsValue"},
{PropertiesRole, "properties"}
};
}
Qt::ItemFlags SettingsEntryModel::flags(const QModelIndex &index) const
{
return QAbstractListModel::flags(index) | Qt::ItemIsEditable;
}
SettingsEntryModel::EntryInfo::EntryInfo(SettingsElements::Entry entry, const QUrl &delegateUrl, SettingsElements::Group group) :
Entry(entry),
delegateUrl(delegateUrl),
group(group)
{
static const QRegularExpression nameRegex(QStringLiteral("&(?!&)"),
QRegularExpression::DontCaptureOption |
QRegularExpression::OptimizeOnFirstUsageOption);
title.remove(nameRegex);
group.entries.clear();
}