Editor: Highlight Search results in scrollbar.

Change-Id: I999ddeeedbd6bdd7abebf75405d7e9281a1893df
Reviewed-by: Eike Ziller <eike.ziller@theqtcompany.com>
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
David Schulz
2015-06-04 10:43:34 +02:00
parent 92c902c70c
commit c82d287cbf
13 changed files with 753 additions and 58 deletions

View File

@@ -130,6 +130,9 @@ VcsBase_FileRenamed_TextColor=ffffa500
Bookmarks_TextMarkColor=ff8080ff Bookmarks_TextMarkColor=ff8080ff
TextEditor_SearchResult_ScrollBarColor=ff00c000
TextEditor_CurrentLine_ScrollBarColor=ffffffff
ProjectExplorer_TaskError_TextMarkColor=ffff4040 ProjectExplorer_TaskError_TextMarkColor=ffff4040
ProjectExplorer_TaskWarn_TextMarkColor=ffffff40 ProjectExplorer_TaskWarn_TextMarkColor=ffffff40

View File

@@ -124,6 +124,9 @@ VcsBase_FileRenamed_TextColor=ffd77d00
Bookmarks_TextMarkColor=ffa0a0ff Bookmarks_TextMarkColor=ffa0a0ff
TextEditor_SearchResult_ScrollBarColor=ff00c000
TextEditor_CurrentLine_ScrollBarColor=ff404040
ProjectExplorer_TaskError_TextMarkColor=ffff0000 ProjectExplorer_TaskError_TextMarkColor=ffff0000
ProjectExplorer_TaskWarn_TextMarkColor=ffffa500 ProjectExplorer_TaskWarn_TextMarkColor=ffffa500

View File

@@ -185,6 +185,10 @@ public:
/* Bookmarks Plugin */ /* Bookmarks Plugin */
Bookmarks_TextMarkColor, Bookmarks_TextMarkColor,
/* TextEditor Plugin */
TextEditor_SearchResult_ScrollBarColor,
TextEditor_CurrentLine_ScrollBarColor,
/* Debugger Plugin */ /* Debugger Plugin */
Debugger_Breakpoint_TextMarkColor, Debugger_Breakpoint_TextMarkColor,

View File

@@ -223,6 +223,8 @@ QtcPlugin {
"findtoolwindow.cpp", "findtoolwindow.cpp",
"findtoolwindow.h", "findtoolwindow.h",
"findwidget.ui", "findwidget.ui",
"highlightscrollbar.cpp",
"highlightscrollbar.h",
"ifindfilter.cpp", "ifindfilter.cpp",
"ifindfilter.h", "ifindfilter.h",
"ifindsupport.cpp", "ifindsupport.cpp",

View File

@@ -1,37 +1,40 @@
HEADERS += \ HEADERS += \
$$PWD/findtoolwindow.h \
$$PWD/textfindconstants.h \
$$PWD/ifindsupport.h \
$$PWD/ifindfilter.h \
$$PWD/currentdocumentfind.h \
$$PWD/basetextfind.h \ $$PWD/basetextfind.h \
$$PWD/findtoolbar.h \ $$PWD/currentdocumentfind.h \
$$PWD/findplugin.h \ $$PWD/findplugin.h \
$$PWD/findtoolbar.h \
$$PWD/findtoolwindow.h \
$$PWD/ifindfilter.h \
$$PWD/ifindsupport.h \
$$PWD/itemviewfind.h \
$$PWD/searchresultcolor.h \ $$PWD/searchresultcolor.h \
$$PWD/searchresulttreeitemdelegate.h \ $$PWD/searchresulttreeitemdelegate.h \
$$PWD/searchresulttreeitemroles.h \ $$PWD/searchresulttreeitemroles.h \
$$PWD/searchresulttreeitems.h \ $$PWD/searchresulttreeitems.h \
$$PWD/searchresulttreemodel.h \ $$PWD/searchresulttreemodel.h \
$$PWD/searchresulttreeview.h \ $$PWD/searchresulttreeview.h \
$$PWD/searchresultwindow.h \
$$PWD/searchresultwidget.h \ $$PWD/searchresultwidget.h \
$$PWD/itemviewfind.h $$PWD/searchresultwindow.h \
$$PWD/textfindconstants.h \
$$PWD/highlightscrollbar.h
SOURCES += \ SOURCES += \
$$PWD/findtoolwindow.cpp \
$$PWD/currentdocumentfind.cpp \
$$PWD/basetextfind.cpp \ $$PWD/basetextfind.cpp \
$$PWD/findtoolbar.cpp \ $$PWD/currentdocumentfind.cpp \
$$PWD/findplugin.cpp \ $$PWD/findplugin.cpp \
$$PWD/findtoolbar.cpp \
$$PWD/findtoolwindow.cpp \
$$PWD/ifindfilter.cpp \
$$PWD/ifindsupport.cpp \
$$PWD/itemviewfind.cpp \
$$PWD/searchresulttreeitemdelegate.cpp \ $$PWD/searchresulttreeitemdelegate.cpp \
$$PWD/searchresulttreeitems.cpp \ $$PWD/searchresulttreeitems.cpp \
$$PWD/searchresulttreemodel.cpp \ $$PWD/searchresulttreemodel.cpp \
$$PWD/searchresulttreeview.cpp \ $$PWD/searchresulttreeview.cpp \
$$PWD/searchresultwindow.cpp \
$$PWD/ifindfilter.cpp \
$$PWD/ifindsupport.cpp \
$$PWD/searchresultwidget.cpp \ $$PWD/searchresultwidget.cpp \
$$PWD/itemviewfind.cpp $$PWD/searchresultwindow.cpp \
$$PWD/highlightscrollbar.cpp
FORMS += \ FORMS += \
$$PWD/findwidget.ui \ $$PWD/findwidget.ui \

View File

@@ -0,0 +1,299 @@
/****************************************************************************
**
** Copyright (C) 2015 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://www.qt.io/licensing. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "highlightscrollbar.h"
#include <QPainter>
#include <QResizeEvent>
#include <QStyle>
#include <QStyleOptionSlider>
#include <QTimer>
using namespace Core;
using namespace Utils;
HighlightScrollBar::HighlightScrollBar(Qt::Orientation orientation, QWidget *parent)
: QScrollBar(orientation, parent)
, m_widget(parent)
, m_overlay(new HighlightScrollBarOverlay(this))
{
connect(m_overlay, &HighlightScrollBarOverlay::destroyed,
this, &HighlightScrollBar::overlayDestroyed);
// valueChanged(0) flashes transient scroll bars, which is needed
// for a correct initialization.
emit valueChanged(0);
}
HighlightScrollBar::~HighlightScrollBar()
{
if (!m_overlay || m_overlay->parent() == this)
return;
delete m_overlay;
}
void HighlightScrollBar::setVisibleRange(float visibleRange)
{
if (!m_overlay)
return;
m_overlay->m_visibleRange = visibleRange;
}
void HighlightScrollBar::setRangeOffset(float offset)
{
if (!m_overlay)
return;
m_overlay->m_offset = offset;
}
void HighlightScrollBar::setColor(Id category, Theme::Color color)
{
if (!m_overlay)
return;
m_overlay->m_colors[category] = color;
}
QRect HighlightScrollBar::overlayRect()
{
QStyleOptionSlider opt;
initStyleOption(&opt);
return style()->subControlRect(QStyle::CC_ScrollBar, &opt, QStyle::SC_ScrollBarGroove, this);
}
void HighlightScrollBar::overlayDestroyed()
{
m_overlay = 0;
}
void HighlightScrollBar::setPriority(Id category, HighlightScrollBar::Priority prio)
{
if (!m_overlay)
return;
m_overlay->m_priorities[category] = prio;
m_overlay->scheduleUpdate();
}
void HighlightScrollBar::addHighlights(Id category, QSet<int> highlights)
{
if (!m_overlay)
return;
m_overlay->m_highlights[category].unite(highlights);
m_overlay->scheduleUpdate();
}
void HighlightScrollBar::addHighlight(Id category, int highlight)
{
if (!m_overlay)
return;
m_overlay->m_highlights[category] << highlight;
m_overlay->scheduleUpdate();
}
void HighlightScrollBar::removeHighlights(Id category)
{
if (!m_overlay)
return;
m_overlay->m_highlights.remove(category);
m_overlay->scheduleUpdate();
}
void HighlightScrollBar::removeAllHighlights()
{
if (!m_overlay)
return;
m_overlay->m_highlights.clear();
m_overlay->scheduleUpdate();
}
bool HighlightScrollBar::eventFilter(QObject *obj, QEvent *event)
{
if (obj == m_widget && m_overlay && m_widget == m_overlay->parent() &&
(event->type() == QEvent::Resize || event->type() == QEvent::Move)) {
QStyleOptionSlider opt;
initStyleOption(&opt);
const int width = style()->pixelMetric(QStyle::PM_ScrollBarExtent, &opt, this);
m_overlay->move(m_widget->width() - width, 0);
resize(width, m_widget->height());
}
return false;
}
void HighlightScrollBar::resizeEvent(QResizeEvent *event)
{
if (!m_overlay)
return;
QScrollBar::resizeEvent(event);
m_overlay->resize(size());
}
void HighlightScrollBar::moveEvent(QMoveEvent *event)
{
if (!m_overlay)
return;
QScrollBar::moveEvent(event);
m_overlay->adjustPosition();
}
void HighlightScrollBar::showEvent(QShowEvent *event)
{
if (!m_overlay)
return;
QScrollBar::showEvent(event);
if (parentWidget() != this) {
m_widget->removeEventFilter(this);
m_overlay->setParent(this);
m_overlay->adjustPosition();
m_overlay->show();
}
}
void HighlightScrollBar::hideEvent(QHideEvent *event)
{
if (!m_overlay)
return;
QScrollBar::hideEvent(event);
if (parentWidget() != m_widget) {
m_widget->installEventFilter(this);
m_overlay->setParent(m_widget);
m_overlay->adjustPosition();
m_overlay->show();
}
}
void HighlightScrollBar::changeEvent(QEvent *event)
{
// Workaround for QTBUG-45579
if (event->type() == QEvent::ParentChange)
setStyle(style());
}
void HighlightScrollBarOverlay::scheduleUpdate()
{
if (m_cacheUpdateScheduled)
return;
m_cacheUpdateScheduled = true;
QTimer::singleShot(0, this, static_cast<void (QWidget::*)()>(&QWidget::update));
}
void HighlightScrollBarOverlay::updateCache()
{
if (!m_cacheUpdateScheduled)
return;
m_cache.clear();
foreach (const Id &category, m_highlights.keys()) {
foreach (const int &highlight, m_highlights[category]) {
Id highlightCategory = m_cache[highlight];
if (highlightCategory.isValid() && (m_priorities[highlightCategory] >= m_priorities[category]))
continue;
m_cache[highlight] = category;
}
}
m_cacheUpdateScheduled = false;
}
void HighlightScrollBarOverlay::adjustPosition()
{
move(parentWidget()->mapFromGlobal(m_scrollBar->mapToGlobal(m_scrollBar->pos())));
}
void HighlightScrollBarOverlay::paintEvent(QPaintEvent *paintEvent)
{
QWidget::paintEvent(paintEvent);
updateCache();
if (m_cache.isEmpty())
return;
const QRect &rect = m_scrollBar->overlayRect();
Id previousCategory;
QRect *previousRect = 0;
const int scrollbarRange = m_scrollBar->maximum() + m_scrollBar->pageStep();
const int range = qMax(m_visibleRange, float(scrollbarRange));
const int horizontalMargin = 3;
const int resultWidth = rect.width() - 2 * horizontalMargin + 1;
const int resultHeight = qMin(int(rect.height() / range) + 1, 4);
const int offset = rect.height() / range * m_offset;
const int verticalMargin = ((rect.height() / range) - resultHeight) / 2;
int previousBottom = -1;
QHash<Id, QVector<QRect> > highlights;
QMapIterator<int, Id> it(m_cache);
while (it.hasNext()) {
const Id currentCategory = it.next().value();
// Calculate start and end
int top = rect.top() + offset + verticalMargin + float(it.key()) / range * rect.height();
const int bottom = top + resultHeight;
if (previousCategory == currentCategory && previousBottom + 1 >= top) {
// If the previous highlight has the same category and is directly prior to this highlight
// we just extend the previous highlight.
previousRect->setBottom(bottom - 1);
} else { // create a new highlight
if (previousRect && previousBottom >= top) {
// make sure that highlights with higher priority are drawn on top of other highlights
// when rectangles are overlapping
if (m_priorities[previousCategory] > m_priorities[currentCategory]) {
// Moving the top of the current highlight when the previous
// highlight has a higher priority
top = previousBottom + 1;
if (top == bottom) // if this would result in an empty highlight just skip
continue;
} else {
previousRect->setBottom(top - 1); // move the end of the last highlight
if (previousRect->height() == 0) // if the result is an empty rect, remove it.
highlights[previousCategory].removeLast();
}
}
highlights[currentCategory] << QRect(rect.left() + horizontalMargin, top,
resultWidth, bottom - top);
previousRect = &highlights[currentCategory].last();
previousCategory = currentCategory;
}
previousBottom = previousRect->bottom();
}
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, false);
foreach (Id category, highlights.keys()) {
const QColor &color = creatorTheme()->color(m_colors[category]);
for (int i = 0, total = highlights[category].size(); i < total; ++i) {
const QRect rect = highlights[category][i];
painter.fillRect(rect, color);
}
}
}

View File

@@ -0,0 +1,125 @@
/****************************************************************************
**
** Copyright (C) 2015 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://www.qt.io/licensing. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef HIGHLIGHTSCROLLBAR_H
#define HIGHLIGHTSCROLLBAR_H
#include <QMap>
#include <QScrollBar>
#include <QSet>
#include <coreplugin/core_global.h>
#include <coreplugin/id.h>
#include <utils/theme/theme.h>
namespace Core {
class HighlightScrollBarOverlay;
class CORE_EXPORT HighlightScrollBar : public QScrollBar
{
Q_OBJECT
public:
HighlightScrollBar(Qt::Orientation orientation, QWidget *parent = 0);
~HighlightScrollBar() override;
void setVisibleRange(float visibleRange);
void setRangeOffset(float offset);
void setColor(Id category, Utils::Theme::Color color);
enum Priority
{
LowPriority = 0,
NormalPriority = 1,
HighPriority = 2,
HighestPriority = 3
};
void setPriority(Id category, Priority prio);
void addHighlight(Id category, int highlight);
void addHighlights(Id category, QSet<int> highlights);
void removeHighlights(Id id);
void removeAllHighlights();
bool eventFilter(QObject *, QEvent *event) override;
protected:
void moveEvent(QMoveEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
void showEvent(QShowEvent *event) override;
void hideEvent(QHideEvent *event) override;
void changeEvent(QEvent *even) override;
private:
QRect overlayRect();
void overlayDestroyed();
QWidget *m_widget;
HighlightScrollBarOverlay *m_overlay;
friend class HighlightScrollBarOverlay;
};
class HighlightScrollBarOverlay : public QWidget
{
Q_OBJECT
public:
HighlightScrollBarOverlay(HighlightScrollBar *scrollBar)
: QWidget(scrollBar)
, m_visibleRange(0.0)
, m_offset(0.0)
, m_cacheUpdateScheduled(false)
, m_scrollBar(scrollBar)
{}
void scheduleUpdate();
void updateCache();
void adjustPosition();
float m_visibleRange;
float m_offset;
QHash<Id, QSet<int> > m_highlights;
QHash<Id, Utils::Theme::Color> m_colors;
QHash<Id, HighlightScrollBar::Priority> m_priorities;
bool m_cacheUpdateScheduled;
QMap<int, Id> m_cache;
protected:
void paintEvent(QPaintEvent *paintEvent) override;
private:
HighlightScrollBar *m_scrollBar;
};
} // namespace Core
#endif // HIGHLIGHTSCROLLBAR_H

View File

@@ -46,6 +46,7 @@ static const char autoFoldFirstCommentKey[] = "AutoFoldFirstComment";
static const char centerCursorOnScrollKey[] = "CenterCursorOnScroll"; static const char centerCursorOnScrollKey[] = "CenterCursorOnScroll";
static const char openLinksInNextSplitKey[] = "OpenLinksInNextSplitKey"; static const char openLinksInNextSplitKey[] = "OpenLinksInNextSplitKey";
static const char displayFileEncodingKey[] = "DisplayFileEncoding"; static const char displayFileEncodingKey[] = "DisplayFileEncoding";
static const char scrollBarHighlightsKey[] = "ScrollBarHighlights";
static const char groupPostfix[] = "DisplaySettings"; static const char groupPostfix[] = "DisplaySettings";
namespace TextEditor { namespace TextEditor {
@@ -64,7 +65,8 @@ DisplaySettings::DisplaySettings() :
m_centerCursorOnScroll(false), m_centerCursorOnScroll(false),
m_openLinksInNextSplit(false), m_openLinksInNextSplit(false),
m_forceOpenLinksInNextSplit(false), m_forceOpenLinksInNextSplit(false),
m_displayFileEncoding(false) m_displayFileEncoding(false),
m_scrollBarHighlights(true)
{ {
} }
@@ -87,6 +89,7 @@ void DisplaySettings::toSettings(const QString &category, QSettings *s) const
s->setValue(QLatin1String(centerCursorOnScrollKey), m_centerCursorOnScroll); s->setValue(QLatin1String(centerCursorOnScrollKey), m_centerCursorOnScroll);
s->setValue(QLatin1String(openLinksInNextSplitKey), m_openLinksInNextSplit); s->setValue(QLatin1String(openLinksInNextSplitKey), m_openLinksInNextSplit);
s->setValue(QLatin1String(displayFileEncodingKey), m_displayFileEncoding); s->setValue(QLatin1String(displayFileEncodingKey), m_displayFileEncoding);
s->setValue(QLatin1String(scrollBarHighlightsKey), m_scrollBarHighlights);
s->endGroup(); s->endGroup();
} }
@@ -112,6 +115,7 @@ void DisplaySettings::fromSettings(const QString &category, const QSettings *s)
m_centerCursorOnScroll = s->value(group + QLatin1String(centerCursorOnScrollKey), m_centerCursorOnScroll).toBool(); m_centerCursorOnScroll = s->value(group + QLatin1String(centerCursorOnScrollKey), m_centerCursorOnScroll).toBool();
m_openLinksInNextSplit = s->value(group + QLatin1String(openLinksInNextSplitKey), m_openLinksInNextSplit).toBool(); m_openLinksInNextSplit = s->value(group + QLatin1String(openLinksInNextSplitKey), m_openLinksInNextSplit).toBool();
m_displayFileEncoding = s->value(group + QLatin1String(displayFileEncodingKey), m_displayFileEncoding).toBool(); m_displayFileEncoding = s->value(group + QLatin1String(displayFileEncodingKey), m_displayFileEncoding).toBool();
m_scrollBarHighlights = s->value(group + QLatin1String(scrollBarHighlightsKey), m_scrollBarHighlights).toBool();
} }
bool DisplaySettings::equals(const DisplaySettings &ds) const bool DisplaySettings::equals(const DisplaySettings &ds) const
@@ -130,6 +134,7 @@ bool DisplaySettings::equals(const DisplaySettings &ds) const
&& m_openLinksInNextSplit == ds.m_openLinksInNextSplit && m_openLinksInNextSplit == ds.m_openLinksInNextSplit
&& m_forceOpenLinksInNextSplit == ds.m_forceOpenLinksInNextSplit && m_forceOpenLinksInNextSplit == ds.m_forceOpenLinksInNextSplit
&& m_displayFileEncoding == ds.m_displayFileEncoding && m_displayFileEncoding == ds.m_displayFileEncoding
&& m_scrollBarHighlights == ds.m_scrollBarHighlights
; ;
} }

View File

@@ -61,6 +61,7 @@ public:
bool m_openLinksInNextSplit; bool m_openLinksInNextSplit;
bool m_forceOpenLinksInNextSplit; bool m_forceOpenLinksInNextSplit;
bool m_displayFileEncoding; bool m_displayFileEncoding;
bool m_scrollBarHighlights;
bool equals(const DisplaySettings &ds) const; bool equals(const DisplaySettings &ds) const;
}; };

View File

@@ -122,6 +122,7 @@ void DisplaySettingsPage::settingsFromUI(DisplaySettings &displaySettings,
displaySettings.m_centerCursorOnScroll = d->m_page->centerOnScroll->isChecked(); displaySettings.m_centerCursorOnScroll = d->m_page->centerOnScroll->isChecked();
displaySettings.m_openLinksInNextSplit = d->m_page->openLinksInNextSplit->isChecked(); displaySettings.m_openLinksInNextSplit = d->m_page->openLinksInNextSplit->isChecked();
displaySettings.m_displayFileEncoding = d->m_page->displayFileEncoding->isChecked(); displaySettings.m_displayFileEncoding = d->m_page->displayFileEncoding->isChecked();
displaySettings.m_scrollBarHighlights = d->m_page->scrollBarHighlights->isChecked();
} }
void DisplaySettingsPage::settingsToUI() void DisplaySettingsPage::settingsToUI()
@@ -143,6 +144,7 @@ void DisplaySettingsPage::settingsToUI()
d->m_page->centerOnScroll->setChecked(displaySettings.m_centerCursorOnScroll); d->m_page->centerOnScroll->setChecked(displaySettings.m_centerCursorOnScroll);
d->m_page->openLinksInNextSplit->setChecked(displaySettings.m_openLinksInNextSplit); d->m_page->openLinksInNextSplit->setChecked(displaySettings.m_openLinksInNextSplit);
d->m_page->displayFileEncoding->setChecked(displaySettings.m_displayFileEncoding); d->m_page->displayFileEncoding->setChecked(displaySettings.m_displayFileEncoding);
d->m_page->scrollBarHighlights->setChecked(displaySettings.m_scrollBarHighlights);
} }
const DisplaySettings &DisplaySettingsPage::displaySettings() const const DisplaySettings &DisplaySettingsPage::displaySettings() const

View File

@@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>501</width> <width>501</width>
<height>323</height> <height>331</height>
</rect> </rect>
</property> </property>
<layout class="QGridLayout" name="gridLayout_3"> <layout class="QGridLayout" name="gridLayout_3">
@@ -80,34 +80,10 @@
<string>Display</string> <string>Display</string>
</property> </property>
<layout class="QGridLayout" name="gridLayout_2"> <layout class="QGridLayout" name="gridLayout_2">
<item row="4" column="1"> <item row="1" column="0">
<widget class="QCheckBox" name="highlightMatchingParentheses"> <widget class="QCheckBox" name="displayFoldingMarkers">
<property name="text"> <property name="text">
<string>&amp;Highlight matching parentheses</string> <string>Display &amp;folding markers</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QCheckBox" name="markTextChanges">
<property name="text">
<string>Mark &amp;text changes</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QCheckBox" name="visualizeWhitespace">
<property name="toolTip">
<string>Shows tabs and spaces.</string>
</property>
<property name="text">
<string>&amp;Visualize whitespace</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="highlightBlocks">
<property name="text">
<string>Highlight &amp;blocks</string>
</property> </property>
</widget> </widget>
</item> </item>
@@ -118,17 +94,17 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="0"> <item row="1" column="1">
<widget class="QCheckBox" name="displayFoldingMarkers"> <widget class="QCheckBox" name="highlightBlocks">
<property name="text"> <property name="text">
<string>Display &amp;folding markers</string> <string>Highlight &amp;blocks</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="6" column="0">
<widget class="QCheckBox" name="highlightCurrentLine"> <widget class="QCheckBox" name="autoFoldFirstComment">
<property name="text"> <property name="text">
<string>Highlight current &amp;line</string> <string>Auto-fold first &amp;comment</string>
</property> </property>
</widget> </widget>
</item> </item>
@@ -139,6 +115,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1">
<widget class="QCheckBox" name="highlightCurrentLine">
<property name="text">
<string>Highlight current &amp;line</string>
</property>
</widget>
</item>
<item row="0" column="0"> <item row="0" column="0">
<widget class="QCheckBox" name="displayLineNumbers"> <widget class="QCheckBox" name="displayLineNumbers">
<property name="text"> <property name="text">
@@ -146,13 +129,6 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="6" column="0">
<widget class="QCheckBox" name="autoFoldFirstComment">
<property name="text">
<string>Auto-fold first &amp;comment</string>
</property>
</widget>
</item>
<item row="5" column="1"> <item row="5" column="1">
<widget class="QCheckBox" name="openLinksInNextSplit"> <widget class="QCheckBox" name="openLinksInNextSplit">
<property name="text"> <property name="text">
@@ -167,6 +143,37 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="4" column="0">
<widget class="QCheckBox" name="visualizeWhitespace">
<property name="toolTip">
<string>Shows tabs and spaces.</string>
</property>
<property name="text">
<string>&amp;Visualize whitespace</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QCheckBox" name="highlightMatchingParentheses">
<property name="text">
<string>&amp;Highlight matching parentheses</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QCheckBox" name="markTextChanges">
<property name="text">
<string>Mark &amp;text changes</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QCheckBox" name="scrollBarHighlights">
<property name="text">
<string>Highlight search results on the scrollbar</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>

View File

@@ -77,15 +77,18 @@
#include <coreplugin/infobar.h> #include <coreplugin/infobar.h>
#include <coreplugin/manhattanstyle.h> #include <coreplugin/manhattanstyle.h>
#include <coreplugin/find/basetextfind.h> #include <coreplugin/find/basetextfind.h>
#include <coreplugin/find/highlightscrollbar.h>
#include <utils/linecolumnlabel.h> #include <utils/linecolumnlabel.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/dropsupport.h> #include <utils/dropsupport.h>
#include <utils/filesearch.h>
#include <utils/hostosinfo.h> #include <utils/hostosinfo.h>
#include <utils/mimetypes/mimedatabase.h> #include <utils/mimetypes/mimedatabase.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/stylehelper.h> #include <utils/stylehelper.h>
#include <utils/tooltip/tooltip.h> #include <utils/tooltip/tooltip.h>
#include <utils/uncommentselection.h> #include <utils/uncommentselection.h>
#include <utils/theme/theme.h>
#include <QAbstractTextDocumentLayout> #include <QAbstractTextDocumentLayout>
#include <QApplication> #include <QApplication>
@@ -271,6 +274,7 @@ public:
void copyLineUpDown(bool up); void copyLineUpDown(bool up);
void saveCurrentCursorPositionForNavigation(); void saveCurrentCursorPositionForNavigation();
void updateHighlights(); void updateHighlights();
void updateCurrentLineInScrollbar();
void updateCurrentLineHighlight(); void updateCurrentLineHighlight();
void drawFoldingMarker(QPainter *painter, const QPalette &pal, void drawFoldingMarker(QPainter *painter, const QPalette &pal,
@@ -309,6 +313,19 @@ public:
void documentAboutToBeReloaded(); void documentAboutToBeReloaded();
void documentReloadFinished(bool success); void documentReloadFinished(bool success);
void highlightSearchResultsSlot(const QString &txt, FindFlags findFlags); void highlightSearchResultsSlot(const QString &txt, FindFlags findFlags);
void searchResultsReady(int beginIndex, int endIndex);
void searchFinished();
void setupScrollBar();
void highlightSearchResultsInScrollBar();
void scheduleUpdateHighlightScrollBar();
void updateHighlightScrollBarNow();
struct SearchResult {
int start;
int length;
};
void addSearchResultsToScrollBar(QVector<SearchResult> results);
void adjustScrollBarRanges();
void setFindScope(const QTextCursor &start, const QTextCursor &end, int, int); void setFindScope(const QTextCursor &start, const QTextCursor &end, int, int);
void updateCursorPosition(); void updateCursorPosition();
@@ -448,6 +465,12 @@ public:
QScopedPointer<AutoCompleter> m_autoCompleter; QScopedPointer<AutoCompleter> m_autoCompleter;
CommentDefinition m_commentDefinition; CommentDefinition m_commentDefinition;
QFutureWatcher<FileSearchResultList> *m_searchWatcher;
QVector<SearchResult> m_searchResults;
QTimer m_scrollBarUpdateTimer;
HighlightScrollBar *m_highlightScrollBar;
bool m_scrollBarUpdateScheduled;
}; };
TextEditorWidgetPrivate::TextEditorWidgetPrivate(TextEditorWidget *parent) TextEditorWidgetPrivate::TextEditorWidgetPrivate(TextEditorWidget *parent)
@@ -499,7 +522,11 @@ TextEditorWidgetPrivate::TextEditorWidgetPrivate(TextEditorWidget *parent)
m_markDragging(false), m_markDragging(false),
m_clipboardAssistProvider(new ClipboardAssistProvider), m_clipboardAssistProvider(new ClipboardAssistProvider),
m_isMissingSyntaxDefinition(false), m_isMissingSyntaxDefinition(false),
m_autoCompleter(new AutoCompleter) m_autoCompleter(new AutoCompleter),
m_searchWatcher(0),
m_scrollBarUpdateTimer(0),
m_highlightScrollBar(0),
m_scrollBarUpdateScheduled(false)
{ {
Aggregation::Aggregate *aggregate = new Aggregation::Aggregate; Aggregation::Aggregate *aggregate = new Aggregation::Aggregate;
BaseTextFind *baseTextFind = new BaseTextFind(q); BaseTextFind *baseTextFind = new BaseTextFind(q);
@@ -606,6 +633,29 @@ void TextEditorWidget::setTextDocument(const QSharedPointer<TextDocument> &doc)
d->ctor(doc); d->ctor(doc);
} }
void TextEditorWidgetPrivate::setupScrollBar()
{
if (m_displaySettings.m_scrollBarHighlights) {
if (m_highlightScrollBar)
return;
m_highlightScrollBar = new HighlightScrollBar(Qt::Vertical, q);
m_highlightScrollBar->setColor(Constants::SCROLL_BAR_SEARCH_RESULT,
Theme::TextEditor_SearchResult_ScrollBarColor);
m_highlightScrollBar->setColor(Constants::SCROLL_BAR_CURRENT_LINE,
Theme::TextEditor_CurrentLine_ScrollBarColor);
m_highlightScrollBar->setPriority(
Constants::SCROLL_BAR_SEARCH_RESULT, HighlightScrollBar::HighPriority);
m_highlightScrollBar->setPriority(
Constants::SCROLL_BAR_CURRENT_LINE, HighlightScrollBar::HighestPriority);
q->setVerticalScrollBar(m_highlightScrollBar);
highlightSearchResultsInScrollBar();
scheduleUpdateHighlightScrollBar();
} else if (m_highlightScrollBar) {
q->setVerticalScrollBar(new QScrollBar(Qt::Vertical, q));
m_highlightScrollBar = 0;
}
}
void TextEditorWidgetPrivate::ctor(const QSharedPointer<TextDocument> &doc) void TextEditorWidgetPrivate::ctor(const QSharedPointer<TextDocument> &doc)
{ {
q->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); q->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
@@ -668,6 +718,10 @@ void TextEditorWidgetPrivate::ctor(const QSharedPointer<TextDocument> &doc)
QObject::connect(&m_highlightBlocksTimer, &QTimer::timeout, QObject::connect(&m_highlightBlocksTimer, &QTimer::timeout,
this, &TextEditorWidgetPrivate::_q_highlightBlocks); this, &TextEditorWidgetPrivate::_q_highlightBlocks);
m_scrollBarUpdateTimer.setSingleShot(true);
QObject::connect(&m_scrollBarUpdateTimer, &QTimer::timeout,
this, &TextEditorWidgetPrivate::highlightSearchResultsInScrollBar);
m_animator = 0; m_animator = 0;
slotUpdateExtraAreaWidth(); slotUpdateExtraAreaWidth();
@@ -1073,6 +1127,7 @@ void TextEditorWidgetPrivate::editorContentsChange(int position, int charsRemove
q->verticalScrollBar()->setValue(q->verticalScrollBar()->value() + newBlockCount - m_blockCount); q->verticalScrollBar()->setValue(q->verticalScrollBar()->value() + newBlockCount - m_blockCount);
} }
m_blockCount = newBlockCount; m_blockCount = newBlockCount;
m_scrollBarUpdateTimer.start(500);
} }
void TextEditorWidgetPrivate::slotSelectionChanged() void TextEditorWidgetPrivate::slotSelectionChanged()
@@ -2568,6 +2623,9 @@ void TextEditorWidgetPrivate::documentAboutToBeReloaded()
m_snippetOverlay->clear(); m_snippetOverlay->clear();
m_searchResultOverlay->clear(); m_searchResultOverlay->clear();
m_refactorOverlay->clear(); m_refactorOverlay->clear();
// clear search results
m_searchResults.clear();
} }
void TextEditorWidgetPrivate::documentReloadFinished(bool success) void TextEditorWidgetPrivate::documentReloadFinished(bool success)
@@ -2873,6 +2931,15 @@ void TextEditorWidgetPrivate::setupDocumentSignals()
QObject::connect(q, &TextEditorWidget::requestBlockUpdate, QObject::connect(q, &TextEditorWidget::requestBlockUpdate,
documentLayout, &QPlainTextDocumentLayout::updateBlock); documentLayout, &QPlainTextDocumentLayout::updateBlock);
QObject::connect(documentLayout, &TextDocumentLayout::updateExtraArea,
this, &TextEditorWidgetPrivate::scheduleUpdateHighlightScrollBar);
QObject::connect(documentLayout, &QAbstractTextDocumentLayout::documentSizeChanged,
this, &TextEditorWidgetPrivate::scheduleUpdateHighlightScrollBar);
QObject::connect(documentLayout, &QAbstractTextDocumentLayout::update,
this, &TextEditorWidgetPrivate::scheduleUpdateHighlightScrollBar);
QObject::connect(doc, &QTextDocument::contentsChange, QObject::connect(doc, &QTextDocument::contentsChange,
this, &TextEditorWidgetPrivate::editorContentsChange); this, &TextEditorWidgetPrivate::editorContentsChange);
@@ -3050,6 +3117,8 @@ void TextEditorWidget::resizeEvent(QResizeEvent *e)
d->m_extraArea->setGeometry( d->m_extraArea->setGeometry(
QStyle::visualRect(layoutDirection(), cr, QStyle::visualRect(layoutDirection(), cr,
QRect(cr.left(), cr.top(), extraAreaWidth(), cr.height()))); QRect(cr.left(), cr.top(), extraAreaWidth(), cr.height())));
d->adjustScrollBarRanges();
d->updateCurrentLineInScrollbar();
} }
QRect TextEditorWidgetPrivate::foldBox() QRect TextEditorWidgetPrivate::foldBox()
@@ -4584,6 +4653,7 @@ void TextEditorWidgetPrivate::updateCurrentLineHighlight()
sel.cursor.clearSelection(); sel.cursor.clearSelection();
extraSelections.append(sel); extraSelections.append(sel);
} }
updateCurrentLineInScrollbar();
q->setExtraSelections(TextEditorWidget::CurrentLineSelection, extraSelections); q->setExtraSelections(TextEditorWidget::CurrentLineSelection, extraSelections);
@@ -4649,6 +4719,20 @@ void TextEditorWidgetPrivate::updateHighlights()
} }
} }
void TextEditorWidgetPrivate::updateCurrentLineInScrollbar()
{
if (m_highlightCurrentLine && m_highlightScrollBar) {
m_highlightScrollBar->removeHighlights(Constants::SCROLL_BAR_CURRENT_LINE);
if (m_highlightScrollBar->maximum() > 0) {
const QTextCursor &tc = q->textCursor();
const int lineNumberInBlock =
tc.block().layout()->lineForTextPosition(tc.positionInBlock()).lineNumber();
m_highlightScrollBar->addHighlight(Constants::SCROLL_BAR_CURRENT_LINE,
q->textCursor().block().firstLineNumber() + lineNumberInBlock);
}
}
}
void TextEditorWidgetPrivate::slotUpdateBlockNotify(const QTextBlock &block) void TextEditorWidgetPrivate::slotUpdateBlockNotify(const QTextBlock &block)
{ {
static bool blockRecursion = false; static bool blockRecursion = false;
@@ -5466,6 +5550,158 @@ void TextEditorWidgetPrivate::highlightSearchResultsSlot(const QString &txt, Fin
m_findFlags = findFlags; m_findFlags = findFlags;
m_delayedUpdateTimer.start(50); m_delayedUpdateTimer.start(50);
if (m_highlightScrollBar)
m_scrollBarUpdateTimer.start(50);
}
void TextEditorWidgetPrivate::searchResultsReady(int beginIndex, int endIndex)
{
QVector<SearchResult> results;
for (int index = beginIndex; index < endIndex; ++index) {
foreach (Utils::FileSearchResult result, m_searchWatcher->resultAt(index)) {
const QTextBlock &block = q->document()->findBlockByNumber(result.lineNumber - 1);
const int matchStart = block.position() + result.matchStart;
if (!q->inFindScope(matchStart, matchStart + result.matchLength))
continue;
results << SearchResult{matchStart, result.matchLength};
}
}
m_searchResults << results;
addSearchResultsToScrollBar(results);
}
void TextEditorWidgetPrivate::searchFinished()
{
delete m_searchWatcher;
m_searchWatcher = 0;
}
void TextEditorWidgetPrivate::adjustScrollBarRanges()
{
if (!m_highlightScrollBar)
return;
const float lineSpacing = QFontMetricsF(q->font()).lineSpacing();
if (lineSpacing == 0)
return;
const float offset = q->contentOffset().y();
m_highlightScrollBar->setVisibleRange((q->viewport()->rect().height() - offset) / lineSpacing);
m_highlightScrollBar->setRangeOffset(offset / lineSpacing);
}
void TextEditorWidgetPrivate::highlightSearchResultsInScrollBar()
{
if (!m_highlightScrollBar)
return;
m_highlightScrollBar->removeHighlights(Constants::SCROLL_BAR_SEARCH_RESULT);
m_searchResults.clear();
if (m_searchWatcher) {
m_searchWatcher->disconnect();
m_searchWatcher->cancel();
m_searchWatcher->deleteLater();
m_searchWatcher = 0;
}
const QString &txt = m_searchExpr.pattern();
if (txt.isEmpty())
return;
adjustScrollBarRanges();
m_searchWatcher = new QFutureWatcher<FileSearchResultList>();
connect(m_searchWatcher, &QFutureWatcher<Utils::FileSearchResultList>::resultsReadyAt,
this, &TextEditorWidgetPrivate::searchResultsReady);
connect(m_searchWatcher, &QFutureWatcher<Utils::FileSearchResultList>::finished,
this, &TextEditorWidgetPrivate::searchFinished);
m_searchWatcher->setPendingResultsLimit(10);
const QTextDocument::FindFlags findFlags = textDocumentFlagsForFindFlags(m_findFlags);
const QString &fileName = m_document->filePath().toString();
FileListIterator *it =
new FileListIterator( { fileName } , { const_cast<QTextCodec *>(m_document->codec()) } );
QMap<QString, QString> fileToContentsMap;
fileToContentsMap[fileName] = m_document->plainText();
if (m_findFlags & FindRegularExpression)
m_searchWatcher->setFuture(findInFilesRegExp(txt, it, findFlags, fileToContentsMap));
else
m_searchWatcher->setFuture(findInFiles(txt, it, findFlags, fileToContentsMap));
}
void TextEditorWidgetPrivate::scheduleUpdateHighlightScrollBar()
{
if (m_scrollBarUpdateScheduled)
return;
m_scrollBarUpdateScheduled = true;
QTimer::singleShot(0, this, &TextEditorWidgetPrivate::updateHighlightScrollBarNow);
}
HighlightScrollBar::Priority textMarkPrioToScrollBarPrio(const TextMark::Priority &prio)
{
switch (prio) {
case TextMark::LowPriority:
return HighlightScrollBar::LowPriority;
case TextMark::NormalPriority:
return HighlightScrollBar::NormalPriority;
case TextMark::HighPriority:
return HighlightScrollBar::HighPriority;
default:
return HighlightScrollBar::NormalPriority;
}
}
void TextEditorWidgetPrivate::addSearchResultsToScrollBar(QVector<SearchResult> results)
{
QSet<int> searchResults;
foreach (SearchResult result, results) {
const QTextBlock &block = q->document()->findBlock(result.start);
if (block.isValid() && block.isVisible()) {
const int firstLine = block.layout()->lineForTextPosition(result.start - block.position()).lineNumber();
const int lastLine = block.layout()->lineForTextPosition(result.start - block.position() + result.length).lineNumber();
for (int line = firstLine; line <= lastLine; ++line)
searchResults << block.firstLineNumber() + line;
}
}
if (m_highlightScrollBar)
m_highlightScrollBar->addHighlights(Constants::SCROLL_BAR_SEARCH_RESULT, searchResults);
}
void TextEditorWidgetPrivate::updateHighlightScrollBarNow()
{
typedef QSet<int> IntSet;
m_scrollBarUpdateScheduled = false;
if (!m_highlightScrollBar)
return;
m_highlightScrollBar->removeAllHighlights();
updateCurrentLineInScrollbar();
// update search results
addSearchResultsToScrollBar(m_searchResults);
// update text marks
QHash<Id, IntSet> marks;
foreach (TextMark *mark, m_document->marks()) {
Id category = mark->category();
if (!mark->isVisible() || !TextMark::categoryHasColor(category))
continue;
m_highlightScrollBar->setPriority(category, textMarkPrioToScrollBarPrio(mark->priority()));
const QTextBlock &block = q->document()->findBlockByNumber(mark->lineNumber() - 1);
if (block.isVisible())
marks[category] << block.firstLineNumber();
}
QHashIterator<Id, IntSet> it(marks);
while (it.hasNext()) {
it.next();
m_highlightScrollBar->setColor(it.key(), TextMark::categoryColor(it.key()));
m_highlightScrollBar->addHighlights(it.key(), it.value());
}
} }
int TextEditorWidget::verticalBlockSelectionFirstColumn() const int TextEditorWidget::verticalBlockSelectionFirstColumn() const
@@ -5510,6 +5746,7 @@ void TextEditorWidgetPrivate::setFindScope(const QTextCursor &start, const QText
m_findScopeVerticalBlockSelectionFirstColumn = verticalBlockSelectionFirstColumn; m_findScopeVerticalBlockSelectionFirstColumn = verticalBlockSelectionFirstColumn;
m_findScopeVerticalBlockSelectionLastColumn = verticalBlockSelectionLastColumn; m_findScopeVerticalBlockSelectionLastColumn = verticalBlockSelectionLastColumn;
q->viewport()->update(); q->viewport()->update();
highlightSearchResultsInScrollBar();
} }
} }
@@ -6204,6 +6441,7 @@ void TextEditorWidget::setDisplaySettings(const DisplaySettings &ds)
d->updateCodeFoldingVisible(); d->updateCodeFoldingVisible();
d->updateHighlights(); d->updateHighlights();
d->setupScrollBar();
viewport()->update(); viewport()->update();
extraArea()->update(); extraArea()->update();
} }

View File

@@ -178,6 +178,9 @@ const char FOLLOW_SYMBOL_UNDER_CURSOR_IN_NEXT_SPLIT[] = "TextEditor.FollowSymbol
const char JUMP_TO_FILE_UNDER_CURSOR[] = "TextEditor.JumpToFileUnderCursor"; const char JUMP_TO_FILE_UNDER_CURSOR[] = "TextEditor.JumpToFileUnderCursor";
const char JUMP_TO_FILE_UNDER_CURSOR_IN_NEXT_SPLIT[] = "TextEditor.JumpToFileUnderCursorInNextSplit"; const char JUMP_TO_FILE_UNDER_CURSOR_IN_NEXT_SPLIT[] = "TextEditor.JumpToFileUnderCursorInNextSplit";
const char SCROLL_BAR_SEARCH_RESULT[] = "TextEditor.ScrollBarSearchResult";
const char SCROLL_BAR_CURRENT_LINE[] = "TextEditor.ScrollBarCurrentLine";
const char *nameForStyle(TextStyle style); const char *nameForStyle(TextStyle style);
TextStyle styleFromName(const char *name); TextStyle styleFromName(const char *name);