Remove CommandButton

The same can be achieve with a factory method in `Command`, similar to
the existing ones for actions and buttons which synchronize their tool
tips with the shortcut.

Change-Id: I7e17654706b902dfa14f37b958fc2a60705d5cb5
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Eike Ziller
2024-01-25 08:59:18 +01:00
parent 34087df11e
commit acedf93ba3
14 changed files with 73 additions and 175 deletions

View File

@@ -11,8 +11,6 @@ add_qtc_plugin(Core
actionmanager/command.cpp
actionmanager/command.h
actionmanager/command_p.h
actionmanager/commandbutton.cpp
actionmanager/commandbutton.h
actionmanager/commandmappings.cpp
actionmanager/commandmappings.h
actionmanager/commandsfile.cpp

View File

@@ -570,6 +570,21 @@ QAction *Command::createActionWithShortcutToolTip(Id commandId, QObject *parent)
return a;
}
/*!
Returns a new QToolButton with the command's icon, icon text, and text, that
given by \a commandId. Sets the button's parent to \a parent.
The action's tool tip is the action's text, augmented with the command's
main keyboard shortcut. Other properties of the button are not updated
automatically.
\sa createActionWithShortcutToolTip()
*/
QToolButton *Command::createToolButtonWithShortcutToolTip(Utils::Id commandId, QWidget *parent)
{
auto button = new QToolButton(parent);
button->setDefaultAction(createActionWithShortcutToolTip(commandId, button));
return button;
}
/*!
Returns a tool button for \a action.
@@ -584,4 +599,15 @@ QToolButton *Command::toolButtonWithAppendedShortcut(QAction *action, Command *c
return button;
}
/*!
Returns a tool button for \a action.
Appends the main keyboard shortcut of the command with ID \a commandId
to the tool tip of the button.
*/
QToolButton *Command::toolButtonWithAppendedShortcut(QAction *action, Utils::Id commandId)
{
return toolButtonWithAppendedShortcut(action, ActionManager::command(commandId));
}
} // namespace Core

View File

@@ -72,7 +72,10 @@ public:
void augmentActionWithShortcutToolTip(QAction *action) const;
static QAction *createActionWithShortcutToolTip(Utils::Id commandId, QObject *parent);
static QToolButton *createToolButtonWithShortcutToolTip(Utils::Id commandId,
QWidget *parent = nullptr);
static QToolButton *toolButtonWithAppendedShortcut(QAction *action, Command *cmd);
static QToolButton *toolButtonWithAppendedShortcut(QAction *action, Utils::Id commandId);
bool isScriptable() const;
bool isScriptable(const Context &) const;

View File

@@ -1,88 +0,0 @@
// Copyright (C) 2016 Konstantin Tokarev.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "commandbutton.h"
#include "actionmanager.h"
#include "command.h"
#include <utils/proxyaction.h>
#include <utils/qtcassert.h>
using namespace Core;
using namespace Utils;
/*!
\class Core::CommandButton
\inheaderfile coreplugin/actionmanager/commandbutton.h
\inmodule QtCreator
\brief The CommandButton class is an action associated with one of
the registered Command objects.
The tooltip of the button consists of toolTipBase property value and Command's
key sequence which is automatically updated when user changes it.
*/
/*!
\internal
*/
CommandButton::CommandButton(QWidget *parent)
: QToolButton(parent)
{}
/*!
\internal
*/
CommandButton::CommandButton(Utils::Id id, QWidget *parent)
: QToolButton(parent)
{
setCommandId(id);
}
void CommandButton::setCommandId(Utils::Id id)
{
if (m_command)
disconnect(m_command.data(),
&Command::keySequenceChanged,
this,
&CommandButton::updateToolTip);
m_command = ActionManager::command(id);
QTC_ASSERT(m_command, return);
if (m_toolTipBase.isEmpty())
m_toolTipBase = m_command->description();
updateToolTip();
connect(m_command.data(), &Command::keySequenceChanged, this, &CommandButton::updateToolTip);
}
/*!
The base tool tip that is extended with the command's shortcut.
Defaults to the command's description.
\sa Command::description()
*/
QString CommandButton::toolTipBase() const
{
return m_toolTipBase;
}
/*!
Sets the base tool tip that is extended with the command's shortcut to
\a toolTipBase.
\sa toolTipBase()
*/
void CommandButton::setToolTipBase(const QString &toolTipBase)
{
m_toolTipBase = toolTipBase;
updateToolTip();
}
void CommandButton::updateToolTip()
{
if (m_command)
setToolTip(Utils::ProxyAction::stringWithAppendedShortcut(m_toolTipBase,
m_command->keySequence()));
}

View File

@@ -1,36 +0,0 @@
// Copyright (C) 2016 Konstantin Tokarev.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once
#include "../core_global.h"
#include <utils/id.h>
#include <QAction>
#include <QPointer>
#include <QString>
#include <QToolButton>
namespace Core {
class Command;
class CORE_EXPORT CommandButton : public QToolButton
{
Q_OBJECT
public:
explicit CommandButton(QWidget *parent = nullptr);
explicit CommandButton(Utils::Id id, QWidget *parent = nullptr);
void setCommandId(Utils::Id id);
QString toolTipBase() const;
void setToolTipBase(const QString &toolTipBase);
private:
void updateToolTip();
QPointer<Command> m_command;
QString m_toolTipBase;
};
}

View File

@@ -186,7 +186,6 @@ QtcPlugin {
"actioncontainer.cpp", "actioncontainer.h", "actioncontainer_p.h",
"actionmanager.cpp", "actionmanager.h", "actionmanager_p.h",
"command.cpp", "command.h", "command_p.h",
"commandbutton.cpp", "commandbutton.h",
"commandmappings.cpp", "commandmappings.h",
"commandsfile.cpp", "commandsfile.h",
]

View File

@@ -10,7 +10,6 @@
#include <utils/id.h>
namespace Core {
class CommandButton;
class IContext;
class OutputWindow;
@@ -103,8 +102,8 @@ private:
Utils::Id m_id;
QString m_displayName;
int m_priority = -1;
Core::CommandButton * const m_zoomInButton;
Core::CommandButton * const m_zoomOutButton;
QToolButton *m_zoomInButton;
QToolButton *m_zoomOutButton;
Utils::FancyLineEdit *m_filterOutputLineEdit = nullptr;
bool m_filterRegexp = false;
bool m_invertFilter = false;

View File

@@ -6,7 +6,6 @@
#include "actionmanager/actioncontainer.h"
#include "actionmanager/actionmanager.h"
#include "actionmanager/command.h"
#include "actionmanager/commandbutton.h"
#include "coreplugintr.h"
#include "editormanager/editormanager.h"
#include "editormanager/ieditor.h"
@@ -67,20 +66,18 @@ static bool g_managerConstructed = false; // For debugging reasons.
// OutputPane
IOutputPane::IOutputPane(QObject *parent)
: QObject(parent),
m_zoomInButton(new Core::CommandButton),
m_zoomOutButton(new Core::CommandButton)
: QObject(parent)
{
// We need all pages first. Ignore latecomers and shout.
QTC_ASSERT(!g_managerConstructed, return);
g_outputPanes.append(OutputPaneData(this));
m_zoomInButton = Command::createToolButtonWithShortcutToolTip(Constants::ZOOM_IN);
m_zoomInButton->setIcon(Utils::Icons::PLUS_TOOLBAR.icon());
m_zoomInButton->setCommandId(Constants::ZOOM_IN);
connect(m_zoomInButton, &QToolButton::clicked, this, [this] { emit zoomInRequested(1); });
m_zoomOutButton = Command::createToolButtonWithShortcutToolTip(Constants::ZOOM_OUT);
m_zoomOutButton->setIcon(Utils::Icons::MINUS_TOOLBAR.icon());
m_zoomOutButton->setCommandId(Constants::ZOOM_OUT);
connect(m_zoomOutButton, &QToolButton::clicked, this, [this] { emit zoomOutRequested(1); });
}

View File

@@ -8,7 +8,7 @@
#include "haskellmanager.h"
#include "haskelltr.h"
#include <coreplugin/actionmanager/commandbutton.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/coreplugintr.h>
#include <texteditor/textdocument.h>
@@ -23,8 +23,8 @@ namespace Haskell::Internal {
static QWidget *createEditorWidget()
{
auto widget = new TextEditorWidget;
auto ghciButton = new Core::CommandButton(Constants::A_RUN_GHCI, widget);
ghciButton->setText(Tr::tr("GHCi"));
auto ghciButton = Core::Command::createToolButtonWithShortcutToolTip(Constants::A_RUN_GHCI);
ghciButton->defaultAction()->setIconText(Tr::tr("GHCi"));
QObject::connect(ghciButton, &QToolButton::clicked, widget, [widget] {
openGhci(widget->textDocument()->filePath());
});

View File

@@ -52,7 +52,6 @@
#include <coreplugin/icore.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/minisplitter.h>
#include <coreplugin/actionmanager/commandbutton.h>
#include <utils/fadingindicator.h>
#include <utils/fileutils.h>
#include <utils/layoutbuilder.h>
@@ -71,18 +70,19 @@
#include <QImageWriter>
#include <QLabel>
#include <QMap>
#include <QMenu>
#include <QMessageBox>
#include <QPainter>
#include <QPixmap>
#include <QScrollArea>
#include <QScrollBar>
#include <QStackedWidget>
#include <QStyleFactory>
#include <QTimer>
#include <QToolBox>
#include <QToolButton>
#include <QUndoStack>
#include <QVBoxLayout>
#include <QMenu>
#include <QScrollBar>
#include <algorithm>
@@ -297,8 +297,9 @@ void ModelEditor::init()
toolbarLayout->setContentsMargins(0, 0, 0, 0);
toolbarLayout->setSpacing(0);
auto openParentButton = new CommandButton(Constants::OPEN_PARENT_DIAGRAM, d->toolbar);
openParentButton->setDefaultAction(d->actionHandler->openParentDiagramAction());
auto openParentButton
= Command::toolButtonWithAppendedShortcut(d->actionHandler->openParentDiagramAction(),
Constants::OPEN_PARENT_DIAGRAM);
toolbarLayout->addWidget(openParentButton);
d->diagramSelector = new QComboBox(d->toolbar);
@@ -327,8 +328,9 @@ void ModelEditor::init()
d->toolbar));
toolbarLayout->addSpacing(20);
auto syncToggleButton = new CommandButton(Constants::ACTION_SYNC_BROWSER, d->toolbar);
syncToggleButton->setDefaultAction(d->actionHandler->synchronizeBrowserAction());
auto syncToggleButton
= Command::toolButtonWithAppendedShortcut(d->actionHandler->synchronizeBrowserAction(),
Constants::ACTION_SYNC_BROWSER);
QMenu *syncMenu = new QMenu(syncToggleButton);
QActionGroup *syncGroup = new QActionGroup(syncMenu);
d->syncBrowserWithDiagramAction = syncMenu->addAction(Tr::tr("Synchronize Structure with Diagram"));

View File

@@ -21,7 +21,7 @@
#include "targetsettingspanel.h"
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/commandbutton.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/coreicons.h>
#include <coreplugin/coreplugintr.h>
@@ -56,6 +56,7 @@
#include <QPushButton>
#include <QStyledItemDelegate>
#include <QTimer>
#include <QToolButton>
#include <QTreeView>
#include <QVBoxLayout>
@@ -136,12 +137,14 @@ BuildSystemOutputWindow::BuildSystemOutputWindow()
setBaseFont(TextEditor::TextEditorSettings::fontSettings().font());
m_zoomIn.setIcon(Utils::Icons::PLUS_TOOLBAR.icon());
m_zoomIn.setText(ActionManager::command(Core::Constants::ZOOM_IN)->action()->text());
connect(&m_zoomIn, &QAction::triggered, this, [this] { zoomIn(); });
ActionManager::registerAction(&m_zoomIn,
Core::Constants::ZOOM_IN,
Context(kBuildSystemOutputContext));
m_zoomOut.setIcon(Utils::Icons::MINUS_TOOLBAR.icon());
m_zoomOut.setText(ActionManager::command(Core::Constants::ZOOM_OUT)->action()->text());
connect(&m_zoomOut, &QAction::triggered, this, [this] { zoomOut(); });
ActionManager::registerAction(&m_zoomOut,
Core::Constants::ZOOM_OUT,
@@ -152,9 +155,8 @@ QWidget *BuildSystemOutputWindow::toolBar()
{
if (!m_toolBar) {
m_toolBar = new StyledBar(this);
auto clearButton = new CommandButton(Core::Constants::OUTPUTPANE_CLEAR);
clearButton->setDefaultAction(&m_clear);
clearButton->setToolTipBase(m_clear.text());
auto clearButton
= Command::toolButtonWithAppendedShortcut(&m_clear, Core::Constants::OUTPUTPANE_CLEAR);
m_filterOutputLineEdit = new FancyLineEdit;
m_filterOutputLineEdit->setButtonVisible(FancyLineEdit::Left, true);
@@ -178,10 +180,10 @@ QWidget *BuildSystemOutputWindow::toolBar()
popup->show();
});
auto zoomInButton = new CommandButton(Core::Constants::ZOOM_IN);
zoomInButton->setDefaultAction(&m_zoomIn);
auto zoomOutButton = new CommandButton(Core::Constants::ZOOM_OUT);
zoomOutButton->setDefaultAction(&m_zoomOut);
auto zoomInButton = Command::toolButtonWithAppendedShortcut(&m_zoomIn,
Core::Constants::ZOOM_IN);
auto zoomOutButton = Command::toolButtonWithAppendedShortcut(&m_zoomOut,
Core::Constants::ZOOM_OUT);
auto layout = new QHBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);

View File

@@ -15,7 +15,6 @@
#include "pythonutils.h"
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/commandbutton.h>
#include <coreplugin/coreplugintr.h>
#include <coreplugin/icore.h>

View File

@@ -10,7 +10,7 @@
#include "qrceditor/resourcefile_p.h"
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/commandbutton.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/coreplugintr.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditorfactory.h>
@@ -27,6 +27,7 @@
#include <QDebug>
#include <QMenu>
#include <QToolBar>
#include <QToolButton>
using namespace Core;
using namespace Utils;
@@ -143,8 +144,8 @@ ResourceEditorImpl::ResourceEditorImpl()
setContext(Context(Constants::C_RESOURCEEDITOR));
setWidget(m_resourceEditor);
CommandButton *refreshButton = new CommandButton(Constants::REFRESH, m_toolBar);
refreshButton->setIcon(QIcon(QLatin1String(":/texteditor/images/finddocuments.png")));
QToolButton *refreshButton = Command::createToolButtonWithShortcutToolTip(Constants::REFRESH);
refreshButton->defaultAction()->setIcon(QIcon(":/texteditor/images/finddocuments.png"));
connect(refreshButton, &QAbstractButton::clicked, m_resourceEditor, &QrcEditor::refresh);
m_toolBar->addWidget(refreshButton);

View File

@@ -9,7 +9,6 @@
#include <aggregation/aggregate.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/commandbutton.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/coreplugintr.h>
#include <coreplugin/icore.h>
@@ -134,33 +133,31 @@ public:
}
agg->add(m_widget.get());
m_togglePreviewVisible = new CommandButton(TOGGLEPREVIEW_ACTION);
m_togglePreviewVisible->setText(m_togglePreviewVisible->toolTipBase());
m_togglePreviewVisible = Command::createToolButtonWithShortcutToolTip(TOGGLEPREVIEW_ACTION);
m_togglePreviewVisible->setCheckable(true);
m_togglePreviewVisible->setChecked(showPreview);
m_previewWidget->setVisible(showPreview);
m_toggleEditorVisible = new CommandButton(TOGGLEEDITOR_ACTION);
m_toggleEditorVisible->setText(m_toggleEditorVisible->toolTipBase());
m_toggleEditorVisible = Command::createToolButtonWithShortcutToolTip(TOGGLEEDITOR_ACTION);
m_toggleEditorVisible->setCheckable(true);
m_toggleEditorVisible->setChecked(showEditor);
m_textEditorWidget->setVisible(showEditor);
auto button = new CommandButton(EMPHASIS_ACTION);
button->setText("i");
auto button = Command::createToolButtonWithShortcutToolTip(EMPHASIS_ACTION);
button->defaultAction()->setIconText("i");
button->setFont([button]{ auto f = button->font(); f.setItalic(true); return f; }());
connect(button, &QToolButton::clicked, this, &MarkdownEditor::triggerEmphasis);
m_markDownButtons.append(button);
button = new CommandButton(STRONG_ACTION);
button->setText("b");
button = Command::createToolButtonWithShortcutToolTip(STRONG_ACTION);
button->defaultAction()->setIconText("b");
button->setFont([button]{ auto f = button->font(); f.setBold(true); return f; }());
connect(button, &QToolButton::clicked, this, &MarkdownEditor::triggerStrong);
m_markDownButtons.append(button);
button = new CommandButton(INLINECODE_ACTION);
button->setText("`");
button = Command::createToolButtonWithShortcutToolTip(INLINECODE_ACTION);
button->defaultAction()->setIconText("`");
connect(button, &QToolButton::clicked, this, &MarkdownEditor::triggerInlineCode);
m_markDownButtons.append(button);
button = new CommandButton(LINK_ACTION);
button = Command::createToolButtonWithShortcutToolTip(LINK_ACTION);
button->setIcon(Utils::Icons::LINK_TOOLBAR.icon());
connect(button, &QToolButton::clicked, this, &MarkdownEditor::triggerLink);
m_markDownButtons.append(button);
@@ -173,8 +170,7 @@ public:
for (auto button : m_markDownButtons | Utils::views::reverse)
m_textEditorWidget->insertExtraToolBarWidget(TextEditorWidget::Left, button);
m_swapViews = new CommandButton(SWAPVIEWS_ACTION);
m_swapViews->setText(m_swapViews->toolTipBase());
m_swapViews = Command::createToolButtonWithShortcutToolTip(SWAPVIEWS_ACTION);
m_swapViews->setEnabled(showEditor && showPreview);
m_swapViewsAction = m_textEditorWidget->insertExtraToolBarWidget(TextEditorWidget::Right, m_swapViews);
@@ -486,9 +482,9 @@ private:
TextEditorWidget *m_textEditorWidget;
TextDocumentPtr m_document;
QList<QToolButton *> m_markDownButtons;
CommandButton *m_toggleEditorVisible;
CommandButton *m_togglePreviewVisible;
CommandButton *m_swapViews;
QToolButton *m_toggleEditorVisible;
QToolButton *m_togglePreviewVisible;
QToolButton *m_swapViews;
QAction *m_toggleEditorVisibleAction;
QAction *m_togglePreviewVisibleAction;
QAction *m_swapViewsAction;