Use tab width in search results.

Task-number: QTCREATORBUG-2042
Change-Id: Ie1769cb44f60da8ef6b26d5d27f9c7a600534f6b
Reviewed-by: Eike Ziller <eike.ziller@theqtcompany.com>
This commit is contained in:
Francois Ferrand
2013-07-03 18:23:03 +02:00
parent edc08ba2bc
commit 24626a6835
10 changed files with 65 additions and 14 deletions

View File

@@ -39,9 +39,10 @@
using namespace Core::Internal; using namespace Core::Internal;
SearchResultTreeItemDelegate::SearchResultTreeItemDelegate(QObject *parent) SearchResultTreeItemDelegate::SearchResultTreeItemDelegate(int tabWidth, QObject *parent)
: QItemDelegate(parent) : QItemDelegate(parent)
{ {
setTabWidth(tabWidth);
} }
void SearchResultTreeItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const void SearchResultTreeItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
@@ -100,6 +101,11 @@ void SearchResultTreeItemDelegate::paint(QPainter *painter, const QStyleOptionVi
painter->restore(); painter->restore();
} }
void SearchResultTreeItemDelegate::setTabWidth(int width)
{
m_tabString = QString(width, QLatin1Char(' '));
}
// returns the width of the line number area // returns the width of the line number area
int SearchResultTreeItemDelegate::drawLineNumber(QPainter *painter, const QStyleOptionViewItemV3 &option, int SearchResultTreeItemDelegate::drawLineNumber(QPainter *painter, const QStyleOptionViewItemV3 &option,
const QRect &rect, const QRect &rect,
@@ -156,14 +162,18 @@ void SearchResultTreeItemDelegate::drawText(QPainter *painter,
const int searchTermStart = index.model()->data(index, ItemDataRoles::SearchTermStartRole).toInt(); const int searchTermStart = index.model()->data(index, ItemDataRoles::SearchTermStartRole).toInt();
int searchTermLength = index.model()->data(index, ItemDataRoles::SearchTermLengthRole).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, option, rect, text); QItemDelegate::drawDisplay(painter, option, rect, text.replace(QLatin1Char('\t'), m_tabString));
return; return;
} }
// clip searchTermLength to end of line // clip searchTermLength to end of line
searchTermLength = qMin(searchTermLength, text.length() - searchTermStart); searchTermLength = qMin(searchTermLength, text.length() - searchTermStart);
const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
int searchTermStartPixels = painter->fontMetrics().width(text.left(searchTermStart)); const QString textBefore = text.left(searchTermStart).replace(QLatin1Char('\t'), m_tabString);
int searchTermLengthPixels = painter->fontMetrics().width(text.mid(searchTermStart, searchTermLength)); const QString textHighlight = text.mid(searchTermStart, searchTermLength).replace(QLatin1Char('\t'), m_tabString);
const QString textAfter = text.mid(searchTermStart + searchTermLength).replace(QLatin1Char('\t'), m_tabString);
int searchTermStartPixels = painter->fontMetrics().width(textBefore);
int searchTermLengthPixels = painter->fontMetrics().width(textHighlight);
// rects // rects
QRect beforeHighlightRect(rect); QRect beforeHighlightRect(rect);
@@ -203,19 +213,16 @@ void SearchResultTreeItemDelegate::drawText(QPainter *painter,
noHighlightOpt.textElideMode = Qt::ElideNone; noHighlightOpt.textElideMode = Qt::ElideNone;
if (isSelected) if (isSelected)
noHighlightOpt.palette.setColor(QPalette::Text, noHighlightOpt.palette.color(cg, QPalette::HighlightedText)); noHighlightOpt.palette.setColor(QPalette::Text, noHighlightOpt.palette.color(cg, QPalette::HighlightedText));
QItemDelegate::drawDisplay(painter, noHighlightOpt, QItemDelegate::drawDisplay(painter, noHighlightOpt, beforeHighlightRect, textBefore);
beforeHighlightRect, text.mid(0, searchTermStart));
// Highlight text // Highlight text
QStyleOptionViewItem highlightOpt = noHighlightOpt; QStyleOptionViewItem highlightOpt = noHighlightOpt;
const QColor highlightForeground = const QColor highlightForeground =
index.model()->data(index, ItemDataRoles::ResultHighlightForegroundColor).value<QColor>(); index.model()->data(index, ItemDataRoles::ResultHighlightForegroundColor).value<QColor>();
highlightOpt.palette.setColor(QPalette::Text, highlightForeground); highlightOpt.palette.setColor(QPalette::Text, highlightForeground);
QItemDelegate::drawDisplay(painter, highlightOpt, resultHighlightRect, QItemDelegate::drawDisplay(painter, highlightOpt, resultHighlightRect, textHighlight);
text.mid(searchTermStart, searchTermLength));
// Text after the Highlight // Text after the Highlight
noHighlightOpt.rect = afterHighlightRect; noHighlightOpt.rect = afterHighlightRect;
QItemDelegate::drawDisplay(painter, noHighlightOpt, afterHighlightRect, QItemDelegate::drawDisplay(painter, noHighlightOpt, afterHighlightRect, textAfter);
text.mid(searchTermStart + searchTermLength));
} }

View File

@@ -39,14 +39,16 @@ namespace Internal {
class SearchResultTreeItemDelegate: public QItemDelegate class SearchResultTreeItemDelegate: public QItemDelegate
{ {
public: public:
SearchResultTreeItemDelegate(QObject *parent = 0); SearchResultTreeItemDelegate(int tabWidth, QObject *parent = 0);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setTabWidth(int width);
private: private:
int drawLineNumber(QPainter *painter, const QStyleOptionViewItemV3 &option, const QRect &rect, const QModelIndex &index) const; int drawLineNumber(QPainter *painter, const QStyleOptionViewItemV3 &option, const QRect &rect, const QModelIndex &index) const;
void drawText(QPainter *painter, const QStyleOptionViewItem &option, void drawText(QPainter *painter, const QStyleOptionViewItem &option,
const QRect &rect, const QModelIndex &index) const; const QRect &rect, const QModelIndex &index) const;
QString m_tabString;
static const int m_minimumLineNumberDigits = 6; static const int m_minimumLineNumberDigits = 6;
}; };

View File

@@ -45,7 +45,7 @@ SearchResultTreeView::SearchResultTreeView(QWidget *parent)
, m_autoExpandResults(false) , m_autoExpandResults(false)
{ {
setModel(m_model); setModel(m_model);
setItemDelegate(new SearchResultTreeItemDelegate(this)); setItemDelegate(new SearchResultTreeItemDelegate(8, this));
setIndentation(14); setIndentation(14);
setUniformRowHeights(true); setUniformRowHeights(true);
setExpandsOnDoubleClick(true); setExpandsOnDoubleClick(true);
@@ -91,6 +91,13 @@ void SearchResultTreeView::emitJumpToSearchResult(const QModelIndex &index)
emit jumpToSearchResult(item); emit jumpToSearchResult(item);
} }
void SearchResultTreeView::setTabWidth(int tabWidth)
{
SearchResultTreeItemDelegate *delegate = static_cast<SearchResultTreeItemDelegate *>(itemDelegate());
delegate->setTabWidth(tabWidth);
doItemsLayout();
}
SearchResultTreeModel *SearchResultTreeView::model() const SearchResultTreeModel *SearchResultTreeView::model() const
{ {
return m_model; return m_model;

View File

@@ -50,6 +50,7 @@ public:
void setAutoExpandResults(bool expand); void setAutoExpandResults(bool expand);
void setTextEditorFont(const QFont &font, const SearchResultColor &color); void setTextEditorFont(const QFont &font, const SearchResultColor &color);
void setTabWidth(int tabWidth);
SearchResultTreeModel *model() const; SearchResultTreeModel *model() const;
void addResults(const QList<SearchResultItem> &items, SearchResult::AddMode mode); void addResults(const QList<SearchResultItem> &items, SearchResult::AddMode mode);

View File

@@ -354,6 +354,11 @@ void SearchResultWidget::setTextEditorFont(const QFont &font, const SearchResult
m_searchResultTreeView->setTextEditorFont(font, color); m_searchResultTreeView->setTextEditorFont(font, color);
} }
void SearchResultWidget::setTabWidth(int tabWidth)
{
m_searchResultTreeView->setTabWidth(tabWidth);
}
void SearchResultWidget::setAutoExpandResults(bool expand) void SearchResultWidget::setAutoExpandResults(bool expand)
{ {
m_searchResultTreeView->setAutoExpandResults(expand); m_searchResultTreeView->setAutoExpandResults(expand);

View File

@@ -81,6 +81,7 @@ public:
void notifyVisibilityChanged(bool visible); void notifyVisibilityChanged(bool visible);
void setTextEditorFont(const QFont &font, const SearchResultColor &color); void setTextEditorFont(const QFont &font, const SearchResultColor &color);
void setTabWidth(int tabWidth);
void setAutoExpandResults(bool expand); void setAutoExpandResults(bool expand);
void expandAll(); void expandAll();

View File

@@ -101,6 +101,7 @@ namespace Internal {
int m_currentIndex; int m_currentIndex;
QFont m_font; QFont m_font;
SearchResultColor m_color; SearchResultColor m_color;
int m_tabWidth;
public slots: public slots:
void setCurrentIndex(int index); void setCurrentIndex(int index);
@@ -109,7 +110,8 @@ namespace Internal {
}; };
SearchResultWindowPrivate::SearchResultWindowPrivate(SearchResultWindow *window) SearchResultWindowPrivate::SearchResultWindowPrivate(SearchResultWindow *window)
: q(window) : q(window),
m_tabWidth(8)
{ {
} }
@@ -406,6 +408,7 @@ SearchResult *SearchResultWindow::startNewSearch(const QString &label,
connect(widget, SIGNAL(restarted()), d, SLOT(moveWidgetToTop())); connect(widget, SIGNAL(restarted()), d, SLOT(moveWidgetToTop()));
connect(widget, SIGNAL(requestPopup(bool)), d, SLOT(popupRequested(bool))); connect(widget, SIGNAL(requestPopup(bool)), d, SLOT(popupRequested(bool)));
widget->setTextEditorFont(d->m_font, d->m_color); widget->setTextEditorFont(d->m_font, d->m_color);
widget->setTabWidth(d->m_tabWidth);
widget->setSupportPreserveCase(preserveCaseMode == PreserveCaseEnabled); widget->setSupportPreserveCase(preserveCaseMode == PreserveCaseEnabled);
widget->setShowReplaceUI(searchOrSearchAndReplace != SearchOnly); widget->setShowReplaceUI(searchOrSearchAndReplace != SearchOnly);
widget->setAutoExpandResults(d->m_expandCollapseAction->isChecked()); widget->setAutoExpandResults(d->m_expandCollapseAction->isChecked());
@@ -494,6 +497,13 @@ void SearchResultWindow::setTextEditorFont(const QFont &font,
widget->setTextEditorFont(font, color); widget->setTextEditorFont(font, color);
} }
void SearchResultWindow::setTabWidth(int tabWidth)
{
d->m_tabWidth = tabWidth;
foreach (Internal::SearchResultWidget *widget, d->m_searchResultWidgets)
widget->setTabWidth(tabWidth);
}
void SearchResultWindow::openNewSearchPanel() void SearchResultWindow::openNewSearchPanel()
{ {
d->setCurrentIndex(0); d->setCurrentIndex(0);

View File

@@ -169,6 +169,7 @@ public:
const QColor &textBackgroundColor, const QColor &textBackgroundColor,
const QColor &highlightForegroundColor, const QColor &highlightForegroundColor,
const QColor &highlightBackgroundColor); const QColor &highlightBackgroundColor);
void setTabWidth(int width);
void openNewSearchPanel(); void openNewSearchPanel();
// The search result window owns the returned SearchResult // The search result window owns the returned SearchResult

View File

@@ -48,6 +48,10 @@
#include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/externaltoolmanager.h> #include <coreplugin/externaltoolmanager.h>
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
#include <texteditor/icodestylepreferences.h>
#include <texteditor/tabsettings.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/macroexpander.h> #include <utils/macroexpander.h>
@@ -143,6 +147,11 @@ void TextEditorPlugin::extensionsInitialized()
updateSearchResultsFont(m_settings->fontSettings()); updateSearchResultsFont(m_settings->fontSettings());
connect(m_settings->codeStyle(), &ICodeStylePreferences::currentTabSettingsChanged,
this, &TextEditorPlugin::updateSearchResultsTabWidth);
updateSearchResultsTabWidth(m_settings->codeStyle()->currentTabSettings());
addAutoReleasedObject(new FindInFiles); addAutoReleasedObject(new FindInFiles);
addAutoReleasedObject(new FindInCurrentFile); addAutoReleasedObject(new FindInCurrentFile);
addAutoReleasedObject(new FindInOpenFiles); addAutoReleasedObject(new FindInOpenFiles);
@@ -221,6 +230,12 @@ void TextEditorPlugin::updateSearchResultsFont(const FontSettings &settings)
} }
} }
void TextEditorPlugin::updateSearchResultsTabWidth(const TabSettings &tabSettings)
{
if (auto window = SearchResultWindow::instance())
window->setTabWidth(tabSettings.m_tabSize);
}
void TextEditorPlugin::updateCurrentSelection(const QString &text) void TextEditorPlugin::updateCurrentSelection(const QString &text)
{ {
if (BaseTextEditor *editor = BaseTextEditor::currentTextEditor()) { if (BaseTextEditor *editor = BaseTextEditor::currentTextEditor()) {

View File

@@ -36,6 +36,7 @@
namespace TextEditor { namespace TextEditor {
class FontSettings; class FontSettings;
class TabSettings;
class TextEditorSettings; class TextEditorSettings;
namespace Internal { namespace Internal {
@@ -62,6 +63,7 @@ public:
private slots: private slots:
void updateSearchResultsFont(const TextEditor::FontSettings &); void updateSearchResultsFont(const TextEditor::FontSettings &);
void updateSearchResultsTabWidth(const TextEditor::TabSettings &tabSettings);
void updateCurrentSelection(const QString &text); void updateCurrentSelection(const QString &text);
private: private: