适配主题的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;
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));
@ -20,8 +20,8 @@ BestListView::BestListView(QWidget *parent) : QTreeView(parent)
m_model = new BestListModel(this);
this->setModel(m_model);
initConnections();
m_style_delegate = new ResultViewDelegate(this);
this->setItemDelegate(m_style_delegate);
m_styleDelegate = new ResultViewDelegate(this);
this->setItemDelegate(m_styleDelegate);
}
bool BestListView::isSelected()
@ -184,7 +184,7 @@ void BestListView::initConnections()
{
connect(this, &BestListView::startSearch, [ = ](const QString &keyword) {
qDebug() << "==========start search!";
m_style_delegate->setSearchKeyword(keyword);
m_styleDelegate->setSearchKeyword(keyword);
m_model->startSearch(keyword);
});
connect(this, &BestListView::startSearch, m_model, &BestListModel::startSearch);

View File

@ -46,7 +46,7 @@ private:
BestListModel * m_model = nullptr;
bool m_is_selected = false;
ResultViewDelegate * m_style_delegate = nullptr;
ResultViewDelegate * m_styleDelegate = nullptr;
int m_count = 0;
QModelIndex m_tmpCurrentIndex;
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 {
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;
if(text.isEmpty()) {
return;
}
opt.text = QString();
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();
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);
//使图标和文本间隔与原来保持一致故文本区域右移4
textRect.adjust(4, 0, 0, 0);
double y = textRect.y();
y += (textRect.height() - height) / 2;
QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &opt, opt.widget);
//使图标和文本间隔与原来保持一致故文本区域右移4
// textRect.adjust(4, 0, 0, 0);
double y = textRect.y();
y += (textRect.height() - height) / 2;
QAbstractTextDocumentLayout::PaintContext context;
QAbstractTextDocumentLayout::PaintContext context;
QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled
? QPalette::Normal : QPalette::Disabled;
if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active))
cg = QPalette::Inactive;
QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled
? QPalette::Normal : QPalette::Disabled;
if (cg == QPalette::Normal && !(opt.state & QStyle::State_Active))
cg = QPalette::Inactive;
if(opt.state & QStyle::State_Selected) {
painter->setPen(opt.palette.color(cg, QPalette::HighlightedText));
} else {
painter->setPen(opt.palette.color(cg, QPalette::Text));
}
painter->save();
painter->translate(QPointF(textRect.x(), y));
layout->draw(painter, context); //绘制文本区域内容
painter->restore();
if(opt.state & QStyle::State_Selected) {
painter->setPen(opt.palette.color(cg, QPalette::HighlightedText));
} else {
painter->setPen(opt.palette.color(cg, QPalette::Text));
}
painter->save();
painter->translate(QPointF(textRect.x(), y));
layout->draw(painter, context); //绘制文本区域内容
painter->restore();
}

View File

@ -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()->setAutoFillBackground(false);
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);
initConnections();
m_plugin_id = plugin_id;
m_style_delegate = new ResultViewDelegate(this);
this->setItemDelegate(m_style_delegate);
m_styleDelegate = new ResultViewDelegate(this);
this->setItemDelegate(m_styleDelegate);
}
bool ResultView::isSelected()
@ -339,7 +339,7 @@ void ResultView::initConnections()
connect(this, &ResultView::startSearch, [ = ](const QString &keyword) {
setExpanded(false);
Q_EMIT this->lableReset();
m_style_delegate->setSearchKeyword(keyword);
m_styleDelegate->setSearchKeyword(keyword);
m_model->startSearch(keyword);
});
connect(this, &ResultView::stopSearch, m_model, &SearchResultModel::stopSearch);

View File

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

View File

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

View File

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