Align the icon and the title in search list by overriding the drawControl method.

This commit is contained in:
baijunjie 2021-12-13 10:53:28 +08:00 committed by iaom
parent 67c134bc17
commit 5fb81dd1d9
5 changed files with 85 additions and 2 deletions

View File

@ -8,6 +8,7 @@
using namespace UkuiSearch;
BestListView::BestListView(QWidget *parent) : QTreeView(parent)
{
setStyle(ResultItemStyle::getStyle());
this->setFrameShape(QFrame::NoFrame);
this->viewport()->setAutoFillBackground(false);
this->setIconSize(QSize(VIEW_ICON_SIZE, VIEW_ICON_SIZE));

View File

@ -1,5 +1,6 @@
#include "result-view-delegate.h"
using namespace UkuiSearch;
static ResultItemStyle *global_instance_of_item_style = nullptr;
ResultViewDelegate::ResultViewDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
@ -38,7 +39,8 @@ void ResultViewDelegate::paint(QPainter * painter, const QStyleOptionViewItem &
ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText));
QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4);
textRect.adjust(0, 0, 0, 0);
//使图标和文本间隔与原来保持一致故文本区域右移4
textRect.adjust(4, 0, 0, 0);
painter->save();
painter->translate(textRect.topLeft());
painter->setClipRect(textRect.translated(-textRect.topLeft()));
@ -87,3 +89,71 @@ QString ResultViewDelegate::escapeHtml(const QString &str) const
temp.replace(">", ">");
return temp;
}
ResultItemStyle *ResultItemStyle::getStyle()
{
if (!global_instance_of_item_style) {
global_instance_of_item_style = new ResultItemStyle;
}
return global_instance_of_item_style;
}
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<const QStyleOptionViewItem *>(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;
}
}

View File

@ -26,6 +26,7 @@
#include <QStyle>
#include <QTextDocument>
#include <QAbstractTextDocumentLayout>
#include <QProxyStyle>
#include "global-settings.h"
namespace UkuiSearch {
@ -43,6 +44,16 @@ private:
QString getHtmlText(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const;
QString escapeHtml(const QString&) const;
};
class ResultItemStyle : public QProxyStyle
{
public:
static ResultItemStyle *getStyle();
void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override;
private:
explicit ResultItemStyle() {}
~ResultItemStyle() = default;
};
}
#endif // RESULTVIEWDELEGATE_H

View File

@ -160,6 +160,7 @@ void ResultWidget::initConnections()
ResultView::ResultView(const QString &plugin_id, QWidget *parent) : QTreeView(parent)
{
setStyle(ResultItemStyle::getStyle());
this->setFrameShape(QFrame::NoFrame);
this->viewport()->setAutoFillBackground(false);
this->setIconSize(QSize(VIEW_ICON_SIZE, VIEW_ICON_SIZE));

View File

@ -4,7 +4,7 @@
#define NOMORL_LINE_STYLE "QFrame{background: rgba(0,0,0,0.1);}"
#define DARK_LINE_STYLE "QFrame{background: rgba(255, 255, 255, 0.16);}"
using namespace Zeeker;
using namespace UkuiSearch;
SeparationLine::SeparationLine(QWidget *parent) : QFrame(parent)
{
this->setLineWidth(0);