MainWindow.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. void
  120. MainWindow::exportJPG ()
  121. {
  122. QString toSave = QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(*suffix).append(".jpg"));
  123. wa->exportPNG (toSave.toStdString ());
  124. }
  125. QMessageBox::StandardButton
  126. MainWindow::checkModificationinProgress()
  127. {
  128. if(wa->getModificationInProgress())
  129. {
  130. if(autoSave)
  131. this->save();
  132. else
  133. return QMessageBox::question(this, "Modification in progress", "Do you really want to change the image ? Modifications in progress will be lost.");
  134. }
  135. return QMessageBox::Yes;
  136. }
  137. void
  138. MainWindow::firstImage ()
  139. {
  140. if(filenames != NULL)
  141. if(filenames->size() > 1)
  142. if(this->checkModificationinProgress() == QMessageBox::Yes){
  143. openedImage = 0;
  144. loadImage();
  145. }
  146. }
  147. void
  148. MainWindow::previousImage ()
  149. {
  150. if(filenames != NULL)
  151. if(filenames->size() > 1)
  152. if(this->checkModificationinProgress() == QMessageBox::Yes){
  153. openedImage = (openedImage-1+filenames->size())%(filenames->size());
  154. loadImage();
  155. }
  156. }
  157. void
  158. MainWindow::nextImage ()
  159. {
  160. if(filenames != NULL)
  161. if(filenames->size() > 1)
  162. if(this->checkModificationinProgress() == QMessageBox::Yes){
  163. openedImage = (openedImage+1)%(filenames->size());
  164. loadImage();
  165. }
  166. }
  167. void
  168. MainWindow::lastImage ()
  169. {
  170. if(filenames != NULL)
  171. if(filenames->size() > 1)
  172. if(this->checkModificationinProgress() == QMessageBox::Yes){
  173. openedImage = filenames->size()-1;
  174. loadImage();
  175. }
  176. }
  177. void
  178. MainWindow::automaticLoading ()
  179. {
  180. autoLoad = !autoLoad;
  181. automaticLoadingAct->setChecked(!autoLoad);
  182. }
  183. void
  184. MainWindow::automaticSaving ()
  185. {
  186. autoSave = !autoSave;
  187. automaticSavingAct->setChecked(!autoSave);
  188. }
  189. void
  190. MainWindow::readOnly ()
  191. {
  192. if(wa->getReadOnly()){
  193. wa->setReadOnly(false);
  194. readOnlyAct->setText(tr ("W&ritable"));
  195. }else{
  196. wa->setReadOnly(true);
  197. readOnlyAct->setText(tr ("&Read Only"));
  198. }
  199. wa->repaint();
  200. }
  201. void
  202. MainWindow::about (){
  203. 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");
  204. }
  205. void
  206. MainWindow::createActions ()
  207. {
  208. openAct = new QAction (tr ("&Open image"), this);
  209. openAct->setShortcuts (QKeySequence::Open);
  210. connect (openAct, &QAction::triggered, this, &MainWindow::openFile);
  211. saveAct = new QAction (tr ("&Save"), this);
  212. saveAct->setShortcuts (QKeySequence::Save);
  213. connect (saveAct, &QAction::triggered, this, &MainWindow::save);
  214. exportAct = new QAction (tr ("&Export in PNG"), this);
  215. exportAct->setShortcut (QKeySequence(Qt::CTRL | Qt::Key_E));
  216. connect (exportAct, &QAction::triggered, this, &MainWindow::exportPNG);
  217. exitAct = new QAction (tr ("E&xit"), this);
  218. exitAct->setShortcuts (QKeySequence::Quit);
  219. connect (exitAct, &QAction::triggered, this, &QWidget::close);
  220. automaticLoadingAct = new QAction (tr ("Automatic &Loading Line json file"), this);
  221. automaticLoadingAct->setShortcut (QKeySequence(Qt::CTRL | Qt::Key_L));
  222. automaticLoadingAct->setCheckable(true);
  223. automaticLoadingAct->setChecked(true);
  224. connect (automaticLoadingAct, &QAction::triggered, this, &MainWindow::automaticLoading);
  225. automaticSavingAct = new QAction (tr ("&Automatic saving lines in json file"), this);
  226. automaticSavingAct->setShortcut (QKeySequence(Qt::CTRL | Qt::Key_A));
  227. automaticSavingAct->setCheckable(true);
  228. automaticSavingAct->setChecked(false);
  229. connect (automaticSavingAct, &QAction::triggered, this, &MainWindow::automaticSaving);
  230. readOnlyAct = new QAction (tr ("&Read Only"), this);
  231. readOnlyAct->setShortcut (QKeySequence(Qt::CTRL | Qt::Key_R));
  232. connect (readOnlyAct, &QAction::triggered, this, &MainWindow::readOnly);
  233. firstImageAct = new QAction (tr ("First"), this);
  234. firstImageAct->setShortcuts (QKeySequence::MoveToPreviousLine);
  235. connect (firstImageAct, &QAction::triggered, this, &MainWindow::firstImage);
  236. previousImageAct = new QAction (tr ("Previous"), this);
  237. previousImageAct->setShortcuts (QKeySequence::MoveToPreviousChar);
  238. connect (previousImageAct, &QAction::triggered, this, &MainWindow::previousImage);
  239. nextImageAct = new QAction (tr ("Next"), this);
  240. nextImageAct->setShortcuts (QKeySequence::MoveToNextChar);
  241. connect (nextImageAct, &QAction::triggered, this, &MainWindow::nextImage);
  242. lastImageAct = new QAction (tr ("Last"), this);
  243. lastImageAct->setShortcuts (QKeySequence::MoveToNextLine);
  244. connect (lastImageAct, &QAction::triggered, this, &MainWindow::lastImage);
  245. aboutAct = new QAction (tr ("About"), this);
  246. connect (aboutAct, &QAction::triggered, this, &MainWindow::about);
  247. }
  248. void
  249. MainWindow::createMenus ()
  250. {
  251. fileMenu = menuBar ()->addMenu (tr ("&File"));
  252. fileMenu->addAction (openAct);
  253. fileMenu->addAction (saveAct);
  254. fileMenu->addAction (exportAct);
  255. fileMenu->addSeparator ();
  256. fileMenu->addAction (exitAct);
  257. editMenu = menuBar()->addMenu(tr("&Edit"));
  258. editMenu->addAction (automaticLoadingAct);
  259. editMenu->addAction (automaticSavingAct);
  260. editMenu->addAction (readOnlyAct);
  261. imageMenu = menuBar ()->addMenu (tr ("&Image"));
  262. imageMenu->addAction (firstImageAct);
  263. imageMenu->addAction (previousImageAct);
  264. imageMenu->addAction (nextImageAct);
  265. imageMenu->addAction (lastImageAct);
  266. menuBar()->addAction(aboutAct);
  267. }