Nicolas Winkler пре 5 година
родитељ
комит
2c97923bb6
4 измењених фајлова са 27 додато и 15 уклоњено
  1. 15 6
      Almond.cpp
  2. 3 2
      Almond.h
  3. 6 5
      BackgroundTask.cpp
  4. 3 2
      BackgroundTask.h

+ 15 - 6
Almond.cpp

@@ -92,6 +92,12 @@ void Almond::submitBackgroundTask(BackgroundTask* task)
     //}
 }
 
+
+void Almond::stopBackgroundTask(void)
+{
+    stoppingBackgroundTasks = true;
+}
+
 bool Almond::eventFilter(QObject *target, QEvent *event)
 {
     if (event->type() == QEvent::KeyPress) {
@@ -138,6 +144,7 @@ void Almond::backgroundTaskFinished(bool succ, QString message)
     ui.backgroundProgress->setFormat("");
     ui.backgroundProgress->setEnabled(false);
     ui.cancelProgress->setEnabled(false);
+    stoppingBackgroundTasks = false;
 }
 
 
@@ -256,7 +263,7 @@ void Almond::on_exportImage_clicked()
         iei.gradient = mw->getGradient();
         iei.path = dialog.getPath().toStdString();
         iei.options.jpegQuality = 95;
-        submitBackgroundTask(new ImageExportTask(iei));
+        submitBackgroundTask(new ImageExportTask(iei, [this] () { return stoppingBackgroundTasks; }));
 
         /*auto exprt = [iei, path = dialog.getPath().toStdString()]() {
             alm::exportPng(path, iei);
@@ -338,11 +345,6 @@ void Almond::pointSelected(mnd::Real x, mnd::Real y)
 }
 
 
-void Almond::on_groupBox_toggled(bool arg1)
-{
-    printf("arg1: %i\n", int(arg1)); fflush(stdout);
-}
-
 void Almond::on_wMandel_clicked()
 {
 
@@ -431,6 +433,7 @@ void Almond::on_radioButton_2_toggled(bool checked)
     }
 }
 
+
 void Almond::on_createCustom_clicked()
 {
     auto response = customGeneratorDialog->exec();
@@ -444,3 +447,9 @@ void Almond::on_createCustom_clicked()
         setViewType(CUSTOM);
     }
 }
+
+
+void Almond::on_cancelProgress_clicked()
+{
+    stopBackgroundTask();
+}

+ 3 - 2
Almond.h

@@ -35,6 +35,7 @@ class Almond : public QMainWindow
 private:
     mnd::MandelContext mandelContext;
     QThreadPool backgroundTasks;
+    bool stoppingBackgroundTasks = false;
 
 
     bool fullscreenMode = false;
@@ -62,6 +63,7 @@ public:
     ~Almond(void);
 
     void submitBackgroundTask(BackgroundTask* task);
+    void stopBackgroundTask();
 
     bool eventFilter(QObject *target, QEvent *event);
 public slots:
@@ -87,8 +89,6 @@ private slots:
 
     void on_wMandel_toggled(bool checked);
 
-    void on_groupBox_toggled(bool arg1);
-
     void saveView(void);
     void setViewType(ViewType v);
 
@@ -96,6 +96,7 @@ private slots:
     void on_radioButton_toggled(bool checked);
     void on_radioButton_2_toggled(bool checked);
     void on_createCustom_clicked();
+    void on_cancelProgress_clicked();
 
 private:
     Ui::AlmondClass ui;

+ 6 - 5
BackgroundTask.cpp

@@ -1,13 +1,14 @@
 #include "BackgroundTask.h"
 
-BackgroundTask::BackgroundTask(const std::string& shortDescription) :
-    shortDescription{ shortDescription }
+BackgroundTask::BackgroundTask(const std::string& shortDescription, std::function<bool(void)> stopCallback) :
+    shortDescription{ shortDescription },
+    stopCallback{ std::move(stopCallback) }
 {
 }
 
 
-ImageExportTask::ImageExportTask(const alm::ImageExportInfo& iei) :
-    BackgroundTask{ "Exporting Image" },
+ImageExportTask::ImageExportTask(const alm::ImageExportInfo& iei, std::function<bool(void)> stopCallback) :
+    BackgroundTask{ "Exporting Image", std::move(stopCallback) },
     iei{ iei }
 {
 }
@@ -18,7 +19,7 @@ void ImageExportTask::run(void)
     try {
         alm::exportImage(iei, [this](float percentage) {
             emit progress(percentage);
-        });
+        }, stopCallback);
         emit finished(true, "Image successfully exported.");
     }
     catch (alm::ImageExportException& ex) {

+ 3 - 2
BackgroundTask.h

@@ -13,8 +13,9 @@ class BackgroundTask : public QObject, public QRunnable
     Q_OBJECT
 protected:
     std::string shortDescription;
+    std::function<bool(void)> stopCallback;
 public:
-    BackgroundTask(const std::string& shortDescription);
+    BackgroundTask(const std::string& shortDescription, std::function<bool(void)> stopCallback = [] () { return false; });
 
     void run(void) = 0;
 
@@ -32,7 +33,7 @@ class ImageExportTask : public BackgroundTask
 private:
     const alm::ImageExportInfo iei;
 public:
-    ImageExportTask(const alm::ImageExportInfo& iei);
+    ImageExportTask(const alm::ImageExportInfo& iei, std::function<bool(void)> stopCallback = [] () { return false; });
 
     void run(void) override;
 };