swift
interpolationsetupprovider.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 using namespace swift::misc::aviation;
7 
8 namespace swift::misc::simulation
9 {
10  CInterpolationAndRenderingSetupPerCallsign
11  IInterpolationSetupProvider::getInterpolationSetupPerCallsignOrDefault(const CCallsign &callsign) const
12  {
13  QReadLocker l(&m_lockSetup);
14  if (!m_setupsPerCallsign.contains(callsign))
15  {
16  return CInterpolationAndRenderingSetupPerCallsign(callsign, m_globalSetup);
17  }
18  return m_setupsPerCallsign.value(callsign);
19  }
20 
21  CInterpolationSetupList IInterpolationSetupProvider::getInterpolationSetupsPerCallsign() const
22  {
23  const SetupsPerCallsign setups = this->getSetupsPerCallsign();
24  return CInterpolationSetupList(setups.values());
25  }
26 
27  bool IInterpolationSetupProvider::hasSetupsPerCallsign() const
28  {
29  QReadLocker l(&m_lockSetup);
30  return !m_setupsPerCallsign.isEmpty();
31  }
32 
33  bool IInterpolationSetupProvider::setInterpolationSetupsPerCallsign(const CInterpolationSetupList &setups,
34  bool ignoreSameAsGlobal)
35  {
36  const CInterpolationAndRenderingSetupGlobal gs = this->getInterpolationSetupGlobal();
37  SetupsPerCallsign setupsPerCs;
38  for (const CInterpolationAndRenderingSetupPerCallsign &setup : setups)
39  {
40  if (ignoreSameAsGlobal && setup.isEqualToGlobal(gs)) { continue; }
41  setupsPerCs.insert(setup.getCallsign(), setup);
42  }
43  {
44  QWriteLocker l(&m_lockSetup);
45  if (m_setupsPerCallsign.isEmpty() && setupsPerCs.isEmpty()) { return false; }
46  m_setupsPerCallsign = setupsPerCs;
47  }
48  this->emitInterpolationSetupChanged();
49  return true;
50  }
51 
52  CInterpolationAndRenderingSetupGlobal IInterpolationSetupProvider::getInterpolationSetupGlobal() const
53  {
54  QReadLocker l(&m_lockSetup);
55  return m_globalSetup;
56  }
57 
58  CCallsignSet IInterpolationSetupProvider::getLogCallsigns() const
59  {
60  const SetupsPerCallsign setups = this->getSetupsPerCallsign();
61  CCallsignSet callsigns;
62  for (const auto [callsign, setup] : makePairsRange(setups))
63  {
64  if (setup.logInterpolation()) { callsigns.insert(callsign); }
65  }
66  return callsigns;
67  }
68 
69  bool IInterpolationSetupProvider::isLogCallsign(const CCallsign &callsign) const
70  {
71  QReadLocker l(&m_lockSetup);
72  if (!m_setupsPerCallsign.contains(callsign)) { return false; }
73  return m_setupsPerCallsign[callsign].logInterpolation();
74  }
75 
76  bool IInterpolationSetupProvider::setInterpolationMode(const QString &modeAsString, const CCallsign &callsign)
77  {
78  CInterpolationAndRenderingSetupPerCallsign setup = this->getInterpolationSetupPerCallsignOrDefault(callsign);
79  if (!setup.setInterpolatorMode(modeAsString)) { return false; }
80 
81  // changed value
82  return this->setInterpolationSetupPerCallsign(setup, callsign, true);
83  }
84 
85  bool IInterpolationSetupProvider::setLogInterpolation(bool log, const CCallsign &callsign)
86  {
87  CInterpolationAndRenderingSetupPerCallsign setup = this->getInterpolationSetupPerCallsignOrDefault(callsign);
88  if (!setup.setLogInterpolation(log)) { return false; }
89 
90  // changed value
91  return this->setInterpolationSetupPerCallsign(setup, callsign, true);
92  }
93 
94  bool IInterpolationSetupProvider::setInterpolationSetupGlobal(const CInterpolationAndRenderingSetupGlobal &setup)
95  {
96  {
97  QWriteLocker l(&m_lockSetup);
98  if (m_globalSetup == setup) { return false; }
99  m_globalSetup = setup;
100  }
101  this->emitInterpolationSetupChanged();
102  return true;
103  }
104 
105  bool IInterpolationSetupProvider::setInterpolationSetupPerCallsign(
106  const CInterpolationAndRenderingSetupPerCallsign &setup, const CCallsign &callsign, bool removeGlobalSetup)
107  {
108  if (removeGlobalSetup)
109  {
110  const CInterpolationAndRenderingSetupGlobal gs = this->getInterpolationSetupGlobal();
111  if (setup.isEqualToGlobal(gs))
112  {
113  QWriteLocker l(&m_lockSetup);
114  m_setupsPerCallsign.remove(callsign);
115  return false;
116  }
117  }
118  {
119  QWriteLocker l(&m_lockSetup);
120  m_setupsPerCallsign[callsign] = setup;
121  }
122  this->emitInterpolationSetupChanged();
123  return true;
124  }
125 
126  bool IInterpolationSetupProvider::removeInterpolationSetupPerCallsign(const CCallsign &callsign)
127  {
128  bool removed = false;
129  {
130  QWriteLocker l(&m_lockSetup);
131  removed = m_setupsPerCallsign.remove(callsign) > 0;
132  }
133  if (removed) { this->emitInterpolationSetupChanged(); }
134  return removed;
135  }
136 
137  void IInterpolationSetupProvider::setLogCallsign(bool log, const CCallsign &callsign)
138  {
139  CInterpolationAndRenderingSetupPerCallsign setup = this->getInterpolationSetupPerCallsignOrDefault(callsign);
140  if (setup.logInterpolation() == log) { return; }
141  setup.setLogInterpolation(log);
142  this->setInterpolationSetupPerCallsign(setup, callsign);
143  }
144 
145  void IInterpolationSetupProvider::clearInterpolationLogCallsigns()
146  {
147  SetupsPerCallsign setupsCopy = this->getSetupsPerCallsign();
148  if (setupsCopy.isEmpty()) { return; }
149 
150  // potential risk, changes inbetween in another thread are missed now
151  // on the other side, we keep locks for a minimal time frame
152  SetupsPerCallsign setupsToKeep;
153  CInterpolationAndRenderingSetupGlobal global = this->getInterpolationSetupGlobal();
154  for (auto [callsign, setup] : makePairsRange(setupsCopy))
155  {
156  setup.setLogInterpolation(false);
157  if (setup.isEqualToGlobal(global)) { continue; }
158  setupsToKeep.insert(callsign, setup);
159  }
160  {
161  QWriteLocker l(&m_lockSetup);
162  m_setupsPerCallsign = setupsToKeep;
163  }
164  this->emitInterpolationSetupChanged();
165  }
166 
167  int IInterpolationSetupProvider::clearInterpolationSetupsPerCallsign()
168  {
169  int r = 0;
170  {
171  QWriteLocker l(&m_lockSetup);
172  r = m_setupsPerCallsign.size();
173  m_setupsPerCallsign.clear();
174  }
175 
176  if (r > 0) { this->emitInterpolationSetupChanged(); }
177  return r;
178  }
179 
180  bool IInterpolationSetupProvider::logAnyCallsign() const
181  {
182  const SetupsPerCallsign setupsCopy = this->getSetupsPerCallsign();
183  if (setupsCopy.isEmpty()) { return false; }
184  for (const CInterpolationAndRenderingSetupPerCallsign &setup : setupsCopy)
185  {
186  if (setup.logInterpolation()) { return true; }
187  }
188  return false;
189  }
190 
191  IInterpolationSetupProvider::SetupsPerCallsign IInterpolationSetupProvider::getSetupsPerCallsign() const
192  {
193  QReadLocker l(&m_lockSetup);
194  return m_setupsPerCallsign;
195  }
196 
197  // pin vtables to this file
198  void CInterpolationSetupAware::anchor() {}
199 
200  CInterpolationAndRenderingSetupPerCallsign
201  CInterpolationSetupAware::getInterpolationSetupPerCallsignOrDefault(const CCallsign &callsign) const
202  {
203  if (!this->hasProvider()) { return CInterpolationAndRenderingSetupPerCallsign(); }
204  return this->provider()->getInterpolationSetupPerCallsignOrDefault(callsign);
205  }
206 
207  CInterpolationAndRenderingSetupGlobal CInterpolationSetupAware::getInterpolationSetupGlobal() const
208  {
209  if (!this->hasProvider()) { return CInterpolationAndRenderingSetupGlobal(); }
210  return this->provider()->getInterpolationSetupGlobal();
211  }
212 } // namespace swift::misc::simulation
iterator insert(const_iterator hint, const T &value)
For compatibility with std::inserter.
Definition: collection.h:199
Value object encapsulating information of a callsign.
Definition: callsign.h:30
Value object for a set of callsigns.
Definition: callsignset.h:26
bool setInterpolatorMode(InterpolatorMode mode)
Set interpolator mode.
Value object for interpolator and rendering per callsign.
bool isEqualToGlobal(const CInterpolationAndRenderingSetupGlobal &globalSetup) const
Equal to global setup?
auto makePairsRange(const T &container)
Returns a const CRange for iterating over the keys and values of a Qt associative container.
Definition: range.h:374