Theme Editor: Working on the project viewer infrastructure
[kugel-rb.git] / utils / themeeditor / editorwindow.cpp
blobd1f3609c50cf6613b8ac653d8331aef7a4cb76ca
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 "ui_editorwindow.h"
25 #include <QDesktopWidget>
26 #include <QFileSystemModel>
27 #include <QSettings>
28 #include <QFileDialog>
30 EditorWindow::EditorWindow(QWidget *parent) :
31 QMainWindow(parent),
32 ui(new Ui::EditorWindow)
34 ui->setupUi(this);
35 prefs = new PreferencesDialog(this);
36 project = 0;
37 loadSettings();
38 setupUI();
39 setupMenus();
42 void EditorWindow::loadSettings()
45 QSettings settings;
47 /* Main Window location */
48 settings.beginGroup("EditorWindow");
49 QSize size = settings.value("size").toSize();
50 QPoint pos = settings.value("position").toPoint();
51 QByteArray state = settings.value("state").toByteArray();
52 settings.endGroup();
54 if(!(size.isNull() || pos.isNull() || state.isNull()))
56 resize(size);
57 move(pos);
58 restoreState(state);
64 void EditorWindow::saveSettings()
67 QSettings settings;
69 /* Saving window and panel positions */
70 settings.beginGroup("EditorWindow");
71 settings.setValue("position", pos());
72 settings.setValue("size", size());
73 settings.setValue("state", saveState());
74 settings.endGroup();
77 void EditorWindow::setupUI()
79 /* Connecting the tab bar signals */
80 QObject::connect(ui->editorTabs, SIGNAL(currentChanged(int)),
81 this, SLOT(shiftTab(int)));
82 QObject::connect(ui->editorTabs, SIGNAL(tabCloseRequested(int)),
83 this, SLOT(closeTab(int)));
85 /* Connecting the code gen button */
86 QObject::connect(ui->fromTree, SIGNAL(pressed()),
87 this, SLOT(updateCurrent()));
89 /* Connecting the preferences dialog */
90 QObject::connect(ui->actionPreferences, SIGNAL(triggered()),
91 prefs, SLOT(exec()));
93 /* Setting up the parse status label */
94 parseStatus = new QLabel(this);
95 ui->statusbar->addPermanentWidget(parseStatus);
99 void EditorWindow::setupMenus()
101 /* Connecting panel show actions */
102 QObject::connect(ui->actionFile_Panel, SIGNAL(triggered()),
103 this, SLOT(showPanel()));
104 QObject::connect(ui->actionDisplay_Panel, SIGNAL(triggered()),
105 this, SLOT(showPanel()));
106 QObject::connect(ui->actionPreview_Panel, SIGNAL(triggered()),
107 this, SLOT(showPanel()));
109 /* Connecting the document management actions */
110 QObject::connect(ui->actionNew_Document, SIGNAL(triggered()),
111 this, SLOT(newTab()));
112 QObject::connect(ui->actionToolbarNew, SIGNAL(triggered()),
113 this, SLOT(newTab()));
115 QObject::connect(ui->actionClose_Document, SIGNAL(triggered()),
116 this, SLOT(closeCurrent()));
118 QObject::connect(ui->actionSave_Document, SIGNAL(triggered()),
119 this, SLOT(saveCurrent()));
120 QObject::connect(ui->actionSave_Document_As, SIGNAL(triggered()),
121 this, SLOT(saveCurrentAs()));
122 QObject::connect(ui->actionToolbarSave, SIGNAL(triggered()),
123 this, SLOT(saveCurrent()));
125 QObject::connect(ui->actionOpen_Document, SIGNAL(triggered()),
126 this, SLOT(openFile()));
127 QObject::connect(ui->actionToolbarOpen, SIGNAL(triggered()),
128 this, SLOT(openFile()));
130 QObject::connect(ui->actionOpen_Project, SIGNAL(triggered()),
131 this, SLOT(openProject()));
134 void EditorWindow::addTab(SkinDocument *doc)
136 ui->editorTabs->addTab(doc, doc->getTitle());
138 /* Connecting to title change events */
139 QObject::connect(doc, SIGNAL(titleChanged(QString)),
140 this, SLOT(tabTitleChanged(QString)));
142 /* Connecting to settings change events */
143 doc->connectPrefs(prefs);
147 void EditorWindow::newTab()
149 SkinDocument* doc = new SkinDocument(parseStatus);
150 addTab(doc);
153 void EditorWindow::shiftTab(int index)
155 if(index < 0)
157 ui->parseTree->setModel(0);
158 ui->actionSave_Document->setEnabled(false);
159 ui->actionSave_Document_As->setEnabled(false);
160 ui->actionClose_Document->setEnabled(false);
161 ui->actionToolbarSave->setEnabled(false);
162 ui->fromTree->setEnabled(false);
164 else
166 /* Syncing the tree view and the status bar */
167 SkinDocument* doc = dynamic_cast<SkinDocument*>
168 (ui->editorTabs->currentWidget());
169 ui->parseTree->setModel(doc->getModel());
170 parseStatus->setText(doc->getStatus());
172 ui->actionSave_Document->setEnabled(true);
173 ui->actionSave_Document_As->setEnabled(true);
174 ui->actionClose_Document->setEnabled(true);
175 ui->actionToolbarSave->setEnabled(true);
176 ui->fromTree->setEnabled(true);
180 bool EditorWindow::closeTab(int index)
182 SkinDocument* widget = dynamic_cast<SkinDocument*>
183 (ui->editorTabs->widget(index));
184 if(widget->requestClose())
186 ui->editorTabs->removeTab(index);
187 widget->deleteLater();
188 return true;
191 return false;
194 void EditorWindow::closeCurrent()
196 closeTab(ui->editorTabs->currentIndex());
199 void EditorWindow::saveCurrent()
201 if(ui->editorTabs->currentIndex() >= 0)
202 dynamic_cast<SkinDocument*>(ui->editorTabs->currentWidget())->save();
205 void EditorWindow::saveCurrentAs()
207 if(ui->editorTabs->currentIndex() >= 0)
208 dynamic_cast<SkinDocument*>(ui->editorTabs->currentWidget())->saveAs();
211 void EditorWindow::openFile()
213 QStringList fileNames;
214 QSettings settings;
216 settings.beginGroup("SkinDocument");
217 QString directory = settings.value("defaultDirectory", "").toString();
218 fileNames = QFileDialog::getOpenFileNames(this, tr("Open Files"), directory,
219 SkinDocument::fileFilter());
221 for(int i = 0; i < fileNames.count(); i++)
223 if(!QFile::exists(fileNames[i]))
224 continue;
226 QString current = fileNames[i];
228 /* Adding a new document for each file name */
229 SkinDocument* doc = new SkinDocument(parseStatus, current);
230 addTab(doc);
232 /* And setting the new default directory */
233 current.chop(current.length() - current.lastIndexOf('/') - 1);
234 settings.setValue("defaultDirectory", current);
238 settings.endGroup();
241 void EditorWindow::openProject()
243 QString fileName;
244 QSettings settings;
246 settings.beginGroup("ProjectModel");
247 QString directory = settings.value("defaultDirectory", "").toString();
248 fileName = QFileDialog::getOpenFileName(this, tr("Open Project"), directory,
249 ProjectModel::fileFilter());
251 if(QFile::exists(fileName))
254 if(project)
255 delete project;
257 project = new ProjectModel(fileName);
258 ui->projectTree->setModel(project);
260 fileName.chop(fileName.length() - fileName.lastIndexOf('/') - 1);
261 settings.setValue("defaultDirectory", fileName);
265 settings.endGroup();
269 void EditorWindow::tabTitleChanged(QString title)
271 SkinDocument* sender = dynamic_cast<SkinDocument*>(QObject::sender());
272 ui->editorTabs->setTabText(ui->editorTabs->indexOf(sender), title);
275 void EditorWindow::showPanel()
277 if(sender() == ui->actionFile_Panel)
278 ui->projectDock->setVisible(true);
279 if(sender() == ui->actionPreview_Panel)
280 ui->skinPreviewDock->setVisible(true);
281 if(sender() == ui->actionDisplay_Panel)
282 ui->parseTreeDock->setVisible(true);
285 void EditorWindow::closeEvent(QCloseEvent* event)
288 saveSettings();
290 /* Closing all the tabs */
291 for(int i = 0; i < ui->editorTabs->count(); i++)
293 if(!dynamic_cast<SkinDocument*>
294 (ui->editorTabs->widget(i))->requestClose())
296 event->ignore();
297 return;
301 event->accept();
304 void EditorWindow::updateCurrent()
306 if(ui->editorTabs->currentIndex() < 0)
307 return;
309 dynamic_cast<SkinDocument*>
310 (ui->editorTabs->currentWidget())->genCode();
313 EditorWindow::~EditorWindow()
315 delete ui;
316 delete prefs;
317 if(project)
318 delete project;