Engauge Digitizer  2
 All Classes Functions Variables Typedefs Enumerations Friends Pages
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 <QTextStream>
13 #include <QtNetwork/QNetworkReply>
14 #include <QUrl>
15 #include "Version.h"
16 
18  m_mainWindow (mainWindow),
19  m_http (this),
20  m_reply (0),
21  m_buffer (0)
22 {
23  connect (this, SIGNAL (signalImportImage (QString, QImage)), &m_mainWindow, SLOT (slotFileImportImage (QString, QImage)));
24 }
25 
26 LoadImageFromUrl::~LoadImageFromUrl ()
27 {
28  deallocate ();
29 }
30 
31 void LoadImageFromUrl::deallocate ()
32 {
33  delete m_reply;
34  delete m_buffer;
35 
36  m_reply = 0;
37  m_buffer = 0;
38 }
39 
40 void LoadImageFromUrl::slotFinished ()
41 {
42  // Download has just finished
43 
44  QString urlWithoutScheme = m_url.toString (QUrl::RemoveScheme);
45 
46  // Import
47  QImage image;
48  if (image.loadFromData (*m_buffer)) {
49 
50  emit signalImportImage (urlWithoutScheme,
51  image);
52  } else {
53 
54  // Images embedded in web pages produce html in m_buffer. No easy way to fix that. Even
55  // gimp fails in the same situations so we just show an error
56 
57  QString message;
58  QTextStream str (&message);
59 
60  str << tr ("Unable to download image from") << " " << urlWithoutScheme;
61 
62  QMessageBox::critical (&m_mainWindow,
63  engaugeWindowTitle(),
64  message,
65  QMessageBox::Ok);
66  }
67 }
68 
69 void LoadImageFromUrl::startLoadImage (const QUrl &url)
70 {
71  LOG4CPP_INFO_S ((*mainCat)) << "LoadImageFromUrl::startLoadImage url=" << url.toString ().toLatin1 ().data ();
72 
73  m_url = url;
74  if (url.isLocalFile ()) {
75 
76  QFileInfo fileInfo (url.toLocalFile ());
77 
78  // Load local file. This is done synchronously
79  QImage image;
80  if (image.load (url.toLocalFile ())) {
81 
82  emit signalImportImage (fileInfo.fileName (),
83  image);
84 
85  } else {
86 
87  // Probably a bad file type
88 
89  QString message;
90  QTextStream str (&message);
91 
92  str << tr ("Unable to load image from") << " " << url.toLocalFile ();
93 
94  QMessageBox::critical (&m_mainWindow,
95  engaugeWindowTitle(),
96  message,
97  QMessageBox::Ok);
98  }
99 
100  } else {
101 
102  // Asynchronous read from url
103  deallocate ();
104  m_buffer = new QByteArray;
105  QNetworkRequest request (url);
106  m_reply = m_http.get (request);
107 
108  connect (m_reply, SIGNAL (readyRead()), this, SLOT (slotReadData()));
109  connect (m_reply, SIGNAL (finished ()), this, SLOT (slotFinished ()));
110  }
111 }
112 
113 void LoadImageFromUrl::slotReadData ()
114 {
115  *m_buffer += m_reply->readAll ();
116 }
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:91