MainWindow.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include <QIcon>
  2. #include <QGuiApplication>
  3. #include <QScreen>
  4. #include <QWidget>
  5. #include <QMenuBar>
  6. #include <QFileDialog>
  7. #include <QUrl>
  8. #include <QFileInfo>
  9. #include <QInputDialog>
  10. #include <QDirIterator>
  11. #include <QDebug>
  12. #include <iostream>
  13. #include <filesystem>
  14. #include "MainWindow.hpp"
  15. MainWindow::MainWindow ()
  16. {
  17. setWindowIcon(QIcon("../dsl.ico"));
  18. createActions ();
  19. createMenus ();
  20. openedImage = -1;
  21. directoryname = NULL;
  22. suffix = NULL;
  23. filenames = NULL;
  24. autoLoad = true;
  25. setWindowTitle (tr ("DSL - Draw Strength Line"));
  26. QScreen *screen = QGuiApplication::primaryScreen ();
  27. QRect screenGeometry = screen->geometry ();
  28. int screenWidth = screenGeometry.width () - 200;
  29. int screenHeight = screenGeometry.height () - 200;
  30. wa = new WorkArea (screenWidth, screenHeight, this);
  31. wa->setReadOnly(false);
  32. setCentralWidget (wa);
  33. setFixedSize (screenWidth, screenHeight);
  34. }
  35. void
  36. MainWindow::openFile ()
  37. {
  38. if(this->checkModificationinProgress() == QMessageBox::Yes){
  39. QString filename = QFileDialog::getOpenFileName (this,
  40. QObject::tr ("Open image file"),
  41. QDir::currentPath (),
  42. QObject::tr("Images files (*.jpg *.png);;All files (*.*)"));
  43. if(!filename.isEmpty()) {
  44. directoryname = new QString(QString(filename).remove(QUrl(filename).fileName()));
  45. bool ok;
  46. suffix = new QString(QInputDialog::getText(this, tr("Suffix for your json files"),
  47. tr("Suffix to use :"), QLineEdit::Normal,
  48. "_"+QDir::home().dirName(), &ok));
  49. if(ok)
  50. {
  51. delete filenames;
  52. filenames = new QStringList();
  53. QDirIterator it(*directoryname, {"*.jpg", "*.png"}, QDir::Files);
  54. while (it.hasNext()) {
  55. filenames->append( it.next() );
  56. }
  57. filenames->sort();
  58. for(int i = 0; i <filenames->size() ; i++)
  59. {
  60. if(filename == (filenames->at(i)))
  61. openedImage = i;
  62. }
  63. QDir::setCurrent(*directoryname);
  64. loadImage();
  65. }
  66. }
  67. }
  68. }
  69. void
  70. MainWindow::loadImage()
  71. {
  72. wa->loadImage (filenames->at(openedImage).toStdString ());
  73. if(autoLoad)
  74. {
  75. QString jsonFile(QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(*suffix).append(".json")));
  76. if(QFile(jsonFile).exists()){
  77. wa->loadSL(QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(*suffix).append(".json")).toStdString());
  78. }
  79. }
  80. wa->repaint();
  81. wa->setModificationInProgress(false);
  82. setFixedSize (wa->geometry().width(), wa->geometry().height()+wa->geometry().y());
  83. }
  84. void
  85. MainWindow::save ()
  86. {
  87. QString toSave = QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(*suffix).append(".json"));
  88. wa->saveStrengthLine (toSave.toStdString ());
  89. }
  90. QMessageBox::StandardButton
  91. MainWindow::checkModificationinProgress()
  92. {
  93. if(wa->getModificationInProgress())
  94. return QMessageBox::question(this, "Modification in progress", "Do you really want to change the image ? Modifications in progress will be lost.");
  95. return QMessageBox::Yes;
  96. }
  97. void
  98. MainWindow::firstImage ()
  99. {
  100. if(this->checkModificationinProgress() == QMessageBox::Yes)
  101. if(filenames != NULL)
  102. if(filenames->size() > 1){
  103. openedImage = 0;
  104. loadImage();
  105. }
  106. }
  107. void
  108. MainWindow::previousImage ()
  109. {
  110. if(this->checkModificationinProgress() == QMessageBox::Yes)
  111. if(filenames != NULL)
  112. if(filenames->size() > 1){
  113. openedImage = (openedImage-1+filenames->size())%(filenames->size());
  114. loadImage();
  115. }
  116. }
  117. void
  118. MainWindow::nextImage ()
  119. {
  120. if(this->checkModificationinProgress() == QMessageBox::Yes)
  121. if(filenames != NULL)
  122. if(filenames->size() > 1){
  123. openedImage = (openedImage+1)%(filenames->size());
  124. loadImage();
  125. }
  126. }
  127. void
  128. MainWindow::lastImage ()
  129. {
  130. if(this->checkModificationinProgress() == QMessageBox::Yes)
  131. if(filenames != NULL)
  132. if(filenames->size() > 1){
  133. openedImage = filenames->size()-1;
  134. loadImage();
  135. }
  136. }
  137. void
  138. MainWindow::automaticLoading ()
  139. {
  140. autoLoad = !autoLoad;
  141. automaticLoadingAct->setChecked(!autoLoad);
  142. }
  143. void
  144. MainWindow::readOnly ()
  145. {
  146. if(wa->getReadOnly()){
  147. wa->setReadOnly(false);
  148. readOnlyAct->setText(tr ("W&ritable"));
  149. }else{
  150. wa->setReadOnly(true);
  151. readOnlyAct->setText(tr ("&Read Only"));
  152. }
  153. wa->repaint();
  154. }
  155. void
  156. MainWindow::about (){
  157. QMessageBox::information(this, "blop", "blop");
  158. }
  159. void
  160. MainWindow::createActions ()
  161. {
  162. openAct = new QAction (tr ("&Open image"), this);
  163. openAct->setShortcuts (QKeySequence::Open);
  164. connect (openAct, &QAction::triggered, this, &MainWindow::openFile);
  165. saveAct = new QAction (tr ("&Save"), this);
  166. saveAct->setShortcuts (QKeySequence::Save);
  167. connect (saveAct, &QAction::triggered, this, &MainWindow::save);
  168. exitAct = new QAction (tr ("E&xit"), this);
  169. exitAct->setShortcuts (QKeySequence::Quit);
  170. connect (exitAct, &QAction::triggered, this, &QWidget::close);
  171. automaticLoadingAct = new QAction (tr ("Automatic &Loading Line json file"), this);
  172. automaticLoadingAct->setShortcut (QKeySequence(Qt::CTRL | Qt::Key_L));
  173. automaticLoadingAct->setCheckable(true);
  174. automaticLoadingAct->setChecked(true);
  175. connect (automaticLoadingAct, &QAction::triggered, this, &MainWindow::automaticLoading);
  176. readOnlyAct = new QAction (tr ("&Read Only"), this);
  177. readOnlyAct->setShortcut (QKeySequence(Qt::CTRL | Qt::Key_R));
  178. connect (readOnlyAct, &QAction::triggered, this, &MainWindow::readOnly);
  179. firstImageAct = new QAction (tr ("First"), this);
  180. firstImageAct->setShortcuts (QKeySequence::MoveToPreviousLine);
  181. connect (firstImageAct, &QAction::triggered, this, &MainWindow::firstImage);
  182. previousImageAct = new QAction (tr ("Previous"), this);
  183. previousImageAct->setShortcuts (QKeySequence::MoveToPreviousChar);
  184. connect (previousImageAct, &QAction::triggered, this, &MainWindow::previousImage);
  185. nextImageAct = new QAction (tr ("Next"), this);
  186. nextImageAct->setShortcuts (QKeySequence::MoveToNextChar);
  187. connect (nextImageAct, &QAction::triggered, this, &MainWindow::nextImage);
  188. lastImageAct = new QAction (tr ("Last"), this);
  189. lastImageAct->setShortcuts (QKeySequence::MoveToNextLine);
  190. connect (lastImageAct, &QAction::triggered, this, &MainWindow::lastImage);
  191. aboutAct = new QAction (tr ("About"), this);
  192. connect (aboutAct, &QAction::triggered, this, &MainWindow::about);
  193. }
  194. void
  195. MainWindow::createMenus ()
  196. {
  197. fileMenu = menuBar ()->addMenu (tr ("&File"));
  198. fileMenu->addAction (openAct);
  199. fileMenu->addAction (saveAct);
  200. fileMenu->addSeparator ();
  201. fileMenu->addAction (exitAct);
  202. editMenu = menuBar()->addMenu(tr("&Edit"));
  203. editMenu->addAction (automaticLoadingAct);
  204. editMenu->addAction (readOnlyAct);
  205. imageMenu = menuBar ()->addMenu (tr ("&Image"));
  206. imageMenu->addAction (firstImageAct);
  207. imageMenu->addAction (previousImageAct);
  208. imageMenu->addAction (nextImageAct);
  209. imageMenu->addAction (lastImageAct);
  210. menuBar()->addAction(aboutAct);
  211. }