适配主题的viewItem圆角效果。

This commit is contained in:
iaom 2022-06-15 15:54:50 +08:00
parent 3848939928
commit d947c274e8
7 changed files with 46 additions and 48 deletions

View File

@ -8,7 +8,7 @@
using namespace UkuiSearch; using namespace UkuiSearch;
BestListView::BestListView(QWidget *parent) : QTreeView(parent) BestListView::BestListView(QWidget *parent) : QTreeView(parent)
{ {
setStyle(ResultItemStyle::getStyle()); // setStyle(ResultItemStyle::getStyle());
this->setFrameShape(QFrame::NoFrame); this->setFrameShape(QFrame::NoFrame);
this->viewport()->setAutoFillBackground(false); this->viewport()->setAutoFillBackground(false);
this->setIconSize(QSize(VIEW_ICON_SIZE, VIEW_ICON_SIZE)); this->setIconSize(QSize(VIEW_ICON_SIZE, VIEW_ICON_SIZE));
@ -20,8 +20,8 @@ BestListView::BestListView(QWidget *parent) : QTreeView(parent)
m_model = new BestListModel(this); m_model = new BestListModel(this);
this->setModel(m_model); this->setModel(m_model);
initConnections(); initConnections();
m_style_delegate = new ResultViewDelegate(this); m_styleDelegate = new ResultViewDelegate(this);
this->setItemDelegate(m_style_delegate); this->setItemDelegate(m_styleDelegate);
} }
bool BestListView::isSelected() bool BestListView::isSelected()
@ -184,7 +184,7 @@ void BestListView::initConnections()
{ {
connect(this, &BestListView::startSearch, [ = ](const QString &keyword) { connect(this, &BestListView::startSearch, [ = ](const QString &keyword) {
qDebug() << "==========start search!"; qDebug() << "==========start search!";
m_style_delegate->setSearchKeyword(keyword); m_styleDelegate->setSearchKeyword(keyword);
m_model->startSearch(keyword); m_model->startSearch(keyword);
}); });
connect(this, &BestListView::startSearch, m_model, &BestListModel::startSearch); connect(this, &BestListView::startSearch, m_model, &BestListModel::startSearch);

View File

@ -46,7 +46,7 @@ private:
BestListModel * m_model = nullptr; BestListModel * m_model = nullptr;
bool m_is_selected = false; bool m_is_selected = false;
ResultViewDelegate * m_style_delegate = nullptr; ResultViewDelegate * m_styleDelegate = nullptr;
int m_count = 0; int m_count = 0;
QModelIndex m_tmpCurrentIndex; QModelIndex m_tmpCurrentIndex;
QModelIndex m_tmpMousePressIndex; QModelIndex m_tmpMousePressIndex;

View File

@ -23,47 +23,45 @@ QSize ResultViewDelegate::sizeHint(const QStyleOptionViewItem &option, const QMo
void ResultViewDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const { void ResultViewDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const {
QStyleOptionViewItem opt = option; QStyleOptionViewItem opt = option;
initStyleOption(&opt, index); initStyleOption(&opt, index);
QStyle *style = opt.widget->style();
QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); QString text = opt.text;
if(text.isEmpty()) {
return;
}
opt.text = QString();
style->proxy()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); //绘制非文本区域内容
QString text = opt.text; opt.text = text;
if(text.isEmpty()) { QTextDocument doc;
return; doc.setHtml(getHtmlText(painter, opt, index)); //提取富文本
} QAbstractTextDocumentLayout* layout = doc.documentLayout();
opt.text = QString(); const double height = layout->documentSize().height();
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter); //绘制非文本区域内容
// style->drawPrimitive(QStyle::PE_PanelItemViewItem, qstyleoption_cast<QStyleOption *>(&opt), painter, opt.widget);
opt.text = text;
QTextDocument doc;
doc.setHtml(getHtmlText(painter, opt, index)); //提取富文本
QAbstractTextDocumentLayout* layout = doc.documentLayout();
const double height = layout->documentSize().height();
QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &opt, opt.widget); QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &opt, opt.widget);
//使图标和文本间隔与原来保持一致故文本区域右移4 //使图标和文本间隔与原来保持一致故文本区域右移4
textRect.adjust(4, 0, 0, 0); // textRect.adjust(4, 0, 0, 0);
double y = textRect.y(); double y = textRect.y();
y += (textRect.height() - height) / 2; y += (textRect.height() - height) / 2;
QAbstractTextDocumentLayout::PaintContext context; QAbstractTextDocumentLayout::PaintContext context;
QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled
? QPalette::Normal : QPalette::Disabled; ? QPalette::Normal : QPalette::Disabled;
if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active)) if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active))
cg = QPalette::Inactive; cg = QPalette::Inactive;
if(opt.state & QStyle::State_Selected) { if(opt.state & QStyle::State_Selected) {
painter->setPen(opt.palette.color(cg, QPalette::HighlightedText)); painter->setPen(opt.palette.color(cg, QPalette::HighlightedText));
} else { } else {
painter->setPen(opt.palette.color(cg, QPalette::Text)); painter->setPen(opt.palette.color(cg, QPalette::Text));
} }
painter->save(); painter->save();
painter->translate(QPointF(textRect.x(), y)); painter->translate(QPointF(textRect.x(), y));
layout->draw(painter, context); //绘制文本区域内容 layout->draw(painter, context); //绘制文本区域内容
painter->restore(); painter->restore();
} }

View File

@ -166,7 +166,7 @@ void ResultWidget::initConnections()
ResultView::ResultView(const QString &plugin_id, QWidget *parent) : QTreeView(parent) ResultView::ResultView(const QString &plugin_id, QWidget *parent) : QTreeView(parent)
{ {
setStyle(ResultItemStyle::getStyle()); // setStyle(ResultItemStyle::getStyle());
this->setFrameShape(QFrame::NoFrame); this->setFrameShape(QFrame::NoFrame);
this->viewport()->setAutoFillBackground(false); this->viewport()->setAutoFillBackground(false);
this->setIconSize(QSize(VIEW_ICON_SIZE, VIEW_ICON_SIZE)); this->setIconSize(QSize(VIEW_ICON_SIZE, VIEW_ICON_SIZE));
@ -179,8 +179,8 @@ ResultView::ResultView(const QString &plugin_id, QWidget *parent) : QTreeView(pa
this->setModel(m_model); this->setModel(m_model);
initConnections(); initConnections();
m_plugin_id = plugin_id; m_plugin_id = plugin_id;
m_style_delegate = new ResultViewDelegate(this); m_styleDelegate = new ResultViewDelegate(this);
this->setItemDelegate(m_style_delegate); this->setItemDelegate(m_styleDelegate);
} }
bool ResultView::isSelected() bool ResultView::isSelected()
@ -339,7 +339,7 @@ void ResultView::initConnections()
connect(this, &ResultView::startSearch, [ = ](const QString &keyword) { connect(this, &ResultView::startSearch, [ = ](const QString &keyword) {
setExpanded(false); setExpanded(false);
Q_EMIT this->lableReset(); Q_EMIT this->lableReset();
m_style_delegate->setSearchKeyword(keyword); m_styleDelegate->setSearchKeyword(keyword);
m_model->startSearch(keyword); m_model->startSearch(keyword);
}); });
connect(this, &ResultView::stopSearch, m_model, &SearchResultModel::stopSearch); connect(this, &ResultView::stopSearch, m_model, &SearchResultModel::stopSearch);

View File

@ -43,7 +43,7 @@ private:
SearchResultModel * m_model = nullptr; SearchResultModel * m_model = nullptr;
QString m_plugin_id; QString m_plugin_id;
bool m_is_selected = false; bool m_is_selected = false;
ResultViewDelegate * m_style_delegate = nullptr; ResultViewDelegate * m_styleDelegate = nullptr;
int m_count = 0; int m_count = 0;
QModelIndex m_tmpCurrentIndex; QModelIndex m_tmpCurrentIndex;
QModelIndex m_tmpMousePressIndex; QModelIndex m_tmpMousePressIndex;

View File

@ -37,8 +37,8 @@ WebSearchView::WebSearchView(QWidget *parent) : QTreeView(parent)
this->setHeaderHidden(true); this->setHeaderHidden(true);
m_model = new WebSearchModel(this); m_model = new WebSearchModel(this);
this->setModel(m_model); this->setModel(m_model);
m_style_delegate = new ResultViewDelegate(this); m_styleDelegate = new ResultViewDelegate(this);
this->setItemDelegate(m_style_delegate); this->setItemDelegate(m_styleDelegate);
} }
bool WebSearchView::isSelected() bool WebSearchView::isSelected()
@ -68,7 +68,7 @@ void WebSearchView::clearSelectedRow()
void WebSearchView::startSearch(const QString & keyword) void WebSearchView::startSearch(const QString & keyword)
{ {
this->m_style_delegate->setSearchKeyword(keyword); this->m_styleDelegate->setSearchKeyword(keyword);
this->m_model->startSearch(keyword); this->m_model->startSearch(keyword);
m_keyWord = keyword; m_keyWord = keyword;
} }

View File

@ -32,7 +32,7 @@ private:
WebSearchModel * m_model = nullptr; WebSearchModel * m_model = nullptr;
bool m_is_selected = false; bool m_is_selected = false;
ResultViewDelegate * m_style_delegate = nullptr; ResultViewDelegate * m_styleDelegate = nullptr;
QString m_keyWord; QString m_keyWord;
}; };