Geometrize  1.0
An application for geometrizing images into geometric primitives
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
Functions
geometrize::network::completionhandlers Namespace Reference

Functions

void onImageDownloadComplete (network::Downloader *self, QNetworkReply::NetworkError error)
 onImageDownloadComplete A callback when an image download completes. More...
 
void onWebpageDownloadComplete (network::Downloader *self, QNetworkReply::NetworkError error)
 onWebpageDownloadComplete A callback when a webpage download completes. More...
 

Function Documentation

void geometrize::network::completionhandlers::onImageDownloadComplete ( network::Downloader *  self,
QNetworkReply::NetworkError  error 
)

onImageDownloadComplete A callback when an image download completes.

Parameters
selfThe downloader that downloaded the image. Note this callback is responsible for deleting the downloader.
errorThe download error, if any.
25 {
26  const QByteArray data{self->getDownloadedData()};
27  const QUrl url{self->getUrl()};
28  self->deleteLater();
29 
30  if(error != QNetworkReply::NoError) {
31  // TODO post error to console or somewhere?
32  qDebug() << "FINISHED DOWNLOADING WEBPAGE WITH ERROR" << error;
33  return;
34  }
35 
36  QImage image{QImage::fromData(data)};
37  if(image.isNull()) {
38  qDebug() << "FAILED TO CREATE IMAGE FROM DOWNLOADED DATA BUFFER";
39  return;
40  }
41 
42  image = image.convertToFormat(QImage::Format_RGBA8888); // Note: to guarantee format is RGBA8888
43  if(image.format() != QImage::Format_RGBA8888) {
44  qDebug() << "FAILED TO CONVERT IMAGE TO RGBA8888";
45  return;
46  }
47 
48  task::createImageTaskAndWindow(url.toString().toStdString(), image);
49 }
ImageTask * createImageTaskAndWindow(const std::string &displayName, const std::string &taskUrl)
createImageTaskAndWindow Creates an image task, and immediately creates a graphical window for manipu...
Definition: imagetaskcreator.cpp:50

Here is the call graph for this function:

Here is the caller graph for this function:

void geometrize::network::completionhandlers::onWebpageDownloadComplete ( network::Downloader *  self,
QNetworkReply::NetworkError  error 
)

onWebpageDownloadComplete A callback when a webpage download completes.

Parameters
selfThe downloader that downloaded the webpage. Note this callback is responsible for deleting the downloader.
errorThe download error, if any.
52 {
53  const QByteArray data{self->getDownloadedData()};
54  const QUrl url{self->getUrl()};
55  self->deleteLater();
56 
57  if(error != QNetworkReply::NoError) {
58  // TODO post error to console or somewhere?
59  qDebug() << "FINISHED DOWNLOADING WEBPAGE WITH ERROR" << error; // TODO error checks
60  return;
61  }
62 
63  const QString document(data);
64 
65  QRegularExpression imageTagRegex("\\<img[^\\>]*src\\s*=\\s*\"([^\"]*)\"[^\\>]*\\>", QRegularExpression::CaseInsensitiveOption | QRegularExpression::InvertedGreedinessOption);
66 
67  QStringList imageMatches;
68  QStringList urlMatches;
69 
70  QRegularExpressionMatchIterator it = imageTagRegex.globalMatch(document);
71  while(it.hasNext()) {
72  QRegularExpressionMatch match = it.next();
73  imageMatches.append(match.captured(0)); // Should hold complete img tag
74  urlMatches.append(match.captured(1)); // Should hold only src property
75  }
76 
77  QList<QUrl> imageUrls;
78  for(const QString& url : urlMatches) {
79  imageUrls.push_back(QUrl(url));
80  }
81 
82  const QString currentPathUrl{url.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash | QUrl::RemoveFragment).toString()};
83  for(QUrl& imageUrl : imageUrls) {
84 
85  // Special cases for relative URLs
86  if(imageUrl.scheme().isEmpty()) {
87  const QString imageUrlString{imageUrl.toString(QUrl::PrettyDecoded)};
88  if(imageUrlString.startsWith("//")) {
89  // Some relative links are like //thedomain.com/example.png - so are relative and just missing the scheme
90  if(!url.scheme().isEmpty()) {
91  imageUrl.setScheme(url.scheme()); // Take the scheme from the source page
92  } else {
93  imageUrl.setScheme("http"); // Assume HTTP if it's '//' relative and have no clue about the scheme
94  }
95  } else if(imageUrlString.startsWith("/")) {
96  imageUrl = imageUrl.resolved(currentPathUrl); // A URL relative to the current path
97  } else {
98  imageUrl = QUrl(currentPathUrl + imageUrl.toString()); // Should be relative path based on the document root, so append the base URL
99  }
100  }
101 
103  }
104 }
void onImageDownloadComplete(network::Downloader *self, const QNetworkReply::NetworkError error)
onImageDownloadComplete A callback when an image download completes.
Definition: completionhandlers.cpp:24
void downloadImage(const QUrl &url, const std::function< void(network::Downloader *self, QNetworkReply::NetworkError error)> &onComplete)
downloadImage Downloads an image over the network.
Definition: networkactions.cpp:13

Here is the call graph for this function:

Here is the caller graph for this function: