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)  

Workbook.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  File : Workbook.h
3  Project : LabPlot
4  Description : Aspect providing a container for storing data
5  in form of spreadsheets and matrices
6  --------------------------------------------------------------------
7  Copyright : (C) 2015 Alexander Semke(alexander.semke@web.de)
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  * This program is distributed in the hope that it will be useful, *
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21  * GNU General Public License for more details. *
22  * *
23  * You should have received a copy of the GNU General Public License *
24  * along with this program; if not, write to the Free Software *
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26  * Boston, MA 02110-1301 USA *
27  * *
28  ***************************************************************************/
29 
30 #include "Workbook.h"
32 #include "backend/matrix/Matrix.h"
36 
37 #include <QIcon>
38 #include <KLocalizedString>
39 
40 /**
41  * \class Workbook
42  * \brief Top-level container for Spreadsheet and Matrix.
43  * \ingroup backend
44  */
45 Workbook::Workbook(const QString& name) : AbstractPart(name, AspectType::Workbook) {
46 }
47 
48 QIcon Workbook::icon() const {
49  return QIcon::fromTheme("labplot-workbook");
50 }
51 
52 /*!
53  * Returns a new context menu. The caller takes ownership of the menu.
54  */
56  QMenu *menu = AbstractPart::createContextMenu();
57  Q_ASSERT(menu);
58  emit requestProjectContextMenu(menu);
59  return menu;
60 }
61 
62 QWidget* Workbook::view() const {
63  if (!m_partView) {
64  m_view = new WorkbookView(const_cast<Workbook*>(this));
66  }
67  return m_partView;
68 }
69 
70 bool Workbook::exportView() const {
72  bool ret = false;
73  if (s)
74  ret = s->exportView();
75  else {
76  Matrix* m = currentMatrix();
77  if (m)
78  ret = m->exportView();
79  }
80  return ret;
81 }
82 
85  bool ret = false;
86  if (s)
87  ret = s->printView();
88  else {
89  Matrix* m = currentMatrix();
90  if (m)
91  ret = m->printView();
92  }
93  return ret;
94 }
95 
96 bool Workbook::printPreview() const {
98  bool ret = false;
99  if (s)
100  ret = s->printPreview();
101  else {
102  Matrix* m = currentMatrix();
103  if (m)
104  ret = m->printPreview();
105  }
106  return ret;
107 }
108 
110  if (!m_view)
111  return nullptr;
112 
113  int index = m_view->currentIndex();
114  if (index != -1) {
115  auto* aspect = child<AbstractAspect>(index);
116  return dynamic_cast<Spreadsheet*>(aspect);
117  }
118  return nullptr;
119 }
120 
122  if (!m_view)
123  return nullptr;
124 
125  int index = reinterpret_cast<const WorkbookView*>(m_view)->currentIndex();
126  if (index != -1) {
127  auto* aspect = child<AbstractAspect>(index);
128  return dynamic_cast<Matrix*>(aspect);
129  }
130  return nullptr;
131 }
132 
133 /*!
134  this slot is called when a workbook child is selected in the project explorer.
135  emits \c workbookItemSelected() to forward this event to the \c WorkbookView
136  in order to select the corresponding tab.
137  */
139  int index = indexOfChild<AbstractAspect>(aspect);
140  emit workbookItemSelected(index);
141 }
142 
143 /*!
144  this slot is called when a worksheet element is deselected in the project explorer.
145  */
147  Q_UNUSED(aspect);
148 }
149 
150 /*!
151  * Emits the signal to select or to deselect the workbook item (spreadsheet or matrix) with the index \c index
152  * in the project explorer, if \c selected=true or \c selected=false, respectively.
153  * The signal is handled in \c AspectTreeModel and forwarded to the tree view in \c ProjectExplorer.
154  * This function is called in \c WorkbookView when the current tab was changed
155  */
156 void Workbook::setChildSelectedInView(int index, bool selected) {
157  auto* aspect = child<AbstractAspect>(index);
158  if (selected) {
159  emit childAspectSelectedInView(aspect);
160 
161  //deselect the workbook in the project explorer, if a child (spreadsheet or matrix) was selected.
162  //prevents unwanted multiple selection with workbook if it was selected before.
163  emit childAspectDeselectedInView(this);
164  } else {
165  emit childAspectDeselectedInView(aspect);
166 
167  //deselect also all children that were potentially selected before (columns of a spreadsheet)
168  for (auto* child : aspect->children<AbstractAspect>())
170  }
171 }
172 
173 //##############################################################################
174 //################## Serialization/Deserialization ###########################
175 //##############################################################################
176 
177 //! Save as XML
178 void Workbook::save(QXmlStreamWriter* writer) const {
179  writer->writeStartElement( "workbook" );
180  writeBasicAttributes(writer);
181  writeCommentElement(writer);
182 
183  //serialize all children
184  for (auto* aspect : children<AbstractAspect>())
185  aspect->save(writer);
186 
187  writer->writeEndElement(); // close "workbook" section
188 }
189 
190 //! Load from XML
191 bool Workbook::load(XmlStreamReader* reader, bool preview) {
192  if (!readBasicAttributes(reader))
193  return false;
194 
195  while (!reader->atEnd()) {
196  reader->readNext();
197  if (reader->isEndElement() && reader->name() == "workbook")
198  break;
199 
200  if (!reader->isStartElement())
201  continue;
202 
203  if (reader->name() == "comment") {
204  if (!readCommentElement(reader))
205  return false;
206  } else if (reader->name() == "spreadsheet") {
207  Spreadsheet* spreadsheet = new Spreadsheet("spreadsheet", true);
208  if (!spreadsheet->load(reader, preview)) {
209  delete spreadsheet;
210  return false;
211  } else
212  addChild(spreadsheet);
213  } else if (reader->name() == "matrix") {
214  Matrix* matrix = new Matrix(i18n("matrix"), true);
215  if (!matrix->load(reader, preview)) {
216  delete matrix;
217  return false;
218  } else
219  addChild(matrix);
220  } else { // unknown element
221  reader->raiseWarning(i18n("unknown workbook element '%1'", reader->name().toString()));
222  if (!reader->skipToEndElement()) return false;
223  }
224  }
225 
226  return true;
227 }
AspectType
Base class of all persistent objects in a Project.
bool readCommentElement(XmlStreamReader *)
Load comment from an XML element.
void addChild(AbstractAspect *)
Add the given Aspect to my list of children.
void childAspectSelectedInView(const AbstractAspect *)
void childAspectDeselectedInView(const AbstractAspect *)
void writeBasicAttributes(QXmlStreamWriter *) const
Save name and creation time to XML.
void selected(const AbstractAspect *)
T * child(int index, ChildIndexFlags flags={}) const
bool readBasicAttributes(XmlStreamReader *)
Load name and creation time from XML.
void writeCommentElement(QXmlStreamWriter *) const
Save the comment to XML.
Base class of Aspects with MDI windows as views (AspectParts).
Definition: AbstractPart.h:36
QMenu * createContextMenu() override
Return AbstractAspect::createContextMenu() plus operations on the primary view.
QWidget * m_partView
Definition: AbstractPart.h:65
Definition: Matrix.h:41
bool load(XmlStreamReader *, bool preview) override
Load from XML.
Definition: Matrix.cpp:1130
Aspect providing a spreadsheet table with column logic.
Definition: Spreadsheet.h:40
bool exportView() const override
bool printPreview() const override
bool load(XmlStreamReader *, bool preview) override
bool printView() override
View class for Workbook.
Definition: WorkbookView.h:41
int currentIndex() const
Top-level container for Spreadsheet and Matrix.
Definition: Workbook.h:40
void workbookItemSelected(int)
void requestProjectContextMenu(QMenu *)
Spreadsheet * currentSpreadsheet() const
Definition: Workbook.cpp:109
Workbook(const QString &name)
Definition: Workbook.cpp:45
bool load(XmlStreamReader *, bool preview) override
Load from XML.
Definition: Workbook.cpp:191
WorkbookView * m_view
Definition: Workbook.h:65
void save(QXmlStreamWriter *) const override
Save as XML.
Definition: Workbook.cpp:178
QMenu * createContextMenu() override
Definition: Workbook.cpp:55
QIcon icon() const override
Return an icon to be used for decorating my views.
Definition: Workbook.cpp:48
Matrix * currentMatrix() const
Definition: Workbook.cpp:121
bool exportView() const override
Definition: Workbook.cpp:70
bool printView() override
Definition: Workbook.cpp:83
QWidget * view() const override
Construct a primary view on me.
Definition: Workbook.cpp:62
bool printPreview() const override
Definition: Workbook.cpp:96
void childDeselected(const AbstractAspect *) override
Definition: Workbook.cpp:146
void setChildSelectedInView(int index, bool selected)
Definition: Workbook.cpp:156
void childSelected(const AbstractAspect *) override
Definition: Workbook.cpp:138
XML stream parser that supports errors as well as warnings. This class also adds line and column numb...
void raiseWarning(const QString &)
#define i18n(m)
Definition: nsl_common.h:38