progressreporterwrapper.hpp 1.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm

#ifndef PROGRESSREPORTERWRAPPER_HPP
#define PROGRESSREPORTERWRAPPER_HPP

#include "libvideostitch/algorithm.hpp"

#include <QProgressBar>

#include <atomic>

class VS_GUI_EXPORT ProgressReporterWrapper : public QProgressBar,
                                              public VideoStitch::Util::Algorithm::ProgressReporter {
  Q_OBJECT

 public:
  explicit ProgressReporterWrapper(QWidget *parent = 0)
      : QProgressBar(parent), VideoStitch::Util::Algorithm::ProgressReporter(), isCanceled(false), valueToDisplay(0) {
    setRange(0, 100);
    connect(this, &ProgressReporterWrapper::reqSetValue, this, &ProgressReporterWrapper::setValue);
  }

  bool hasBeenCanceled() { return isCanceled; }

 signals:
  void reqSetValue(int);
  void reqProgressMessage(QString);

 public slots:
  /**
   * @brief notify thread-safe function to update the progress bar.
   */
  bool notify(const std::string &message, double percent) {
    // If we have several running algorithms linked to the progress bar,
    // valueToDisplay assures us that the progress bar will not go back
    valueToDisplay = qMax(valueToDisplay, qRound(percent));
    emit reqSetValue(valueToDisplay);
    emit reqProgressMessage(QString::fromStdString(message));
    return isCanceled;
  }

  void cancel() { isCanceled = true; }

  void reset() {
    isCanceled = false;
    valueToDisplay = 0;
  }

 private:
  std::atomic<bool> isCanceled;
  int valueToDisplay;
};

#endif  // PROGRESSREPORTERWRAPPER_HPP