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 "ui_editorwindow.h"
25 #include <QDesktopWidget>
26 #include <QFileSystemModel>
28 #include <QFileDialog>
30 EditorWindow::EditorWindow(QWidget
*parent
) :
32 ui(new Ui::EditorWindow
)
40 void EditorWindow::loadSettings()
45 /* Main Window location */
46 settings
.beginGroup("MainWindow");
47 QSize size
= settings
.value("size").toSize();
48 QPoint pos
= settings
.value("position").toPoint();
49 QByteArray state
= settings
.value("state").toByteArray();
52 if(!(size
.isNull() || pos
.isNull() || state
.isNull()))
62 void EditorWindow::saveSettings()
67 /* Saving window and panel positions */
68 settings
.beginGroup("MainWindow");
69 settings
.setValue("position", pos());
70 settings
.setValue("size", size());
71 settings
.setValue("state", saveState());
75 void EditorWindow::setupUI()
77 /* Displaying some files to test the file tree view */
78 QFileSystemModel
* model
= new QFileSystemModel
;
79 model
->setRootPath(QDir::currentPath());
80 ui
->fileTree
->setModel(model
);
82 /* Connecting the tab bar signals */
83 QObject::connect(ui
->editorTabs
, SIGNAL(currentChanged(int)),
84 this, SLOT(shiftTab(int)));
85 QObject::connect(ui
->editorTabs
, SIGNAL(tabCloseRequested(int)),
86 this, SLOT(closeTab(int)));
88 /* Connecting the code gen button */
89 QObject::connect(ui
->fromTree
, SIGNAL(pressed()),
90 this, SLOT(updateCurrent()));
94 void EditorWindow::setupMenus()
96 /* Connecting panel show actions */
97 QObject::connect(ui
->actionFile_Panel
, SIGNAL(triggered()),
98 this, SLOT(showPanel()));
99 QObject::connect(ui
->actionDisplay_Panel
, SIGNAL(triggered()),
100 this, SLOT(showPanel()));
101 QObject::connect(ui
->actionPreview_Panel
, SIGNAL(triggered()),
102 this, SLOT(showPanel()));
104 /* Connecting the document management actions */
105 QObject::connect(ui
->actionNew_Document
, SIGNAL(triggered()),
106 this, SLOT(newTab()));
107 QObject::connect(ui
->actionToolbarNew
, SIGNAL(triggered()),
108 this, SLOT(newTab()));
110 QObject::connect(ui
->actionClose_Document
, SIGNAL(triggered()),
111 this, SLOT(closeCurrent()));
113 QObject::connect(ui
->actionSave_Document
, SIGNAL(triggered()),
114 this, SLOT(saveCurrent()));
115 QObject::connect(ui
->actionSave_Document_As
, SIGNAL(triggered()),
116 this, SLOT(saveCurrentAs()));
117 QObject::connect(ui
->actionToolbarSave
, SIGNAL(triggered()),
118 this, SLOT(saveCurrent()));
123 void EditorWindow::newTab()
125 SkinDocument
* doc
= new SkinDocument
;
126 ui
->editorTabs
->addTab(doc
, doc
->getTitle());
128 /* Connecting to title change events */
129 QObject::connect(doc
, SIGNAL(titleChanged(QString
)),
130 this, SLOT(tabTitleChanged(QString
)));
133 void EditorWindow::shiftTab(int index
)
137 ui
->parseTree
->setModel(0);
138 ui
->actionSave_Document
->setEnabled(false);
139 ui
->actionSave_Document_As
->setEnabled(false);
140 ui
->actionClose_Document
->setEnabled(false);
141 ui
->actionToolbarSave
->setEnabled(false);
142 ui
->fromTree
->setEnabled(false);
146 ui
->parseTree
->setModel(dynamic_cast<SkinDocument
*>
147 (ui
->editorTabs
->currentWidget())->getModel());
148 ui
->actionSave_Document
->setEnabled(true);
149 ui
->actionSave_Document_As
->setEnabled(true);
150 ui
->actionClose_Document
->setEnabled(true);
151 ui
->actionToolbarSave
->setEnabled(true);
152 ui
->fromTree
->setEnabled(true);
156 bool EditorWindow::closeTab(int index
)
158 SkinDocument
* widget
= dynamic_cast<SkinDocument
*>
159 (ui
->editorTabs
->widget(index
));
160 if(widget
->requestClose())
162 ui
->editorTabs
->removeTab(index
);
163 widget
->deleteLater();
170 void EditorWindow::closeCurrent()
172 closeTab(ui
->editorTabs
->currentIndex());
175 void EditorWindow::saveCurrent()
177 if(ui
->editorTabs
->currentIndex() >= 0)
178 dynamic_cast<SkinDocument
*>(ui
->editorTabs
->currentWidget())->save();
181 void EditorWindow::saveCurrentAs()
183 if(ui
->editorTabs
->currentIndex() >= 0)
184 dynamic_cast<SkinDocument
*>(ui
->editorTabs
->currentWidget())->saveAs();
187 void EditorWindow::openFile()
189 QStringList fileNames
;
192 settings
.beginGroup("SkinDocument");
193 QString directory
= settings
.value("defaultDirectory", "").toString();
194 fileNames
= QFileDialog::getOpenFileNames(this, tr("Open Files"), directory
,
195 SkinDocument::fileFilter());
199 void EditorWindow::tabTitleChanged(QString title
)
201 SkinDocument
* sender
= dynamic_cast<SkinDocument
*>(QObject::sender());
202 ui
->editorTabs
->setTabText(ui
->editorTabs
->indexOf(sender
), title
);
205 void EditorWindow::showPanel()
207 if(sender() == ui
->actionFile_Panel
)
208 ui
->fileDock
->setVisible(true);
209 if(sender() == ui
->actionPreview_Panel
)
210 ui
->skinPreviewDock
->setVisible(true);
211 if(sender() == ui
->actionDisplay_Panel
)
212 ui
->parseTreeDock
->setVisible(true);
215 void EditorWindow::closeEvent(QCloseEvent
* event
)
220 /* Closing all the tabs */
221 for(int i
= 0; i
< ui
->editorTabs
->count(); i
++)
223 if(!dynamic_cast<SkinDocument
*>
224 (ui
->editorTabs
->widget(i
))->requestClose())
234 void EditorWindow::updateCurrent()
236 if(ui
->editorTabs
->currentIndex() < 0)
239 dynamic_cast<SkinDocument
*>
240 (ui
->editorTabs
->currentWidget())->genCode();
243 EditorWindow::~EditorWindow()