diff options
author | Christian Kandeler <christian.kandeler@qt.io> | 2020-09-10 14:33:14 +0200 |
---|---|---|
committer | Christian Kandeler <christian.kandeler@qt.io> | 2020-09-18 09:33:07 +0000 |
commit | 4376bc1e3979a6cfb7a3a431207a4ca6694ad501 (patch) | |
tree | b9acd35a7f64b97f7a03819c33981511edf786b0 /src | |
parent | 03983a26483ca9a27babecc73681765c50af1ae0 (diff) |
Core: Allow different highlight colors in search result window
... and make use of that in CppTool's "Find Usages" by assigning
different colors to read and write accesses.
Fixes: QTCREATORBUG-12734
Change-Id: I067db2c8d693bb2c5be44249931ee4f0269f7e52
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
Diffstat (limited to 'src')
17 files changed, 114 insertions, 65 deletions
diff --git a/src/plugins/coreplugin/find/searchresultcolor.h b/src/plugins/coreplugin/find/searchresultcolor.h index 51da45b9ca..0291133c0b 100644 --- a/src/plugins/coreplugin/find/searchresultcolor.h +++ b/src/plugins/coreplugin/find/searchresultcolor.h @@ -25,18 +25,41 @@ #pragma once +#include "../core_global.h" + #include <QColor> +#include <QHash> namespace Core { -namespace Internal { -class SearchResultColor{ +class CORE_EXPORT SearchResultColor { public: + enum class Style { Default, Alt1, Alt2 }; + + SearchResultColor() = default; + SearchResultColor(const QColor &textBg, const QColor &textFg, + const QColor &highlightBg, const QColor &highlightFg) + : textBackground(textBg), textForeground(textFg), + highlightBackground(highlightBg), highlightForeground(highlightFg) + { + if (!highlightBackground.isValid()) + highlightBackground = textBackground; + if (!highlightForeground.isValid()) + highlightForeground = textForeground; + } + QColor textBackground; QColor textForeground; QColor highlightBackground; QColor highlightForeground; }; -} // namespace Internal + +inline uint qHash(SearchResultColor::Style style) +{ + return QT_PREPEND_NAMESPACE(qHash(int(style))); +} + +using SearchResultColors = QHash<SearchResultColor::Style, SearchResultColor>; + } // namespace Core diff --git a/src/plugins/coreplugin/find/searchresultitem.h b/src/plugins/coreplugin/find/searchresultitem.h index 8717b73b11..ea7ac657e3 100644 --- a/src/plugins/coreplugin/find/searchresultitem.h +++ b/src/plugins/coreplugin/find/searchresultitem.h @@ -25,6 +25,8 @@ #pragma once +#include "searchresultcolor.h" + #include <utils/hostosinfo.h> #include <QIcon> @@ -94,6 +96,7 @@ public: QVariant userData; // user data for identification of the item Search::TextRange mainRange; bool useTextEditorFont = false; + SearchResultColor::Style style = SearchResultColor::Style::Default; }; } // namespace Core diff --git a/src/plugins/coreplugin/find/searchresulttreemodel.cpp b/src/plugins/coreplugin/find/searchresulttreemodel.cpp index 81ba280c0d..67fee6f98b 100644 --- a/src/plugins/coreplugin/find/searchresulttreemodel.cpp +++ b/src/plugins/coreplugin/find/searchresulttreemodel.cpp @@ -69,11 +69,11 @@ void SearchResultTreeModel::setShowReplaceUI(bool show) } } -void SearchResultTreeModel::setTextEditorFont(const QFont &font, const SearchResultColor &color) +void SearchResultTreeModel::setTextEditorFont(const QFont &font, const SearchResultColors &colors) { emit layoutAboutToBeChanged(); m_textEditorFont = font; - m_color = color; + m_colors = colors; emit layoutChanged(); } @@ -248,10 +248,10 @@ QVariant SearchResultTreeModel::data(const SearchResultTreeItem *row, int role) result = QVariant(); break; case Qt::ForegroundRole: - result = m_color.textForeground; + result = m_colors.value(row->item.style).textForeground; break; case Qt::BackgroundRole: - result = m_color.textBackground; + result = m_colors.value(row->item.style).textBackground; break; case ItemDataRoles::ResultLineRole: case Qt::DisplayRole: @@ -267,10 +267,10 @@ QVariant SearchResultTreeModel::data(const SearchResultTreeItem *row, int role) result = row->item.icon; break; case ItemDataRoles::ResultHighlightBackgroundColor: - result = m_color.highlightBackground; + result = m_colors.value(row->item.style).highlightBackground; break; case ItemDataRoles::ResultHighlightForegroundColor: - result = m_color.highlightForeground; + result = m_colors.value(row->item.style).highlightForeground; break; case ItemDataRoles::ResultBeginColumnNumberRole: result = row->item.mainRange.begin.column; diff --git a/src/plugins/coreplugin/find/searchresulttreemodel.h b/src/plugins/coreplugin/find/searchresulttreemodel.h index a8514e74c3..09762c5147 100644 --- a/src/plugins/coreplugin/find/searchresulttreemodel.h +++ b/src/plugins/coreplugin/find/searchresulttreemodel.h @@ -45,7 +45,7 @@ public: ~SearchResultTreeModel() override; void setShowReplaceUI(bool show); - void setTextEditorFont(const QFont &font, const SearchResultColor &color); + void setTextEditorFont(const QFont &font, const SearchResultColors &colors); Qt::ItemFlags flags(const QModelIndex &index) const override; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; @@ -80,7 +80,7 @@ private: SearchResultTreeItem *m_rootItem; SearchResultTreeItem *m_currentParent; - SearchResultColor m_color; + SearchResultColors m_colors; QModelIndex m_currentIndex; QStringList m_currentPath; // the path that belongs to the current parent QFont m_textEditorFont; diff --git a/src/plugins/coreplugin/find/searchresulttreeview.cpp b/src/plugins/coreplugin/find/searchresulttreeview.cpp index 48d1845053..44f46b0e2a 100644 --- a/src/plugins/coreplugin/find/searchresulttreeview.cpp +++ b/src/plugins/coreplugin/find/searchresulttreeview.cpp @@ -57,12 +57,12 @@ void SearchResultTreeView::setAutoExpandResults(bool expand) m_autoExpandResults = expand; } -void SearchResultTreeView::setTextEditorFont(const QFont &font, const SearchResultColor &color) +void SearchResultTreeView::setTextEditorFont(const QFont &font, const SearchResultColors &colors) { - m_model->setTextEditorFont(font, color); + m_model->setTextEditorFont(font, colors); QPalette p; - p.setColor(QPalette::Base, color.textBackground); + p.setColor(QPalette::Base, colors.value(SearchResultColor::Style::Default).textBackground); setPalette(p); } diff --git a/src/plugins/coreplugin/find/searchresulttreeview.h b/src/plugins/coreplugin/find/searchresulttreeview.h index 4809a6887b..ff9a3f0218 100644 --- a/src/plugins/coreplugin/find/searchresulttreeview.h +++ b/src/plugins/coreplugin/find/searchresulttreeview.h @@ -30,10 +30,11 @@ #include <utils/itemviews.h> namespace Core { +class SearchResultColor; + namespace Internal { class SearchResultTreeModel; -class SearchResultColor; class SearchResultTreeView : public Utils::TreeView { @@ -43,7 +44,7 @@ public: explicit SearchResultTreeView(QWidget *parent = nullptr); void setAutoExpandResults(bool expand); - void setTextEditorFont(const QFont &font, const SearchResultColor &color); + void setTextEditorFont(const QFont &font, const SearchResultColors &colors); void setTabWidth(int tabWidth); SearchResultTreeModel *model() const; diff --git a/src/plugins/coreplugin/find/searchresultwidget.cpp b/src/plugins/coreplugin/find/searchresultwidget.cpp index ce1e079303..048bef157b 100644 --- a/src/plugins/coreplugin/find/searchresultwidget.cpp +++ b/src/plugins/coreplugin/find/searchresultwidget.cpp @@ -246,7 +246,8 @@ void SearchResultWidget::setAdditionalReplaceWidget(QWidget *widget) void SearchResultWidget::addResult(const QString &fileName, const QString &rowText, Search::TextRange mainRange, - const QVariant &userData) + const QVariant &userData, + SearchResultColor::Style style) { SearchResultItem item; item.path = QStringList({QDir::toNativeSeparators(fileName)}); @@ -254,6 +255,7 @@ void SearchResultWidget::addResult(const QString &fileName, item.text = rowText; item.useTextEditorFont = true; item.userData = userData; + item.style = style; addResults(QList<SearchResultItem>() << item, SearchResult::AddOrdered); } @@ -367,9 +369,9 @@ void SearchResultWidget::notifyVisibilityChanged(bool visible) emit visibilityChanged(visible); } -void SearchResultWidget::setTextEditorFont(const QFont &font, const SearchResultColor &color) +void SearchResultWidget::setTextEditorFont(const QFont &font, const SearchResultColors &colors) { - m_searchResultTreeView->setTextEditorFont(font, color); + m_searchResultTreeView->setTextEditorFont(font, colors); } void SearchResultWidget::setTabWidth(int tabWidth) diff --git a/src/plugins/coreplugin/find/searchresultwidget.h b/src/plugins/coreplugin/find/searchresultwidget.h index be563a5bc1..a22ac982da 100644 --- a/src/plugins/coreplugin/find/searchresultwidget.h +++ b/src/plugins/coreplugin/find/searchresultwidget.h @@ -40,10 +40,9 @@ class QCheckBox; QT_END_NAMESPACE namespace Core { -namespace Internal { +namespace Internal { class SearchResultTreeView; -class SearchResultColor; class SearchResultWidget : public QWidget { @@ -59,7 +58,8 @@ public: void addResult(const QString &fileName, const QString &lineText, Search::TextRange mainRange, - const QVariant &userData = QVariant()); + const QVariant &userData = QVariant(), + SearchResultColor::Style style = SearchResultColor::Style::Default); void addResults(const QList<SearchResultItem> &items, SearchResult::AddMode mode); int count() const; @@ -76,7 +76,7 @@ public: void notifyVisibilityChanged(bool visible); - void setTextEditorFont(const QFont &font, const SearchResultColor &color); + void setTextEditorFont(const QFont &font, const SearchResultColors &colors); void setTabWidth(int tabWidth); void setAutoExpandResults(bool expand); diff --git a/src/plugins/coreplugin/find/searchresultwindow.cpp b/src/plugins/coreplugin/find/searchresultwindow.cpp index ce4e6f3b83..7d67c6dbd4 100644 --- a/src/plugins/coreplugin/find/searchresultwindow.cpp +++ b/src/plugins/coreplugin/find/searchresultwindow.cpp @@ -25,7 +25,6 @@ #include "searchresultwindow.h" #include "searchresultwidget.h" -#include "searchresultcolor.h" #include "textfindconstants.h" #include <coreplugin/icore.h> @@ -126,7 +125,7 @@ namespace Internal { QList<SearchResult *> m_searchResults; int m_currentIndex; QFont m_font; - SearchResultColor m_color; + SearchResultColors m_colors; int m_tabWidth; }; @@ -503,7 +502,7 @@ SearchResult *SearchResultWindow::startNewSearch(const QString &label, d, &SearchResultWindowPrivate::moveWidgetToTop); connect(widget, &SearchResultWidget::requestPopup, d, &SearchResultWindowPrivate::popupRequested); - widget->setTextEditorFont(d->m_font, d->m_color); + widget->setTextEditorFont(d->m_font, d->m_colors); widget->setTabWidth(d->m_tabWidth); widget->setSupportPreserveCase(preserveCaseMode == PreserveCaseEnabled); bool supportsReplace = searchOrSearchAndReplace != SearchOnly; @@ -574,25 +573,12 @@ void SearchResultWindow::setFocus() /*! \internal */ -void SearchResultWindow::setTextEditorFont(const QFont &font, - const QColor &textForegroundColor, - const QColor &textBackgroundColor, - const QColor &highlightForegroundColor, - const QColor &highlightBackgroundColor) +void SearchResultWindow::setTextEditorFont(const QFont &font, const SearchResultColors &colors) { 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; + d->m_colors = colors; foreach (Internal::SearchResultWidget *widget, d->m_searchResultWidgets) - widget->setTextEditorFont(font, color); + widget->setTextEditorFont(font, colors); } /*! @@ -796,7 +782,8 @@ void SearchResult::setAdditionalReplaceWidget(QWidget *widget) \sa addResults() */ void SearchResult::addResult(const QString &fileName, int lineNumber, const QString &lineText, - int searchTermStart, int searchTermLength, const QVariant &userData) + int searchTermStart, int searchTermLength, const QVariant &userData, + SearchResultColor::Style style) { Search::TextRange mainRange; mainRange.begin.line = lineNumber; @@ -804,7 +791,7 @@ void SearchResult::addResult(const QString &fileName, int lineNumber, const QStr mainRange.end.line = mainRange.begin.line; mainRange.end.column = mainRange.begin.column + searchTermLength; - m_widget->addResult(fileName, lineText, mainRange, userData); + m_widget->addResult(fileName, lineText, mainRange, userData, style); } /*! @@ -822,9 +809,10 @@ void SearchResult::addResult(const QString &fileName, int lineNumber, const QStr void SearchResult::addResult(const QString &fileName, const QString &lineText, Search::TextRange mainRange, - const QVariant &userData) + const QVariant &userData, + SearchResultColor::Style style) { - m_widget->addResult(fileName, lineText, mainRange, userData); + m_widget->addResult(fileName, lineText, mainRange, userData, style); emit countChanged(m_widget->count()); } diff --git a/src/plugins/coreplugin/find/searchresultwindow.h b/src/plugins/coreplugin/find/searchresultwindow.h index 0ca0a55b63..935c85ce97 100644 --- a/src/plugins/coreplugin/find/searchresultwindow.h +++ b/src/plugins/coreplugin/find/searchresultwindow.h @@ -25,6 +25,7 @@ #pragma once +#include "searchresultcolor.h" #include "searchresultitem.h" #include <coreplugin/ioutputpane.h> @@ -68,11 +69,13 @@ public slots: const QString &lineText, int searchTermStart, int searchTermLength, - const QVariant &userData = QVariant()); + const QVariant &userData = QVariant(), + SearchResultColor::Style style = SearchResultColor::Style::Default); void addResult(const QString &fileName, const QString &lineText, Search::TextRange mainRange, - const QVariant &userData = QVariant()); + const QVariant &userData = QVariant(), + SearchResultColor::Style style = SearchResultColor::Style::Default); void addResults(const QList<SearchResultItem> &items, AddMode mode); void finishSearch(bool canceled); void setTextToReplace(const QString &textToReplace); @@ -137,11 +140,7 @@ public: void goToPrev() override; bool canNavigate() const override; - void setTextEditorFont(const QFont &font, - const QColor &textForegroundColor, - const QColor &textBackgroundColor, - const QColor &highlightForegroundColor, - const QColor &highlightBackgroundColor); + void setTextEditorFont(const QFont &font, const SearchResultColors &colors); void setTabWidth(int width); void openNewSearchPanel(); diff --git a/src/plugins/cpptools/cppfindreferences.cpp b/src/plugins/cpptools/cppfindreferences.cpp index 1f567bed7e..00d8c096e1 100644 --- a/src/plugins/cpptools/cppfindreferences.cpp +++ b/src/plugins/cpptools/cppfindreferences.cpp @@ -551,13 +551,23 @@ static void displayResults(SearchResult *search, QFutureWatcher<CPlusPlus::Usage { CppFindReferencesParameters parameters = search->userData().value<CppFindReferencesParameters>(); + static const auto colorStyleForUsageType = [](CPlusPlus::Usage::Type type) { + switch (type) { + case CPlusPlus::Usage::Type::Read: + return SearchResultColor::Style::Alt1; + case CPlusPlus::Usage::Type::Write: + case CPlusPlus::Usage::Type::WritableRef: + return SearchResultColor::Style::Alt2; + case CPlusPlus::Usage::Type::Declaration: + case CPlusPlus::Usage::Type::Other: + return SearchResultColor::Style::Default; + } + return SearchResultColor::Style::Default; // For dumb compilers. + }; for (int index = first; index != last; ++index) { - CPlusPlus::Usage result = watcher->future().resultAt(index); - search->addResult(result.path.toString(), - result.line, - result.lineText, - result.col, - result.len); + const CPlusPlus::Usage result = watcher->future().resultAt(index); + search->addResult(result.path.toString(), result.line, result.lineText, + result.col, result.len, {}, colorStyleForUsageType(result.type)); if (parameters.prettySymbolName.isEmpty()) continue; diff --git a/src/plugins/texteditor/fontsettings.cpp b/src/plugins/texteditor/fontsettings.cpp index 9e8b39c72d..d74a13b2cc 100644 --- a/src/plugins/texteditor/fontsettings.cpp +++ b/src/plugins/texteditor/fontsettings.cpp @@ -151,6 +151,8 @@ static bool isOverlayCategory(TextStyle category) return category == C_OCCURRENCES || category == C_OCCURRENCES_RENAME || category == C_SEARCH_RESULT + || category == C_SEARCH_RESULT_ALT1 + || category == C_SEARCH_RESULT_ALT2 || category == C_PARENTHESES_MISMATCH; } diff --git a/src/plugins/texteditor/fontsettingspage.cpp b/src/plugins/texteditor/fontsettingspage.cpp index 56ecb8f023..0e35cade04 100644 --- a/src/plugins/texteditor/fontsettingspage.cpp +++ b/src/plugins/texteditor/fontsettingspage.cpp @@ -340,9 +340,9 @@ QColor FormatDescription::defaultBackground(TextStyle id) return col; } else if (id == C_SELECTION) { return Utils::Theme::initialPalette().color(QPalette::Highlight); - } else if (id == C_OCCURRENCES) { + } else if (id == C_OCCURRENCES || id == C_SEARCH_RESULT_ALT1) { return QColor(180, 180, 180); - } else if (id == C_OCCURRENCES_RENAME) { + } else if (id == C_OCCURRENCES_RENAME || id == C_SEARCH_RESULT_ALT2) { return QColor(255, 100, 100); } else if (id == C_DISABLED_CODE) { return QColor(239, 239, 239); diff --git a/src/plugins/texteditor/texteditorconstants.cpp b/src/plugins/texteditor/texteditorconstants.cpp index 70ad3e6c04..3902c13665 100644 --- a/src/plugins/texteditor/texteditorconstants.cpp +++ b/src/plugins/texteditor/texteditorconstants.cpp @@ -39,6 +39,8 @@ const char *nameForStyle(TextStyle style) case C_SELECTION: return "Selection"; case C_LINE_NUMBER: return "LineNumber"; case C_SEARCH_RESULT: return "SearchResult"; + case C_SEARCH_RESULT_ALT1: return "SearchResultAlt1"; + case C_SEARCH_RESULT_ALT2: return "SearchResultAlt2"; case C_SEARCH_SCOPE: return "SearchScope"; case C_PARENTHESES: return "Parentheses"; case C_PARENTHESES_MISMATCH:return "ParenthesesMismatch"; diff --git a/src/plugins/texteditor/texteditorconstants.h b/src/plugins/texteditor/texteditorconstants.h index 740c687d3e..6a1e8637d3 100644 --- a/src/plugins/texteditor/texteditorconstants.h +++ b/src/plugins/texteditor/texteditorconstants.h @@ -39,6 +39,8 @@ enum TextStyle : quint8 { C_SELECTION, C_LINE_NUMBER, C_SEARCH_RESULT, + C_SEARCH_RESULT_ALT1, + C_SEARCH_RESULT_ALT2, C_SEARCH_SCOPE, C_PARENTHESES, C_PARENTHESES_MISMATCH, diff --git a/src/plugins/texteditor/texteditorplugin.cpp b/src/plugins/texteditor/texteditorplugin.cpp index 6c614a36b3..2fcad58e37 100644 --- a/src/plugins/texteditor/texteditorplugin.cpp +++ b/src/plugins/texteditor/texteditorplugin.cpp @@ -250,11 +250,20 @@ ExtensionSystem::IPlugin::ShutdownFlag TextEditorPlugin::aboutToShutdown() void TextEditorPluginPrivate::updateSearchResultsFont(const FontSettings &settings) { if (auto window = SearchResultWindow::instance()) { + const Format textFormat = settings.formatFor(C_TEXT); + const Format defaultResultFormat = settings.formatFor(C_SEARCH_RESULT); + const Format alt1ResultFormat = settings.formatFor(C_SEARCH_RESULT_ALT1); + const Format alt2ResultFormat = settings.formatFor(C_SEARCH_RESULT_ALT2); window->setTextEditorFont(QFont(settings.family(), settings.fontSize() * settings.fontZoom() / 100), - settings.formatFor(C_TEXT).foreground(), - settings.formatFor(C_TEXT).background(), - settings.formatFor(C_SEARCH_RESULT).foreground(), - settings.formatFor(C_SEARCH_RESULT).background()); + {std::make_pair(SearchResultColor::Style::Default, + SearchResultColor(textFormat.background(), textFormat.foreground(), + defaultResultFormat.background(), defaultResultFormat.foreground())), + std::make_pair(SearchResultColor::Style::Alt1, + SearchResultColor(textFormat.background(), textFormat.foreground(), + alt1ResultFormat.background(), alt1ResultFormat.foreground())), + std::make_pair(SearchResultColor::Style::Alt2, + SearchResultColor(textFormat.background(), textFormat.foreground(), + alt2ResultFormat.background(), alt2ResultFormat.foreground()))}); } } diff --git a/src/plugins/texteditor/texteditorsettings.cpp b/src/plugins/texteditor/texteditorsettings.cpp index e9a49cbc8a..db6127d72d 100644 --- a/src/plugins/texteditor/texteditorsettings.cpp +++ b/src/plugins/texteditor/texteditorsettings.cpp @@ -104,6 +104,14 @@ FormatDescriptions TextEditorSettingsPrivate::initialFormats() formatDescr.emplace_back(C_SEARCH_RESULT, tr("Search Result"), tr("Highlighted search results inside the editor."), FormatDescription::ShowBackgroundControl); + formatDescr.emplace_back(C_SEARCH_RESULT_ALT1, tr("Search Result (alternative 1)"), + tr("Highlighted search results inside the editor.\n" + "Used to mark read accesses to C++ symbols."), + FormatDescription::ShowBackgroundControl); + formatDescr.emplace_back(C_SEARCH_RESULT_ALT2, tr("Search Result (alternative 2)"), + tr("Highlighted search results inside the editor.\n" + "Used to mark write accesses to C++ symbols."), + FormatDescription::ShowBackgroundControl); formatDescr.emplace_back(C_SEARCH_SCOPE, tr("Search Scope"), tr("Section where the pattern is searched in."), FormatDescription::ShowBackgroundControl); |