Theme Editor: Stripped out the sub-classes for ProjectModel and turned ProjectModel...
[kugel-rb.git] / utils / themeeditor / editorwindow.cpp
blob035fe57f2d3d65be59282b0a0153b1dbb6e0450d
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"
26 #include <QDesktopWidget>
27 #include <QFileSystemModel>
28 #include <QSettings>
29 #include <QFileDialog>
31 EditorWindow::EditorWindow(QWidget *parent) :
32 QMainWindow(parent),
33 ui(new Ui::EditorWindow)
35 ui->setupUi(this);
36 prefs = new PreferencesDialog(this);
37 project = 0;
38 loadSettings();
39 setupUI();
40 setupMenus();
43 void EditorWindow::loadTabFromFile(QString fileName)
45 /* Checking to see if the file is already open */
46 for(int i = 0; i < ui->editorTabs->count(); i++)
48 SkinDocument* current = dynamic_cast<SkinDocument*>
49 (ui->editorTabs->widget(i));
50 if(current->getFile() == fileName)
52 ui->editorTabs->setCurrentIndex(i);
53 return;
57 /* Adding a new document*/
58 SkinDocument* doc = new SkinDocument(parseStatus, fileName);
59 addTab(doc);
63 void EditorWindow::loadSettings()
66 QSettings settings;
68 /* Main Window location */
69 settings.beginGroup("EditorWindow");
70 QSize size = settings.value("size").toSize();
71 QPoint pos = settings.value("position").toPoint();
72 QByteArray state = settings.value("state").toByteArray();
73 settings.endGroup();
75 if(!(size.isNull() || pos.isNull() || state.isNull()))
77 resize(size);
78 move(pos);
79 restoreState(state);
85 void EditorWindow::saveSettings()
88 QSettings settings;
90 /* Saving window and panel positions */
91 settings.beginGroup("EditorWindow");
92 settings.setValue("position", pos());
93 settings.setValue("size", size());
94 settings.setValue("state", saveState());
95 settings.endGroup();
98 void EditorWindow::setupUI()
100 /* Connecting the tab bar signals */
101 QObject::connect(ui->editorTabs, SIGNAL(currentChanged(int)),
102 this, SLOT(shiftTab(int)));
103 QObject::connect(ui->editorTabs, SIGNAL(tabCloseRequested(int)),
104 this, SLOT(closeTab(int)));
106 /* Connecting the code gen button */
107 QObject::connect(ui->fromTree, SIGNAL(pressed()),
108 this, SLOT(updateCurrent()));
110 /* Connecting the preferences dialog */
111 QObject::connect(ui->actionPreferences, SIGNAL(triggered()),
112 prefs, SLOT(exec()));
114 /* Setting up the parse status label */
115 parseStatus = new QLabel(this);
116 ui->statusbar->addPermanentWidget(parseStatus);
120 void EditorWindow::setupMenus()
122 /* Connecting panel show actions */
123 QObject::connect(ui->actionFile_Panel, SIGNAL(triggered()),
124 this, SLOT(showPanel()));
125 QObject::connect(ui->actionDisplay_Panel, SIGNAL(triggered()),
126 this, SLOT(showPanel()));
127 QObject::connect(ui->actionPreview_Panel, SIGNAL(triggered()),
128 this, SLOT(showPanel()));
130 /* Connecting the document management actions */
131 QObject::connect(ui->actionNew_Document, SIGNAL(triggered()),
132 this, SLOT(newTab()));
133 QObject::connect(ui->actionToolbarNew, SIGNAL(triggered()),
134 this, SLOT(newTab()));
136 QObject::connect(ui->actionClose_Document, SIGNAL(triggered()),
137 this, SLOT(closeCurrent()));
139 QObject::connect(ui->actionSave_Document, SIGNAL(triggered()),
140 this, SLOT(saveCurrent()));
141 QObject::connect(ui->actionSave_Document_As, SIGNAL(triggered()),
142 this, SLOT(saveCurrentAs()));
143 QObject::connect(ui->actionToolbarSave, SIGNAL(triggered()),
144 this, SLOT(saveCurrent()));
146 QObject::connect(ui->actionOpen_Document, SIGNAL(triggered()),
147 this, SLOT(openFile()));
148 QObject::connect(ui->actionToolbarOpen, SIGNAL(triggered()),
149 this, SLOT(openFile()));
151 QObject::connect(ui->actionOpen_Project, SIGNAL(triggered()),
152 this, SLOT(openProject()));
155 void EditorWindow::addTab(SkinDocument *doc)
157 ui->editorTabs->addTab(doc, doc->getTitle());
159 /* Connecting to title change events */
160 QObject::connect(doc, SIGNAL(titleChanged(QString)),
161 this, SLOT(tabTitleChanged(QString)));
163 /* Connecting to settings change events */
164 doc->connectPrefs(prefs);
168 void EditorWindow::newTab()
170 SkinDocument* doc = new SkinDocument(parseStatus);
171 addTab(doc);
174 void EditorWindow::shiftTab(int index)
176 if(index < 0)
178 ui->parseTree->setModel(0);
179 ui->actionSave_Document->setEnabled(false);
180 ui->actionSave_Document_As->setEnabled(false);
181 ui->actionClose_Document->setEnabled(false);
182 ui->actionToolbarSave->setEnabled(false);
183 ui->fromTree->setEnabled(false);
185 else
187 /* Syncing the tree view and the status bar */
188 SkinDocument* doc = dynamic_cast<SkinDocument*>
189 (ui->editorTabs->currentWidget());
190 ui->parseTree->setModel(doc->getModel());
191 parseStatus->setText(doc->getStatus());
193 ui->actionSave_Document->setEnabled(true);
194 ui->actionSave_Document_As->setEnabled(true);
195 ui->actionClose_Document->setEnabled(true);
196 ui->actionToolbarSave->setEnabled(true);
197 ui->fromTree->setEnabled(true);
201 bool EditorWindow::closeTab(int index)
203 SkinDocument* widget = dynamic_cast<SkinDocument*>
204 (ui->editorTabs->widget(index));
205 if(widget->requestClose())
207 ui->editorTabs->removeTab(index);
208 widget->deleteLater();
209 return true;
212 return false;
215 void EditorWindow::closeCurrent()
217 closeTab(ui->editorTabs->currentIndex());
220 void EditorWindow::saveCurrent()
222 if(ui->editorTabs->currentIndex() >= 0)
223 dynamic_cast<SkinDocument*>(ui->editorTabs->currentWidget())->save();
226 void EditorWindow::saveCurrentAs()
228 if(ui->editorTabs->currentIndex() >= 0)
229 dynamic_cast<SkinDocument*>(ui->editorTabs->currentWidget())->saveAs();
232 void EditorWindow::openFile()
234 QStringList fileNames;
235 QSettings settings;
237 settings.beginGroup("SkinDocument");
238 QString directory = settings.value("defaultDirectory", "").toString();
239 fileNames = QFileDialog::getOpenFileNames(this, tr("Open Files"), directory,
240 SkinDocument::fileFilter());
242 for(int i = 0; i < fileNames.count(); i++)
244 if(!QFile::exists(fileNames[i]))
245 continue;
247 QString current = fileNames[i];
249 loadTabFromFile(current);
251 /* And setting the new default directory */
252 current.chop(current.length() - current.lastIndexOf('/') - 1);
253 settings.setValue("defaultDirectory", current);
257 settings.endGroup();
260 void EditorWindow::openProject()
262 QString fileName;
263 QSettings settings;
265 settings.beginGroup("ProjectModel");
266 QString directory = settings.value("defaultDirectory", "").toString();
267 fileName = QFileDialog::getOpenFileName(this, tr("Open Project"), directory,
268 ProjectModel::fileFilter());
270 if(QFile::exists(fileName))
273 if(project)
274 delete project;
276 project = new ProjectModel(fileName, this);
277 ui->projectTree->setModel(project);
279 QObject::connect(ui->projectTree, SIGNAL(activated(QModelIndex)),
280 project, SLOT(activated(QModelIndex)));
282 fileName.chop(fileName.length() - fileName.lastIndexOf('/') - 1);
283 settings.setValue("defaultDirectory", fileName);
287 settings.endGroup();
291 void EditorWindow::tabTitleChanged(QString title)
293 SkinDocument* sender = dynamic_cast<SkinDocument*>(QObject::sender());
294 ui->editorTabs->setTabText(ui->editorTabs->indexOf(sender), title);
297 void EditorWindow::showPanel()
299 if(sender() == ui->actionFile_Panel)
300 ui->projectDock->setVisible(true);
301 if(sender() == ui->actionPreview_Panel)
302 ui->skinPreviewDock->setVisible(true);
303 if(sender() == ui->actionDisplay_Panel)
304 ui->parseTreeDock->setVisible(true);
307 void EditorWindow::closeEvent(QCloseEvent* event)
310 saveSettings();
312 /* Closing all the tabs */
313 for(int i = 0; i < ui->editorTabs->count(); i++)
315 if(!dynamic_cast<SkinDocument*>
316 (ui->editorTabs->widget(i))->requestClose())
318 event->ignore();
319 return;
323 event->accept();
326 void EditorWindow::updateCurrent()
328 if(ui->editorTabs->currentIndex() < 0)
329 return;
331 dynamic_cast<SkinDocument*>
332 (ui->editorTabs->currentWidget())->genCode();
335 EditorWindow::~EditorWindow()
337 delete ui;
338 delete prefs;
339 if(project)
340 delete project;