labplot  2.8.2
About: LabPlot is an application for plotting and analysis of 2D and 3D functions and data. It is a complete rewrite of LabPlot1 and lacks in the first release a lot of features available in the predecessor. On the other hand, the GUI and the usability is more superior.
  Fossies Dox: labplot-2.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

DatapickerView.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  File : DatapickerView.cpp
3  Project : LabPlot
4  Description : View class for Datapicker
5  --------------------------------------------------------------------
6  Copyright : (C) 2015 by Ankit Wagadre (wagadre.ankit@gmail.com)
7  Copyright : (C) 2015-2020 by Alexander Semke (alexander.semke@web.de)
8 
9  ***************************************************************************/
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  * This program is distributed in the hope that it will be useful, *
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20  * GNU General Public License for more details. *
21  * *
22  * You should have received a copy of the GNU General Public License *
23  * along with this program; if not, write to the Free Software *
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
25  * Boston, MA 02110-1301 USA *
26  * *
27  ***************************************************************************/
28 
29 #include "DatapickerView.h"
31 #include "backend/lib/macros.h"
37 
38 #include <QHBoxLayout>
39 #include <QMenu>
40 #include <QTabBar>
41 #include <QTabWidget>
42 
43 #include <KLocalizedString>
44 
45 /*!
46  \class DatapickerView
47  \brief View class for Datapicker
48 
49  \ingroup commonfrontend
50  */
51 DatapickerView::DatapickerView(Datapicker* datapicker) : QWidget(),
52  m_tabWidget(new QTabWidget(this)),
53  m_datapicker(datapicker) {
54 
55  m_tabWidget->setTabPosition(QTabWidget::South);
56  m_tabWidget->setTabShape(QTabWidget::Rounded);
57 // m_tabWidget->setMovable(true);
58  m_tabWidget->setContextMenuPolicy(Qt::CustomContextMenu);
59  m_tabWidget->setMinimumSize(600, 600);
60 
61  auto* layout = new QHBoxLayout(this);
62  layout->setContentsMargins(0, 0, 0, 0);
63  layout->addWidget(m_tabWidget);
64 
65  //add tab for each children view
66  m_initializing = true;
68  handleAspectAdded(aspect);
69  for (const auto* child : aspect->children<AbstractAspect>()) {
70  handleAspectAdded(child);
71  }
72  }
73  m_initializing = false;
74 
75  //SIGNALs/SLOTs
80 
81  connect(m_tabWidget, &QTabWidget::currentChanged, this, &DatapickerView::tabChanged);
82  connect(m_tabWidget, &QTabWidget::customContextMenuRequested, this, &DatapickerView::showTabContextMenu);
83  connect(m_tabWidget->tabBar(), &QTabBar::tabMoved, this, &DatapickerView::tabMoved);
84 }
85 
87  //delete all children views here, its own view will be deleted in ~AbstractPart()
89  for (const auto* child : aspect->children<AbstractAspect>()) {
90  const auto* part = dynamic_cast<const AbstractPart*>(child);
91  if (part)
92  part->deleteView();
93  }
94  const auto* part = dynamic_cast<const AbstractPart*>(aspect);
95  if (part)
96  part->deleteView();
97  }
98 }
99 
100 void DatapickerView::fillToolBar(QToolBar* toolBar) {
101  auto* view = static_cast<DatapickerImageView*>(m_datapicker->image()->view());
102  view->fillToolBar(toolBar);
103 }
104 
105 void DatapickerView::createContextMenu(QMenu* menu) const {
106  Q_ASSERT(menu);
108 }
109 
111  return m_tabWidget->currentIndex();
112 }
113 
114 //##############################################################################
115 //######################### Private slots ####################################
116 //##############################################################################
117 /*!
118  called when the current tab was changed. Propagates the selection of \c Spreadsheet
119  or of a \c DatapickerImage object to \c Datapicker.
120 */
121 void DatapickerView::tabChanged(int index) {
122  if (m_initializing)
123  return;
124 
125  if (index == -1)
126  return;
127 
129  m_datapicker->setChildSelectedInView(index, true);
130  lastSelectedIndex = index;
131 }
132 
133 void DatapickerView::tabMoved(int from, int to) {
134  Q_UNUSED(from);
135  Q_UNUSED(to);
136  //TODO:
137 // AbstractAspect* aspect = m_datapicker->child<AbstractAspect>(to);
138 // if (aspect) {
139 // m_tabMoving = true;
140 // AbstractAspect* sibling = m_datapicker->child<AbstractAspect>(from);
141 // qDebug()<<"insert: " << to << " " << aspect->name() << ", " << from << " " << sibling->name();
142 // aspect->remove();
143 // m_datapicker->insertChildBefore(aspect, sibling);
144 // qDebug()<<"inserted";
145 // m_tabMoving = false;
146 // }
147 }
148 
150  m_initializing = true;
151  m_tabWidget->setCurrentIndex(index);
152  m_initializing = false;
153 }
154 
156  QMenu* menu = nullptr;
158  auto* spreadsheet = dynamic_cast<Spreadsheet*>(aspect);
159  if (spreadsheet) {
160  menu = spreadsheet->createContextMenu();
161  } else {
162  auto* image = dynamic_cast<DatapickerImage*>(aspect);
163  if (image)
164  menu = image->createContextMenu();
165  }
166 
167  if (menu)
168  menu->exec(m_tabWidget->mapToGlobal(point));
169 }
170 
172  if (aspect == m_datapicker)
173  return;
174 
175  //determine the child that was changed and adjust the name of the corresponding tab widget
176  int index = -1;
177  QString name;
178  if (aspect->parentAspect() == m_datapicker) {
179  //datapicker curve was renamed
181  if (index != -1)
182  name = aspect->name() + ": " + aspect->children<Spreadsheet>().constFirst()->name();
183  } else {
184  //data spreadsheet was renamed or one of its columns, which is not relevant here
186  if (index != -1)
187  name = aspect->parentAspect()->name() + ": " + aspect->name();
188  }
189 
190  if (index != -1)
191  m_tabWidget->setTabText(index, name);
192 }
193 
195  int index;
196  QString name;
197  const AbstractPart* part = dynamic_cast<const DatapickerImage*>(aspect);
198  if (part) {
199  index = 0;
200  name = aspect->name();
201  } else if (dynamic_cast<const DatapickerCurve*>(aspect)) {
203  const Spreadsheet* spreadsheet = static_cast<const Spreadsheet*>(aspect->child<AbstractAspect>(0));
204  part = spreadsheet;
205  name = aspect->name() + ": " + spreadsheet->name();
206  } else
207  return;
208 
209  m_tabWidget->insertTab(index, part->view(), name);
210  m_tabWidget->setTabIcon(m_tabWidget->count(), aspect->icon());
211 }
212 
214  const auto* curve = dynamic_cast<const DatapickerCurve*>(aspect);
215  if (curve) {
217  m_tabWidget->removeTab(index);
218  }
219 }
Base class of all persistent objects in a Project.
virtual QIcon icon() const
Return an icon to be used for decorating my views.
void aspectAdded(const AbstractAspect *)
Emitted after a new Aspect has been added to the tree.
int indexOfChild(const AbstractAspect *child, ChildIndexFlags flags={}) const
Return (0 based) index of child in the list of children inheriting from class T.
@ IncludeHidden
Include aspects marked as "hidden" in numbering or listing children.
void aspectDescriptionChanged(const AbstractAspect *)
Emitted after the name, comment or caption spec have changed.
void aspectAboutToBeRemoved(const AbstractAspect *)
Emitted before an aspect is removed from its parent.
QString name() const
virtual QMenu * createContextMenu()
Return a new context menu.
T * child(int index, ChildIndexFlags flags={}) const
AbstractAspect * parentAspect() const
Return my parent Aspect or 0 if I currently don't have one.
QVector< AbstractAspect * > children(AspectType type, ChildIndexFlags flags={}) const
Base class of Aspects with MDI windows as views (AspectParts).
Definition: AbstractPart.h:36
void deleteView() const
virtual QWidget * view() const =0
Construct a primary view on me.
Top-level container for Curve-Point and Datasheet/Spreadsheet of datapicker.
Datapicker/DatapickerImage view.
void fillToolBar(QToolBar *)
container to open image/plot.
QWidget * view() const override
Construct a primary view on me.
QMenu * createContextMenu() override
~DatapickerView() override
void createContextMenu(QMenu *) const
int currentIndex() const
void handleAspectAdded(const AbstractAspect *)
void fillToolBar(QToolBar *)
void tabMoved(int, int)
QTabWidget * m_tabWidget
void handleDescriptionChanged(const AbstractAspect *)
void itemSelected(int)
void tabChanged(int)
DatapickerView(Datapicker *)
void handleAspectAboutToBeRemoved(const AbstractAspect *)
Datapicker * m_datapicker
void showTabContextMenu(QPoint)
Top-level container for DatapickerCurve and DatapickerImage.
Definition: Datapicker.h:45
void datapickerItemSelected(int)
void setChildSelectedInView(int index, bool selected)
Definition: Datapicker.cpp:184
DatapickerImage * image() const
Definition: Datapicker.cpp:143
Aspect providing a spreadsheet table with column logic.
Definition: Spreadsheet.h:40