Commit f06ff7b1 authored by David Schulz's avatar David Schulz

Find: Add text editor color scheme to search result

Task-number: QTCREATORBUG-8070

Change-Id: Ic220e3364aa9a5227518a210dbf0590deb06a4d6
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
parent e0939df1
......@@ -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
......@@ -35,6 +35,7 @@ QtcPlugin {
"ifindfilter.h",
"ifindsupport.cpp",
"ifindsupport.h",
"searchresultcolor.h",
"searchresulttreeitemdelegate.cpp",
"searchresulttreeitemdelegate.h",
"searchresulttreeitemroles.h",
......
#ifndef SEARCHRESULTCOLOR_H
#define SEARCHRESULTCOLOR_H
#include <QColor>
namespace Find {
namespace Internal {
struct SearchResultColor{
QColor textBackground;
QColor textForeground;
QColor highlightBackground;
QColor highlightForeground;
};
} // namespace Internal
} // namespace Find
#endif // SEARCHRESULTCOLOR_H
......@@ -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<QColor>();
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<QColor>();
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));
}
......@@ -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;
};
......
......@@ -42,6 +42,8 @@ enum Roles
ResultLineRole,
ResultLineNumberRole,
ResultIconRole,
ResultHighlightBackgroundColor,
ResultHighlightForegroundColor,
SearchTermStartRole,
SearchTermLengthRole,
IsGeneratedRole
......
......@@ -30,6 +30,7 @@
#include "searchresulttreemodel.h"
#include "searchresulttreeitems.h"
#include "searchresulttreeitemroles.h"
#include "searchresultcolor.h"
#include <QApplication>
#include <QFont>
......@@ -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;
......
......@@ -31,6 +31,7 @@
#define SEARCHRESULTTREEMODEL_H
#include "searchresultwindow.h"
#include "searchresultcolor.h"
#include <QAbstractItemModel>
#include <QRegExp>
......@@ -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;
......
......@@ -31,6 +31,7 @@
#include "searchresulttreeitemroles.h"
#include "searchresulttreemodel.h"
#include "searchresulttreeitemdelegate.h"
#include "searchresultcolor.h"
#include <QHeaderView>
#include <QKeyEvent>
......@@ -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()
......
......@@ -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<Find::SearchResultItem> &items, SearchResult::AddMode mode);
......
......@@ -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)
......
......@@ -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();
......
......@@ -29,6 +29,7 @@
#include "searchresultwindow.h"
#include "searchresultwidget.h"
#include "searchresultcolor.h"
#include "findtoolwindow.h"
#include <coreplugin/icore.h>
......@@ -96,6 +97,7 @@ namespace Internal {
QList<SearchResult *> 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()
......
......@@ -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
......
......@@ -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);
}
......
......@@ -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);
......
......@@ -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)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment