File: Qt_progress_bar_callback.h

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (68 lines) | stat: -rw-r--r-- 1,393 bytes parent folder | download | duplicates (4)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef QT_PROGRESS_BAR_CALLBACK_H
#define QT_PROGRESS_BAR_CALLBACK_H

#include <CGAL/Real_timer.h>

#include <QProgressDialog>
#include <QApplication>

class Qt_progress_bar_callback
{

private:

  CGAL::Real_timer timer;
  double t_start;
  mutable double t_latest;

  mutable std::size_t nb;

  QProgressDialog* dialog;

public:

  Qt_progress_bar_callback()
  {
  }

  Qt_progress_bar_callback(const char* title, QWidget* parent)
    : dialog (new QProgressDialog (QString(title),
                                   QString("Cancel"),
                                   0, 100,
                                   parent))
  {
    dialog->setMinimumDuration(0);
    dialog->setWindowModality(Qt::WindowModal);
    timer.start();
    t_start = timer.time();
    t_latest = t_start;
  }
  ~Qt_progress_bar_callback()
  {
  }

  bool operator() (double advancement) const
  {
    // Avoid calling time() at every single iteration, which could
    // impact performances very badly
    ++ nb;
    if (advancement != 1 && nb % 1000 != 0)
      return true;

    // If the limit is reach, interrupt the algorithm
    double t = timer.time();
    if (advancement == 1 || (t - t_latest) > 0.25)
    {
      dialog->setValue (int (100. * advancement));
      t_latest = t;

      if (dialog->wasCanceled())
        return false;
    }

    return true;
  }
};


#endif // QT_PROGRESS_BAR_CALLBACK_H