OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / imageviewer / imagevieweractionhandler.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 ** Copyright (c) 2010 Denis Mingulov.
7 **
8 ** Contact: Nokia Corporation (info@qt.nokia.com)
9 **
10 **
11 ** GNU Lesser General Public License Usage
12 **
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.
19 **
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.
23 **
24 ** Other Usage
25 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "imagevieweractionhandler.h"
35 #include "imageviewer.h"
36 #include "imageviewerconstants.h"
37
38 #include <QtCore/QList>
39 #include <QtCore/QPointer>
40 #include <QtCore/QSignalMapper>
41 #include <QtGui/QAction>
42
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>
48
49 namespace ImageViewer {
50 namespace Internal {
51
52 enum SupportedActions { ZoomIn = 0, ZoomOut, OriginalSize, FitToScreen, Background, Outline };
53
54 struct ImageViewerActionHandlerPrivate
55 {
56     ImageViewerActionHandlerPrivate()
57         : context(Constants::IMAGEVIEWER_ID)
58     {}
59
60     QPointer<QAction> actionZoomIn;
61     QPointer<QAction> actionZoomOut;
62     QPointer<QAction> actionOriginalSize;
63     QPointer<QAction> actionFitToScreen;
64     QPointer<QAction> actionBackground;
65     QPointer<QAction> actionOutline;
66
67     Core::Context context;
68     QPointer<QSignalMapper> signalMapper;
69 };
70
71 ImageViewerActionHandler::ImageViewerActionHandler(QObject *parent) :
72     QObject(parent),
73     d_ptr(new ImageViewerActionHandlerPrivate)
74 {
75     d_ptr->signalMapper = new QSignalMapper(this);
76     connect(d_ptr->signalMapper, SIGNAL(mapped(int)), SLOT(actionTriggered(int)));
77 }
78
79 ImageViewerActionHandler::~ImageViewerActionHandler()
80 {
81 }
82
83 void ImageViewerActionHandler::actionTriggered(int supportedAction)
84 {
85     Core::EditorManager *editorManager = Core::EditorManager::instance();
86     Core::IEditor *editor = editorManager->currentEditor();
87     ImageViewer *viewer = qobject_cast<ImageViewer *>(editor);
88     if (!viewer)
89         return;
90
91     SupportedActions action = static_cast<SupportedActions>(supportedAction);
92     switch(action) {
93     case ZoomIn:
94         viewer->zoomIn();
95         break;
96     case ZoomOut:
97         viewer->zoomOut();
98         break;
99     case OriginalSize:
100         viewer->resetToOriginalSize();
101         break;
102     case FitToScreen:
103         viewer->fitToScreen();
104         break;
105     case Background:
106         viewer->switchViewBackground();
107         break;
108     case Outline:
109         viewer->switchViewOutline();
110         break;
111     default:
112         break;
113     }
114 }
115
116 void ImageViewerActionHandler::createActions()
117 {
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+]")));
130 }
131
132 QAction *ImageViewerActionHandler::registerNewAction(int actionId, const QString &id,
133                            const QString &title, const Core::Context &context, const QKeySequence &key)
134 {
135     Core::ActionManager *actionManager = Core::ICore::instance()->actionManager();
136     Core::Command *command = 0;
137
138     QAction *action = new QAction(title, this);
139     command = actionManager->registerAction(action, id, context);
140     if (command)
141         command->setDefaultKeySequence(key);
142     connect(action, SIGNAL(triggered()), d_ptr->signalMapper, SLOT(map()));
143     d_ptr->signalMapper->setMapping(action, actionId);
144
145     return action;
146 }
147
148 } // namespace Internal
149 } // namespace ImageViewer