Geometrize  1.0
An application for geometrizing images into geometric primitives
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

◆ onImageDownloadComplete()

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.
27 {
28  const QByteArray data{self->getDownloadedData()};
29  const QUrl url{self->getUrl()};
30  self->deleteLater();
31 
32  if(error != QNetworkReply::NoError) {
33  // TODO post error to console or somewhere?
34  qDebug() << "FINISHED DOWNLOADING WEBPAGE WITH ERROR" << error;
35  return;
36  }
37 
38  QImage image{QImage::fromData(data)};
39  if(image.isNull()) {
40  qDebug() << "FAILED TO CREATE IMAGE FROM DOWNLOADED DATA BUFFER";
41  return;
42  }
43 
44  image = image.convertToFormat(QImage::Format_RGBA8888); // Note: to guarantee format is RGBA8888
45  if(image.format() != QImage::Format_RGBA8888) {
46  qDebug() << "FAILED TO CONVERT IMAGE TO RGBA8888";
47  return;
48  }
49 
50  task::createImageTaskAndWindow(url.toString().toStdString(), image, std::nullopt);
51 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ onWebpageDownloadComplete()

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