1 /***************************************************************************
2 * Copyright (C) 2007 by David Cuadrado *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "mainwindow.h"
24 #include <dcore/config.h>
25 #include <dgui/application.h>
26 #include <dgui/iconloader.h>
30 #include <yamf/common/yamf.h>
32 #include <yamf/model/project.h>
33 #include <yamf/model/library.h>
34 #include <yamf/model/libraryobject.h>
36 #include <yamf/gui/projectview.h>
38 #include <yamf/drawing/view.h>
39 #include <yamf/drawing/paintarea.h>
40 #include <yamf/drawing/brushmanager.h>
44 #include "documentviewer.h"
45 #include "projectmanager.h"
46 #include "settingsdialog.h"
47 #include "newprojectdialog.h"
48 #include "exportdialog.h"
50 #include <components/timeline/timeline.h>
51 #include <components/library/librarywidget.h>
52 #include <components/base/manager.h>
54 #include <components/colorpalette/mixer.h>
55 #include <components/colorpalette/palettes.h>
59 #include <QMessageBox>
63 #include <QApplication>
64 #include <QFileDialog>
67 #include <QCloseEvent>
69 #define COMING_SOON QMessageBox::warning(this, tr("Coming soon"), tr("Not implemented yet"));
71 MainWindow::MainWindow(QWidget
*parent
)
72 : Ideality::StackedMainWindow(parent
)
74 setWindowTitle(tr("Dash"));
78 m_actionManager
= new DGui::ActionManager(this);
79 m_componentManager
= new Dash::Component::Manager(this);
81 m_projectManager
= new ProjectManager(this);
82 m_viewer
= new DocumentViewer(m_projectManager
->project(), this);
83 m_brushSynchronizer
= new Component::BrushSynchronizer(this);
93 m_projectManager
->registerModule(m_viewer
);
94 foreach(Dash::Component::Widget
*module
, m_componentManager
->widgets())
96 m_projectManager
->registerModule(module
);
100 addWidget(m_viewer
, Dash::Drawing
);
101 addWidget(new QWorkspace(this), Dash::Animation
);
104 QTimer::singleShot(100, this, SLOT(loadLastProject()));
108 MainWindow::~MainWindow()
110 delete m_actionManager
;
114 void MainWindow::setupFileActions()
116 QAction
*newProject
= new QAction( DGui::IconLoader::self()->load("project.png"), tr("New project"), this);
117 newProject
->setShortcut(QKeySequence::New
);
118 connect(newProject
, SIGNAL(triggered()), this, SLOT(newProject()));
120 newProject
->setStatusTip(tr( "Opens a new project"));
121 m_actionManager
->add( newProject
, "newproject", "file" );
124 QAction
*openFile
= new QAction( DGui::IconLoader::self()->load("open.png"), tr( "Open project" ), this);
125 openFile
->setShortcut(QKeySequence::Open
);
127 connect(openFile
, SIGNAL(triggered()), this, SLOT(openProject()));
129 m_actionManager
->add( openFile
, "openproject", "file" );
130 openFile
->setStatusTip(tr("Loads an existent project"));
132 QAction
*openFromServer
= new QAction(DGui::IconLoader::self()->load("open.png"), tr("Open project from server..."), this);
134 connect(openFromServer
, SIGNAL(triggered()), this, SLOT(openProjectFromServer()));
135 m_actionManager
->add(openFromServer
, "openprojectfromserver", "file");
137 QAction
*exportToServer
= new QAction(QIcon(), tr("Export project to server..."), this);
139 connect(exportToServer
, SIGNAL(triggered()), this, SLOT(exportProjectToServer()));
140 m_actionManager
->add(exportToServer
, "exportprojecttoserver", "file");
142 QAction
*save
= new QAction( DGui::IconLoader::self()->load("save.png"), tr( "Save project..." ), this);
143 save
->setShortcut(QKeySequence::Save
);
145 connect(save
, SIGNAL(triggered()), this, SLOT(saveProject()));
146 m_actionManager
->add( save
, "saveproject", "file" );
147 save
->setStatusTip(tr("Saves the current project in the current location"));
149 QAction
*saveAs
= new QAction( tr( "Save project &As..." ), this);
150 connect(saveAs
, SIGNAL(triggered()), this, SLOT(saveProjectAs()));
151 saveAs
->setStatusTip(tr("Opens a dialog box to save the current project in any location"));
152 m_actionManager
->add( saveAs
, "saveprojectas", "file" );
154 QAction
*close
= new QAction(DGui::IconLoader::self()->load("close.png"), tr( "Cl&ose project" ), this);
155 close
->setShortcut(QKeySequence::Close
);
156 connect(close
, SIGNAL(triggered()), this, SLOT(closeProject()));
157 close
->setStatusTip(tr("Closes the active project"));
158 m_actionManager
->add( close
, "closeproject", "file" );
160 QAction
*exportProject
= new QAction( DGui::IconLoader::self()->load("export.png"), tr( "&Export..." ), this);
162 connect(exportProject
, SIGNAL(triggered()), this, SLOT(exportProject()));
163 exportProject
->setStatusTip(tr("Exports project to different formats"));
164 m_actionManager
->add( exportProject
, "export", "file" );
166 QAction
*importBitmap
= new QAction(DGui::IconLoader::self()->load("import.png"), tr( "&Import bitmap..." ), this);
167 importBitmap
->setStatusTip(tr("Import a bitmap to the project"));
168 m_actionManager
->add( importBitmap
, "importbitmap", "file" );
170 QAction
*importSound
= new QAction(DGui::IconLoader::self()->load("import.png"), tr( "&Import sounds..." ), this);
171 importSound
->setStatusTip(tr("Import a sound to the project"));
172 m_actionManager
->add( importSound
, "importsound", "file" );
174 QAction
*importSvg
= new QAction(DGui::IconLoader::self()->load("import.png"), tr( "&Import svg..." ), this);
175 importSvg
->setStatusTip(tr("Import a SVG to the project"));
176 m_actionManager
->add( importSvg
, "importsvg", "file" );
179 void MainWindow::setupEditActions()
181 QAction
*settings
= new QAction(DGui::IconLoader::self()->load("settings.png"), tr( "Settings..." ), this);
182 connect(settings
, SIGNAL(triggered()), this, SLOT(configure()));
183 // settings->setStatusTip(tr(""));
184 m_actionManager
->add( settings
, "settings", "edit" );
187 void MainWindow::setupMenu()
190 QMenu
*fileMenu
= menuBar()->addMenu(tr("&File"));
193 QMenu
*newMenu
= fileMenu
->addMenu(tr("New..."));
194 newMenu
->addAction(m_actionManager
->find("newproject"));
197 fileMenu
->addAction(m_actionManager
->find("openproject"));
198 fileMenu
->addAction(m_actionManager
->find("openprojectfromserver"));
199 fileMenu
->addAction(m_actionManager
->find("exportprojecttoserver"));
201 fileMenu
->addSeparator();
203 fileMenu
->addAction(m_actionManager
->find("saveproject"));
204 fileMenu
->addAction(m_actionManager
->find("saveprojectas"));
206 fileMenu
->addSeparator();
208 fileMenu
->addAction(m_actionManager
->find("closeproject"));
210 fileMenu
->addSeparator();
212 fileMenu
->addAction(m_actionManager
->find("export"));
214 QMenu
*importMenu
= fileMenu
->addMenu(tr("Import"));
215 importMenu
->addAction(m_actionManager
->find("importbitmap"));
216 importMenu
->addAction(m_actionManager
->find("importsvg"));
217 importMenu
->addAction(m_actionManager
->find("importsound"));
219 fileMenu
->addSeparator();
221 QAction
*quit
= fileMenu
->addAction(tr("&Quit"));
222 connect(quit
, SIGNAL(triggered()), qApp
, SLOT(closeAllWindows()));
226 QMenu
*editMenu
= menuBar()->addMenu(tr("&Edit"));
227 editMenu
->addAction( m_actionManager
->find("settings") );
231 QMenu
*workspaceMenu
= menuBar()->addMenu(tr("&Workspace"));
232 connect( workspaceMenu
, SIGNAL(triggered( QAction
* )), this, SLOT(changePerspective( QAction
*)));
234 QActionGroup
*workspaces
= new QActionGroup(this);
235 workspaces
->setExclusive(true);
237 QAction
*drawingAction
= new QAction( tr("Drawing"), workspaceMenu
);
238 drawingAction
->setShortcut(QKeySequence(Qt::Key_F9
));
239 drawingAction
->setCheckable(true);
240 drawingAction
->setChecked(true);
241 drawingAction
->setData(Dash::Drawing
);
243 QAction
*animationAction
= new QAction( tr("Animation"), workspaceMenu
);
244 animationAction
->setShortcut(QKeySequence(Qt::Key_F10
));
245 animationAction
->setCheckable(true);
246 animationAction
->setData(Dash::Animation
);
248 workspaces
->addAction(drawingAction
);
249 workspaces
->addAction(animationAction
);
251 workspaceMenu
->addActions(workspaces
->actions());
255 QMenu
*helpMenu
= menuBar()->addMenu(tr("&Help"));
257 helpMenu
->addAction(tr("Contents..."), this, SLOT(showHelp()))->setShortcut(QKeySequence::HelpContents
);
258 helpMenu
->addSeparator();
259 helpMenu
->addAction(tr("About Dash..."), this, SLOT(aboutDash()));
260 helpMenu
->addAction(tr("About Qt..."), qApp
, SLOT(aboutQt()));
264 void MainWindow::setupComponents()
266 Dash::Component::TimeLine
*tl
= new Dash::Component::TimeLine(m_projectManager
->project());
267 m_componentManager
->add(tl
);
269 connectToPaintArea(tl
);
272 // m_viewer->view()->paintArea()->brushManager()->setPen(QPen(Qt::blue,3));
274 // Component::ColorPalette *colorPalette = new Component::ColorPalette(m_projectManager->project());
275 // m_componentManager->add(colorPalette);
277 Dash::Component::LibraryWidget
*library
= new Dash::Component::LibraryWidget(m_projectManager
->project());
278 connect(m_actionManager
->find("importbitmap", "file"), SIGNAL(triggered()), library
, SLOT(importBitmap()));
279 connect(m_actionManager
->find("importsvg", "file"), SIGNAL(triggered()), library
, SLOT(importSvg()));
280 connect(m_actionManager
->find("importsound", "file"), SIGNAL(triggered()), library
, SLOT(importSound()));
281 connect(library
, SIGNAL(addSymbolToProject(const QString
&)), m_viewer
->view(), SLOT(addSymbol(const QString
&)));
283 m_componentManager
->add(library
);
284 connectToPaintArea(library
);
287 connectToPaintArea(m_brushSynchronizer
);
288 addToolView(m_brushSynchronizer
->mixer(), Qt::LeftDockWidgetArea
, Dash::Drawing
);
289 addToolView(m_brushSynchronizer
->palettes(), Qt::LeftDockWidgetArea
, Dash::Drawing
);
291 /*********************/
292 m_projectView
= new YAMF::Gui::ProjectView(m_projectManager
->project());
293 addToolView(m_projectView
, Qt::RightDockWidgetArea
, Dash::All
);
296 void MainWindow::newProject()
298 if(!closeProject()) return;
300 NewProjectDialog dialog
;
302 if( dialog
.exec() == QDialog::Accepted
)
304 m_projectManager
->createProject(dialog
.projectName(), dialog
.author(), dialog
.description());
306 foreach(QString scene
, dialog
.scenes())
308 m_projectManager
->createScene(scene
, dialog
.framesPerScene());
311 m_projectView
->setProject(m_projectManager
->project());
315 bool MainWindow::openProject()
317 if(!closeProject()) return false;
319 QString filePath
= QFileDialog::getOpenFileName(this, QString(), QString(), tr("Dash project (*.dsh)") );
321 if( filePath
.isEmpty() )
326 return openProject(filePath
);
329 bool MainWindow::openProject(const QString
&filePath
)
331 if( m_projectManager
->loadProject(filePath
) )
333 m_currentFile
= filePath
;
334 m_projectView
->setProject(m_projectManager
->project());
336 m_viewer
->view()->setCurrentFrame(0, 0, 0);
344 void MainWindow::openProjectFromServer()
349 void MainWindow::exportProjectToServer()
354 void MainWindow::saveProject()
356 if( m_currentFile
.isEmpty() )
362 if( m_projectManager
->saveProject(m_currentFile
) )
364 DGui::Osd::self()->display(tr("Project %1 saved").arg(m_projectManager
->project()->projectName()), DGui::Osd::Info
);
368 m_currentFile
= QString();
369 DGui::Osd::self()->display(tr("Cannot save the project!"), DGui::Osd::Error
);
373 void MainWindow::autoSaveProject()
375 if( m_projectManager
->project()->isOpen() && !m_currentFile
.isEmpty() )
377 QTimer::singleShot(100, this, SLOT(saveProject()));
381 void MainWindow::saveProjectAs()
383 QString filePath
= QFileDialog::getSaveFileName(this);
385 if( !filePath
.isEmpty() )
387 m_currentFile
= filePath
;
389 if( !m_currentFile
.endsWith(".dsh") )
390 m_currentFile
+= ".dsh";
392 QTimer::singleShot(0, this, SLOT(saveProject()));
396 bool MainWindow::closeProject()
398 if(!m_projectManager
->project()->isOpen())
403 if ( m_projectManager
->isModified() )
405 QMessageBox
mb(QApplication::applicationName (), tr("Do you want to save?"),
406 QMessageBox::Information
,
407 QMessageBox::Yes
| QMessageBox::Default
,
409 QMessageBox::Cancel
| QMessageBox::Escape
);
410 mb
.setButtonText(QMessageBox::Yes
, tr("Save"));
411 mb
.setButtonText(QMessageBox::No
, tr("Discard"));
415 case QMessageBox::Yes
:
420 case QMessageBox::No
:
424 case QMessageBox::Cancel
:
432 setUpdatesEnabled(false);
434 m_projectManager
->closeProject();
435 m_projectView
->reload();
436 m_currentFile
= QString();
438 setUpdatesEnabled(true);
443 void MainWindow::exportProject()
445 ExportDialog
exportDialog(m_projectManager
->project());
446 if( exportDialog
.exec() == QDialog::Accepted
)
448 if( exportDialog
.generate() )
450 DGui::Osd::self()->display(tr("Export successfully!"));
454 DGui::Osd::self()->display(tr("Export failed!"), DGui::Osd::Error
);
459 void MainWindow::loadLastProject()
461 DCore::Config
*config
= dApp
->config("General");
462 QString lastProject
= config
->value("last_project", QString()).toString();
465 if( !lastProject
.isEmpty() )
467 openProject(lastProject
);
473 void MainWindow::configure()
475 SettingsDialog settings
;
476 if( settings
.exec() != QDialog::Rejected
)
484 void MainWindow::showHelp()
489 void MainWindow::aboutDash()
491 QMessageBox::about(this, tr("About Dash..."), tr("Dash is a tool to build up animations and ...") );
496 void MainWindow::changePerspective( QAction
*action
)
500 int id
= action
->data().toInt(&ok
);
504 setCurrentPerspective(id
);
508 void MainWindow::connectToPaintArea(QObject
*object
)
510 if( object
->metaObject()->indexOfSignal("frameSelected(int,int,int)") > -1 )
512 connect(object
, SIGNAL(frameSelected(int, int, int)), m_viewer
->view(), SLOT(setCurrentFrame(int, int, int)));
515 if( object
->metaObject()->indexOfSignal("brushChanged(QBrush)") > -1 )
517 connect(object
, SIGNAL(brushChanged(const QBrush
&)), m_viewer
->view(), SLOT(setBrush(const QBrush
&)));
520 if( object
->metaObject()->indexOfSignal("addCurrentItemsToLibrary()") > -1 )
522 connect(object
, SIGNAL(addCurrentItemsToLibrary()), m_viewer
->view(), SLOT(addToLibrary()));
526 void MainWindow::saveSettings()
528 DCore::Config
*config
= dApp
->config("General");
529 config
->setValue("last_project", m_currentFile
);
533 void MainWindow::loadSettings()
536 DCore::Config
*config
= dApp
->config("General");
538 m_saver
.setInterval(config
->value("save_project_interval", 15).toInt() * 60 * 1000);
540 if(config
->value("auto_save_project", true).toBool() )
553 void MainWindow::closeEvent(QCloseEvent
*event
)
557 if (! closeProject() )
563 Ideality::StackedMainWindow::closeEvent(event
);