Engauge Digitizer
2
Toggle main menu visibility
Loading...
Searching...
No Matches
Load
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 "
UrlDirty.h
"
17
#include "
Version.h
"
18
19
LoadImageFromUrl::LoadImageFromUrl
(
MainWindow
&mainWindow) :
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
30
LoadImageFromUrl::~LoadImageFromUrl
()
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,
69
engaugeWindowTitle
(),
70
message,
71
QMessageBox::Ok);
72
}
73
}
74
75
void
LoadImageFromUrl::startLoadImage
(
const
UrlDirty
&url)
76
{
77
qCInfo(ENGAUGE_LOG) <<
"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,
101
engaugeWindowTitle
(),
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.h
Logger.h
MainWindow.h
UrlDirty.h
engaugeWindowTitle
QString engaugeWindowTitle()
Text for title bars of dialogs.
Definition
Version.cpp:15
Version.h
LoadImageFromUrl::~LoadImageFromUrl
~LoadImageFromUrl()
Definition
LoadImageFromUrl.cpp:30
LoadImageFromUrl::startLoadImage
void startLoadImage(const UrlDirty &url)
Start the asynchronous loading of an image from the specified url.
Definition
LoadImageFromUrl.cpp:75
LoadImageFromUrl::LoadImageFromUrl
LoadImageFromUrl(MainWindow &mainWindow)
Single constructor.
Definition
LoadImageFromUrl.cpp:19
LoadImageFromUrl::signalImportImage
void signalImportImage(QString, QImage)
Send the imported image to MainWindow. This completes the asynchronous loading of the image.
MainWindow
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition
MainWindow.h:95
UrlDirty
Adds ability to QUrl to cleanup url path.
Definition
UrlDirty.h:16
UrlDirty::toLocalFile
QString toLocalFile() const
Override method to get string value to remove trailing whitepace.
Definition
UrlDirty.cpp:18
Generated on
for Engauge Digitizer by
1.17.0