2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01: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.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2014-07-19 11:27:28 +02:00
|
|
|
#include "textmark.h"
|
2014-09-26 09:14:03 +02:00
|
|
|
#include "textdocument.h"
|
|
|
|
|
#include "texteditor.h"
|
2012-02-08 17:21:12 +01:00
|
|
|
#include "texteditorplugin.h"
|
2010-05-28 14:06:57 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2012-03-12 16:56:25 +01:00
|
|
|
#include <coreplugin/documentmanager.h>
|
2012-05-02 14:33:28 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2019-01-28 09:32:11 +01:00
|
|
|
#include <utils/tooltip/tooltip.h>
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2019-01-28 09:32:11 +01:00
|
|
|
#include <QAction>
|
2016-08-16 14:19:41 +02:00
|
|
|
#include <QGridLayout>
|
2017-06-20 08:28:10 +02:00
|
|
|
#include <QPainter>
|
2019-01-28 09:32:11 +01:00
|
|
|
#include <QToolButton>
|
2016-07-22 08:57:32 +02:00
|
|
|
|
2013-08-29 17:56:28 +02:00
|
|
|
using namespace Core;
|
|
|
|
|
using namespace Utils;
|
2014-07-19 11:27:28 +02:00
|
|
|
using namespace TextEditor::Internal;
|
2013-08-29 17:56:28 +02:00
|
|
|
|
|
|
|
|
namespace TextEditor {
|
2010-10-29 15:20:10 +02:00
|
|
|
|
2017-05-29 14:30:43 +02:00
|
|
|
class TextMarkRegistry : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
static void add(TextMark *mark);
|
|
|
|
|
static bool remove(TextMark *mark);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
TextMarkRegistry(QObject *parent);
|
|
|
|
|
static TextMarkRegistry* instance();
|
|
|
|
|
void editorOpened(Core::IEditor *editor);
|
|
|
|
|
void documentRenamed(Core::IDocument *document, const QString &oldName, const QString &newName);
|
|
|
|
|
void allDocumentsRenamed(const QString &oldName, const QString &newName);
|
|
|
|
|
|
|
|
|
|
QHash<Utils::FileName, QSet<TextMark *> > m_marks;
|
|
|
|
|
};
|
|
|
|
|
|
2017-06-20 08:28:10 +02:00
|
|
|
class AnnotationColors
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static AnnotationColors &getAnnotationColors(const QColor &markColor,
|
|
|
|
|
const QColor &backgroundColor);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
using SourceColors = QPair<QColor, QColor>;
|
|
|
|
|
QColor rectColor;
|
|
|
|
|
QColor textColor;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static QHash<SourceColors, AnnotationColors> m_colorCache;
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-29 14:30:43 +02:00
|
|
|
TextMarkRegistry *m_instance = nullptr;
|
|
|
|
|
|
2018-05-02 15:02:00 +02:00
|
|
|
TextMark::TextMark(const FileName &fileName, int lineNumber, Id category, double widthFactor)
|
2017-06-01 08:48:11 +02:00
|
|
|
: m_fileName(fileName)
|
|
|
|
|
, m_lineNumber(lineNumber)
|
|
|
|
|
, m_visible(true)
|
|
|
|
|
, m_category(category)
|
|
|
|
|
, m_widthFactor(widthFactor)
|
2014-07-19 11:27:28 +02:00
|
|
|
{
|
|
|
|
|
if (!m_fileName.isEmpty())
|
2017-05-29 14:30:43 +02:00
|
|
|
TextMarkRegistry::add(this);
|
2014-07-19 11:27:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextMark::~TextMark()
|
|
|
|
|
{
|
2019-01-28 09:32:11 +01:00
|
|
|
qDeleteAll(m_actions);
|
|
|
|
|
m_actions.clear();
|
2017-07-19 12:56:17 +03:00
|
|
|
if (!m_fileName.isEmpty())
|
|
|
|
|
TextMarkRegistry::remove(this);
|
2014-07-19 11:27:28 +02:00
|
|
|
if (m_baseTextDocument)
|
|
|
|
|
m_baseTextDocument->removeMark(this);
|
2017-07-18 23:15:52 +02:00
|
|
|
m_baseTextDocument = nullptr;
|
2014-07-19 11:27:28 +02:00
|
|
|
}
|
|
|
|
|
|
2018-05-02 15:02:00 +02:00
|
|
|
FileName TextMark::fileName() const
|
2014-07-19 11:27:28 +02:00
|
|
|
{
|
|
|
|
|
return m_fileName;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-02 15:02:00 +02:00
|
|
|
void TextMark::updateFileName(const FileName &fileName)
|
2014-07-19 11:27:28 +02:00
|
|
|
{
|
|
|
|
|
if (fileName == m_fileName)
|
|
|
|
|
return;
|
|
|
|
|
if (!m_fileName.isEmpty())
|
2017-05-29 14:30:43 +02:00
|
|
|
TextMarkRegistry::remove(this);
|
2014-07-19 11:27:28 +02:00
|
|
|
m_fileName = fileName;
|
|
|
|
|
if (!m_fileName.isEmpty())
|
2017-05-29 14:30:43 +02:00
|
|
|
TextMarkRegistry::add(this);
|
2014-07-19 11:27:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int TextMark::lineNumber() const
|
|
|
|
|
{
|
|
|
|
|
return m_lineNumber;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-20 08:28:10 +02:00
|
|
|
void TextMark::paintIcon(QPainter *painter, const QRect &rect) const
|
2014-07-19 11:27:28 +02:00
|
|
|
{
|
|
|
|
|
m_icon.paint(painter, rect, Qt::AlignCenter);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-19 14:51:21 +02:00
|
|
|
void TextMark::paintAnnotation(QPainter &painter, QRectF *annotationRect,
|
2017-10-05 10:03:25 +02:00
|
|
|
const qreal fadeInOffset, const qreal fadeOutOffset,
|
|
|
|
|
const QPointF &contentOffset) const
|
2017-06-20 08:28:10 +02:00
|
|
|
{
|
|
|
|
|
QString text = lineAnnotation();
|
|
|
|
|
if (text.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
2017-07-19 14:51:21 +02:00
|
|
|
const AnnotationRects &rects = annotationRects(*annotationRect, painter.fontMetrics(),
|
|
|
|
|
fadeInOffset, fadeOutOffset);
|
|
|
|
|
const QColor &markColor = m_hasColor ? Utils::creatorTheme()->color(m_color).toHsl()
|
|
|
|
|
: painter.pen().color();
|
|
|
|
|
const AnnotationColors &colors = AnnotationColors::getAnnotationColors(
|
|
|
|
|
markColor, painter.background().color());
|
|
|
|
|
|
|
|
|
|
painter.save();
|
2017-10-05 10:03:25 +02:00
|
|
|
QLinearGradient grad(rects.fadeInRect.topLeft() - contentOffset,
|
|
|
|
|
rects.fadeInRect.topRight() - contentOffset);
|
2017-07-19 14:51:21 +02:00
|
|
|
grad.setColorAt(0.0, Qt::transparent);
|
|
|
|
|
grad.setColorAt(1.0, colors.rectColor);
|
|
|
|
|
painter.fillRect(rects.fadeInRect, grad);
|
|
|
|
|
painter.fillRect(rects.annotationRect, colors.rectColor);
|
|
|
|
|
painter.setPen(colors.textColor);
|
|
|
|
|
paintIcon(&painter, rects.iconRect.toAlignedRect());
|
|
|
|
|
painter.drawText(rects.textRect, Qt::AlignLeft, rects.text);
|
|
|
|
|
if (rects.fadeOutRect.isValid()) {
|
2017-10-05 10:03:25 +02:00
|
|
|
grad = QLinearGradient(rects.fadeOutRect.topLeft() - contentOffset,
|
|
|
|
|
rects.fadeOutRect.topRight() - contentOffset);
|
2017-07-19 14:51:21 +02:00
|
|
|
grad.setColorAt(0.0, colors.rectColor);
|
|
|
|
|
grad.setColorAt(1.0, Qt::transparent);
|
|
|
|
|
painter.fillRect(rects.fadeOutRect, grad);
|
|
|
|
|
}
|
|
|
|
|
painter.restore();
|
|
|
|
|
annotationRect->setRight(rects.fadeOutRect.right());
|
2017-07-05 11:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextMark::AnnotationRects TextMark::annotationRects(const QRectF &boundingRect,
|
2017-07-19 14:51:21 +02:00
|
|
|
const QFontMetrics &fm,
|
|
|
|
|
const qreal fadeInOffset,
|
|
|
|
|
const qreal fadeOutOffset) const
|
2017-07-05 11:09:07 +02:00
|
|
|
{
|
|
|
|
|
AnnotationRects rects;
|
|
|
|
|
rects.text = lineAnnotation();
|
2017-07-19 14:51:21 +02:00
|
|
|
if (rects.text.isEmpty())
|
|
|
|
|
return rects;
|
|
|
|
|
rects.fadeInRect = boundingRect;
|
|
|
|
|
rects.fadeInRect.setWidth(fadeInOffset);
|
|
|
|
|
rects.annotationRect = boundingRect;
|
|
|
|
|
rects.annotationRect.setLeft(rects.fadeInRect.right());
|
2017-07-05 11:09:07 +02:00
|
|
|
const bool drawIcon = !m_icon.isNull();
|
|
|
|
|
constexpr qreal margin = 1;
|
2017-07-19 14:51:21 +02:00
|
|
|
rects.iconRect = QRectF(rects.annotationRect.left(), boundingRect.top(),
|
|
|
|
|
0, boundingRect.height());
|
|
|
|
|
if (drawIcon)
|
2017-07-05 11:09:07 +02:00
|
|
|
rects.iconRect.setWidth(rects.iconRect.height() * m_widthFactor);
|
|
|
|
|
rects.textRect = QRectF(rects.iconRect.right() + margin, boundingRect.top(),
|
|
|
|
|
qreal(fm.width(rects.text)), boundingRect.height());
|
|
|
|
|
rects.annotationRect.setRight(rects.textRect.right() + margin);
|
|
|
|
|
if (rects.annotationRect.right() > boundingRect.right()) {
|
|
|
|
|
rects.textRect.setRight(boundingRect.right() - margin);
|
|
|
|
|
rects.text = fm.elidedText(rects.text, Qt::ElideRight, int(rects.textRect.width()));
|
|
|
|
|
rects.annotationRect.setRight(boundingRect.right());
|
2017-07-19 14:51:21 +02:00
|
|
|
rects.fadeOutRect = QRectF(rects.annotationRect.topRight(),
|
|
|
|
|
rects.annotationRect.bottomRight());
|
|
|
|
|
} else {
|
|
|
|
|
rects.fadeOutRect = boundingRect;
|
|
|
|
|
rects.fadeOutRect.setLeft(rects.annotationRect.right());
|
|
|
|
|
rects.fadeOutRect.setWidth(fadeOutOffset);
|
2017-07-05 11:09:07 +02:00
|
|
|
}
|
|
|
|
|
return rects;
|
2017-06-20 08:28:10 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-19 11:27:28 +02:00
|
|
|
void TextMark::updateLineNumber(int lineNumber)
|
|
|
|
|
{
|
|
|
|
|
m_lineNumber = lineNumber;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextMark::move(int line)
|
|
|
|
|
{
|
|
|
|
|
if (line == m_lineNumber)
|
|
|
|
|
return;
|
|
|
|
|
const int previousLine = m_lineNumber;
|
|
|
|
|
m_lineNumber = line;
|
|
|
|
|
if (m_baseTextDocument)
|
|
|
|
|
m_baseTextDocument->moveMark(this, previousLine);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextMark::updateBlock(const QTextBlock &)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void TextMark::removedFromEditor()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void TextMark::updateMarker()
|
|
|
|
|
{
|
|
|
|
|
if (m_baseTextDocument)
|
|
|
|
|
m_baseTextDocument->updateMark(this);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-26 14:39:13 +02:00
|
|
|
void TextMark::setPriority(TextMark::Priority prioriy)
|
|
|
|
|
{
|
|
|
|
|
m_priority = prioriy;
|
|
|
|
|
updateMarker();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-19 11:27:28 +02:00
|
|
|
bool TextMark::isVisible() const
|
|
|
|
|
{
|
|
|
|
|
return m_visible;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextMark::setVisible(bool visible)
|
|
|
|
|
{
|
|
|
|
|
m_visible = visible;
|
2017-06-26 14:39:13 +02:00
|
|
|
updateMarker();
|
2014-07-19 11:27:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double TextMark::widthFactor() const
|
|
|
|
|
{
|
|
|
|
|
return m_widthFactor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextMark::setWidthFactor(double factor)
|
|
|
|
|
{
|
|
|
|
|
m_widthFactor = factor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TextMark::isClickable() const
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextMark::clicked()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
bool TextMark::isDraggable() const
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextMark::dragToLine(int lineNumber)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(lineNumber);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-20 08:28:10 +02:00
|
|
|
void TextMark::addToToolTipLayout(QGridLayout *target) const
|
2016-08-16 14:19:41 +02:00
|
|
|
{
|
2018-09-20 01:16:01 +03:00
|
|
|
auto contentLayout = new QVBoxLayout;
|
2016-08-16 14:19:41 +02:00
|
|
|
addToolTipContent(contentLayout);
|
2019-01-28 09:32:11 +01:00
|
|
|
if (contentLayout->count() <= 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Left column: text mark icon
|
|
|
|
|
const int row = target->rowCount();
|
|
|
|
|
if (!m_icon.isNull()) {
|
|
|
|
|
auto iconLabel = new QLabel;
|
|
|
|
|
iconLabel->setPixmap(m_icon.pixmap(16, 16));
|
|
|
|
|
target->addWidget(iconLabel, row, 0, Qt::AlignTop | Qt::AlignHCenter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Middle column: tooltip content
|
|
|
|
|
target->addLayout(contentLayout, row, 1);
|
|
|
|
|
|
|
|
|
|
// Right column: action icons/button
|
|
|
|
|
if (!m_actions.isEmpty()) {
|
|
|
|
|
auto actionsLayout = new QHBoxLayout;
|
|
|
|
|
for (QAction *action : m_actions) {
|
|
|
|
|
QTC_ASSERT(!action->icon().isNull(), continue);
|
|
|
|
|
auto button = new QToolButton;
|
|
|
|
|
button->setIcon(action->icon());
|
|
|
|
|
QObject::connect(button, &QToolButton::clicked, action, &QAction::triggered);
|
|
|
|
|
QObject::connect(button, &QToolButton::clicked, []() {
|
|
|
|
|
Utils::ToolTip::hideImmediately();
|
|
|
|
|
});
|
|
|
|
|
actionsLayout->addWidget(button, 0, Qt::AlignTop | Qt::AlignRight);
|
2016-08-16 14:19:41 +02:00
|
|
|
}
|
2019-01-28 09:32:11 +01:00
|
|
|
target->addLayout(actionsLayout, row, 2);
|
2016-08-16 14:19:41 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-20 08:28:10 +02:00
|
|
|
bool TextMark::addToolTipContent(QLayout *target) const
|
2016-07-22 08:57:32 +02:00
|
|
|
{
|
2016-08-16 14:19:41 +02:00
|
|
|
QString text = m_toolTip;
|
|
|
|
|
if (text.isEmpty()) {
|
2017-06-01 08:48:11 +02:00
|
|
|
text = m_defaultToolTip;
|
2016-08-16 14:19:41 +02:00
|
|
|
if (text.isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto textLabel = new QLabel;
|
2018-10-04 08:41:24 +03:00
|
|
|
textLabel->setOpenExternalLinks(true);
|
2016-08-16 14:19:41 +02:00
|
|
|
textLabel->setText(text);
|
|
|
|
|
// Differentiate between tool tips that where explicitly set and default tool tips.
|
|
|
|
|
textLabel->setEnabled(!m_toolTip.isEmpty());
|
|
|
|
|
target->addWidget(textLabel);
|
|
|
|
|
|
|
|
|
|
return true;
|
2016-07-22 08:57:32 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-01 08:48:11 +02:00
|
|
|
Theme::Color TextMark::color() const
|
2014-07-19 11:27:28 +02:00
|
|
|
{
|
2017-06-01 08:48:11 +02:00
|
|
|
QTC_CHECK(m_hasColor);
|
|
|
|
|
return m_color;
|
2014-07-19 11:27:28 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-01 08:48:11 +02:00
|
|
|
void TextMark::setColor(const Theme::Color &color)
|
2014-07-19 11:27:28 +02:00
|
|
|
{
|
2017-06-01 08:48:11 +02:00
|
|
|
m_hasColor = true;
|
|
|
|
|
m_color = color;
|
2016-07-19 11:58:15 +02:00
|
|
|
}
|
2014-07-19 11:27:28 +02:00
|
|
|
|
2019-01-28 09:32:11 +01:00
|
|
|
QVector<QAction *> TextMark::actions() const
|
|
|
|
|
{
|
|
|
|
|
return m_actions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextMark::setActions(const QVector<QAction *> &actions)
|
|
|
|
|
{
|
|
|
|
|
m_actions = actions;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-19 11:27:28 +02:00
|
|
|
TextMarkRegistry::TextMarkRegistry(QObject *parent)
|
2012-02-08 17:21:12 +01:00
|
|
|
: QObject(parent)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2015-12-13 01:18:33 +02:00
|
|
|
connect(EditorManager::instance(), &EditorManager::editorOpened,
|
|
|
|
|
this, &TextMarkRegistry::editorOpened);
|
2012-03-12 16:56:25 +01:00
|
|
|
|
2015-12-13 01:18:33 +02:00
|
|
|
connect(DocumentManager::instance(), &DocumentManager::allDocumentsRenamed,
|
|
|
|
|
this, &TextMarkRegistry::allDocumentsRenamed);
|
|
|
|
|
connect(DocumentManager::instance(), &DocumentManager::documentRenamed,
|
|
|
|
|
this, &TextMarkRegistry::documentRenamed);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-19 11:27:28 +02:00
|
|
|
void TextMarkRegistry::add(TextMark *mark)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2018-05-02 15:02:00 +02:00
|
|
|
instance()->m_marks[mark->fileName()].insert(mark);
|
|
|
|
|
auto document = qobject_cast<TextDocument *>(
|
|
|
|
|
DocumentModel::documentForFilePath(mark->fileName().toString()));
|
2013-07-18 12:26:23 +02:00
|
|
|
if (!document)
|
|
|
|
|
return;
|
2014-07-18 15:29:04 +02:00
|
|
|
document->addMark(mark);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-19 11:27:28 +02:00
|
|
|
bool TextMarkRegistry::remove(TextMark *mark)
|
2012-02-14 15:24:37 +01:00
|
|
|
{
|
2018-05-02 15:02:00 +02:00
|
|
|
return instance()->m_marks[mark->fileName()].remove(mark);
|
2012-02-14 15:24:37 +01:00
|
|
|
}
|
|
|
|
|
|
2017-05-29 14:30:43 +02:00
|
|
|
TextMarkRegistry *TextMarkRegistry::instance()
|
|
|
|
|
{
|
|
|
|
|
if (!m_instance)
|
|
|
|
|
m_instance = new TextMarkRegistry(TextEditorPlugin::instance());
|
|
|
|
|
return m_instance;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-03 23:46:35 +02:00
|
|
|
void TextMarkRegistry::editorOpened(IEditor *editor)
|
2010-05-28 14:06:57 +02:00
|
|
|
{
|
2018-09-20 01:16:01 +03:00
|
|
|
auto document = qobject_cast<TextDocument *>(editor ? editor->document() : nullptr);
|
2013-12-09 16:54:31 +01:00
|
|
|
if (!document)
|
2010-05-28 14:06:57 +02:00
|
|
|
return;
|
2014-12-21 21:54:30 +02:00
|
|
|
if (!m_marks.contains(document->filePath()))
|
2012-02-08 17:21:12 +01:00
|
|
|
return;
|
|
|
|
|
|
2014-12-21 21:54:30 +02:00
|
|
|
foreach (TextMark *mark, m_marks.value(document->filePath()))
|
2014-07-18 15:29:04 +02:00
|
|
|
document->addMark(mark);
|
2012-02-08 17:21:12 +01:00
|
|
|
}
|
2010-05-28 14:06:57 +02:00
|
|
|
|
2014-07-19 11:27:28 +02:00
|
|
|
void TextMarkRegistry::documentRenamed(IDocument *document, const
|
2012-03-12 16:56:25 +01:00
|
|
|
QString &oldName, const QString &newName)
|
|
|
|
|
{
|
2018-09-20 01:16:01 +03:00
|
|
|
auto baseTextDocument = qobject_cast<TextDocument *>(document);
|
2016-08-25 09:40:55 +02:00
|
|
|
if (!baseTextDocument)
|
2012-03-12 16:56:25 +01:00
|
|
|
return;
|
2013-08-29 17:56:28 +02:00
|
|
|
FileName oldFileName = FileName::fromString(oldName);
|
|
|
|
|
FileName newFileName = FileName::fromString(newName);
|
2012-03-12 16:56:25 +01:00
|
|
|
if (!m_marks.contains(oldFileName))
|
|
|
|
|
return;
|
|
|
|
|
|
2014-07-19 11:27:28 +02:00
|
|
|
QSet<TextMark *> toBeMoved;
|
|
|
|
|
foreach (TextMark *mark, baseTextDocument->marks())
|
|
|
|
|
toBeMoved.insert(mark);
|
2012-03-12 16:56:25 +01:00
|
|
|
|
|
|
|
|
m_marks[oldFileName].subtract(toBeMoved);
|
|
|
|
|
m_marks[newFileName].unite(toBeMoved);
|
|
|
|
|
|
2014-07-19 11:27:28 +02:00
|
|
|
foreach (TextMark *mark, toBeMoved)
|
2018-05-02 15:02:00 +02:00
|
|
|
mark->updateFileName(newFileName);
|
2012-03-12 16:56:25 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-19 11:27:28 +02:00
|
|
|
void TextMarkRegistry::allDocumentsRenamed(const QString &oldName, const QString &newName)
|
2012-03-12 16:56:25 +01:00
|
|
|
{
|
2013-08-29 17:56:28 +02:00
|
|
|
FileName oldFileName = FileName::fromString(oldName);
|
|
|
|
|
FileName newFileName = FileName::fromString(newName);
|
2012-03-12 16:56:25 +01:00
|
|
|
if (!m_marks.contains(oldFileName))
|
|
|
|
|
return;
|
|
|
|
|
|
2014-07-19 11:27:28 +02:00
|
|
|
QSet<TextMark *> oldFileNameMarks = m_marks.value(oldFileName);
|
2012-03-12 16:56:25 +01:00
|
|
|
|
|
|
|
|
m_marks[newFileName].unite(oldFileNameMarks);
|
|
|
|
|
m_marks[oldFileName].clear();
|
|
|
|
|
|
2014-07-19 11:27:28 +02:00
|
|
|
foreach (TextMark *mark, oldFileNameMarks)
|
2018-05-02 15:02:00 +02:00
|
|
|
mark->updateFileName(newFileName);
|
2012-03-12 16:56:25 +01:00
|
|
|
}
|
|
|
|
|
|
2017-06-20 08:28:10 +02:00
|
|
|
QHash<AnnotationColors::SourceColors, AnnotationColors> AnnotationColors::m_colorCache;
|
|
|
|
|
|
|
|
|
|
AnnotationColors &AnnotationColors::getAnnotationColors(const QColor &markColor,
|
|
|
|
|
const QColor &backgroundColor)
|
|
|
|
|
{
|
2017-07-19 09:19:29 +02:00
|
|
|
auto highClipHsl = [](qreal value) {
|
|
|
|
|
return std::max(0.7, std::min(0.9, value));
|
|
|
|
|
};
|
|
|
|
|
auto lowClipHsl = [](qreal value) {
|
|
|
|
|
return std::max(0.1, std::min(0.3, value));
|
|
|
|
|
};
|
2017-06-20 08:28:10 +02:00
|
|
|
AnnotationColors &colors = m_colorCache[{markColor, backgroundColor}];
|
|
|
|
|
if (!colors.rectColor.isValid() || !colors.textColor.isValid()) {
|
2017-07-19 09:19:29 +02:00
|
|
|
const double backgroundLightness = backgroundColor.lightnessF();
|
|
|
|
|
const double foregroundLightness = backgroundLightness > 0.5
|
|
|
|
|
? lowClipHsl(backgroundLightness - 0.5)
|
|
|
|
|
: highClipHsl(backgroundLightness + 0.5);
|
|
|
|
|
|
|
|
|
|
colors.rectColor = markColor;
|
|
|
|
|
colors.rectColor.setAlphaF(0.15);
|
|
|
|
|
|
2017-06-20 08:28:10 +02:00
|
|
|
colors.textColor.setHslF(markColor.hslHueF(),
|
|
|
|
|
markColor.hslSaturationF(),
|
|
|
|
|
foregroundLightness);
|
|
|
|
|
}
|
|
|
|
|
return colors;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-29 17:56:28 +02:00
|
|
|
} // namespace TextEditor
|
2017-05-29 14:30:43 +02:00
|
|
|
|
|
|
|
|
#include "textmark.moc"
|