diff --git a/src/plugins/core/gui/dockheader.cpp b/src/plugins/core/gui/dockheader.cpp index ef018070d..7aa7b0452 100644 --- a/src/plugins/core/gui/dockheader.cpp +++ b/src/plugins/core/gui/dockheader.cpp @@ -40,8 +40,8 @@ DockHeader::DockHeader(QWidget *parent) d->select->hide(); d->mainLayout = new QHBoxLayout(this); - d->mainLayout->setContentsMargins(0, 0, 0, 5); - d->mainLayout->setSpacing(0); + d->mainLayout->setContentsMargins(10, 7, 8, 7); + d->mainLayout->setSpacing(2); d->mainLayout->setAlignment(Qt::AlignRight); auto nameLayout = new QHBoxLayout; diff --git a/src/plugins/core/uicontroller/mainwindow.cpp b/src/plugins/core/uicontroller/mainwindow.cpp index 3666ff8e7..d42e6c717 100644 --- a/src/plugins/core/uicontroller/mainwindow.cpp +++ b/src/plugins/core/uicontroller/mainwindow.cpp @@ -95,7 +95,6 @@ DDockWidget *MainWindow::createDockWidget(DWidget *widget) delete dock->titleBarWidget(); auto header = new DockHeader(this); - header->setContentsMargins(10, 8, 8, 8); dock->setTitleBarWidget(header); dock->setWidget(widget); diff --git a/src/plugins/find/gui/advancedsearchwidget.cpp b/src/plugins/find/gui/advancedsearchwidget.cpp index 078d891ae..41cdfe5a2 100644 --- a/src/plugins/find/gui/advancedsearchwidget.cpp +++ b/src/plugins/find/gui/advancedsearchwidget.cpp @@ -204,6 +204,7 @@ QWidget *AdvancedSearchWidgetPrivate::createSearchParamWidget() QGridLayout *gridLayout = new QGridLayout; gridLayout->setColumnStretch(0, 1); gridLayout->setContentsMargins(0, 0, 0, 0); + gridLayout->setSpacing(10); gridLayout->addWidget(searchEdit, 0, 0); gridLayout->addWidget(caseBtn, 0, 1); gridLayout->addWidget(wholeWordBtn, 0, 2); diff --git a/src/plugins/find/gui/searchresultitemdelegate.cpp b/src/plugins/find/gui/searchresultitemdelegate.cpp index 4c0c3ab9b..6411394a0 100644 --- a/src/plugins/find/gui/searchresultitemdelegate.cpp +++ b/src/plugins/find/gui/searchresultitemdelegate.cpp @@ -42,9 +42,9 @@ void SearchResultItemDelegate::paint(QPainter *painter, const QStyleOptionViewIt QStyleOptionViewItem opt = option; DStyledItemDelegate::initStyleOption(&opt, index); + opt.rect.adjust(0, 0, -10, 0); painter->setRenderHint(QPainter::Antialiasing); - drawBackground(painter, opt); if (!index.parent().isValid()) { const auto &iconRect = drawFileIcon(painter, opt, index); @@ -86,47 +86,57 @@ bool SearchResultItemDelegate::helpEvent(QHelpEvent *event, QAbstractItemView *v return DStyledItemDelegate::helpEvent(event, view, option, index); } -bool SearchResultItemDelegate::eventFilter(QObject *object, QEvent *event) +bool SearchResultItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { - if (event->type() == QEvent::MouseButtonPress && object == view()->viewport()) { - auto mouseEvent = dynamic_cast(event); - auto index = view()->indexAt(mouseEvent->pos()); - if (!index.isValid()) - return DStyledItemDelegate::eventFilter(object, event); - - auto itemRect = view()->visualRect(index); - itemRect.adjust(0, 0, -10, 0); - - auto arrowRect = this->arrowRect(iconRect(itemRect)); + if (!index.isValid()) + return DStyledItemDelegate::editorEvent(event, model, option, index); + + auto mouseEvent = dynamic_cast(event); + auto itemRect = option.rect; + itemRect.adjust(0, 0, -10, 0); + auto arrowRect = this->arrowRect(iconRect(itemRect)); + auto replaceRect = replaceButtonRect(itemRect); + auto closeRect = closeButtonRect(itemRect); + + switch (event->type()) { + case QEvent::MouseMove: + case QEvent::MouseButtonPress: + view()->update(index); + break; + case QEvent::MouseButtonRelease: { if (arrowRect.contains(mouseEvent->pos())) { if (view()->isExpanded(index)) { view()->collapse(index); } else { view()->expand(index); } + event->accept(); return true; } - auto replaceRect = replaceButtonRect(itemRect); if (replaceRect.contains(mouseEvent->pos())) { - auto model = qobject_cast(view()->model()); - if (model) { - Q_EMIT model->requestReplace(index); + auto viewModel = qobject_cast(model); + if (viewModel) { + Q_EMIT viewModel->requestReplace(index); + event->accept(); return true; } } - auto closeRect = closeButtonRect(itemRect); if (closeRect.contains(mouseEvent->pos())) { - auto model = qobject_cast(view()->model()); - if (model) { - model->remove(index); + auto viewModel = qobject_cast(model); + if (viewModel) { + viewModel->remove(index); + event->accept(); return true; } } + } break; + default: + break; } - return DStyledItemDelegate::eventFilter(object, event); + return DStyledItemDelegate::editorEvent(event, model, option, index); } QTreeView *SearchResultItemDelegate::view() const @@ -234,8 +244,8 @@ QRect SearchResultItemDelegate::drawOptionButton(QPainter *painter, const QStyle // draw close button QRect iconRect = closeButtonRect(opt.rect); - QIcon closeIcon = DStyle::standardIcon(opt.widget->style(), DStyle::SP_CloseButton); - drawIcon(painter, opt, closeIcon, iconRect); + drawOptionBackground(painter, option, iconRect); + drawIcon(painter, opt, QIcon::fromTheme("common_close"), iconRect); // draw replace button QIcon replaceIcon; @@ -245,11 +255,39 @@ QRect SearchResultItemDelegate::drawOptionButton(QPainter *painter, const QStyle replaceIcon = QIcon::fromTheme("replace"); iconRect = replaceButtonRect(opt.rect); + drawOptionBackground(painter, option, iconRect); drawIcon(painter, opt, replaceIcon, iconRect); - return iconRect; } +void SearchResultItemDelegate::drawOptionBackground(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect) const +{ + QPoint mousePos = view()->mapFromGlobal(QCursor::pos()); + if (!rect.contains(mousePos)) + return; + + if (QApplication::mouseButtons() & Qt::LeftButton) { + QColor bgColor(255, 255, 255, qRound(255 * 0.15)); + painter->save(); + painter->setPen(Qt::NoPen); + painter->setBrush(bgColor); + painter->drawRoundedRect(rect, 6, 6); + painter->restore(); + } else if (option.state.testFlag(QStyle::State_MouseOver)) { + QColor bgColor(0, 0, 0, qRound(255 * 0.08)); + if (option.state.testFlag(QStyle::State_Selected)) { + bgColor.setRgba(qRgba(255, 255, 255, qRound(255 * 0.2))); + } else if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType) { + bgColor.setRgba(qRgba(255, 255, 255, qRound(255 * 0.08))); + } + painter->save(); + painter->setPen(Qt::NoPen); + painter->setBrush(bgColor); + painter->drawRoundedRect(rect, 6, 6); + painter->restore(); + } +} + void SearchResultItemDelegate::drawNameItem(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index, const QRect &iconRect) const { @@ -341,7 +379,9 @@ void SearchResultItemDelegate::drawContextItem(QPainter *painter, const QStyleOp replaceBackground.setNamedColor("#57965C"); replaceBackground.setAlpha(180); } - formats << createFormatRange(opt, column, matchedLength, {}, matchedBackground); + auto matchedRange = createFormatRange(opt, column, matchedLength, {}, matchedBackground); + matchedRange.format.setFontStrikeOut(true); + formats << matchedRange; formats << createFormatRange(opt, replaceTextOffset, replaceText.length(), {}, replaceBackground); } else { QColor background; @@ -358,7 +398,7 @@ void SearchResultItemDelegate::drawContextItem(QPainter *painter, const QStyleOp } void SearchResultItemDelegate::drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, - const QString &text, const QList &format) const + const QString &text, const QList &formatList) const { if (option.state & QStyle::State_Selected) { painter->setPen(option.palette.color(QPalette::Normal, QPalette::HighlightedText)); @@ -366,7 +406,8 @@ void SearchResultItemDelegate::drawDisplay(QPainter *painter, const QStyleOption painter->setPen(option.palette.color(QPalette::Normal, QPalette::Text)); } - if (text.isEmpty()) + QString displayText = text.trimmed(); + if (displayText.isEmpty()) return; const QStyleOptionViewItem opt = option; @@ -375,46 +416,24 @@ void SearchResultItemDelegate::drawDisplay(QPainter *painter, const QStyleOption QStyle *style = widget ? widget->style() : QApplication::style(); const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, widget) + 1; QRect textRect = rect.adjusted(textMargin, 0, -textMargin, 0); // remove width padding - const bool wrapText = opt.features & QStyleOptionViewItem::WrapText; QTextOption textOption; - textOption.setWrapMode(wrapText ? QTextOption::WordWrap : QTextOption::ManualWrap); + textOption.setWrapMode(QTextOption::NoWrap); textOption.setTextDirection(option.direction); textOption.setAlignment(QStyle::visualAlignment(option.direction, option.displayAlignment)); QTextLayout textLayout; textLayout.setTextOption(textOption); textLayout.setFont(option.font); - textLayout.setText(text); + textLayout.setText(displayText); + textLayout.setFormats(formatList.toVector()); QSizeF textLayoutSize = doTextLayout(&textLayout, textRect.width()); - - if (textRect.width() < textLayoutSize.width() - || textRect.height() < textLayoutSize.height()) { - QString elided; - int start = 0; - int end = text.indexOf(QChar::LineSeparator, start); - if (end == -1) { - elided += option.fontMetrics.elidedText(text, option.textElideMode, textRect.width()); - } else { - while (end != -1) { - elided += option.fontMetrics.elidedText(text.mid(start, end - start), - option.textElideMode, textRect.width()); - elided += QChar::LineSeparator; - start = end + 1; - end = text.indexOf(QChar::LineSeparator, start); - } - // let's add the last line (after the last QChar::LineSeparator) - elided += option.fontMetrics.elidedText(text.mid(start), - option.textElideMode, textRect.width()); - } - textLayout.setText(elided); - textLayoutSize = doTextLayout(&textLayout, textRect.width()); + if (textRect.width() < textLayoutSize.width()) { + displayText = option.fontMetrics.elidedText(displayText, Qt::ElideRight, textRect.width()); + textLayout.setText(displayText); + doTextLayout(&textLayout, textRect.width()); } - const QSize layoutSize(textRect.width(), int(textLayoutSize.height())); - const QRect layoutRect = QStyle::alignedRect(option.direction, option.displayAlignment, - layoutSize, textRect); - - textLayout.draw(painter, layoutRect.topLeft(), format.toVector(), layoutRect); + textLayout.draw(painter, textRect.topLeft()); } QSizeF SearchResultItemDelegate::doTextLayout(QTextLayout *textLayout, int width) const @@ -462,8 +481,8 @@ QRect SearchResultItemDelegate::replaceButtonRect(const QRect &itemRect) const { QRect replaceButtonRect = itemRect; - replaceButtonRect.setSize(view()->iconSize()); - replaceButtonRect.moveLeft(itemRect.right() - 2 * view()->iconSize().width() - 2 * ItemMargin); + replaceButtonRect.setSize({ 20, 20 }); + replaceButtonRect.moveLeft(itemRect.right() - 2 * 20 - 5); replaceButtonRect.moveTop(replaceButtonRect.top() + ((itemRect.bottom() - replaceButtonRect.bottom()) / 2)); return replaceButtonRect; @@ -473,8 +492,8 @@ QRect SearchResultItemDelegate::closeButtonRect(const QRect &itemRect) const { QRect closeButtonRect = itemRect; - closeButtonRect.setSize(view()->iconSize()); - closeButtonRect.moveLeft(itemRect.right() - view()->iconSize().width() - ItemMargin); + closeButtonRect.setSize({ 20, 20 }); + closeButtonRect.moveLeft(itemRect.right() - 20 - 3); closeButtonRect.moveTop(closeButtonRect.top() + ((itemRect.bottom() - closeButtonRect.bottom()) / 2)); return closeButtonRect; @@ -501,7 +520,7 @@ void SearchResultItemDelegate::drawIcon(QPainter *painter, const QStyleOptionVie if (option.state.testFlag(QStyle::State_Selected)) iconMode = QIcon::Selected; - auto px = icon.pixmap({ 18, 18 }, iconMode); + auto px = icon.pixmap(view()->iconSize(), iconMode); px.setDevicePixelRatio(qApp->devicePixelRatio()); qreal x = rect.x(); diff --git a/src/plugins/find/gui/searchresultitemdelegate.h b/src/plugins/find/gui/searchresultitemdelegate.h index cd1e419d1..43264ab46 100644 --- a/src/plugins/find/gui/searchresultitemdelegate.h +++ b/src/plugins/find/gui/searchresultitemdelegate.h @@ -20,7 +20,7 @@ class SearchResultItemDelegate : public DTK_WIDGET_NAMESPACE::DStyledItemDelegat protected: bool helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index) override; - bool eventFilter(QObject *object, QEvent *event) override; + bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override; private: QTreeView *view() const; @@ -30,12 +30,13 @@ class SearchResultItemDelegate : public DTK_WIDGET_NAMESPACE::DStyledItemDelegat const QStyleOptionViewItem &option, const QModelIndex &index) const; QRect drawResultCount(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; QRect drawOptionButton(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; + void drawOptionBackground(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect) const; void drawNameItem(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index, const QRect &iconRect) const; void drawContextItem(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, - const QString &text, const QList &format) const; + const QString &text, const QList &formatList) const; QSizeF doTextLayout(QTextLayout *textLayout, int width) const; QRect iconRect(const QRect &itemRect) const; QRect arrowRect(const QRect &iconRect) const; diff --git a/src/plugins/find/gui/searchresultwidget.cpp b/src/plugins/find/gui/searchresultwidget.cpp index 2f54533ac..b2f8ddf79 100644 --- a/src/plugins/find/gui/searchresultwidget.cpp +++ b/src/plugins/find/gui/searchresultwidget.cpp @@ -61,6 +61,7 @@ void SearchResultWidgetPrivate::initUI() resultView->setFrameShape(QFrame::NoFrame); resultView->setIconSize({ 16, 16 }); resultView->setIndentation(0); + resultView->setMouseTracking(true); mainLayout->addWidget(msgLabel); mainLayout->addWidget(resultView);