From 34fa8c7498c926441d6bdf33cacce974b3f9b98c Mon Sep 17 00:00:00 2001 From: iaom Date: Fri, 4 Nov 2022 17:35:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=B9=B3=E6=9D=BF=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E4=B8=8B=EF=BC=8C=E5=8D=95=E6=9C=BA=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E9=A1=B9=E7=9B=B4=E6=8E=A5=E6=89=A7=E8=A1=8C=E6=89=93=E5=BC=80?= =?UTF-8?q?=E5=8A=A8=E4=BD=9C=EF=BC=8C=E6=97=A0=E6=B3=95=E6=89=93=E5=BC=80?= =?UTF-8?q?=E8=AF=A6=E6=83=85=E9=A1=B5=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/view/best-list-view.cpp | 2 +- frontend/view/result-view-delegate.cpp | 291 +++++++++++++------------ frontend/view/result-view-delegate.h | 5 +- frontend/view/result-view.cpp | 2 +- frontend/view/web-search-view.cpp | 1 + 5 files changed, 156 insertions(+), 145 deletions(-) diff --git a/frontend/view/best-list-view.cpp b/frontend/view/best-list-view.cpp index da9bad0..ea89998 100644 --- a/frontend/view/best-list-view.cpp +++ b/frontend/view/best-list-view.cpp @@ -8,7 +8,7 @@ using namespace UkuiSearch; BestListView::BestListView(QWidget *parent) : QTreeView(parent) { -// setStyle(ResultItemStyle::getStyle()); + setStyle(ResultItemStyle::getStyle()); this->setFrameShape(QFrame::NoFrame); this->viewport()->setAutoFillBackground(false); this->setIconSize(QSize(VIEW_ICON_SIZE, VIEW_ICON_SIZE)); diff --git a/frontend/view/result-view-delegate.cpp b/frontend/view/result-view-delegate.cpp index 5cac2c2..2e151a7 100644 --- a/frontend/view/result-view-delegate.cpp +++ b/frontend/view/result-view-delegate.cpp @@ -45,7 +45,6 @@ void ResultViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op m_hightLightEffectHelper->setTextColor(QBrush(opt.palette.text().color())); } - m_textDoc->setPlainText(text); m_hightLightEffectHelper->setDocument(m_textDoc); m_hightLightEffectHelper->rehighlight(); @@ -63,151 +62,161 @@ ResultItemStyle *ResultItemStyle::getStyle() return global_instance_of_item_style; } -void ResultItemStyle::drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const +int ResultItemStyle::styleHint(QStyle::StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const { - switch (element) { - case PE_PanelItemViewItem: { - - bool isIconView = false; - auto opt = qstyleoption_cast(option); - if (!opt) - return; - if (opt) { - isIconView = (opt->decorationPosition & QStyleOptionViewItem::Top); - } - - bool isHover = (option->state & State_MouseOver) && (option->state & ~State_Selected); - bool isSelected = option->state & State_Selected; - bool enable = option->state & State_Enabled; - QColor color = option->palette.color(enable? QPalette::Active: QPalette::Disabled, QPalette::Highlight); - - if (isSelected) { - color.setAlpha(255); - } else if (isHover) { - // color = opt->palette.color(QPalette::Active, QPalette::BrightText); - // color.setAlpha(0.05); - color = QColor(241, 241, 241); - } else { - color.setAlpha(0); - } - QPainterPath path; - - if(opt->viewItemPosition == QStyleOptionViewItem::OnlyOne) { - path.addRoundedRect(option->rect, 6, 6); - } else if(opt->viewItemPosition == QStyleOptionViewItem::Beginning) { - //一个左侧有两个圆角的矩形 - path.moveTo(option->rect.topLeft() + QPoint(6, 0)); - path.cubicTo(option->rect.topLeft() + QPoint(6, 0), - option->rect.topLeft(), option->rect.topLeft() + - QPoint(0, 6)); - - path.lineTo(option->rect.bottomLeft() - QPoint(0, 6)); - path.cubicTo(option->rect.bottomLeft() - QPoint(0, 6), - option->rect.bottomLeft() + QPoint(0, 1), - option->rect.bottomLeft() + QPoint(6, 1)); - - path.lineTo(option->rect.bottomRight() + QPoint(1, 1)); - path.lineTo(option->rect.topRight()+ QPoint(1, 0)); - path.lineTo(option->rect.topLeft() + QPoint(6, 0)); - } else if(opt->viewItemPosition == QStyleOptionViewItem::Middle) { - path.addRect(option->rect.adjusted(-1, 0, 1, 0)); - } else if(opt->viewItemPosition == QStyleOptionViewItem::End) { - //一个右侧有两个圆角的矩形 - path.moveTo(option->rect.topRight() + QPoint(-6, 0)); - path.cubicTo(option->rect.topRight() + QPoint(-6, 0), - option->rect.topRight(), - option->rect.topRight() + - QPoint(0, 6)); - - path.lineTo(option->rect.bottomRight() - QPoint(0, 6)); - path.cubicTo(option->rect.bottomRight() - QPoint(0, 6), - option->rect.bottomRight() + QPoint(0, 1), - option->rect.bottomRight() + QPoint(-6, 1)); - - path.lineTo(option->rect.bottomLeft() + QPoint(0, 1)); - path.lineTo(option->rect.topLeft()); - path.lineTo(option->rect.topRight() + QPoint(-6, 0)); - } else { - // path.addRoundedRect(option->rect, 8, 8); - } - - painter->save(); - painter->setRenderHint(QPainter::Antialiasing); - painter->setPen(Qt::NoPen); - painter->setBrush(color); - painter->drawPath(path); - // painter->fillPath(path, painter->brush()); - - painter->restore(); - break; - } - default: - break; -// return QProxyStyle::drawPrimitive(element, option, painter, widget); - } - -} - -void ResultItemStyle::drawControl(QStyle::ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const -{ - switch (element) { - case CE_ItemViewItem: { - if (const QStyleOptionViewItem *vopt = qstyleoption_cast(option)) { - painter->save(); - if (painter->clipPath().isEmpty()) { - painter->setClipRect(option->rect); - } - - QRect checkRect = proxy()->subElementRect(SE_ItemViewItemCheckIndicator, vopt, widget); - QRect iconRect = proxy()->subElementRect(SE_ItemViewItemDecoration, vopt, widget); - - // draw the background - proxy()->drawPrimitive(PE_PanelItemViewItem, option, painter, widget); - - // draw the check mark - if (vopt->features & QStyleOptionViewItem::HasCheckIndicator) { - QStyleOptionViewItem option(*vopt); - option.rect = checkRect; - option.state = option.state & ~QStyle::State_HasFocus; - - switch (vopt->checkState) { - case Qt::Unchecked: - option.state |= QStyle::State_Off; - break; - case Qt::PartiallyChecked: - option.state |= QStyle::State_NoChange; - break; - case Qt::Checked: - option.state |= QStyle::State_On; - break; - default: - break; - } - proxy()->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &option, painter, widget); - } - - // draw the icon - QIcon::Mode mode = QIcon::Normal; - if (!(vopt->state & QStyle::State_Enabled)) { - mode = QIcon::Disabled; - } else if (vopt->state & QStyle::State_Selected) { - mode = QIcon::Selected; - } - QIcon::State state = vopt->state & QStyle::State_Open ? QIcon::On : QIcon::Off; - auto pixmap = vopt->icon.pixmap(vopt->decorationSize, mode, state); - - iconRect.moveLeft(8); - QStyle::drawItemPixmap(painter, iconRect, vopt->decorationAlignment, pixmap); - painter->restore(); - return; - } - break; - } + switch (hint) { + case SH_ItemView_ActivateItemOnSingleClick: + return false; default: - break; + return qApp->style()->styleHint(hint, option, widget, returnData); } } +//void ResultItemStyle::drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const +//{ +// switch (element) { +// case PE_PanelItemViewItem: { + +// bool isIconView = false; +// auto opt = qstyleoption_cast(option); +// if (!opt) +// return; +// if (opt) { +// isIconView = (opt->decorationPosition & QStyleOptionViewItem::Top); +// } + +// bool isHover = (option->state & State_MouseOver) && (option->state & ~State_Selected); +// bool isSelected = option->state & State_Selected; +// bool enable = option->state & State_Enabled; +// QColor color = option->palette.color(enable? QPalette::Active: QPalette::Disabled, QPalette::Highlight); + +// if (isSelected) { +// color.setAlpha(255); +// } else if (isHover) { +// // color = opt->palette.color(QPalette::Active, QPalette::BrightText); +// // color.setAlpha(0.05); +// color = QColor(241, 241, 241); +// } else { +// color.setAlpha(0); +// } +// QPainterPath path; + +// if(opt->viewItemPosition == QStyleOptionViewItem::OnlyOne) { +// path.addRoundedRect(option->rect, 6, 6); +// } else if(opt->viewItemPosition == QStyleOptionViewItem::Beginning) { +// //一个左侧有两个圆角的矩形 +// path.moveTo(option->rect.topLeft() + QPoint(6, 0)); +// path.cubicTo(option->rect.topLeft() + QPoint(6, 0), +// option->rect.topLeft(), option->rect.topLeft() + +// QPoint(0, 6)); + +// path.lineTo(option->rect.bottomLeft() - QPoint(0, 6)); +// path.cubicTo(option->rect.bottomLeft() - QPoint(0, 6), +// option->rect.bottomLeft() + QPoint(0, 1), +// option->rect.bottomLeft() + QPoint(6, 1)); + +// path.lineTo(option->rect.bottomRight() + QPoint(1, 1)); +// path.lineTo(option->rect.topRight()+ QPoint(1, 0)); +// path.lineTo(option->rect.topLeft() + QPoint(6, 0)); +// } else if(opt->viewItemPosition == QStyleOptionViewItem::Middle) { +// path.addRect(option->rect.adjusted(-1, 0, 1, 0)); +// } else if(opt->viewItemPosition == QStyleOptionViewItem::End) { +// //一个右侧有两个圆角的矩形 +// path.moveTo(option->rect.topRight() + QPoint(-6, 0)); +// path.cubicTo(option->rect.topRight() + QPoint(-6, 0), +// option->rect.topRight(), +// option->rect.topRight() + +// QPoint(0, 6)); + +// path.lineTo(option->rect.bottomRight() - QPoint(0, 6)); +// path.cubicTo(option->rect.bottomRight() - QPoint(0, 6), +// option->rect.bottomRight() + QPoint(0, 1), +// option->rect.bottomRight() + QPoint(-6, 1)); + +// path.lineTo(option->rect.bottomLeft() + QPoint(0, 1)); +// path.lineTo(option->rect.topLeft()); +// path.lineTo(option->rect.topRight() + QPoint(-6, 0)); +// } else { +// // path.addRoundedRect(option->rect, 8, 8); +// } + +// painter->save(); +// painter->setRenderHint(QPainter::Antialiasing); +// painter->setPen(Qt::NoPen); +// painter->setBrush(color); +// painter->drawPath(path); +// // painter->fillPath(path, painter->brush()); + +// painter->restore(); +// break; +// } +// default: +// break; +//// return QProxyStyle::drawPrimitive(element, option, painter, widget); +// } + +//} + +//void ResultItemStyle::drawControl(QStyle::ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const +//{ +// switch (element) { +// case CE_ItemViewItem: { +// if (const QStyleOptionViewItem *vopt = qstyleoption_cast(option)) { +// painter->save(); +// if (painter->clipPath().isEmpty()) { +// painter->setClipRect(option->rect); +// } + +// QRect checkRect = proxy()->subElementRect(SE_ItemViewItemCheckIndicator, vopt, widget); +// QRect iconRect = proxy()->subElementRect(SE_ItemViewItemDecoration, vopt, widget); + +// // draw the background +// proxy()->drawPrimitive(PE_PanelItemViewItem, option, painter, widget); + +// // draw the check mark +// if (vopt->features & QStyleOptionViewItem::HasCheckIndicator) { +// QStyleOptionViewItem option(*vopt); +// option.rect = checkRect; +// option.state = option.state & ~QStyle::State_HasFocus; + +// switch (vopt->checkState) { +// case Qt::Unchecked: +// option.state |= QStyle::State_Off; +// break; +// case Qt::PartiallyChecked: +// option.state |= QStyle::State_NoChange; +// break; +// case Qt::Checked: +// option.state |= QStyle::State_On; +// break; +// default: +// break; +// } +// proxy()->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &option, painter, widget); +// } + +// // draw the icon +// QIcon::Mode mode = QIcon::Normal; +// if (!(vopt->state & QStyle::State_Enabled)) { +// mode = QIcon::Disabled; +// } else if (vopt->state & QStyle::State_Selected) { +// mode = QIcon::Selected; +// } +// QIcon::State state = vopt->state & QStyle::State_Open ? QIcon::On : QIcon::Off; +// auto pixmap = vopt->icon.pixmap(vopt->decorationSize, mode, state); + +// iconRect.moveLeft(8); +// QStyle::drawItemPixmap(painter, iconRect, vopt->decorationAlignment, pixmap); +// painter->restore(); +// return; +// } +// break; +// } +// default: +// break; +// } +//} + HightLightEffectHelper::HightLightEffectHelper(QObject *parent) : QSyntaxHighlighter(parent) { m_expression.setCaseSensitivity(Qt::CaseInsensitive); diff --git a/frontend/view/result-view-delegate.h b/frontend/view/result-view-delegate.h index d6a177a..fb9eea4 100644 --- a/frontend/view/result-view-delegate.h +++ b/frontend/view/result-view-delegate.h @@ -69,8 +69,9 @@ class ResultItemStyle : public QProxyStyle { public: static ResultItemStyle *getStyle(); - void drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const; - void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override; + int styleHint(StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const; +// void drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const; +// void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override; private: explicit ResultItemStyle() {} ~ResultItemStyle() = default; diff --git a/frontend/view/result-view.cpp b/frontend/view/result-view.cpp index f444bce..de1c567 100644 --- a/frontend/view/result-view.cpp +++ b/frontend/view/result-view.cpp @@ -166,7 +166,7 @@ void ResultWidget::initConnections() ResultView::ResultView(const QString &plugin_id, QWidget *parent) : QTreeView(parent) { -// setStyle(ResultItemStyle::getStyle()); + setStyle(ResultItemStyle::getStyle()); this->setFrameShape(QFrame::NoFrame); this->viewport()->setAttribute(Qt::WA_AcceptTouchEvents); this->viewport()->setAutoFillBackground(false); diff --git a/frontend/view/web-search-view.cpp b/frontend/view/web-search-view.cpp index 3c05f71..1bae643 100644 --- a/frontend/view/web-search-view.cpp +++ b/frontend/view/web-search-view.cpp @@ -28,6 +28,7 @@ using namespace UkuiSearch; WebSearchView::WebSearchView(QWidget *parent) : QTreeView(parent) { + setStyle(ResultItemStyle::getStyle()); this->setFrameShape(QFrame::NoFrame); this->viewport()->setAutoFillBackground(false); this->setRootIsDecorated(false);