Commit c82d287c authored by David Schulz's avatar David Schulz

Editor: Highlight Search results in scrollbar.

Change-Id: I999ddeeedbd6bdd7abebf75405d7e9281a1893df
Reviewed-by: default avatarEike Ziller <eike.ziller@theqtcompany.com>
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
parent 92c902c7
......@@ -130,6 +130,9 @@ VcsBase_FileRenamed_TextColor=ffffa500
Bookmarks_TextMarkColor=ff8080ff
TextEditor_SearchResult_ScrollBarColor=ff00c000
TextEditor_CurrentLine_ScrollBarColor=ffffffff
ProjectExplorer_TaskError_TextMarkColor=ffff4040
ProjectExplorer_TaskWarn_TextMarkColor=ffffff40
......
......@@ -124,6 +124,9 @@ VcsBase_FileRenamed_TextColor=ffd77d00
Bookmarks_TextMarkColor=ffa0a0ff
TextEditor_SearchResult_ScrollBarColor=ff00c000
TextEditor_CurrentLine_ScrollBarColor=ff404040
ProjectExplorer_TaskError_TextMarkColor=ffff0000
ProjectExplorer_TaskWarn_TextMarkColor=ffffa500
......
......@@ -185,6 +185,10 @@ public:
/* Bookmarks Plugin */
Bookmarks_TextMarkColor,
/* TextEditor Plugin */
TextEditor_SearchResult_ScrollBarColor,
TextEditor_CurrentLine_ScrollBarColor,
/* Debugger Plugin */
Debugger_Breakpoint_TextMarkColor,
......
......@@ -223,6 +223,8 @@ QtcPlugin {
"findtoolwindow.cpp",
"findtoolwindow.h",
"findwidget.ui",
"highlightscrollbar.cpp",
"highlightscrollbar.h",
"ifindfilter.cpp",
"ifindfilter.h",
"ifindsupport.cpp",
......
HEADERS += \
$$PWD/findtoolwindow.h \
$$PWD/textfindconstants.h \
$$PWD/ifindsupport.h \
$$PWD/ifindfilter.h \
$$PWD/currentdocumentfind.h \
$$PWD/basetextfind.h \
$$PWD/findtoolbar.h \
$$PWD/currentdocumentfind.h \
$$PWD/findplugin.h \
$$PWD/findtoolbar.h \
$$PWD/findtoolwindow.h \
$$PWD/ifindfilter.h \
$$PWD/ifindsupport.h \
$$PWD/itemviewfind.h \
$$PWD/searchresultcolor.h \
$$PWD/searchresulttreeitemdelegate.h \
$$PWD/searchresulttreeitemroles.h \
$$PWD/searchresulttreeitems.h \
$$PWD/searchresulttreemodel.h \
$$PWD/searchresulttreeview.h \
$$PWD/searchresultwindow.h \
$$PWD/searchresultwidget.h \
$$PWD/itemviewfind.h
$$PWD/searchresultwindow.h \
$$PWD/textfindconstants.h \
$$PWD/highlightscrollbar.h
SOURCES += \
$$PWD/findtoolwindow.cpp \
$$PWD/currentdocumentfind.cpp \
$$PWD/basetextfind.cpp \
$$PWD/findtoolbar.cpp \
$$PWD/currentdocumentfind.cpp \
$$PWD/findplugin.cpp \
$$PWD/findtoolbar.cpp \
$$PWD/findtoolwindow.cpp \
$$PWD/ifindfilter.cpp \
$$PWD/ifindsupport.cpp \
$$PWD/itemviewfind.cpp \
$$PWD/searchresulttreeitemdelegate.cpp \
$$PWD/searchresulttreeitems.cpp \
$$PWD/searchresulttreemodel.cpp \
$$PWD/searchresulttreeview.cpp \
$$PWD/searchresultwindow.cpp \
$$PWD/ifindfilter.cpp \
$$PWD/ifindsupport.cpp \
$$PWD/searchresultwidget.cpp \
$$PWD/itemviewfind.cpp
$$PWD/searchresultwindow.cpp \
$$PWD/highlightscrollbar.cpp
FORMS += \
$$PWD/findwidget.ui \
......
/****************************************************************************
**
** 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);
}
}
}
/****************************************************************************
**
** 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
......@@ -46,6 +46,7 @@ static const char autoFoldFirstCommentKey[] = "AutoFoldFirstComment";
static const char centerCursorOnScrollKey[] = "CenterCursorOnScroll";
static const char openLinksInNextSplitKey[] = "OpenLinksInNextSplitKey";
static const char displayFileEncodingKey[] = "DisplayFileEncoding";
static const char scrollBarHighlightsKey[] = "ScrollBarHighlights";
static const char groupPostfix[] = "DisplaySettings";
namespace TextEditor {
......@@ -64,7 +65,8 @@ DisplaySettings::DisplaySettings() :
m_centerCursorOnScroll(false),
m_openLinksInNextSplit(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(openLinksInNextSplitKey), m_openLinksInNextSplit);
s->setValue(QLatin1String(displayFileEncodingKey), m_displayFileEncoding);
s->setValue(QLatin1String(scrollBarHighlightsKey), m_scrollBarHighlights);
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_openLinksInNextSplit = s->value(group + QLatin1String(openLinksInNextSplitKey), m_openLinksInNextSplit).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
......@@ -130,6 +134,7 @@ bool DisplaySettings::equals(const DisplaySettings &ds) const
&& m_openLinksInNextSplit == ds.m_openLinksInNextSplit
&& m_forceOpenLinksInNextSplit == ds.m_forceOpenLinksInNextSplit
&& m_displayFileEncoding == ds.m_displayFileEncoding
&& m_scrollBarHighlights == ds.m_scrollBarHighlights
;
}
......
......@@ -61,6 +61,7 @@ public:
bool m_openLinksInNextSplit;
bool m_forceOpenLinksInNextSplit;
bool m_displayFileEncoding;
bool m_scrollBarHighlights;
bool equals(const DisplaySettings &ds) const;
};
......
......@@ -122,6 +122,7 @@ void DisplaySettingsPage::settingsFromUI(DisplaySettings &displaySettings,
displaySettings.m_centerCursorOnScroll = d->m_page->centerOnScroll->isChecked();
displaySettings.m_openLinksInNextSplit = d->m_page->openLinksInNextSplit->isChecked();
displaySettings.m_displayFileEncoding = d->m_page->displayFileEncoding->isChecked();
displaySettings.m_scrollBarHighlights = d->m_page->scrollBarHighlights->isChecked();
}
void DisplaySettingsPage::settingsToUI()
......@@ -143,6 +144,7 @@ void DisplaySettingsPage::settingsToUI()
d->m_page->centerOnScroll->setChecked(displaySettings.m_centerCursorOnScroll);
d->m_page->openLinksInNextSplit->setChecked(displaySettings.m_openLinksInNextSplit);
d->m_page->displayFileEncoding->setChecked(displaySettings.m_displayFileEncoding);
d->m_page->scrollBarHighlights->setChecked(displaySettings.m_scrollBarHighlights);
}
const DisplaySettings &DisplaySettingsPage::displaySettings() const
......
......@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>501</width>
<height>323</height>
<height>331</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_3">
......@@ -80,27 +80,17 @@
<string>Display</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<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">
<item row="1" column="0">
<widget class="QCheckBox" name="displayFoldingMarkers">
<property name="text">
<string>Mark &amp;text changes</string>
<string>Display &amp;folding markers</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QCheckBox" name="visualizeWhitespace">
<property name="toolTip">
<string>Shows tabs and spaces.</string>
</property>
<item row="5" column="0">
<widget class="QCheckBox" name="centerOnScroll">
<property name="text">
<string>&amp;Visualize whitespace</string>
<string>Center &amp;cursor on scroll</string>
</property>
</widget>
</item>
......@@ -111,17 +101,17 @@
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QCheckBox" name="centerOnScroll">
<item row="6" column="0">
<widget class="QCheckBox" name="autoFoldFirstComment">
<property name="text">
<string>Center &amp;cursor on scroll</string>
<string>Auto-fold first &amp;comment</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="displayFoldingMarkers">
<item row="2" column="1">
<widget class="QCheckBox" name="animateMatchingParentheses">
<property name="text">
<string>Display &amp;folding markers</string>
<string>&amp;Animate matching parentheses</string>
</property>
</widget>
</item>
......@@ -132,13 +122,6 @@
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QCheckBox" name="animateMatchingParentheses">
<property name="text">
<string>&amp;Animate matching parentheses</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="displayLineNumbers">