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;
SearchResultTreeItemDelegate::SearchResultTreeItemDelegate(QObject *parent)
SearchResultTreeItemDelegate::SearchResultTreeItemDelegate(int tabWidth, QObject *parent)
: QItemDelegate(parent)
{
setTabWidth(tabWidth);
}
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();
}
void SearchResultTreeItemDelegate::setTabWidth(int width)
{
m_tabString = QString(width, QLatin1Char(' '));
}
// returns the width of the line number area
int SearchResultTreeItemDelegate::drawLineNumber(QPainter *painter, const QStyleOptionViewItemV3 &option,
const QRect &rect,
@@ -156,14 +162,18 @@ void SearchResultTreeItemDelegate::drawText(QPainter *painter,
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) {
QItemDelegate::drawDisplay(painter, option, rect, text);
QItemDelegate::drawDisplay(painter, option, rect, text.replace(QLatin1Char('\t'), m_tabString));
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));
const QString textBefore = text.left(searchTermStart).replace(QLatin1Char('\t'), m_tabString);
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
QRect beforeHighlightRect(rect);
@@ -203,19 +213,16 @@ void SearchResultTreeItemDelegate::drawText(QPainter *painter,
noHighlightOpt.textElideMode = Qt::ElideNone;
if (isSelected)
noHighlightOpt.palette.setColor(QPalette::Text, noHighlightOpt.palette.color(cg, QPalette::HighlightedText));
QItemDelegate::drawDisplay(painter, noHighlightOpt,
beforeHighlightRect, text.mid(0, searchTermStart));
QItemDelegate::drawDisplay(painter, noHighlightOpt, beforeHighlightRect, textBefore);
// Highlight text
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));
QItemDelegate::drawDisplay(painter, highlightOpt, resultHighlightRect, textHighlight);
// Text after the Highlight
noHighlightOpt.rect = afterHighlightRect;
QItemDelegate::drawDisplay(painter, noHighlightOpt, afterHighlightRect,
text.mid(searchTermStart + searchTermLength));
QItemDelegate::drawDisplay(painter, noHighlightOpt, afterHighlightRect, textAfter);
}

View File

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

View File

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

View File

@@ -50,6 +50,7 @@ public:
void setAutoExpandResults(bool expand);
void setTextEditorFont(const QFont &font, const SearchResultColor &color);
void setTabWidth(int tabWidth);
SearchResultTreeModel *model() const;
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);
}
void SearchResultWidget::setTabWidth(int tabWidth)
{
m_searchResultTreeView->setTabWidth(tabWidth);
}
void SearchResultWidget::setAutoExpandResults(bool expand)
{
m_searchResultTreeView->setAutoExpandResults(expand);

View File

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

View File

@@ -101,6 +101,7 @@ namespace Internal {
int m_currentIndex;
QFont m_font;
SearchResultColor m_color;
int m_tabWidth;
public slots:
void setCurrentIndex(int index);
@@ -109,7 +110,8 @@ namespace Internal {
};
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(requestPopup(bool)), d, SLOT(popupRequested(bool)));
widget->setTextEditorFont(d->m_font, d->m_color);
widget->setTabWidth(d->m_tabWidth);
widget->setSupportPreserveCase(preserveCaseMode == PreserveCaseEnabled);
widget->setShowReplaceUI(searchOrSearchAndReplace != SearchOnly);
widget->setAutoExpandResults(d->m_expandCollapseAction->isChecked());
@@ -494,6 +497,13 @@ void SearchResultWindow::setTextEditorFont(const QFont &font,
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()
{
d->setCurrentIndex(0);

View File

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

View File

@@ -48,6 +48,10 @@
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/externaltoolmanager.h>
#include <extensionsystem/pluginmanager.h>
#include <texteditor/icodestylepreferences.h>
#include <texteditor/tabsettings.h>
#include <utils/qtcassert.h>
#include <utils/macroexpander.h>
@@ -143,6 +147,11 @@ void TextEditorPlugin::extensionsInitialized()
updateSearchResultsFont(m_settings->fontSettings());
connect(m_settings->codeStyle(), &ICodeStylePreferences::currentTabSettingsChanged,
this, &TextEditorPlugin::updateSearchResultsTabWidth);
updateSearchResultsTabWidth(m_settings->codeStyle()->currentTabSettings());
addAutoReleasedObject(new FindInFiles);
addAutoReleasedObject(new FindInCurrentFile);
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)
{
if (BaseTextEditor *editor = BaseTextEditor::currentTextEditor()) {

View File

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