GUI for add library (bitmap|sound|svg) object
[dashstudio.git] / src / shell / mainwindow.cpp
blob4ef90a66b1d04e38dc8caf7932637fa56719de20
1 /***************************************************************************
2 * Copyright (C) 2007 by David Cuadrado *
3 * krawek@gmail.com *
4 * *
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. *
9 * *
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. *
14 * *
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"
23 // DLib
24 #include <dcore/config.h>
25 #include <dgui/application.h>
26 #include <dgui/iconloader.h>
27 #include <dgui/osd.h>
29 // YAMF
30 #include <yamf/common/yamf.h>
32 #include <yamf/model/project.h>
34 #include <yamf/gui/projectview.h>
36 #include <yamf/drawing/view.h>
37 #include <yamf/drawing/paintarea.h>
38 #include <yamf/drawing/brushmanager.h>
40 // DASH
42 #include "documentviewer.h"
43 #include "projectmanager.h"
44 #include "settingsdialog.h"
45 #include "newprojectdialog.h"
47 #include <components/timeline/timeline.h>
48 #include <components/library/librarywidget.h>
49 #include <components/base/manager.h>
51 #include <components/colorpalette/mixer.h>
52 #include <components/colorpalette/palettes.h>
54 // Qt
56 #include <QMessageBox>
57 #include <QMenuBar>
58 #include <QMenu>
59 #include <QWorkspace>
60 #include <QApplication>
61 #include <QFileDialog>
62 #include <QTimer>
63 #include <QSettings>
64 #include <QCloseEvent>
66 #define COMING_SOON QMessageBox::warning(this, tr("Coming soon"), tr("Not implemented yet"));
68 MainWindow::MainWindow(QWidget *parent)
69 : Ideality::StackedMainWindow(parent)
71 setWindowTitle(tr("Dash"));
73 YAMF::initialize();
75 m_actionManager = new DGui::ActionManager(this);
76 m_componentManager = new Dash::Component::Manager(this);
78 m_projectManager = new ProjectManager(this);
79 m_viewer = new DocumentViewer(m_projectManager->project(), this);
80 m_brushSynchronizer = new Component::BrushSynchronizer(this);
82 loadSettings();
84 setupFileActions();
85 setupEditActions();
87 setupMenu();
88 setupComponents();
90 m_projectManager->registerModule(m_viewer);
91 foreach(Dash::Component::Widget *module, m_componentManager->widgets())
93 m_projectManager->registerModule(module);
97 addWidget(m_viewer, Dash::Drawing);
98 addWidget(new QWorkspace(this), Dash::Animation);
101 QTimer::singleShot(100, this, SLOT(loadLastProject()));
105 MainWindow::~MainWindow()
107 delete m_actionManager;
111 void MainWindow::setupFileActions()
113 QAction *newProject = new QAction( DGui::IconLoader::self()->load("project.png"), tr("New project"), this);
114 newProject->setShortcut(QKeySequence::New);
115 connect(newProject, SIGNAL(triggered()), this, SLOT(newProject()));
117 newProject->setStatusTip(tr( "Opens a new project"));
118 m_actionManager->add( newProject, "newproject", "file" );
121 QAction *openFile = new QAction( DGui::IconLoader::self()->load("open.png"), tr( "Open project" ), this);
122 openFile->setShortcut(QKeySequence::Open);
124 connect(openFile, SIGNAL(triggered()), this, SLOT(openProject()));
126 m_actionManager->add( openFile, "openproject", "file" );
127 openFile->setStatusTip(tr("Loads an existent project"));
129 QAction *openFromServer = new QAction(DGui::IconLoader::self()->load("open.png"), tr("Open project from server..."), this);
131 connect(openFromServer, SIGNAL(triggered()), this, SLOT(openProjectFromServer()));
132 m_actionManager->add(openFromServer, "openprojectfromserver", "file");
134 QAction *exportToServer = new QAction(QIcon(), tr("Export project to server..."), this);
136 connect(exportToServer, SIGNAL(triggered()), this, SLOT(exportProjectToServer()));
137 m_actionManager->add(exportToServer, "exportprojecttoserver", "file");
139 QAction *save = new QAction( DGui::IconLoader::self()->load("save.png"), tr( "Save project..." ), this);
140 save->setShortcut(QKeySequence::Save);
142 connect(save, SIGNAL(triggered()), this, SLOT(saveProject()));
143 m_actionManager->add( save, "saveproject", "file" );
144 save->setStatusTip(tr("Saves the current project in the current location"));
146 QAction *saveAs = new QAction( tr( "Save project &As..." ), this);
147 connect(saveAs, SIGNAL(triggered()), this, SLOT(saveProjectAs()));
148 saveAs->setStatusTip(tr("Opens a dialog box to save the current project in any location"));
149 m_actionManager->add( saveAs, "saveprojectas", "file" );
151 QAction *close = new QAction(DGui::IconLoader::self()->load("close.png"), tr( "Cl&ose project" ), this);
152 close->setShortcut(QKeySequence::Close);
153 connect(close, SIGNAL(triggered()), this, SLOT(closeProject()));
154 close->setStatusTip(tr("Closes the active project"));
155 m_actionManager->add( close, "closeproject", "file" );
157 QAction *exportProject = new QAction( DGui::IconLoader::self()->load("export.png"), tr( "&Export..." ), this);
159 connect(exportProject, SIGNAL(triggered()), this, SLOT(exportProject()));
160 exportProject->setStatusTip(tr("Exports project to different formats"));
161 m_actionManager->add( exportProject, "export", "file" );
163 QAction *importBitmap = new QAction(DGui::IconLoader::self()->load("import.png"), tr( "&Import bitmap..." ), this);
164 importBitmap->setStatusTip(tr("Import a bitmap to the project"));
165 m_actionManager->add( importBitmap, "importbitmap", "file" );
167 QAction *importSound = new QAction(DGui::IconLoader::self()->load("import.png"), tr( "&Import sounds..." ), this);
168 importSound->setStatusTip(tr("Import a sound to the project"));
169 m_actionManager->add( importSound, "importsound", "file" );
172 void MainWindow::setupEditActions()
174 QAction *settings = new QAction(DGui::IconLoader::self()->load("settings.png"), tr( "Settings..." ), this);
175 connect(settings, SIGNAL(triggered()), this, SLOT(configure()));
176 // settings->setStatusTip(tr(""));
177 m_actionManager->add( settings, "settings", "edit" );
180 void MainWindow::setupMenu()
183 QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
186 QMenu *newMenu = fileMenu->addMenu(tr("New..."));
187 newMenu->addAction(m_actionManager->find("newproject"));
190 fileMenu->addAction(m_actionManager->find("openproject"));
191 fileMenu->addAction(m_actionManager->find("openprojectfromserver"));
192 fileMenu->addAction(m_actionManager->find("exportprojecttoserver"));
194 fileMenu->addSeparator();
196 fileMenu->addAction(m_actionManager->find("saveproject"));
197 fileMenu->addAction(m_actionManager->find("saveprojectas"));
199 fileMenu->addSeparator();
201 fileMenu->addAction(m_actionManager->find("closeproject"));
203 fileMenu->addSeparator();
205 fileMenu->addAction(m_actionManager->find("export"));
207 QMenu *importMenu = fileMenu->addMenu(tr("Import"));
208 importMenu->addAction(m_actionManager->find("importbitmap"));
209 importMenu->addAction(m_actionManager->find("importsound"));
211 fileMenu->addSeparator();
213 QAction *quit = fileMenu->addAction(tr("&Quit"));
214 connect(quit, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
218 QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
219 editMenu->addAction( m_actionManager->find("settings") );
223 QMenu *workspaceMenu = menuBar()->addMenu(tr("&Workspace"));
224 connect( workspaceMenu, SIGNAL(triggered( QAction * )), this, SLOT(changePerspective( QAction *)));
226 QActionGroup *workspaces = new QActionGroup(this);
227 workspaces->setExclusive(true);
229 QAction *drawingAction = new QAction( tr("Drawing"), workspaceMenu );
230 drawingAction->setShortcut(QKeySequence(Qt::Key_F9));
231 drawingAction->setCheckable(true);
232 drawingAction->setChecked(true);
233 drawingAction->setData(Dash::Drawing );
235 QAction *animationAction = new QAction( tr("Animation"), workspaceMenu );
236 animationAction->setShortcut(QKeySequence(Qt::Key_F10));
237 animationAction->setCheckable(true);
238 animationAction->setData(Dash::Animation);
240 workspaces->addAction(drawingAction);
241 workspaces->addAction(animationAction);
243 workspaceMenu->addActions(workspaces->actions());
247 QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
249 helpMenu->addAction(tr("Contents..."), this, SLOT(showHelp()))->setShortcut(QKeySequence::HelpContents);
250 helpMenu->addSeparator();
251 helpMenu->addAction(tr("About Dash..."), this, SLOT(aboutDash()));
252 helpMenu->addAction(tr("About Qt..."), qApp, SLOT(aboutQt()));
256 void MainWindow::setupComponents()
258 Dash::Component::TimeLine *tl = new Dash::Component::TimeLine(m_projectManager->project());
259 m_componentManager->add(tl);
261 connectToPaintArea(tl);
264 // m_viewer->view()->paintArea()->brushManager()->setPen(QPen(Qt::blue,3));
266 // Component::ColorPalette *colorPalette = new Component::ColorPalette(m_projectManager->project());
267 // m_componentManager->add(colorPalette);
269 Dash::Component::LibraryWidget *library = new Dash::Component::LibraryWidget(m_projectManager->project());
270 connect(m_actionManager->find("importbitmap", "file"), SIGNAL(triggered()), library, SLOT(importBitmap()));
271 connect(m_actionManager->find("importsound", "file"), SIGNAL(triggered()), library, SLOT(importBitmap()));
272 m_componentManager->add(library);
273 connectToPaintArea(library);
276 connectToPaintArea(m_brushSynchronizer);
277 addToolView(m_brushSynchronizer->mixer(), Qt::LeftDockWidgetArea, Dash::Drawing);
278 addToolView(m_brushSynchronizer->palettes(), Qt::LeftDockWidgetArea, Dash::Drawing);
280 /*********************/
281 m_projectView = new YAMF::Gui::ProjectView(m_projectManager->project());
282 addToolView(m_projectView, Qt::RightDockWidgetArea, Dash::All);
285 void MainWindow::newProject()
287 if(!closeProject()) return;
289 NewProjectDialog dialog;
291 if( dialog.exec() == QDialog::Accepted )
293 m_projectManager->createProject(dialog.projectName(), dialog.author(), dialog.description());
295 foreach(QString scene, dialog.scenes())
297 m_projectManager->createScene(scene, dialog.framesPerScene());
300 m_projectView->setProject(m_projectManager->project());
304 bool MainWindow::openProject()
306 if(!closeProject()) return false;
308 QString filePath = QFileDialog::getOpenFileName(this, QString(), QString(), tr("Dash project (*.dsh)") );
310 if( filePath.isEmpty() )
312 return false;
315 return openProject(filePath);
318 bool MainWindow::openProject(const QString &filePath)
320 if( m_projectManager->loadProject(filePath) )
322 m_currentFile = filePath;
323 m_projectView->setProject(m_projectManager->project());
325 m_viewer->view()->setCurrentFrame(0, 0, 0);
327 return true;
330 return false;
333 void MainWindow::openProjectFromServer()
335 COMING_SOON;
338 void MainWindow::exportProjectToServer()
340 COMING_SOON;
343 void MainWindow::saveProject()
345 if( m_currentFile.isEmpty() )
347 saveProjectAs();
348 return;
351 if( m_projectManager->saveProject(m_currentFile) )
353 DGui::Osd::self()->display(tr("Project %1 saved").arg(m_projectManager->project()->projectName()), DGui::Osd::Info);
355 else
357 m_currentFile = QString();
358 DGui::Osd::self()->display(tr("Cannot save the project!"), DGui::Osd::Error );
362 void MainWindow::autoSaveProject()
364 if( m_projectManager->project()->isOpen() && !m_currentFile.isEmpty() )
366 QTimer::singleShot(100, this, SLOT(saveProject()));
370 void MainWindow::saveProjectAs()
372 QString filePath = QFileDialog::getSaveFileName(this);
374 if( !filePath.isEmpty() )
376 m_currentFile = filePath;
378 if( !m_currentFile.endsWith(".dsh") )
379 m_currentFile += ".dsh";
381 QTimer::singleShot(0, this, SLOT(saveProject()));
385 bool MainWindow::closeProject()
387 if(!m_projectManager->project()->isOpen())
389 return true;
392 if ( m_projectManager->isModified() )
394 QMessageBox mb(QApplication::applicationName (), tr("Do you want to save?"),
395 QMessageBox::Information,
396 QMessageBox::Yes | QMessageBox::Default,
397 QMessageBox::No,
398 QMessageBox::Cancel | QMessageBox::Escape);
399 mb.setButtonText(QMessageBox::Yes, tr("Save"));
400 mb.setButtonText(QMessageBox::No, tr("Discard"));
402 switch(mb.exec())
404 case QMessageBox::Yes:
406 saveProject();
408 break;
409 case QMessageBox::No:
412 break;
413 case QMessageBox::Cancel:
415 return false;
417 break;
421 setUpdatesEnabled(false);
423 m_projectManager->closeProject();
424 m_projectView->reload();
425 m_currentFile = QString();
427 setUpdatesEnabled(true);
429 return true;
432 void MainWindow::exportProject()
434 COMING_SOON;
437 void MainWindow::loadLastProject()
439 DCore::Config *config = dApp->config("General");
440 QString lastProject = config->value("last_project", QString()).toString();
441 config->endGroup();
443 if( !lastProject.isEmpty() )
445 openProject(lastProject);
449 // EDIT
451 void MainWindow::configure()
453 SettingsDialog settings;
454 if( settings.exec() != QDialog::Rejected )
456 loadSettings();
460 // HELP
462 void MainWindow::showHelp()
464 COMING_SOON;
467 void MainWindow::aboutDash()
469 QMessageBox::about(this, tr("About Dash..."), tr("Dash is a tool to build up animations and ...") );
474 void MainWindow::changePerspective( QAction *action )
476 bool ok = false;
478 int id = action->data().toInt(&ok);
480 if(ok)
482 setCurrentPerspective(id);
486 void MainWindow::connectToPaintArea(QObject *object)
488 if( object->metaObject()->indexOfSignal("frameSelected(int,int,int)") > -1 )
490 connect(object, SIGNAL(frameSelected(int, int, int)), m_viewer->view(), SLOT(setCurrentFrame(int, int, int)));
493 if( object->metaObject()->indexOfSignal("brushChanged(QBrush)") > -1 )
495 connect(object, SIGNAL(brushChanged(const QBrush &)), m_viewer->view(), SLOT(setBrush(const QBrush &)));
498 if( object->metaObject()->indexOfSignal("addCurrentItemsToLibrary()") > -1 )
500 connect(object, SIGNAL(addCurrentItemsToLibrary()), m_viewer->view(), SLOT(addToLibrary()));
504 void MainWindow::saveSettings()
506 DCore::Config *config = dApp->config("General");
507 config->setValue("last_project", m_currentFile);
508 config->endGroup();
511 void MainWindow::loadSettings()
513 // Section General
514 DCore::Config *config = dApp->config("General");
516 m_saver.setInterval(config->value("save_project_interval", 15).toInt() * 60 * 1000);
518 if(config->value("auto_save_project", true).toBool() )
520 m_saver.start();
522 else
524 m_saver.stop();
527 config->endGroup();
531 void MainWindow::closeEvent(QCloseEvent *event)
533 saveSettings();
535 if (! closeProject() )
537 event->ignore();
538 return;
541 Ideality::StackedMainWindow::closeEvent(event);