diff --git a/glabels/File.cpp b/glabels/File.cpp index 1e65f59..71fa160 100644 --- a/glabels/File.cpp +++ b/glabels/File.cpp @@ -26,6 +26,7 @@ #include "model/FileUtil.h" #include "model/Model.h" +#include "model/Settings.h" #include "model/XmlLabelParser.h" #include "model/XmlLabelCreator.h" @@ -124,6 +125,47 @@ namespace glabels newWindow->setModel( model ); newWindow->show(); } + model::Settings::addToRecentFileList( fileName ); + + // Save CWD + mCwd = QFileInfo( fileName ).absolutePath(); + } + else + { + QMessageBox msgBox; + msgBox.setText( tr("Unable to open \"") + fileName + tr("\".") ); + msgBox.setStandardButtons( QMessageBox::Ok ); + msgBox.setDefaultButton( QMessageBox::Ok ); + msgBox.exec(); + } + } + } + + + /// + /// Open file + /// + void File::open( const QString& fileName, MainWindow *window ) + { + if ( !fileName.isEmpty() ) + { + model::Model *model = model::XmlLabelParser::readFile( fileName ); + if ( model ) + { + model->setFileName( fileName ); + + // Either apply to current window or open a new one + if ( window->isEmpty() ) + { + window->setModel( model ); + } + else + { + auto *newWindow = new MainWindow(); + newWindow->setModel( model ); + newWindow->show(); + } + model::Settings::addToRecentFileList( fileName ); // Save CWD mCwd = QFileInfo( fileName ).absolutePath(); @@ -157,6 +199,7 @@ namespace glabels model::XmlLabelCreator::writeFile( window->model(), window->model()->fileName() ); window->model()->clearModified(); + model::Settings::addToRecentFileList( window->model()->fileName() ); // Save CWD mCwd = QFileInfo( window->model()->fileName() ).absolutePath(); @@ -212,6 +255,7 @@ namespace glabels model::XmlLabelCreator::writeFile( window->model(), fileName ); window->model()->setFileName( fileName ); window->model()->clearModified(); + model::Settings::addToRecentFileList( fileName ); // Save CWD mCwd = QFileInfo( fileName ).absolutePath(); diff --git a/glabels/File.h b/glabels/File.h index f151b04..0dddf52 100644 --- a/glabels/File.h +++ b/glabels/File.h @@ -44,6 +44,7 @@ namespace glabels public: static bool newLabel( MainWindow *window = nullptr ); static void open( MainWindow *window ); + static void open( const QString& fileName, MainWindow *window ); static bool save( MainWindow *window ); static bool saveAs( MainWindow *window ); static void templateDesigner( MainWindow *window ); diff --git a/glabels/MainWindow.cpp b/glabels/MainWindow.cpp index 0ad77b2..93aa47c 100644 --- a/glabels/MainWindow.cpp +++ b/glabels/MainWindow.cpp @@ -177,6 +177,7 @@ namespace glabels connect( mMergeButton, SIGNAL(toggled(bool)), this, SLOT(changePage(bool))); connect( mPrintButton, SIGNAL(toggled(bool)), this, SLOT(changePage(bool))); connect( mLabelEditor, SIGNAL(zoomChanged()), this, SLOT(onZoomChanged()) ); + connect( model::Settings::instance(), SIGNAL(changed()), this, SLOT(onSettingsChanged()) ); connect( QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardChanged()) ); #if 0 connect( mLabelEditor, SIGNAL(pointerMoved(double, double)), @@ -279,6 +280,14 @@ namespace glabels fileOpenAction->setStatusTip( tr("Open an existing gLabels project") ); connect( fileOpenAction, SIGNAL(triggered()), this, SLOT(fileOpen()) ); + for ( int i = 0; i < model::Settings::maxRecentFiles(); i++ ) + { + auto* action = new QAction( this ); + action->setVisible( false ); + fileRecentActionList.append( action ); + connect( action, SIGNAL(triggered()), this, SLOT(fileOpenRecent()) ); + } + fileSaveAction = new QAction( tr("&Save"), this ); fileSaveAction->setIcon( Icons::FileSave() ); fileSaveAction->setShortcut( QKeySequence::Save ); @@ -583,6 +592,11 @@ namespace glabels fileMenu = menuBar()->addMenu( tr("&File") ); fileMenu->addAction( fileNewAction ); fileMenu->addAction( fileOpenAction ); + fileRecentMenu = fileMenu->addMenu( tr("Open Recent") ); + for ( auto* action : fileRecentActionList ) + { + fileRecentMenu->addAction( action ); + } fileMenu->addAction( fileSaveAction ); fileMenu->addAction( fileSaveAsAction ); fileMenu->addSeparator(); @@ -838,9 +852,24 @@ namespace glabels mMergeAction->setVisible( !isWelcomePage ); mPrintAction->setVisible( !isWelcomePage ); + // Recent file actions + QStringList recentFileList = model::Settings::recentFileList(); + for ( int i = 0; i < recentFileList.size(); i++ ) + { + QString baseName = QFileInfo( recentFileList.at(i) ).completeBaseName(); + fileRecentActionList.at(i)->setText(baseName); + fileRecentActionList.at(i)->setData(recentFileList.at(i)); + fileRecentActionList.at(i)->setVisible( true ); + } + for ( int i = recentFileList.size(); i < model::Settings::maxRecentFiles(); i++ ) + { + fileRecentActionList.at(i)->setVisible( false ); + } + // File actions fileNewAction->setEnabled( true ); fileOpenAction->setEnabled( true ); + fileRecentMenu->setEnabled( !recentFileList.isEmpty() ); fileSaveAction->setEnabled( hasModel && mModel->isModified() ); fileSaveAsAction->setEnabled( hasModel ); fileShowEditorPageAction->setEnabled( !isWelcomePage && !isEditorPage ); @@ -1079,6 +1108,20 @@ namespace glabels } + /// + /// File->OpenRecent Action + /// + void MainWindow::fileOpenRecent() + { + QAction* action = qobject_cast( sender() ); + if ( action ) + { + QString fileName = action->data().toString(); + File::open( fileName, this ); + } + } + + /// /// File->Save Action /// @@ -1646,4 +1689,13 @@ namespace glabels manageActions(); } + + /// + /// Settings changed handler + /// + void MainWindow::onSettingsChanged() + { + manageActions(); + } + } // namespace glabels diff --git a/glabels/MainWindow.h b/glabels/MainWindow.h index 64abe5d..dc6a2a2 100644 --- a/glabels/MainWindow.h +++ b/glabels/MainWindow.h @@ -91,6 +91,7 @@ namespace glabels void fileNew(); void fileOpen(); + void fileOpenRecent(); void fileSave(); void fileSaveAs(); void fileShowEditorPage(); @@ -158,6 +159,8 @@ namespace glabels void onLabelChanged(); void onUndoRedoChanged(); + void onSettingsChanged(); + ///////////////////////////////////// // Internal Private Methods @@ -182,13 +185,14 @@ namespace glabels void writeSettings(); bool isOkToClose(); - + ///////////////////////////////////// // Private Data ///////////////////////////////////// private: QMenu* fileMenu; + QMenu* fileRecentMenu; QMenu* editMenu; QMenu* viewMenu; QMenu* viewToolBarsMenu; @@ -250,6 +254,8 @@ namespace glabels QAction* fileCloseAction; QAction* fileExitAction; + QList fileRecentActionList; + QAction* editUndoAction; QAction* editRedoAction; QAction* editCutAction; diff --git a/model/Settings.cpp b/model/Settings.cpp index b0ac10e..db9288e 100644 --- a/model/Settings.cpp +++ b/model/Settings.cpp @@ -294,5 +294,44 @@ namespace glabels emit mInstance->changed(); } + + int Settings::maxRecentFiles() + { + return mMaxRecentFiles; + } + + + QStringList Settings::recentFileList() + { + QStringList defaultList; + + mInstance->beginGroup( "Recent" ); + QStringList returnList = mInstance->value( "files", defaultList ).toStringList(); + mInstance->endGroup(); + + return returnList; + } + + + void Settings::addToRecentFileList( const QString& filePath ) + { + mInstance->beginGroup( "Recent" ); + + QStringList list = mInstance->value( "files" ).toStringList(); + + list.removeAll( filePath ); + list.prepend( filePath ); + while ( list.count() > mMaxRecentFiles ) + { + list.removeLast(); + } + + mInstance->setValue( "files", list ); + + mInstance->endGroup(); + + emit mInstance->changed(); + } + } } diff --git a/model/Settings.h b/model/Settings.h index f16c4f0..44178a5 100644 --- a/model/Settings.h +++ b/model/Settings.h @@ -90,9 +90,14 @@ namespace glabels static QStringList recentTemplateList(); static void addToRecentTemplateList( const QString& name ); + static int maxRecentFiles(); + static QStringList recentFileList(); + static void addToRecentFileList( const QString& filePath ); + private: static Settings* mInstance; + static const int mMaxRecentFiles{5}; }; diff --git a/translations/glabels_C.ts b/translations/glabels_C.ts index a850f82..e8b655c 100644 --- a/translations/glabels_C.ts +++ b/translations/glabels_C.ts @@ -655,11 +655,11 @@ - &Launch Bug Report Webpage + Ctrl+C - Ctrl+C + &Launch Issue Tracker @@ -1732,6 +1732,10 @@ &User Manual... + + Open Recent + + glabels::MergeView