forked from qt-creator/qt-creator
TextMark: Delay context menu tooltip action creation
Change-Id: I25bad9a774a6d95162251c8f0512eb3f8439d9ae Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -309,31 +309,32 @@ ClangdTextMark::ClangdTextMark(const FilePath &filePath,
|
|||||||
client->addTask(createTask(m_diagnostic));
|
client->addTask(createTask(m_diagnostic));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy to clipboard action
|
setActionsProvider([diag = m_diagnostic] {
|
||||||
QVector<QAction *> actions;
|
// Copy to clipboard action
|
||||||
QAction *action = new QAction();
|
QList<QAction *> actions;
|
||||||
action->setIcon(QIcon::fromTheme("edit-copy", Icons::COPY.icon()));
|
QAction *action = new QAction();
|
||||||
action->setToolTip(tr("Copy to Clipboard", "Clang Code Model Marks"));
|
action->setIcon(QIcon::fromTheme("edit-copy", Icons::COPY.icon()));
|
||||||
QObject::connect(action, &QAction::triggered, [diag = m_diagnostic]() {
|
action->setToolTip(tr("Copy to Clipboard", "Clang Code Model Marks"));
|
||||||
const QString text = ClangDiagnosticWidget::createText({diag},
|
QObject::connect(action, &QAction::triggered, [diag] {
|
||||||
ClangDiagnosticWidget::InfoBar);
|
const QString text = ClangDiagnosticWidget::createText({diag},
|
||||||
setClipboardAndSelection(text);
|
ClangDiagnosticWidget::InfoBar);
|
||||||
});
|
setClipboardAndSelection(text);
|
||||||
actions << action;
|
|
||||||
|
|
||||||
// Remove diagnostic warning action
|
|
||||||
Project *project = projectForCurrentEditor();
|
|
||||||
if (project && isDiagnosticConfigChangable(project, m_diagnostic)) {
|
|
||||||
action = new QAction();
|
|
||||||
action->setIcon(Icons::BROKEN.icon());
|
|
||||||
action->setToolTip(tr("Disable Diagnostic in Current Project"));
|
|
||||||
QObject::connect(action, &QAction::triggered, [diag = m_diagnostic]() {
|
|
||||||
disableDiagnosticInCurrentProjectConfig(diag);
|
|
||||||
});
|
});
|
||||||
actions << action;
|
actions << action;
|
||||||
}
|
|
||||||
|
|
||||||
setActions(actions);
|
// Remove diagnostic warning action
|
||||||
|
Project *project = projectForCurrentEditor();
|
||||||
|
if (project && isDiagnosticConfigChangable(project, diag)) {
|
||||||
|
action = new QAction();
|
||||||
|
action->setIcon(Icons::BROKEN.icon());
|
||||||
|
action->setToolTip(tr("Disable Diagnostic in Current Project"));
|
||||||
|
QObject::connect(action, &QAction::triggered, [diag] {
|
||||||
|
disableDiagnosticInCurrentProjectConfig(diag);
|
||||||
|
});
|
||||||
|
actions << action;
|
||||||
|
}
|
||||||
|
return actions;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClangdTextMark::addToolTipContent(QLayout *target) const
|
bool ClangdTextMark::addToolTipContent(QLayout *target) const
|
||||||
|
@@ -54,28 +54,28 @@ DiagnosticMark::DiagnosticMark(const Diagnostic &diagnostic)
|
|||||||
setIcon(markIcon.isNull() ? Utils::Icons::CODEMODEL_WARNING.icon() : markIcon);
|
setIcon(markIcon.isNull() ? Utils::Icons::CODEMODEL_WARNING.icon() : markIcon);
|
||||||
setToolTip(createDiagnosticToolTipString(diagnostic, Utils::nullopt, true));
|
setToolTip(createDiagnosticToolTipString(diagnostic, Utils::nullopt, true));
|
||||||
setLineAnnotation(diagnostic.description);
|
setLineAnnotation(diagnostic.description);
|
||||||
|
setActionsProvider([diagnostic] {
|
||||||
|
// Copy to clipboard action
|
||||||
|
QList<QAction *> actions;
|
||||||
|
QAction *action = new QAction();
|
||||||
|
action->setIcon(QIcon::fromTheme("edit-copy", Utils::Icons::COPY.icon()));
|
||||||
|
action->setToolTip(tr("Copy to Clipboard"));
|
||||||
|
QObject::connect(action, &QAction::triggered, [diagnostic]() {
|
||||||
|
const QString text = createFullLocationString(diagnostic.location)
|
||||||
|
+ ": "
|
||||||
|
+ diagnostic.description;
|
||||||
|
Utils::setClipboardAndSelection(text);
|
||||||
|
});
|
||||||
|
actions << action;
|
||||||
|
|
||||||
// Copy to clipboard action
|
// Disable diagnostic action
|
||||||
QVector<QAction *> actions;
|
action = new QAction();
|
||||||
QAction *action = new QAction();
|
action->setIcon(Utils::Icons::BROKEN.icon());
|
||||||
action->setIcon(QIcon::fromTheme("edit-copy", Utils::Icons::COPY.icon()));
|
action->setToolTip(tr("Disable Diagnostic"));
|
||||||
action->setToolTip(tr("Copy to Clipboard"));
|
QObject::connect(action, &QAction::triggered, [diagnostic] { disableChecks({diagnostic}); });
|
||||||
QObject::connect(action, &QAction::triggered, [diagnostic]() {
|
actions << action;
|
||||||
const QString text = createFullLocationString(diagnostic.location)
|
return actions;
|
||||||
+ ": "
|
|
||||||
+ diagnostic.description;
|
|
||||||
Utils::setClipboardAndSelection(text);
|
|
||||||
});
|
});
|
||||||
actions << action;
|
|
||||||
|
|
||||||
// Disable diagnostic action
|
|
||||||
action = new QAction();
|
|
||||||
action->setIcon(Utils::Icons::BROKEN.icon());
|
|
||||||
action->setToolTip(tr("Disable Diagnostic"));
|
|
||||||
QObject::connect(action, &QAction::triggered, [diagnostic] { disableChecks({diagnostic}); });
|
|
||||||
actions << action;
|
|
||||||
|
|
||||||
setActions(actions);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiagnosticMark::disable()
|
void DiagnosticMark::disable()
|
||||||
|
@@ -80,19 +80,20 @@ CppcheckTextMark::CppcheckTextMark (const Diagnostic &diagnostic)
|
|||||||
setToolTip(toolTipText(diagnostic.severityText));
|
setToolTip(toolTipText(diagnostic.severityText));
|
||||||
setLineAnnotation(diagnostic.message);
|
setLineAnnotation(diagnostic.message);
|
||||||
setSettingsPage(Constants::OPTIONS_PAGE_ID);
|
setSettingsPage(Constants::OPTIONS_PAGE_ID);
|
||||||
|
setActionsProvider([diagnostic] {
|
||||||
// Copy to clipboard action
|
// Copy to clipboard action
|
||||||
QAction *action = new QAction();
|
QAction *action = new QAction;
|
||||||
action->setIcon(QIcon::fromTheme("edit-copy", Utils::Icons::COPY.icon()));
|
action->setIcon(QIcon::fromTheme("edit-copy", Utils::Icons::COPY.icon()));
|
||||||
action->setToolTip(TextMark::tr("Copy to Clipboard"));
|
action->setToolTip(TextMark::tr("Copy to Clipboard"));
|
||||||
QObject::connect(action, &QAction::triggered, [diagnostic]() {
|
QObject::connect(action, &QAction::triggered, [diagnostic]() {
|
||||||
const QString text = QString("%1:%2: %3")
|
const QString text = QString("%1:%2: %3")
|
||||||
.arg(diagnostic.fileName.toUserOutput())
|
.arg(diagnostic.fileName.toUserOutput())
|
||||||
.arg(diagnostic.lineNumber)
|
.arg(diagnostic.lineNumber)
|
||||||
.arg(diagnostic.message);
|
.arg(diagnostic.message);
|
||||||
QApplication::clipboard()->setText(text);
|
QApplication::clipboard()->setText(text);
|
||||||
|
});
|
||||||
|
return QList<QAction *>{action};
|
||||||
});
|
});
|
||||||
setActions({action});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString CppcheckTextMark::toolTipText(const QString &severityText) const
|
QString CppcheckTextMark::toolTipText(const QString &severityText) const
|
||||||
|
@@ -143,14 +143,16 @@ TextEditor::TextMark *DiagnosticManager::createTextMark(const FilePath &filePath
|
|||||||
{
|
{
|
||||||
static const auto icon = QIcon::fromTheme("edit-copy", Utils::Icons::COPY.icon());
|
static const auto icon = QIcon::fromTheme("edit-copy", Utils::Icons::COPY.icon());
|
||||||
static const QString tooltip = tr("Copy to Clipboard");
|
static const QString tooltip = tr("Copy to Clipboard");
|
||||||
QAction *action = new QAction();
|
|
||||||
action->setIcon(icon);
|
|
||||||
action->setToolTip(tooltip);
|
|
||||||
QObject::connect(action, &QAction::triggered, [text = diagnostic.message()]() {
|
|
||||||
QApplication::clipboard()->setText(text);
|
|
||||||
});
|
|
||||||
auto mark = new TextMark(filePath, diagnostic, m_client->id());
|
auto mark = new TextMark(filePath, diagnostic, m_client->id());
|
||||||
mark->setActions({action});
|
mark->setActionsProvider([text = diagnostic.message()] {
|
||||||
|
QAction *action = new QAction();
|
||||||
|
action->setIcon(icon);
|
||||||
|
action->setToolTip(tooltip);
|
||||||
|
QObject::connect(action, &QAction::triggered, [text] {
|
||||||
|
QApplication::clipboard()->setText(text);
|
||||||
|
});
|
||||||
|
return QList<QAction *>{action};
|
||||||
|
});
|
||||||
return mark;
|
return mark;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -97,9 +97,6 @@ TextMark::TextMark(const FilePath &fileName, int lineNumber, Id category, double
|
|||||||
|
|
||||||
TextMark::~TextMark()
|
TextMark::~TextMark()
|
||||||
{
|
{
|
||||||
qDeleteAll(m_actions);
|
|
||||||
m_actions.clear();
|
|
||||||
delete m_settingsAction;
|
|
||||||
if (!m_fileName.isEmpty())
|
if (!m_fileName.isEmpty())
|
||||||
TextMarkRegistry::remove(this);
|
TextMarkRegistry::remove(this);
|
||||||
if (m_baseTextDocument)
|
if (m_baseTextDocument)
|
||||||
@@ -311,19 +308,29 @@ void TextMark::addToToolTipLayout(QGridLayout *target) const
|
|||||||
target->addLayout(contentLayout, row, 1);
|
target->addLayout(contentLayout, row, 1);
|
||||||
|
|
||||||
// Right column: action icons/button
|
// Right column: action icons/button
|
||||||
QVector<QAction *> actions = m_actions;
|
QList<QAction *> actions{m_actions.begin(), m_actions.end()};
|
||||||
if (m_settingsAction)
|
if (m_actionsProvider)
|
||||||
actions << m_settingsAction;
|
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);
|
||||||
|
}
|
||||||
if (!actions.isEmpty()) {
|
if (!actions.isEmpty()) {
|
||||||
auto actionsLayout = new QHBoxLayout;
|
auto actionsLayout = new QHBoxLayout;
|
||||||
QMargins margins = actionsLayout->contentsMargins();
|
QMargins margins = actionsLayout->contentsMargins();
|
||||||
margins.setLeft(margins.left() + 5);
|
margins.setLeft(margins.left() + 5);
|
||||||
actionsLayout->setContentsMargins(margins);
|
actionsLayout->setContentsMargins(margins);
|
||||||
for (QAction *action : qAsConst(actions)) {
|
for (QAction *action : qAsConst(actions)) {
|
||||||
QTC_ASSERT(!action->icon().isNull(), continue);
|
QTC_ASSERT(!action->icon().isNull(), delete action; continue);
|
||||||
auto button = new QToolButton;
|
auto button = new QToolButton;
|
||||||
button->setIcon(action->icon());
|
button->setIcon(action->icon());
|
||||||
button->setToolTip(action->toolTip());
|
button->setToolTip(action->toolTip());
|
||||||
|
action->setParent(button);
|
||||||
QObject::connect(button, &QToolButton::clicked, action, &QAction::triggered);
|
QObject::connect(button, &QToolButton::clicked, action, &QAction::triggered);
|
||||||
QObject::connect(button, &QToolButton::clicked, []() {
|
QObject::connect(button, &QToolButton::clicked, []() {
|
||||||
Utils::ToolTip::hideImmediately();
|
Utils::ToolTip::hideImmediately();
|
||||||
@@ -423,15 +430,14 @@ void TextMark::setActions(const QVector<QAction *> &actions)
|
|||||||
m_actions = actions;
|
m_actions = actions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TextMark::setActionsProvider(const std::function<QList<QAction *>()> &actionsProvider)
|
||||||
|
{
|
||||||
|
m_actionsProvider = actionsProvider;
|
||||||
|
}
|
||||||
|
|
||||||
void TextMark::setSettingsPage(Id settingsPage)
|
void TextMark::setSettingsPage(Id settingsPage)
|
||||||
{
|
{
|
||||||
delete m_settingsAction;
|
m_settingsPage = settingsPage;
|
||||||
m_settingsAction = new QAction;
|
|
||||||
m_settingsAction->setIcon(Utils::Icons::SETTINGS_TOOLBAR.icon());
|
|
||||||
m_settingsAction->setToolTip(tr("Show Diagnostic Settings"));
|
|
||||||
QObject::connect(m_settingsAction, &QAction::triggered, Core::ICore::instance(),
|
|
||||||
[settingsPage] { Core::ICore::showOptionsDialog(settingsPage); },
|
|
||||||
Qt::QueuedConnection);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TextMarkRegistry::TextMarkRegistry(QObject *parent)
|
TextMarkRegistry::TextMarkRegistry(QObject *parent)
|
||||||
|
@@ -133,6 +133,7 @@ public:
|
|||||||
|
|
||||||
QVector<QAction *> actions() const;
|
QVector<QAction *> actions() const;
|
||||||
void setActions(const QVector<QAction *> &actions); // Takes ownership
|
void setActions(const QVector<QAction *> &actions); // Takes ownership
|
||||||
|
void setActionsProvider(const std::function<QList<QAction *>()> &actionsProvider); // Takes ownership
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void setSettingsPage(Utils::Id settingsPage);
|
void setSettingsPage(Utils::Id settingsPage);
|
||||||
@@ -154,8 +155,9 @@ private:
|
|||||||
QString m_toolTip;
|
QString m_toolTip;
|
||||||
std::function<QString()> m_toolTipProvider;
|
std::function<QString()> m_toolTipProvider;
|
||||||
QString m_defaultToolTip;
|
QString m_defaultToolTip;
|
||||||
QVector<QAction *> m_actions;
|
QVector<QAction *> m_actions; // FIXME Remove in master
|
||||||
QAction *m_settingsAction = nullptr;
|
std::function<QList<QAction *>()> m_actionsProvider;
|
||||||
|
Utils::Id m_settingsPage;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace TextEditor
|
} // namespace TextEditor
|
||||||
|
Reference in New Issue
Block a user