From f06ff7b1009f1b2297cc910ad5cf5d499365bcfe Mon Sep 17 00:00:00 2001 From: David Schulz Date: Thu, 18 Oct 2012 08:02:25 +0200 Subject: [PATCH] Find: Add text editor color scheme to search result Task-number: QTCREATORBUG-8070 Change-Id: Ic220e3364aa9a5227518a210dbf0590deb06a4d6 Reviewed-by: Eike Ziller --- src/plugins/find/find.pro | 2 +- src/plugins/find/find.qbs | 1 + src/plugins/find/searchresultcolor.h | 19 +++++ .../find/searchresulttreeitemdelegate.cpp | 72 ++++++++++++++----- .../find/searchresulttreeitemdelegate.h | 3 +- src/plugins/find/searchresulttreeitemroles.h | 2 + src/plugins/find/searchresulttreemodel.cpp | 20 +++++- src/plugins/find/searchresulttreemodel.h | 4 +- src/plugins/find/searchresulttreeview.cpp | 5 +- src/plugins/find/searchresulttreeview.h | 3 +- src/plugins/find/searchresultwidget.cpp | 5 +- src/plugins/find/searchresultwidget.h | 3 +- src/plugins/find/searchresultwindow.cpp | 22 +++++- src/plugins/find/searchresultwindow.h | 6 +- src/plugins/texteditor/fontsettings.cpp | 6 ++ src/plugins/texteditor/fontsettings.h | 1 + src/plugins/texteditor/texteditorplugin.cpp | 9 ++- 17 files changed, 148 insertions(+), 35 deletions(-) create mode 100644 src/plugins/find/searchresultcolor.h diff --git a/src/plugins/find/find.pro b/src/plugins/find/find.pro index 0f71955f911..59e6f32d93a 100644 --- a/src/plugins/find/find.pro +++ b/src/plugins/find/find.pro @@ -12,6 +12,7 @@ HEADERS += findtoolwindow.h \ find_global.h \ findtoolbar.h \ findplugin.h \ + searchresultcolor.h \ searchresulttreeitemdelegate.h \ searchresulttreeitemroles.h \ searchresulttreeitems.h \ @@ -38,4 +39,3 @@ FORMS += findwidget.ui \ finddialog.ui RESOURCES += find.qrc - diff --git a/src/plugins/find/find.qbs b/src/plugins/find/find.qbs index 56e3eab12a2..7b8287e5be9 100644 --- a/src/plugins/find/find.qbs +++ b/src/plugins/find/find.qbs @@ -35,6 +35,7 @@ QtcPlugin { "ifindfilter.h", "ifindsupport.cpp", "ifindsupport.h", + "searchresultcolor.h", "searchresulttreeitemdelegate.cpp", "searchresulttreeitemdelegate.h", "searchresulttreeitemroles.h", diff --git a/src/plugins/find/searchresultcolor.h b/src/plugins/find/searchresultcolor.h new file mode 100644 index 00000000000..964efd69fd5 --- /dev/null +++ b/src/plugins/find/searchresultcolor.h @@ -0,0 +1,19 @@ +#ifndef SEARCHRESULTCOLOR_H +#define SEARCHRESULTCOLOR_H + +#include + +namespace Find { +namespace Internal { + +struct SearchResultColor{ + QColor textBackground; + QColor textForeground; + QColor highlightBackground; + QColor highlightForeground; +}; + +} // namespace Internal +} // namespace Find + +#endif // SEARCHRESULTCOLOR_H diff --git a/src/plugins/find/searchresulttreeitemdelegate.cpp b/src/plugins/find/searchresulttreeitemdelegate.cpp index f6464731c58..5110e3d24f1 100644 --- a/src/plugins/find/searchresulttreeitemdelegate.cpp +++ b/src/plugins/find/searchresulttreeitemdelegate.cpp @@ -97,19 +97,8 @@ void SearchResultTreeItemDelegate::paint(QPainter *painter, const QStyleOptionVi int lineNumberAreaWidth = drawLineNumber(painter, opt, textRect, index); textRect.adjust(lineNumberAreaWidth, 0, 0, 0); - // selected text - QString displayString = index.model()->data(index, Qt::DisplayRole).toString(); - drawMarker(painter, index, displayString, textRect); - - // show number of subresults in displayString - if (index.model()->hasChildren(index)) { - displayString += QString::fromLatin1(" (") - + QString::number(index.model()->rowCount(index)) - + QLatin1Char(')'); - } - // text and focus/selection - QItemDelegate::drawDisplay(painter, opt, textRect, displayString); + drawText(painter, opt, textRect, index); QItemDelegate::drawFocus(painter, opt, opt.rect); // check mark @@ -159,20 +148,65 @@ int SearchResultTreeItemDelegate::drawLineNumber(QPainter *painter, const QStyle return lineNumberAreaWidth; } -void SearchResultTreeItemDelegate::drawMarker(QPainter *painter, const QModelIndex &index, const QString text, - const QRect &rect) const +void SearchResultTreeItemDelegate::drawText(QPainter *painter, + const QStyleOptionViewItem &opt, + const QRect &rect, + const QModelIndex &index) const { - int searchTermStart = index.model()->data(index, ItemDataRoles::SearchTermStartRole).toInt(); + QString text = index.model()->data(index, Qt::DisplayRole).toString(); + // show number of subresults in displayString + if (index.model()->hasChildren(index)) { + text += QLatin1String(" (") + + QString::number(index.model()->rowCount(index)) + + QLatin1Char(')'); + } + + const int searchTermStart = index.model()->data(index, ItemDataRoles::SearchTermStartRole).toInt(); int searchTermLength = index.model()->data(index, ItemDataRoles::SearchTermLengthRole).toInt(); - if (searchTermStart < 0 || searchTermStart >= text.length() || searchTermLength < 1) + if (searchTermStart < 0 || searchTermStart >= text.length() || searchTermLength < 1) { + QItemDelegate::drawDisplay(painter, opt, rect, text); return; + } // clip searchTermLength to end of line searchTermLength = qMin(searchTermLength, text.length() - searchTermStart); const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; int searchTermStartPixels = painter->fontMetrics().width(text.left(searchTermStart)); int searchTermLengthPixels = painter->fontMetrics().width(text.mid(searchTermStart, searchTermLength)); + + // Text before the highlighting + QRect beforeHighlightRect(rect); + beforeHighlightRect.setRight(beforeHighlightRect.left() + searchTermStartPixels); + QStyleOptionViewItem noHighlightOpt = opt; + noHighlightOpt.rect = beforeHighlightRect; + noHighlightOpt.textElideMode = Qt::ElideNone; + QItemDelegate::drawDisplay(painter, noHighlightOpt, + beforeHighlightRect, text.mid(0, searchTermStart)); + + // Highlight background + QRect highlightBackgroundRect(rect); + highlightBackgroundRect.setLeft(highlightBackgroundRect.left() + + searchTermStartPixels + textMargin - 1); // -1: Cosmetics + highlightBackgroundRect.setRight(highlightBackgroundRect.left() + + searchTermLengthPixels + 1); // +1: Cosmetics + const QColor highlightBackground = + index.model()->data(index, ItemDataRoles::ResultHighlightBackgroundColor).value(); + painter->fillRect(highlightBackgroundRect, QBrush(highlightBackground)); + + // Highlight text QRect resultHighlightRect(rect); - resultHighlightRect.setLeft(resultHighlightRect.left() + searchTermStartPixels + textMargin - 1); // -1: Cosmetics - resultHighlightRect.setRight(resultHighlightRect.left() + searchTermLengthPixels + 1); // +1: Cosmetics - painter->fillRect(resultHighlightRect, QBrush(qRgb(255, 240, 120))); + resultHighlightRect.setLeft(beforeHighlightRect.right()); + resultHighlightRect.setRight(resultHighlightRect.left() + searchTermLengthPixels + textMargin); + QStyleOptionViewItem highlightOpt = noHighlightOpt; + const QColor highlightForeground = + index.model()->data(index, ItemDataRoles::ResultHighlightForegroundColor).value(); + highlightOpt.palette.setColor(QPalette::Text, highlightForeground); + QItemDelegate::drawDisplay(painter, highlightOpt, resultHighlightRect, + text.mid(searchTermStart, searchTermLength)); + + // Text after the Highlight + QRect afterHighlightRect(rect); + afterHighlightRect.setLeft(resultHighlightRect.right()); + noHighlightOpt.rect = afterHighlightRect; + QItemDelegate::drawDisplay(painter, noHighlightOpt, afterHighlightRect, + text.mid(searchTermStart + searchTermLength)); } diff --git a/src/plugins/find/searchresulttreeitemdelegate.h b/src/plugins/find/searchresulttreeitemdelegate.h index ef1aed121b8..e2ff90555ad 100644 --- a/src/plugins/find/searchresulttreeitemdelegate.h +++ b/src/plugins/find/searchresulttreeitemdelegate.h @@ -43,7 +43,8 @@ public: private: int drawLineNumber(QPainter *painter, const QStyleOptionViewItemV3 &option, const QRect &rect, const QModelIndex &index) const; - void drawMarker(QPainter *painter, const QModelIndex &index, const QString text, const QRect &rect) const; + void drawText(QPainter *painter, const QStyleOptionViewItem &opt, + const QRect &rect, const QModelIndex &index) const; static const int m_minimumLineNumberDigits = 6; }; diff --git a/src/plugins/find/searchresulttreeitemroles.h b/src/plugins/find/searchresulttreeitemroles.h index 892dd2c6075..f633a767758 100644 --- a/src/plugins/find/searchresulttreeitemroles.h +++ b/src/plugins/find/searchresulttreeitemroles.h @@ -42,6 +42,8 @@ enum Roles ResultLineRole, ResultLineNumberRole, ResultIconRole, + ResultHighlightBackgroundColor, + ResultHighlightForegroundColor, SearchTermStartRole, SearchTermLengthRole, IsGeneratedRole diff --git a/src/plugins/find/searchresulttreemodel.cpp b/src/plugins/find/searchresulttreemodel.cpp index c30e7a06f54..b2f75305e8f 100644 --- a/src/plugins/find/searchresulttreemodel.cpp +++ b/src/plugins/find/searchresulttreemodel.cpp @@ -30,6 +30,7 @@ #include "searchresulttreemodel.h" #include "searchresulttreeitems.h" #include "searchresulttreeitemroles.h" +#include "searchresultcolor.h" #include #include @@ -64,10 +65,11 @@ void SearchResultTreeModel::setShowReplaceUI(bool show) m_showReplaceUI = show; } -void SearchResultTreeModel::setTextEditorFont(const QFont &font) +void SearchResultTreeModel::setTextEditorFont(const QFont &font, const SearchResultColor color) { layoutAboutToBeChanged(); m_textEditorFont = font; + m_color = color; layoutChanged(); } @@ -251,6 +253,14 @@ QVariant SearchResultTreeModel::data(const SearchResultTreeItem *row, int role) else result = QVariant(); break; + case Qt::TextColorRole: + if (row->item.useTextEditorFont) + result = m_color.textForeground; + break; + case Qt::BackgroundRole: + if (row->item.useTextEditorFont) + result = m_color.textBackground; + break; case ItemDataRoles::ResultLineRole: case Qt::DisplayRole: result = row->item.text; @@ -264,6 +274,14 @@ QVariant SearchResultTreeModel::data(const SearchResultTreeItem *row, int role) case ItemDataRoles::ResultIconRole: result = row->item.icon; break; + case ItemDataRoles::ResultHighlightBackgroundColor: + if (row->item.useTextEditorFont) + result = m_color.highlightBackground; + break; + case ItemDataRoles::ResultHighlightForegroundColor: + if (row->item.useTextEditorFont) + result = m_color.highlightForeground; + break; case ItemDataRoles::SearchTermStartRole: result = row->item.textMarkPos; break; diff --git a/src/plugins/find/searchresulttreemodel.h b/src/plugins/find/searchresulttreemodel.h index 8dd368f4bf4..328b39d7548 100644 --- a/src/plugins/find/searchresulttreemodel.h +++ b/src/plugins/find/searchresulttreemodel.h @@ -31,6 +31,7 @@ #define SEARCHRESULTTREEMODEL_H #include "searchresultwindow.h" +#include "searchresultcolor.h" #include #include @@ -51,7 +52,7 @@ public: ~SearchResultTreeModel(); void setShowReplaceUI(bool show); - void setTextEditorFont(const QFont &font); + void setTextEditorFont(const QFont &font, const SearchResultColor color); Qt::ItemFlags flags(const QModelIndex &index) const; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; @@ -86,6 +87,7 @@ private: SearchResultTreeItem *m_rootItem; SearchResultTreeItem *m_currentParent; + SearchResultColor m_color; QModelIndex m_currentIndex; QStringList m_currentPath; // the path that belongs to the current parent QFont m_textEditorFont; diff --git a/src/plugins/find/searchresulttreeview.cpp b/src/plugins/find/searchresulttreeview.cpp index 494ece8f8d2..15b9c62e6ff 100644 --- a/src/plugins/find/searchresulttreeview.cpp +++ b/src/plugins/find/searchresulttreeview.cpp @@ -31,6 +31,7 @@ #include "searchresulttreeitemroles.h" #include "searchresulttreemodel.h" #include "searchresulttreeitemdelegate.h" +#include "searchresultcolor.h" #include #include @@ -57,9 +58,9 @@ void SearchResultTreeView::setAutoExpandResults(bool expand) m_autoExpandResults = expand; } -void SearchResultTreeView::setTextEditorFont(const QFont &font) +void SearchResultTreeView::setTextEditorFont(const QFont &font, const SearchResultColor color) { - m_model->setTextEditorFont(font); + m_model->setTextEditorFont(font, color); } void SearchResultTreeView::clear() diff --git a/src/plugins/find/searchresulttreeview.h b/src/plugins/find/searchresulttreeview.h index c439a2a7b35..1a123be455c 100644 --- a/src/plugins/find/searchresulttreeview.h +++ b/src/plugins/find/searchresulttreeview.h @@ -38,6 +38,7 @@ namespace Find { namespace Internal { class SearchResultTreeModel; +class SearchResultColor; class SearchResultTreeView : public QTreeView { @@ -47,7 +48,7 @@ public: explicit SearchResultTreeView(QWidget *parent = 0); void setAutoExpandResults(bool expand); - void setTextEditorFont(const QFont &font); + void setTextEditorFont(const QFont &font, const SearchResultColor color); SearchResultTreeModel *model() const; void addResults(const QList &items, SearchResult::AddMode mode); diff --git a/src/plugins/find/searchresultwidget.cpp b/src/plugins/find/searchresultwidget.cpp index 6178848e2c3..e61d36c614f 100644 --- a/src/plugins/find/searchresultwidget.cpp +++ b/src/plugins/find/searchresultwidget.cpp @@ -32,6 +32,7 @@ #include "searchresulttreemodel.h" #include "searchresulttreeitems.h" #include "searchresulttreeitemroles.h" +#include "searchresultcolor.h" #include "ifindsupport.h" #include "treeviewfind.h" @@ -318,9 +319,9 @@ void SearchResultWidget::notifyVisibilityChanged(bool visible) emit visibilityChanged(visible); } -void SearchResultWidget::setTextEditorFont(const QFont &font) +void SearchResultWidget::setTextEditorFont(const QFont &font, const SearchResultColor color) { - m_searchResultTreeView->setTextEditorFont(font); + m_searchResultTreeView->setTextEditorFont(font, color); } void SearchResultWidget::setAutoExpandResults(bool expand) diff --git a/src/plugins/find/searchresultwidget.h b/src/plugins/find/searchresultwidget.h index f9022bdf0ec..62c4321c93b 100644 --- a/src/plugins/find/searchresultwidget.h +++ b/src/plugins/find/searchresultwidget.h @@ -44,6 +44,7 @@ namespace Find { namespace Internal { class SearchResultTreeView; +class SearchResultColor; class SearchResultWidget : public QWidget { @@ -73,7 +74,7 @@ public: void notifyVisibilityChanged(bool visible); - void setTextEditorFont(const QFont &font); + void setTextEditorFont(const QFont &font, const SearchResultColor color); void setAutoExpandResults(bool expand); void expandAll(); diff --git a/src/plugins/find/searchresultwindow.cpp b/src/plugins/find/searchresultwindow.cpp index 980456178a1..f8e89d19298 100644 --- a/src/plugins/find/searchresultwindow.cpp +++ b/src/plugins/find/searchresultwindow.cpp @@ -29,6 +29,7 @@ #include "searchresultwindow.h" #include "searchresultwidget.h" +#include "searchresultcolor.h" #include "findtoolwindow.h" #include @@ -96,6 +97,7 @@ namespace Internal { QList m_searchResults; int m_currentIndex; QFont m_font; + SearchResultColor m_color; public slots: void setCurrentIndex(int index); @@ -398,7 +400,7 @@ SearchResult *SearchResultWindow::startNewSearch(const QString &label, connect(widget, SIGNAL(navigateStateChanged()), this, SLOT(navigateStateChanged())); connect(widget, SIGNAL(restarted()), d, SLOT(moveWidgetToTop())); connect(widget, SIGNAL(requestPopup(bool)), d, SLOT(popupRequested(bool))); - widget->setTextEditorFont(d->m_font); + widget->setTextEditorFont(d->m_font, d->m_color); widget->setShowReplaceUI(searchOrSearchAndReplace != SearchOnly); widget->setAutoExpandResults(d->m_expandCollapseAction->isChecked()); widget->setInfo(label, toolTip, searchTerm); @@ -470,11 +472,25 @@ void SearchResultWindow::setFocus() \fn void SearchResultWindow::setTextEditorFont(const QFont &font) \internal */ -void SearchResultWindow::setTextEditorFont(const QFont &font) +void SearchResultWindow::setTextEditorFont(const QFont &font, + const QColor &textForegroundColor, + const QColor &textBackgroundColor, + const QColor &highlightForegroundColor, + const QColor &highlightBackgroundColor) { d->m_font = font; + Internal::SearchResultColor color; + color.textBackground = textBackgroundColor; + color.textForeground = textForegroundColor; + color.highlightBackground = highlightBackgroundColor.isValid() + ? highlightBackgroundColor + : textBackgroundColor; + color.highlightForeground = highlightForegroundColor.isValid() + ? highlightForegroundColor + : textForegroundColor; + d->m_color = color; foreach (Internal::SearchResultWidget *widget, d->m_searchResultWidgets) - widget->setTextEditorFont(font); + widget->setTextEditorFont(font, color); } void SearchResultWindow::openNewSearchPanel() diff --git a/src/plugins/find/searchresultwindow.h b/src/plugins/find/searchresultwindow.h index e625f32e493..dc737d13915 100644 --- a/src/plugins/find/searchresultwindow.h +++ b/src/plugins/find/searchresultwindow.h @@ -157,7 +157,11 @@ public: void goToPrev(); bool canNavigate() const; - void setTextEditorFont(const QFont &font); + void setTextEditorFont(const QFont &font, + const QColor &textForegroundColor, + const QColor &textBackgroundColor, + const QColor &highlightForegroundColor, + const QColor &highlightBackgroundColor); void openNewSearchPanel(); // The search result window owns the returned SearchResult diff --git a/src/plugins/texteditor/fontsettings.cpp b/src/plugins/texteditor/fontsettings.cpp index e9c4b2df092..4b434c6aa91 100644 --- a/src/plugins/texteditor/fontsettings.cpp +++ b/src/plugins/texteditor/fontsettings.cpp @@ -260,6 +260,12 @@ void FontSettings::setAntialias(bool antialias) * Returns the format for the given font category. */ Format &FontSettings::formatFor(TextStyle category) + +{ + return m_scheme.formatFor(category); +} + +Format FontSettings::formatFor(TextStyle category) const { return m_scheme.formatFor(category); } diff --git a/src/plugins/texteditor/fontsettings.h b/src/plugins/texteditor/fontsettings.h index 31720e5eae9..33f7a0299f9 100644 --- a/src/plugins/texteditor/fontsettings.h +++ b/src/plugins/texteditor/fontsettings.h @@ -85,6 +85,7 @@ public: void setAntialias(bool antialias); Format &formatFor(TextStyle category); + Format formatFor(TextStyle category) const; QString colorSchemeFileName() const; void setColorSchemeFileName(const QString &fileName); diff --git a/src/plugins/texteditor/texteditorplugin.cpp b/src/plugins/texteditor/texteditorplugin.cpp index d6d75baafb7..e3ef0e315bc 100644 --- a/src/plugins/texteditor/texteditorplugin.cpp +++ b/src/plugins/texteditor/texteditorplugin.cpp @@ -274,9 +274,14 @@ void TextEditorPlugin::invokeQuickFix() void TextEditorPlugin::updateSearchResultsFont(const FontSettings &settings) { - if (m_searchResultWindow) + if (m_searchResultWindow) { m_searchResultWindow->setTextEditorFont(QFont(settings.family(), - settings.fontSize() * settings.fontZoom() / 100)); + settings.fontSize() * settings.fontZoom() / 100), + settings.formatFor(TextEditor::C_TEXT).foreground(), + settings.formatFor(TextEditor::C_TEXT).background(), + settings.formatFor(TextEditor::C_SEARCH_RESULT).foreground(), + settings.formatFor(TextEditor::C_SEARCH_RESULT).background()); + } } void TextEditorPlugin::updateVariable(const QByteArray &variable)