MainWindow.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. setWindowTitle (tr ("DSL - Draw Strength Line"));
  48. QScreen *screen = QGuiApplication::primaryScreen ();
  49. QRect screenGeometry = screen->geometry ();
  50. int screenWidth = screenGeometry.width () - 200;
  51. int screenHeight = screenGeometry.height () - 200;
  52. wa = new WorkArea (screenWidth, screenHeight, this);
  53. wa->setReadOnly(false);
  54. setCentralWidget (wa);
  55. setFixedSize (screenWidth, screenHeight);
  56. }
  57. void
  58. MainWindow::openFile ()
  59. {
  60. if(this->checkModificationinProgress() == QMessageBox::Yes){
  61. QString filename = QFileDialog::getOpenFileName (this,
  62. QObject::tr ("Open image file"),
  63. QDir::currentPath (),
  64. QObject::tr("Images files (*.jpg);;All files (*.*)"));
  65. if(!filename.isEmpty()) {
  66. directoryname = new QString(QString(filename).remove(QUrl(filename).fileName()));
  67. bool ok;
  68. suffix = new QString(QInputDialog::getText(this, tr("Suffix for your json files"),
  69. tr("Suffix to use :"), QLineEdit::Normal,
  70. "_"+QDir::home().dirName(), &ok));
  71. if(ok)
  72. {
  73. delete filenames;
  74. filenames = new QStringList();
  75. QDirIterator it(*directoryname, {"*.jpg"}, QDir::Files);
  76. while (it.hasNext()) {
  77. filenames->append( it.next() );
  78. }
  79. filenames->sort();
  80. for(int i = 0; i <filenames->size() ; i++)
  81. {
  82. if(filename == (filenames->at(i)))
  83. openedImage = i;
  84. }
  85. QDir::setCurrent(*directoryname);
  86. loadImage();
  87. }
  88. }
  89. }
  90. }
  91. void
  92. MainWindow::loadImage()
  93. {
  94. wa->loadImage (filenames->at(openedImage).toStdString ());
  95. if(autoLoad)
  96. {
  97. QString jsonFile(QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(*suffix).append(".json")));
  98. if(QFile(jsonFile).exists()){
  99. wa->loadSL(QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(*suffix).append(".json")).toStdString());
  100. }
  101. }
  102. wa->repaint();
  103. wa->setModificationInProgress(false);
  104. setFixedSize (wa->geometry().width(), wa->geometry().height()+wa->geometry().y());
  105. }
  106. void
  107. MainWindow::save ()
  108. {
  109. QString toSave = QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(*suffix).append(".json"));
  110. wa->saveStrengthLine (toSave.toStdString ());
  111. }
  112. void
  113. MainWindow::exportPNG ()
  114. {
  115. QString toSave = QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(*suffix).append(".png"));
  116. wa->exportPNG (toSave.toStdString ());
  117. }
  118. QMessageBox::StandardButton
  119. MainWindow::checkModificationinProgress()
  120. {
  121. if(wa->getModificationInProgress())
  122. return QMessageBox::question(this, "Modification in progress", "Do you really want to change the image ? Modifications in progress will be lost.");
  123. return QMessageBox::Yes;
  124. }
  125. void
  126. MainWindow::firstImage ()
  127. {
  128. if(filenames != NULL)
  129. if(filenames->size() > 1)
  130. if(this->checkModificationinProgress() == QMessageBox::Yes){
  131. openedImage = 0;
  132. loadImage();
  133. }
  134. }
  135. void
  136. MainWindow::previousImage ()
  137. {
  138. if(filenames != NULL)
  139. if(filenames->size() > 1)
  140. if(this->checkModificationinProgress() == QMessageBox::Yes){
  141. openedImage = (openedImage-1+filenames->size())%(filenames->size());
  142. loadImage();
  143. }
  144. }
  145. void
  146. MainWindow::nextImage ()
  147. {
  148. if(filenames != NULL)
  149. if(filenames->size() > 1)
  150. if(this->checkModificationinProgress() == QMessageBox::Yes){
  151. openedImage = (openedImage+1)%(filenames->size());
  152. loadImage();
  153. }
  154. }
  155. void
  156. MainWindow::lastImage ()
  157. {
  158. if(filenames != NULL)
  159. if(filenames->size() > 1)
  160. if(this->checkModificationinProgress() == QMessageBox::Yes){
  161. openedImage = filenames->size()-1;
  162. loadImage();
  163. }
  164. }
  165. void
  166. MainWindow::automaticLoading ()
  167. {
  168. autoLoad = !autoLoad;
  169. automaticLoadingAct->setChecked(!autoLoad);
  170. }
  171. void
  172. MainWindow::readOnly ()
  173. {
  174. if(wa->getReadOnly()){
  175. wa->setReadOnly(false);
  176. readOnlyAct->setText(tr ("W&ritable"));
  177. }else{
  178. wa->setReadOnly(true);
  179. readOnlyAct->setText(tr ("&Read Only"));
  180. }
  181. wa->repaint();
  182. }
  183. void
  184. MainWindow::about (){
  185. 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");
  186. }
  187. void
  188. MainWindow::createActions ()
  189. {
  190. openAct = new QAction (tr ("&Open image"), this);
  191. openAct->setShortcuts (QKeySequence::Open);
  192. connect (openAct, &QAction::triggered, this, &MainWindow::openFile);
  193. saveAct = new QAction (tr ("&Save"), this);
  194. saveAct->setShortcuts (QKeySequence::Save);
  195. connect (saveAct, &QAction::triggered, this, &MainWindow::save);
  196. exportAct = new QAction (tr ("&Export in PNG"), this);
  197. exportAct->setShortcut (QKeySequence(Qt::CTRL | Qt::Key_E));
  198. connect (exportAct, &QAction::triggered, this, &MainWindow::exportPNG);
  199. exitAct = new QAction (tr ("E&xit"), this);
  200. exitAct->setShortcuts (QKeySequence::Quit);
  201. connect (exitAct, &QAction::triggered, this, &QWidget::close);
  202. automaticLoadingAct = new QAction (tr ("Automatic &Loading Line json file"), this);
  203. automaticLoadingAct->setShortcut (QKeySequence(Qt::CTRL | Qt::Key_L));
  204. automaticLoadingAct->setCheckable(true);
  205. automaticLoadingAct->setChecked(true);
  206. connect (automaticLoadingAct, &QAction::triggered, this, &MainWindow::automaticLoading);
  207. readOnlyAct = new QAction (tr ("&Read Only"), this);
  208. readOnlyAct->setShortcut (QKeySequence(Qt::CTRL | Qt::Key_R));
  209. connect (readOnlyAct, &QAction::triggered, this, &MainWindow::readOnly);
  210. firstImageAct = new QAction (tr ("First"), this);
  211. firstImageAct->setShortcuts (QKeySequence::MoveToPreviousLine);
  212. connect (firstImageAct, &QAction::triggered, this, &MainWindow::firstImage);
  213. previousImageAct = new QAction (tr ("Previous"), this);
  214. previousImageAct->setShortcuts (QKeySequence::MoveToPreviousChar);
  215. connect (previousImageAct, &QAction::triggered, this, &MainWindow::previousImage);
  216. nextImageAct = new QAction (tr ("Next"), this);
  217. nextImageAct->setShortcuts (QKeySequence::MoveToNextChar);
  218. connect (nextImageAct, &QAction::triggered, this, &MainWindow::nextImage);
  219. lastImageAct = new QAction (tr ("Last"), this);
  220. lastImageAct->setShortcuts (QKeySequence::MoveToNextLine);
  221. connect (lastImageAct, &QAction::triggered, this, &MainWindow::lastImage);
  222. aboutAct = new QAction (tr ("About"), this);
  223. connect (aboutAct, &QAction::triggered, this, &MainWindow::about);
  224. }
  225. void
  226. MainWindow::createMenus ()
  227. {
  228. fileMenu = menuBar ()->addMenu (tr ("&File"));
  229. fileMenu->addAction (openAct);
  230. fileMenu->addAction (saveAct);
  231. fileMenu->addAction (exportAct);
  232. fileMenu->addSeparator ();
  233. fileMenu->addAction (exitAct);
  234. editMenu = menuBar()->addMenu(tr("&Edit"));
  235. editMenu->addAction (automaticLoadingAct);
  236. editMenu->addAction (readOnlyAct);
  237. imageMenu = menuBar ()->addMenu (tr ("&Image"));
  238. imageMenu->addAction (firstImageAct);
  239. imageMenu->addAction (previousImageAct);
  240. imageMenu->addAction (nextImageAct);
  241. imageMenu->addAction (lastImageAct);
  242. menuBar()->addAction(aboutAct);
  243. }