Sfoglia il codice sorgente

Modification in progress checking before image switch feature added

Rémi Synave 2 anni fa
parent
commit
41010a80ac
4 ha cambiato i file con 88 aggiunte e 46 eliminazioni
  1. 17 0
      WorkArea/WorkArea.cpp
  2. 3 0
      WorkArea/WorkArea.hpp
  3. 66 46
      dsl/MainWindow.cpp
  4. 2 0
      dsl/MainWindow.hpp

+ 17 - 0
WorkArea/WorkArea.cpp

@@ -18,6 +18,7 @@ WorkArea::WorkArea (int screenSizeX, int screenSizeY, QWidget * parent):
   this->screenSizeX = screenSizeX;
   this->screenSizeY = screenSizeY;
   readOnly = false;
+  modificationInProgress = false;
   img = NULL;
   resizeFactor = 1;
   indexPointClicked = -1;
@@ -29,6 +30,7 @@ WorkArea::WorkArea (int screenSizeX, int screenSizeY, const std::string & filena
   this->screenSizeX = screenSizeX;
   this->screenSizeY = screenSizeY;
   readOnly = false;
+  modificationInProgress = false;
   img = NULL;
   resizeFactor = 1;
   indexPointClicked = -1;
@@ -40,6 +42,8 @@ WorkArea::WorkArea (int screenSizeX, int screenSizeY, const std::string & imageF
 {
   this->screenSizeX = screenSizeX;
   this->screenSizeY = screenSizeY;
+  readOnly = false;
+  modificationInProgress = false;
   img = NULL;
   resizeFactor = 1;
   indexPointClicked = -1;
@@ -113,6 +117,8 @@ WorkArea::saveStrengthLine (const std::string & filename)
   QFile fout(QString::fromStdString (filename));
   fout.open(QIODevice::WriteOnly);
   fout.write(ba);
+
+  this->modificationInProgress = false;
 }
 
 void
@@ -125,6 +131,16 @@ WorkArea::getReadOnly(){
   return this->readOnly;
 }
 
+void
+WorkArea::setModificationInProgress(bool mip){
+  this->modificationInProgress = mip;
+}
+
+bool
+WorkArea::getModificationInProgress(){
+  return this->modificationInProgress;
+}
+
 
 void
 WorkArea::paintEvent (QPaintEvent * event)
@@ -171,6 +187,7 @@ WorkArea::mousePressEvent (QMouseEvent * event)
     {
       if (img != NULL)
 	{
+	  this->modificationInProgress = true;
 	  // Est ce qu'on a cliqué sur un point existant ?
 	  indexPointClicked = -1;
 	  for (int i = 0; i < liste_points.length (); i++)

+ 3 - 0
WorkArea/WorkArea.hpp

@@ -23,6 +23,7 @@ private:
   
   unsigned int screenSizeX, screenSizeY;
   bool readOnly;
+  bool modificationInProgress;
   
   QImage *img;
   double resizeFactor;
@@ -51,6 +52,8 @@ public:
   void saveStrengthLine (const std::string & filename);
   void setReadOnly(bool readOnly);
   bool getReadOnly();
+  void setModificationInProgress(bool mip);
+  bool getModificationInProgress();
 
 };
 

+ 66 - 46
dsl/MainWindow.cpp

@@ -26,7 +26,7 @@ MainWindow::MainWindow ()
   directoryname = NULL;
   suffix = NULL;
   filenames = NULL;
-  autoLoad = false;
+  autoLoad = true;
 
   setWindowTitle (tr ("DSL - Draw Strength Line"));
 
@@ -45,40 +45,42 @@ MainWindow::MainWindow ()
 void
 MainWindow::openFile ()
 {
-  QString filename = QFileDialog::getOpenFileName (this,
-						   QObject::tr ("Open image file"),
-						   QDir::currentPath (),
-						   QObject::tr("Images files (*.jpg *.png);;All files (*.*)"));
+  if(this->checkModificationinProgress() == QMessageBox::Yes){
+    QString filename = QFileDialog::getOpenFileName (this,
+						     QObject::tr ("Open image file"),
+						     QDir::currentPath (),
+						     QObject::tr("Images files (*.jpg *.png);;All files (*.*)"));
 
-  if(!filename.isEmpty()) {
+    if(!filename.isEmpty()) {
     
-    directoryname = new QString(QString(filename).remove(QUrl(filename).fileName()));
+      directoryname = new QString(QString(filename).remove(QUrl(filename).fileName()));
     
-    bool ok;
-    suffix = new QString(QInputDialog::getText(this, tr("Suffix for your json files"),
-					       tr("Suffix to use :"), QLineEdit::Normal,
-					       "_"+QDir::home().dirName(), &ok));
-
-    if(ok)
-      {
-	delete filenames;
-	filenames = new QStringList();
+      bool ok;
+      suffix = new QString(QInputDialog::getText(this, tr("Suffix for your json files"),
+						 tr("Suffix to use :"), QLineEdit::Normal,
+						 "_"+QDir::home().dirName(), &ok));
+
+      if(ok)
+	{
+	  delete filenames;
+	  filenames = new QStringList();
 	
-	QDirIterator it(*directoryname, {"*.jpg", "*.png"}, QDir::Files);
-	while (it.hasNext()) {
-	  filenames->append( it.next() );
-	}
-	filenames->sort();
-	
-	for(int i = 0; i <filenames->size() ; i++)
-	  {
-	    if(filename == (filenames->at(i)))
-	      openedImage = i;
+	  QDirIterator it(*directoryname, {"*.jpg", "*.png"}, QDir::Files);
+	  while (it.hasNext()) {
+	    filenames->append( it.next() );
 	  }
-
-	QDir::setCurrent(*directoryname);
-	loadImage();
-      }
+	  filenames->sort();
+	
+	  for(int i = 0; i <filenames->size() ; i++)
+	    {
+	      if(filename == (filenames->at(i)))
+		openedImage = i;
+	    }
+
+	  QDir::setCurrent(*directoryname);
+	  loadImage();
+	}
+    }
   }
 }
 
@@ -94,6 +96,7 @@ MainWindow::loadImage()
       }
     }
   wa->repaint();
+  wa->setModificationInProgress(false);
   setFixedSize (wa->geometry().width(), wa->geometry().height()+wa->geometry().y());
 }
 
@@ -104,40 +107,56 @@ MainWindow::save ()
   wa->saveStrengthLine (toSave.toStdString ());
 }
 
+QMessageBox::StandardButton
+MainWindow::checkModificationinProgress()
+{
+  if(wa->getModificationInProgress())
+    return QMessageBox::question(this, "Modification in progress", "Do you really want to change the image ? Modifications in progress will be lost.");
+  return QMessageBox::Yes;
+}
+
 void
 MainWindow::firstImage ()
 {
-  if(filenames->size() > 1){
-    openedImage = 0;
-    loadImage();
-  }
+  if(this->checkModificationinProgress() == QMessageBox::Yes)
+    if(filenames != NULL)
+      if(filenames->size() > 1){
+	openedImage = 0;
+	loadImage();
+      }
 }
 
 void
 MainWindow::previousImage ()
 {
-  if(filenames->size() > 1){
-    openedImage = (openedImage-1+filenames->size())%(filenames->size());
-    loadImage();
-  }
+  if(this->checkModificationinProgress() == QMessageBox::Yes)
+    if(filenames != NULL)
+      if(filenames->size() > 1){
+	openedImage = (openedImage-1+filenames->size())%(filenames->size());
+	loadImage();
+      }
 }
 
 void
 MainWindow::nextImage ()
 {
-  if(filenames->size() > 1){
-    openedImage = (openedImage+1)%(filenames->size());
-    loadImage();
-  }
+  if(this->checkModificationinProgress() == QMessageBox::Yes)
+    if(filenames != NULL)
+      if(filenames->size() > 1){
+	openedImage = (openedImage+1)%(filenames->size());
+	loadImage();
+      }
 }
 
 void
 MainWindow::lastImage ()
 {
-  if(filenames->size() > 1){
-    openedImage = filenames->size()-1;
-    loadImage();
-  }
+  if(this->checkModificationinProgress() == QMessageBox::Yes)
+    if(filenames != NULL)
+      if(filenames->size() > 1){
+	openedImage = filenames->size()-1;
+	loadImage();
+      }
 }
 
 void
@@ -180,6 +199,7 @@ MainWindow::createActions ()
   automaticLoadingAct = new QAction (tr ("Automatic &Loading Line json file"), this);
   automaticLoadingAct->setShortcut (QKeySequence(Qt::CTRL | Qt::Key_L));
   automaticLoadingAct->setCheckable(true);
+  automaticLoadingAct->setChecked(true);
   connect (automaticLoadingAct, &QAction::triggered, this, &MainWindow::automaticLoading);
 
   readOnlyAct = new QAction (tr ("&Read Only"), this);

+ 2 - 0
dsl/MainWindow.hpp

@@ -5,6 +5,7 @@
 #include <QAction>
 #include <QMenu>
 #include <QString>
+#include <QMessageBox>
 
 #include "WorkArea.hpp"
 
@@ -40,6 +41,7 @@ private:
   void createActions ();
   void createMenus ();
   void loadImage ();
+  QMessageBox::StandardButton checkModificationinProgress();