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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/***************************************************************************
    File                 : Project.cpp
    Project              : SciDAVis
    Description          : Represents a SciDAVis project.
    --------------------------------------------------------------------
    Copyright            : (C) 2007 Tilman Benkert (thzs*gmx.net)
    Copyright            : (C) 2007 Knut Franke (knut.franke*gmx.de)
                           (replace * with @ in the email addresses) 

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *  This program is free software; you can redistribute it and/or modify   *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation; either version 2 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  This program is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the Free Software           *
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
 *   Boston, MA  02110-1301  USA                                           *
 *                                                                         *
 ***************************************************************************/
#include "core/Project.h"
#ifndef LEGACY_CODE_0_2_x
#include "core/ProjectWindow.h"
#include "core/ScriptingEngineManager.h"
#endif
#include "core/interfaces.h"
#include "core/globals.h"
#include "lib/XmlStreamReader.h"
#include "core/ProjectConfigPage.h"
#include <QUndoStack>
#include <QString>
#include <QKeySequence>
#include <QMenu>
#include <QSettings>
#include <QPluginLoader>
#include <QComboBox>
#include <QFile>
#include <QXmlStreamWriter>
#include <QtDebug>

#define NOT_IMPL (QMessageBox::information(0, "info", "not yet implemented"))

class Project::Private
{
	public:
		Private() :
			mdi_window_visibility(static_cast<MdiWindowVisibility>(Project::global("default_mdi_window_visibility").toInt())),
			primary_view(0),
			scripting_engine(0) {}
		~Private() {
#ifndef LEGACY_CODE_0_2_x
			delete primary_view;
#endif
		}
		QUndoStack undo_stack;
		MdiWindowVisibility mdi_window_visibility;
#ifndef LEGACY_CODE_0_2_x
		ProjectWindow * primary_view;
#else
		void * primary_view;
#endif
		AbstractScriptingEngine * scripting_engine;
		QString file_name;
};

Project::Project()
	: future::Folder(tr("Unnamed")), d(new Private())
{
#ifndef LEGACY_CODE_0_2_x
	// TODO: intelligent engine choosing
	Q_ASSERT(ScriptingEngineManager::instance()->engineNames().size() > 0);
	QString engine_name = ScriptingEngineManager::instance()->engineNames()[0];
	d->scripting_engine = ScriptingEngineManager::instance()->engine(engine_name);
#endif
}

Project::~Project()
{
	delete d;
}

QUndoStack *Project::undoStack() const
{
	return &d->undo_stack;
}

#ifndef LEGACY_CODE_0_2_x
ProjectWindow *Project::view()
{
	if (!d->primary_view)
		d->primary_view = new ProjectWindow(this);
	return d->primary_view;
#else
void *Project::view()
{
	return NULL;
#endif
}

QMenu *Project::createContextMenu() const
{
#ifndef LEGACY_CODE_0_2_x
	return const_cast<Project *>(this)->view()->createContextMenu();
#else
	return NULL;
#endif
}
		
QMenu *Project::createFolderContextMenu(const future::Folder * folder) const
{
#ifndef LEGACY_CODE_0_2_x
	return const_cast<Project *>(this)->view()->createFolderContextMenu(folder);
#else
	Q_UNUSED(folder)
	return NULL;
#endif
}

void Project::setMdiWindowVisibility(MdiWindowVisibility visibility)
{ 
	d->mdi_window_visibility = visibility; 
#ifndef LEGACY_CODE_0_2_x
	view()->updateMdiWindowVisibility();
#endif
}
		
Project::MdiWindowVisibility Project::mdiWindowVisibility() const 
{ 
	return d->mdi_window_visibility; 
}

AbstractScriptingEngine * Project::scriptingEngine() const
{
	return d->scripting_engine;
}

/* ================== static methods ======================= */
ConfigPageWidget * Project::makeConfigPage()
{
	 return new ProjectConfigPage();
}

QString Project::configPageLabel()
{
	return QObject::tr("General");
}

void Project::setFileName(const QString & file_name)
{
	d->file_name = file_name;
}

QString Project::fileName() const
{
	return d->file_name;
}

void Project::save(QXmlStreamWriter * writer) const
{
	writer->writeStartDocument();
	writer->writeStartElement("scidavis_project");
	writer->writeAttribute("version", QString::number(SciDAVis::version()));
	// TODO: write project attributes
	writer->writeStartElement("project_root");
	future::Folder::save(writer);
	writer->writeEndElement(); // "project_root"
	writer->writeEndElement(); // "scidavis_project"
	writer->writeEndDocument();
}

bool Project::load(XmlStreamReader * reader)
{
	while (!(reader->isStartDocument() || reader->atEnd()))
		reader->readNext();
	if(!(reader->atEnd()))
	{
		if (!reader->skipToNextTag()) return false;

		if (reader->name() == "scidavis_project") 
		{
			bool ok;
			int version = reader->readAttributeInt("version", &ok);
			if(!ok) 
			{
				reader->raiseError(tr("invalid or missing project version"));
				return false;
			}

			// version dependent staff goes here
			
			while (!reader->atEnd()) 
			{
				reader->readNext();

				if (reader->isEndElement()) break;

				if (reader->isStartElement()) 
				{
					if (reader->name() == "project_root")
					{
						if (!reader->skipToNextTag()) return false;
						if (!future::Folder::load(reader)) return false;
						if (!reader->skipToNextTag()) return false;
						Q_ASSERT(reader->isEndElement() && reader->name() == "project_root");
					}
					else // unknown element
					{
						reader->raiseWarning(tr("unknown element '%1'").arg(reader->name().toString()));
						if (!reader->skipToEndElement()) return false;
					}
				} 
			}
		}
		else // no project element
			reader->raiseError(tr("no scidavis_project element found"));
	}
	else // no start document
		reader->raiseError(tr("no valid XML document found"));

	return !reader->hasError();
}