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