2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2014-07-19 11:27:28 +02:00
|
|
|
#include "textmark.h"
|
2020-09-15 13:33:37 +02:00
|
|
|
|
|
|
|
|
#include "fontsettings.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>
|
2021-01-15 11:17:21 +01:00
|
|
|
#include <coreplugin/icore.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>
|
2021-01-15 11:17:21 +01:00
|
|
|
#include <utils/utilsicons.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);
|
2021-06-11 14:34:34 +02:00
|
|
|
void documentRenamed(Core::IDocument *document,
|
|
|
|
|
const FilePath &oldPath,
|
|
|
|
|
const FilePath &newPath);
|
|
|
|
|
void allDocumentsRenamed(const FilePath &oldPath, const FilePath &newPath);
|
2017-05-29 14:30:43 +02:00
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
QHash<Utils::FilePath, QSet<TextMark *> > m_marks;
|
2017-05-29 14:30:43 +02:00
|
|
|
};
|
|
|
|
|
|
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;
|
|
|
|
|
|
2022-04-27 14:34:08 +02:00
|
|
|
TextMark::TextMark(const FilePath &fileName, int lineNumber, Id category)
|
2017-06-01 08:48:11 +02:00
|
|
|
: m_fileName(fileName)
|
|
|
|
|
, m_lineNumber(lineNumber)
|
|
|
|
|
, m_visible(true)
|
|
|
|
|
, m_category(category)
|
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()
|
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath TextMark::fileName() const
|
2014-07-19 11:27:28 +02:00
|
|
|
{
|
|
|
|
|
return m_fileName;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
void TextMark::updateFileName(const FilePath &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
|
|
|
{
|
2020-09-24 10:14:57 +02:00
|
|
|
icon().paint(painter, rect, Qt::AlignCenter);
|
2014-07-19 11:27:28 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-11 13:37:21 +01:00
|
|
|
void TextMark::paintAnnotation(QPainter &painter,
|
|
|
|
|
const QRect &eventRect,
|
|
|
|
|
QRectF *annotationRect,
|
|
|
|
|
const qreal fadeInOffset,
|
|
|
|
|
const qreal fadeOutOffset,
|
2017-10-05 10:03:25 +02:00
|
|
|
const QPointF &contentOffset) const
|
2017-06-20 08:28:10 +02:00
|
|
|
{
|
|
|
|
|
QString text = lineAnnotation();
|
|
|
|
|
if (text.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
2022-03-11 13:37:21 +01:00
|
|
|
const AnnotationRects &rects = annotationRects(*annotationRect,
|
|
|
|
|
painter.fontMetrics(),
|
|
|
|
|
fadeInOffset,
|
|
|
|
|
fadeOutOffset);
|
|
|
|
|
annotationRect->setRight(rects.fadeOutRect.right());
|
|
|
|
|
const QRectF eventRectF(eventRect);
|
|
|
|
|
if (!(rects.fadeInRect.intersects(eventRectF) || rects.annotationRect.intersects(eventRectF)
|
|
|
|
|
|| rects.fadeOutRect.intersects(eventRectF))) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-09 14:58:37 +02:00
|
|
|
const QColor &markColor = annotationColor();
|
2020-09-15 13:33:37 +02:00
|
|
|
|
|
|
|
|
const FontSettings &fontSettings = m_baseTextDocument->fontSettings();
|
2017-07-19 14:51:21 +02:00
|
|
|
const AnnotationColors &colors = AnnotationColors::getAnnotationColors(
|
2022-05-09 14:58:37 +02:00
|
|
|
markColor.isValid() ? markColor : painter.pen().color(),
|
|
|
|
|
fontSettings.toTextCharFormat(C_TEXT).background().color());
|
2017-07-19 14:51:21 +02:00
|
|
|
|
|
|
|
|
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();
|
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());
|
2020-09-24 10:14:57 +02:00
|
|
|
const bool drawIcon = !icon().isNull();
|
2017-07-05 11:09:07 +02:00
|
|
|
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)
|
2022-04-27 14:34:08 +02:00
|
|
|
rects.iconRect.setWidth(rects.iconRect.height());
|
2017-07-05 11:09:07 +02:00
|
|
|
rects.textRect = QRectF(rects.iconRect.right() + margin, boundingRect.top(),
|
2019-02-11 10:32:46 +01:00
|
|
|
qreal(fm.horizontalAdvance(rects.text)), boundingRect.height());
|
2017-07-05 11:09:07 +02:00
|
|
|
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)
|
2022-08-04 08:42:48 +02:00
|
|
|
m_baseTextDocument->scheduleUpdateLayout();
|
2014-07-19 11:27:28 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-26 14:39:13 +02:00
|
|
|
void TextMark::setPriority(TextMark::Priority prioriy)
|
|
|
|
|
{
|
|
|
|
|
m_priority = prioriy;
|
2022-05-24 09:56:23 +02:00
|
|
|
if (m_baseTextDocument)
|
|
|
|
|
m_baseTextDocument->updateMark(this);
|
2017-06-26 14:39:13 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TextMark::isClickable() const
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextMark::clicked()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
bool TextMark::isDraggable() const
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextMark::dragToLine(int lineNumber)
|
|
|
|
|
{
|
2019-07-23 10:58:00 +02:00
|
|
|
Q_UNUSED(lineNumber)
|
2014-07-19 11:27:28 +02:00
|
|
|
}
|
|
|
|
|
|
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();
|
2020-09-24 10:14:57 +02:00
|
|
|
const QIcon icon = this->icon();
|
|
|
|
|
if (!icon.isNull()) {
|
2019-01-28 09:32:11 +01:00
|
|
|
auto iconLabel = new QLabel;
|
2020-09-24 10:14:57 +02:00
|
|
|
iconLabel->setPixmap(icon.pixmap(16, 16));
|
2019-01-28 09:32:11 +01:00
|
|
|
target->addWidget(iconLabel, row, 0, Qt::AlignTop | Qt::AlignHCenter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Middle column: tooltip content
|
|
|
|
|
target->addLayout(contentLayout, row, 1);
|
|
|
|
|
|
|
|
|
|
// Right column: action icons/button
|
2022-08-03 15:25:17 +02:00
|
|
|
QList<QAction *> actions{m_actions.begin(), m_actions.end()};
|
|
|
|
|
if (m_actionsProvider)
|
|
|
|
|
actions = m_actionsProvider();
|
|
|
|
|
if (m_settingsPage.isValid()) {
|
|
|
|
|
auto settingsAction = new QAction;
|
|
|
|
|
settingsAction->setIcon(Utils::Icons::SETTINGS_TOOLBAR.icon());
|
|
|
|
|
settingsAction->setToolTip(tr("Show Diagnostic Settings"));
|
|
|
|
|
QObject::connect(settingsAction, &QAction::triggered, Core::ICore::instance(),
|
|
|
|
|
[id = m_settingsPage] { Core::ICore::showOptionsDialog(id); },
|
|
|
|
|
Qt::QueuedConnection);
|
|
|
|
|
actions.append(settingsAction);
|
|
|
|
|
}
|
2021-01-15 11:17:21 +01:00
|
|
|
if (!actions.isEmpty()) {
|
2019-01-28 09:32:11 +01:00
|
|
|
auto actionsLayout = new QHBoxLayout;
|
2019-02-14 12:31:39 +01:00
|
|
|
QMargins margins = actionsLayout->contentsMargins();
|
|
|
|
|
margins.setLeft(margins.left() + 5);
|
|
|
|
|
actionsLayout->setContentsMargins(margins);
|
2022-10-07 14:46:06 +02:00
|
|
|
for (QAction *action : std::as_const(actions)) {
|
2022-08-03 15:25:17 +02:00
|
|
|
QTC_ASSERT(!action->icon().isNull(), delete action; continue);
|
2019-01-28 09:32:11 +01:00
|
|
|
auto button = new QToolButton;
|
|
|
|
|
button->setIcon(action->icon());
|
2021-02-02 16:24:21 +01:00
|
|
|
button->setToolTip(action->toolTip());
|
2022-08-03 15:25:17 +02:00
|
|
|
action->setParent(button);
|
2019-01-28 09:32:11 +01:00
|
|
|
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
|
|
|
{
|
2020-09-24 10:14:57 +02:00
|
|
|
bool useDefaultToolTip = false;
|
|
|
|
|
QString text = toolTip();
|
2016-08-16 14:19:41 +02:00
|
|
|
if (text.isEmpty()) {
|
2020-09-24 10:14:57 +02:00
|
|
|
useDefaultToolTip = true;
|
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.
|
2020-09-24 10:14:57 +02:00
|
|
|
textLabel->setDisabled(useDefaultToolTip);
|
2016-08-16 14:19:41 +02:00
|
|
|
target->addWidget(textLabel);
|
|
|
|
|
|
|
|
|
|
return true;
|
2016-07-22 08:57:32 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-09 14:58:37 +02:00
|
|
|
QColor TextMark::annotationColor() const
|
|
|
|
|
{
|
|
|
|
|
if (m_color.has_value())
|
|
|
|
|
return Utils::creatorTheme()->color(*m_color).toHsl();
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-24 10:14:57 +02:00
|
|
|
void TextMark::setIcon(const QIcon &icon)
|
|
|
|
|
{
|
|
|
|
|
m_icon = icon;
|
|
|
|
|
m_iconProvider = std::function<QIcon()>();
|
2022-03-11 13:20:57 +01:00
|
|
|
updateMarker();
|
2020-09-24 10:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextMark::setIconProvider(const std::function<QIcon ()> &iconProvider)
|
|
|
|
|
{
|
|
|
|
|
m_iconProvider = iconProvider;
|
2022-03-11 13:20:57 +01:00
|
|
|
updateMarker();
|
2020-09-24 10:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QIcon TextMark::icon() const
|
|
|
|
|
{
|
|
|
|
|
return m_iconProvider ? m_iconProvider() : m_icon;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 10:30:00 +02:00
|
|
|
std::optional<Theme::Color> TextMark::color() const
|
2014-07-19 11:27:28 +02:00
|
|
|
{
|
2017-06-01 08:48:11 +02:00
|
|
|
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
|
|
|
{
|
2022-05-24 18:46:44 +02:00
|
|
|
if (m_color.has_value() && *m_color == color)
|
|
|
|
|
return;
|
2017-06-01 08:48:11 +02:00
|
|
|
m_color = color;
|
2022-03-11 13:20:57 +01:00
|
|
|
updateMarker();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextMark::setLineAnnotation(const QString &lineAnnotation)
|
|
|
|
|
{
|
|
|
|
|
m_lineAnnotation = lineAnnotation;
|
|
|
|
|
updateMarker();
|
2016-07-19 11:58:15 +02:00
|
|
|
}
|
2014-07-19 11:27:28 +02:00
|
|
|
|
2020-09-24 10:14:57 +02:00
|
|
|
void TextMark::setToolTipProvider(const std::function<QString()> &toolTipProvider)
|
|
|
|
|
{
|
|
|
|
|
m_toolTipProvider = toolTipProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString TextMark::toolTip() const
|
|
|
|
|
{
|
|
|
|
|
return m_toolTipProvider ? m_toolTipProvider() : m_toolTip;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextMark::setToolTip(const QString &toolTip)
|
|
|
|
|
{
|
|
|
|
|
m_toolTip = toolTip;
|
|
|
|
|
m_toolTipProvider = std::function<QString()>();
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-03 15:25:17 +02:00
|
|
|
void TextMark::setActionsProvider(const std::function<QList<QAction *>()> &actionsProvider)
|
|
|
|
|
{
|
|
|
|
|
m_actionsProvider = actionsProvider;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-15 11:17:21 +01:00
|
|
|
void TextMark::setSettingsPage(Id settingsPage)
|
|
|
|
|
{
|
2022-08-03 15:25:17 +02:00
|
|
|
m_settingsPage = settingsPage;
|
2021-01-15 11:17:21 +01:00
|
|
|
}
|
|
|
|
|
|
2022-09-06 13:01:51 +02:00
|
|
|
bool TextMark::isLocationMarker() const
|
|
|
|
|
{
|
|
|
|
|
return m_isLocationMarker;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextMark::setIsLocationMarker(bool newIsLocationMarker)
|
|
|
|
|
{
|
|
|
|
|
m_isLocationMarker = newIsLocationMarker;
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
2019-10-01 09:11:02 +02:00
|
|
|
if (TextDocument *document = TextDocument::textDocumentForFilePath(mark->fileName()))
|
2019-02-14 11:07:33 +01: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;
|
|
|
|
|
|
2022-05-17 10:31:15 +02:00
|
|
|
const QSet<TextMark *> marks = m_marks.value(document->filePath());
|
|
|
|
|
for (TextMark *mark : marks)
|
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
|
|
|
|
2021-06-11 14:34:34 +02:00
|
|
|
void TextMarkRegistry::documentRenamed(IDocument *document,
|
|
|
|
|
const FilePath &oldPath,
|
|
|
|
|
const FilePath &newPath)
|
2012-03-12 16:56:25 +01:00
|
|
|
{
|
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;
|
2021-06-11 14:34:34 +02:00
|
|
|
if (!m_marks.contains(oldPath))
|
2012-03-12 16:56:25 +01:00
|
|
|
return;
|
|
|
|
|
|
2014-07-19 11:27:28 +02:00
|
|
|
QSet<TextMark *> toBeMoved;
|
2022-05-17 10:31:15 +02:00
|
|
|
const QList<TextMark *> marks = baseTextDocument->marks();
|
|
|
|
|
for (TextMark *mark : marks)
|
2014-07-19 11:27:28 +02:00
|
|
|
toBeMoved.insert(mark);
|
2012-03-12 16:56:25 +01:00
|
|
|
|
2021-06-11 14:34:34 +02:00
|
|
|
m_marks[oldPath].subtract(toBeMoved);
|
|
|
|
|
m_marks[newPath].unite(toBeMoved);
|
2012-03-12 16:56:25 +01:00
|
|
|
|
2022-10-07 14:46:06 +02:00
|
|
|
for (TextMark *mark : std::as_const(toBeMoved))
|
2021-06-11 14:34:34 +02:00
|
|
|
mark->updateFileName(newPath);
|
2012-03-12 16:56:25 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-11 14:34:34 +02:00
|
|
|
void TextMarkRegistry::allDocumentsRenamed(const FilePath &oldPath, const FilePath &newPath)
|
2012-03-12 16:56:25 +01:00
|
|
|
{
|
2021-06-11 14:34:34 +02:00
|
|
|
if (!m_marks.contains(oldPath))
|
2012-03-12 16:56:25 +01:00
|
|
|
return;
|
|
|
|
|
|
2022-05-17 10:31:15 +02:00
|
|
|
const QSet<TextMark *> oldFileNameMarks = m_marks.value(oldPath);
|
2012-03-12 16:56:25 +01:00
|
|
|
|
2021-06-11 14:34:34 +02:00
|
|
|
m_marks[newPath].unite(oldFileNameMarks);
|
|
|
|
|
m_marks[oldPath].clear();
|
2012-03-12 16:56:25 +01:00
|
|
|
|
2022-05-17 10:31:15 +02:00
|
|
|
for (TextMark *mark : oldFileNameMarks)
|
2021-06-11 14:34:34 +02:00
|
|
|
mark->updateFileName(newPath);
|
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;
|
2021-07-09 14:32:41 +02:00
|
|
|
colors.rectColor.setAlphaF(0.15f);
|
2017-07-19 09:19:29 +02:00
|
|
|
|
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"
|