Changeset View
Changeset View
Standalone View
Standalone View
src/project/project.cpp
Show First 20 Lines • Show All 177 Lines • ▼ Show 20 Line(s) | |||||
178 | 178 | | |||
179 | //TODO make graphEditor singleton | 179 | //TODO make graphEditor singleton | ||
180 | Project::Project(const QUrl &projectFile, GraphTheory::Editor *graphEditor) | 180 | Project::Project(const QUrl &projectFile, GraphTheory::Editor *graphEditor) | ||
181 | : d(new ProjectPrivate) | 181 | : d(new ProjectPrivate) | ||
182 | { | 182 | { | ||
183 | d->m_graphEditor = graphEditor; | 183 | d->m_graphEditor = graphEditor; | ||
184 | d->m_projectUrl = projectFile; | 184 | d->m_projectUrl = projectFile; | ||
185 | if (!d->loadProject(projectFile)) { | 185 | if (!d->loadProject(projectFile)) { | ||
186 | addCodeDocument(KTextEditor::Editor::instance()->createDocument(nullptr)); | | |||
187 | addGraphDocument(graphEditor->createDocument()); | 186 | addGraphDocument(graphEditor->createDocument()); | ||
188 | setModified(false); | 187 | setModified(false); | ||
189 | d->m_journal = KTextEditor::Editor::instance()->createDocument(nullptr); | 188 | d->m_journal = KTextEditor::Editor::instance()->createDocument(nullptr); | ||
190 | 189 | | |||
191 | KMessageBox::error(nullptr, | 190 | KMessageBox::error(nullptr, | ||
192 | i18nc("@info", | 191 | i18nc("@info", | ||
193 | "The Rocs project could not be imported because the project file could not be parsed.")); | 192 | "The Rocs project could not be imported because the project file could not be parsed.")); | ||
194 | } | 193 | } | ||
Show All 22 Lines | 215 | { | |||
217 | d->m_projectUrl = url; | 216 | d->m_projectUrl = url; | ||
218 | } | 217 | } | ||
219 | 218 | | |||
220 | QString Project::workingDir() const | 219 | QString Project::workingDir() const | ||
221 | { | 220 | { | ||
222 | return d->m_workingDirectory.path(); | 221 | return d->m_workingDirectory.path(); | ||
223 | } | 222 | } | ||
224 | 223 | | |||
225 | bool Project::addCodeDocument(KTextEditor::Document *document) | 224 | KTextEditor::Document* Project::createCodeDocument(const QString& filePath) | ||
226 | { | 225 | { | ||
227 | // compute first unused document path | 226 | auto path = d->m_workingDirectory.path() + QLatin1Char('/') + filePath + QStringLiteral(".js"); | ||
228 | QStringList usedPaths; | 227 | | ||
229 | foreach (KTextEditor::Document *document, d->m_codeDocuments) { | 228 | auto doc = KTextEditor::Editor::instance()->createDocument(nullptr); | ||
230 | usedPaths.append(document->url().toLocalFile()); | 229 | if (!doc->saveAs(QUrl::fromLocalFile(path))) { | ||
230 | qCritical() << "Error when saving code file to working directory, aborting."; | ||||
231 | return nullptr; | ||||
tcanabrava: QLatin1Char('/'),
QStringLiteral(".js")
Those creates the data in copile time instead of… | |||||
231 | } | 232 | } | ||
232 | QString path; | 233 | | ||
233 | for (int i = 0; i <= d->m_codeDocuments.count(); ++i) { | 234 | if (!addCodeDocument(doc)) { | ||
234 | path = d->m_workingDirectory.path() | 235 | return nullptr; | ||
235 | + QChar('/') | | |||
236 | + "codefile" + QString::number(i) + QString(".js"); | | |||
237 | if (!usedPaths.contains(path)) { | | |||
238 | break; | | |||
239 | } | 236 | } | ||
237 | | ||||
238 | return doc; | ||||
240 | } | 239 | } | ||
241 | 240 | | |||
242 | // put document into working directory | 241 | KTextEditor::Document* Project::openCodeDocument(const QUrl &url) | ||
243 | if (!document->saveAs(QUrl::fromLocalFile(path))) { | 242 | { | ||
244 | qCritical() << "Error when saving code file to working directory, aborting."; | 243 | auto doc = KTextEditor::Editor::instance()->createDocument(nullptr); | ||
244 | if (!doc->openUrl(url)) { | ||||
245 | qCritical() << "Error when opening code file, aborting."; | ||||
246 | return nullptr; | ||||
247 | } | ||||
248 | | ||||
249 | if (!addCodeDocument(doc)) { | ||||
250 | return nullptr; | ||||
251 | } | ||||
252 | | ||||
253 | return doc; | ||||
the code for createCode and openCode document are the same, share the internal code. tcanabrava: the code for createCode and openCode document are the same, share the internal code. | |||||
254 | } | ||||
255 | | ||||
256 | bool Project::addCodeDocument(KTextEditor::Document *document) | ||||
257 | { | ||||
258 | if (document == nullptr) { | ||||
245 | return false; | 259 | return false; | ||
246 | } | 260 | } | ||
247 | 261 | | |||
248 | emit codeDocumentAboutToBeAdded(document, d->m_codeDocuments.count()); | 262 | emit codeDocumentAboutToBeAdded(document, d->m_codeDocuments.count()); | ||
249 | connect(document, &KTextEditor::Document::modifiedChanged, | 263 | connect(document, &KTextEditor::Document::modifiedChanged, | ||
250 | this, &Project::modifiedChanged); | 264 | this, &Project::modifiedChanged); | ||
251 | d->m_codeDocuments.append(document); | 265 | d->m_codeDocuments.append(document); | ||
252 | emit codeDocumentAdded(); | 266 | emit codeDocumentAdded(); | ||
253 | setModified(true); | 267 | setModified(true); | ||
254 | return true; | 268 | return true; | ||
255 | } | 269 | } | ||
256 | 270 | | |||
257 | KTextEditor::Document * Project::importCodeDocument(const QUrl &url) | 271 | KTextEditor::Document * Project::importCodeDocument(const QUrl &url) | ||
258 | { | 272 | { | ||
259 | KTextEditor::Document *document = KTextEditor::Editor::instance()->createDocument(nullptr); | 273 | return openCodeDocument(url); | ||
260 | document->openUrl(url); | | |||
261 | addCodeDocument(document); | | |||
262 | return document; | | |||
263 | } | 274 | } | ||
264 | 275 | | |||
265 | void Project::tryToRemoveCodeDocument(KTextEditor::Document *document) | 276 | void Project::tryToRemoveCodeDocument(KTextEditor::Document *document) | ||
266 | { | 277 | { | ||
267 | QString path = document->url().toString(); | 278 | QString path = document->url().toString(); | ||
268 | if(!document->closeUrl()) | 279 | if(!document->closeUrl()) | ||
269 | return; | 280 | return; | ||
270 | int index = d->m_codeDocuments.indexOf(document); | 281 | int index = d->m_codeDocuments.indexOf(document); | ||
▲ Show 20 Lines • Show All 199 Lines • Show Last 20 Lines |
QLatin1Char('/'),
QStringLiteral(".js")
Those creates the data in copile time instead of runtime