File: progresswindow.cpp

package info (click to toggle)
aoflagger 3.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,688 kB
  • sloc: cpp: 83,116; python: 10,187; sh: 260; makefile: 178
file content (165 lines) | stat: -rw-r--r-- 5,355 bytes parent folder | download
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "progresswindow.h"

#include <iostream>

#include <boost/date_time/posix_time/posix_time.hpp>

#include <gtkmm/messagedialog.h>

ProgressWindow::ProgressWindow()
    : _blockProgressSignal(false),
      _currentTaskTitleLabel("Current task:"),
      _currentTaskLabel("-"),
      _timeElapsedTitleLabel("Time elapsed:"),
      _timeElapsedLabel("-"),
      _timeEstimatedTitleLabel("Estimated time:"),
      _timeEstimatedLabel("-"),
      _started(false),
      _exceptionQueued(false),
      _progress(0),
      _maxProgress(0),
      _finished(false) {
  set_default_size(350, 60);

  _currentTaskTitleLabel.set_vexpand(true);
  _currentTaskTitleLabel.set_alignment(Gtk::ALIGN_END);
  _currentTaskTitleLabel.set_padding(10, 2);
  _grid.attach(_currentTaskTitleLabel, 0, 0, 1, 1);
  _currentTaskLabel.set_alignment(Gtk::ALIGN_START);
  _grid.attach(_currentTaskLabel, 1, 0, 1, 1);
  _timeElapsedTitleLabel.set_vexpand(true);
  _timeElapsedTitleLabel.set_alignment(Gtk::ALIGN_END);
  _timeElapsedTitleLabel.set_padding(10, 2);
  _grid.attach(_timeElapsedTitleLabel, 0, 1, 1, 1);
  _timeElapsedLabel.set_alignment(Gtk::ALIGN_START);
  _grid.attach(_timeElapsedLabel, 1, 1, 1, 1);
  _timeEstimatedTitleLabel.set_vexpand(true);
  _timeEstimatedTitleLabel.set_alignment(Gtk::ALIGN_END);
  _timeEstimatedTitleLabel.set_padding(10, 2);
  _grid.attach(_timeEstimatedTitleLabel, 0, 2, 1, 1);
  _timeEstimatedLabel.set_alignment(Gtk::ALIGN_START);
  _grid.attach(_timeEstimatedLabel, 1, 2, 1, 1);
  _progressBar.set_hexpand(true);
  _progressBar.set_vexpand(true);
  _grid.attach(_progressBar, 0, 3, 2, 1);

  add(_grid);
  _grid.show_all();

  _progressChangeSignal.connect(
      sigc::mem_fun(*this, &ProgressWindow::updateProgress));
}

ProgressWindow::~ProgressWindow() = default;

void ProgressWindow::updateProgress() {
  if (!_blockProgressSignal) {
    if (!_started) {
      _startTime = boost::posix_time::microsec_clock::local_time();
      _lastUpdate = _startTime - boost::posix_time::time_duration(0, 0, 1);
      _started = true;
    }

    std::unique_lock<std::mutex> lock(_mutex);

    const boost::posix_time::ptime now =
        boost::posix_time::microsec_clock::local_time();
    const boost::posix_time::time_duration sinceLast = now - _lastUpdate;
    const boost::posix_time::time_duration updTime =
        boost::posix_time::millisec(100);
    const bool doUpdate = sinceLast >= updTime;
    if (doUpdate) {
      _lastUpdate = now;
      const boost::posix_time::time_duration duration = now - _startTime;
      std::stringstream timeStr;
      timeStr << duration;
      _timeElapsedLabel.set_text(timeStr.str());

      const std::string taskDesc = _taskDescription;
      const double progress = std::min(
          1.0, std::max(0.0, double(_progress) / double(_maxProgress)));

      _currentTaskLabel.set_text(taskDesc);
      _progressBar.set_fraction(progress);

      if (progress > 0.0) {
        std::stringstream estimatedTimeStr;
        boost::posix_time::time_duration estimated =
            (boost::posix_time::microsec_clock::local_time() - _startTime) *
            (int)(1000000.0 * (1.0 - progress)) / (int)(1000000.0 * progress);
        estimated = boost::posix_time::seconds(estimated.total_seconds());
        estimatedTimeStr << estimated;
        _timeEstimatedLabel.set_text(estimatedTimeStr.str());
      }
    }

    const bool hasException = _exceptionQueued;
    if (hasException) {
      _exceptionQueued = false;
      _blockProgressSignal = true;
      if (_signalError.empty()) {
        // Default handler: show a message dialog
        const std::string errMsg =
            std::string("An exception was thrown of type '") + _exceptionType +
            ("' -- ") + _exceptionDescription;
        lock.unlock();
        Gtk::MessageDialog dialog(*this, errMsg, false, Gtk::MESSAGE_ERROR);
        dialog.run();
      } else {
        const std::string errDescr =
            _exceptionDescription;  // local copy to unlock mutex
        lock.unlock();
        _signalError(errDescr);
      }
      _signalFinished(false);
      lock.lock();
      _blockProgressSignal = false;
    } else if (_finished) {
      _finished = false;  // Reset in case window is reused
      lock.unlock();

      _currentTaskLabel.set_text("-");
      _timeEstimatedLabel.set_text("-");
      _progressBar.set_fraction(1.0);

      // Parent might delete this window in this call -- don't do anything
      // after.
      _signalFinished(true);
    }
  }
}

void ProgressWindow::OnStartTask(const std::string& description) {
  std::unique_lock<std::mutex> lock(_mutex);
  _taskDescription = description;
  lock.unlock();

  _progressChangeSignal();
}

void ProgressWindow::OnFinish() {
  std::unique_lock<std::mutex> lock(_mutex);
  _finished = true;
  lock.unlock();

  _progressChangeSignal();
}

void ProgressWindow::OnProgress(size_t progress, size_t maxProgress) {
  std::unique_lock<std::mutex> lock(_mutex);
  _progress = progress;
  _maxProgress = maxProgress;
  lock.unlock();

  _progressChangeSignal();
}

void ProgressWindow::OnException(std::exception& thrownException) {
  std::unique_lock<std::mutex> lock(_mutex);
  _exceptionQueued = true;
  _exceptionDescription = thrownException.what();
  _exceptionType = typeid(thrownException).name();
  lock.unlock();

  _progressChangeSignal();
}