forked from qt-creator/qt-creator
ImageViewer: Use standard plugin setup pattern
Change-Id: Iab9536115b243563bb9a4bfd56b9d211a45f1b0b Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
@@ -29,15 +29,12 @@
|
||||
#include "imageviewer.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QMap>
|
||||
#include <QImageReader>
|
||||
#include <QDebug>
|
||||
|
||||
namespace ImageViewer {
|
||||
namespace Internal {
|
||||
|
||||
ImageViewerFactory::ImageViewerFactory(QObject *parent) :
|
||||
Core::IEditorFactory(parent)
|
||||
ImageViewerFactory::ImageViewerFactory()
|
||||
{
|
||||
setId(Constants::IMAGEVIEWER_ID);
|
||||
setDisplayName(QCoreApplication::translate("OpenWith::Editors", Constants::IMAGEVIEWER_DISPLAY_NAME));
|
||||
|
@@ -37,7 +37,7 @@ class ImageViewerFactory : public Core::IEditorFactory
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ImageViewerFactory(QObject *parent = nullptr);
|
||||
ImageViewerFactory();
|
||||
|
||||
Core::IEditor *createEditor() override;
|
||||
};
|
||||
|
@@ -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
|
||||
|
@@ -28,14 +28,6 @@
|
||||
|
||||
#include <extensionsystem/iplugin.h>
|
||||
|
||||
#include <QKeySequence>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QAction;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
namespace Core { class Id; }
|
||||
|
||||
namespace ImageViewer {
|
||||
namespace Internal {
|
||||
|
||||
@@ -46,13 +38,13 @@ class ImageViewerPlugin : public ExtensionSystem::IPlugin
|
||||
|
||||
public:
|
||||
ImageViewerPlugin() = default;
|
||||
~ImageViewerPlugin();
|
||||
|
||||
private:
|
||||
bool initialize(const QStringList &arguments, QString *errorMessage) final;
|
||||
void extensionsInitialized() final;
|
||||
void extensionsInitialized() final {}
|
||||
|
||||
QAction *registerNewAction(Core::Id id, const QString &title = QString(),
|
||||
const QKeySequence &key = QKeySequence());
|
||||
class ImageViewerPluginPrivate *d = nullptr;
|
||||
};
|
||||
|
||||
} // namespace Internal
|
||||
|
Reference in New Issue
Block a user