Fix: Icon in detail view will not refresh when icon-theme changed.

修复主题图标改变时详情页图标未更新的问题.
This commit is contained in:
zhangjiaping 2021-05-12 16:00:01 +08:00
parent f4ea89dec8
commit d94ea704cd
2 changed files with 44 additions and 13 deletions

View File

@ -30,7 +30,6 @@
#include <QFileInfo> #include <QFileInfo>
#include <QProcess> #include <QProcess>
#include <QClipboard> #include <QClipboard>
#include <QApplication>
#include <QFileInfo> #include <QFileInfo>
#include <QDateTime> #include <QDateTime>
#include <QDBusMetaType> #include <QDBusMetaType>
@ -40,6 +39,7 @@
using namespace Zeeker; using namespace Zeeker;
SearchDetailView::SearchDetailView(QWidget *parent) : QWidget(parent) { SearchDetailView::SearchDetailView(QWidget *parent) : QWidget(parent) {
initUI(); initUI();
connect(qApp, &QApplication::paletteChanged, this, &SearchDetailView::refreshIcon);
} }
SearchDetailView::~SearchDetailView() { SearchDetailView::~SearchDetailView() {
@ -200,9 +200,7 @@ void SearchDetailView::setAppWidget(const QString &appname, const QString &path,
m_typeLabel->show(); m_typeLabel->show();
m_hLine->show(); m_hLine->show();
QIcon icon;
if(path.isEmpty() || path == "") { if(path.isEmpty() || path == "") {
icon = QIcon(iconpath);
m_optionView->setupOptions(m_type, false); m_optionView->setupOptions(m_type, false);
//未安装应用有一个label显示软件描述 //未安装应用有一个label显示软件描述
if(description != "" && !description.isEmpty()) { if(description != "" && !description.isEmpty()) {
@ -210,17 +208,13 @@ void SearchDetailView::setAppWidget(const QString &appname, const QString &path,
m_contentLabel->show(); m_contentLabel->show();
m_contentLabel->setText(QString(tr("Introduction: %1")).arg(description)); m_contentLabel->setText(QString(tr("Introduction: %1")).arg(description));
} }
setIcon(iconpath, false);
} else { } else {
m_optionView->setupOptions(m_type, true); m_optionView->setupOptions(m_type, true);
if(QIcon::fromTheme(iconpath).isNull()) { setIcon(iconpath, true);
icon = QIcon(":/res/icons/desktop.png");
} else {
icon = QIcon::fromTheme(iconpath);
}
} }
m_optionView->show(); m_optionView->show();
m_iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96))));
QFontMetrics fontMetrics = m_nameLabel->fontMetrics(); QFontMetrics fontMetrics = m_nameLabel->fontMetrics();
QString showname = fontMetrics.elidedText(m_name, Qt::ElideRight, 274); //当字体长度超过215时显示为省略号 QString showname = fontMetrics.elidedText(m_name, Qt::ElideRight, 274); //当字体长度超过215时显示为省略号
m_nameLabel->setText(showname); m_nameLabel->setText(showname);
@ -352,8 +346,7 @@ void SearchDetailView::setupWidget(const int& type, const QString& path) {
case SearchListView::ResType::Content: case SearchListView::ResType::Content:
case SearchListView::ResType::Dir : case SearchListView::ResType::Dir :
case SearchListView::ResType::File : { case SearchListView::ResType::File : {
QIcon icon = FileUtils::getFileIcon(QUrl::fromLocalFile(path).toString()); setIcon(path);
m_iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96))));
QFontMetrics fontMetrics = m_nameLabel->fontMetrics(); QFontMetrics fontMetrics = m_nameLabel->fontMetrics();
QString wholeName = FileUtils::getFileName(path); QString wholeName = FileUtils::getFileName(path);
QString name = fontMetrics.elidedText(wholeName, Qt::ElideRight, 274); QString name = fontMetrics.elidedText(wholeName, Qt::ElideRight, 274);
@ -366,8 +359,7 @@ void SearchDetailView::setupWidget(const int& type, const QString& path) {
break; break;
} }
case SearchListView::ResType::Setting : { case SearchListView::ResType::Setting : {
QIcon icon = FileUtils::getSettingIcon(path, true); setIcon(path);
m_iconLabel->setPixmap(icon.pixmap(icon.actualSize(QSize(96, 96))));
QString settingType = path.mid(path.indexOf("/") + 1, path.lastIndexOf("/") - path.indexOf("/") - 1); //配置项所属控制面板插件名 QString settingType = path.mid(path.indexOf("/") + 1, path.lastIndexOf("/") - path.indexOf("/") - 1); //配置项所属控制面板插件名
m_nameLabel->setText(settingType); m_nameLabel->setText(settingType);
m_typeLabel->setText(FileUtils::getSettingName(path)); m_typeLabel->setText(FileUtils::getSettingName(path));
@ -567,6 +559,42 @@ void SearchDetailView::initUI() {
this->clearLayout(); //初始化时隐藏所有控件 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 * @brief SearchDetailView::addDesktopShortcut
* @return * @return

View File

@ -71,6 +71,8 @@ private:
QString m_pkgname = 0; //目前只有未安装应用在打开软件商店时需要此参数 QString m_pkgname = 0; //目前只有未安装应用在打开软件商店时需要此参数
void initUI(); void initUI();
void setIcon(const QString& path, const bool &installed = true);
QString m_iconPath;
QLabel * m_iconLabel = nullptr; QLabel * m_iconLabel = nullptr;
QFrame * m_nameFrame = nullptr; QFrame * m_nameFrame = nullptr;
QHBoxLayout * m_nameLayout = nullptr; QHBoxLayout * m_nameLayout = nullptr;
@ -105,6 +107,7 @@ Q_SIGNALS:
void configFileChanged(); void configFileChanged();
private Q_SLOTS: private Q_SLOTS:
void execActions(const int&, const int&, const QString&); void execActions(const int&, const int&, const QString&);
void refreshIcon();
}; };
//此类用于url拦截 //此类用于url拦截