LivePlotController.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#pragma once
18
19#include <map>
20#include <memory>
21#include <mutex>
22#include <set>
23#include <string>
24#include <vector>
25
26#include <QObject>
27
29
32
33class QThread;
34class QTimer;
35
36namespace armarx
37{
39}
40
42{
43 class LivePlotWorker;
44
45 /**
46 * @brief Streams selected memory values to the DebugObserver for live plotting.
47 *
48 * Holds the (thread-safe) set of currently plotted values and runs a background worker that
49 * periodically reads them from the memory and pushes them to the DebugObserver (reusing
50 * \ref armarx::armem::client::util::MemoryToDebugObserver). Plotted values are highlighted in
51 * the views (via \ref valuesChanged) and all created channels are removed when this controller
52 * is destroyed (i.e. when the MemoryViewer GUI is closed).
53 */
54 class LivePlotController : public QObject, public armarx::Logging
55 {
56 Q_OBJECT
57 using This = LivePlotController;
58
59 public:
61
62 LivePlotController(std::shared_ptr<armem::gui::model::MemoryViewerModel> model);
63 ~LivePlotController() override;
64
65 // Called from the GUI thread (views).
66 bool hasDebugObserver() const;
67 bool isPlotted(const MemoryValueID& id) const;
68 bool allPlotted(const std::vector<MemoryValueID>& ids) const;
69 void setPlotted(const std::vector<MemoryValueID>& ids, bool enable);
70
71 // Called from the worker thread.
72 std::vector<MemoryValueID> valuesSnapshot() const;
73
74 // Lifecycle (called by MemoryViewer).
75 void onConnect();
76 void onDisconnect();
77
78 signals:
80
83
84 private:
85 void startWorker();
86 void removeAllChannels();
87
88 std::shared_ptr<armem::gui::model::MemoryViewerModel> _model;
89
90 mutable std::mutex _valuesMutex;
91 std::vector<MemoryValueID> _values;
92 std::set<std::string> _usedChannels;
93
94 QThread* _workerThread = nullptr;
95 LivePlotWorker* _worker = nullptr;
96 };
97
98 /**
99 * @brief Background worker that does the (blocking) memory queries and DebugObserver calls.
100 *
101 * Lives in its own thread; all DebugObserver mutations for the live plot go through this
102 * worker so that pushing and removing values are serialized (no zombie datafields).
103 */
104 class LivePlotWorker : public QObject, public armarx::Logging
105 {
106 Q_OBJECT
107 using This = LivePlotWorker;
108
109 public:
110 LivePlotWorker(std::shared_ptr<armem::gui::model::MemoryViewerModel> model,
112
113 public slots:
114 void init();
115 void cleanup();
116
117 void onConnect();
118 void onDisconnect();
119
120 private slots:
121 void onPoll();
122
123 private:
124 // Remove datafields that were sent last time but not in this poll (e.g. deactivated values).
125 void removeStaleDatafields(const std::map<std::string, std::set<std::string>>& current);
126
127 std::shared_ptr<armem::gui::model::MemoryViewerModel> _model;
128 LivePlotController* _controller;
129
130 QTimer* _timer = nullptr;
131 std::unique_ptr<armarx::DebugObserverHelper> _debugObserverHelper;
132 std::unique_ptr<armem::client::util::MemoryToDebugObserver> _memoryToDebugObserver;
133
134 std::map<std::string, std::set<std::string>> _lastPushed;
135 };
136
137} // namespace armarx::armem::gui::controller
Brief description of class DebugObserverHelper.
Base Class for all Logging classes.
Definition Logging.h:240
Streams selected memory values to the DebugObserver for live plotting.
LivePlotController(std::shared_ptr< armem::gui::model::MemoryViewerModel > model)
bool allPlotted(const std::vector< MemoryValueID > &ids) const
void setPlotted(const std::vector< MemoryValueID > &ids, bool enable)
armem::client::util::MemoryValueID MemoryValueID
std::vector< MemoryValueID > valuesSnapshot() const
Background worker that does the (blocking) memory queries and DebugObserver calls.
LivePlotWorker(std::shared_ptr< armem::gui::model::MemoryViewerModel > model, LivePlotController *controller)
This file offers overloads of toIce() and fromIce() functions for STL container types.