Commit 76af2694 authored by Kai Uwe Broulik's avatar Kai Uwe Broulik 🍇

[Folder View Config] Make model writable and drop custom setRowChecked method

By implementing setData we make the model magic property writable and can directly assign model.checked a new value.
Not only is this cleaner but it will just work with proxy models.

Differential Revision: https://phabricator.kde.org/D6938
parent 2bebd7bb
......@@ -103,12 +103,7 @@ Item {
Layout.maximumWidth: 18 // FIXME HACK: Use actual radio button width.
checked: model.checked
onCheckedChanged: {
if (checked != model.checked) {
mimeTypesModel.setRowChecked(model.index, checked);
}
}
onCheckedChanged: model.checked = checked
}
PlasmaCore.IconItem {
......
......@@ -62,12 +62,7 @@ ApplicationWindow {
text: model.display
checked: model.checked
onCheckedChanged: {
if (checked != model.checked) {
previewPluginsModel.setRowChecked(model.index, checked);
}
}
onCheckedChanged: model.checked = checked
}
}
}
......
......@@ -76,15 +76,23 @@ QVariant MimeTypesModel::data(const QModelIndex &index, int role) const
return QVariant();
}
void MimeTypesModel::setRowChecked(int row, bool checked)
bool MimeTypesModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
checkedRows[row] = checked;
QModelIndex idx = index(row, 0);
if (index.row() < 0 || index.row() >= m_mimeTypesList.size()) {
return false;
}
emit dataChanged(idx, idx);
if (role == Qt::CheckStateRole) {
const bool newChecked = value.toBool();
if (checkedRows.at(index.row()) != newChecked) {
checkedRows[index.row()] = newChecked;
emit dataChanged(index, index, {role});
emit checkedTypesChanged();
return true;
}
}
emit checkedTypesChanged();
return false;
}
void MimeTypesModel::checkAll()
......@@ -153,13 +161,6 @@ FilterableMimeTypesModel::~FilterableMimeTypesModel()
{
}
void FilterableMimeTypesModel::setRowChecked(int row, bool checked)
{
QModelIndex idx = index(row, 0);
m_sourceModel->setRowChecked(mapToSource(idx).row(), checked);
}
void FilterableMimeTypesModel::checkAll()
{
m_sourceModel->checkAll();
......
......@@ -38,9 +38,9 @@ class MimeTypesModel : public QAbstractListModel
QHash<int, QByteArray> roleNames() const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
bool setData(const QModelIndex &index, const QVariant &value, int role) Q_DECL_OVERRIDE;
Q_INVOKABLE void checkAll();
Q_INVOKABLE void setRowChecked(int row, bool checked);
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE { Q_UNUSED(parent) return m_mimeTypesList.size(); }
......@@ -68,7 +68,6 @@ class FilterableMimeTypesModel : public QSortFilterProxyModel
FilterableMimeTypesModel(QObject *parent = 0);
~FilterableMimeTypesModel();
Q_INVOKABLE void setRowChecked(int row, bool checked);
Q_INVOKABLE void checkAll();
QStringList checkedTypes() const;
......
......@@ -65,13 +65,19 @@ QVariant PreviewPluginsModel::data(const QModelIndex &index, int role) const
return QVariant();
}
void PreviewPluginsModel::setRowChecked(int row, bool checked)
bool PreviewPluginsModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
m_checkedRows[row] = checked;
if (index.row() < 0 || index.row() >= m_plugins.size()) {
return false;
}
QModelIndex idx = index(row, 0);
if (role == Qt::CheckStateRole) {
m_checkedRows[index.row()] = value.toBool();
emit dataChanged(index, index, {role});
return true;
}
emit dataChanged(idx, idx);
return false;
}
int PreviewPluginsModel::indexOfPlugin(const QString &name) const
......
......@@ -38,7 +38,7 @@ class PreviewPluginsModel : public QAbstractListModel
QHash<int, QByteArray> roleNames() const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
Q_INVOKABLE void setRowChecked(int row, bool checked);
bool setData(const QModelIndex &index, const QVariant &value, int role) Q_DECL_OVERRIDE;
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE { Q_UNUSED(parent) return m_plugins.size(); }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment