Added File->"Open Recent" menu.

Initially based on pull request #40 by hochwasser <oo.o+github@windbuechse.samba-tng.org>, with refactoring.
This commit is contained in:
Jim Evins
2019-01-28 12:28:45 -05:00
parent 0e60c52d4c
commit ff9188f7e7
7 changed files with 154 additions and 3 deletions
+44
View File
@@ -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();
+1
View File
@@ -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 );
+52
View File
@@ -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<QAction*>( 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
+7 -1
View File
@@ -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<QAction*> fileRecentActionList;
QAction* editUndoAction;
QAction* editRedoAction;
QAction* editCutAction;
+39
View File
@@ -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();
}
}
}
+5
View File
@@ -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};
};
+6 -2
View File
@@ -655,11 +655,11 @@
<translation type="unfinished"></translation>
</message>
<message>
<source>&amp;Launch Bug Report Webpage</source>
<source>Ctrl+C</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Ctrl+C</source>
<source>&amp;Launch Issue Tracker</source>
<translation type="unfinished"></translation>
</message>
</context>
@@ -1732,6 +1732,10 @@
<source>&amp;User Manual...</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Open Recent</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>glabels::MergeView</name>