##// END OF EJS Templates
Uses weak pointers in lambda functions to prevent a shared_ptr from ever being deleted (because a reference would always be kept in the lambda)
Alexandre Leroux -
r361:ca227f940fcb
parent child
Show More
@@ -1,206 +1,207
1 1 #include <Variable/Variable.h>
2 2 #include <Variable/VariableCacheController.h>
3 3 #include <Variable/VariableController.h>
4 4 #include <Variable/VariableModel.h>
5 5
6 6 #include <Data/DataProviderParameters.h>
7 7 #include <Data/IDataProvider.h>
8 8 #include <Data/IDataSeries.h>
9 9 #include <Time/TimeController.h>
10 10
11 11 #include <QDateTime>
12 12 #include <QMutex>
13 13 #include <QThread>
14 14 #include <QtCore/QItemSelectionModel>
15 15
16 16 #include <unordered_map>
17 17
18 18 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
19 19
20 20 namespace {
21 21
22 22 /// @todo Generates default dataseries, according to the provider passed in parameter. This method
23 23 /// will be deleted when the timerange is recovered from SciQlop
24 24 std::shared_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider,
25 25 const SqpDateTime &dateTime) noexcept
26 26 {
27 27 auto parameters = DataProviderParameters{dateTime};
28 28
29 29 return provider.retrieveData(parameters);
30 30 }
31 31
32 32 } // namespace
33 33
34 34 struct VariableController::VariableControllerPrivate {
35 35 explicit VariableControllerPrivate(VariableController *parent)
36 36 : m_WorkingMutex{},
37 37 m_VariableModel{new VariableModel{parent}},
38 38 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
39 39 m_VariableCacheController{std::make_unique<VariableCacheController>()}
40 40 {
41 41 }
42 42
43 43 QMutex m_WorkingMutex;
44 44 /// Variable model. The VariableController has the ownership
45 45 VariableModel *m_VariableModel;
46 46 QItemSelectionModel *m_VariableSelectionModel;
47 47
48 48
49 49 TimeController *m_TimeController{nullptr};
50 50 std::unique_ptr<VariableCacheController> m_VariableCacheController;
51 51
52 52 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
53 53 m_VariableToProviderMap;
54 54 };
55 55
56 56 VariableController::VariableController(QObject *parent)
57 57 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
58 58 {
59 59 qCDebug(LOG_VariableController()) << tr("VariableController construction")
60 60 << QThread::currentThread();
61 61 }
62 62
63 63 VariableController::~VariableController()
64 64 {
65 65 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
66 66 << QThread::currentThread();
67 67 this->waitForFinish();
68 68 }
69 69
70 70 VariableModel *VariableController::variableModel() noexcept
71 71 {
72 72 return impl->m_VariableModel;
73 73 }
74 74
75 75 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
76 76 {
77 77 return impl->m_VariableSelectionModel;
78 78 }
79 79
80 80 void VariableController::setTimeController(TimeController *timeController) noexcept
81 81 {
82 82 impl->m_TimeController = timeController;
83 83 }
84 84
85 85 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
86 86 {
87 87 if (!variable) {
88 88 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
89 89 return;
90 90 }
91 91
92 92 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
93 93 // make some treatments before the deletion
94 94 emit variableAboutToBeDeleted(variable);
95 95
96 96 // Deletes provider
97 97 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
98 98 qCDebug(LOG_VariableController())
99 99 << tr("Number of providers deleted for variable %1: %2")
100 100 .arg(variable->name(), QString::number(nbProvidersDeleted));
101 101
102 102 // Clears cache
103 103 impl->m_VariableCacheController->clear(variable);
104 104
105 105 // Deletes from model
106 106 impl->m_VariableModel->deleteVariable(variable);
107 107 }
108 108
109 109 void VariableController::deleteVariables(
110 110 const QVector<std::shared_ptr<Variable> > &variables) noexcept
111 111 {
112 112 for (auto variable : qAsConst(variables)) {
113 113 deleteVariable(variable);
114 114 }
115 115 }
116 116
117 117 void VariableController::createVariable(const QString &name,
118 118 std::shared_ptr<IDataProvider> provider) noexcept
119 119 {
120 120
121 121 if (!impl->m_TimeController) {
122 122 qCCritical(LOG_VariableController())
123 123 << tr("Impossible to create variable: The time controller is null");
124 124 return;
125 125 }
126 126
127 127
128 128 /// @todo : for the moment :
129 129 /// - the provider is only used to retrieve data from the variable for its initialization, but
130 130 /// it will be retained later
131 131 /// - default data are generated for the variable, without taking into account the timerange set
132 132 /// in sciqlop
133 133 auto dateTime = impl->m_TimeController->dateTime();
134 134 if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) {
135 135
136 136 // store the provider
137 137 impl->m_VariableToProviderMap[newVariable] = provider;
138 138
139 auto addDateTimeAcquired
140 = [this, newVariable](auto dataSeriesAcquired, auto dateTimeToPutInCache) {
141
142 impl->m_VariableCacheController->addDateTime(newVariable, dateTimeToPutInCache);
143 newVariable->setDataSeries(dataSeriesAcquired);
144
145 };
139 auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ](
140 auto dataSeriesAcquired, auto dateTimeToPutInCache)
141 {
142 if (auto variable = varW.lock()) {
143 impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache);
144 variable->setDataSeries(dataSeriesAcquired);
145 }
146 };
146 147
147 148 connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired);
148 149 this->onRequestDataLoading(newVariable, dateTime);
149 150 }
150 151 }
151 152
152 153 void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime)
153 154 {
154 155 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
155 156
156 157 for (const auto &selectedRow : qAsConst(selectedRows)) {
157 158 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
158 159 selectedVariable->setDateTime(dateTime);
159 160 this->onRequestDataLoading(selectedVariable, dateTime);
160 161 }
161 162 }
162 163 }
163 164
164 165
165 166 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
166 167 const SqpDateTime &dateTime)
167 168 {
168 169 // we want to load data of the variable for the dateTime.
169 170 // First we check if the cache contains some of them.
170 171 // For the other, we ask the provider to give them.
171 172 if (variable) {
172 173
173 174 auto dateTimeListNotInCache
174 175 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
175 176
176 177 if (!dateTimeListNotInCache.empty()) {
177 178 // Ask the provider for each data on the dateTimeListNotInCache
178 179 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(
179 180 std::move(dateTimeListNotInCache));
180 181 }
181 182 else {
182 183 emit variable->updated();
183 184 }
184 185 }
185 186 else {
186 187 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
187 188 }
188 189 }
189 190
190 191
191 192 void VariableController::initialize()
192 193 {
193 194 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
194 195 impl->m_WorkingMutex.lock();
195 196 qCDebug(LOG_VariableController()) << tr("VariableController init END");
196 197 }
197 198
198 199 void VariableController::finalize()
199 200 {
200 201 impl->m_WorkingMutex.unlock();
201 202 }
202 203
203 204 void VariableController::waitForFinish()
204 205 {
205 206 QMutexLocker locker{&impl->m_WorkingMutex};
206 207 }
@@ -1,183 +1,196
1 1 #include "Visualization/operations/GenerateVariableMenuOperation.h"
2 2 #include "Visualization/operations/MenuBuilder.h"
3 3
4 4 #include "Visualization/VisualizationGraphWidget.h"
5 5 #include "Visualization/VisualizationTabWidget.h"
6 6 #include "Visualization/VisualizationZoneWidget.h"
7 7
8 8 #include <Variable/Variable.h>
9 9
10 10 #include <QMenu>
11 11 #include <QStack>
12 12
13 13 Q_LOGGING_CATEGORY(LOG_GenerateVariableMenuOperation, "GenerateVariableMenuOperation")
14 14
15 15 struct GenerateVariableMenuOperation::GenerateVariableMenuOperationPrivate {
16 16 explicit GenerateVariableMenuOperationPrivate(QMenu *menu, std::shared_ptr<Variable> variable)
17 17 : m_Variable{variable}, m_PlotMenuBuilder{menu}, m_UnplotMenuBuilder{menu}
18 18 {
19 19 }
20 20
21 21 void visitRootEnter()
22 22 {
23 23 // Creates the root menu
24 24 m_PlotMenuBuilder.addMenu(QObject::tr("Plot"), QIcon{":/icones/plot.png"});
25 25 m_UnplotMenuBuilder.addMenu(QObject::tr("Unplot"), QIcon{":/icones/unplot.png"});
26 26 }
27 27
28 28 void visitRootLeave()
29 29 {
30 30 // Closes the root menu
31 31 m_PlotMenuBuilder.closeMenu();
32 32 m_UnplotMenuBuilder.closeMenu();
33 33 }
34 34
35 35 void visitNodeEnter(const IVisualizationWidget &container)
36 36 {
37 37 // Opens a new menu associated to the node
38 38 m_PlotMenuBuilder.addMenu(container.name());
39 39 m_UnplotMenuBuilder.addMenu(container.name());
40 40 }
41 41
42 42 template <typename ActionFun>
43 43 void visitNodeLeavePlot(const IVisualizationWidget &container, const QString &actionName,
44 44 ActionFun actionFunction)
45 45 {
46 46 if (m_Variable && container.canDrop(*m_Variable)) {
47 47 m_PlotMenuBuilder.addSeparator();
48 48 m_PlotMenuBuilder.addAction(actionName, actionFunction);
49 49 }
50 50
51 51 // Closes the menu associated to the node
52 52 m_PlotMenuBuilder.closeMenu();
53 53 }
54 54
55 55 void visitNodeLeaveUnplot()
56 56 {
57 57 // Closes the menu associated to the node
58 58 m_UnplotMenuBuilder.closeMenu();
59 59 }
60 60
61 61 template <typename ActionFun>
62 62 void visitLeafPlot(const IVisualizationWidget &container, const QString &actionName,
63 63 ActionFun actionFunction)
64 64 {
65 65 if (m_Variable && container.canDrop(*m_Variable)) {
66 66 m_PlotMenuBuilder.addAction(actionName, actionFunction);
67 67 }
68 68 }
69 69
70 70 template <typename ActionFun>
71 71 void visitLeafUnplot(const IVisualizationWidget &container, const QString &actionName,
72 72 ActionFun actionFunction)
73 73 {
74 74 if (m_Variable && container.contains(*m_Variable)) {
75 75 m_UnplotMenuBuilder.addAction(actionName, actionFunction);
76 76 }
77 77 }
78 78
79 79 std::shared_ptr<Variable> m_Variable;
80 80 MenuBuilder m_PlotMenuBuilder; ///< Builder for the 'Plot' menu
81 81 MenuBuilder m_UnplotMenuBuilder; ///< Builder for the 'Unplot' menu
82 82 };
83 83
84 84 GenerateVariableMenuOperation::GenerateVariableMenuOperation(QMenu *menu,
85 85 std::shared_ptr<Variable> variable)
86 86 : impl{spimpl::make_unique_impl<GenerateVariableMenuOperationPrivate>(menu, variable)}
87 87 {
88 88 }
89 89
90 90 void GenerateVariableMenuOperation::visitEnter(VisualizationWidget *widget)
91 91 {
92 92 // VisualizationWidget is not intended to accommodate a variable
93 93 Q_UNUSED(widget)
94 94
95 95 // 'Plot' and 'Unplot' menus
96 96 impl->visitRootEnter();
97 97 }
98 98
99 99 void GenerateVariableMenuOperation::visitLeave(VisualizationWidget *widget)
100 100 {
101 101 // VisualizationWidget is not intended to accommodate a variable
102 102 Q_UNUSED(widget)
103 103
104 104 // 'Plot' and 'Unplot' menus
105 105 impl->visitRootLeave();
106 106 }
107 107
108 108 void GenerateVariableMenuOperation::visitEnter(VisualizationTabWidget *tabWidget)
109 109 {
110 110 if (tabWidget) {
111 111 // 'Plot' and 'Unplot' menus
112 112 impl->visitNodeEnter(*tabWidget);
113 113 }
114 114 else {
115 115 qCCritical(LOG_GenerateVariableMenuOperation(),
116 116 "Can't visit enter VisualizationTabWidget : the widget is null");
117 117 }
118 118 }
119 119
120 120 void GenerateVariableMenuOperation::visitLeave(VisualizationTabWidget *tabWidget)
121 121 {
122 122 if (tabWidget) {
123 123 // 'Plot' menu
124 impl->visitNodeLeavePlot(
125 *tabWidget, QObject::tr("Open in a new zone"),
126 [ var = impl->m_Variable, tabWidget ]() { tabWidget->createZone(var); });
124 impl->visitNodeLeavePlot(*tabWidget, QObject::tr("Open in a new zone"),
125 [ varW = std::weak_ptr<Variable>{impl->m_Variable}, tabWidget ]() {
126 if (auto var = varW.lock()) {
127 tabWidget->createZone(var);
128 }
129 });
127 130
128 131 // 'Unplot' menu
129 132 impl->visitNodeLeaveUnplot();
130 133 }
131 134 else {
132 135 qCCritical(LOG_GenerateVariableMenuOperation(),
133 136 "Can't visit leave VisualizationTabWidget : the widget is null");
134 137 }
135 138 }
136 139
137 140 void GenerateVariableMenuOperation::visitEnter(VisualizationZoneWidget *zoneWidget)
138 141 {
139 142 if (zoneWidget) {
140 143 // 'Plot' and 'Unplot' menus
141 144 impl->visitNodeEnter(*zoneWidget);
142 145 }
143 146 else {
144 147 qCCritical(LOG_GenerateVariableMenuOperation(),
145 148 "Can't visit enter VisualizationZoneWidget : the widget is null");
146 149 }
147 150 }
148 151
149 152 void GenerateVariableMenuOperation::visitLeave(VisualizationZoneWidget *zoneWidget)
150 153 {
151 154 if (zoneWidget) {
152 155 // 'Plot' menu
153 156 impl->visitNodeLeavePlot(
154 157 *zoneWidget, QObject::tr("Open in a new graph"),
155 [ var = impl->m_Variable, zoneWidget ]() { zoneWidget->createGraph(var); });
158 [ varW = std::weak_ptr<Variable>{impl->m_Variable}, zoneWidget ]() {
159 if (auto var = varW.lock()) {
160 zoneWidget->createGraph(var);
161 }
162 });
156 163
157 164 // 'Unplot' menu
158 165 impl->visitNodeLeaveUnplot();
159 166 }
160 167 else {
161 168 qCCritical(LOG_GenerateVariableMenuOperation(),
162 169 "Can't visit leave VisualizationZoneWidget : the widget is null");
163 170 }
164 171 }
165 172
166 173 void GenerateVariableMenuOperation::visit(VisualizationGraphWidget *graphWidget)
167 174 {
168 175 if (graphWidget) {
169 176 // 'Plot' menu
170 impl->visitLeafPlot(
171 *graphWidget, QObject::tr("Open in %1").arg(graphWidget->name()),
172 [ var = impl->m_Variable, graphWidget ]() { graphWidget->addVariableUsingGraph(var); });
177 impl->visitLeafPlot(*graphWidget, QObject::tr("Open in %1").arg(graphWidget->name()),
178 [ varW = std::weak_ptr<Variable>{impl->m_Variable}, graphWidget ]() {
179 if (auto var = varW.lock()) {
180 graphWidget->addVariableUsingGraph(var);
181 }
182 });
173 183
174 184 // 'Unplot' menu
175 impl->visitLeafUnplot(
176 *graphWidget, QObject::tr("Remove from %1").arg(graphWidget->name()),
177 [ var = impl->m_Variable, graphWidget ]() { graphWidget->removeVariable(var); });
185 impl->visitLeafUnplot(*graphWidget, QObject::tr("Remove from %1").arg(graphWidget->name()),
186 [ varW = std::weak_ptr<Variable>{impl->m_Variable}, graphWidget ]() {
187 if (auto var = varW.lock()) {
188 graphWidget->removeVariable(var);
189 }
190 });
178 191 }
179 192 else {
180 193 qCCritical(LOG_GenerateVariableMenuOperation(),
181 194 "Can't visit VisualizationGraphWidget : the widget is null");
182 195 }
183 196 }
General Comments 0
You need to be logged in to leave comments. Login now