Use nullptr in a few places.
[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::string;
30
31 using Glib::VariantBase;
32
33 namespace pv {
34 namespace dialogs {
35
36 StoreProgress::StoreProgress(const QString &file_name,
37         const std::shared_ptr<sigrok::OutputFormat> output_format,
38         const map<string, VariantBase> &options,
39         const std::pair<uint64_t, uint64_t> sample_range,
40         const Session &session, QWidget *parent) :
41         QProgressDialog(tr("Saving..."), tr("Cancel"), 0, 0, parent),
42         session_(file_name.toStdString(), output_format, options, sample_range,
43                 session)
44 {
45         connect(&session_, SIGNAL(progress_updated()),
46                 this, SLOT(on_progress_updated()));
47         connect(&session_, SIGNAL(store_successful()),
48                 &session, SLOT(on_data_saved()));
49 }
50
51 StoreProgress::~StoreProgress()
52 {
53         session_.wait();
54 }
55
56 void StoreProgress::run()
57 {
58         if (session_.start())
59                 show();
60         else
61                 show_error();
62 }
63
64 void StoreProgress::show_error()
65 {
66         QMessageBox msg(parentWidget());
67         msg.setText(tr("Failed to save session."));
68         msg.setInformativeText(session_.error());
69         msg.setStandardButtons(QMessageBox::Ok);
70         msg.setIcon(QMessageBox::Warning);
71         msg.exec();
72 }
73
74 void StoreProgress::closeEvent(QCloseEvent*)
75 {
76         session_.cancel();
77 }
78
79 void StoreProgress::on_progress_updated()
80 {
81         const std::pair<int, int> p = session_.progress();
82         assert(p.first <= p.second);
83
84         if (p.second) {
85                 setValue(p.first);
86                 setMaximum(p.second);
87         } else {
88                 const QString err = session_.error();
89                 if (!err.isEmpty())
90                         show_error();
91                 close();
92         }
93 }
94
95 } // dialogs
96 } // pv