]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <cassert> | |
21 | ||
22 | #include <QMessageBox> | |
23 | ||
24 | #include "pv/session.hpp" | |
25 | ||
26 | #include "storeprogress.hpp" | |
27 | ||
28 | using std::map; | |
29 | using std::pair; | |
30 | using std::shared_ptr; | |
31 | using std::string; | |
32 | ||
33 | using Glib::VariantBase; | |
34 | ||
35 | namespace pv { | |
36 | namespace dialogs { | |
37 | ||
38 | StoreProgress::StoreProgress(const QString &file_name, | |
39 | const shared_ptr<sigrok::OutputFormat> output_format, | |
40 | const map<string, VariantBase> &options, | |
41 | const pair<uint64_t, uint64_t> sample_range, | |
42 | const Session &session, QWidget *parent) : | |
43 | QProgressDialog(tr("Saving..."), tr("Cancel"), 0, 0, parent), | |
44 | session_(file_name.toStdString(), output_format, options, sample_range, | |
45 | session) | |
46 | { | |
47 | connect(&session_, SIGNAL(progress_updated()), | |
48 | this, SLOT(on_progress_updated())); | |
49 | connect(&session_, SIGNAL(store_successful()), | |
50 | &session, SLOT(on_data_saved())); | |
51 | ||
52 | // Since we're not setting any progress in case of an error, the dialog | |
53 | // will pop up after the minimumDuration time has been reached - 4000 ms | |
54 | // by default. | |
55 | // We do not want this as it overlaps with the error message box, so we | |
56 | // set the minimumDuration to 0 so that it only appears when we feed it | |
57 | // progress data. Then, call reset() to prevent the progress dialog from | |
58 | // popping up anyway. This would happen in Qt5 because the behavior was | |
59 | // changed in such a way that the duration timer is started by the | |
60 | // constructor. We don't want that and reset() stops the timer, so we | |
61 | // use it. | |
62 | setMinimumDuration(0); | |
63 | reset(); | |
64 | } | |
65 | ||
66 | StoreProgress::~StoreProgress() | |
67 | { | |
68 | session_.wait(); | |
69 | } | |
70 | ||
71 | void StoreProgress::run() | |
72 | { | |
73 | if (session_.start()) | |
74 | show(); | |
75 | else | |
76 | show_error(); | |
77 | } | |
78 | ||
79 | void StoreProgress::show_error() | |
80 | { | |
81 | QMessageBox msg(parentWidget()); | |
82 | msg.setText(tr("Failed to save session.")); | |
83 | msg.setInformativeText(session_.error()); | |
84 | msg.setStandardButtons(QMessageBox::Ok); | |
85 | msg.setIcon(QMessageBox::Warning); | |
86 | msg.exec(); | |
87 | ||
88 | close(); | |
89 | } | |
90 | ||
91 | void StoreProgress::closeEvent(QCloseEvent*) | |
92 | { | |
93 | session_.cancel(); | |
94 | ||
95 | // Closing doesn't mean we're going to be destroyed because our parent | |
96 | // still owns our handle. Make sure this stale instance doesn't hang around. | |
97 | deleteLater(); | |
98 | } | |
99 | ||
100 | void StoreProgress::on_progress_updated() | |
101 | { | |
102 | const pair<int, int> p = session_.progress(); | |
103 | assert(p.first <= p.second); | |
104 | ||
105 | if (p.second) { | |
106 | setValue(p.first); | |
107 | setMaximum(p.second); | |
108 | } else { | |
109 | const QString err = session_.error(); | |
110 | if (!err.isEmpty()) | |
111 | show_error(); | |
112 | close(); | |
113 | } | |
114 | } | |
115 | ||
116 | } // namespace dialogs | |
117 | } // namespace pv |