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  QRegExp imageTagRegex("\\<img[^\\>]*src\\s*=\\s*\"([^\"]*)\"[^\\>]*\\>", Qt::CaseInsensitive);
66  imageTagRegex.setMinimal(true);
67 
68  QStringList imageMatches;
69  QStringList urlMatches;
70 
71  int offset{0};
72  while((offset = imageTagRegex.indexIn(document, offset)) != -1) {
73  offset += imageTagRegex.matchedLength();
74  imageMatches.append(imageTagRegex.cap(0)); // Should hold complete img tag
75  urlMatches.append(imageTagRegex.cap(1)); // Should hold only src property
76  }
77 
78  QList<QUrl> imageUrls;
79  for(const QString& url : urlMatches) {
80  imageUrls.push_back(QUrl(url));
81  }
82 
83  const QString currentPathUrl{url.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash | QUrl::RemoveFragment).toString()};
84  for(QUrl& imageUrl : imageUrls) {
85 
86  // Special cases for relative URLs
87  if(imageUrl.scheme().isEmpty()) {
88  const QString imageUrlString{imageUrl.toString(QUrl::PrettyDecoded)};
89  if(imageUrlString.startsWith("//")) {
90  // Some relative links are like //thedomain.com/example.png - so are relative and just missing the scheme
91  if(!url.scheme().isEmpty()) {
92  imageUrl.setScheme(url.scheme()); // Take the scheme from the source page
93  } else {
94  imageUrl.setScheme("http"); // Assume HTTP if it's '//' relative and have no clue about the scheme
95  }
96  } else if(imageUrlString.startsWith("/")) {
97  imageUrl = imageUrl.resolved(currentPathUrl); // A URL relative to the current path
98  } else {
99  imageUrl = QUrl(currentPathUrl + imageUrl.toString()); // Should be relative path based on the document root, so append the base URL
100  }
101  }
102 
104  }
105 }
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: