~valavanisalex/ubuntu/maverick/scidavis/fix-604811

« back to all changes in this revision

Viewing changes to scidavis/src/future/core/Project.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ruben Molina
  • Date: 2009-09-06 11:34:04 UTC
  • Revision ID: james.westby@ubuntu.com-20090906113404-4awaey82l3686w4q
Tags: upstream-0.2.3
ImportĀ upstreamĀ versionĀ 0.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    File                 : Project.cpp
 
3
    Project              : SciDAVis
 
4
    Description          : Represents a SciDAVis project.
 
5
    --------------------------------------------------------------------
 
6
    Copyright            : (C) 2007 Tilman Benkert (thzs*gmx.net)
 
7
    Copyright            : (C) 2007 Knut Franke (knut.franke*gmx.de)
 
8
                           (replace * with @ in the email addresses) 
 
9
 
 
10
 ***************************************************************************/
 
11
 
 
12
/***************************************************************************
 
13
 *                                                                         *
 
14
 *  This program is free software; you can redistribute it and/or modify   *
 
15
 *  it under the terms of the GNU General Public License as published by   *
 
16
 *  the Free Software Foundation; either version 2 of the License, or      *
 
17
 *  (at your option) any later version.                                    *
 
18
 *                                                                         *
 
19
 *  This program is distributed in the hope that it will be useful,        *
 
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 
22
 *  GNU General Public License for more details.                           *
 
23
 *                                                                         *
 
24
 *   You should have received a copy of the GNU General Public License     *
 
25
 *   along with this program; if not, write to the Free Software           *
 
26
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
 
27
 *   Boston, MA  02110-1301  USA                                           *
 
28
 *                                                                         *
 
29
 ***************************************************************************/
 
30
#include "core/Project.h"
 
31
#ifndef LEGACY_CODE_0_2_x
 
32
#include "core/ProjectWindow.h"
 
33
#include "core/ScriptingEngineManager.h"
 
34
#endif
 
35
#include "core/interfaces.h"
 
36
#include "core/globals.h"
 
37
#include "lib/XmlStreamReader.h"
 
38
#include "core/ProjectConfigPage.h"
 
39
#include <QUndoStack>
 
40
#include <QString>
 
41
#include <QKeySequence>
 
42
#include <QMenu>
 
43
#include <QSettings>
 
44
#include <QPluginLoader>
 
45
#include <QComboBox>
 
46
#include <QFile>
 
47
#include <QXmlStreamWriter>
 
48
#include <QtDebug>
 
49
 
 
50
#define NOT_IMPL (QMessageBox::information(0, "info", "not yet implemented"))
 
51
 
 
52
class Project::Private
 
53
{
 
54
        public:
 
55
                Private() :
 
56
                        mdi_window_visibility(static_cast<MdiWindowVisibility>(Project::global("default_mdi_window_visibility").toInt())),
 
57
                        primary_view(0),
 
58
                        scripting_engine(0) {}
 
59
                ~Private() {
 
60
#ifndef LEGACY_CODE_0_2_x
 
61
                        delete primary_view;
 
62
#endif
 
63
                }
 
64
                QUndoStack undo_stack;
 
65
                MdiWindowVisibility mdi_window_visibility;
 
66
#ifndef LEGACY_CODE_0_2_x
 
67
                ProjectWindow * primary_view;
 
68
#else
 
69
                void * primary_view;
 
70
#endif
 
71
                AbstractScriptingEngine * scripting_engine;
 
72
                QString file_name;
 
73
};
 
74
 
 
75
Project::Project()
 
76
        : future::Folder(tr("Unnamed")), d(new Private())
 
77
{
 
78
#ifndef LEGACY_CODE_0_2_x
 
79
        // TODO: intelligent engine choosing
 
80
        Q_ASSERT(ScriptingEngineManager::instance()->engineNames().size() > 0);
 
81
        QString engine_name = ScriptingEngineManager::instance()->engineNames()[0];
 
82
        d->scripting_engine = ScriptingEngineManager::instance()->engine(engine_name);
 
83
#endif
 
84
}
 
85
 
 
86
Project::~Project()
 
87
{
 
88
        delete d;
 
89
}
 
90
 
 
91
QUndoStack *Project::undoStack() const
 
92
{
 
93
        return &d->undo_stack;
 
94
}
 
95
 
 
96
#ifndef LEGACY_CODE_0_2_x
 
97
ProjectWindow *Project::view()
 
98
{
 
99
        if (!d->primary_view)
 
100
                d->primary_view = new ProjectWindow(this);
 
101
        return d->primary_view;
 
102
#else
 
103
void *Project::view()
 
104
{
 
105
        return NULL;
 
106
#endif
 
107
}
 
108
 
 
109
QMenu *Project::createContextMenu() const
 
110
{
 
111
#ifndef LEGACY_CODE_0_2_x
 
112
        return const_cast<Project *>(this)->view()->createContextMenu();
 
113
#else
 
114
        return NULL;
 
115
#endif
 
116
}
 
117
                
 
118
QMenu *Project::createFolderContextMenu(const future::Folder * folder) const
 
119
{
 
120
#ifndef LEGACY_CODE_0_2_x
 
121
        return const_cast<Project *>(this)->view()->createFolderContextMenu(folder);
 
122
#else
 
123
        Q_UNUSED(folder)
 
124
        return NULL;
 
125
#endif
 
126
}
 
127
 
 
128
void Project::setMdiWindowVisibility(MdiWindowVisibility visibility)
 
129
 
130
        d->mdi_window_visibility = visibility; 
 
131
#ifndef LEGACY_CODE_0_2_x
 
132
        view()->updateMdiWindowVisibility();
 
133
#endif
 
134
}
 
135
                
 
136
Project::MdiWindowVisibility Project::mdiWindowVisibility() const 
 
137
 
138
        return d->mdi_window_visibility; 
 
139
}
 
140
 
 
141
AbstractScriptingEngine * Project::scriptingEngine() const
 
142
{
 
143
        return d->scripting_engine;
 
144
}
 
145
 
 
146
/* ================== static methods ======================= */
 
147
ConfigPageWidget * Project::makeConfigPage()
 
148
{
 
149
         return new ProjectConfigPage();
 
150
}
 
151
 
 
152
QString Project::configPageLabel()
 
153
{
 
154
        return QObject::tr("General");
 
155
}
 
156
 
 
157
void Project::setFileName(const QString & file_name)
 
158
{
 
159
        d->file_name = file_name;
 
160
}
 
161
 
 
162
QString Project::fileName() const
 
163
{
 
164
        return d->file_name;
 
165
}
 
166
 
 
167
void Project::save(QXmlStreamWriter * writer) const
 
168
{
 
169
        writer->writeStartDocument();
 
170
        writer->writeStartElement("scidavis_project");
 
171
        writer->writeAttribute("version", QString::number(SciDAVis::version()));
 
172
        // TODO: write project attributes
 
173
        writer->writeStartElement("project_root");
 
174
        future::Folder::save(writer);
 
175
        writer->writeEndElement(); // "project_root"
 
176
        writer->writeEndElement(); // "scidavis_project"
 
177
        writer->writeEndDocument();
 
178
}
 
179
 
 
180
bool Project::load(XmlStreamReader * reader)
 
181
{
 
182
        while (!(reader->isStartDocument() || reader->atEnd()))
 
183
                reader->readNext();
 
184
        if(!(reader->atEnd()))
 
185
        {
 
186
                if (!reader->skipToNextTag()) return false;
 
187
 
 
188
                if (reader->name() == "scidavis_project") 
 
189
                {
 
190
                        bool ok;
 
191
                        int version = reader->readAttributeInt("version", &ok);
 
192
                        if(!ok) 
 
193
                        {
 
194
                                reader->raiseError(tr("invalid or missing project version"));
 
195
                                return false;
 
196
                        }
 
197
 
 
198
                        // version dependent staff goes here
 
199
                        
 
200
                        while (!reader->atEnd()) 
 
201
                        {
 
202
                                reader->readNext();
 
203
 
 
204
                                if (reader->isEndElement()) break;
 
205
 
 
206
                                if (reader->isStartElement()) 
 
207
                                {
 
208
                                        if (reader->name() == "project_root")
 
209
                                        {
 
210
                                                if (!reader->skipToNextTag()) return false;
 
211
                                                if (!future::Folder::load(reader)) return false;
 
212
                                                if (!reader->skipToNextTag()) return false;
 
213
                                                Q_ASSERT(reader->isEndElement() && reader->name() == "project_root");
 
214
                                        }
 
215
                                        else // unknown element
 
216
                                        {
 
217
                                                reader->raiseWarning(tr("unknown element '%1'").arg(reader->name().toString()));
 
218
                                                if (!reader->skipToEndElement()) return false;
 
219
                                        }
 
220
                                } 
 
221
                        }
 
222
                }
 
223
                else // no project element
 
224
                        reader->raiseError(tr("no scidavis_project element found"));
 
225
        }
 
226
        else // no start document
 
227
                reader->raiseError(tr("no valid XML document found"));
 
228
 
 
229
        return !reader->hasError();
 
230
}
 
231