From d94ea704cd568487a00432551b408c6f56fde315 Mon Sep 17 00:00:00 2001 From: zhangjiaping Date: Wed, 12 May 2021 16:00:01 +0800 Subject: [PATCH] =?UTF-8?q?Fix:=20Icon=20in=20detail=20view=20will=20not?= =?UTF-8?q?=20refresh=20when=20icon-theme=20changed.=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=B8=BB=E9=A2=98=E5=9B=BE=E6=A0=87=E6=94=B9=E5=8F=98=E6=97=B6?= =?UTF-8?q?=E8=AF=A6=E6=83=85=E9=A1=B5=E5=9B=BE=E6=A0=87=E6=9C=AA=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E7=9A=84=E9=97=AE=E9=A2=98.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/control/search-detail-view.cpp | 54 +++++++++++++++++++++++------- src/control/search-detail-view.h | 3 ++ 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/control/search-detail-view.cpp b/src/control/search-detail-view.cpp index 6f16695..319cbeb 100644 --- a/src/control/search-detail-view.cpp +++ b/src/control/search-detail-view.cpp @@ -30,7 +30,6 @@ #include #include #include -#include #include #include #include @@ -40,6 +39,7 @@ using namespace Zeeker; SearchDetailView::SearchDetailView(QWidget *parent) : QWidget(parent) { initUI(); + connect(qApp, &QApplication::paletteChanged, this, &SearchDetailView::refreshIcon); } SearchDetailView::~SearchDetailView() { @@ -200,9 +200,7 @@ void SearchDetailView::setAppWidget(const QString &appname, const QString &path, m_typeLabel->show(); m_hLine->show(); - QIcon icon; if(path.isEmpty() || path == "") { - icon = QIcon(iconpath); m_optionView->setupOptions(m_type, false); //未安装应用有一个label显示软件描述 if(description != "" && !description.isEmpty()) { @@ -210,17 +208,13 @@ void SearchDetailView::setAppWidget(const QString &appname, const QString &path, m_contentLabel->show(); m_contentLabel->setText(QString(tr("Introduction: %1")).arg(description)); } + setIcon(iconpath, false); } else { m_optionView->setupOptions(m_type, true); - if(QIcon::fromTheme(iconpath).isNull()) { - icon = QIcon(":/res/icons/desktop.png"); - } else { - icon = QIcon::fromTheme(iconpath); - } + setIcon(iconpath, true); } m_optionView->show(); - m_iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96)))); QFontMetrics fontMetrics = m_nameLabel->fontMetrics(); QString showname = fontMetrics.elidedText(m_name, Qt::ElideRight, 274); //当字体长度超过215时显示为省略号 m_nameLabel->setText(showname); @@ -352,8 +346,7 @@ void SearchDetailView::setupWidget(const int& type, const QString& path) { case SearchListView::ResType::Content: case SearchListView::ResType::Dir : case SearchListView::ResType::File : { - QIcon icon = FileUtils::getFileIcon(QUrl::fromLocalFile(path).toString()); - m_iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96)))); + setIcon(path); QFontMetrics fontMetrics = m_nameLabel->fontMetrics(); QString wholeName = FileUtils::getFileName(path); QString name = fontMetrics.elidedText(wholeName, Qt::ElideRight, 274); @@ -366,8 +359,7 @@ void SearchDetailView::setupWidget(const int& type, const QString& path) { break; } case SearchListView::ResType::Setting : { - QIcon icon = FileUtils::getSettingIcon(path, true); - m_iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96)))); + setIcon(path); QString settingType = path.mid(path.indexOf("/") + 1, path.lastIndexOf("/") - path.indexOf("/") - 1); //配置项所属控制面板插件名 m_nameLabel->setText(settingType); m_typeLabel->setText(FileUtils::getSettingName(path)); @@ -567,6 +559,42 @@ void SearchDetailView::initUI() { this->clearLayout(); //初始化时隐藏所有控件 } +/** + * @brief SearchDetailView::refreshIcon 图标主题变更时,刷新图标的槽函数 + */ +void SearchDetailView::refreshIcon() { + this->setIcon(m_iconPath); +} + +/** + * @brief SearchDetailView::setIcon 设置图标区域 + * @param path 图标路径或图标名 + * @param installed 如果是应用,是否已安装 + */ +void SearchDetailView::setIcon(const QString &path, const bool &installed) +{ + m_iconPath = path; + if (m_type == SearchListView::ResType::App) { + QIcon icon; + if(!installed) { + icon = QIcon(path); + } else { + if(QIcon::fromTheme(path).isNull()) { + icon = QIcon(":/res/icons/desktop.png"); + } else { + icon = QIcon::fromTheme(path); + } + } + m_iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96)))); + } else if (m_type == SearchListView::ResType::Setting) { + QIcon icon = FileUtils::getSettingIcon(path, true); + m_iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96)))); + } else { + QIcon icon = FileUtils::getFileIcon(QUrl::fromLocalFile(path).toString()); + m_iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96)))); + } +} + /** * @brief SearchDetailView::addDesktopShortcut 添加到桌面快捷方式 * @return diff --git a/src/control/search-detail-view.h b/src/control/search-detail-view.h index 96551da..a06cf87 100644 --- a/src/control/search-detail-view.h +++ b/src/control/search-detail-view.h @@ -71,6 +71,8 @@ private: QString m_pkgname = 0; //目前只有未安装应用在打开软件商店时需要此参数 void initUI(); + void setIcon(const QString& path, const bool &installed = true); + QString m_iconPath; QLabel * m_iconLabel = nullptr; QFrame * m_nameFrame = nullptr; QHBoxLayout * m_nameLayout = nullptr; @@ -105,6 +107,7 @@ Q_SIGNALS: void configFileChanged(); private Q_SLOTS: void execActions(const int&, const int&, const QString&); + void refreshIcon(); }; //此类用于url拦截