Camera
[dashstudio.git] / src / shell / mainwindow.cpp
blob61fb0bfc628853be6d8c3fa475bc2ef06d45530a
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>
28 #include <dgui/tipdialog.h>
30 // YAMF
31 #include <yamf/common/yamf.h>
33 #include <yamf/model/project.h>
34 #include <yamf/model/library.h>
35 #include <yamf/model/libraryobject.h>
37 #include <yamf/gui/projectview.h>
39 #include <yamf/drawing/view.h>
40 #include <yamf/drawing/paintarea.h>
41 #include <yamf/drawing/brushmanager.h>
43 // DASH
45 #include "documentviewer.h"
46 #include "projectmanager.h"
47 #include "settingsdialog.h"
48 #include "newprojectdialog.h"
49 #include "exportdialog.h"
51 #include <components/timeline/timeline.h>
52 #include <components/projectbrowser/projectbrowser.h>
53 #include <components/library/librarywidget.h>
54 #include <components/base/manager.h>
56 #include <components/colorpalette/mixer.h>
57 #include <components/colorpalette/palettes.h>
59 #include <components/camera/camerawidget.h>
61 // Qt
63 #include <QMessageBox>
64 #include <QMenuBar>
65 #include <QMenu>
66 #include <QMdiArea>
67 #include <QApplication>
68 #include <QFileDialog>
69 #include <QTimer>
70 #include <QSettings>
71 #include <QCloseEvent>
73 #define COMING_SOON QMessageBox::warning(this, tr("Coming soon"), tr("Not implemented yet"));
75 MainWindow::MainWindow(QWidget *parent)
76 : Ideality::StackedMainWindow(parent)
78 setWindowTitle(tr("Dash"));
80 YAMF::initialize();
82 m_actionManager = new DGui::ActionManager(this);
83 m_componentManager = new Dash::Component::Manager(this);
85 m_projectManager = new ProjectManager(this);
86 m_viewer = new DocumentViewer(m_projectManager->project(), this);
87 m_brushSynchronizer = new Component::BrushSynchronizer(this);
89 loadSettings();
91 setupFileActions();
92 setupViewActions();
93 setupEditActions();
95 setupMenu();
96 setupComponents();
98 m_projectManager->registerModule(m_viewer);
99 foreach(Dash::Component::Widget *module, m_componentManager->widgets())
101 m_projectManager->registerModule(module);
105 addWidget(m_viewer, Dash::Drawing);
107 QMdiArea *ws = new QMdiArea(this);
108 Dash::Component::CameraWidget *camera = new Dash::Component::CameraWidget(m_projectManager->project());
110 m_projectManager->registerModule(camera);
111 ws->addSubWindow(camera, Qt::CustomizeWindowHint | Qt::WindowMinMaxButtonsHint);
113 addWidget(ws, Dash::Animation);
116 QTimer::singleShot(100, this, SLOT(loadLastProject()));
120 MainWindow::~MainWindow()
122 delete m_actionManager;
126 void MainWindow::setupFileActions()
128 QAction *newProject = new QAction( DGui::IconLoader::self()->load("project.png"), tr("New project"), this);
129 newProject->setShortcut(QKeySequence::New);
130 connect(newProject, SIGNAL(triggered()), this, SLOT(newProject()));
132 newProject->setStatusTip(tr( "Opens a new project"));
133 m_actionManager->add( newProject, "newproject", "file" );
136 QAction *openFile = new QAction( DGui::IconLoader::self()->load("open.png"), tr( "Open project" ), this);
137 openFile->setShortcut(QKeySequence::Open);
139 connect(openFile, SIGNAL(triggered()), this, SLOT(openProject()));
141 m_actionManager->add( openFile, "openproject", "file" );
142 openFile->setStatusTip(tr("Loads an existent project"));
144 QAction *openFromServer = new QAction(DGui::IconLoader::self()->load("open.png"), tr("Open collaborative project..."), this);
146 connect(openFromServer, SIGNAL(triggered()), this, SLOT(openCollaborativeProject()));
147 m_actionManager->add(openFromServer, "opencollaborativeproject", "file");
149 QAction *exportToServer = new QAction(QIcon(), tr("Export project to server..."), this);
151 connect(exportToServer, SIGNAL(triggered()), this, SLOT(exportProjectToServer()));
152 m_actionManager->add(exportToServer, "exportprojecttoserver", "file");
154 QAction *save = new QAction( DGui::IconLoader::self()->load("save.png"), tr( "Save project..." ), this);
155 save->setShortcut(QKeySequence::Save);
157 connect(save, SIGNAL(triggered()), this, SLOT(saveProject()));
158 m_actionManager->add( save, "saveproject", "file" );
159 save->setStatusTip(tr("Saves the current project in the current location"));
161 QAction *saveAs = new QAction( tr( "Save project &As..." ), this);
162 connect(saveAs, SIGNAL(triggered()), this, SLOT(saveProjectAs()));
163 saveAs->setStatusTip(tr("Opens a dialog box to save the current project in any location"));
164 m_actionManager->add( saveAs, "saveprojectas", "file" );
166 QAction *close = new QAction(DGui::IconLoader::self()->load("close.png"), tr( "Cl&ose project" ), this);
167 close->setShortcut(QKeySequence::Close);
168 connect(close, SIGNAL(triggered()), this, SLOT(closeProject()));
169 close->setStatusTip(tr("Closes the active project"));
170 m_actionManager->add( close, "closeproject", "file" );
172 QAction *exportProject = new QAction( DGui::IconLoader::self()->load("export.png"), tr( "&Export..." ), this);
174 connect(exportProject, SIGNAL(triggered()), this, SLOT(exportProject()));
175 exportProject->setStatusTip(tr("Exports project to different formats"));
176 m_actionManager->add( exportProject, "export", "file" );
178 QAction *importBitmap = new QAction(DGui::IconLoader::self()->load("import.png"), tr( "&Import bitmap..." ), this);
179 importBitmap->setStatusTip(tr("Import a bitmap to the project"));
180 m_actionManager->add( importBitmap, "importbitmap", "file" );
182 QAction *importSound = new QAction(DGui::IconLoader::self()->load("import.png"), tr( "&Import sounds..." ), this);
183 importSound->setStatusTip(tr("Import a sound to the project"));
184 m_actionManager->add( importSound, "importsound", "file" );
186 QAction *importSvg = new QAction(DGui::IconLoader::self()->load("import.png"), tr( "&Import svg..." ), this);
187 importSvg->setStatusTip(tr("Import a SVG to the project"));
188 m_actionManager->add( importSvg, "importsvg", "file" );
191 void MainWindow::setupViewActions()
193 QAction *showChatWindow = new QAction(QIcon(), tr( "Show chat window" ), this);
194 showChatWindow->setVisible(false);
196 connect(showChatWindow, SIGNAL(triggered()), this, SLOT(showChatWindow()));
197 // settings->setStatusTip(tr(""));
198 m_actionManager->add( showChatWindow, "showchatwindow", "view" );
201 void MainWindow::setupEditActions()
203 QAction *settings = new QAction(DGui::IconLoader::self()->load("settings.png"), tr( "Settings..." ), this);
204 connect(settings, SIGNAL(triggered()), this, SLOT(configure()));
205 // settings->setStatusTip(tr(""));
206 m_actionManager->add( settings, "settings", "edit" );
209 void MainWindow::setupMenu()
212 QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
215 QMenu *newMenu = fileMenu->addMenu(tr("New..."));
216 newMenu->addAction(m_actionManager->find("newproject"));
219 fileMenu->addAction(m_actionManager->find("openproject"));
221 QMenu *recent = fileMenu->addMenu(tr("Open recent"));
222 foreach(QString filePath, m_recentProjects)
224 recent->addAction(filePath);
226 connect(recent, SIGNAL(triggered( QAction* )), this, SLOT(openRecent(QAction *)));
228 fileMenu->addAction(m_actionManager->find("opencollaborativeproject"));
229 fileMenu->addAction(m_actionManager->find("exportprojecttoserver"));
231 fileMenu->addSeparator();
233 fileMenu->addAction(m_actionManager->find("saveproject"));
234 fileMenu->addAction(m_actionManager->find("saveprojectas"));
236 fileMenu->addSeparator();
238 fileMenu->addAction(m_actionManager->find("closeproject"));
240 fileMenu->addSeparator();
242 fileMenu->addAction(m_actionManager->find("export"));
244 QMenu *importMenu = fileMenu->addMenu(tr("Import"));
245 importMenu->addAction(m_actionManager->find("importbitmap"));
246 importMenu->addAction(m_actionManager->find("importsvg"));
247 importMenu->addAction(m_actionManager->find("importsound"));
249 fileMenu->addSeparator();
251 QAction *quit = fileMenu->addAction(tr("&Quit"));
252 connect(quit, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
256 QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
257 viewMenu->addAction( m_actionManager->find("showchatwindow") );
258 m_actionManager->find("showchatwindow")->setVisible(false);
262 QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
263 editMenu->addAction( m_actionManager->find("settings") );
267 QMenu *workspaceMenu = menuBar()->addMenu(tr("&Workspace"));
268 connect( workspaceMenu, SIGNAL(triggered( QAction * )), this, SLOT(changePerspective( QAction *)));
270 QActionGroup *workspaces = new QActionGroup(this);
271 workspaces->setExclusive(true);
273 QAction *drawingAction = new QAction( tr("Drawing"), workspaceMenu );
274 drawingAction->setShortcut(QKeySequence(Qt::Key_F9));
275 drawingAction->setCheckable(true);
276 drawingAction->setChecked(true);
277 drawingAction->setData(Dash::Drawing );
279 QAction *animationAction = new QAction( tr("Animation"), workspaceMenu );
280 animationAction->setShortcut(QKeySequence(Qt::Key_F10));
281 animationAction->setCheckable(true);
282 animationAction->setData(Dash::Animation);
284 workspaces->addAction(drawingAction);
285 workspaces->addAction(animationAction);
287 workspaceMenu->addActions(workspaces->actions());
291 QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
293 helpMenu->addAction(tr("Contents..."), this, SLOT(showHelp()))->setShortcut(QKeySequence::HelpContents);
294 helpMenu->addSeparator();
295 helpMenu->addAction(tr("About Dash..."), this, SLOT(aboutDash()));
296 helpMenu->addAction(tr("About Qt..."), qApp, SLOT(aboutQt()));
300 void MainWindow::setupComponents()
302 Dash::Component::TimeLine *tl = new Dash::Component::TimeLine(m_projectManager->project());
303 m_componentManager->add(tl);
305 connectToPaintArea(tl);
308 // m_viewer->view()->paintArea()->brushManager()->setPen(QPen(Qt::blue,3));
310 // Component::ColorPalette *colorPalette = new Component::ColorPalette(m_projectManager->project());
311 // m_componentManager->add(colorPalette);
313 Dash::Component::LibraryWidget *library = new Dash::Component::LibraryWidget(m_projectManager->project());
314 connect(m_actionManager->find("importbitmap", "file"), SIGNAL(triggered()), library, SLOT(importBitmap()));
315 connect(m_actionManager->find("importsvg", "file"), SIGNAL(triggered()), library, SLOT(importSvg()));
316 connect(m_actionManager->find("importsound", "file"), SIGNAL(triggered()), library, SLOT(importSound()));
317 connect(library, SIGNAL(addSymbolToProject(const QString&)), m_viewer->view(), SLOT(addSymbol(const QString&)));
319 m_componentManager->add(library);
320 connectToPaintArea(library);
323 connectToPaintArea(m_brushSynchronizer);
324 addToolView(m_brushSynchronizer->mixer(), Qt::LeftDockWidgetArea, Dash::Drawing);
325 addToolView(m_brushSynchronizer->palettes(), Qt::LeftDockWidgetArea, Dash::Drawing);
327 /*********************/
328 Dash::Component::ProjectBrowser *pb = new Dash::Component::ProjectBrowser(m_projectManager->project(), m_viewer->view()->paintArea()->scene());
330 m_componentManager->add(pb);
332 // m_projectView = new YAMF::Gui::ProjectView(m_projectManager->project());
333 // addToolView(m_projectView, Qt::RightDockWidgetArea, Dash::All);
336 void MainWindow::newProject()
338 if(!closeProject()) return;
340 NewProjectDialog dialog;
342 if( dialog.exec() == QDialog::Accepted )
344 ProjectParams params;
345 params.projectName = dialog.projectName();
346 params.author = dialog.author();
347 params.description = dialog.description();
349 params.isLocal = !dialog.useNetwork();
351 if( ! params.isLocal )
353 params.login = dialog.login();
354 params.password = dialog.password();
355 params.server = dialog.server();
356 params.port = dialog.port();
358 m_actionManager->find("showchatwindow")->setVisible(true);
360 else
362 m_actionManager->find("showchatwindow")->setVisible(false);
365 m_projectManager->createProject(params);
367 foreach(QString scene, dialog.scenes())
369 m_projectManager->createScene(scene, dialog.framesPerScene());
372 // m_projectView->setProject(m_projectManager->project());
376 bool MainWindow::openProject()
378 QString filePath = QFileDialog::getOpenFileName(this, QString(), QString(), tr("Dash project (*.dsh)") );
380 if( filePath.isEmpty() )
382 return false;
385 return openProject(filePath);
388 bool MainWindow::openProject(const QString &filePath)
390 if(!closeProject()) return false;
392 setUpdatesEnabled(false);
393 if( m_projectManager->loadProject(filePath) )
396 m_recentProjects.removeAll(filePath);
397 m_recentProjects.prepend(filePath);
400 m_currentFile = filePath;
401 // m_projectView->setProject(m_projectManager->project());
403 m_viewer->view()->setCurrentFrame(0, 0, 0);
405 m_actionManager->find("showchatwindow")->setVisible(false);
407 setUpdatesEnabled(true);
408 return true;
411 setUpdatesEnabled(true);
412 return false;
415 void MainWindow::openCollaborativeProject()
417 if( closeProject() )
419 m_projectManager->openCollaborativeProject();
420 m_actionManager->find("showchatwindow")->setVisible(true);
424 void MainWindow::exportProjectToServer()
426 COMING_SOON;
429 void MainWindow::saveProject()
431 if( m_currentFile.isEmpty() )
433 saveProjectAs();
434 return;
437 if( m_projectManager->saveProject(m_currentFile) )
439 DGui::Osd::self()->display(tr("Project %1 saved").arg(m_projectManager->project()->projectName()), DGui::Osd::Info);
441 else
443 m_currentFile = QString();
444 DGui::Osd::self()->display(tr("Cannot save the project!"), DGui::Osd::Error );
448 void MainWindow::autoSaveProject()
450 if( m_projectManager->project()->isOpen() && !m_currentFile.isEmpty() )
452 QTimer::singleShot(100, this, SLOT(saveProject()));
456 void MainWindow::saveProjectAs()
458 QString filePath = QFileDialog::getSaveFileName(this);
460 if( !filePath.isEmpty() )
462 m_currentFile = filePath;
464 if( !m_currentFile.endsWith(".dsh") )
465 m_currentFile += ".dsh";
467 QTimer::singleShot(0, this, SLOT(saveProject()));
471 bool MainWindow::closeProject()
473 if(!m_projectManager->project()->isOpen())
475 return true;
478 if ( m_projectManager->isModified() )
480 QMessageBox mb(QApplication::applicationName (), tr("Do you want to save?"),
481 QMessageBox::Information,
482 QMessageBox::Yes | QMessageBox::Default,
483 QMessageBox::No,
484 QMessageBox::Cancel | QMessageBox::Escape);
485 mb.setButtonText(QMessageBox::Yes, tr("Save"));
486 mb.setButtonText(QMessageBox::No, tr("Discard"));
488 switch(mb.exec())
490 case QMessageBox::Yes:
492 saveProject();
494 break;
495 case QMessageBox::No:
498 break;
499 case QMessageBox::Cancel:
501 return false;
503 break;
507 setUpdatesEnabled(false);
509 m_projectManager->closeProject();
510 // m_projectView->reload();
511 m_currentFile = QString();
513 setUpdatesEnabled(true);
515 return true;
518 void MainWindow::exportProject()
520 ExportDialog exportDialog(m_projectManager->project());
521 if( exportDialog.exec() == QDialog::Accepted )
523 if( exportDialog.generate() )
525 DGui::Osd::self()->display(tr("Export successfully!"));
527 else
529 DGui::Osd::self()->display(tr("Export failed!"), DGui::Osd::Error);
535 // View
537 void MainWindow::showChatWindow()
539 if( !m_projectManager->isLocal() )
541 m_projectManager->showChatWindow();
545 void MainWindow::loadLastProject()
547 DCore::Config *config = dApp->config("General");
548 QString lastProject = config->value("last_project", QString()).toString();
549 config->endGroup();
551 if( !lastProject.isEmpty() )
553 openProject(lastProject);
556 DGui::TipDialog dialog;
558 if( dialog.showOnStart() )
560 dialog.exec();
564 // EDIT
566 void MainWindow::configure()
568 SettingsDialog settings(m_viewer->view()->paintArea());
569 if( settings.exec() != QDialog::Rejected )
571 loadSettings();
575 // HELP
577 void MainWindow::showHelp()
579 COMING_SOON;
582 void MainWindow::aboutDash()
584 QMessageBox::about(this, tr("About Dash..."), tr("Dash is a tool to build up animations and ...") );
589 void MainWindow::changePerspective( QAction *action )
591 bool ok = false;
593 int id = action->data().toInt(&ok);
595 if(ok)
597 setCurrentPerspective(id);
601 void MainWindow::connectToPaintArea(QObject *object)
603 if( object->metaObject()->indexOfSignal("frameSelected(int,int,int)") > -1 )
605 connect(object, SIGNAL(frameSelected(int, int, int)), m_viewer->view(), SLOT(setCurrentFrame(int, int, int)));
608 if( object->metaObject()->indexOfSignal("brushChanged(QBrush)") > -1 )
610 connect(object, SIGNAL(brushChanged(const QBrush &)), m_viewer->view(), SLOT(setBrush(const QBrush &)));
613 if( object->metaObject()->indexOfSignal("addCurrentItemsToLibrary()") > -1 )
615 connect(object, SIGNAL(addCurrentItemsToLibrary()), m_viewer->view(), SLOT(addToLibrary()));
619 void MainWindow::saveSettings()
621 DCore::Config *config = dApp->config("General");
622 config->setValue("last_project", m_currentFile);
623 config->setValue("recents", m_recentProjects);
625 config->endGroup();
628 void MainWindow::loadSettings()
630 // Section General
631 DCore::Config *config = dApp->config("General");
633 m_saver.setInterval(config->value("save_project_interval", 15).toInt() * 60 * 1000);
635 if(config->value("auto_save_project", true).toBool() )
637 m_saver.start();
639 else
641 m_saver.stop();
644 m_recentProjects = config->value("recents").toStringList();
646 config->endGroup();
650 void MainWindow::openRecent(QAction *act)
653 QString filePath = act->text();
654 openProject(filePath);
657 void MainWindow::closeEvent(QCloseEvent *event)
659 saveSettings();
661 if (! closeProject() )
663 event->ignore();
664 return;
667 Ideality::StackedMainWindow::closeEvent(event);