]>
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 <QDebug> | |
23 | #include <QMessageBox> | |
24 | ||
25 | #include "pv/session.hpp" | |
26 | ||
27 | #include "storeprogress.hpp" | |
28 | ||
29 | using std::map; | |
30 | using std::pair; | |
31 | using std::shared_ptr; | |
32 | using std::string; | |
33 | ||
34 | using Glib::VariantBase; | |
35 | ||
36 | namespace pv { | |
37 | namespace dialogs { | |
38 | ||
39 | StoreProgress::StoreProgress(const QString &file_name, | |
40 | const shared_ptr<sigrok::OutputFormat> output_format, | |
41 | const map<string, VariantBase> &options, | |
42 | const pair<uint64_t, uint64_t> sample_range, | |
43 | const Session &session, QWidget *parent) : | |
44 | QProgressDialog(tr("Saving..."), tr("Cancel"), 0, 0, parent), | |
45 | session_(file_name.toStdString(), output_format, options, sample_range, | |
46 | session) | |
47 | { | |
48 | connect(&session_, SIGNAL(progress_updated()), | |
49 | this, SLOT(on_progress_updated())); | |
50 | connect(&session_, SIGNAL(store_successful()), | |
51 | &session, SLOT(on_data_saved())); | |
52 | ||
53 | // Since we're not setting any progress in case of an error, the dialog | |
54 | // will pop up after the minimumDuration time has been reached - 4000 ms | |
55 | // by default. | |
56 | // We do not want this as it overlaps with the error message box, so we | |
57 | // set the minimumDuration to 0 so that it only appears when we feed it | |
58 | // progress data. Then, call reset() to prevent the progress dialog from | |
59 | // popping up anyway. This would happen in Qt5 because the behavior was | |
60 | // changed in such a way that the duration timer is started by the | |
61 | // constructor. We don't want that and reset() stops the timer, so we | |
62 | // use it. | |
63 | setMinimumDuration(0); | |
64 | reset(); | |
65 | } | |
66 | ||
67 | StoreProgress::~StoreProgress() | |
68 | { | |
69 | session_.wait(); | |
70 | } | |
71 | ||
72 | void StoreProgress::run() | |
73 | { | |
74 | if (session_.start()) | |
75 | show(); | |
76 | else | |
77 | show_error(); | |
78 | } | |
79 | ||
80 | void StoreProgress::show_error() | |
81 | { | |
82 | qDebug() << "Error trying to save:" << session_.error(); | |
83 | ||
84 | QMessageBox msg(parentWidget()); | |
85 | msg.setText(tr("Failed to save session.") + "\n\n" + session_.error()); | |
86 | msg.setStandardButtons(QMessageBox::Ok); | |
87 | msg.setIcon(QMessageBox::Warning); | |
88 | msg.exec(); | |
89 | ||
90 | close(); | |
91 | } | |
92 | ||
93 | void StoreProgress::closeEvent(QCloseEvent*) | |
94 | { | |
95 | session_.cancel(); | |
96 | ||
97 | // Closing doesn't mean we're going to be destroyed because our parent | |
98 | // still owns our handle. Make sure this stale instance doesn't hang around. | |
99 | deleteLater(); | |
100 | } | |
101 | ||
102 | void StoreProgress::on_progress_updated() | |
103 | { | |
104 | const pair<int, int> p = session_.progress(); | |
105 | assert(p.first <= p.second); | |
106 | ||
107 | if (p.second) { | |
108 | setValue(p.first); | |
109 | setMaximum(p.second); | |
110 | } else { | |
111 | const QString err = session_.error(); | |
112 | if (!err.isEmpty()) | |
113 | show_error(); | |
114 | close(); | |
115 | } | |
116 | } | |
117 | ||
118 | } // namespace dialogs | |
119 | } // namespace pv |