MainWindow.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*********************************************************************/
  2. /* */
  3. /* Copyright 2022-2023 Rémi Synave - remi.synave@univ-littoral.fr */
  4. /* */
  5. /* This file is part of DSL. */
  6. /* This software uses Qt to build the Graphical User Interface */
  7. /* https://www.qt.io/ */
  8. /* */
  9. /* DSL is free software: you can redistribute it and/or modify */
  10. /* it under the terms of the GNU General Public License as published */
  11. /* by the Free Software Foundation, either version 3 of the License, */
  12. /* or (at your option) any later version. */
  13. /* */
  14. /* DSL is distributed in the hope that it will be useful, */
  15. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  16. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  17. /* GNU General Public License for more details. */
  18. /* */
  19. /* You should have received a copy of the GNU General Public License */
  20. /* along with DSL. If not, see <http://www.gnu.org/licenses/>. */
  21. /* */
  22. /*********************************************************************/
  23. #include <QIcon>
  24. #include <QGuiApplication>
  25. #include <QScreen>
  26. #include <QWidget>
  27. #include <QMenuBar>
  28. #include <QFileDialog>
  29. #include <QUrl>
  30. #include <QFileInfo>
  31. #include <QInputDialog>
  32. #include <QDirIterator>
  33. #include <QDebug>
  34. #include <iostream>
  35. #include <filesystem>
  36. #include "MainWindow.hpp"
  37. MainWindow::MainWindow ()
  38. {
  39. setWindowIcon(QIcon("../dsl.ico"));
  40. createActions ();
  41. createMenus ();
  42. openedImage = -1;
  43. directoryname = NULL;
  44. suffix = NULL;
  45. filenames = NULL;
  46. autoLoad = true;
  47. autoSave = false;
  48. setWindowTitle (tr ("DSL - Draw Strength Line"));
  49. QScreen *screen = QGuiApplication::primaryScreen ();
  50. QRect screenGeometry = screen->geometry ();
  51. int screenWidth = screenGeometry.width () - 200;
  52. int screenHeight = screenGeometry.height () - 200;
  53. wa = new WorkArea (screenWidth, screenHeight, this);
  54. wa->setReadOnly(false);
  55. setCentralWidget (wa);
  56. setFixedSize (screenWidth, screenHeight);
  57. }
  58. void
  59. MainWindow::openFile ()
  60. {
  61. if(this->checkModificationinProgress() == QMessageBox::Yes){
  62. QString filename = QFileDialog::getOpenFileName (this,
  63. QObject::tr ("Open image file"),
  64. QDir::currentPath (),
  65. QObject::tr("Images files (*.jpg);;All files (*.*)"));
  66. if(!filename.isEmpty()) {
  67. directoryname = new QString(QString(filename).remove(QUrl(filename).fileName()));
  68. bool ok;
  69. suffix = new QString(QInputDialog::getText(this, tr("Suffix for your json files"),
  70. tr("Suffix to use :"), QLineEdit::Normal,
  71. "_"+QDir::home().dirName(), &ok));
  72. if(ok)
  73. {
  74. delete filenames;
  75. filenames = new QStringList();
  76. QDirIterator it(*directoryname, {"*.jpg"}, QDir::Files);
  77. while (it.hasNext()) {
  78. filenames->append( it.next() );
  79. }
  80. filenames->sort();
  81. for(int i = 0; i <filenames->size() ; i++)
  82. {
  83. if(filename == (filenames->at(i)))
  84. openedImage = i;
  85. }
  86. QDir::setCurrent(*directoryname);
  87. loadImage();
  88. }
  89. }
  90. }
  91. }
  92. void
  93. MainWindow::loadImage()
  94. {
  95. wa->loadImage (filenames->at(openedImage).toStdString ());
  96. if(autoLoad)
  97. {
  98. QString jsonFile(QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(*suffix).append(".json")));
  99. if(QFile(jsonFile).exists()){
  100. wa->loadSL(QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(*suffix).append(".json")).toStdString());
  101. }
  102. }
  103. wa->repaint();
  104. wa->setModificationInProgress(false);
  105. setFixedSize (wa->geometry().width(), wa->geometry().height()+wa->geometry().y());
  106. }
  107. void
  108. MainWindow::save ()
  109. {
  110. QString toSave = QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(*suffix).append(".json"));
  111. wa->saveStrengthLine (toSave.toStdString ());
  112. }
  113. void
  114. MainWindow::exportPNG ()
  115. {
  116. QString toSave = QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(*suffix).append(".png"));
  117. wa->exportPNG (toSave.toStdString ());
  118. }
  119. QMessageBox::StandardButton
  120. MainWindow::checkModificationinProgress()
  121. {
  122. if(wa->getModificationInProgress())
  123. {
  124. if(autoSave)
  125. this->save();
  126. else
  127. return QMessageBox::question(this, "Modification in progress", "Do you really want to change the image ? Modifications in progress will be lost.");
  128. }
  129. return QMessageBox::Yes;
  130. }
  131. void
  132. MainWindow::firstImage ()
  133. {
  134. if(filenames != NULL)
  135. if(filenames->size() > 1)
  136. if(this->checkModificationinProgress() == QMessageBox::Yes){
  137. openedImage = 0;
  138. loadImage();
  139. }
  140. }
  141. void
  142. MainWindow::previousImage ()
  143. {
  144. if(filenames != NULL)
  145. if(filenames->size() > 1)
  146. if(this->checkModificationinProgress() == QMessageBox::Yes){
  147. openedImage = (openedImage-1+filenames->size())%(filenames->size());
  148. loadImage();
  149. }
  150. }
  151. void
  152. MainWindow::nextImage ()
  153. {
  154. if(filenames != NULL)
  155. if(filenames->size() > 1)
  156. if(this->checkModificationinProgress() == QMessageBox::Yes){
  157. openedImage = (openedImage+1)%(filenames->size());
  158. loadImage();
  159. }
  160. }
  161. void
  162. MainWindow::lastImage ()
  163. {
  164. if(filenames != NULL)
  165. if(filenames->size() > 1)
  166. if(this->checkModificationinProgress() == QMessageBox::Yes){
  167. openedImage = filenames->size()-1;
  168. loadImage();
  169. }
  170. }
  171. void
  172. MainWindow::automaticLoading ()
  173. {
  174. autoLoad = !autoLoad;
  175. automaticLoadingAct->setChecked(!autoLoad);
  176. }
  177. void
  178. MainWindow::automaticSaving ()
  179. {
  180. autoSave = !autoSave;
  181. automaticSavingAct->setChecked(!autoSave);
  182. }
  183. void
  184. MainWindow::readOnly ()
  185. {
  186. if(wa->getReadOnly()){
  187. wa->setReadOnly(false);
  188. readOnlyAct->setText(tr ("W&ritable"));
  189. }else{
  190. wa->setReadOnly(true);
  191. readOnlyAct->setText(tr ("&Read Only"));
  192. }
  193. wa->repaint();
  194. }
  195. void
  196. MainWindow::about (){
  197. QMessageBox::information(this, "About", "DSL - Draw Strength Line\nVersion 1.1\n\nRémi Synave - remi.synave@univ-littoral.fr\n\nGNU GENERAL PUBLIC LICENSE Version 3");
  198. }
  199. void
  200. MainWindow::createActions ()
  201. {
  202. openAct = new QAction (tr ("&Open image"), this);
  203. openAct->setShortcuts (QKeySequence::Open);
  204. connect (openAct, &QAction::triggered, this, &MainWindow::openFile);
  205. saveAct = new QAction (tr ("&Save"), this);
  206. saveAct->setShortcuts (QKeySequence::Save);
  207. connect (saveAct, &QAction::triggered, this, &MainWindow::save);
  208. exportAct = new QAction (tr ("&Export in PNG"), this);
  209. exportAct->setShortcut (QKeySequence(Qt::CTRL | Qt::Key_E));
  210. connect (exportAct, &QAction::triggered, this, &MainWindow::exportPNG);
  211. exitAct = new QAction (tr ("E&xit"), this);
  212. exitAct->setShortcuts (QKeySequence::Quit);
  213. connect (exitAct, &QAction::triggered, this, &QWidget::close);
  214. automaticLoadingAct = new QAction (tr ("Automatic &Loading Line json file"), this);
  215. automaticLoadingAct->setShortcut (QKeySequence(Qt::CTRL | Qt::Key_L));
  216. automaticLoadingAct->setCheckable(true);
  217. automaticLoadingAct->setChecked(true);
  218. connect (automaticLoadingAct, &QAction::triggered, this, &MainWindow::automaticLoading);
  219. automaticSavingAct = new QAction (tr ("&Automatic saving lines in json file"), this);
  220. automaticSavingAct->setShortcut (QKeySequence(Qt::CTRL | Qt::Key_A));
  221. automaticSavingAct->setCheckable(true);
  222. automaticSavingAct->setChecked(false);
  223. connect (automaticSavingAct, &QAction::triggered, this, &MainWindow::automaticSaving);
  224. readOnlyAct = new QAction (tr ("&Read Only"), this);
  225. readOnlyAct->setShortcut (QKeySequence(Qt::CTRL | Qt::Key_R));
  226. connect (readOnlyAct, &QAction::triggered, this, &MainWindow::readOnly);
  227. firstImageAct = new QAction (tr ("First"), this);
  228. firstImageAct->setShortcuts (QKeySequence::MoveToPreviousLine);
  229. connect (firstImageAct, &QAction::triggered, this, &MainWindow::firstImage);
  230. previousImageAct = new QAction (tr ("Previous"), this);
  231. previousImageAct->setShortcuts (QKeySequence::MoveToPreviousChar);
  232. connect (previousImageAct, &QAction::triggered, this, &MainWindow::previousImage);
  233. nextImageAct = new QAction (tr ("Next"), this);
  234. nextImageAct->setShortcuts (QKeySequence::MoveToNextChar);
  235. connect (nextImageAct, &QAction::triggered, this, &MainWindow::nextImage);
  236. lastImageAct = new QAction (tr ("Last"), this);
  237. lastImageAct->setShortcuts (QKeySequence::MoveToNextLine);
  238. connect (lastImageAct, &QAction::triggered, this, &MainWindow::lastImage);
  239. aboutAct = new QAction (tr ("About"), this);
  240. connect (aboutAct, &QAction::triggered, this, &MainWindow::about);
  241. }
  242. void
  243. MainWindow::createMenus ()
  244. {
  245. fileMenu = menuBar ()->addMenu (tr ("&File"));
  246. fileMenu->addAction (openAct);
  247. fileMenu->addAction (saveAct);
  248. fileMenu->addAction (exportAct);
  249. fileMenu->addSeparator ();
  250. fileMenu->addAction (exitAct);
  251. editMenu = menuBar()->addMenu(tr("&Edit"));
  252. editMenu->addAction (automaticLoadingAct);
  253. editMenu->addAction (automaticSavingAct);
  254. editMenu->addAction (readOnlyAct);
  255. imageMenu = menuBar ()->addMenu (tr ("&Image"));
  256. imageMenu->addAction (firstImageAct);
  257. imageMenu->addAction (previousImageAct);
  258. imageMenu->addAction (nextImageAct);
  259. imageMenu->addAction (lastImageAct);
  260. menuBar()->addAction(aboutAct);
  261. }