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>
This commit is contained in:
Christian Kandeler
2020-09-10 14:33:14 +02:00
parent 03983a2648
commit 4376bc1e39
24 changed files with 128 additions and 65 deletions

View File

@@ -5,6 +5,8 @@
<style name="Selection" foreground="#bec0c2" background="#1d545c"/>
<style name="LineNumber" foreground="#bec0c2" background="#404244"/>
<style name="SearchResult" background="#8a7f2c"/>
<style name="SearchResultAlt1" background="#8a402c"/>
<style name="SearchResultAlt2" foreground="#ffaaaa" background="#553636"/>
<style name="SearchScope" background="#8a602c"/>
<style name="Parentheses" foreground="#bec0c2" background="#1d545c"/>
<style name="ParenthesesMismatch" background="#ff6aad"/>

View File

@@ -33,6 +33,8 @@
<style name="AutoComplete" foreground="#a0a0ff" background="#333333"/>
<style name="Preprocessor" foreground="#5555ff"/>
<style name="SearchResult" background="#555500"/>
<style name="SearchResultAlt1" background="#363636"/>
<style name="SearchResultAlt2" foreground="#ffaaaa" background="#553636"/>
<style name="SearchScope" background="#222200"/>
<style name="Selection" foreground="#000000" background="#aaaaaa"/>
<style name="Static" foreground="#55ff55" italic="true"/>

View File

@@ -5,6 +5,8 @@
<style name="Selection" foreground="#ffffff" background="#308cc6"/>
<style name="LineNumber" foreground="#9f9d9a" background="#efebe7"/>
<style name="SearchResult" background="#ffef0b"/>
<style name="SearchResultAlt1" background="#b4b4b4"/>
<style name="SearchResultAlt2" background="#ff6464"/>
<style name="SearchScope" background="#f5fafd"/>
<style name="Parentheses" foreground="#ff0000" background="#b4eeb4"/>
<style name="ParenthesesMismatch" background="#ff00ff"/>

View File

@@ -40,6 +40,8 @@
<style name="Preprocessor" foreground="#409090"/>
<style name="RemovedLine" foreground="#ff0000"/>
<style name="SearchResult" foreground="#000000" background="#ffef0b"/>
<style name="SearchResultAlt1" foreground="#000000" background="#616161"/>
<style name="SearchResultAlt2" foreground="#000000" background="#ff6464"/>
<style name="SearchScope" foreground="#000000" background="#f8fafc"/>
<style name="Selection" foreground="#ffffff" background="#4e4e8f"/>
<style name="Static" foreground="#cb6ecb"/>

View File

@@ -14,6 +14,8 @@
<style name="Selection" background="#11404c"/>
<style name="LineNumber" foreground="#888888" background="#272822"/>
<style name="SearchResult" background="#555500"/>
<style name="SearchResultAlt1" background="#aa0000"/>
<style name="SearchResultAlt2" foreground="#ffaaaa" background="#553636"/>
<style name="SearchScope" background="#222200"/>
<style name="Parentheses" foreground="#ffffff" background="#11404c"/>
<style name="CurrentLine" background="#373737"/>

View File

@@ -11,6 +11,8 @@
<style name="Selection" foreground="#002b36" background="#586e75"/>
<style name="LineNumber" foreground="#586e75" background="#073642"/>
<style name="SearchResult" background="#073642"/>
<style name="SearchResultAlt1" background="#586e75"/>
<style name="SearchResultAlt2" background="#073642"/>
<style name="SearchScope" background="#073642"/>
<style name="Parentheses" foreground="#dc322f" background="#586e75" bold="true"/>
<style name="ParenthesesMismatch" foreground="#fdff2c" background="#d33682" bold="true"/>

View File

@@ -11,6 +11,8 @@
<style name="Selection" foreground="#eee8d5" background="#93a1a1"/>
<style name="LineNumber" foreground="#93a1a1" background="#eee8d5"/>
<style name="SearchResult" background="#eee8d5"/>
<style name="SearchResultAlt1" background="#93a1a1"/>
<style name="SearchResultAlt2" background="#e0dbcb"/>
<style name="SearchScope" background="#073642"/>
<style name="Parentheses" foreground="#dc322f" background="#93a1a1" bold="true"/>
<style name="ParenthesesMismatch" foreground="#ffff00" background="#ff00ff" bold="true"/>

View File

@@ -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

View File

@@ -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

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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)

View File

@@ -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);

View File

@@ -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());
}

View File

@@ -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();

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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";

View File

@@ -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,

View File

@@ -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()))});
}
}

View File

@@ -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);