• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

rocs/RocsCore

  • sources
  • kde-4.12
  • kdeedu
  • rocs
  • RocsCore
DocumentManager.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright 2010-2011 Tomaz Canabrava <tomaz.canabrava@gmail.com>
4  Copyright 2010-2011 Wagner Reck <wagner.reck@gmail.com>
5  Copyright 2011-2012 Andreas Cord-Landwehr <cola@uni-paderborn.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "DocumentManager.h"
22 #include "Document.h"
23 #include "DataStructure.h"
24 #include "DataStructureBackendManager.h"
25 #include "QtScriptBackend.h"
26 #include "LoadSave/GraphFileBackendManager.h"
27 #include "DataStructureBackendInterface.h"
28 
29 
30 #include <KDebug>
31 #include <KLocale>
32 #include <QWaitCondition>
33 #include <QAction>
34 #include <QSvgRenderer>
35 
36 
37 // load catalog for library
38 static const KCatalogLoader loader("rocscore");
39 
40 class DocumentManagerPrivate
41 {
42 public:
43  bool _initialized;
44  QList<Document*> _documents;
45  Document *_activeDocument;
46 
47  // The list of shared renders provide resource efficient use of icon renders for same icon
48  // packages. A possible future problem is that this list is never cleaned.
49  QMap<QString, QSvgRenderer *> _sharedRenderers;
50 
51  DocumentManagerPrivate()
52  : _initialized(false)
53  {
54  }
55 };
56 
57 DocumentManager & DocumentManager::self()
58 {
59  static DocumentManager instance;
60  if(!instance.d->_initialized) {
61  instance.d->_initialized = true;
62  connect(&DataStructureBackendManager::self(), SIGNAL(backendChanged(QString)),
63  &instance, SLOT(convertToDataStructure()));
64  }
65 
66  return instance;
67 }
68 
69 DocumentManager::DocumentManager(QObject *parent)
70  : QObject(parent)
71  , d(new DocumentManagerPrivate())
72 {
73  d->_activeDocument = 0;
74 }
75 
76 DocumentManager::~DocumentManager()
77 {
78  foreach(Document *g, d->_documents) {
79  removeDocument(g);
80  }
81 
82  // remove shared renderers
83  QMap <QString, QSvgRenderer* >::iterator iter = d->_sharedRenderers.begin();
84  while (iter != d->_sharedRenderers.end()) {
85  iter.value()->deleteLater();
86  ++iter;
87  }
88  d->_sharedRenderers.clear();
89 }
90 
91 Document * DocumentManager::document(const int index) const
92 {
93  return (index < d->_documents.count() && index >= 0) ? d->_documents.at(index) : 0;
94 }
95 
96 Document * DocumentManager::activeDocument() const
97 {
98  return d->_activeDocument;
99 }
100 
101 QList< Document* > DocumentManager::documentList() const
102 {
103  return d->_documents;
104 }
105 
106 void DocumentManager::addDocument(Document *document)
107 {
108  if (!d->_documents.contains(document)) {
109  if (document->dataStructures().count() == 0) {
110  document->addDataStructure();
111  }
112  d->_documents.append(document);
113  changeDocument(document);
114  }
115  emit documentListChanged();
116 }
117 
118 
119 void DocumentManager::changeDocument(int index)
120 {
121 //FIXME It crash in some systems, in other no. Let's try reactive it in future
122 // Q_ASSERT(index >= 0 && index < documentList().length());
123  if (index < 0 || index >= documentList().length()) {
124  return;
125  }
126  changeDocument(documentList().at(index));
127 }
128 
129 
130 void DocumentManager::changeDocument()
131 {
132  QAction *action = qobject_cast<QAction *> (sender());
133 
134  if (! action) {
135  return;
136  }
137  if (Document *doc = d->_documents.value(action->data().toInt())) {
138  changeDocument(doc);
139  }
140 }
141 
142 
143 void DocumentManager::changeDocument(Document *document)
144 {
145  if (!d->_documents.contains(document)) {
146  d->_documents.append(document);
147  }
148  if (d->_activeDocument != document) {
149  if (d->_activeDocument) {
150  emit deactivateDocument(d->_activeDocument);
151  DataStructureBackendManager::self().disconnect(d->_activeDocument);
152  document->disconnect(SIGNAL(activeDataStructureChanged(DataStructurePtr)));
153  document->engineBackend()->disconnect(SIGNAL(sendDebug(QString)));
154  document->engineBackend()->disconnect(SIGNAL(sendOutput(QString)));
155  document->engineBackend()->disconnect(SIGNAL(finished()));
156  }
157  d->_activeDocument = document;
158  if (d->_activeDocument) {
159  emit activateDocument();
160  }
161  }
162 }
163 
164 
165 void DocumentManager::closeAllDocuments()
166 {
167  foreach(Document *document, documentList()) {
168  removeDocument(document);
169  }
170 }
171 
172 
173 void DocumentManager::removeDocument(Document *document)
174 {
175  if (d->_documents.removeOne(document)) {
176  document->engineBackend()->stop();
177  document->disconnect();
178 
179  if (d->_activeDocument == document) {
180  if (d->_documents.count() > 0) {
181  changeDocument(d->_documents.last()); //
182  } else {
183  emit deactivateDocument(d->_activeDocument);
184  d->_activeDocument = 0;
185  }
186  }
187  emit documentRemoved(document);
188  emit documentListChanged();
189 
190  document->clear();
191  document->deleteLater();
192  }
193 }
194 
195 
196 void DocumentManager::convertToDataStructure()
197 {
198  if (!d->_activeDocument) {
199  kWarning() << "No active document found, creating new document with active backend.";
200  newDocument();
201  return;
202  }
203 
204  //Check if need to convert (different DS) and if is possible to convert without data lost.
205  if (d->_activeDocument->backend()->internalName() != DataStructureBackendManager::self().activeBackend()->internalName()
206  && DataStructureBackendManager::self().activeBackend()->canConvertFrom(d->_activeDocument))
207  {
208  d->_activeDocument->changeBackend();
209  kDebug() << "Data structure converted to " << DataStructureBackendManager::self().activeBackend()->name();
210  emit activateDocument();
211  }
212 }
213 
214 
215 Document * DocumentManager::newDocument()
216 {
217  Document *doc;
218  QString name;
219 
220  // find unused name
221  QList<QString> usedNames;
222  foreach(Document * document, d->_documents) {
223  usedNames.append(document->name());
224  }
225  // For at least one i in this range, the name is not used, yet.
226  for (int i = 0; i < d->_documents.length() + 1; ++i) {
227  name = QString("%1 %2").arg(i18nc("document that contains graphs or data structures as a visual representation", "Document")).arg(i);
228  if (!usedNames.contains(name)) {
229  break;
230  }
231  }
232  doc = new Document(name);
233  doc->addDataStructure();
234  doc->setModified(false);
235  addDocument(doc);
236 
237  if (d->_activeDocument==0) {
238  d->_activeDocument = doc;
239  emit activateDocument();
240  }
241  return doc;
242 }
243 
244 
245 Document * DocumentManager::openDocument(const KUrl &documentUrl)
246 {
247  GraphFilePluginInterface* loader = GraphFileBackendManager::self()->defaultBackend();
248  loader->setFile(documentUrl);
249  loader->readFile();
250  if (loader->error() != GraphFilePluginInterface::None) {
251  kDebug() << "Could not load file. Graph loader returned error: " << loader->errorString();
252  return new Document(documentUrl.fileName());;
253  }
254  Document* doc = loader->graphDocument();
255  doc->setName(documentUrl.fileName());
256  doc->setModified(false);
257  addDocument(doc);
258  emit activateDocument();
259  return doc;
260 }
261 
262 
263 void DocumentManager::saveDocumentAs(Document *document, const KUrl &documentUrl)
264 {
265  exportDocument(document, documentUrl);
266  document->setFileUrl(documentUrl);
267  document->setModified(false);
268  return;
269 }
270 
271 void DocumentManager::exportDocument(Document *document, const KUrl &documentUrl)
272 {
273  GraphFilePluginInterface* serializer = GraphFileBackendManager::self()->defaultBackend();
274  serializer->setFile(documentUrl);
275  serializer->writeFile(*document);
276  if (serializer->error() != GraphFilePluginInterface::None) {
277  kDebug() << "Could not save file. Serializer returned error: " << serializer->errorString();
278  return;
279  }
280  return;
281 }
282 
283 
284 QSvgRenderer * DocumentManager::sharedRenderer(const QString &iconPackage)
285 {
286  if (d->_sharedRenderers.count(iconPackage) == 0 || !d->_sharedRenderers.contains(iconPackage)) {
287  registerSharedRenderer(iconPackage);
288  }
289  return d->_sharedRenderers.value(iconPackage);
290 }
291 
292 
293 QSvgRenderer * DocumentManager::registerSharedRenderer(const QString &iconPackage)
294 {
295  if (!d->_sharedRenderers.contains(iconPackage)) {
296  QSvgRenderer *z = new QSvgRenderer(iconPackage);
297  d->_sharedRenderers.insert(iconPackage, z);
298  }
299  return d->_sharedRenderers.value(iconPackage);
300 }
301 
302 
303 void DocumentManager::removeSharedRenderer(const QString &iconPackage)
304 {
305  d->_sharedRenderers[iconPackage]->deleteLater();
306  d->_sharedRenderers.remove(iconPackage);
307 }
308 
DocumentManager::sharedRenderer
QSvgRenderer * sharedRenderer(const QString &iconPackage)
Definition: DocumentManager.cpp:284
Document::engineBackend
QtScriptBackend * engineBackend() const
Definition: Document.cpp:239
DataStructureBackendManager::activeBackend
DataStructureBackendInterface * activeBackend() const
Returns the currently active data structure backend.
Definition: DataStructureBackendManager.cpp:275
GraphFilePluginInterface::None
Definition: GraphFilePluginInterface.h:51
DocumentManager.h
DocumentManager::self
static DocumentManager & self()
Definition: DocumentManager.cpp:57
DataStructurePtr
boost::shared_ptr< DataStructure > DataStructurePtr
Definition: CoreTypes.h:38
GmlParser::document
Document * document
Definition: GmlGrammar.cpp:40
DocumentManager::activeDocument
Document * activeDocument() const
Returns the currently active document, or 0 if there document list is empty.
Definition: DocumentManager.cpp:96
QtScriptBackend::stop
void stop()
Abort script evaluation.
Definition: QtScriptBackend.cpp:142
DocumentManager::deactivateDocument
void deactivateDocument(Document *document)
Signal is emitted if the currently active document is deactivated.
Document::setModified
void setModified(const bool mod=true)
Definition: Document.cpp:96
Document::setName
void setName(const QString &name)
Definition: Document.cpp:245
DocumentManager::closeAllDocuments
void closeAllDocuments()
Close all documents from document list.
Definition: DocumentManager.cpp:165
DocumentManager::changeDocument
void changeDocument()
Definition: DocumentManager.cpp:130
QtScriptBackend.h
loader
static const KCatalogLoader loader("rocscore")
QObject
DataStructureBackendManager::self
static DataStructureBackendManager & self()
Returns self reference to backend manager.
Definition: DataStructureBackendManager.cpp:233
DataStructureBackendInterface.h
DocumentManager::~DocumentManager
virtual ~DocumentManager()
Definition: DocumentManager.cpp:76
Document.h
DocumentManager::saveDocumentAs
void saveDocumentAs(Document *document, const KUrl &documentUrl)
Save graph document document at url documentUrl.
Definition: DocumentManager.cpp:263
GraphFilePluginInterface::writeFile
virtual void writeFile(Document &graph)=0
Writes given graph document to formerly specified file.
DataStructureBackendInterface::name
QString name()
return the translated name to be used in user visible strings
Definition: DataStructureBackendInterface.cpp:54
GraphFileBackendManager::self
static GraphFileBackendManager * self()
Returns self reference to backend manager.
Definition: GraphFileBackendManager.cpp:49
GraphFileBackendManager::defaultBackend
GraphFilePluginInterface * defaultBackend()
Returns the default backend used for serialization/loading of graph files.
Definition: GraphFileBackendManager.cpp:151
DocumentManager::newDocument
Document * newDocument()
Creates and loads a new graph document.
Definition: DocumentManager.cpp:215
DocumentManager::convertToDataStructure
void convertToDataStructure()
Convert document to new data structure.
Definition: DocumentManager.cpp:196
DataStructure.h
DocumentManager::document
Document * document(int index) const
Returns document with index i in document list.
Definition: DocumentManager.cpp:91
Document::addDataStructure
DataStructurePtr addDataStructure(const QString &name=QString())
Add data structure to graph document with name name.
Definition: Document.cpp:333
Document::clear
void clear()
Remove everything contained at this document.
Definition: Document.cpp:408
GraphFilePluginInterface::error
Error error() const
Returns last error.
Definition: GraphFilePluginInterface.cpp:71
Document
Definition: Document.h:41
DataStructureBackendInterface::canConvertFrom
virtual bool canConvertFrom(Document *doc) const =0
Check if is possíble to convert from the doc document to this data structure.
GraphFileBackendManager.h
DocumentManager::documentList
QList< Document * > documentList() const
Returns the document list of the document manager.
Definition: DocumentManager.cpp:101
GraphFilePluginInterface::setFile
void setFile(const KUrl &file)
Set file that shall be used for nexte read or write operation.
Definition: GraphFilePluginInterface.cpp:114
GraphFilePluginInterface::readFile
virtual void readFile()=0
Open given file and imports it into internal format.
Document::dataStructures
QList< DataStructurePtr > & dataStructures() const
Definition: Document.cpp:227
DocumentManager::activateDocument
void activateDocument()
Signal is emitted if the currently active document changes.
DocumentManager::documentRemoved
void documentRemoved(Document *document)
Signal is emitted if document was removed from list.
GraphFilePluginInterface::graphDocument
virtual Document * graphDocument() const
If.
Definition: GraphFilePluginInterface.cpp:102
DocumentManager
Definition: DocumentManager.h:35
DataStructureBackendInterface::internalName
QString internalName()
return the internal name from plugin.
Definition: DataStructureBackendInterface.cpp:63
Document::name
QString name() const
Definition: Document.cpp:253
DocumentManager::documentListChanged
void documentListChanged()
Signal is emitted if the document list changes and older document index values get invalid...
GraphFilePluginInterface::errorString
QString errorString() const
Definition: GraphFilePluginInterface.cpp:77
DocumentManager::removeSharedRenderer
void removeSharedRenderer(const QString &iconPackage)
Definition: DocumentManager.cpp:303
DocumentManager::addDocument
void addDocument(Document *document)
Add document to document list and set this document as active document.
Definition: DocumentManager.cpp:106
DocumentManager::exportDocument
void exportDocument(Document *document, const KUrl &documentUrl)
Save graph document document at url documentUrl.
Definition: DocumentManager.cpp:271
DataStructureBackendManager.h
GraphFilePluginInterface
This class provides an interface for graph file format plugins.
Definition: GraphFilePluginInterface.h:42
DocumentManager::registerSharedRenderer
QSvgRenderer * registerSharedRenderer(const QString &iconPackage)
Definition: DocumentManager.cpp:293
Document::setFileUrl
void setFileUrl(const KUrl &fileUrl)
Set file path used for saving.
Definition: Document.cpp:391
DocumentManager::openDocument
Document * openDocument(const KUrl &documentUrl)
Loads graph document specified by documentUrl and adds document to document list. ...
Definition: DocumentManager.cpp:245
DocumentManager::removeDocument
void removeDocument(Document *document)
Remove document from document list.
Definition: DocumentManager.cpp:173
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:25 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

rocs/RocsCore

Skip menu "rocs/RocsCore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal