OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / imageviewer / imageviewer.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 "imageviewer.h"
35 #include "imageviewerfile.h"
36 #include "imagevieweractionhandler.h"
37 #include "imageviewerconstants.h"
38 #include "imageview.h"
39 #include "ui_imageviewertoolbar.h"
40
41 #include <coreplugin/icore.h>
42 #include <coreplugin/actionmanager/actionmanager.h>
43 #include <coreplugin/actionmanager/command.h>
44 #include <utils/qtcassert.h>
45
46 #include <QtCore/QMap>
47 #include <QtCore/QFileInfo>
48 #include <QtGui/QWidget>
49 #include <QtCore/QtDebug>
50
51 namespace ImageViewer {
52 namespace Internal {
53
54 struct ImageViewerPrivate
55 {
56     ImageViewerPrivate()
57         : context(Constants::IMAGEVIEWER_ID)
58     {}
59
60     Core::Context context;
61     QString displayName;
62     ImageViewerFile *file;
63     ImageView *imageView;
64     QWidget *toolbar;
65     Ui::ImageViewerToolbar ui_toolbar;
66 };
67
68 ImageViewer::ImageViewer(QWidget *parent)
69     : IEditor(parent),
70     d_ptr(new ImageViewerPrivate)
71 {
72     d_ptr->file = new ImageViewerFile(this);
73
74     d_ptr->imageView = new ImageView();
75
76     // toolbar
77     d_ptr->toolbar = new QWidget();
78     d_ptr->ui_toolbar.setupUi(d_ptr->toolbar);
79
80     // icons update - try to use system theme
81     updateButtonIconByTheme(d_ptr->ui_toolbar.toolButtonZoomIn, "zoom-in");
82     updateButtonIconByTheme(d_ptr->ui_toolbar.toolButtonZoomOut, "zoom-out");
83     updateButtonIconByTheme(d_ptr->ui_toolbar.toolButtonOriginalSize, "zoom-original");
84     updateButtonIconByTheme(d_ptr->ui_toolbar.toolButtonFitToScreen, "zoom-fit-best");
85     // a display - something is on the background
86     updateButtonIconByTheme(d_ptr->ui_toolbar.toolButtonBackground, "video-display");
87     // "emblem to specify the directory where the user stores photographs"
88     // (photograph has outline - piece of paper)
89     updateButtonIconByTheme(d_ptr->ui_toolbar.toolButtonOutline, "emblem-photos");
90
91     // connections
92     connect(d_ptr->file, SIGNAL(changed()), this, SIGNAL(changed()));
93
94     connect(d_ptr->ui_toolbar.toolButtonZoomIn, SIGNAL(clicked()),
95             d_ptr->imageView, SLOT(zoomIn()));
96     connect(d_ptr->ui_toolbar.toolButtonZoomOut, SIGNAL(clicked()),
97             d_ptr->imageView, SLOT(zoomOut()));
98     connect(d_ptr->ui_toolbar.toolButtonFitToScreen, SIGNAL(clicked()),
99             d_ptr->imageView, SLOT(fitToScreen()));
100     connect(d_ptr->ui_toolbar.toolButtonOriginalSize, SIGNAL(clicked()),
101             d_ptr->imageView, SLOT(resetToOriginalSize()));
102     connect(d_ptr->ui_toolbar.toolButtonBackground, SIGNAL(toggled(bool)),
103             d_ptr->imageView, SLOT(setViewBackground(bool)));
104     connect(d_ptr->ui_toolbar.toolButtonOutline, SIGNAL(toggled(bool)),
105             d_ptr->imageView, SLOT(setViewOutline(bool)));
106     connect(d_ptr->imageView, SIGNAL(scaleFactorChanged(qreal)),
107             this, SLOT(scaleFactorUpdate(qreal)));
108 }
109
110 ImageViewer::~ImageViewer()
111 {
112     delete d_ptr->imageView;
113     delete d_ptr->toolbar;
114 }
115
116 Core::Context ImageViewer::context() const
117 {
118     return d_ptr->context;
119 }
120
121 QWidget *ImageViewer::widget()
122 {
123     return d_ptr->imageView;
124 }
125
126 bool ImageViewer::createNew(const QString &contents)
127 {
128     Q_UNUSED(contents)
129     return false;
130 }
131
132 bool ImageViewer::open(const QString &fileName)
133 {
134     if (!d_ptr->imageView->openFile(fileName))
135         return false;
136     setDisplayName(QFileInfo(fileName).fileName());
137     d_ptr->file->setFileName(fileName);
138     // d_ptr->file->setMimeType
139     emit changed();
140     return true;
141 }
142
143 Core::IFile *ImageViewer::file()
144 {
145     return d_ptr->file;
146 }
147
148 QString ImageViewer::id() const
149 {
150     return QLatin1String(Constants::IMAGEVIEWER_ID);
151 }
152
153 QString ImageViewer::displayName() const
154 {
155     return d_ptr->displayName;
156 }
157
158 void ImageViewer::setDisplayName(const QString &title)
159 {
160     d_ptr->displayName = title;
161     emit changed();
162 }
163
164 bool ImageViewer::duplicateSupported() const
165 {
166     return false;
167 }
168
169 Core::IEditor *ImageViewer::duplicate(QWidget *parent)
170 {
171     Q_UNUSED(parent);
172     return 0;
173 }
174
175 QByteArray ImageViewer::saveState() const
176 {
177     return QByteArray();
178 }
179
180 bool ImageViewer::restoreState(const QByteArray &state)
181 {
182     Q_UNUSED(state);
183     return true;
184 }
185
186 int ImageViewer::currentLine() const
187 {
188     return 0;
189 }
190
191 int ImageViewer::currentColumn() const
192 {
193     return 0;
194 }
195
196 bool ImageViewer::isTemporary() const
197 {
198     return false;
199 }
200
201 QWidget *ImageViewer::toolBar()
202 {
203     return d_ptr->toolbar;
204 }
205
206 void ImageViewer::scaleFactorUpdate(qreal factor)
207 {
208     const QString info = QString::number(factor * 100, 'f', 2) + QLatin1Char('%');
209     d_ptr->ui_toolbar.labelInfo->setText(info);
210 }
211
212 bool ImageViewer::updateButtonIconByTheme(QAbstractButton *button, const QString &name)
213 {
214     QTC_ASSERT(button, return false);
215     QTC_ASSERT(!name.isEmpty(), return false);
216
217     if (QIcon::hasThemeIcon(name)) {
218         button->setIcon(QIcon::fromTheme(name));
219         return true;
220     }
221
222     return false;
223 }
224
225 void ImageViewer::switchViewBackground()
226 {
227     d_ptr->ui_toolbar.toolButtonBackground->click();
228 }
229
230 void ImageViewer::switchViewOutline()
231 {
232     d_ptr->ui_toolbar.toolButtonOutline->click();
233 }
234
235 void ImageViewer::zoomIn()
236 {
237     d_ptr->ui_toolbar.toolButtonZoomIn->click();
238 }
239
240 void ImageViewer::zoomOut()
241 {
242     d_ptr->ui_toolbar.toolButtonZoomOut->click();
243 }
244
245 void ImageViewer::resetToOriginalSize()
246 {
247     d_ptr->ui_toolbar.toolButtonOriginalSize->click();
248 }
249
250 void ImageViewer::fitToScreen()
251 {
252     d_ptr->ui_toolbar.toolButtonFitToScreen->click();
253 }
254
255 } // namespace Internal
256 } // namespace ImageViewer