ImageViewer: Use standard plugin setup pattern

Change-Id: Iab9536115b243563bb9a4bfd56b9d211a45f1b0b
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
hjk
2020-01-22 15:37:47 +01:00
parent eea2ff5f79
commit 3a5cb65128
4 changed files with 98 additions and 102 deletions

View File

@@ -29,110 +29,117 @@
#include "imageviewerfactory.h"
#include "imageviewerconstants.h"
#include <QAction>
#include <QCoreApplication>
#include <coreplugin/icore.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/id.h>
#include <QAction>
#include <QCoreApplication>
#include <QKeySequence>
using namespace Core;
namespace ImageViewer {
namespace Internal {
///////////////////////////////// ImageViewerPlugin //////////////////////////////////
class ImageViewerAction final : public QAction
{
public:
ImageViewerAction(Id id,
const std::function<void(ImageViewer *v)> &onTriggered,
const QString &title = {},
const QKeySequence &key = {})
: QAction(title)
{
Command *command = ActionManager::registerAction(this, id, Context(Constants::IMAGEVIEWER_ID));
if (!key.isEmpty())
command->setDefaultKeySequence(key);
connect(this, &QAction::triggered, this, [onTriggered] {
if (auto iv = qobject_cast<ImageViewer *>(EditorManager::currentEditor()))
onTriggered(iv);
});
}
};
class ImageViewerPluginPrivate final
{
Q_DECLARE_TR_FUNCTIONS(Imageviewer::Internal::ImageViewerPlugin)
public:
ImageViewerFactory imageViewerFactory;
ImageViewerAction zoomInAction {
Core::Constants::ZOOM_IN,
&ImageViewer::zoomIn
};
ImageViewerAction zoomOutAction {
Core::Constants::ZOOM_OUT,
&ImageViewer::zoomOut
};
ImageViewerAction zoomResetAction {
Core::Constants::ZOOM_RESET,
&ImageViewer::resetToOriginalSize
};
ImageViewerAction fitToScreenAction {
Constants::ACTION_FIT_TO_SCREEN,
&ImageViewer::fitToScreen,
tr("Fit to Screen"),
tr("Ctrl+=")
};
ImageViewerAction switchBackgroundAction {
Constants::ACTION_BACKGROUND,
&ImageViewer::switchViewBackground,
tr("Switch Background"),
tr("Ctrl+[")
};
ImageViewerAction switchOutlineAction {
Constants::ACTION_OUTLINE,
&ImageViewer::switchViewOutline,
tr("Switch Outline"),
tr("Ctrl+]")
};
ImageViewerAction toggleAnimationAction {
Constants::ACTION_TOGGLE_ANIMATION,
&ImageViewer::togglePlay,
tr("Toggle Animation")
};
ImageViewerAction exportImageAction {
Constants::ACTION_EXPORT_IMAGE,
&ImageViewer::exportImage,
tr("Export Image")
};
ImageViewerAction exportMulitImagesAction {
Constants::ACTION_EXPORT_MULTI_IMAGES,
&ImageViewer::exportMultiImages,
tr("Export Multiple Images"),
};
};
ImageViewerPlugin::~ImageViewerPlugin()
{
delete d;
}
bool ImageViewerPlugin::initialize(const QStringList &arguments, QString *errorMessage)
{
Q_UNUSED(arguments)
Q_UNUSED(errorMessage)
(void) new ImageViewerFactory(this);
d = new ImageViewerPluginPrivate;
return true;
}
static inline ImageViewer *currentImageViewer()
{
return qobject_cast<ImageViewer *>(Core::EditorManager::currentEditor());
}
void ImageViewerPlugin::extensionsInitialized()
{
QAction *a = registerNewAction(Core::Constants::ZOOM_IN);
connect(a, &QAction::triggered, this, []() {
if (ImageViewer *iv = currentImageViewer())
iv->zoomIn();
});
a = registerNewAction(Core::Constants::ZOOM_OUT);
connect(a, &QAction::triggered, this, []() {
if (ImageViewer *iv = currentImageViewer())
iv->zoomOut();
});
a = registerNewAction(Core::Constants::ZOOM_RESET);
connect(a, &QAction::triggered, this, []() {
if (ImageViewer *iv = currentImageViewer())
iv->resetToOriginalSize();
});
a = registerNewAction(Constants::ACTION_FIT_TO_SCREEN, tr("Fit to Screen"),
QKeySequence(tr("Ctrl+=")));
connect(a, &QAction::triggered, this, []() {
if (ImageViewer *iv = currentImageViewer())
iv->fitToScreen();
});
a = registerNewAction(Constants::ACTION_BACKGROUND, tr("Switch Background"),
QKeySequence(tr("Ctrl+[")));
connect(a, &QAction::triggered, this, []() {
if (ImageViewer *iv = currentImageViewer())
iv->switchViewBackground();
});
a = registerNewAction(Constants::ACTION_OUTLINE, tr("Switch Outline"),
QKeySequence(tr("Ctrl+]")));
connect(a, &QAction::triggered, this, []() {
if (ImageViewer *iv = currentImageViewer())
iv->switchViewOutline();
});
a = registerNewAction(Constants::ACTION_TOGGLE_ANIMATION, tr("Toggle Animation"),
QKeySequence());
connect(a, &QAction::triggered, this, []() {
if (ImageViewer *iv = currentImageViewer())
iv->togglePlay();
});
a = registerNewAction(Constants::ACTION_EXPORT_IMAGE, tr("Export Image"),
QKeySequence());
connect(a, &QAction::triggered, this, []() {
if (ImageViewer *iv = currentImageViewer())
iv->exportImage();
});
a = registerNewAction(Constants::ACTION_EXPORT_MULTI_IMAGES, tr("Export Multiple Images"),
QKeySequence());
connect(a, &QAction::triggered, this, []() {
if (ImageViewer *iv = currentImageViewer())
iv->exportMultiImages();
});
}
QAction *ImageViewerPlugin::registerNewAction(Core::Id id,
const QString &title, const QKeySequence &key)
{
Core::Context context(Constants::IMAGEVIEWER_ID);
auto action = new QAction(title, this);
Core::Command *command = Core::ActionManager::registerAction(action, id, context);
if (!key.isEmpty())
command->setDefaultKeySequence(key);
return action;
}
} // namespace Internal
} // namespace ImageViewer