Tips
[dashstudio.git] / src / shell / mainwindow.cpp
blobf84dc8c6108bf269a9cca246435d51bac7be176b
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/library/librarywidget.h>
53 #include <components/base/manager.h>
55 #include <components/colorpalette/mixer.h>
56 #include <components/colorpalette/palettes.h>
58 // Qt
60 #include <QMessageBox>
61 #include <QMenuBar>
62 #include <QMenu>
63 #include <QWorkspace>
64 #include <QApplication>
65 #include <QFileDialog>
66 #include <QTimer>
67 #include <QSettings>
68 #include <QCloseEvent>
70 #define COMING_SOON QMessageBox::warning(this, tr("Coming soon"), tr("Not implemented yet"));
72 MainWindow::MainWindow(QWidget *parent)
73 : Ideality::StackedMainWindow(parent)
75 setWindowTitle(tr("Dash"));
77 YAMF::initialize();
79 m_actionManager = new DGui::ActionManager(this);
80 m_componentManager = new Dash::Component::Manager(this);
82 m_projectManager = new ProjectManager(this);
83 m_viewer = new DocumentViewer(m_projectManager->project(), this);
84 m_brushSynchronizer = new Component::BrushSynchronizer(this);
86 loadSettings();
88 setupFileActions();
89 setupViewActions();
90 setupEditActions();
92 setupMenu();
93 setupComponents();
95 m_projectManager->registerModule(m_viewer);
96 foreach(Dash::Component::Widget *module, m_componentManager->widgets())
98 m_projectManager->registerModule(module);
102 addWidget(m_viewer, Dash::Drawing);
103 addWidget(new QWorkspace(this), Dash::Animation);
106 QTimer::singleShot(100, this, SLOT(loadLastProject()));
110 MainWindow::~MainWindow()
112 delete m_actionManager;
116 void MainWindow::setupFileActions()
118 QAction *newProject = new QAction( DGui::IconLoader::self()->load("project.png"), tr("New project"), this);
119 newProject->setShortcut(QKeySequence::New);
120 connect(newProject, SIGNAL(triggered()), this, SLOT(newProject()));
122 newProject->setStatusTip(tr( "Opens a new project"));
123 m_actionManager->add( newProject, "newproject", "file" );
126 QAction *openFile = new QAction( DGui::IconLoader::self()->load("open.png"), tr( "Open project" ), this);
127 openFile->setShortcut(QKeySequence::Open);
129 connect(openFile, SIGNAL(triggered()), this, SLOT(openProject()));
131 m_actionManager->add( openFile, "openproject", "file" );
132 openFile->setStatusTip(tr("Loads an existent project"));
134 QAction *openFromServer = new QAction(DGui::IconLoader::self()->load("open.png"), tr("Open collaborative project..."), this);
136 connect(openFromServer, SIGNAL(triggered()), this, SLOT(openCollaborativeProject()));
137 m_actionManager->add(openFromServer, "opencollaborativeproject", "file");
139 QAction *exportToServer = new QAction(QIcon(), tr("Export project to server..."), this);
141 connect(exportToServer, SIGNAL(triggered()), this, SLOT(exportProjectToServer()));
142 m_actionManager->add(exportToServer, "exportprojecttoserver", "file");
144 QAction *save = new QAction( DGui::IconLoader::self()->load("save.png"), tr( "Save project..." ), this);
145 save->setShortcut(QKeySequence::Save);
147 connect(save, SIGNAL(triggered()), this, SLOT(saveProject()));
148 m_actionManager->add( save, "saveproject", "file" );
149 save->setStatusTip(tr("Saves the current project in the current location"));
151 QAction *saveAs = new QAction( tr( "Save project &As..." ), this);
152 connect(saveAs, SIGNAL(triggered()), this, SLOT(saveProjectAs()));
153 saveAs->setStatusTip(tr("Opens a dialog box to save the current project in any location"));
154 m_actionManager->add( saveAs, "saveprojectas", "file" );
156 QAction *close = new QAction(DGui::IconLoader::self()->load("close.png"), tr( "Cl&ose project" ), this);
157 close->setShortcut(QKeySequence::Close);
158 connect(close, SIGNAL(triggered()), this, SLOT(closeProject()));
159 close->setStatusTip(tr("Closes the active project"));
160 m_actionManager->add( close, "closeproject", "file" );
162 QAction *exportProject = new QAction( DGui::IconLoader::self()->load("export.png"), tr( "&Export..." ), this);
164 connect(exportProject, SIGNAL(triggered()), this, SLOT(exportProject()));
165 exportProject->setStatusTip(tr("Exports project to different formats"));
166 m_actionManager->add( exportProject, "export", "file" );
168 QAction *importBitmap = new QAction(DGui::IconLoader::self()->load("import.png"), tr( "&Import bitmap..." ), this);
169 importBitmap->setStatusTip(tr("Import a bitmap to the project"));
170 m_actionManager->add( importBitmap, "importbitmap", "file" );
172 QAction *importSound = new QAction(DGui::IconLoader::self()->load("import.png"), tr( "&Import sounds..." ), this);
173 importSound->setStatusTip(tr("Import a sound to the project"));
174 m_actionManager->add( importSound, "importsound", "file" );
176 QAction *importSvg = new QAction(DGui::IconLoader::self()->load("import.png"), tr( "&Import svg..." ), this);
177 importSvg->setStatusTip(tr("Import a SVG to the project"));
178 m_actionManager->add( importSvg, "importsvg", "file" );
181 void MainWindow::setupViewActions()
183 QAction *showChatWindow = new QAction(QIcon(), tr( "Show chat window" ), this);
184 showChatWindow->setVisible(false);
186 connect(showChatWindow, SIGNAL(triggered()), this, SLOT(showChatWindow()));
187 // settings->setStatusTip(tr(""));
188 m_actionManager->add( showChatWindow, "showchatwindow", "view" );
191 void MainWindow::setupEditActions()
193 QAction *settings = new QAction(DGui::IconLoader::self()->load("settings.png"), tr( "Settings..." ), this);
194 connect(settings, SIGNAL(triggered()), this, SLOT(configure()));
195 // settings->setStatusTip(tr(""));
196 m_actionManager->add( settings, "settings", "edit" );
199 void MainWindow::setupMenu()
202 QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
205 QMenu *newMenu = fileMenu->addMenu(tr("New..."));
206 newMenu->addAction(m_actionManager->find("newproject"));
209 fileMenu->addAction(m_actionManager->find("openproject"));
211 QMenu *recent = fileMenu->addMenu(tr("Open recent"));
212 foreach(QString filePath, m_recentProjects)
214 recent->addAction(filePath);
216 connect(recent, SIGNAL(triggered( QAction* )), this, SLOT(openRecent(QAction *)));
218 fileMenu->addAction(m_actionManager->find("opencollaborativeproject"));
219 fileMenu->addAction(m_actionManager->find("exportprojecttoserver"));
221 fileMenu->addSeparator();
223 fileMenu->addAction(m_actionManager->find("saveproject"));
224 fileMenu->addAction(m_actionManager->find("saveprojectas"));
226 fileMenu->addSeparator();
228 fileMenu->addAction(m_actionManager->find("closeproject"));
230 fileMenu->addSeparator();
232 fileMenu->addAction(m_actionManager->find("export"));
234 QMenu *importMenu = fileMenu->addMenu(tr("Import"));
235 importMenu->addAction(m_actionManager->find("importbitmap"));
236 importMenu->addAction(m_actionManager->find("importsvg"));
237 importMenu->addAction(m_actionManager->find("importsound"));
239 fileMenu->addSeparator();
241 QAction *quit = fileMenu->addAction(tr("&Quit"));
242 connect(quit, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
246 QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
247 viewMenu->addAction( m_actionManager->find("showchatwindow") );
248 m_actionManager->find("showchatwindow")->setVisible(false);
252 QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
253 editMenu->addAction( m_actionManager->find("settings") );
257 QMenu *workspaceMenu = menuBar()->addMenu(tr("&Workspace"));
258 connect( workspaceMenu, SIGNAL(triggered( QAction * )), this, SLOT(changePerspective( QAction *)));
260 QActionGroup *workspaces = new QActionGroup(this);
261 workspaces->setExclusive(true);
263 QAction *drawingAction = new QAction( tr("Drawing"), workspaceMenu );
264 drawingAction->setShortcut(QKeySequence(Qt::Key_F9));
265 drawingAction->setCheckable(true);
266 drawingAction->setChecked(true);
267 drawingAction->setData(Dash::Drawing );
269 QAction *animationAction = new QAction( tr("Animation"), workspaceMenu );
270 animationAction->setShortcut(QKeySequence(Qt::Key_F10));
271 animationAction->setCheckable(true);
272 animationAction->setData(Dash::Animation);
274 workspaces->addAction(drawingAction);
275 workspaces->addAction(animationAction);
277 workspaceMenu->addActions(workspaces->actions());
281 QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
283 helpMenu->addAction(tr("Contents..."), this, SLOT(showHelp()))->setShortcut(QKeySequence::HelpContents);
284 helpMenu->addSeparator();
285 helpMenu->addAction(tr("About Dash..."), this, SLOT(aboutDash()));
286 helpMenu->addAction(tr("About Qt..."), qApp, SLOT(aboutQt()));
290 void MainWindow::setupComponents()
292 Dash::Component::TimeLine *tl = new Dash::Component::TimeLine(m_projectManager->project());
293 m_componentManager->add(tl);
295 connectToPaintArea(tl);
298 // m_viewer->view()->paintArea()->brushManager()->setPen(QPen(Qt::blue,3));
300 // Component::ColorPalette *colorPalette = new Component::ColorPalette(m_projectManager->project());
301 // m_componentManager->add(colorPalette);
303 Dash::Component::LibraryWidget *library = new Dash::Component::LibraryWidget(m_projectManager->project());
304 connect(m_actionManager->find("importbitmap", "file"), SIGNAL(triggered()), library, SLOT(importBitmap()));
305 connect(m_actionManager->find("importsvg", "file"), SIGNAL(triggered()), library, SLOT(importSvg()));
306 connect(m_actionManager->find("importsound", "file"), SIGNAL(triggered()), library, SLOT(importSound()));
307 connect(library, SIGNAL(addSymbolToProject(const QString&)), m_viewer->view(), SLOT(addSymbol(const QString&)));
309 m_componentManager->add(library);
310 connectToPaintArea(library);
313 connectToPaintArea(m_brushSynchronizer);
314 addToolView(m_brushSynchronizer->mixer(), Qt::LeftDockWidgetArea, Dash::Drawing);
315 addToolView(m_brushSynchronizer->palettes(), Qt::LeftDockWidgetArea, Dash::Drawing);
317 /*********************/
318 m_projectView = new YAMF::Gui::ProjectView(m_projectManager->project());
319 addToolView(m_projectView, Qt::RightDockWidgetArea, Dash::All);
322 void MainWindow::newProject()
324 if(!closeProject()) return;
326 NewProjectDialog dialog;
328 if( dialog.exec() == QDialog::Accepted )
330 ProjectParams params;
331 params.projectName = dialog.projectName();
332 params.author = dialog.author();
333 params.description = dialog.description();
335 params.isLocal = !dialog.useNetwork();
337 if( ! params.isLocal )
339 params.login = dialog.login();
340 params.password = dialog.password();
341 params.server = dialog.server();
342 params.port = dialog.port();
344 m_actionManager->find("showchatwindow")->setVisible(true);
346 else
348 m_actionManager->find("showchatwindow")->setVisible(false);
351 m_projectManager->createProject(params);
353 foreach(QString scene, dialog.scenes())
355 m_projectManager->createScene(scene, dialog.framesPerScene());
358 m_projectView->setProject(m_projectManager->project());
362 bool MainWindow::openProject()
364 QString filePath = QFileDialog::getOpenFileName(this, QString(), QString(), tr("Dash project (*.dsh)") );
366 if( filePath.isEmpty() )
368 return false;
371 return openProject(filePath);
374 bool MainWindow::openProject(const QString &filePath)
376 if(!closeProject()) return false;
378 setUpdatesEnabled(false);
379 if( m_projectManager->loadProject(filePath) )
382 m_recentProjects.removeAll(filePath);
383 m_recentProjects.prepend(filePath);
386 m_currentFile = filePath;
387 m_projectView->setProject(m_projectManager->project());
389 m_viewer->view()->setCurrentFrame(0, 0, 0);
391 m_actionManager->find("showchatwindow")->setVisible(false);
393 setUpdatesEnabled(true);
394 return true;
397 setUpdatesEnabled(true);
398 return false;
401 void MainWindow::openCollaborativeProject()
403 if( closeProject() )
405 m_projectManager->openCollaborativeProject();
406 m_actionManager->find("showchatwindow")->setVisible(true);
410 void MainWindow::exportProjectToServer()
412 COMING_SOON;
415 void MainWindow::saveProject()
417 if( m_currentFile.isEmpty() )
419 saveProjectAs();
420 return;
423 if( m_projectManager->saveProject(m_currentFile) )
425 DGui::Osd::self()->display(tr("Project %1 saved").arg(m_projectManager->project()->projectName()), DGui::Osd::Info);
427 else
429 m_currentFile = QString();
430 DGui::Osd::self()->display(tr("Cannot save the project!"), DGui::Osd::Error );
434 void MainWindow::autoSaveProject()
436 if( m_projectManager->project()->isOpen() && !m_currentFile.isEmpty() )
438 QTimer::singleShot(100, this, SLOT(saveProject()));
442 void MainWindow::saveProjectAs()
444 QString filePath = QFileDialog::getSaveFileName(this);
446 if( !filePath.isEmpty() )
448 m_currentFile = filePath;
450 if( !m_currentFile.endsWith(".dsh") )
451 m_currentFile += ".dsh";
453 QTimer::singleShot(0, this, SLOT(saveProject()));
457 bool MainWindow::closeProject()
459 if(!m_projectManager->project()->isOpen())
461 return true;
464 if ( m_projectManager->isModified() )
466 QMessageBox mb(QApplication::applicationName (), tr("Do you want to save?"),
467 QMessageBox::Information,
468 QMessageBox::Yes | QMessageBox::Default,
469 QMessageBox::No,
470 QMessageBox::Cancel | QMessageBox::Escape);
471 mb.setButtonText(QMessageBox::Yes, tr("Save"));
472 mb.setButtonText(QMessageBox::No, tr("Discard"));
474 switch(mb.exec())
476 case QMessageBox::Yes:
478 saveProject();
480 break;
481 case QMessageBox::No:
484 break;
485 case QMessageBox::Cancel:
487 return false;
489 break;
493 setUpdatesEnabled(false);
495 m_projectManager->closeProject();
496 m_projectView->reload();
497 m_currentFile = QString();
499 setUpdatesEnabled(true);
501 return true;
504 void MainWindow::exportProject()
506 ExportDialog exportDialog(m_projectManager->project());
507 if( exportDialog.exec() == QDialog::Accepted )
509 if( exportDialog.generate() )
511 DGui::Osd::self()->display(tr("Export successfully!"));
513 else
515 DGui::Osd::self()->display(tr("Export failed!"), DGui::Osd::Error);
521 // View
523 void MainWindow::showChatWindow()
525 if( !m_projectManager->isLocal() )
527 m_projectManager->showChatWindow();
531 void MainWindow::loadLastProject()
533 DCore::Config *config = dApp->config("General");
534 QString lastProject = config->value("last_project", QString()).toString();
535 config->endGroup();
537 if( !lastProject.isEmpty() )
539 openProject(lastProject);
542 DGui::TipDialog dialog;
544 if( dialog.showOnStart() )
546 dialog.exec();
550 // EDIT
552 void MainWindow::configure()
554 SettingsDialog settings(m_viewer->view()->paintArea());
555 if( settings.exec() != QDialog::Rejected )
557 loadSettings();
561 // HELP
563 void MainWindow::showHelp()
565 COMING_SOON;
568 void MainWindow::aboutDash()
570 QMessageBox::about(this, tr("About Dash..."), tr("Dash is a tool to build up animations and ...") );
575 void MainWindow::changePerspective( QAction *action )
577 bool ok = false;
579 int id = action->data().toInt(&ok);
581 if(ok)
583 setCurrentPerspective(id);
587 void MainWindow::connectToPaintArea(QObject *object)
589 if( object->metaObject()->indexOfSignal("frameSelected(int,int,int)") > -1 )
591 connect(object, SIGNAL(frameSelected(int, int, int)), m_viewer->view(), SLOT(setCurrentFrame(int, int, int)));
594 if( object->metaObject()->indexOfSignal("brushChanged(QBrush)") > -1 )
596 connect(object, SIGNAL(brushChanged(const QBrush &)), m_viewer->view(), SLOT(setBrush(const QBrush &)));
599 if( object->metaObject()->indexOfSignal("addCurrentItemsToLibrary()") > -1 )
601 connect(object, SIGNAL(addCurrentItemsToLibrary()), m_viewer->view(), SLOT(addToLibrary()));
605 void MainWindow::saveSettings()
607 DCore::Config *config = dApp->config("General");
608 config->setValue("last_project", m_currentFile);
609 config->setValue("recents", m_recentProjects);
611 config->endGroup();
614 void MainWindow::loadSettings()
616 // Section General
617 DCore::Config *config = dApp->config("General");
619 m_saver.setInterval(config->value("save_project_interval", 15).toInt() * 60 * 1000);
621 if(config->value("auto_save_project", true).toBool() )
623 m_saver.start();
625 else
627 m_saver.stop();
630 m_recentProjects = config->value("recents").toStringList();
632 config->endGroup();
636 void MainWindow::openRecent(QAction *act)
639 QString filePath = act->text();
640 openProject(filePath);
643 void MainWindow::closeEvent(QCloseEvent *event)
645 saveSettings();
647 if (! closeProject() )
649 event->ignore();
650 return;
653 Ideality::StackedMainWindow::closeEvent(event);