]> sigrok.org Git - pulseview.git/blob - pv/dialogs/storeprogress.cpp
Consistenty use auto-generated namespace comments.
[pulseview.git] / pv / dialogs / storeprogress.cpp
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
53 StoreProgress::~StoreProgress()
54 {
55         session_.wait();
56 }
57
58 void StoreProgress::run()
59 {
60         if (session_.start())
61                 show();
62         else
63                 show_error();
64 }
65
66 void StoreProgress::show_error()
67 {
68         QMessageBox msg(parentWidget());
69         msg.setText(tr("Failed to save session."));
70         msg.setInformativeText(session_.error());
71         msg.setStandardButtons(QMessageBox::Ok);
72         msg.setIcon(QMessageBox::Warning);
73         msg.exec();
74 }
75
76 void StoreProgress::closeEvent(QCloseEvent*)
77 {
78         session_.cancel();
79 }
80
81 void StoreProgress::on_progress_updated()
82 {
83         const pair<int, int> p = session_.progress();
84         assert(p.first <= p.second);
85
86         if (p.second) {
87                 setValue(p.first);
88                 setMaximum(p.second);
89         } else {
90                 const QString err = session_.error();
91                 if (!err.isEmpty())
92                         show_error();
93                 close();
94         }
95 }
96
97 }  // namespace dialogs
98 }  // namespace pv