Engauge Digitizer  2
LoadImageFromUrl.cpp
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "LoadImageFromUrl.h"
8 #include "Logger.h"
9 #include "MainWindow.h"
10 #include <QFileInfo>
11 #include <QMessageBox>
12 #include <QtNetwork/QNetworkReply>
13 #include <QUrl>
14 #include "Version.h"
15 
17  m_mainWindow (mainWindow),
18  m_http (this),
19  m_reply (0),
20  m_buffer (0)
21 {
22  connect (this, SIGNAL (signalImportImage (QString, QImage)), &m_mainWindow, SLOT (slotFileImportImage (QString, QImage)));
23 }
24 
25 LoadImageFromUrl::~LoadImageFromUrl ()
26 {
27  deallocate ();
28 }
29 
30 void LoadImageFromUrl::deallocate ()
31 {
32  if (m_reply != 0) {
33  delete m_reply;
34  m_reply = 0;
35  }
36 
37  if (m_buffer != 0) {
38  delete m_buffer;
39  m_buffer = 0;
40  }
41 }
42 
43 void LoadImageFromUrl::slotFinished ()
44 {
45  // Download has just finished
46 
47  QString urlWithoutScheme = m_url.toString (QUrl::RemoveScheme);
48 
49  // Import
50  QImage image;
51  if (image.loadFromData (*m_buffer)) {
52 
53  emit signalImportImage (urlWithoutScheme,
54  image);
55  } else {
56 
57  // Images embedded in web pages produce html in m_buffer. No easy way to fix that. Even
58  // gimp fails in the same situations so we just show an error
59 
60  QString message;
61  QTextStream str (&message);
62 
63  str << tr ("Unable to download image from") << " " << urlWithoutScheme;
64 
65  QMessageBox::critical (&m_mainWindow,
66  engaugeWindowTitle(),
67  message,
68  QMessageBox::Ok);
69  }
70 }
71 
72 void LoadImageFromUrl::startLoadImage (const QUrl &url)
73 {
74  LOG4CPP_INFO_S ((*mainCat)) << "LoadImageFromUrl::startLoadImage url=" << url.toString ().toLatin1 ().data ();
75 
76  m_url = url;
77  if (url.isLocalFile ()) {
78 
79  QFileInfo fileInfo (url.toLocalFile ());
80 
81  // Load local file. This is done synchronously
82  QImage image;
83  if (image.load (url.toLocalFile ())) {
84 
85  emit signalImportImage (fileInfo.fileName (),
86  image);
87 
88  } else {
89 
90  // Probably a bad file type
91 
92  QString message;
93  QTextStream str (&message);
94 
95  str << tr ("Unable to load image from") << " " << url.toLocalFile ();
96 
97  QMessageBox::critical (&m_mainWindow,
98  engaugeWindowTitle(),
99  message,
100  QMessageBox::Ok);
101  }
102 
103  } else {
104 
105  // Asynchronous read from url
106  deallocate ();
107  m_buffer = new QByteArray;
108  QNetworkRequest request (url);
109  m_reply = m_http.get (request);
110 
111  connect (m_reply, SIGNAL (readyRead()), this, SLOT (slotReadData()));
112  connect (m_reply, SIGNAL (finished ()), this, SLOT (slotFinished ()));
113  }
114 }
115 
116 void LoadImageFromUrl::slotReadData ()
117 {
118  *m_buffer += m_reply->readAll ();
119 }
LoadImageFromUrl(MainWindow &mainWindow)
Single constructor.
void signalImportImage(QString, QImage)
Send the imported image to MainWindow. This completes the asynchronous loading of the image...
void startLoadImage(const QUrl &url)
Start the asynchronous loading of an image from the specified url.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:89