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 "imageviewer.h"
35 #include "imageviewerfile.h"
36 #include "imagevieweractionhandler.h"
37 #include "imageviewerconstants.h"
38 #include "imageview.h"
39 #include "ui_imageviewertoolbar.h"
41 #include <coreplugin/icore.h>
42 #include <coreplugin/actionmanager/actionmanager.h>
43 #include <coreplugin/actionmanager/command.h>
44 #include <utils/qtcassert.h>
46 #include <QtCore/QMap>
47 #include <QtCore/QFileInfo>
48 #include <QtGui/QWidget>
49 #include <QtCore/QtDebug>
51 namespace ImageViewer {
54 struct ImageViewerPrivate
57 : context(Constants::IMAGEVIEWER_ID)
60 Core::Context context;
62 ImageViewerFile *file;
65 Ui::ImageViewerToolbar ui_toolbar;
68 ImageViewer::ImageViewer(QWidget *parent)
70 d_ptr(new ImageViewerPrivate)
72 d_ptr->file = new ImageViewerFile(this);
74 d_ptr->imageView = new ImageView();
77 d_ptr->toolbar = new QWidget();
78 d_ptr->ui_toolbar.setupUi(d_ptr->toolbar);
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");
92 connect(d_ptr->file, SIGNAL(changed()), this, SIGNAL(changed()));
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)));
110 ImageViewer::~ImageViewer()
112 delete d_ptr->imageView;
113 delete d_ptr->toolbar;
116 Core::Context ImageViewer::context() const
118 return d_ptr->context;
121 QWidget *ImageViewer::widget()
123 return d_ptr->imageView;
126 bool ImageViewer::createNew(const QString &contents)
132 bool ImageViewer::open(const QString &fileName)
134 if (!d_ptr->imageView->openFile(fileName))
136 setDisplayName(QFileInfo(fileName).fileName());
137 d_ptr->file->setFileName(fileName);
138 // d_ptr->file->setMimeType
143 Core::IFile *ImageViewer::file()
148 QString ImageViewer::id() const
150 return QLatin1String(Constants::IMAGEVIEWER_ID);
153 QString ImageViewer::displayName() const
155 return d_ptr->displayName;
158 void ImageViewer::setDisplayName(const QString &title)
160 d_ptr->displayName = title;
164 bool ImageViewer::duplicateSupported() const
169 Core::IEditor *ImageViewer::duplicate(QWidget *parent)
175 QByteArray ImageViewer::saveState() const
180 bool ImageViewer::restoreState(const QByteArray &state)
186 int ImageViewer::currentLine() const
191 int ImageViewer::currentColumn() const
196 bool ImageViewer::isTemporary() const
201 QWidget *ImageViewer::toolBar()
203 return d_ptr->toolbar;
206 void ImageViewer::scaleFactorUpdate(qreal factor)
208 const QString info = QString::number(factor * 100, 'f', 2) + QLatin1Char('%');
209 d_ptr->ui_toolbar.labelInfo->setText(info);
212 bool ImageViewer::updateButtonIconByTheme(QAbstractButton *button, const QString &name)
214 QTC_ASSERT(button, return false);
215 QTC_ASSERT(!name.isEmpty(), return false);
217 if (QIcon::hasThemeIcon(name)) {
218 button->setIcon(QIcon::fromTheme(name));
225 void ImageViewer::switchViewBackground()
227 d_ptr->ui_toolbar.toolButtonBackground->click();
230 void ImageViewer::switchViewOutline()
232 d_ptr->ui_toolbar.toolButtonOutline->click();
235 void ImageViewer::zoomIn()
237 d_ptr->ui_toolbar.toolButtonZoomIn->click();
240 void ImageViewer::zoomOut()
242 d_ptr->ui_toolbar.toolButtonZoomOut->click();
245 void ImageViewer::resetToOriginalSize()
247 d_ptr->ui_toolbar.toolButtonOriginalSize->click();
250 void ImageViewer::fitToScreen()
252 d_ptr->ui_toolbar.toolButtonFitToScreen->click();
255 } // namespace Internal
256 } // namespace ImageViewer