From a5054306a1529c46e9a2150cde00beb95c8815cc Mon Sep 17 00:00:00 2001 From: zhangjiaping Date: Thu, 15 Apr 2021 15:06:24 +0800 Subject: [PATCH] =?UTF-8?q?Fix:=20Font-size=20&=20row=20height=20will=20no?= =?UTF-8?q?t=20refresh=20in=20search=20list.=20=E4=BF=AE=E5=A4=8D=E5=AD=97?= =?UTF-8?q?=E4=BD=93=E5=A4=A7=E5=B0=8F=E6=94=B9=E5=8F=98=E6=97=B6=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E7=BB=93=E6=9E=9C=E5=88=97=E8=A1=A8=E8=A1=8C=E9=AB=98?= =?UTF-8?q?=E5=92=8Cfontmetircs=E4=B8=8D=E5=88=B7=E6=96=B0=E7=9A=84bug.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libsearch/global-settings.cpp | 10 ++++++++++ libsearch/global-settings.h | 1 + src/control/highlight-item-delegate.cpp | 8 ++++++-- src/control/home-page-item.cpp | 7 ++++++- src/control/home-page-item.h | 1 + src/control/search-list-view.cpp | 4 ++++ src/custom-style.cpp | 18 ++++++++++++++++++ src/custom-style.h | 14 ++++++++++++++ src/src.pro | 2 ++ 9 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 src/custom-style.cpp create mode 100644 src/custom-style.h diff --git a/libsearch/global-settings.cpp b/libsearch/global-settings.cpp index a70199d..34b7176 100644 --- a/libsearch/global-settings.cpp +++ b/libsearch/global-settings.cpp @@ -77,8 +77,18 @@ GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent) if (key == STYLE_NAME_KEY) { //当前主题改变时也发出paletteChanged信号,通知主界面刷新 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()); } } diff --git a/libsearch/global-settings.h b/libsearch/global-settings.h index 858185c..87b9a61 100644 --- a/libsearch/global-settings.h +++ b/libsearch/global-settings.h @@ -39,6 +39,7 @@ #define TRANSPARENCY_KEY "transparency" #define THEME_GSETTINGS_ID "org.ukui.style" #define STYLE_NAME_KEY "styleName" +#define FONT_SIZE_KEY "systemFontSize" #define INDEX_DATABASE_STATE "index_database_state" #define CONTENT_INDEX_DATABASE_STATE "content_index_database_state" #define INDEX_GENERATOR_NORMAL_EXIT "index_generator_normal_exit" diff --git a/src/control/highlight-item-delegate.cpp b/src/control/highlight-item-delegate.cpp index 9aae3d8..334a8bc 100644 --- a/src/control/highlight-item-delegate.cpp +++ b/src/control/highlight-item-delegate.cpp @@ -25,6 +25,7 @@ #include #include #include +#include "global-settings.h" HighlightItemDelegate::HighlightItemDelegate(QObject *parent) : QStyledItemDelegate (parent) { @@ -83,8 +84,11 @@ QString HighlightItemDelegate::getHtmlText(QPainter *painter, const QStyleOption { int indexFindLeft = 0; QString indexString = index.model()->data(index,Qt::DisplayRole).toString(); - QFontMetrics m_QFontMetrics = painter->fontMetrics(); - QString indexColString = m_QFontMetrics.elidedText(indexString, Qt::ElideRight, itemOption.rect.width() + 10); //当字体超过Item的长度时显示为省略号 + QFont ft(painter->font().family(), GlobalSettings::getInstance()->getValue(FONT_SIZE_KEY).toInt()); + 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; if ((indexColString.toUpper()).contains((m_regFindKeyWords.toUpper()))) { indexFindLeft = indexColString.toUpper().indexOf(m_regFindKeyWords.toUpper()); //得到查找字体在当前整个Item字体中的位置 diff --git a/src/control/home-page-item.cpp b/src/control/home-page-item.cpp index 7ee8c35..d98bbc2 100644 --- a/src/control/home-page-item.cpp +++ b/src/control/home-page-item.cpp @@ -35,7 +35,11 @@ HomePageItem::HomePageItem(QWidget *parent, const int& type, const QString& path connect(qApp, &QApplication::paletteChanged, this, [ = ]() { if (m_namelabel) { 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) { m_path = path; + m_type = type; m_widget = new QWidget(this); m_widget->setObjectName("MainWidget"); // m_widget->setStyleSheet("QWidget#MainWidget{background: rgba(0, 0, 0, 0.05); border-radius: 4px;}"); diff --git a/src/control/home-page-item.h b/src/control/home-page-item.h index 1c8fa61..58acdac 100644 --- a/src/control/home-page-item.h +++ b/src/control/home-page-item.h @@ -56,6 +56,7 @@ private: QLabel * m_namelabel = nullptr; double m_transparency = 0; QString m_path; + int m_type = 0; }; #endif // HOMEPAGEITEM_H diff --git a/src/control/search-list-view.cpp b/src/control/search-list-view.cpp index 5fcca26..a4ddca6 100644 --- a/src/control/search-list-view.cpp +++ b/src/control/search-list-view.cpp @@ -21,9 +21,13 @@ #include "search-list-view.h" #include #include +#include "custom-style.h" 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); setSelectionBehavior(QAbstractItemView::SelectRows); setSelectionMode(QAbstractItemView::SingleSelection); diff --git a/src/custom-style.cpp b/src/custom-style.cpp new file mode 100644 index 0000000..2e82aa8 --- /dev/null +++ b/src/custom-style.cpp @@ -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); +} diff --git a/src/custom-style.h b/src/custom-style.h new file mode 100644 index 0000000..c220bc5 --- /dev/null +++ b/src/custom-style.h @@ -0,0 +1,14 @@ +#ifndef CUSTOMSTYLE_H +#define CUSTOMSTYLE_H +#include +#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 diff --git a/src/src.pro b/src/src.pro index 9b1133f..31dc032 100644 --- a/src/src.pro +++ b/src/src.pro @@ -26,6 +26,7 @@ include(singleapplication/qt-single-application.pri) SOURCES += \ content-widget.cpp \ + custom-style.cpp \ input-box.cpp \ main.cpp \ mainwindow.cpp \ @@ -37,6 +38,7 @@ SOURCES += \ HEADERS += \ content-widget.h \ + custom-style.h \ input-box.h \ mainwindow.h \ search-app-thread.h \