From 602e3f9ab6a2f5b45ff912d6341efb794da6ea9a Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Fri, 6 Jan 2017 04:44:29 -0500 Subject: [PATCH] Intelligently choose CWD in open and saveAs dialogs. --- CODING-STYLE.md | 7 +++++- glabels/File.cpp | 55 +++++++++++++++++++++++++++++++++++------------- glabels/File.h | 9 ++++++-- 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/CODING-STYLE.md b/CODING-STYLE.md index 9bb334d..ec07ead 100644 --- a/CODING-STYLE.md +++ b/CODING-STYLE.md @@ -1,3 +1,6 @@ +Glabels Coding Style +==================== + Tabs vs. Spaces --------------- @@ -13,7 +16,7 @@ See https://www.emacswiki.org/emacs/SmartTabs for more information. Indentation Style ----------------- -Glabels code uses the Allman (a.k.a "BSD Style") of code indentation. I.e. the brace associated with a +Glabels code uses the Allman style (a.k.a "BSD Style") of code indentation. I.e. the brace associated with a control statement is placed on the next line, indented to the same level as the control statement. Statements within the braces are indented to the next level. @@ -34,4 +37,6 @@ else } ``` +Also applies to class and namespace declaration statements. + See https://en.wikipedia.org/wiki/Indent_style#Allman_style for more information. diff --git a/glabels/File.cpp b/glabels/File.cpp index 9cc41d9..ead2b38 100644 --- a/glabels/File.cpp +++ b/glabels/File.cpp @@ -31,7 +31,11 @@ #include -/// @TODO keep track of cwd between open/save dialogs +/// +/// Static data +/// +QString File::mCwd = "."; + /// /// New Label Dialog @@ -78,10 +82,21 @@ bool File::newLabel( MainWindow *window ) /// void File::open( MainWindow *window ) { + // Either use the saved CWD from a previous open/save or grab it from the path of the current file + QString cwd = mCwd; + if ( window->model() && !window->model()->fileName().isEmpty() ) + { + QFileInfo fileInfo( window->model()->fileName() ); + if ( fileInfo.isFile() ) + { + cwd = fileInfo.absolutePath(); + } + } + QString fileName = QFileDialog::getOpenFileName( window, tr("gLabels - Open Project"), - ".", + cwd, tr("glabels files (*.glabels);;All files (*)") ); if ( !fileName.isEmpty() ) @@ -102,6 +117,9 @@ void File::open( MainWindow *window ) newWindow->setModel( label ); newWindow->show(); } + + // Save CWD + mCwd = QFileInfo( fileName ).absolutePath(); } else { @@ -132,7 +150,10 @@ bool File::save( MainWindow *window ) XmlLabelCreator::writeFile( window->model(), window->model()->fileName() ); window->model()->clearModified(); - + + // Save CWD + mCwd = QFileInfo( window->model()->fileName() ).absolutePath(); + return true; } @@ -142,14 +163,24 @@ bool File::save( MainWindow *window ) /// bool File::saveAs( MainWindow *window ) { + // Either use the saved CWD from a previous open/save or grab it from the path of the current file + QString cwd = mCwd; + if ( window->model() && !window->model()->fileName().isEmpty() ) + { + QFileInfo fileInfo( window->model()->fileName() ); + if ( fileInfo.isFile() ) + { + cwd = fileInfo.filePath(); + } + } + QString rawFileName = QFileDialog::getSaveFileName( window, tr("gLabels - Save Project As"), - ".", + cwd, tr("glabels files (*.glabels);;All files (*)"), 0, - QFileDialog::DontConfirmOverwrite - ); + QFileDialog::DontConfirmOverwrite ); if ( !rawFileName.isEmpty() ) { QString fileName = FileUtil::addExtension( rawFileName, ".glabels" ); @@ -175,6 +206,9 @@ bool File::saveAs( MainWindow *window ) window->model()->setFileName( fileName ); window->model()->clearModified(); + // Save CWD + mCwd = QFileInfo( fileName ).absolutePath(); + return true; } @@ -182,15 +216,6 @@ bool File::saveAs( MainWindow *window ) } -/// -/// Print file -/// -void File::print( MainWindow *window ) -{ - qDebug() << "ACTION: file->print"; -} - - /// /// Close file /// diff --git a/glabels/File.h b/glabels/File.h index aa562c9..f913141 100644 --- a/glabels/File.h +++ b/glabels/File.h @@ -1,6 +1,6 @@ /* File.h * - * Copyright (C) 2014 Jim Evins + * Copyright (C) 2017 Jim Evins * * This file is part of gLabels-qt. * @@ -31,6 +31,8 @@ class MainWindow; /// /// File Actions /// +/// Note: class provides a translation context for these static functions. +/// class File : public QObject { Q_OBJECT @@ -40,9 +42,12 @@ public: static void open( MainWindow *window ); static bool save( MainWindow *window ); static bool saveAs( MainWindow *window ); - static void print( MainWindow *window ); static void close( MainWindow *window ); static void exit(); + +private: + static QString mCwd; + };