Преглед на файлове

New open file management

Rémi Synave преди 2 години
родител
ревизия
8649f6c945
променени са 2 файла, в които са добавени 33 реда и са изтрити 81 реда
  1. 31 76
      dsl/MainWindow.cpp
  2. 2 5
      dsl/MainWindow.hpp

+ 31 - 76
dsl/MainWindow.cpp

@@ -25,7 +25,6 @@ MainWindow::MainWindow ()
   openedImage = -1;
   directoryname = NULL;
   suffix = NULL;
-  filenameToSave = NULL;
   filenames = NULL;
 
   setWindowTitle (tr ("DSL - Draw Strength Line"));
@@ -51,81 +50,44 @@ MainWindow::openFile ()
 						   QObject::tr("Images files (*.jpg *.png);;All files (*.*)"));
 
   if(!filename.isEmpty()) {
-    delete filenames;
-    filenames = new QList < QString* >();
-    delete filenameToSave;
-    filenameToSave = NULL;
-    delete suffix;
-    suffix = NULL;
-    filenames->append(new QString(filename));
-    openedImage = 0;
-    QDir::setCurrent(QString(*(filenames->at(openedImage))).remove(QUrl(*(filenames->at(openedImage))).fileName()));
-    wa->loadImage (filenames->at(openedImage)->toStdString ());
-    wa->repaint();
-    setFixedSize (wa->geometry().width(), wa->geometry().height()+wa->geometry().y());
-  }
-}
-
-void
-MainWindow::openDir ()
-{
-  directoryname = new QString(QFileDialog::getExistingDirectory (this,
-								 QObject::tr ("Open directory"),
-								 QDir::currentPath (),
-								 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks));
-
-  if(!directoryname->isEmpty()) {
+    
+    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 QList < QString* >();
-    
-      QDirIterator it(*directoryname);
-      while (it.hasNext()) {
-	QString next(it.next());
-	if(next.endsWith(".png") || next.endsWith(".jpg"))
-	  filenames->append( new QString(next) );
+    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;
+	  }
+
+	QDir::setCurrent(*directoryname);
+	wa->loadImage (filenames->at(openedImage).toStdString ());
+	wa->repaint();
+	setFixedSize (wa->geometry().width(), wa->geometry().height()+wa->geometry().y());
       }
-    }
   }
-
-  if(filenames->size()>0){
-    delete filenameToSave;
-    filenameToSave = NULL;
-    openedImage = 0;
-    QDir::setCurrent(QString(*(filenames->at(openedImage))).remove(QUrl(*(filenames->at(openedImage))).fileName()));
-    wa->loadImage (filenames->at(openedImage)->toStdString ());
-    wa->repaint();
-    setFixedSize (wa->geometry().width(), wa->geometry().height()+wa->geometry().y());
-  }
-
-  
-
 }
 
 void
 MainWindow::save ()
 {
-  if(suffix != NULL){
-    QString toSave =  QDir::currentPath ().append("/").append(QFileInfo(*(filenames->at(openedImage))).baseName().append(*suffix).append(".json"));
-    wa->saveStrengthLine (toSave.toStdString ());
-  }
-  else{
-    if(filenameToSave == NULL)
-      filenameToSave = new QString(QFileDialog::getSaveFileName (this,
-								 QObject::tr ("Save file"),
-								 QDir::currentPath ().append("/").append(QFileInfo(*(filenames->at(openedImage))).baseName()).append(".json"),
-								 QObject::tr("JSON file (*.json);;All files (*.*)")));
-    if(filenameToSave != NULL){
-      if(!filenameToSave->endsWith(".json"))
-	filenameToSave->append(".json");
-      wa->saveStrengthLine (filenameToSave->toStdString ());
-    }
-  }
+  QString toSave =  QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(*suffix).append(".json"));
+  wa->saveStrengthLine (toSave.toStdString ());
 }
 
 void
@@ -167,9 +129,7 @@ MainWindow::lastImage ()
 void
 MainWindow::loadImage()
 {
-  delete filenameToSave;
-  filenameToSave = NULL;
-  wa->loadImage (filenames->at(openedImage)->toStdString ());
+  wa->loadImage (filenames->at(openedImage).toStdString ());
   wa->repaint();
   setFixedSize (wa->geometry().width(), wa->geometry().height()+wa->geometry().y());
 }
@@ -178,13 +138,9 @@ MainWindow::loadImage()
 void
 MainWindow::createActions ()
 {
-  openFileAct = new QAction (tr ("&Open image"), this);
-  openFileAct->setShortcuts (QKeySequence::Open);
-  connect (openFileAct, &QAction::triggered, this, &MainWindow::openFile);
-
-  openDirAct = new QAction (tr ("&Open directory"), this);
-  openDirAct->setShortcut (QKeySequence(Qt::CTRL, Qt::Key_Alt, Qt::Key_O));
-  connect (openDirAct, &QAction::triggered, this, &MainWindow::openDir);
+  openAct = new QAction (tr ("&Open image"), this);
+  openAct->setShortcuts (QKeySequence::Open);
+  connect (openAct, &QAction::triggered, this, &MainWindow::openFile);
 
   saveAct = new QAction (tr ("&Save"), this);
   saveAct->setShortcuts (QKeySequence::Save);
@@ -215,8 +171,7 @@ void
 MainWindow::createMenus ()
 {
   fileMenu = menuBar ()->addMenu (tr ("&File"));
-  fileMenu->addAction (openFileAct);
-  fileMenu->addAction (openDirAct);
+  fileMenu->addAction (openAct);
   fileMenu->addAction (saveAct);
 
   fileMenu->addSeparator ();

+ 2 - 5
dsl/MainWindow.hpp

@@ -18,8 +18,7 @@ private:
   QMenu *fileMenu;
   QMenu *imageMenu;
 
-  QAction *openFileAct;
-  QAction *openDirAct;
+  QAction *openAct;
   QAction *saveAct;
   QAction *exitAct;
   QAction *firstImageAct;
@@ -29,11 +28,10 @@ private:
 
   WorkArea *wa;
 
-  QList< QString* > *filenames;
+  QStringList *filenames;
   int openedImage;
   QString *directoryname;
   QString *suffix;
-  QString *filenameToSave;
 
   void createActions ();
   void createMenus ();
@@ -44,7 +42,6 @@ private:
 		     
 private slots:
   void openFile ();
-  void openDir ();
   void save ();
   void firstImage ();
   void previousImage ();