Terminal: Fix warnings about re-registered action

Especially on Linux the pointer value of the Terminal might be reused,
leading to warnings about actions being registered for the same context.

Cleaning up the registration fixes this.

Change-Id: Ie1d53bf79581e9f98576e7a4e70420ec63da0f86
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Marcus Tillmanns
2023-06-06 08:23:09 +02:00
parent cdf7b63218
commit 886ca55b5a
2 changed files with 41 additions and 23 deletions

View File

@@ -252,23 +252,40 @@ void TerminalWidget::setupColors()
update(); update();
} }
static RegisteredAction registerAction(Id commandId, const Context &context)
{
QAction *action = new QAction;
ActionManager::registerAction(action, commandId, context);
return RegisteredAction(action, [commandId](QAction *a) {
ActionManager::unregisterAction(a, commandId);
delete a;
});
}
void TerminalWidget::setupActions() void TerminalWidget::setupActions()
{ {
ActionManager::registerAction(&m_copy, Constants::COPY, m_context); m_copy = registerAction(Constants::COPY, m_context);
ActionManager::registerAction(&m_paste, Constants::PASTE, m_context); m_paste = registerAction(Constants::PASTE, m_context);
ActionManager::registerAction(&m_close, Core::Constants::CLOSE, m_context); m_close = registerAction(Core::Constants::CLOSE, m_context);
ActionManager::registerAction(&m_clearTerminal, Constants::CLEAR_TERMINAL, m_context); m_clearTerminal = registerAction(Constants::CLEAR_TERMINAL, m_context);
ActionManager::registerAction(&m_clearSelection, Constants::CLEARSELECTION, m_context); m_clearSelection = registerAction(Constants::CLEARSELECTION, m_context);
ActionManager::registerAction(&m_moveCursorWordLeft, Constants::MOVECURSORWORDLEFT, m_context); m_moveCursorWordLeft = registerAction(Constants::MOVECURSORWORDLEFT, m_context);
ActionManager::registerAction(&m_moveCursorWordRight, Constants::MOVECURSORWORDRIGHT, m_context); m_moveCursorWordRight = registerAction(Constants::MOVECURSORWORDRIGHT, m_context);
connect(&m_copy, &QAction::triggered, this, &TerminalWidget::copyToClipboard); connect(m_copy.get(), &QAction::triggered, this, &TerminalWidget::copyToClipboard);
connect(&m_paste, &QAction::triggered, this, &TerminalWidget::pasteFromClipboard); connect(m_paste.get(), &QAction::triggered, this, &TerminalWidget::pasteFromClipboard);
connect(&m_close, &QAction::triggered, this, &TerminalWidget::closeTerminal); connect(m_close.get(), &QAction::triggered, this, &TerminalWidget::closeTerminal);
connect(&m_clearTerminal, &QAction::triggered, this, &TerminalWidget::clearContents); connect(m_clearTerminal.get(), &QAction::triggered, this, &TerminalWidget::clearContents);
connect(&m_clearSelection, &QAction::triggered, this, &TerminalWidget::clearSelection); connect(m_clearSelection.get(), &QAction::triggered, this, &TerminalWidget::clearSelection);
connect(&m_moveCursorWordLeft, &QAction::triggered, this, &TerminalWidget::moveCursorWordLeft); connect(m_moveCursorWordLeft.get(),
connect(&m_moveCursorWordRight, &QAction::triggered, this, &TerminalWidget::moveCursorWordRight); &QAction::triggered,
this,
&TerminalWidget::moveCursorWordLeft);
connect(m_moveCursorWordRight.get(),
&QAction::triggered,
this,
&TerminalWidget::moveCursorWordRight);
m_exit = unlockGlobalAction(Core::Constants::EXIT, m_context); m_exit = unlockGlobalAction(Core::Constants::EXIT, m_context);
m_options = unlockGlobalAction(Core::Constants::OPTIONS, m_context); m_options = unlockGlobalAction(Core::Constants::OPTIONS, m_context);
@@ -401,7 +418,7 @@ void TerminalWidget::updateCopyState()
if (!hasFocus()) if (!hasFocus())
return; return;
m_copy.setEnabled(m_selection.has_value()); m_copy->setEnabled(m_selection.has_value());
} }
void TerminalWidget::setFont(const QFont &font) void TerminalWidget::setFont(const QFont &font)
@@ -1095,7 +1112,7 @@ void TerminalWidget::keyPressEvent(QKeyEvent *event)
} }
if (m_selection) if (m_selection)
m_clearSelection.trigger(); m_clearSelection->trigger();
else { else {
QAction *returnAction = ActionManager::command(Core::Constants::S_RETURNTOEDITOR) QAction *returnAction = ActionManager::command(Core::Constants::S_RETURNTOEDITOR)
->actionForContext(Core::Constants::C_GLOBAL); ->actionForContext(Core::Constants::C_GLOBAL);

View File

@@ -25,6 +25,7 @@
namespace Terminal { namespace Terminal {
using UnlockedGlobalAction = std::unique_ptr<QAction, std::function<void(QAction *)>>; using UnlockedGlobalAction = std::unique_ptr<QAction, std::function<void(QAction *)>>;
using RegisteredAction = std::unique_ptr<QAction, std::function<void(QAction *)>>;
class TerminalWidget : public QAbstractScrollArea class TerminalWidget : public QAbstractScrollArea
{ {
@@ -239,13 +240,13 @@ private:
Aggregation::Aggregate *m_aggregate{nullptr}; Aggregation::Aggregate *m_aggregate{nullptr};
SearchHit m_lastSelectedHit{}; SearchHit m_lastSelectedHit{};
QAction m_copy; RegisteredAction m_copy;
QAction m_paste; RegisteredAction m_paste;
QAction m_clearSelection; RegisteredAction m_clearSelection;
QAction m_clearTerminal; RegisteredAction m_clearTerminal;
QAction m_moveCursorWordLeft; RegisteredAction m_moveCursorWordLeft;
QAction m_moveCursorWordRight; RegisteredAction m_moveCursorWordRight;
QAction m_close; RegisteredAction m_close;
UnlockedGlobalAction m_findInDocument; UnlockedGlobalAction m_findInDocument;
UnlockedGlobalAction m_exit; UnlockedGlobalAction m_exit;