2015-06-04 10:43:34 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2017-11-02 13:06:38 +01:00
|
|
|
** Copyright (C) 2017 The Qt Company Ltd.
|
2016-01-15 14:57:40 +01:00
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-06-04 10:43:34 +02:00
|
|
|
**
|
|
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2015-06-04 10:43:34 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2015-06-04 10:43:34 +02:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2017-11-02 13:06:38 +01:00
|
|
|
#include "highlightscrollbarcontroller.h"
|
2015-06-04 10:43:34 +02:00
|
|
|
|
2017-11-02 13:06:38 +01:00
|
|
|
#include <QAbstractScrollArea>
|
2015-06-04 10:43:34 +02:00
|
|
|
#include <QPainter>
|
|
|
|
|
#include <QResizeEvent>
|
2017-11-02 13:06:38 +01:00
|
|
|
#include <QScrollBar>
|
2015-06-04 10:43:34 +02:00
|
|
|
#include <QStyle>
|
|
|
|
|
#include <QStyleOptionSlider>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
|
|
using namespace Utils;
|
2017-01-14 22:49:37 +02:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
|
|
class HighlightScrollBarOverlay : public QWidget
|
|
|
|
|
{
|
|
|
|
|
public:
|
2017-11-02 13:06:38 +01:00
|
|
|
HighlightScrollBarOverlay(HighlightScrollBarController *scrollBarController)
|
|
|
|
|
: QWidget(scrollBarController->scrollArea())
|
|
|
|
|
, m_scrollBar(scrollBarController->scrollBar())
|
|
|
|
|
, m_highlightController(scrollBarController)
|
|
|
|
|
{
|
|
|
|
|
setAttribute(Qt::WA_TransparentForMouseEvents);
|
|
|
|
|
m_scrollBar->parentWidget()->installEventFilter(this);
|
|
|
|
|
doResize();
|
|
|
|
|
doMove();
|
|
|
|
|
show();
|
|
|
|
|
}
|
2017-01-14 22:49:37 +02:00
|
|
|
|
2017-11-02 13:06:38 +01:00
|
|
|
void doResize()
|
|
|
|
|
{
|
|
|
|
|
resize(m_scrollBar->size());
|
|
|
|
|
}
|
2017-01-14 22:49:37 +02:00
|
|
|
|
2017-11-02 13:06:38 +01:00
|
|
|
void doMove()
|
|
|
|
|
{
|
|
|
|
|
move(parentWidget()->mapFromGlobal(m_scrollBar->mapToGlobal(m_scrollBar->pos())));
|
|
|
|
|
}
|
2017-01-14 22:49:37 +02:00
|
|
|
|
2017-11-02 13:06:38 +01:00
|
|
|
void scheduleUpdate();
|
2017-01-14 22:49:37 +02:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void paintEvent(QPaintEvent *paintEvent) override;
|
2017-11-02 13:06:38 +01:00
|
|
|
bool eventFilter(QObject *object, QEvent *event) override;
|
2017-01-14 22:49:37 +02:00
|
|
|
|
|
|
|
|
private:
|
2017-11-02 13:06:38 +01:00
|
|
|
void updateCache();
|
|
|
|
|
QRect overlayRect() const;
|
2015-06-04 10:43:34 +02:00
|
|
|
|
2017-11-02 13:06:38 +01:00
|
|
|
bool m_cacheUpdateScheduled = true;
|
|
|
|
|
QMap<int, Highlight> m_cache;
|
2015-06-04 10:43:34 +02:00
|
|
|
|
2017-11-02 13:06:38 +01:00
|
|
|
QScrollBar *m_scrollBar;
|
|
|
|
|
HighlightScrollBarController *m_highlightController;
|
|
|
|
|
};
|
2015-06-04 10:43:34 +02:00
|
|
|
|
|
|
|
|
void HighlightScrollBarOverlay::scheduleUpdate()
|
|
|
|
|
{
|
|
|
|
|
if (m_cacheUpdateScheduled)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_cacheUpdateScheduled = true;
|
|
|
|
|
QTimer::singleShot(0, this, static_cast<void (QWidget::*)()>(&QWidget::update));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HighlightScrollBarOverlay::paintEvent(QPaintEvent *paintEvent)
|
|
|
|
|
{
|
|
|
|
|
QWidget::paintEvent(paintEvent);
|
|
|
|
|
|
|
|
|
|
updateCache();
|
|
|
|
|
|
|
|
|
|
if (m_cache.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
2017-11-02 13:06:38 +01:00
|
|
|
const QRect &rect = overlayRect();
|
2015-06-04 10:43:34 +02:00
|
|
|
|
2017-06-01 12:06:03 +02:00
|
|
|
Utils::Theme::Color previousColor = Utils::Theme::TextColorNormal;
|
|
|
|
|
Highlight::Priority previousPriority = Highlight::LowPriority;
|
2017-11-02 13:06:38 +01:00
|
|
|
QRect *previousRect = nullptr;
|
2015-06-04 10:43:34 +02:00
|
|
|
|
|
|
|
|
const int scrollbarRange = m_scrollBar->maximum() + m_scrollBar->pageStep();
|
2017-11-02 13:06:38 +01:00
|
|
|
const int range = qMax(m_highlightController->visibleRange(), float(scrollbarRange));
|
2015-06-04 10:43:34 +02:00
|
|
|
const int horizontalMargin = 3;
|
|
|
|
|
const int resultWidth = rect.width() - 2 * horizontalMargin + 1;
|
|
|
|
|
const int resultHeight = qMin(int(rect.height() / range) + 1, 4);
|
2017-11-02 13:06:38 +01:00
|
|
|
const int offset = rect.height() / range * m_highlightController->rangeOffset();
|
2015-06-04 10:43:34 +02:00
|
|
|
const int verticalMargin = ((rect.height() / range) - resultHeight) / 2;
|
|
|
|
|
int previousBottom = -1;
|
|
|
|
|
|
2017-06-01 12:06:03 +02:00
|
|
|
QHash<Utils::Theme::Color, QVector<QRect> > highlights;
|
2018-04-08 23:40:00 +03:00
|
|
|
for (const Highlight ¤tHighlight : qAsConst(m_cache)) {
|
2017-06-01 12:06:03 +02:00
|
|
|
// Calculate top and bottom
|
|
|
|
|
int top = rect.top() + offset + verticalMargin
|
|
|
|
|
+ float(currentHighlight.position) / range * rect.height();
|
2015-06-04 10:43:34 +02:00
|
|
|
const int bottom = top + resultHeight;
|
|
|
|
|
|
2017-06-01 12:06:03 +02:00
|
|
|
if (previousRect && previousColor == currentHighlight.color && previousBottom + 1 >= top) {
|
|
|
|
|
// If the previous highlight has the same color and is directly prior to this highlight
|
2015-06-04 10:43:34 +02:00
|
|
|
// we just extend the previous highlight.
|
2017-06-01 12:06:03 +02:00
|
|
|
previousRect->setBottom(bottom - 1);
|
2015-06-04 10:43:34 +02:00
|
|
|
|
|
|
|
|
} 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
|
|
|
|
|
|
2017-06-01 12:06:03 +02:00
|
|
|
if (previousPriority > currentHighlight.priority) {
|
2015-06-04 10:43:34 +02:00
|
|
|
// 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.
|
2017-06-01 12:06:03 +02:00
|
|
|
highlights[previousColor].removeLast();
|
2015-06-04 10:43:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
2017-06-01 12:06:03 +02:00
|
|
|
highlights[currentHighlight.color] << QRect(rect.left() + horizontalMargin, top,
|
|
|
|
|
resultWidth, bottom - top);
|
|
|
|
|
previousRect = &highlights[currentHighlight.color].last();
|
|
|
|
|
previousColor = currentHighlight.color;
|
|
|
|
|
previousPriority = currentHighlight.priority;
|
2015-06-04 10:43:34 +02:00
|
|
|
}
|
|
|
|
|
previousBottom = previousRect->bottom();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QPainter painter(this);
|
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing, false);
|
2017-10-19 10:38:34 +02:00
|
|
|
const auto highlightEnd = highlights.cend();
|
|
|
|
|
for (auto highlightIt = highlights.cbegin(); highlightIt != highlightEnd; ++highlightIt) {
|
|
|
|
|
const QColor &color = creatorTheme()->color(highlightIt.key());
|
2017-10-24 08:21:50 +02:00
|
|
|
for (const QRect &rect : highlightIt.value())
|
2015-06-04 10:43:34 +02:00
|
|
|
painter.fillRect(rect, color);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-14 22:49:37 +02:00
|
|
|
|
2017-11-02 13:06:38 +01:00
|
|
|
bool HighlightScrollBarOverlay::eventFilter(QObject *object, QEvent *event)
|
|
|
|
|
{
|
|
|
|
|
switch (event->type()) {
|
|
|
|
|
case QEvent::Move:
|
|
|
|
|
doMove();
|
|
|
|
|
break;
|
|
|
|
|
case QEvent::Resize:
|
|
|
|
|
doResize();
|
|
|
|
|
break;
|
|
|
|
|
case QEvent::ZOrderChange:
|
|
|
|
|
raise();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return QWidget::eventFilter(object, event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HighlightScrollBarOverlay::updateCache()
|
|
|
|
|
{
|
|
|
|
|
if (!m_cacheUpdateScheduled)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_cache.clear();
|
|
|
|
|
const QHash<Id, QVector<Highlight>> highlights = m_highlightController->highlights();
|
|
|
|
|
const QList<Id> &categories = highlights.keys();
|
|
|
|
|
for (const Id &category : categories) {
|
|
|
|
|
for (const Highlight &highlight : highlights.value(category)) {
|
|
|
|
|
const Highlight oldHighlight = m_cache.value(highlight.position);
|
|
|
|
|
if (highlight.priority > oldHighlight.priority)
|
|
|
|
|
m_cache[highlight.position] = highlight;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m_cacheUpdateScheduled = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QRect HighlightScrollBarOverlay::overlayRect() const
|
|
|
|
|
{
|
|
|
|
|
QStyleOptionSlider opt = qt_qscrollbarStyleOption(m_scrollBar);
|
|
|
|
|
return m_scrollBar->style()->subControlRect(QStyle::CC_ScrollBar, &opt, QStyle::SC_ScrollBarGroove, m_scrollBar);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/////////////
|
|
|
|
|
|
2017-06-01 12:06:03 +02:00
|
|
|
Highlight::Highlight(Id category_, int position_,
|
|
|
|
|
Theme::Color color_, Highlight::Priority priority_)
|
|
|
|
|
: category(category_)
|
|
|
|
|
, position(position_)
|
|
|
|
|
, color(color_)
|
|
|
|
|
, priority(priority_)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-02 13:06:38 +01:00
|
|
|
/////////////
|
|
|
|
|
|
|
|
|
|
HighlightScrollBarController::~HighlightScrollBarController()
|
|
|
|
|
{
|
|
|
|
|
if (m_overlay)
|
|
|
|
|
delete m_overlay;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QScrollBar *HighlightScrollBarController::scrollBar() const
|
|
|
|
|
{
|
|
|
|
|
if (m_scrollArea)
|
|
|
|
|
return m_scrollArea->verticalScrollBar();
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QAbstractScrollArea *HighlightScrollBarController::scrollArea() const
|
|
|
|
|
{
|
|
|
|
|
return m_scrollArea;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HighlightScrollBarController::setScrollArea(QAbstractScrollArea *scrollArea)
|
|
|
|
|
{
|
|
|
|
|
if (m_scrollArea == scrollArea)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (m_overlay) {
|
|
|
|
|
delete m_overlay;
|
|
|
|
|
m_overlay = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_scrollArea = scrollArea;
|
|
|
|
|
|
|
|
|
|
if (m_scrollArea) {
|
|
|
|
|
m_overlay = new HighlightScrollBarOverlay(this);
|
|
|
|
|
m_overlay->scheduleUpdate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float HighlightScrollBarController::visibleRange() const
|
|
|
|
|
{
|
|
|
|
|
return m_visibleRange;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HighlightScrollBarController::setVisibleRange(float visibleRange)
|
|
|
|
|
{
|
|
|
|
|
m_visibleRange = visibleRange;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float HighlightScrollBarController::rangeOffset() const
|
|
|
|
|
{
|
|
|
|
|
return m_rangeOffset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HighlightScrollBarController::setRangeOffset(float offset)
|
|
|
|
|
{
|
|
|
|
|
m_rangeOffset = offset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QHash<Id, QVector<Highlight>> HighlightScrollBarController::highlights() const
|
|
|
|
|
{
|
|
|
|
|
return m_highlights;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HighlightScrollBarController::addHighlight(Highlight highlight)
|
|
|
|
|
{
|
|
|
|
|
if (!m_overlay)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_highlights[highlight.category] << highlight;
|
|
|
|
|
m_overlay->scheduleUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HighlightScrollBarController::removeHighlights(Id category)
|
|
|
|
|
{
|
|
|
|
|
if (!m_overlay)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_highlights.remove(category);
|
|
|
|
|
m_overlay->scheduleUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HighlightScrollBarController::removeAllHighlights()
|
|
|
|
|
{
|
|
|
|
|
if (!m_overlay)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_highlights.clear();
|
|
|
|
|
m_overlay->scheduleUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-14 22:49:37 +02:00
|
|
|
} // namespace Core
|