Theme Editor: Recent documents/projects that no longer exist are now cleared from...
[kugel-rb.git] / utils / themeeditor / gui / editorwindow.cpp
blob6bc40fbff63c98dac0666bf38e5220b831f10e85
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Robert Bieber
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "editorwindow.h"
23 #include "projectmodel.h"
24 #include "ui_editorwindow.h"
25 #include "rbfontcache.h"
26 #include "rbtextcache.h"
27 #include "newprojectdialog.h"
28 #include "projectexporter.h"
30 #include <QDesktopWidget>
31 #include <QFileSystemModel>
32 #include <QSettings>
33 #include <QFileDialog>
34 #include <QMessageBox>
35 #include <QGraphicsScene>
36 #include <QDir>
37 #include <QFile>
39 const int EditorWindow::numRecent = 5;
41 EditorWindow::EditorWindow(QWidget *parent) :
42 QMainWindow(parent), ui(new Ui::EditorWindow), parseTreeSelection(0)
44 ui->setupUi(this);
45 prefs = new PreferencesDialog(this);
46 project = 0;
47 setupUI();
48 loadSettings();
49 setupMenus();
53 EditorWindow::~EditorWindow()
55 delete ui;
56 delete prefs;
57 if(project)
58 delete project;
59 delete deviceConfig;
60 delete deviceDock;
61 delete timer;
62 delete timerDock;
64 RBFontCache::clearCache();
65 RBTextCache::clearCache();
68 void EditorWindow::loadTabFromSkinFile(QString fileName)
70 docToTop(fileName);
72 /* Checking to see if the file is already open */
73 for(int i = 0; i < ui->editorTabs->count(); i++)
75 TabContent* current = dynamic_cast<TabContent*>
76 (ui->editorTabs->widget(i));
77 if(current->file() == fileName)
79 ui->editorTabs->setCurrentIndex(i);
80 return;
84 /* Adding a new document*/
85 SkinDocument* doc = new SkinDocument(parseStatus, fileName, project,
86 deviceConfig);
87 addTab(doc);
88 ui->editorTabs->setCurrentWidget(doc);
92 void EditorWindow::loadConfigTab(ConfigDocument* doc)
94 for(int i = 0; i < ui->editorTabs->count(); i++)
96 TabContent* current = dynamic_cast<TabContent*>
97 (ui->editorTabs->widget(i));
98 if(current->file() == doc->file())
100 ui->editorTabs->setCurrentIndex(i);
101 doc->deleteLater();
102 return;
106 addTab(doc);
107 ui->editorTabs->setCurrentWidget(doc);
109 QObject::connect(doc, SIGNAL(titleChanged(QString)),
110 this, SLOT(tabTitleChanged(QString)));
113 void EditorWindow::loadSettings()
116 QSettings settings;
118 /* Main Window location */
119 settings.beginGroup("EditorWindow");
120 QSize size = settings.value("size").toSize();
121 QPoint pos = settings.value("position").toPoint();
122 QByteArray state = settings.value("state").toByteArray();
124 /* Recent docs/projects */
125 recentDocs = settings.value("recentDocs", QStringList()).toStringList();
126 recentProjects = settings.value("recentProjects",
127 QStringList()).toStringList();
129 settings.endGroup();
131 if(!(size.isNull() || pos.isNull() || state.isNull()))
133 resize(size);
134 move(pos);
135 restoreState(state);
140 void EditorWindow::saveSettings()
143 QSettings settings;
145 /* Saving window and panel positions */
146 settings.beginGroup("EditorWindow");
147 settings.setValue("position", pos());
148 settings.setValue("size", size());
149 settings.setValue("state", saveState());
151 /* Saving recent docs/projects */
152 settings.setValue("recentDocs", recentDocs);
153 settings.setValue("recentProjects", recentProjects);
155 settings.endGroup();
158 void EditorWindow::setupUI()
160 /* Connecting the tab bar signals */
161 QObject::connect(ui->editorTabs, SIGNAL(currentChanged(int)),
162 this, SLOT(shiftTab(int)));
163 QObject::connect(ui->editorTabs, SIGNAL(tabCloseRequested(int)),
164 this, SLOT(closeTab(int)));
166 /* Connecting the code gen button */
167 QObject::connect(ui->fromTree, SIGNAL(pressed()),
168 this, SLOT(updateCurrent()));
170 /* Connecting the preferences dialog */
171 QObject::connect(ui->actionPreferences, SIGNAL(triggered()),
172 prefs, SLOT(exec()));
174 /* Setting up the parse status label */
175 parseStatus = new QLabel(this);
176 ui->statusbar->addPermanentWidget(parseStatus);
178 /* Setting the selection for parse tree highlighting initially NULL */
179 parseTreeSelection = 0;
181 /* Adding the skin viewer */
182 viewer = new SkinViewer(this);
183 ui->skinPreviewLayout->addWidget(viewer);
185 /* Positioning the device settings dialog */
186 deviceDock = new QDockWidget(tr("Device Configuration"), this);
187 deviceConfig = new DeviceState(deviceDock);
189 deviceDock->setObjectName("deviceDock");
190 deviceDock->setWidget(deviceConfig);
191 deviceDock->setFloating(true);
192 deviceDock->move(QPoint(x() + width() / 2, y() + height() / 2));
193 deviceDock->hide();
195 /* Positioning the timer panel */
196 timerDock = new QDockWidget(tr("Timer"), this);
197 timer = new SkinTimer(deviceConfig, timerDock);
199 timerDock->setObjectName("timerDock");
200 timerDock->setWidget(timer);
201 timerDock->setFloating(true);
202 timerDock->move(QPoint(x() + width() / 2, y() + height() / 2));
203 timerDock->hide();
205 shiftTab(-1);
208 void EditorWindow::setupMenus()
210 /* Connecting panel show actions */
211 QObject::connect(ui->actionFile_Panel, SIGNAL(triggered()),
212 this, SLOT(showPanel()));
213 QObject::connect(ui->actionDisplay_Panel, SIGNAL(triggered()),
214 this, SLOT(showPanel()));
215 QObject::connect(ui->actionPreview_Panel, SIGNAL(triggered()),
216 this, SLOT(showPanel()));
217 QObject::connect(ui->actionDevice_Configuration, SIGNAL(triggered()),
218 deviceDock, SLOT(show()));
219 QObject::connect(ui->actionTimer, SIGNAL(triggered()),
220 timerDock, SLOT(show()));
222 /* Connecting the document management actions */
223 QObject::connect(ui->actionNew_Document, SIGNAL(triggered()),
224 this, SLOT(newTab()));
225 QObject::connect(ui->actionNew_Project, SIGNAL(triggered()),
226 this, SLOT(newProject()));
227 QObject::connect(ui->actionToolbarNew, SIGNAL(triggered()),
228 this, SLOT(newTab()));
230 QObject::connect(ui->actionClose_Document, SIGNAL(triggered()),
231 this, SLOT(closeCurrent()));
232 QObject::connect(ui->actionClose_Project, SIGNAL(triggered()),
233 this, SLOT(closeProject()));
235 QObject::connect(ui->actionSave_Document, SIGNAL(triggered()),
236 this, SLOT(saveCurrent()));
237 QObject::connect(ui->actionSave_Document_As, SIGNAL(triggered()),
238 this, SLOT(saveCurrentAs()));
239 QObject::connect(ui->actionToolbarSave, SIGNAL(triggered()),
240 this, SLOT(saveCurrent()));
241 QObject::connect(ui->actionExport_Project, SIGNAL(triggered()),
242 this, SLOT(exportProject()));
244 QObject::connect(ui->actionOpen_Document, SIGNAL(triggered()),
245 this, SLOT(openFile()));
246 QObject::connect(ui->actionToolbarOpen, SIGNAL(triggered()),
247 this, SLOT(openFile()));
249 QObject::connect(ui->actionOpen_Project, SIGNAL(triggered()),
250 this, SLOT(openProject()));
252 /* Connecting the edit menu */
253 QObject::connect(ui->actionUndo, SIGNAL(triggered()),
254 this, SLOT(undo()));
255 QObject::connect(ui->actionRedo, SIGNAL(triggered()),
256 this, SLOT(redo()));
257 QObject::connect(ui->actionCut, SIGNAL(triggered()),
258 this, SLOT(cut()));
259 QObject::connect(ui->actionCopy, SIGNAL(triggered()),
260 this, SLOT(copy()));
261 QObject::connect(ui->actionPaste, SIGNAL(triggered()),
262 this, SLOT(paste()));
263 QObject::connect(ui->actionFind_Replace, SIGNAL(triggered()),
264 this, SLOT(findReplace()));
266 /* Adding the recent docs/projects menus */
267 for(int i = 0; i < numRecent; i++)
269 recentDocsMenu.append(new QAction(tr("Recent Doc"),
270 ui->menuRecent_Files));
271 recentDocsMenu.last()
272 ->setShortcut(QKeySequence(tr("CTRL+")
273 + QString::number(i + 1)));
274 QObject::connect(recentDocsMenu.last(), SIGNAL(triggered()),
275 this, SLOT(openRecentFile()));
276 ui->menuRecent_Files->addAction(recentDocsMenu.last());
279 recentProjectsMenu.append(new QAction(tr("Recent Project"),
280 ui->menuRecent_Projects));
281 recentProjectsMenu.last()
282 ->setShortcut(QKeySequence(tr("CTRL+SHIFT+") +
283 QString::number(i + 1)));
284 QObject::connect(recentProjectsMenu.last(), SIGNAL(triggered()),
285 this, SLOT(openRecentProject()));
286 ui->menuRecent_Projects->addAction(recentProjectsMenu.last());
288 refreshRecentMenus();
291 void EditorWindow::addTab(TabContent *doc)
293 ui->editorTabs->addTab(doc, doc->title());
295 /* Connecting to title change events */
296 QObject::connect(doc, SIGNAL(titleChanged(QString)),
297 this, SLOT(tabTitleChanged(QString)));
298 QObject::connect(doc, SIGNAL(lineChanged(int)),
299 this, SLOT(lineChanged(int)));
301 /* Connecting to settings change events */
302 if(doc->type() == TabContent::Skin)
303 dynamic_cast<SkinDocument*>(doc)->connectPrefs(prefs);
307 void EditorWindow::newTab()
309 SkinDocument* doc = new SkinDocument(parseStatus, project, deviceConfig);
310 addTab(doc);
311 ui->editorTabs->setCurrentWidget(doc);
314 void EditorWindow::newProject()
316 NewProjectDialog dialog(this);
317 if(dialog.exec() == QDialog::Rejected)
318 return;
320 /* Assembling the new project if the dialog was accepted */
321 NewProjectDialog::NewProjectInfo info = dialog.results();
323 QDir path(info.path);
324 if(!path.exists())
326 QMessageBox::warning(this, tr("Error Creating Project"), tr("Error:"
327 " Project directory does not exist"));
328 return;
331 if(path.exists(info.name))
333 QMessageBox::warning(this, tr("Error Creating Project"), tr("Error:"
334 " Project directory already exists"));
335 return;
338 if(!path.mkdir(info.name))
340 QMessageBox::warning(this, tr("Error Creating Project"), tr("Error:"
341 " Project directory not writeable"));
342 return;
345 path.cd(info.name);
347 /* Making standard directories */
348 path.mkdir("fonts");
349 path.mkdir("icons");
350 path.mkdir("backdrops");
352 /* Adding the desired wps documents */
353 path.mkdir("wps");
354 path.cd("wps");
356 if(info.sbs)
357 createFile(path.filePath(info.name + ".sbs"), tr("# SBS Document\n"));
358 if(info.wps)
359 createFile(path.filePath(info.name + ".wps"), tr("# WPS Document\n"));
360 if(info.fms)
361 createFile(path.filePath(info.name + ".fms"), tr("# FMS Document\n"));
362 if(info.rsbs)
363 createFile(path.filePath(info.name + ".rsbs"), tr("# RSBS Document\n"));
364 if(info.rwps)
365 createFile(path.filePath(info.name + ".rwps"), tr("# RWPS Document\n"));
366 if(info.rfms)
367 createFile(path.filePath(info.name + ".rfms"), tr("# RFMS Document\n"));
369 path.mkdir(info.name);
371 path.cdUp();
373 /* Adding the config file */
374 path.mkdir("themes");
375 path.cd("themes");
377 /* Generating the config file */
378 QString config = tr("# Config file for ") + info.name + "\n";
379 config.append("#target: " + info.target + "\n\n");
380 QString wpsBase = "/.rockbox/wps/";
381 if(info.sbs)
382 config.append("sbs: " + wpsBase + info.name + ".sbs\n");
383 if(info.wps)
384 config.append("wps: " + wpsBase + info.name + ".wps\n");
385 if(info.fms)
386 config.append("fms: " + wpsBase + info.name + ".fms\n");
387 if(info.rsbs)
388 config.append("rsbs: " + wpsBase + info.name + ".rsbs\n");
389 if(info.rwps)
390 config.append("rwps: " + wpsBase + info.name + ".rwps\n");
391 if(info.rfms)
392 config.append("rfms: " + wpsBase + info.name + ".rfms\n");
395 createFile(path.filePath(info.name + ".cfg"),
396 config);
398 /* Opening the new project */
399 loadProjectFile(path.filePath(info.name + ".cfg"));
402 void EditorWindow::shiftTab(int index)
404 TabContent* widget = dynamic_cast<TabContent*>
405 (ui->editorTabs->currentWidget());
406 if(index < 0)
408 ui->parseTree->setModel(0);
409 ui->actionSave_Document->setEnabled(false);
410 ui->actionSave_Document_As->setEnabled(false);
411 ui->actionClose_Document->setEnabled(false);
412 ui->actionToolbarSave->setEnabled(false);
413 ui->fromTree->setEnabled(false);
414 ui->actionUndo->setEnabled(false);
415 ui->actionRedo->setEnabled(false);
416 ui->actionCut->setEnabled(false);
417 ui->actionCopy->setEnabled(false);
418 ui->actionPaste->setEnabled(false);
419 ui->actionFind_Replace->setEnabled(false);
420 viewer->setScene(0);
422 else if(widget->type() == TabContent::Config)
424 ui->actionSave_Document->setEnabled(true);
425 ui->actionSave_Document_As->setEnabled(true);
426 ui->actionClose_Document->setEnabled(true);
427 ui->actionToolbarSave->setEnabled(true);
428 ui->actionUndo->setEnabled(false);
429 ui->actionRedo->setEnabled(false);
430 ui->actionCut->setEnabled(false);
431 ui->actionCopy->setEnabled(false);
432 ui->actionPaste->setEnabled(false);
433 ui->actionFind_Replace->setEnabled(false);
434 viewer->setScene(0);
436 else if(widget->type() == TabContent::Skin)
438 /* Syncing the tree view and the status bar */
439 SkinDocument* doc = dynamic_cast<SkinDocument*>(widget);
440 ui->parseTree->setModel(doc->getModel());
441 parseStatus->setText(doc->getStatus());
443 ui->actionSave_Document->setEnabled(true);
444 ui->actionSave_Document_As->setEnabled(true);
445 ui->actionClose_Document->setEnabled(true);
446 ui->actionToolbarSave->setEnabled(true);
447 ui->fromTree->setEnabled(true);
449 ui->actionUndo->setEnabled(true);
450 ui->actionRedo->setEnabled(true);
451 ui->actionCut->setEnabled(true);
452 ui->actionCopy->setEnabled(true);
453 ui->actionPaste->setEnabled(true);
454 ui->actionFind_Replace->setEnabled(true);
456 sizeColumns();
458 /* Syncing the preview */
459 viewer->setScene(doc->scene());
463 /* Hiding all the find/replace dialogs */
464 for(int i = 0; i < ui->editorTabs->count(); i++)
465 if(dynamic_cast<TabContent*>(ui->editorTabs->widget(i))->type() ==
466 TabContent::Skin)
467 dynamic_cast<SkinDocument*>(ui->editorTabs->widget(i))->hideFind();
471 bool EditorWindow::closeTab(int index)
473 TabContent* widget = dynamic_cast<TabContent*>
474 (ui->editorTabs->widget(index));
475 if(widget->requestClose())
477 ui->editorTabs->removeTab(index);
478 widget->deleteLater();
479 return true;
482 return false;
485 void EditorWindow::closeCurrent()
487 closeTab(ui->editorTabs->currentIndex());
490 void EditorWindow::closeProject()
492 if(project)
494 project->deleteLater();
495 project = 0;
498 for(int i = 0; i < ui->editorTabs->count(); i++)
500 TabContent* doc = dynamic_cast<TabContent*>
501 (ui->editorTabs->widget(i));
502 if(doc->type() == TabContent::Skin)
504 dynamic_cast<SkinDocument*>(doc)->setProject(project);
505 if(i == ui->editorTabs->currentIndex())
507 viewer->setScene(dynamic_cast<SkinDocument*>(doc)->scene());
512 ui->actionClose_Project->setEnabled(false);
513 ui->actionExport_Project->setEnabled(false);
516 void EditorWindow::saveCurrent()
518 if(ui->editorTabs->currentIndex() >= 0)
519 dynamic_cast<TabContent*>(ui->editorTabs->currentWidget())->save();
522 void EditorWindow::saveCurrentAs()
524 if(ui->editorTabs->currentIndex() >= 0)
525 dynamic_cast<TabContent*>(ui->editorTabs->currentWidget())->saveAs();
528 void EditorWindow::exportProject()
530 QDir dir = project->getSetting("themebase", "");
531 dir.cdUp();
532 QString file = project->getSetting("configfile", "").split("/").
533 last().split(".").first() + ".zip";
534 file = dir.filePath(file);
536 file = QFileDialog::getSaveFileName(this, tr("Export Project"),
537 file, "Zip Files (*.zip *.ZIP);;"
538 "All Files (*)");
540 if(file != "")
542 ProjectExporter* exporter = new ProjectExporter(file, project, this);
543 exporter->show();
547 void EditorWindow::openFile()
549 QStringList fileNames;
550 QSettings settings;
552 settings.beginGroup("SkinDocument");
553 QString directory = settings.value("defaultDirectory", "").toString();
554 fileNames = QFileDialog::getOpenFileNames(this, tr("Open Files"), directory,
555 SkinDocument::fileFilter());
557 for(int i = 0; i < fileNames.count(); i++)
559 if(!QFile::exists(fileNames[i]))
560 continue;
562 QString current = fileNames[i];
564 loadTabFromSkinFile(current);
566 /* And setting the new default directory */
567 current.chop(current.length() - current.lastIndexOf('/') - 1);
568 settings.setValue("defaultDirectory", current);
572 settings.endGroup();
575 void EditorWindow::openProject()
577 QString fileName;
578 QSettings settings;
580 settings.beginGroup("ProjectModel");
581 QString directory = settings.value("defaultDirectory", "").toString();
582 fileName = QFileDialog::getOpenFileName(this, tr("Open Project"), directory,
583 ProjectModel::fileFilter());
585 settings.endGroup();
586 loadProjectFile(fileName);
590 void EditorWindow::openRecentFile()
592 loadTabFromSkinFile(dynamic_cast<QAction*>(QObject::sender())->text());
595 void EditorWindow::openRecentProject()
597 loadProjectFile(dynamic_cast<QAction*>(QObject::sender())->text());
600 void EditorWindow::configFileChanged(QString configFile)
603 if(QFile::exists(configFile))
606 if(project)
607 delete project;
609 project = new ProjectModel(configFile, this);
610 ui->projectTree->setModel(project);
612 QObject::connect(ui->projectTree, SIGNAL(activated(QModelIndex)),
613 project, SLOT(activated(QModelIndex)));
615 for(int i = 0; i < ui->editorTabs->count(); i++)
617 TabContent* doc = dynamic_cast<TabContent*>
618 (ui->editorTabs->widget(i));
619 if(doc->type() == TabContent::Skin)
621 dynamic_cast<SkinDocument*>(doc)->setProject(project);
622 if(i == ui->editorTabs->currentIndex())
624 viewer->setScene(dynamic_cast<SkinDocument*>(doc)->scene());
634 void EditorWindow::tabTitleChanged(QString title)
636 TabContent* sender = dynamic_cast<TabContent*>(QObject::sender());
637 ui->editorTabs->setTabText(ui->editorTabs->indexOf(sender), title);
640 void EditorWindow::showPanel()
642 if(sender() == ui->actionFile_Panel)
643 ui->projectDock->setVisible(true);
644 if(sender() == ui->actionPreview_Panel)
645 ui->skinPreviewDock->setVisible(true);
646 if(sender() == ui->actionDisplay_Panel)
647 ui->parseTreeDock->setVisible(true);
650 void EditorWindow::closeEvent(QCloseEvent* event)
653 saveSettings();
655 /* Closing all the tabs */
656 for(int i = 0; i < ui->editorTabs->count(); i++)
658 if(!dynamic_cast<TabContent*>
659 (ui->editorTabs->widget(i))->requestClose())
661 event->ignore();
662 return;
666 event->accept();
669 void EditorWindow::updateCurrent()
671 if(ui->editorTabs->currentIndex() < 0)
672 return;
674 dynamic_cast<SkinDocument*>
675 (ui->editorTabs->currentWidget())->genCode();
678 void EditorWindow::lineChanged(int line)
680 QSettings settings;
681 settings.beginGroup("EditorWindow");
683 if(settings.value("autoExpandTree", false).toBool())
685 ui->parseTree->collapseAll();
686 ParseTreeModel* model = dynamic_cast<ParseTreeModel*>
687 (ui->parseTree->model());
688 parseTreeSelection = new QItemSelectionModel(model);
689 expandLine(model, QModelIndex(), line,
690 settings.value("autoHighlightTree", false).toBool());
691 sizeColumns();
692 ui->parseTree->setSelectionModel(parseTreeSelection);
695 settings.endGroup();
698 void EditorWindow::undo()
700 TabContent* doc = dynamic_cast<TabContent*>
701 (ui->editorTabs->currentWidget());
702 if(doc->type() == TabContent::Skin)
703 dynamic_cast<SkinDocument*>(doc)->getEditor()->undo();
706 void EditorWindow::redo()
708 TabContent* doc = dynamic_cast<TabContent*>
709 (ui->editorTabs->currentWidget());
710 if(doc->type() == TabContent::Skin)
711 dynamic_cast<SkinDocument*>(doc)->getEditor()->redo();
715 void EditorWindow::cut()
717 TabContent* doc = dynamic_cast<TabContent*>
718 (ui->editorTabs->currentWidget());
719 if(doc->type() == TabContent::Skin)
720 dynamic_cast<SkinDocument*>(doc)->getEditor()->cut();
723 void EditorWindow::copy()
725 TabContent* doc = dynamic_cast<TabContent*>
726 (ui->editorTabs->currentWidget());
727 if(doc->type() == TabContent::Skin)
728 dynamic_cast<SkinDocument*>(doc)->getEditor()->copy();
731 void EditorWindow::paste()
733 TabContent* doc = dynamic_cast<TabContent*>
734 (ui->editorTabs->currentWidget());
735 if(doc->type() == TabContent::Skin)
736 dynamic_cast<SkinDocument*>(doc)->getEditor()->paste();
739 void EditorWindow::findReplace()
741 TabContent* doc = dynamic_cast<TabContent*>
742 (ui->editorTabs->currentWidget());
743 if(doc->type() == TabContent::Skin)
744 dynamic_cast<SkinDocument*>(doc)->showFind();
748 void EditorWindow::expandLine(ParseTreeModel* model, QModelIndex parent,
749 int line, bool highlight)
751 for(int i = 0; i < model->rowCount(parent); i++)
753 QModelIndex dataType = model->index(i, ParseTreeModel::typeColumn,
754 parent);
755 QModelIndex dataVal = model->index(i, ParseTreeModel::valueColumn,
756 parent);
757 QModelIndex data = model->index(i, ParseTreeModel::lineColumn, parent);
758 QModelIndex recurse = model->index(i, 0, parent);
760 expandLine(model, recurse, line, highlight);
762 if(model->data(data, Qt::DisplayRole) == line)
764 ui->parseTree->expand(parent);
765 ui->parseTree->expand(data);
766 ui->parseTree->scrollTo(parent, QAbstractItemView::PositionAtTop);
768 if(highlight)
770 parseTreeSelection->select(data,
771 QItemSelectionModel::Select);
772 parseTreeSelection->select(dataType,
773 QItemSelectionModel::Select);
774 parseTreeSelection->select(dataVal,
775 QItemSelectionModel::Select);
782 void EditorWindow::sizeColumns()
784 /* Setting the column widths */
785 ui->parseTree->resizeColumnToContents(ParseTreeModel::lineColumn);
786 ui->parseTree->resizeColumnToContents(ParseTreeModel::typeColumn);
787 ui->parseTree->resizeColumnToContents(ParseTreeModel::valueColumn);
790 void EditorWindow::loadProjectFile(QString fileName)
792 QSettings settings;
793 settings.beginGroup("ProjectModel");
795 if(QFile::exists(fileName))
797 projectToTop(fileName);
799 if(project)
800 project->deleteLater();
802 ui->actionClose_Project->setEnabled(true);
803 ui->actionExport_Project->setEnabled(true);
805 project = new ProjectModel(fileName, this);
806 ui->projectTree->setModel(project);
808 /* Setting target info if necessary */
809 TargetData targets;
810 QString target = project->getSetting("#target", "");
811 if(target != "" && targets.index(target) >= 0)
813 int index = targets.index(target);
815 QRect screen = targets.screenSize(index);
816 deviceConfig->setData("screenwidth", screen.width());
817 deviceConfig->setData("screenheight", screen.height());
819 if(targets.remoteDepth(index) != TargetData::None)
821 QRect remote = targets.remoteSize(index);
822 deviceConfig->setData("remotewidth", remote.width());
823 deviceConfig->setData("remoteheight", remote.height());
826 deviceConfig->setData("tp", targets.fm(index));
827 deviceConfig->setData("Rp", targets.canRecord(index));
830 if(project->getSetting("#screenwidth") != "")
831 deviceConfig->setData("screenwidth",
832 project->getSetting("#screenwidth"));
833 if(project->getSetting("#screenheight") != "")
834 deviceConfig->setData("screenheight",
835 project->getSetting("#screenheight"));
837 QObject::connect(ui->projectTree, SIGNAL(activated(QModelIndex)),
838 project, SLOT(activated(QModelIndex)));
840 fileName.chop(fileName.length() - fileName.lastIndexOf('/') - 1);
841 settings.setValue("defaultDirectory", fileName);
843 for(int i = 0; i < ui->editorTabs->count(); i++)
845 TabContent* doc = dynamic_cast<TabContent*>
846 (ui->editorTabs->widget(i));
847 if(doc->type() == TabContent::Skin)
849 dynamic_cast<SkinDocument*>(doc)->setProject(project);
850 if(i == ui->editorTabs->currentIndex())
852 viewer->setScene(dynamic_cast<SkinDocument*>(doc)->scene());
859 settings.endGroup();
863 void EditorWindow::createFile(QString filename, QString contents)
865 QFile fout(filename);
866 fout.open(QFile::WriteOnly);
868 fout.write(contents.toAscii());
870 fout.close();
873 void EditorWindow::docToTop(QString file)
875 if(!QFile::exists(file))
876 return;
878 int index = recentDocs.indexOf(file);
879 if(index == -1)
881 /* Bumping off the last file */
882 if(recentDocs.count() >= numRecent)
883 recentDocs.removeLast();
884 recentDocs.prepend(file);
886 else
888 /* Shuffling file to the top of the list */
889 recentDocs.removeAt(index);
890 recentDocs.prepend(file);
893 refreshRecentMenus();
896 void EditorWindow::projectToTop(QString file)
898 if(!QFile::exists(file))
899 return;
901 int index = recentProjects.indexOf(file);
902 if(index == -1)
904 /* Bumping off the last project */
905 if(recentProjects.count() >= numRecent)
906 recentProjects.removeLast();
907 recentProjects.prepend(file);
909 else
911 /* Shuffling file to the top of the list */
912 recentProjects.removeAt(index);
913 recentProjects.prepend(file);
916 refreshRecentMenus();
919 void EditorWindow::refreshRecentMenus()
921 /* Clearing any deleted documents */
922 for(int i = 0; i < recentDocs.count(); i++)
923 if(!QFile::exists(recentDocs[i]))
924 recentDocs.removeAt(i--);
926 /* Clearing any deleted projects */
927 for(int i = 0; i < recentProjects.count(); i++)
928 if(!QFile::exists(recentProjects[i]))
929 recentProjects.removeAt(i--);
931 /* First hiding all the menu items */
932 for(int i = 0; i < recentDocsMenu.count(); i++)
933 recentDocsMenu[i]->setVisible(false);
934 for(int i = 0; i < recentProjectsMenu.count(); i++)
935 recentProjectsMenu[i]->setVisible(false);
937 /* Then setting the text of and showing any available */
938 for(int i = 0; i < recentDocs.count(); i++)
940 recentDocsMenu[i]->setText(recentDocs[i]);
941 recentDocsMenu[i]->setVisible(true);
944 for(int i = 0; i < recentProjects.count(); i++)
946 recentProjectsMenu[i]->setText(recentProjects[i]);
947 recentProjectsMenu[i]->setVisible(true);