Engauge Digitizer  2
LoadImageFromUrl.cpp
Go to the documentation of this file.
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 #ifdef NETWORKING
14 #include <QtNetwork/QNetworkReply>
15 #endif
16 #include <QUrl>
17 #include "Version.h"
18 
20  m_mainWindow (mainWindow),
21 #ifdef NETWORKING
22  m_http (this),
23  m_reply (nullptr),
24 #endif
25  m_buffer (nullptr)
26 {
27  connect (this, SIGNAL (signalImportImage (QString, QImage)), &m_mainWindow, SLOT (slotFileImportImage (QString, QImage)));
28 }
29 
31 {
32  deallocate ();
33 }
34 
35 void LoadImageFromUrl::deallocate ()
36 {
37 #ifdef NETWORKING
38  delete m_reply;
39  delete m_buffer;
40 
41  m_reply = nullptr;
42  m_buffer = nullptr;
43 #endif
44 }
45 
46 void LoadImageFromUrl::slotFinished ()
47 {
48  // Download has just finished
49 
50  QString urlWithoutScheme = m_url.toString (QUrl::RemoveScheme);
51 
52  // Import
53  QImage image;
54  if (image.loadFromData (*m_buffer)) {
55 
56  emit signalImportImage (urlWithoutScheme,
57  image);
58  } else {
59 
60  // Images embedded in web pages produce html in m_buffer. No easy way to fix that. Even
61  // gimp fails in the same situations so we just show an error
62 
63  QString message;
64  QTextStream str (&message);
65 
66  str << tr ("Unable to download image from") << " " << urlWithoutScheme;
67 
68  QMessageBox::critical (&m_mainWindow,
70  message,
71  QMessageBox::Ok);
72  }
73 }
74 
75 void LoadImageFromUrl::startLoadImage (const QUrl &url)
76 {
77  LOG4CPP_INFO_S ((*mainCat)) << "LoadImageFromUrl::startLoadImage url=" << url.toString ().toLatin1 ().data ();
78 
79  m_url = url;
80  if (url.isLocalFile ()) {
81 
82  QFileInfo fileInfo (url.toLocalFile ());
83 
84  // Load local file. This is done synchronously
85  QImage image;
86  if (image.load (url.toLocalFile ())) {
87 
88  emit signalImportImage (fileInfo.fileName (),
89  image);
90 
91  } else {
92 
93  // Probably a bad file type
94 
95  QString message;
96  QTextStream str (&message);
97 
98  str << tr ("Unable to load image from") << " " << url.toLocalFile ();
99 
100  QMessageBox::critical (&m_mainWindow,
102  message,
103  QMessageBox::Ok);
104  }
105 
106  } else {
107 
108  // Drop on the floor if networking is not enabled
109 #ifdef NETWORKING
110  // Asynchronous read from url
111  deallocate ();
112  m_buffer = new QByteArray;
113  QNetworkRequest request (url);
114  m_reply = m_http.get (request);
115 
116  connect (m_reply, SIGNAL (readyRead()), this, SLOT (slotReadData()));
117  connect (m_reply, SIGNAL (finished ()), this, SLOT (slotFinished ()));
118 #endif
119  }
120 }
121 
122 void LoadImageFromUrl::slotReadData ()
123 {
124 #ifdef NETWORKING
125  *m_buffer += m_reply->readAll ();
126 #endif
127 }
LoadImageFromUrl::startLoadImage
void startLoadImage(const QUrl &url)
Start the asynchronous loading of an image from the specified url.
Definition: LoadImageFromUrl.cpp:75
LoadImageFromUrl::signalImportImage
void signalImportImage(QString, QImage)
Send the imported image to MainWindow. This completes the asynchronous loading of the image.
Version.h
MainWindow
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:90
Logger.h
LoadImageFromUrl::~LoadImageFromUrl
~LoadImageFromUrl()
Definition: LoadImageFromUrl.cpp:30
LOG4CPP_INFO_S
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
engaugeWindowTitle
QString engaugeWindowTitle()
Text for title bars of dialogs.
Definition: Version.cpp:14
mainCat
log4cpp::Category * mainCat
Definition: Logger.cpp:14
MainWindow.h
LoadImageFromUrl::LoadImageFromUrl
LoadImageFromUrl(MainWindow &mainWindow)
Single constructor.
Definition: LoadImageFromUrl.cpp:19
LoadImageFromUrl.h