Fix: Font-size & row height will not refresh in search list.

修复字体大小改变时搜索结果列表行高和fontmetircs不刷新的bug.
This commit is contained in:
zhangjiaping 2021-04-15 15:06:24 +08:00
parent b4775df2eb
commit a5054306a1
9 changed files with 62 additions and 3 deletions

View File

@ -77,8 +77,18 @@ GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent)
if (key == STYLE_NAME_KEY) { if (key == STYLE_NAME_KEY) {
//当前主题改变时也发出paletteChanged信号通知主界面刷新 //当前主题改变时也发出paletteChanged信号通知主界面刷新
qApp->paletteChanged(qApp->palette()); qApp->paletteChanged(qApp->palette());
m_cache.remove(STYLE_NAME_KEY);
m_cache.insert(STYLE_NAME_KEY, m_theme_gsettings->get(STYLE_NAME_KEY).toString());
} else if (key == FONT_SIZE_KEY) {
qApp->paletteChanged(qApp->palette());
m_cache.remove(FONT_SIZE_KEY);
m_cache.insert(FONT_SIZE_KEY, m_theme_gsettings->get(FONT_SIZE_KEY).toDouble());
} }
}); });
m_cache.remove(STYLE_NAME_KEY);
m_cache.insert(STYLE_NAME_KEY, m_theme_gsettings->get(STYLE_NAME_KEY).toString());
m_cache.remove(FONT_SIZE_KEY);
m_cache.insert(FONT_SIZE_KEY, m_theme_gsettings->get(FONT_SIZE_KEY).toDouble());
} }
} }

View File

@ -39,6 +39,7 @@
#define TRANSPARENCY_KEY "transparency" #define TRANSPARENCY_KEY "transparency"
#define THEME_GSETTINGS_ID "org.ukui.style" #define THEME_GSETTINGS_ID "org.ukui.style"
#define STYLE_NAME_KEY "styleName" #define STYLE_NAME_KEY "styleName"
#define FONT_SIZE_KEY "systemFontSize"
#define INDEX_DATABASE_STATE "index_database_state" #define INDEX_DATABASE_STATE "index_database_state"
#define CONTENT_INDEX_DATABASE_STATE "content_index_database_state" #define CONTENT_INDEX_DATABASE_STATE "content_index_database_state"
#define INDEX_GENERATOR_NORMAL_EXIT "index_generator_normal_exit" #define INDEX_GENERATOR_NORMAL_EXIT "index_generator_normal_exit"

View File

@ -25,6 +25,7 @@
#include <QDebug> #include <QDebug>
#include <QTextDocument> #include <QTextDocument>
#include <QAbstractTextDocumentLayout> #include <QAbstractTextDocumentLayout>
#include "global-settings.h"
HighlightItemDelegate::HighlightItemDelegate(QObject *parent) : QStyledItemDelegate (parent) HighlightItemDelegate::HighlightItemDelegate(QObject *parent) : QStyledItemDelegate (parent)
{ {
@ -83,8 +84,11 @@ QString HighlightItemDelegate::getHtmlText(QPainter *painter, const QStyleOption
{ {
int indexFindLeft = 0; int indexFindLeft = 0;
QString indexString = index.model()->data(index,Qt::DisplayRole).toString(); QString indexString = index.model()->data(index,Qt::DisplayRole).toString();
QFontMetrics m_QFontMetrics = painter->fontMetrics(); QFont ft(painter->font().family(), GlobalSettings::getInstance()->getValue(FONT_SIZE_KEY).toInt());
QString indexColString = m_QFontMetrics.elidedText(indexString, Qt::ElideRight, itemOption.rect.width() + 10); //当字体超过Item的长度时显示为省略号 QFontMetrics fm(ft);
QString indexColString = fm.elidedText(indexString, Qt::ElideRight, itemOption.rect.width() + 10); //当字体超过Item的长度时显示为省略号
// QFontMetrics m_QFontMetrics = painter->fontMetrics();
// QString indexColString = m_QFontMetrics.elidedText(indexString, Qt::ElideRight, itemOption.rect.width() + 10); //当字体超过Item的长度时显示为省略号
QString htmlString; QString htmlString;
if ((indexColString.toUpper()).contains((m_regFindKeyWords.toUpper()))) { if ((indexColString.toUpper()).contains((m_regFindKeyWords.toUpper()))) {
indexFindLeft = indexColString.toUpper().indexOf(m_regFindKeyWords.toUpper()); //得到查找字体在当前整个Item字体中的位置 indexFindLeft = indexColString.toUpper().indexOf(m_regFindKeyWords.toUpper()); //得到查找字体在当前整个Item字体中的位置

View File

@ -35,7 +35,11 @@ HomePageItem::HomePageItem(QWidget *parent, const int& type, const QString& path
connect(qApp, &QApplication::paletteChanged, this, [ = ]() { connect(qApp, &QApplication::paletteChanged, this, [ = ]() {
if (m_namelabel) { if (m_namelabel) {
QString name = this->toolTip(); QString name = this->toolTip();
m_namelabel->setText(m_namelabel->fontMetrics().elidedText(name, Qt::ElideRight, 108)); if (m_type == ItemType::Recent) {
m_namelabel->setText(m_namelabel->fontMetrics().elidedText(name, Qt::ElideRight, 250));
} else {
m_namelabel->setText(m_namelabel->fontMetrics().elidedText(name, Qt::ElideRight, 108));
}
} }
}); });
} }
@ -51,6 +55,7 @@ HomePageItem::~HomePageItem()
*/ */
void HomePageItem::setupUi(const int& type, const QString& path) { void HomePageItem::setupUi(const int& type, const QString& path) {
m_path = path; m_path = path;
m_type = type;
m_widget = new QWidget(this); m_widget = new QWidget(this);
m_widget->setObjectName("MainWidget"); m_widget->setObjectName("MainWidget");
// m_widget->setStyleSheet("QWidget#MainWidget{background: rgba(0, 0, 0, 0.05); border-radius: 4px;}"); // m_widget->setStyleSheet("QWidget#MainWidget{background: rgba(0, 0, 0, 0.05); border-radius: 4px;}");

View File

@ -56,6 +56,7 @@ private:
QLabel * m_namelabel = nullptr; QLabel * m_namelabel = nullptr;
double m_transparency = 0; double m_transparency = 0;
QString m_path; QString m_path;
int m_type = 0;
}; };
#endif // HOMEPAGEITEM_H #endif // HOMEPAGEITEM_H

View File

@ -21,9 +21,13 @@
#include "search-list-view.h" #include "search-list-view.h"
#include <QDebug> #include <QDebug>
#include <QFileInfo> #include <QFileInfo>
#include "custom-style.h"
SearchListView::SearchListView(QWidget * parent, const QStringList& list, const int& type) : QTreeView(parent) SearchListView::SearchListView(QWidget * parent, const QStringList& list, const int& type) : QTreeView(parent)
{ {
CustomStyle * style = new CustomStyle(GlobalSettings::getInstance()->getValue(STYLE_NAME_KEY).toString());
this->setStyle(style);
this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setSelectionBehavior(QAbstractItemView::SelectRows); setSelectionBehavior(QAbstractItemView::SelectRows);
setSelectionMode(QAbstractItemView::SingleSelection); setSelectionMode(QAbstractItemView::SingleSelection);

18
src/custom-style.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "custom-style.h"
CustomStyle::CustomStyle(const QString &proxyStyleName, QObject *parent) : QProxyStyle(proxyStyleName)
{
}
QSize CustomStyle::sizeFromContents(QStyle::ContentsType type, const QStyleOption *option, const QSize &contentsSize, const QWidget *widget) const
{
switch (type) {
case CT_ItemViewItem: {
QSize size(0, GlobalSettings::getInstance()->getValue(FONT_SIZE_KEY).toDouble() * 2);
return size;
} break;
default: break;
}
return QProxyStyle::sizeFromContents(type, option, contentsSize, widget);
}

14
src/custom-style.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef CUSTOMSTYLE_H
#define CUSTOMSTYLE_H
#include <QProxyStyle>
#include "global-settings.h"
class CustomStyle : public QProxyStyle
{
Q_OBJECT
public:
explicit CustomStyle(const QString &proxyStyleName = "windows",QObject *parent = nullptr);
virtual QSize sizeFromContents(QStyle::ContentsType type, const QStyleOption *option, const QSize &contentsSize, const QWidget *widget = nullptr) const;
};
#endif // CUSTOMSTYLE_H

View File

@ -26,6 +26,7 @@ include(singleapplication/qt-single-application.pri)
SOURCES += \ SOURCES += \
content-widget.cpp \ content-widget.cpp \
custom-style.cpp \
input-box.cpp \ input-box.cpp \
main.cpp \ main.cpp \
mainwindow.cpp \ mainwindow.cpp \
@ -37,6 +38,7 @@ SOURCES += \
HEADERS += \ HEADERS += \
content-widget.h \ content-widget.h \
custom-style.h \
input-box.h \ input-box.h \
mainwindow.h \ mainwindow.h \
search-app-thread.h \ search-app-thread.h \