FreeDebks  1.0.3
 All Classes Files Functions Variables Friends Pages
FdMainWindow.cpp
Go to the documentation of this file.
1 // --------------------------------------------------------------------
2 // Copyright © 2011-2013 Mathieu Schopfer
3 //
4 // This file is part of FreeDebks.
5 //
6 // FreeDebks is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // FreeDebks is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with FreeDebks. If not, see <http://www.gnu.org/licenses/>.
18 // --------------------------------------------------------------------
19 
20 #include "FdMainWindow.hpp"
21 
22 #include <QMdiSubWindow>
23 #include <QFileDialog>
24 
25 #include "FdSubWindow.hpp"
26 #include "FdDialogBkgProperties.hpp"
27 
29 
33 FdMainWindow::FdMainWindow(QWidget* parent) : QMainWindow(parent)
34 {
35  QString file = QString::fromUtf8(qApp->argv()[1]);
36 
37  FdMainWindow::self = this;
38  setupUi(this);
39 
40  QSettings settings;
41  settings.beginGroup("MainWindow");
42  resize(settings.value("size", QSize(1000, 650)).toSize());
43  move(settings.value("pos", QPoint(200, 200)).toPoint());
44  QStringList files = settings.value("RecentFilesOpened").toStringList();
45  settings.endGroup();
46 
47  // Create recent files opened actions
48  for(int i = 0; i < files.size(); ++i)
49  mRecentOpenedFiles << createOpenRecentFileAction(files[i]);
50  if(files.size())
51  menuOpenRecent->addActions(mRecentOpenedFiles);
52  else
53  menuOpenRecent->setEnabled(false);
54 
55  // Sets icon
56  // On Windows, buttons' icons are compiled-in resources referenced in resources_win.qrc
57 #ifdef Q_OS_LINUX
58  menuOpenRecent->setIcon(QIcon::fromTheme("document-open-recent",QIcon(DIR_PREFIX + "/share/freedebks/icons/document-open-recent.png")));
59  // Sets icons and shortcuts for actions defined in QtDesigner
60  // File
61  actionNew->setIcon(QIcon::fromTheme("document-new",QIcon(DIR_PREFIX + "/share/freedebks/icons/document-new.png")));
62  actionOpen->setIcon(QIcon::fromTheme("document-open",QIcon(DIR_PREFIX + "/share/freedebks/icons/document-open.png")));
63  actionSave->setIcon(QIcon::fromTheme("document-save",QIcon(DIR_PREFIX + "/share/freedebks/icons/document-save.png")));
64  actionSaveAs->setIcon(QIcon::fromTheme("document-save-as",QIcon(DIR_PREFIX + "/share/freedebks/icons/document-save-as.png")));
65  actionPrint->setIcon(QIcon::fromTheme("document-print",QIcon(DIR_PREFIX + "/share/freedebks/icons/document-print.png")));
66  actionClose->setIcon(QIcon::fromTheme("window-close",QIcon(DIR_PREFIX + "/share/freedebks/icons/window-close.png")));
67  actionQuit->setIcon(QIcon::fromTheme("application-exit",QIcon(DIR_PREFIX + "/share/freedebks/icons/application-exit.png")));
68 
69  // Edit
70  actionAddLine->setIcon(QIcon::fromTheme("list-add",QIcon(DIR_PREFIX + "/share/freedebks/icons/list-add.png")));
71  actionRemoveSelected->setIcon(QIcon::fromTheme("list-remove",QIcon(DIR_PREFIX + "/share/freedebks/icons/list-remove.png")));
72  actionCopy->setIcon(QIcon::fromTheme("edit-copy",QIcon(DIR_PREFIX + "/share/freedebks/icons/edit-copy.png")));
73  actionFindNext->setIcon(QIcon::fromTheme("system-search",QIcon(DIR_PREFIX + "/share/freedebks/icons/system-search.png")));
74  // Journal
75  actionOrderByDate->setIcon(QIcon::fromTheme("view-sort-ascending",QIcon(DIR_PREFIX + "/share/freedebks/icons/view-sort-ascending.png")));
76  actionCopyPreviousLine->setIcon(QIcon::fromTheme("go-bottom",QIcon(DIR_PREFIX + "/share/freedebks/icons/go-bottom.png")));
77  // Accounting
78  actionEditProperties->setIcon(QIcon::fromTheme("document-properties",QIcon(DIR_PREFIX + "/share/freedebks/icons/document-properties.png")));
79  actionRecomputeAll->setIcon(QIcon::fromTheme("view-refresh",QIcon(DIR_PREFIX + "/share/freedebks/icons/view-refresh.png")));
80  // View
81  // Help
82  actionHelp->setIcon(QIcon::fromTheme("help-contents",QIcon(DIR_PREFIX + "/share/freedebks/icons/help-contents.png")));
83  actionAbout->setIcon(QIcon::fromTheme("help-about",QIcon(DIR_PREFIX + "/share/freedebks/icons/help-about.png")));
84 #else
85  menuOpenRecent->setIcon(QIcon::fromTheme("document-open-recent",QIcon(":/buttons/document-open-recent")));
86  // Sets icons and shortcuts for actions defined in QtDesigner
87  // File
88  actionNew->setIcon(QIcon::fromTheme("document-new",QIcon(":/buttons/document-new")));
89  actionOpen->setIcon(QIcon::fromTheme("document-open",QIcon(":/buttons/document-open")));
90  actionSave->setIcon(QIcon::fromTheme("document-save",QIcon(":/buttons/document-save")));
91  actionSaveAs->setIcon(QIcon::fromTheme("document-save-as",QIcon(":/buttons/document-save-as")));
92  actionPrint->setIcon(QIcon::fromTheme("document-print",QIcon(":/buttons/document-print")));
93  actionClose->setIcon(QIcon::fromTheme("window-close",QIcon(":/buttons/window-close")));
94  actionQuit->setIcon(QIcon::fromTheme("application-exit",QIcon(":/buttons/application-exit")));
95  // Edit
96  actionAddLine->setIcon(QIcon::fromTheme("list-add",QIcon(":/buttons/list-add")));
97  actionRemoveSelected->setIcon(QIcon::fromTheme("list-remove",QIcon(":/buttons/list-remove")));
98  actionCopy->setIcon(QIcon::fromTheme("edit-copy",QIcon(":/buttons/edit-copy")));
99  actionFindNext->setIcon(QIcon::fromTheme("system-search",QIcon(":/buttons/system-search")));
100  // Journal
101  actionOrderByDate->setIcon(QIcon::fromTheme("view-sort-ascending",QIcon(":/buttons/view-sort-ascending")));
102  actionCopyPreviousLine->setIcon(QIcon::fromTheme("go-bottom",QIcon(":/buttons/go-bottom")));
103  // Accounting
104  actionEditProperties->setIcon(QIcon::fromTheme("document-properties",QIcon(":/buttons/document-properties")));
105  actionRecomputeAll->setIcon(QIcon::fromTheme("view-refresh",QIcon(":/buttons/view-refresh")));
106  // View
107  // Help
108  actionHelp->setIcon(QIcon::fromTheme("help-contents",QIcon(":/buttons/help-contents")));
109  actionAbout->setIcon(QIcon::fromTheme("help-about",QIcon(":/buttons/help-about")));
110 #endif
111 
112  // Keyboard shortcuts
113  actionNew->setShortcut(QKeySequence::New);
114  actionOpen->setShortcut(QKeySequence::Open);
115  actionSave->setShortcut(QKeySequence::Save);
116  actionSaveAs->setShortcut(QKeySequence::SaveAs);
117  actionPrint->setShortcut(QKeySequence::Print);
118  actionClose->setShortcut(QKeySequence::Close);
119  actionQuit->setShortcut(QKeySequence::Quit);
120  actionCopy->setShortcut(QKeySequence::Copy);
121  actionFindNext->setShortcut(QKeySequence::FindNext);
122  actionRecomputeAll->setShortcut(QKeySequence::Refresh);
123 
124  menuHelp->addAction(QWhatsThis::createAction());
125 
126  // Context menus
127  mContextMenuActionsEdit << actionAddLine << actionRemoveSelected << actionCopy;
128  mContextMenuActionsCoa << actionRecomputeAll << actionShowAccount;
129  mContextMenuActionsJournal << actionOrderByDate << actionCopyPreviousLine << actionShowAccount << actionRecomputeAll;
130  mContextMenuActionsResults << actionRecomputeAll;
131  mContextMenuActionsAccount << actionCopy << actionShowAccount << actionShowEntry << actionRecomputeAll;
132 
133  setDockText();
134  activeTabChanged();
135 
136  if(!file.isNull())
137  open(file);
138 }
139 
140 void FdMainWindow::enableSaveAction(bool value)
141 {
142  actionSave->setEnabled(value);
143 }
144 
145 void FdMainWindow::enableRemoveAction(bool value)
146 {
147  actionRemoveSelected->setEnabled(value);
148 }
149 
151 {
152  actionShowAccount->setEnabled(value);
153 }
154 
156 {
157  actionShowEntry->setEnabled(value);
158 }
159 
160 QList<QAction*> FdMainWindow::getContextMenuActionsEdit() const
161 {
163 }
164 
165 QList<QAction*> FdMainWindow::getContextMenuActionsCoa() const
166 {
167  return mContextMenuActionsCoa;
168 }
169 
170 QList<QAction*> FdMainWindow::getContextMenuActionsJournal() const
171 {
173 }
174 
175 QList<QAction*> FdMainWindow::getContextMenuActionsResults() const
176 {
178 }
179 
180 QList<QAction*> FdMainWindow::getContextMenuActionsAccount() const
181 {
183 }
184 
188 void FdMainWindow::setDockText(QString text)
189 {
190  if(text.isEmpty())
191  text = tr("<b>Welcome in FreeDebks !</b>");
192 
193  dockContent->setText(text);
194 }
195 
199 void FdMainWindow::closeEvent(QCloseEvent* event)
200 {
201  int size = mdiArea->subWindowList().size();
202  bool b = true;
203  for(int i = 0; i < size; ++i)
204  {
205  mdiArea->activateNextSubWindow();
207  if(!b)
208  {
209  event->ignore();
210  break;
211  }
212  }
213  if(b)
214  {
215  QStringList files;
216  int max = qMin(mRecentOpenedFiles.size(), 5);
217  for(int i = 0; i < max; ++i)
218  files << mRecentOpenedFiles[i]->data().toString();
219 
220  QSettings settings;
221  settings.beginGroup("MainWindow");
222  settings.setValue("size", this->size());
223  settings.setValue("pos", pos());
224  settings.setValue("RecentFilesOpened", files);
225  settings.endGroup();
226 
227  event->accept();
228  }
229 }
230 
231 QAction* FdMainWindow::createOpenRecentFileAction(QString file)
232 {
233  QAction* action = new QAction(this);
234  action->setText(QFileInfo(file).fileName());
235  action->setData(file);
236  connect(action, SIGNAL(triggered()), this, SLOT(openRecentFile()));
237  return action;
238 }
239 
241 {
242 
243  int pos = -1;
244  for(int i = 0; i < mRecentOpenedFiles.size(); ++i)
245  {
246  if(mRecentOpenedFiles[i]->data().toString() == file)
247  {
248  pos = i;
249  break;
250  }
251  }
252 
253  if(pos == -1)
255  else if(pos)
256  mRecentOpenedFiles.move(pos, 0);
257 
258  menuOpenRecent->clear();
259  menuOpenRecent->addActions(mRecentOpenedFiles);
260  menuOpenRecent->setEnabled(true);
261 }
262 
267 {
268  FdDialogBkgProperties dialog(this, this);
270  connect(&dialog, SIGNAL(accepted(FdSubWindow*)), this, SLOT(addSubWindow(FdSubWindow*)));
271  dialog.exec();
272 }
273 
275 {
276  QString file = QFileDialog::getOpenFileName(this, QObject::tr("Select a file to open"), QDir::homePath(),
277  QObject::tr("FreeDebks files (*.fds);;XML files (*.xml);;Any type (*.*)", "File format, so leave (*.fds), (*.xml) and (*.*) unchanged."));
278  if(!file.isNull())
279  open(file);
280 }
281 
282 void FdMainWindow::open(QString filePath)
283 {
284  QFile* file = new QFile(filePath);
285  if(file->exists())
286  {
287  FdSubWindow* subwindow = new FdSubWindow(file);
288  addSubWindow(subwindow);
289 
290  updateOpenRecentFileActions(filePath);
291  }
292  else
293  QMessageBox::warning(this, tr("File not found", "File to open not found."),
294  tr("Impossible to open the file you requested. Please make sure it exists."));
295 
296 }
297 
299 {
300  QAction *action = qobject_cast<QAction *>(sender());
301  if(action)
302  open(action->data().toString());
303 }
304 
306 {
308  (qobject_cast<FdSubWindow*>(mdiArea->currentSubWindow()))->save();
309 }
310 
312 {
313  QString file = QFileDialog::getSaveFileName(qobject_cast<QWidget*>(parent()),
314  QObject::tr("Enter a new file name or select an existing file"),
315  QDir::homePath(), QObject::tr("FreeDebks files (*.fds);;XML files (*.xml)", "File format, so leave (*.fds) and (*.xml) unchanged."));
316  if(!file.isNull())
317  {
318  (qobject_cast<FdSubWindow*>(mdiArea->currentSubWindow()))->saveAs(file);
320  }
321 }
322 
324 {
325  (qobject_cast<FdSubWindow*>(mdiArea->currentSubWindow()))->print();
326 }
327 
329 {
330  return qobject_cast<FdSubWindow*>(mdiArea->currentSubWindow())->close();
331 }
332 
334 {
335  close();
336 }
337 
342 {
344  (qobject_cast<FdSubWindow*>(mdiArea->currentSubWindow()))->addLine();
345 }
346 
348 {
349  (qobject_cast<FdSubWindow*>(mdiArea->currentSubWindow()))->removeSelected();
350 }
351 
353 {
354  (qobject_cast<FdSubWindow*>(mdiArea->currentSubWindow()))->copy();
355 }
356 
358 {
359  (qobject_cast<FdSubWindow*>(mdiArea->currentSubWindow()))->findNext();
360 }
361 
363 {
364  (qobject_cast<FdSubWindow*>(mdiArea->currentSubWindow()))->recomputeAll();
365 }
366 
371 {
372  FdDialogBkgProperties dialog(this, qobject_cast<FdSubWindow*>(mdiArea->currentSubWindow()));
373  dialog.exec();
374 }
375 
380 {
382  FdSubWindow* newYear = new FdSubWindow(qobject_cast<FdSubWindow*>(mdiArea->currentSubWindow()));
383  FdDialogBkgProperties dialog(this, newYear);
384  // Connects FdDialogBkgProperties::newBkgCreated() signal to the addSubWindow() slot of this class.
385  connect(&dialog, SIGNAL(accepted(FdSubWindow*)), this, SLOT(addSubWindow(FdSubWindow*)));
386  connect(&dialog, SIGNAL(rejected()), newYear, SLOT(deleteLater()));
387  dialog.exec();
388 }
389 
391 {
392  (qobject_cast<FdSubWindow*>(mdiArea->currentSubWindow()))->journalCopyFromPreviousLine();
393 }
394 
399 {
400  // Gets the activated FdSubWindow, calling QMdiArea::currentSubWindow() method and calls FdSubWindow::addLine().
401  (qobject_cast<FdSubWindow*>(mdiArea->currentSubWindow()))->journalOrderByDate();
402 }
403 
405 {
406  (qobject_cast<FdSubWindow*>(mdiArea->currentSubWindow()))->showAccount();
407 }
408 
410 {
411  (qobject_cast<FdSubWindow*>(mdiArea->currentSubWindow()))->showEntry();
412 }
413 
415 {
416  QString locale = QLocale::system().name().section('_', 0, 0);
417  QString url;
418 #ifdef Q_OS_LINUX
419  url = DIR_PREFIX+"/share/freedebks/documentation/doc_"+locale+".pdf";
420 #else
421  url = "documentation/doc_"+locale+".pdf";
422 #endif
423  if(QFile(url).exists())
424  QDesktopServices::openUrl(url);
425  else
426  QDesktopServices::openUrl(QUrl("http://freedebks.sourceforge.net/documentation/en/"));
427 
428 }
429 
431 {
432  QMessageBox::about(this, "About FreeDebks", QString("<b>FreeDebks version %1</b><br />"
433  "Distributed under GPLv3.<br />"
434  "Copyright (c) Mathieu Schopfer (mathieus@users.sourceforge.net)<br />"
435  "Visit the application website at <a href = %2>%2</a>."
436  ).arg(qApp->applicationVersion(), qApp->organizationDomain()));
437 }
438 
445 {
446  mdiArea->addSubWindow(subwindow);
447  subwindow->showMaximized();
448 }
449 
453 void FdMainWindow::activeTabChanged(TabRole role, QAction *undo, QAction *redo, bool windowModified)
454 {
455  if(role && mActiveTab != role)
456  {
457  switch(role)
458  {
459  case CoaRole:
460  actionAddLine->setEnabled(true);
461  actionOrderByDate->setEnabled(false);
462  actionCopyPreviousLine->setEnabled(false);
463  actionShowEntry->setEnabled(false);
464  break;
465  case JournalRole:
466  actionAddLine->setEnabled(true);
467  actionOrderByDate->setEnabled(true);
468  actionCopyPreviousLine->setEnabled(true);
469  actionShowEntry->setEnabled(false);
470  break;
471  case ResultsRole:
472  actionAddLine->setEnabled(true);
473  actionOrderByDate->setEnabled(false);
474  actionCopyPreviousLine->setEnabled(false);
475  actionShowEntry->setEnabled(false);
476  break;
477  case AccountRole:
478  actionAddLine->setEnabled(false);
479  actionOrderByDate->setEnabled(false);
480  actionCopyPreviousLine->setEnabled(false);
481  break;
482  }
483 
484  // File
485  actionSaveAs->setEnabled(true);
486  actionPrint->setEnabled(true);
487  actionClose->setEnabled(true);
488  // Edit
489  actionCopy->setEnabled(true);
490  actionFindNext->setEnabled(true);
491  // Accounting
492  actionRecomputeAll->setEnabled(true);
493  actionEditProperties->setEnabled(true);
494  actionCreateNewYear->setEnabled(true);
495  }
496  // Handels closing of the last subwindow
497  else if(!role)
498  {
499  // File
500  actionSaveAs->setDisabled(true);
501  actionPrint->setDisabled(true);
502  actionClose->setDisabled(true);
503  // Edit
504  actionAddLine->setDisabled(true);
505  actionRemoveSelected->setDisabled(true);
506  actionCopy->setDisabled(true);
507  actionFindNext->setDisabled(true);
508  // Journal
509  actionOrderByDate->setDisabled(true);
510  actionCopyPreviousLine->setDisabled(true);
511  // Accounting
512  actionRecomputeAll->setDisabled(true);
513  actionEditProperties->setDisabled(true);
514  actionCreateNewYear->setDisabled(true);
515  // View
516  actionShowAccount->setDisabled(true);
517  actionShowEntry->setDisabled(true);
518 
519  undo->setDisabled(true);
520  redo->setDisabled(true);
521  }
522 
523  toolBarUndoRedo->clear();
524  undo->setIcon(QIcon::fromTheme("edit-undo",QIcon(":/buttons/edit-undo")));
525  undo->setShortcut(QKeySequence::Undo);
526  redo->setIcon(QIcon::fromTheme("edit-redo",QIcon(":/buttons/edit-redo")));
527  redo->setShortcut(QKeySequence::Redo);
528  toolBarUndoRedo->addAction(undo);
529  toolBarUndoRedo->addAction(redo);
530 
531  enableSaveAction(windowModified);
532 
533  mActiveTab = role;
534 }
535 
540 {
541  if(!mdiArea->subWindowList().size()-1)
542  {
544  setDockText();
545  }
546 }