From cdd8aad28a0df96b400c9480d6a28f1530d6192d Mon Sep 17 00:00:00 2001 From: con <qtc-committer@nokia.com> Date: Wed, 17 Feb 2010 19:14:08 +0100 Subject: [PATCH] Add a (very) simple image viewer. --- .../bineditor/ImageViewer.mimetypes.xml | 70 +++++++ src/plugins/bineditor/bineditor.pro | 9 +- src/plugins/bineditor/bineditor.qrc | 3 +- src/plugins/bineditor/bineditorplugin.cpp | 7 +- src/plugins/bineditor/imageviewer.cpp | 184 ++++++++++++++++++ src/plugins/bineditor/imageviewer.h | 134 +++++++++++++ .../qt4projectmanager/qt4projectmanager.pro | 2 +- src/plugins/texteditor/texteditor.pro | 2 +- 8 files changed, 403 insertions(+), 8 deletions(-) create mode 100644 src/plugins/bineditor/ImageViewer.mimetypes.xml create mode 100644 src/plugins/bineditor/imageviewer.cpp create mode 100644 src/plugins/bineditor/imageviewer.h diff --git a/src/plugins/bineditor/ImageViewer.mimetypes.xml b/src/plugins/bineditor/ImageViewer.mimetypes.xml new file mode 100644 index 00000000000..f4f99a7a1cf --- /dev/null +++ b/src/plugins/bineditor/ImageViewer.mimetypes.xml @@ -0,0 +1,70 @@ +<?xml version="1.0" encoding="UTF-8"?> +<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'> + <mime-type type="image/bmp"> + <sub-class-of type="application/octet-stream"/> + <comment>BMP image</comment> + <glob pattern="*.bmp"/> + </mime-type> + <mime-type type="image/gif"> + <sub-class-of type="application/octet-stream"/> + <comment>GIF image</comment> + <glob pattern="*.gif"/> + </mime-type> + <mime-type type="image/x-icon"> + <sub-class-of type="application/octet-stream"/> + <comment>ICO image</comment> + <glob pattern="*.ico"/> + </mime-type> + <mime-type type="image/jpeg"> + <sub-class-of type="application/octet-stream"/> + <comment>JPEG image</comment> + <glob pattern="*.jpg"/> + <glob pattern="*.jpeg"/> + </mime-type> + <mime-type type="video/x-mng"> + <sub-class-of type="application/octet-stream"/> + <comment>MNG video</comment> + <glob pattern="*.mng"/> + </mime-type> + <mime-type type="image/x-portable-bitmap"> + <sub-class-of type="application/octet-stream"/> + <comment>PBM image</comment> + <glob pattern="*.pbm"/> + </mime-type> + <mime-type type="image/x-portable-graymap"> + <sub-class-of type="application/octet-stream"/> + <comment>PGM image</comment> + <glob pattern="*.pgm"/> + </mime-type> + <mime-type type="image/png"> + <sub-class-of type="application/octet-stream"/> + <comment>PNG image</comment> + <glob pattern="*.png"/> + </mime-type> + <mime-type type="image/x-portable-pixmap"> + <sub-class-of type="application/octet-stream"/> + <comment>PPM image</comment> + <glob pattern="*.ppm"/> + </mime-type> + <mime-type type="image/svg+xml"> + <sub-class-of type="application/octet-stream"/> + <comment>SVG image</comment> + <glob pattern="*.svg"/> + </mime-type> + <mime-type type="image/tiff"> + <sub-class-of type="application/octet-stream"/> + <comment>TIFF image</comment> + <glob pattern="*.tif"/> + <glob pattern="*.tiff"/> + </mime-type> + <mime-type type="image/xbm"> + <sub-class-of type="application/octet-stream"/> + <comment>XBM image</comment> + <glob pattern="*.xbm"/> + </mime-type> + <mime-type type="image/xpm"> + <sub-class-of type="application/octet-stream"/> + <comment>XPM image</comment> + <glob pattern="*.xpm"/> + </mime-type> +</mime-info> diff --git a/src/plugins/bineditor/bineditor.pro b/src/plugins/bineditor/bineditor.pro index 976e701ff4a..06724a155ff 100644 --- a/src/plugins/bineditor/bineditor.pro +++ b/src/plugins/bineditor/bineditor.pro @@ -5,11 +5,14 @@ include(bineditor_dependencies.pri) HEADERS += bineditorplugin.h \ bineditor.h \ - bineditorconstants.h + bineditorconstants.h \ + imageviewer.h SOURCES += bineditorplugin.cpp \ - bineditor.cpp + bineditor.cpp \ + imageviewer.cpp RESOURCES += bineditor.qrc -OTHER_FILES += BinEditor.pluginspec +OTHER_FILES += BinEditor.pluginspec BinEditor.mimetypes.xml \ + ImageViewer.mimetypes.xml diff --git a/src/plugins/bineditor/bineditor.qrc b/src/plugins/bineditor/bineditor.qrc index c7215495dba..fd8e171d3f7 100644 --- a/src/plugins/bineditor/bineditor.qrc +++ b/src/plugins/bineditor/bineditor.qrc @@ -1,5 +1,6 @@ <RCC> - <qresource prefix="/bineditor" > + <qresource prefix="/bineditor"> <file>BinEditor.mimetypes.xml</file> + <file>ImageViewer.mimetypes.xml</file> </qresource> </RCC> diff --git a/src/plugins/bineditor/bineditorplugin.cpp b/src/plugins/bineditor/bineditorplugin.cpp index fe7f3f489a9..03dd19c65e3 100644 --- a/src/plugins/bineditor/bineditorplugin.cpp +++ b/src/plugins/bineditor/bineditorplugin.cpp @@ -31,6 +31,8 @@ #include "bineditor.h" #include "bineditorconstants.h" +#include "imageviewer.h" + #include <QtCore/QFile> #include <QtCore/QFileInfo> #include <QtCore/QDebug> @@ -477,9 +479,10 @@ void BinEditorPlugin::initializeEditor(BinEditor *editor) bool BinEditorPlugin::initialize(const QStringList &arguments, QString *errorMessage) { Q_UNUSED(arguments) - Q_UNUSED(errorMessage) Core::ICore *core = Core::ICore::instance(); + if (!core->mimeDatabase()->addMimeTypes(QLatin1String(":/bineditor/ImageViewer.mimetypes.xml"), errorMessage)) + return false; if (!core->mimeDatabase()->addMimeTypes(QLatin1String(":/bineditor/BinEditor.mimetypes.xml"), errorMessage)) return false; @@ -487,7 +490,7 @@ bool BinEditorPlugin::initialize(const QStringList &arguments, QString *errorMes this, SLOT(updateCurrentEditor(Core::IContext *))); addAutoReleasedObject(new BinEditorFactory(this)); - + addAutoReleasedObject(new ImageViewerFactory); return true; } diff --git a/src/plugins/bineditor/imageviewer.cpp b/src/plugins/bineditor/imageviewer.cpp new file mode 100644 index 00000000000..6e4d65a4810 --- /dev/null +++ b/src/plugins/bineditor/imageviewer.cpp @@ -0,0 +1,184 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#include "imageviewer.h" + +#include <coreplugin/icore.h> +#include <coreplugin/uniqueidmanager.h> + +#include <QtCore/QMap> +#include <QtCore/QFileInfo> +#include <QtGui/QImageReader> +#include <QtGui/QWidget> +#include <QtGui/QGridLayout> +#include <QtDebug> + +namespace { + const char * const IMAGE_VIEWER_ID = "Editors.ImageViewer"; + const char * const C_IMAGE_VIEWER = "Editors.ImageViewer"; +} + +using namespace BINEditor::Internal; +using namespace Core; + +// #pragma mark -- ImageViewerFactory + +ImageViewerFactory::ImageViewerFactory(QObject *parent) : + Core::IEditorFactory(parent) +{ + QMap<QByteArray, QString> possibleMimeTypes; + possibleMimeTypes.insert("bmp", QLatin1String("image/bmp")); + possibleMimeTypes.insert("gif", QLatin1String("image/gif")); + possibleMimeTypes.insert("ico", QLatin1String("image/x-icon")); + possibleMimeTypes.insert("jpeg", QLatin1String("image/jpeg")); + possibleMimeTypes.insert("jpg", QLatin1String("image/jpeg")); + possibleMimeTypes.insert("mng", QLatin1String("video/x-mng")); + possibleMimeTypes.insert("pbm", QLatin1String("image/x-portable-bitmap")); + possibleMimeTypes.insert("pgm", QLatin1String("image/x-portable-graymap")); + possibleMimeTypes.insert("png", QLatin1String("image/png")); + possibleMimeTypes.insert("ppm", QLatin1String("image/x-portable-pixmap")); + possibleMimeTypes.insert("svg", QLatin1String("image/svg+xml")); + possibleMimeTypes.insert("tif", QLatin1String("image/tiff")); + possibleMimeTypes.insert("tiff", QLatin1String("image/tiff")); + possibleMimeTypes.insert("xbm", QLatin1String("image/xbm")); + possibleMimeTypes.insert("xpm", QLatin1String("image/xpm")); + + QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats(); + foreach (const QByteArray &format, supportedFormats) { + const QString &value = possibleMimeTypes.value(format); + if (!value.isEmpty()) + m_mimeTypes.append(value); + } +} + +IEditor *ImageViewerFactory::createEditor(QWidget *parent) +{ + return new ImageViewer(parent); +} + +QStringList ImageViewerFactory::mimeTypes() const +{ + return m_mimeTypes; +} + +QString ImageViewerFactory::id() const +{ + return QLatin1String(IMAGE_VIEWER_ID); +} + +QString ImageViewerFactory::displayName() const +{ + return tr("Image Viewer"); +} + +Core::IFile *ImageViewerFactory::open(const QString &fileName) +{ + return 0; +} + +// #pragma mark -- ImageViewerFile + +void ImageViewerFile::modified(ReloadBehavior *behavior) +{ + // TODO +} + +// #pragma mark -- ImageViewer + +ImageViewer::ImageViewer(QObject *parent) + : IEditor(parent) +{ + m_file = new ImageViewerFile; + m_context << Core::ICore::instance()->uniqueIDManager()->uniqueIdentifier(C_IMAGE_VIEWER); + + m_scrollArea = new QScrollArea; + m_scrollArea->setWidgetResizable(true); + m_imageview = new QWidget; + QGridLayout *layout = new QGridLayout(); + m_label = new QLabel; + m_label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); + m_label->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); + layout->setMargin(0); + m_imageview->setLayout(layout); + layout->addWidget(m_label, 0, 0, 1, 1); +} + +ImageViewer::~ImageViewer() +{ + delete m_scrollArea; + delete m_file; +} + +QList<int> ImageViewer::context() const +{ + return m_context; +} + +QWidget *ImageViewer::widget() +{ + return m_scrollArea; +} + +bool ImageViewer::createNew(const QString &contents) +{ + Q_UNUSED(contents) + return false; +} + +bool ImageViewer::open(const QString &fileName) +{ + m_label->setPixmap(QPixmap(fileName)); + m_scrollArea->setWidget(m_imageview); + setDisplayName(QFileInfo(fileName).fileName()); + m_file->setFileName(fileName); + // m_file->setMimeType + emit changed(); + return !m_label->pixmap()->isNull(); +} + +Core::IFile *ImageViewer::file() +{ + return m_file; +} + +QString ImageViewer::id() const +{ + return QLatin1String(IMAGE_VIEWER_ID); +} + +QString ImageViewer::displayName() const +{ + return m_displayName; +} + +void ImageViewer::setDisplayName(const QString &title) +{ + m_displayName = title; + emit changed(); +} diff --git a/src/plugins/bineditor/imageviewer.h b/src/plugins/bineditor/imageviewer.h new file mode 100644 index 00000000000..cb4e5859c6c --- /dev/null +++ b/src/plugins/bineditor/imageviewer.h @@ -0,0 +1,134 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#ifndef IMAGEVIEWER_H +#define IMAGEVIEWER_H + +#include <coreplugin/editormanager/ieditorfactory.h> +#include <coreplugin/editormanager/ieditor.h> +#include <coreplugin/ifile.h> + +#include <QtCore/QStringList> +#include <QtGui/QWidget> +#include <QtGui/QLabel> +#include <QtGui/QScrollArea> + +namespace BINEditor { +namespace Internal { + +class ImageViewerFactory : public Core::IEditorFactory +{ +Q_OBJECT +public: + explicit ImageViewerFactory(QObject *parent = 0); + + Core::IEditor *createEditor(QWidget *parent); + + QStringList mimeTypes() const; + + QString id() const; + QString displayName() const; + + Core::IFile *open(const QString &fileName); + +private: + QStringList m_mimeTypes; +}; + +class ImageViewerFile : public Core::IFile +{ + Q_OBJECT +public: + explicit ImageViewerFile(QObject *parent = 0) {} + + bool save(const QString &fileName = QString()) { Q_UNUSED(fileName); return false; } + QString fileName() const { return m_fileName; } + + QString defaultPath() const { return QString(); } + QString suggestedFileName() const { return QString(); } + QString mimeType() const { return m_mimeType; } + + bool isModified() const { return false; } + bool isReadOnly() const { return true; } + bool isSaveAsAllowed() const { return false; } + + void modified(ReloadBehavior *behavior); + + void checkPermissions() {} + + void setMimetype(const QString &mimetype) { m_mimeType = mimetype; emit changed(); } + void setFileName(const QString &filename) { m_fileName = filename; emit changed(); } +private: + QString m_fileName; + QString m_mimeType; +}; + +class ImageViewer : public Core::IEditor +{ + Q_OBJECT +public: + explicit ImageViewer(QObject *parent = 0); + ~ImageViewer(); + + QList<int> context() const; + QWidget *widget(); + + bool createNew(const QString &contents = QString()); + bool open(const QString &fileName = QString()); + Core::IFile *file(); + QString id() const; + QString displayName() const; + void setDisplayName(const QString &title); + + bool duplicateSupported() const { return false; } + IEditor *duplicate(QWidget *parent) { return 0; } + + QByteArray saveState() const { return QByteArray(); } + bool restoreState(const QByteArray &state) { Q_UNUSED(state); } + + int currentLine() const { return 0; } + int currentColumn() const { return 0; } + + bool isTemporary() const { return false; } + + QWidget *toolBar() { return 0; } + +private: + QList<int> m_context; + QString m_displayName; + ImageViewerFile *m_file; + QScrollArea *m_scrollArea; + QWidget *m_imageview; + QLabel *m_label; +}; + +} +} + +#endif // IMAGEVIEWER_H diff --git a/src/plugins/qt4projectmanager/qt4projectmanager.pro b/src/plugins/qt4projectmanager/qt4projectmanager.pro index 5fbb251b300..399ef99b1cc 100644 --- a/src/plugins/qt4projectmanager/qt4projectmanager.pro +++ b/src/plugins/qt4projectmanager/qt4projectmanager.pro @@ -100,4 +100,4 @@ include(qt-s60/qt-s60.pri) include(qt-maemo/qt-maemo.pri) include(customwidgetwizard/customwidgetwizard.pri) DEFINES += QT_NO_CAST_TO_ASCII -OTHER_FILES += Qt4ProjectManager.pluginspec +OTHER_FILES += Qt4ProjectManager.pluginspec Qt4ProjectManager.mimetypes.xml diff --git a/src/plugins/texteditor/texteditor.pro b/src/plugins/texteditor/texteditor.pro index bcaa2c32ee1..2ce222b55c6 100644 --- a/src/plugins/texteditor/texteditor.pro +++ b/src/plugins/texteditor/texteditor.pro @@ -75,4 +75,4 @@ FORMS += behaviorsettingspage.ui \ fontsettingspage.ui \ colorschemeedit.ui RESOURCES += texteditor.qrc -OTHER_FILES += TextEditor.pluginspec +OTHER_FILES += TextEditor.pluginspec TextEditor.mimetypes.xml -- GitLab