plot_docker.cpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5  */
6 
7 #include "plot_docker.h"
8 #include "plotwidget_editor.h"
9 #include "Qads/DockSplitter.h"
10 #include <QPushButton>
11 #include <QBoxLayout>
12 #include <QMouseEvent>
13 #include <QSplitter>
14 #include <QDebug>
15 #include <QInputDialog>
16 #include <QLineEdit>
17 #include "PlotJuggler/svg_util.h"
18 
20 {
21 public:
23  createDockAreaTitleBar(ads::CDockAreaWidget* dock_area) const override
24  {
25  auto title_bar = new ads::CDockAreaTitleBar(dock_area);
26  title_bar->setVisible(false);
27  return title_bar;
28  }
29 };
30 
31 PlotDocker::PlotDocker(QString name, PlotDataMapRef& datamap, QWidget* parent)
32  : ads::CDockManager(parent), _name(name), _datamap(datamap)
33 {
35 
36  auto CreateFirstWidget = [&]() {
37  if (dockAreaCount() == 0)
38  {
39  DockWidget* widget = new DockWidget(datamap, this);
40 
41  auto area = addDockWidget(ads::TopDockWidgetArea, widget);
42  area->setAllowedAreas(ads::OuterDockAreas);
43 
44  this->plotWidgetAdded(widget->plotWidget());
45 
47  }
48  };
49 
50  connect(this, &ads::CDockManager::dockWidgetRemoved, this, CreateFirstWidget);
51 
53 
54  CreateFirstWidget();
55 }
56 
58 {
59 }
60 
61 QString PlotDocker::name() const
62 {
63  return _name;
64 }
65 
66 void PlotDocker::setName(QString name)
67 {
68  _name = name;
69 }
70 
71 QDomElement saveChildNodesState(QDomDocument& doc, QWidget* widget)
72 {
73  QSplitter* splitter = qobject_cast<QSplitter*>(widget);
74  if (splitter)
75  {
76  QDomElement splitter_elem = doc.createElement("DockSplitter");
77  splitter_elem.setAttribute("orientation",
78  (splitter->orientation() == Qt::Horizontal) ? "|" : "-");
79  splitter_elem.setAttribute("count", QString::number(splitter->count()));
80 
81  QString sizes_str;
82  int total_size = 0;
83  for (int size : splitter->sizes())
84  {
85  total_size += size;
86  }
87  for (int size : splitter->sizes())
88  {
89  sizes_str += QString::number(double(size) / double(total_size));
90  sizes_str += ";";
91  }
92  sizes_str.resize(sizes_str.size() - 1);
93  splitter_elem.setAttribute("sizes", sizes_str);
94 
95  for (int i = 0; i < splitter->count(); ++i)
96  {
97  auto child = saveChildNodesState(doc, splitter->widget(i));
98  splitter_elem.appendChild(child);
99  }
100  return splitter_elem;
101  }
102  else
103  {
104  ads::CDockAreaWidget* dockArea = qobject_cast<ads::CDockAreaWidget*>(widget);
105  if (dockArea)
106  {
107  QDomElement area_elem = doc.createElement("DockArea");
108  for (int i = 0; i < dockArea->dockWidgetsCount(); ++i)
109  {
110  auto dock_widget = dynamic_cast<DockWidget*>(dockArea->dockWidget(i));
111  if (dock_widget)
112  {
113  auto plotwidget_elem = dock_widget->plotWidget()->xmlSaveState(doc);
114  area_elem.appendChild(plotwidget_elem);
115  area_elem.setAttribute("name", dock_widget->toolBar()->label()->text());
116  }
117  }
118  return area_elem;
119  }
120  }
121  return {};
122 }
123 
124 QDomElement PlotDocker::xmlSaveState(QDomDocument& doc) const
125 {
126  QDomElement containers_elem = doc.createElement("Tab");
127 
128  containers_elem.setAttribute("containers", dockContainers().count());
129 
130  for (CDockContainerWidget* container : dockContainers())
131  {
132  QDomElement elem = doc.createElement("Container");
133  auto child = saveChildNodesState(doc, container->rootSplitter());
134  elem.appendChild(child);
135  containers_elem.appendChild(elem);
136  }
137  return containers_elem;
138 }
139 
140 void PlotDocker::restoreSplitter(QDomElement elem, DockWidget* widget)
141 {
142  QString orientation_str = elem.attribute("orientation");
143  int splitter_count = elem.attribute("count").toInt();
144 
145  // Check if the orientation string is right
146  if (!orientation_str.startsWith("|") && !orientation_str.startsWith("-"))
147  {
148  return;
149  }
150 
151  Qt::Orientation orientation =
152  orientation_str.startsWith("|") ? Qt::Horizontal : Qt::Vertical;
153 
154  std::vector<DockWidget*> widgets(splitter_count);
155 
156  widgets[0] = widget;
157  for (int i = 1; i < splitter_count; i++)
158  {
159  widget = (orientation == Qt::Horizontal) ? widget->splitHorizontal() :
160  widget->splitVertical();
161  widgets[i] = widget;
162  }
163 
164  int tot_size = 0;
165 
166  for (int i = 0; i < splitter_count; i++)
167  {
168  tot_size +=
169  (orientation == Qt::Horizontal) ? widgets[i]->width() : widgets[i]->height();
170  }
171 
172  auto sizes_str = elem.attribute("sizes").split(";");
173  QList<int> sizes;
174 
175  for (int i = 0; i < splitter_count; i++)
176  {
177  sizes.push_back(static_cast<int>(sizes_str[i].toDouble() * tot_size));
178  }
179 
180  auto splitter = ads::internal::findParent<ads::CDockSplitter*>(widget);
181  splitter->setSizes(sizes);
182 
183  int index = 0;
184 
185  QDomElement child_elem = elem.firstChildElement();
186  while (child_elem.isNull() == false)
187  {
188  if (child_elem.tagName() == "DockArea")
189  {
190  auto plot_elem = child_elem.firstChildElement("plot");
191  widgets[index]->plotWidget()->xmlLoadState(plot_elem);
192  if (child_elem.hasAttribute("name"))
193  {
194  QString area_name = child_elem.attribute("name");
195  widgets[index]->toolBar()->label()->setText(area_name);
196  }
197  index++;
198  }
199  else if (child_elem.tagName() == "DockSplitter")
200  {
201  restoreSplitter(child_elem, widgets[index++]);
202  }
203  else
204  {
205  return;
206  }
207 
208  child_elem = child_elem.nextSiblingElement();
209  }
210 };
211 
212 bool PlotDocker::xmlLoadState(QDomElement& tab_element)
213 {
214  if (!isHidden())
215  {
216  hide();
217  }
218 
219  for (auto container_elem = tab_element.firstChildElement("Container");
220  !container_elem.isNull(); container_elem = container_elem.nextSiblingElement("Cont"
221  "aine"
222  "r"))
223  {
224  auto splitter_elem = container_elem.firstChildElement("DockSplitter");
225  if (!splitter_elem.isNull())
226  {
227  auto widget = dynamic_cast<DockWidget*>(dockArea(0)->currentDockWidget());
228  restoreSplitter(splitter_elem, widget);
229  }
230  }
231 
232  if (isHidden())
233  {
234  show();
235  }
236  return true;
237 }
238 
240 {
241  return dockAreaCount();
242 }
243 
245 {
246  DockWidget* dock_widget =
247  dynamic_cast<DockWidget*>(dockArea(index)->currentDockWidget());
248  return static_cast<PlotWidget*>(dock_widget->plotWidget());
249 }
250 
252 {
253  // TODO
254 }
255 
257 {
258  for (int index = 0; index < plotCount(); index++)
259  {
260  plotAt(index)->zoomOut(false); // TODO is it false?
261  }
262 }
263 
265 {
266  for (int index = 0; index < plotCount(); index++)
267  {
268  plotAt(index)->replot();
269  }
270 }
271 
273 {
274  for (int index = 0; index < plotCount(); index++)
275  {
276  auto dock_widget = static_cast<DockWidget*>(dockArea(index)->currentDockWidget());
277  dock_widget->toolBar()->on_stylesheetChanged(theme);
278  }
279 }
280 
281 DockWidget::DockWidget(PlotDataMapRef& datamap, QWidget* parent)
282  : ads::CDockWidget("Plot", parent), _datamap(datamap)
283 {
284  setFrameShape(QFrame::NoFrame);
285 
286  static int plot_count = 0;
287  QString plot_name = QString("_plot_%1_").arg(plot_count++);
288  _plot_widget = new PlotWidget(datamap, this);
292 
293  _toolbar = new DockToolbar(this);
294  _toolbar->label()->setText("...");
295  qobject_cast<QBoxLayout*>(layout())->insertWidget(0, _toolbar);
296 
297  connect(_toolbar->buttonSplitHorizontal(), &QPushButton::clicked, this,
299 
300  connect(_toolbar->buttonSplitVertical(), &QPushButton::clicked, this,
302 
305 
307 
309 
311  [=](QString title) { _plot_widget->setStatisticsTitle(title); });
312 
313  auto FullscreenAction = [=]() {
314  PlotDocker* parent_docker = static_cast<PlotDocker*>(dockManager());
315 
316  this->toolBar()->toggleFullscreen();
317  bool fullscreen = this->toolBar()->isFullscreen();
318 
319  for (int i = 0; i < parent_docker->dockAreaCount(); i++)
320  {
321  auto area = parent_docker->dockArea(i);
322  if (area != dockAreaWidget())
323  {
324  area->setVisible(!fullscreen);
325  }
326  this->toolBar()->buttonClose()->setHidden(fullscreen);
327  }
328  };
329 
330  QObject::connect(_toolbar->buttonFullscreen(), &QPushButton::clicked, FullscreenAction);
331 
332  QObject::connect(_toolbar->buttonClose(), &QPushButton::pressed, [=]() {
333  dockAreaWidget()->closeArea();
334  takeWidget();
335  _plot_widget->deleteLater();
336  _plot_widget = nullptr;
337  this->undoableChange();
338  });
339 
340  this->layout()->setMargin(10);
341 }
342 
344 {
345 }
346 
348 {
349  // create a sibling (same parent)
350  auto new_widget = new DockWidget(_datamap, qobject_cast<QWidget*>(parent()));
351 
352  PlotDocker* parent_docker = static_cast<PlotDocker*>(dockManager());
353  auto area = parent_docker->addDockWidget(ads::RightDockWidgetArea, new_widget,
354  dockAreaWidget());
355 
357 
358  parent_docker->plotWidgetAdded(new_widget->plotWidget());
359 
360  connect(this, &DockWidget::undoableChange, parent_docker, &PlotDocker::undoableChange);
361 
362  this->undoableChange();
363 
364  return new_widget;
365 }
366 
368 {
369  auto new_widget = new DockWidget(_datamap, qobject_cast<QWidget*>(parent()));
370 
371  PlotDocker* parent_docker = static_cast<PlotDocker*>(dockManager());
372 
373  auto area = parent_docker->addDockWidget(ads::BottomDockWidgetArea, new_widget,
374  dockAreaWidget());
375 
377  parent_docker->plotWidgetAdded(new_widget->plotWidget());
378 
379  connect(this, &DockWidget::undoableChange, parent_docker, &PlotDocker::undoableChange);
380 
381  this->undoableChange();
382 
383  return new_widget;
384 }
385 
387 {
388  return _plot_widget;
389 }
390 
392 {
393  return _toolbar;
394 }
DockSplitter.h
Declaration of CDockSplitter.
DockWidget::splitHorizontal
DockWidget * splitHorizontal()
Definition: plot_docker.cpp:347
ads::CDockComponentsFactory::setFactory
static void setFactory(CDockComponentsFactory *Factory)
Definition: DockComponentsFactory.cpp:55
ads::CDockContainerWidget::dockAreaCount
int dockAreaCount() const
Definition: DockContainerWidget.cpp:1501
DockToolbar::buttonFullscreen
QPushButton * buttonFullscreen()
Definition: plot_docker_toolbar.h:31
ads::CDockManager::dockWidgetRemoved
void dockWidgetRemoved(ads::CDockWidget *DockWidget)
DockToolbar::titleChanged
void titleChanged(QString title)
PlotDocker::restoreSplitter
void restoreSplitter(QDomElement elem, DockWidget *widget)
Definition: plot_docker.cpp:140
ads::CDockWidget::dockAreaWidget
CDockAreaWidget * dockAreaWidget() const
Definition: DockWidget.cpp:394
DockWidget::plotWidget
PlotWidget * plotWidget()
Definition: plot_docker.cpp:386
DockWidget::~DockWidget
~DockWidget() override
Definition: plot_docker.cpp:343
ads::CDockAreaWidget::setAllowedAreas
void setAllowedAreas(DockWidgetAreas areas)
Definition: DockAreaWidget.cpp:877
ads::CDockComponentsFactory
Definition: DockComponentsFactory.h:35
ads::CDockAreaTitleBar
Definition: DockAreaTitleBar.h:50
DockWidget::_toolbar
DockToolbar * _toolbar
Definition: plot_docker.h:37
PlotDocker::replot
void replot()
Definition: plot_docker.cpp:264
PlotWidget::splitHorizontal
void splitHorizontal()
PlotDocker::on_stylesheetChanged
void on_stylesheetChanged(QString theme)
Definition: plot_docker.cpp:272
QList
Definition: qwt_abstract_legend.h:17
ads::CDockAreaWidget
Definition: DockAreaWidget.h:55
ads::CDockWidget::dockManager
CDockManager * dockManager() const
Definition: DockWidget.cpp:366
PlotWidget::onBackgroundColorRequest
void onBackgroundColorRequest(QString name)
Definition: plotwidget.cpp:1216
PlotDocker::xmlSaveState
QDomElement xmlSaveState(QDomDocument &doc) const
Definition: plot_docker.cpp:124
SplittableComponentsFactory
Definition: plot_docker.cpp:19
PlotWidget
Definition: plotwidget.h:38
ads::CDockContainerWidget::dockAreasAdded
void dockAreasAdded()
PJ::PlotWidgetBase::replot
void replot()
Definition: plotwidget_base.cpp:780
DockToolbar::buttonClose
QPushButton * buttonClose()
Definition: plot_docker_toolbar.h:35
nonstd::span_lite::size
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span.hpp:1554
DockToolbar::backgroundColorRequest
void backgroundColorRequest(QString name)
detail::count
constexpr auto count() -> size_t
Definition: core.h:1222
PlotDocker::undoableChange
void undoableChange()
ads
Definition: ads_globals.h:69
PlotDocker::setHorizontalLink
void setHorizontalLink(bool enabled)
Definition: plot_docker.cpp:251
DockWidget::_plot_widget
PlotWidget * _plot_widget
Definition: plot_docker.h:35
DockToolbar::buttonSplitHorizontal
QPushButton * buttonSplitHorizontal()
Definition: plot_docker_toolbar.h:39
DockWidget
Definition: plot_docker.h:16
ads::CDockWidget::DockWidgetFloatable
@ DockWidgetFloatable
dock widget can be dragged into a floating window
Definition: DockWidget.h:152
saveChildNodesState
QDomElement saveChildNodesState(QDomDocument &doc, QWidget *widget)
Definition: plot_docker.cpp:71
ads::TopDockWidgetArea
@ TopDockWidgetArea
Definition: ads_globals.h:78
ads::CDockAreaWidget::currentDockWidget
CDockWidget * currentDockWidget() const
Definition: DockAreaWidget.cpp:554
DockWidget::DockWidget
DockWidget(PlotDataMapRef &datamap, QWidget *parent=nullptr)
Definition: plot_docker.cpp:281
PlotDocker::plotAt
PlotWidget * plotAt(int index)
Definition: plot_docker.cpp:244
ads::CDockWidget::setFeature
void setFeature(DockWidgetFeature flag, bool on)
Definition: DockWidget.cpp:350
PlotDocker::plotCount
int plotCount() const
Definition: plot_docker.cpp:239
DockToolbar::buttonSplitVertical
QPushButton * buttonSplitVertical()
Definition: plot_docker_toolbar.h:43
ads::CDockManager::dockContainers
const QList< CDockContainerWidget * > dockContainers() const
Definition: DockManager.cpp:629
plot_docker.h
ads::CDockWidget::DockWidgetDeleteOnClose
@ DockWidgetDeleteOnClose
deletes the dock widget when it is closed
Definition: DockWidget.h:153
PlotDocker
Definition: plot_docker.h:45
ads::CDockManager::addDockWidget
CDockAreaWidget * addDockWidget(DockWidgetArea area, CDockWidget *Dockwidget, CDockAreaWidget *DockAreaWidget=nullptr)
Definition: DockManager.cpp:758
toDouble
double toDouble(const void *data)
Definition: dataload_zcm.cpp:196
DockWidget::toolBar
DockToolbar * toolBar()
Definition: plot_docker.cpp:391
ads::CDockManager::CDockContainerWidget
friend class CDockContainerWidget
Definition: DockManager.h:77
ads::RightDockWidgetArea
@ RightDockWidgetArea
Definition: ads_globals.h:77
PlotDocker::plotWidgetAdded
void plotWidgetAdded(PlotWidget *)
PlotDocker::zoomOut
void zoomOut()
Definition: plot_docker.cpp:256
DockWidget::undoableChange
void undoableChange()
PlotDocker::~PlotDocker
~PlotDocker()
Definition: plot_docker.cpp:57
PlotWidget::xmlSaveState
QDomElement xmlSaveState(QDomDocument &doc) const
Definition: plotwidget.cpp:637
PlotDocker::PlotDocker
PlotDocker(QString name, PlotDataMapRef &datamap, QWidget *parent=nullptr)
Definition: plot_docker.cpp:31
DockWidget::splitVertical
DockWidget * splitVertical()
Definition: plot_docker.cpp:367
ads::BottomDockWidgetArea
@ BottomDockWidgetArea
Definition: ads_globals.h:79
plotwidget_editor.h
ads::CDockAreaWidget::setVisible
virtual void setVisible(bool Visible) override
Definition: DockAreaWidget.cpp:866
ads::CDockWidget::setWidget
void setWidget(QWidget *widget, eInsertMode InsertMode=AutoScrollArea)
Definition: DockWidget.cpp:266
PJ::PlotDataMapRef
Definition: plotdata.h:34
PlotDocker::_name
QString _name
Definition: plot_docker.h:79
ads::CDockContainerWidget::dockArea
CDockAreaWidget * dockArea(int Index) const
Definition: DockContainerWidget.cpp:1487
ads::OuterDockAreas
@ OuterDockAreas
Definition: ads_globals.h:83
PlotDocker::name
QString name() const
Definition: plot_docker.cpp:61
PlotWidget::setStatisticsTitle
void setStatisticsTitle(QString title)
Definition: plotwidget.cpp:1265
PlotWidget::zoomOut
void zoomOut(bool emit_signal)
Definition: plotwidget.cpp:1319
PlotDocker::setName
void setName(QString name)
Definition: plot_docker.cpp:66
ads::CDockAreaWidget::dockWidgetsCount
int dockWidgetsCount() const
Definition: DockAreaWidget.cpp:702
DockToolbar::toggleFullscreen
void toggleFullscreen()
Definition: plot_docker_toolbar.cpp:46
ads::CDockAreaWidget::dockWidget
CDockWidget * dockWidget(int Index) const
Definition: DockAreaWidget.cpp:709
PlotWidget::splitVertical
void splitVertical()
DockToolbar
Definition: plot_docker_toolbar.h:19
SplittableComponentsFactory::createDockAreaTitleBar
ads::CDockAreaTitleBar * createDockAreaTitleBar(ads::CDockAreaWidget *dock_area) const override
Definition: plot_docker.cpp:23
DockToolbar::isFullscreen
bool isFullscreen() const
Definition: plot_docker_toolbar.h:50
DockToolbar::on_stylesheetChanged
void on_stylesheetChanged(QString theme)
Definition: plot_docker_toolbar.cpp:114
svg_util.h
DockWidget::_datamap
PlotDataMapRef & _datamap
Definition: plot_docker.h:39
PlotDocker::xmlLoadState
bool xmlLoadState(QDomElement &tab_element)
Definition: plot_docker.cpp:212
DockToolbar::label
QLabel * label()
Definition: plot_docker_toolbar.h:27


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45