TextEditor: Add a mechanism to provide dynamic icons and tooltips

... to TextMarks more easily by allowing callbacks in addition to
fixed values.

Change-Id: Ica9e7d54dd1a53a59dc11812857402d0b7594d6c
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2020-09-24 10:14:57 +02:00
parent cc502e3667
commit 94df9822f9
2 changed files with 49 additions and 10 deletions

View File

@@ -125,7 +125,7 @@ int TextMark::lineNumber() const
void TextMark::paintIcon(QPainter *painter, const QRect &rect) const
{
m_icon.paint(painter, rect, Qt::AlignCenter);
icon().paint(painter, rect, Qt::AlignCenter);
}
void TextMark::paintAnnotation(QPainter &painter, QRectF *annotationRect,
@@ -180,7 +180,7 @@ TextMark::AnnotationRects TextMark::annotationRects(const QRectF &boundingRect,
rects.fadeInRect.setWidth(fadeInOffset);
rects.annotationRect = boundingRect;
rects.annotationRect.setLeft(rects.fadeInRect.right());
const bool drawIcon = !m_icon.isNull();
const bool drawIcon = !icon().isNull();
constexpr qreal margin = 1;
rects.iconRect = QRectF(rects.annotationRect.left(), boundingRect.top(),
0, boundingRect.height());
@@ -284,9 +284,10 @@ void TextMark::addToToolTipLayout(QGridLayout *target) const
// Left column: text mark icon
const int row = target->rowCount();
if (!m_icon.isNull()) {
const QIcon icon = this->icon();
if (!icon.isNull()) {
auto iconLabel = new QLabel;
iconLabel->setPixmap(m_icon.pixmap(16, 16));
iconLabel->setPixmap(icon.pixmap(16, 16));
target->addWidget(iconLabel, row, 0, Qt::AlignTop | Qt::AlignHCenter);
}
@@ -315,8 +316,10 @@ void TextMark::addToToolTipLayout(QGridLayout *target) const
bool TextMark::addToolTipContent(QLayout *target) const
{
QString text = m_toolTip;
bool useDefaultToolTip = false;
QString text = toolTip();
if (text.isEmpty()) {
useDefaultToolTip = true;
text = m_defaultToolTip;
if (text.isEmpty())
return false;
@@ -326,12 +329,28 @@ bool TextMark::addToolTipContent(QLayout *target) const
textLabel->setOpenExternalLinks(true);
textLabel->setText(text);
// Differentiate between tool tips that where explicitly set and default tool tips.
textLabel->setEnabled(!m_toolTip.isEmpty());
textLabel->setDisabled(useDefaultToolTip);
target->addWidget(textLabel);
return true;
}
void TextMark::setIcon(const QIcon &icon)
{
m_icon = icon;
m_iconProvider = std::function<QIcon()>();
}
void TextMark::setIconProvider(const std::function<QIcon ()> &iconProvider)
{
m_iconProvider = iconProvider;
}
const QIcon TextMark::icon() const
{
return m_iconProvider ? m_iconProvider() : m_icon;
}
Utils::optional<Theme::Color> TextMark::color() const
{
return m_color;
@@ -342,6 +361,22 @@ void TextMark::setColor(const Theme::Color &color)
m_color = color;
}
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()>();
}
QVector<QAction *> TextMark::actions() const
{
return m_actions;