1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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>
29 #include <QFileDialog>
31 EditorWindow::EditorWindow(QWidget
*parent
) :
33 ui(new Ui::EditorWindow
)
36 prefs
= new PreferencesDialog(this);
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
);
57 /* Adding a new document*/
58 SkinDocument
* doc
= new SkinDocument(parseStatus
, fileName
);
63 void EditorWindow::loadSettings()
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();
75 if(!(size
.isNull() || pos
.isNull() || state
.isNull()))
85 void EditorWindow::saveSettings()
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());
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
);
174 void EditorWindow::shiftTab(int index
)
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);
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();
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
;
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
]))
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
);
260 void EditorWindow::openProject()
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
))
276 project
= new ProjectModel(fileName
, this);
277 ui
->projectTree
->setModel(project
);
278 ui
->projectTree
->expandAll();
280 QObject::connect(ui
->projectTree
, SIGNAL(activated(QModelIndex
)),
281 project
, SLOT(activated(QModelIndex
)));
283 fileName
.chop(fileName
.length() - fileName
.lastIndexOf('/') - 1);
284 settings
.setValue("defaultDirectory", fileName
);
292 void EditorWindow::tabTitleChanged(QString title
)
294 SkinDocument
* sender
= dynamic_cast<SkinDocument
*>(QObject::sender());
295 ui
->editorTabs
->setTabText(ui
->editorTabs
->indexOf(sender
), title
);
298 void EditorWindow::showPanel()
300 if(sender() == ui
->actionFile_Panel
)
301 ui
->projectDock
->setVisible(true);
302 if(sender() == ui
->actionPreview_Panel
)
303 ui
->skinPreviewDock
->setVisible(true);
304 if(sender() == ui
->actionDisplay_Panel
)
305 ui
->parseTreeDock
->setVisible(true);
308 void EditorWindow::closeEvent(QCloseEvent
* event
)
313 /* Closing all the tabs */
314 for(int i
= 0; i
< ui
->editorTabs
->count(); i
++)
316 if(!dynamic_cast<SkinDocument
*>
317 (ui
->editorTabs
->widget(i
))->requestClose())
327 void EditorWindow::updateCurrent()
329 if(ui
->editorTabs
->currentIndex() < 0)
332 dynamic_cast<SkinDocument
*>
333 (ui
->editorTabs
->currentWidget())->genCode();
336 EditorWindow::~EditorWindow()