diff --git a/launcher/minecraft/mod/ModFolderModel.cpp b/launcher/minecraft/mod/ModFolderModel.cpp index bc1e32406..1aaf1d09f 100644 --- a/launcher/minecraft/mod/ModFolderModel.cpp +++ b/launcher/minecraft/mod/ModFolderModel.cpp @@ -139,8 +139,10 @@ QVariant ModFolderModel::data(const QModelIndex& index, int role) const case Qt::CheckStateRole: if (column == ActiveColumn) return at(row).enabled() ? Qt::Checked : Qt::Unchecked; - else if (column == LockUpdateCoumn) - return !at(row).lockUpdate() ? Qt::Checked : Qt::Unchecked; + return QVariant(); + case Qt::UserRole: + if (column == LockUpdateCoumn) + return at(row).lockUpdate(); return QVariant(); default: break; diff --git a/launcher/minecraft/mod/Resource.h b/launcher/minecraft/mod/Resource.h index dc9605d25..8b100abc4 100644 --- a/launcher/minecraft/mod/Resource.h +++ b/launcher/minecraft/mod/Resource.h @@ -121,6 +121,7 @@ class Resource : public QObject { auto metadata() const -> std::shared_ptr { return m_metadata; } auto provider() const -> QString; virtual auto homepage() const -> QString; + bool lockUpdate() const; void setStatus(ResourceStatus status) { m_status = status; } void setMetadata(std::shared_ptr&& metadata); diff --git a/launcher/minecraft/mod/ResourceFolderModel.cpp b/launcher/minecraft/mod/ResourceFolderModel.cpp index 9577a223a..80d4beb8d 100644 --- a/launcher/minecraft/mod/ResourceFolderModel.cpp +++ b/launcher/minecraft/mod/ResourceFolderModel.cpp @@ -596,8 +596,10 @@ QVariant ResourceFolderModel::data(const QModelIndex& index, int role) const case Qt::CheckStateRole: if (column == ActiveColumn) { return m_resources[row]->enabled() ? Qt::Checked : Qt::Unchecked; - } else if (column == LockUpdateCoumn) { - return !at(row).lockUpdate() ? Qt::Checked : Qt::Unchecked; + } + case Qt::UserRole: + if (column == LockUpdateCoumn) { + return at(row).lockUpdate(); } return {}; default: @@ -612,10 +614,10 @@ bool ResourceFolderModel::setData(const QModelIndex& index, [[maybe_unused]] con return false; } + if (role == Qt::UserRole && columnNames(false).at(index.column()) == "Update") { + return setModUpdate({ index }, EnableAction::TOGGLE); + } if (role == Qt::CheckStateRole) { - if (columnNames(false).at(index.column()) == "Update") { - return setModUpdate({ index }, EnableAction::TOGGLE); - } return setResourceEnabled({ index }, EnableAction::TOGGLE); } diff --git a/launcher/minecraft/mod/ResourcePackFolderModel.cpp b/launcher/minecraft/mod/ResourcePackFolderModel.cpp index 08e8d2b90..ae11fe3d5 100644 --- a/launcher/minecraft/mod/ResourcePackFolderModel.cpp +++ b/launcher/minecraft/mod/ResourcePackFolderModel.cpp @@ -97,8 +97,10 @@ QVariant ResourcePackFolderModel::data(const QModelIndex& index, int role) const case Qt::CheckStateRole: if (column == ActiveColumn) { return at(row).enabled() ? Qt::Checked : Qt::Unchecked; - } else if (column == LockUpdateCoumn) { - return !at(row).lockUpdate() ? Qt::Checked : Qt::Unchecked; + } + case Qt::UserRole: + if (column == LockUpdateCoumn) { + return at(row).lockUpdate(); } return {}; default: diff --git a/launcher/minecraft/mod/TexturePackFolderModel.cpp b/launcher/minecraft/mod/TexturePackFolderModel.cpp index 79a803d1d..6ab825ce6 100644 --- a/launcher/minecraft/mod/TexturePackFolderModel.cpp +++ b/launcher/minecraft/mod/TexturePackFolderModel.cpp @@ -82,10 +82,12 @@ QVariant TexturePackFolderModel::data(const QModelIndex& index, int role) const case Qt::CheckStateRole: if (column == ActiveColumn) { return m_resources[row]->enabled() ? Qt::Checked : Qt::Unchecked; - } else if (column == LockUpdateCoumn) { - return !m_resources[row]->lockUpdate() ? Qt::Checked : Qt::Unchecked; } return {}; + case Qt::UserRole: + if (column == LockUpdateCoumn) + return at(row).lockUpdate(); + return {}; default: break; } diff --git a/launcher/ui/pages/instance/ExternalResourcesPage.cpp b/launcher/ui/pages/instance/ExternalResourcesPage.cpp index 138a0cfdd..90e1ec0d0 100644 --- a/launcher/ui/pages/instance/ExternalResourcesPage.cpp +++ b/launcher/ui/pages/instance/ExternalResourcesPage.cpp @@ -45,8 +45,45 @@ #include #include #include +#include #include +class LockDelegate : public QStyledItemDelegate { + public: + explicit LockDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent) {} + + void paint(QPainter* painter, const QStyleOptionViewItem& opt, const QModelIndex& index) const override + { + QStyleOptionViewItem option(opt); + initStyleOption(&option, index); + + bool locked = index.data(Qt::UserRole).toBool(); + + const QIcon& icon = QIcon::fromTheme(locked ? "lock" : "unlock"); + + // Draw default background / selection + option.text.clear(); + option.icon = QIcon(); + + option.widget->style()->drawControl(QStyle::CE_ItemViewItem, &option, painter); + + int size = qMin(option.rect.width(), option.rect.height()) * 0.75; + QRect iconRect(option.rect.center().x() - size / 2, option.rect.center().y() - size / 2, size, size); + + icon.paint(painter, iconRect); + } + + bool editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index) override + { + if (event->type() == QEvent::MouseButtonRelease) { + bool locked = index.data(Qt::UserRole).toBool(); + model->setData(index, !locked, Qt::UserRole); + return true; + } + return event->type() == QEvent::MouseButtonDblClick; // if double click ignore it + } +}; + ExternalResourcesPage::ExternalResourcesPage(BaseInstance* instance, ResourceFolderModel* model, QWidget* parent) : QMainWindow(parent), m_instance(instance), ui(new Ui::ExternalResourcesPage), m_model(model) { @@ -61,6 +98,10 @@ ExternalResourcesPage::ExternalResourcesPage(BaseInstance* instance, ResourceFol m_filterModel->setSourceModel(m_model); m_filterModel->setFilterKeyColumn(-1); ui->treeView->setModel(m_filterModel); + + // keep the Update at the end of the list(otherwise there will be a need to iterate over the columns) + int lockColumn = model->columnNames(false).size() - 1; + ui->treeView->setItemDelegateForColumn(lockColumn, new LockDelegate(ui->treeView)); // must come after setModel ui->treeView->setResizeModes(m_model->columnResizeModes());