]>
Commit | Line | Data |
---|---|---|
bf9f1268 SA |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net> | |
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 "globalsettings.hpp" | |
21 | ||
6f925ba9 UH |
22 | using std::function; |
23 | using std::map; | |
24 | using std::multimap; | |
25 | ||
bf9f1268 SA |
26 | namespace pv { |
27 | ||
28 | const QString GlobalSettings::Key_View_AlwaysZoomToFit = "View_AlwaysZoomToFit"; | |
29 | const QString GlobalSettings::Key_View_ColouredBG = "View_ColouredBG"; | |
87a97d8a | 30 | const QString GlobalSettings::Key_View_StickyScrolling = "View_StickyScrolling"; |
051ba3b3 | 31 | const QString GlobalSettings::Key_View_ShowSamplingPoints = "View_ShowSamplingPoints"; |
8ad61f40 | 32 | const QString GlobalSettings::Key_View_ShowAnalogMinorGrid = "View_ShowAnalogMinorGrid"; |
bf9f1268 | 33 | |
6f925ba9 | 34 | multimap< QString, function<void(QVariant)> > GlobalSettings::callbacks_; |
2cca9ebf | 35 | bool GlobalSettings::tracking_ = false; |
6f925ba9 | 36 | map<QString, QVariant> GlobalSettings::tracked_changes_; |
bf9f1268 SA |
37 | |
38 | GlobalSettings::GlobalSettings() : | |
39 | QSettings() | |
40 | { | |
41 | beginGroup("Settings"); | |
42 | } | |
43 | ||
44 | void GlobalSettings::register_change_handler(const QString key, | |
6f925ba9 | 45 | function<void(QVariant)> cb) |
bf9f1268 SA |
46 | { |
47 | callbacks_.emplace(key, cb); | |
48 | } | |
49 | ||
50 | void GlobalSettings::setValue(const QString &key, const QVariant &value) | |
51 | { | |
2cca9ebf SA |
52 | // Save previous value if we're tracking changes, |
53 | // not altering an already-existing saved setting | |
54 | if (tracking_) | |
55 | tracked_changes_.emplace(key, QSettings::value(key)); | |
56 | ||
bf9f1268 SA |
57 | QSettings::setValue(key, value); |
58 | ||
59 | // Call all registered callbacks for this key | |
60 | auto range = callbacks_.equal_range(key); | |
61 | ||
62 | for (auto it = range.first; it != range.second; it++) | |
63 | it->second(value); | |
64 | } | |
65 | ||
2cca9ebf SA |
66 | void GlobalSettings::start_tracking() |
67 | { | |
68 | tracking_ = true; | |
69 | tracked_changes_.clear(); | |
70 | } | |
71 | ||
72 | void GlobalSettings::stop_tracking() | |
73 | { | |
74 | tracking_ = false; | |
75 | tracked_changes_.clear(); | |
76 | } | |
77 | ||
78 | void GlobalSettings::undo_tracked_changes() | |
79 | { | |
80 | tracking_ = false; | |
81 | ||
82 | for (auto entry : tracked_changes_) | |
83 | setValue(entry.first, entry.second); | |
84 | ||
85 | tracked_changes_.clear(); | |
86 | } | |
bf9f1268 SA |
87 | |
88 | } // namespace pv |