1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 ** Copyright (c) 2010 Denis Mingulov.
8 ** Contact: Nokia Corporation (info@qt.nokia.com)
11 ** GNU Lesser General Public License Usage
13 ** This file may be used under the terms of the GNU Lesser General Public
14 ** License version 2.1 as published by the Free Software Foundation and
15 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
16 ** Please review the following information to ensure the GNU Lesser General
17 ** Public License version 2.1 requirements will be met:
18 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
20 ** In addition, as a special exception, Nokia gives you certain additional
21 ** rights. These rights are described in the Nokia Qt LGPL Exception
22 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
26 ** Alternatively, this file may be used in accordance with the terms and
27 ** conditions contained in a signed written agreement between you and Nokia.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "imagevieweractionhandler.h"
35 #include "imageviewer.h"
36 #include "imageviewerconstants.h"
38 #include <QtCore/QList>
39 #include <QtCore/QPointer>
40 #include <QtCore/QSignalMapper>
41 #include <QtGui/QAction>
43 #include <coreplugin/icore.h>
44 #include <coreplugin/actionmanager/actionmanager.h>
45 #include <coreplugin/actionmanager/command.h>
46 #include <coreplugin/editormanager/editormanager.h>
47 #include <coreplugin/uniqueidmanager.h>
49 namespace ImageViewer {
52 enum SupportedActions { ZoomIn = 0, ZoomOut, OriginalSize, FitToScreen, Background, Outline };
54 struct ImageViewerActionHandlerPrivate
56 ImageViewerActionHandlerPrivate()
57 : context(Constants::IMAGEVIEWER_ID)
60 QPointer<QAction> actionZoomIn;
61 QPointer<QAction> actionZoomOut;
62 QPointer<QAction> actionOriginalSize;
63 QPointer<QAction> actionFitToScreen;
64 QPointer<QAction> actionBackground;
65 QPointer<QAction> actionOutline;
67 Core::Context context;
68 QPointer<QSignalMapper> signalMapper;
71 ImageViewerActionHandler::ImageViewerActionHandler(QObject *parent) :
73 d_ptr(new ImageViewerActionHandlerPrivate)
75 d_ptr->signalMapper = new QSignalMapper(this);
76 connect(d_ptr->signalMapper, SIGNAL(mapped(int)), SLOT(actionTriggered(int)));
79 ImageViewerActionHandler::~ImageViewerActionHandler()
83 void ImageViewerActionHandler::actionTriggered(int supportedAction)
85 Core::EditorManager *editorManager = Core::EditorManager::instance();
86 Core::IEditor *editor = editorManager->currentEditor();
87 ImageViewer *viewer = qobject_cast<ImageViewer *>(editor);
91 SupportedActions action = static_cast<SupportedActions>(supportedAction);
100 viewer->resetToOriginalSize();
103 viewer->fitToScreen();
106 viewer->switchViewBackground();
109 viewer->switchViewOutline();
116 void ImageViewerActionHandler::createActions()
118 registerNewAction(ZoomIn, Constants::ACTION_ZOOM_IN, tr("Zoom In"),
119 d_ptr->context, QKeySequence(tr("Ctrl++")));
120 registerNewAction(ZoomOut, Constants::ACTION_ZOOM_OUT, tr("Zoom Out"),
121 d_ptr->context, QKeySequence(tr("Ctrl+-")));
122 registerNewAction(OriginalSize, Constants::ACTION_ORIGINAL_SIZE, tr("Original Size"),
123 d_ptr->context, QKeySequence(tr("Ctrl+0")));
124 registerNewAction(FitToScreen, Constants::ACTION_FIT_TO_SCREEN, tr("Fit To Screen"),
125 d_ptr->context, QKeySequence(tr("Ctrl+=")));
126 registerNewAction(Background, Constants::ACTION_BACKGROUND, tr("Switch Background"),
127 d_ptr->context, QKeySequence(tr("Ctrl+[")));
128 registerNewAction(Outline, Constants::ACTION_OUTLINE, tr("Switch Outline"),
129 d_ptr->context, QKeySequence(tr("Ctrl+]")));
132 QAction *ImageViewerActionHandler::registerNewAction(int actionId, const QString &id,
133 const QString &title, const Core::Context &context, const QKeySequence &key)
135 Core::ActionManager *actionManager = Core::ICore::instance()->actionManager();
136 Core::Command *command = 0;
138 QAction *action = new QAction(title, this);
139 command = actionManager->registerAction(action, id, context);
141 command->setDefaultKeySequence(key);
142 connect(action, SIGNAL(triggered()), d_ptr->signalMapper, SLOT(map()));
143 d_ptr->signalMapper->setMapping(action, actionId);
148 } // namespace Internal
149 } // namespace ImageViewer