From 2f91f8108923e70412a295ff9000fd3f9f9a852f Mon Sep 17 00:00:00 2001 From: David Rosca Date: Sun, 20 Mar 2016 19:22:26 +0100 Subject: [PATCH] GM Settings: Fix redrawing items when script updating state changes --- .../GreaseMonkey/settings/gm_settings.cpp | 12 +++++------- .../settings/gm_settingslistdelegate.cpp | 18 +++++++++++------- .../settings/gm_settingslistwidget.cpp | 9 +++++++-- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/plugins/GreaseMonkey/settings/gm_settings.cpp b/src/plugins/GreaseMonkey/settings/gm_settings.cpp index 63ac24bdd..da220bc43 100644 --- a/src/plugins/GreaseMonkey/settings/gm_settings.cpp +++ b/src/plugins/GreaseMonkey/settings/gm_settings.cpp @@ -1,6 +1,6 @@ /* ============================================================ * GreaseMonkey plugin for QupZilla -* Copyright (C) 2012-2013 David Rosca +* Copyright (C) 2012-2016 David Rosca * * 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 @@ -155,17 +155,15 @@ void GM_Settings::loadScripts() QListWidgetItem* item = new QListWidgetItem(ui->listWidget); QIcon icon = QIcon(":/gm/data/script.png"); item->setIcon(icon); - item->setText(script->name()); - item->setData(Qt::UserRole, script->version()); - item->setData(Qt::UserRole + 1, script->description()); - item->setData(Qt::UserRole + 2, !script->downloadUrl().isEmpty()); - item->setData(Qt::UserRole + 3, script->isUpdating()); - item->setFlags(item->flags() | Qt::ItemIsUserCheckable); item->setCheckState(script->isEnabled() ? Qt::Checked : Qt::Unchecked); item->setData(Qt::UserRole + 10, QVariant::fromValue((void*)script)); + connect(script, &GM_Script::updatingChanged, this, [this]() { + ui->listWidget->viewport()->update(); + }); + ui->listWidget->addItem(item); } diff --git a/src/plugins/GreaseMonkey/settings/gm_settingslistdelegate.cpp b/src/plugins/GreaseMonkey/settings/gm_settingslistdelegate.cpp index 4d4f5fff2..ba436a354 100644 --- a/src/plugins/GreaseMonkey/settings/gm_settingslistdelegate.cpp +++ b/src/plugins/GreaseMonkey/settings/gm_settingslistdelegate.cpp @@ -1,6 +1,6 @@ /* ============================================================ * GreaseMonkey plugin for QupZilla -* Copyright (C) 2012 David Rosca +* Copyright (C) 2012-2016 David Rosca * * 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 @@ -16,6 +16,7 @@ * along with this program. If not, see . * ============================================================ */ #include "gm_settingslistdelegate.h" +#include "gm_script.h" #include "iconprovider.h" @@ -39,6 +40,10 @@ int GM_SettingsListDelegate::padding() const void GM_SettingsListDelegate::paint(QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { + GM_Script *script = static_cast(index.data(Qt::UserRole + 10).value()); + if (!script) + return; + QStyleOptionViewItemV4 opt = option; initStyleOption(&opt, index); @@ -64,7 +69,7 @@ void GM_SettingsListDelegate::paint(QPainter* painter, const QStyleOptionViewIte int leftPosition = m_padding; int rightPosition = opt.rect.right() - m_padding - 16; // 16 for remove button - if (index.data(Qt::UserRole + 2).toBool()) + if (!script->downloadUrl().isEmpty()) rightPosition -= m_padding + 16; // 16 for update button // Draw background @@ -98,25 +103,24 @@ void GM_SettingsListDelegate::paint(QPainter* painter, const QStyleOptionViewIte style->drawItemText(painter, nameRect, Qt::AlignLeft, opt.palette, true, name, colorRole); // Draw version - const QString version = index.data(Qt::UserRole).toString(); QRect versionRect(nameRect.x() + leftPosForVersion, nameRect.y(), rightTitleEdge - leftPosForVersion, titleMetrics.height()); QFont versionFont = titleFont; versionFont.setBold(false); painter->setFont(versionFont); - style->drawItemText(painter, versionRect, Qt::AlignLeft, opt.palette, true, version, colorRole); + style->drawItemText(painter, versionRect, Qt::AlignLeft, opt.palette, true, script->version(), colorRole); // Draw description const int infoYPos = nameRect.bottom() + opt.fontMetrics.leading(); QRect infoRect(nameRect.x(), infoYPos, nameRect.width(), opt.fontMetrics.height()); - const QString info = opt.fontMetrics.elidedText(index.data(Qt::UserRole + 1).toString(), Qt::ElideRight, infoRect.width()); + const QString info = opt.fontMetrics.elidedText(script->description(), Qt::ElideRight, infoRect.width()); painter->setFont(opt.font); style->drawItemText(painter, infoRect, Qt::TextSingleLine | Qt::AlignLeft, opt.palette, true, info, colorRole); // Draw update button - if (index.data(Qt::UserRole + 2).toBool()) { + if (!script->downloadUrl().isEmpty()) { const int updateIconSize = 16; const int updateIconYPos = center - (updateIconSize / 2); - const QPixmap updatePixmap = m_updateIcon.pixmap(16, index.data(Qt::UserRole + 3).toBool() ? QIcon::Disabled : QIcon::Normal); + const QPixmap updatePixmap = m_updateIcon.pixmap(16, script->isUpdating() ? QIcon::Disabled : QIcon::Normal); QRect updateIconRect(rightPosition, updateIconYPos, updateIconSize, updateIconSize); painter->drawPixmap(updateIconRect, updatePixmap); rightPosition += m_padding + 16; diff --git a/src/plugins/GreaseMonkey/settings/gm_settingslistwidget.cpp b/src/plugins/GreaseMonkey/settings/gm_settingslistwidget.cpp index 55a90ccaf..a0b9dcb76 100644 --- a/src/plugins/GreaseMonkey/settings/gm_settingslistwidget.cpp +++ b/src/plugins/GreaseMonkey/settings/gm_settingslistwidget.cpp @@ -1,6 +1,6 @@ /* ============================================================ * GreaseMonkey plugin for QupZilla -* Copyright (C) 2012 David Rosca +* Copyright (C) 2012-2016 David Rosca * * 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 @@ -17,6 +17,7 @@ * ============================================================ */ #include "gm_settingslistwidget.h" #include "gm_settingslistdelegate.h" +#include "gm_script.h" #include @@ -72,7 +73,11 @@ bool GM_SettingsListWidget::containsRemoveIcon(const QPoint &pos) const bool GM_SettingsListWidget::containsUpdateIcon(const QPoint &pos) const { QListWidgetItem *item = itemAt(pos); - if (!item || !item->data(Qt::UserRole + 2).toBool()) + if (!item) + return false; + + GM_Script *script = static_cast(item->data(Qt::UserRole + 10).value()); + if (!script || script->downloadUrl().isEmpty()) return false; const QRect rect = visualItemRect(item);