summaryrefslogtreecommitdiffstats
path: root/dragremote
diff options
context:
space:
mode:
authorAriya Hidayat <ariya.hidayat@nokia.com>2008-09-11 13:38:44 +0000
committerDavid Boddie <dboddie@trolltech.com>2008-09-11 13:38:44 +0000
commit23f43393c62ae872fb07fa32f289bd5698c9e4ee (patch)
tree4cb98eb8a9d1e321abd95b158aa2915ae581fb17 /dragremote
parent190cbb68ca862d126758f90252fa0030ca68de38 (diff)
Add image viewer example (with support for drag-and-drop from remote URL)
Diffstat (limited to 'dragremote')
-rw-r--r--dragremote/dragremote.cpp154
-rw-r--r--dragremote/dragremote.pro5
-rw-r--r--dragremote/dragremote.qrc5
-rw-r--r--dragremote/paris.jpgbin0 -> 87517 bytes
4 files changed, 164 insertions, 0 deletions
diff --git a/dragremote/dragremote.cpp b/dragremote/dragremote.cpp
new file mode 100644
index 0000000..23760b9
--- /dev/null
+++ b/dragremote/dragremote.cpp
@@ -0,0 +1,154 @@
+/****************************************************************************
+**
+** Copyright (C) 2008-2008 Trolltech ASA. All rights reserved.
+**
+** This file is part of the Graphics Dojo project on Trolltech Labs.
+**
+** This file may be used under the terms of the GNU General Public
+** License version 2.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of
+** this file. Please review the following information to ensure GNU
+** General Public Licensing requirements will be met:
+** http://www.trolltech.com/products/qt/opensource.html
+**
+** If you are unsure which license is appropriate for your use, please
+** review the following information:
+** http://www.trolltech.com/products/qt/licensing.html or contact the
+** sales department at sales@trolltech.com.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+****************************************************************************/
+
+#include <QtGui>
+#include <QtNetwork>
+
+class ImageViewer: public QWidget
+{
+Q_OBJECT
+
+public:
+ ImageViewer();
+ void loadImage(const QString &file);
+ void loadImage(const QImage &image);
+
+protected:
+ void dragEnterEvent(QDragEnterEvent*);
+ void dropEvent(QDropEvent *event);
+ void paintEvent(QPaintEvent*);
+
+public slots:
+ void handleNetworkData(QNetworkReply*);
+
+private:
+ QString m_fileName;
+ QImage m_image;
+ QNetworkAccessManager m_manager;
+};
+
+ImageViewer::ImageViewer(): QWidget(), m_manager(0)
+{
+ setAcceptDrops(true);
+
+ setAttribute(Qt::WA_StaticContents, true);
+ setAttribute(Qt::WA_OpaquePaintEvent, true);
+ setAttribute(Qt::WA_NoSystemBackground, true);
+
+ connect(&m_manager, SIGNAL(finished(QNetworkReply*)),
+ this, SLOT(handleNetworkData(QNetworkReply*)));
+}
+
+void ImageViewer::handleNetworkData(QNetworkReply *networkReply)
+{
+ QImage image;
+
+ qDebug() << "Received" << networkReply->size() << "bytes";
+ QUrl url = networkReply->url();
+ if (networkReply->error()) {
+ m_fileName = QString();
+ qDebug() << "Can't download" << url.toString()
+ << ":" << networkReply->errorString();
+ } else {
+ m_fileName = url.toString();
+ if (url.scheme() == "file")
+ m_fileName = url.toLocalFile();
+ image.load(networkReply, 0);
+ }
+
+ networkReply->deleteLater();
+ loadImage(image);
+}
+
+void ImageViewer::loadImage(const QImage &image)
+{
+ m_image = image;
+ if (m_image.isNull()) {
+ setFixedSize(512, 256);
+ setWindowTitle(QString("Can not load %1").arg(m_fileName));
+ } else {
+ QString title = "Image Viewer ";
+ if ((m_image.width() > 1024) || (m_image.height() > 800)) {
+ qDebug() << "Image is too large. Rescaling....";
+ int w = m_image.width();
+ m_image = m_image.scaled(640, 480, Qt::KeepAspectRatio, Qt::SmoothTransformation);
+ title += QString("[Zoom %1%] ").arg(m_image.width() * 100 / w);
+ }
+ setWindowTitle(QString("%1: %2 (%3 x %4)").arg(title).arg(m_fileName).
+ arg(m_image.width()).arg(m_image.height()));
+ setFixedSize(m_image.width(), m_image.height());
+ }
+ update();
+}
+
+void ImageViewer::loadImage(const QString &fileName)
+{
+ m_fileName = QFileInfo(fileName).fileName();
+ loadImage(QImage(fileName));
+}
+
+void ImageViewer::dragEnterEvent(QDragEnterEvent *event)
+{
+ if (event->mimeData()->hasFormat("text/uri-list"))
+ event->acceptProposedAction();
+}
+
+void ImageViewer::dropEvent(QDropEvent *event)
+{
+ QList<QUrl> urls = event->mimeData()->urls();
+ if (urls.count()) {
+ QUrl url = urls[0];
+ if (event->mimeData()->hasImage()) {
+ QImage img = qvariant_cast<QImage>(event->mimeData()->imageData());
+ m_fileName = url.toString();
+ loadImage(img);
+ } else {
+ m_manager.get(QNetworkRequest(url));
+ setWindowTitle(QString("Loading %1...").arg(url.toString()));
+ }
+
+ event->acceptProposedAction();
+ }
+}
+
+void ImageViewer::paintEvent(QPaintEvent*)
+{
+ QPainter painter(this);
+ if (!m_image.isNull())
+ painter.drawImage(0, 0, m_image);
+ else
+ painter.fillRect(rect(), palette().window());
+}
+
+#include "dragremote.moc"
+
+int main(int argc, char *argv[])
+{
+ QApplication app(argc, argv);
+
+ ImageViewer widget;
+ widget.show();
+ widget.loadImage(":/paris.jpg");
+
+ return app.exec();
+}
diff --git a/dragremote/dragremote.pro b/dragremote/dragremote.pro
new file mode 100644
index 0000000..290b4c2
--- /dev/null
+++ b/dragremote/dragremote.pro
@@ -0,0 +1,5 @@
+TEMPLATE = app
+TARGET = dragremote
+SOURCES = dragremote.cpp
+RESOURCES = dragremote.qrc
+QT += network
diff --git a/dragremote/dragremote.qrc b/dragremote/dragremote.qrc
new file mode 100644
index 0000000..f6aab7d
--- /dev/null
+++ b/dragremote/dragremote.qrc
@@ -0,0 +1,5 @@
+<RCC>
+ <qresource>
+ <file>paris.jpg</file>
+ </qresource>
+</RCC>
diff --git a/dragremote/paris.jpg b/dragremote/paris.jpg
new file mode 100644
index 0000000..d4967aa
--- /dev/null
+++ b/dragremote/paris.jpg
Binary files differ