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

rocs/App

  • sources
  • kde-4.14
  • kdeedu
  • rocs
  • App
  • Ui
ImporterExporterManager.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 Wagner Reck <wagner.reck@gmail.com>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation; either version 2 of
9  the License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "ImporterExporterManager.h"
21 #include <KFileDialog>
22 #include <KLocalizedString>
23 #include "LoadSave/GraphFilePluginInterface.h"
24 #include <KDebug>
25 #include <GraphFileBackendManager.h>
26 #include <KPushButton>
27 #include <KMessageBox>
28 #include <KGuiItem>
29 #include "Document.h"
30 #include <settings.h>
31 
32 #include <QFile>
33 #include <QPointer>
34 
35 ImporterExporterManager::ImporterExporterManager(QObject* parent): QObject(parent), _scriptToRun(QString())
36 {
37 }
38 
39 bool ImporterExporterManager::exportFile(Document * doc) const
40 {
41  QString ext;
42  QList<GraphFilePluginInterface*> exportBackends = GraphFileBackendManager::self()->backends(GraphFileBackendManager::Export);
43  foreach(GraphFilePluginInterface * f, exportBackends) {
44  ext.append(f->extensions().join(""));
45  }
46  ext.append(i18n("*|All files"));
47 
48  KUrl startDirectory = Settings::lastOpenedDirectory();
49 
50  QPointer<KFileDialog> exportDialog = new KFileDialog(QString(), ext, qobject_cast< QWidget* >(parent()));
51  exportDialog->okButton()->setText(i18nc("@action:button", "Export"));
52  exportDialog->okButton()->setToolTip(i18nc("@info:tooltip", "Export graphs to file"));
53  if (exportDialog->exec() != KDialog::Accepted) {
54  return false;
55  }
56 
57  if (exportDialog->selectedFile().isEmpty()) {
58  return false;
59  }
60 
61  // set file ending
62  KUrl file;
63  ext = exportDialog->currentFilter().remove('*');
64  if (exportDialog->selectedFile().endsWith(ext)) {
65  file = KUrl::fromLocalFile(exportDialog->selectedFile());
66  } else {
67  file = KUrl::fromLocalFile(exportDialog->selectedFile().append(ext));
68  }
69 
70  // test if any file is overwritten
71  if (QFile::exists(exportDialog->selectedFile())) {
72  if (KMessageBox::warningContinueCancel(qobject_cast< QWidget* >(parent()), i18n(
73  "<p>The file <br /><strong>'%1'</strong><br /> already exists; if you "
74  "do not want to overwrite it, change the file name to "
75  "something else.</p>", file.prettyUrl()),
76  i18n("File Exists"), KGuiItem(i18n("Overwrite") ))
77  == KMessageBox::Cancel ) {
78  return false;
79  }
80  }
81 
82  // select plugin by extension
83  GraphFilePluginInterface * filePlugin = GraphFileBackendManager::self()->backendByExtension(ext);
84  if (!filePlugin) {
85  kDebug() << "Cannot export file: " << file.toLocalFile();
86  return false;
87  }
88 
89  filePlugin->setFile(file);
90  filePlugin->writeFile(*doc);
91  if (filePlugin->hasError()) {
92  kDebug() << "Error occurred when writing file: " << filePlugin->errorString();
93  return false;
94  }
95 
96  Settings::setLastOpenedDirectory(file.path());
97 
98  return true;
99 }
100 
101 Document* ImporterExporterManager::importFile()
102 {
103  QString ext;
104  QList<GraphFilePluginInterface*> importBackends = GraphFileBackendManager::self()->backends(GraphFileBackendManager::Import);
105  foreach(GraphFilePluginInterface * f, importBackends) {
106  ext.append(f->extensions().join(""));
107  }
108  ext.append(i18n("*|All files"));
109 
110  QPointer<KFileDialog> dialog = new KFileDialog(QString(), ext, qobject_cast< QWidget* >(parent()));
111  dialog->setCaption(i18nc("@title:window", "Import Graph File into Project"));
112  if (!dialog->exec()) {
113  return 0;
114  }
115 
116  kDebug() << "Extensions:" << ext;
117  QString fileName = dialog->selectedFile();
118  if (fileName.isEmpty()) {
119  return 0;
120  }
121 
122  int index = fileName.lastIndexOf('.');
123  GraphFilePluginInterface * filePlugin = 0;
124  if (index == -1) {
125  kDebug() << "Cannot open file without extension.";
126  return 0;
127  }
128 
129  kDebug() << fileName.right(fileName.count() - index);
130  filePlugin = GraphFileBackendManager::self()->backendByExtension(fileName.right(fileName.count() - index));
131 
132  if (!filePlugin) {
133  kDebug() << "Cannot handle extension " << fileName.right(3);
134  return 0;
135  }
136 
137  filePlugin->setFile(fileName);
138  filePlugin->readFile();
139  if (filePlugin->hasError()) {
140  kDebug() << "Error loading file" << fileName << filePlugin->errorString();
141  return 0;
142  }
143  else {
144  return filePlugin->graphDocument();
145  }
146 }
147 
148 void ImporterExporterManager::dialogExec()
149 {
150 
151 }
152 
153 bool ImporterExporterManager::hasDialog()
154 {
155  return false;
156 }
ImporterExporterManager::exportFile
bool exportFile(Document *doc) const
Definition: ImporterExporterManager.cpp:39
ImporterExporterManager::hasDialog
bool hasDialog()
Definition: ImporterExporterManager.cpp:153
QString::append
QString & append(QChar ch)
ImporterExporterManager::dialogExec
void dialogExec()
Definition: ImporterExporterManager.cpp:148
QPointer
QFile::exists
bool exists() const
Settings::lastOpenedDirectory
static QString lastOpenedDirectory()
Get lastOpenedDirectory.
Definition: settings.h:201
QString::lastIndexOf
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
ImporterExporterManager::ImporterExporterManager
ImporterExporterManager(QObject *parent=0)
Definition: ImporterExporterManager.cpp:35
QObject
QString::isEmpty
bool isEmpty() const
QString
QList< GraphFilePluginInterface * >
Settings::setLastOpenedDirectory
static void setLastOpenedDirectory(const QString &v)
Set lastOpenedDirectory.
Definition: settings.h:191
QString::right
QString right(int n) const
ImporterExporterManager::importFile
Document * importFile()
Definition: ImporterExporterManager.cpp:101
settings.h
QString::count
int count() const
QObject::parent
QObject * parent() const
ImporterExporterManager.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:16:13 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

rocs/App

Skip menu "rocs/App"
  • Main Page
  • Namespace List
  • 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
  • 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