forked from qt-creator/qt-creator
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:
@@ -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;
|
||||
|
@@ -97,8 +97,9 @@ public:
|
||||
void addToToolTipLayout(QGridLayout *target) const;
|
||||
virtual bool addToolTipContent(QLayout *target) const;
|
||||
|
||||
void setIcon(const QIcon &icon) { m_icon = icon; }
|
||||
const QIcon &icon() const { return m_icon; }
|
||||
void setIcon(const QIcon &icon);
|
||||
void setIconProvider(const std::function<QIcon()> &iconProvider);
|
||||
const QIcon icon() const;
|
||||
// call this if the icon has changed.
|
||||
void updateMarker();
|
||||
Priority priority() const { return m_priority;}
|
||||
@@ -121,8 +122,9 @@ public:
|
||||
QString lineAnnotation() const { return m_lineAnnotation; }
|
||||
void setLineAnnotation(const QString &lineAnnotation) { m_lineAnnotation = lineAnnotation; }
|
||||
|
||||
QString toolTip() const { return m_toolTip; }
|
||||
void setToolTip(const QString &toolTip) { m_toolTip = toolTip; }
|
||||
QString toolTip() const;
|
||||
void setToolTip(const QString &toolTip);
|
||||
void setToolTipProvider(const std::function<QString ()> &toolTipProvider);
|
||||
|
||||
QVector<QAction *> actions() const;
|
||||
void setActions(const QVector<QAction *> &actions); // Takes ownership
|
||||
@@ -135,12 +137,14 @@ private:
|
||||
int m_lineNumber = 0;
|
||||
Priority m_priority = LowPriority;
|
||||
QIcon m_icon;
|
||||
std::function<QIcon()> m_iconProvider;
|
||||
Utils::optional<Utils::Theme::Color> m_color;
|
||||
bool m_visible = false;
|
||||
Utils::Id m_category;
|
||||
double m_widthFactor = 1.0;
|
||||
QString m_lineAnnotation;
|
||||
QString m_toolTip;
|
||||
std::function<QString()> m_toolTipProvider;
|
||||
QString m_defaultToolTip;
|
||||
QVector<QAction *> m_actions;
|
||||
};
|
||||
|
Reference in New Issue
Block a user