Add new class VersionedSaveDialog

A file save dialog (inherits FileDialog) that provides buttons to
increment or decrement a version which is appended to the file name.
(e.g. "MyProject-01.mmpz")
This commit is contained in:
Lukas W
2014-01-24 15:33:50 +01:00
parent 2e7bfe17e6
commit 5c186eba0e
5 changed files with 204 additions and 3 deletions

View File

@@ -35,6 +35,8 @@ public:
explicit FileDialog( QWidget *parent = 0, const QString &caption = QString(),
const QString &directory = QString(),
const QString &filter = QString() );
void clearSelection();
};
#endif // FILEDIALOG_HPP

View File

@@ -0,0 +1,53 @@
/*
* VersionedSaveDialog.h - declaration of class VersionedSaveDialog, a file save
* dialog that provides buttons to increment or decrement a version which is
* appended to the file name. (e.g. "MyProject-01.mmpz")
*
* Copyright (c) 2014 Lukas W <lukaswhl/at/gmail.com>
*
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#ifndef VERSIONEDSAVEDIALOG_H
#define VERSIONEDSAVEDIALOG_H
#include "FileDialog.h"
class QLineEdit;
class VersionedSaveDialog : public FileDialog
{
Q_OBJECT
public:
explicit VersionedSaveDialog( QWidget *parent = 0,
const QString &caption = QString(),
const QString &directory = QString(),
const QString &filter = QString() );
// Returns true if file name was changed, returns false if it wasn't
static bool changeFileNameVersion( QString &fileName, bool increment );
public slots:
void incrementVersion();
void decrementVersion();
};
#endif // VERSIONEDSAVEDIALOG_H

View File

@@ -62,6 +62,7 @@
#include "AutomationEditor.h"
#include "templates.h"
#include "FileDialog.h"
#include "VersionedSaveDialog.h"
@@ -748,11 +749,9 @@ bool MainWindow::saveProject( void )
bool MainWindow::saveProjectAs( void )
{
FileDialog sfd( this, tr( "Save project" ), "",
VersionedSaveDialog sfd( this, tr( "Save project" ), "",
tr( "MultiMedia Project (*.mmp *.mmpz);;"
"MultiMedia Project Template (*.mpt)" ) );
sfd.setAcceptMode( QFileDialog::AcceptSave );
sfd.setFileMode( QFileDialog::AnyFile );
QString f = engine::getSong()->projectFileName();
if( f != "" )
{

View File

@@ -26,6 +26,7 @@
#include <QtCore/QList>
#include <QtCore/QUrl>
#include <QtGui/QDesktopServices>
#include <QtGui/QListView>
#include "FileDialog.h"
@@ -53,5 +54,12 @@ FileDialog::FileDialog( QWidget *parent, const QString &caption,
setSidebarUrls(urls);
}
void FileDialog::clearSelection()
{
static QListView *view = findChild<QListView*>();
Q_ASSERT( view );
view->clearSelection();
}
#include "moc_FileDialog.cxx"

View File

@@ -0,0 +1,139 @@
/*
* VersionedSaveDialog.cpp - implementation of class VersionedSaveDialog
*
* Copyright (c) 2014 Lukas W <lukaswhl/at/gmail.com>
*
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include <QtGui/QLayout>
#include <QtGui/QPushButton>
#include <QtGui/QFontMetrics>
#include <QtGui/QLineEdit>
#include "VersionedSaveDialog.h"
VersionedSaveDialog::VersionedSaveDialog( QWidget *parent,
const QString &caption,
const QString &directory,
const QString &filter ) :
FileDialog(parent, caption, directory, filter)
{
setAcceptMode( QFileDialog::AcceptSave );
setFileMode( QFileDialog::AnyFile );
// Create + and - buttons
QPushButton *plusButton( new QPushButton( "+", this) );
plusButton->setToolTip( tr( "Increment version number" ) );
QPushButton *minusButton( new QPushButton( "-", this ) );
minusButton->setToolTip( tr( "Decrement version number" ) );
plusButton->setFixedWidth( plusButton->fontMetrics().width( "+" ) + 30 );
minusButton->setFixedWidth( minusButton->fontMetrics().width( "+" ) + 30 );
// Add buttons to grid layout. For doing this, remove the lineEdit and
// replace it with a HBox containing lineEdit and the buttons.
QGridLayout *layout = dynamic_cast<QGridLayout*>( this->layout() );
QWidget *lineEdit = findChild<QLineEdit*>();
layout->removeWidget( lineEdit );
QHBoxLayout* hLayout( new QHBoxLayout() );
hLayout->addWidget( lineEdit );
hLayout->addWidget( plusButton );
hLayout->addWidget( minusButton );
layout->addLayout( hLayout, 2, 1 );
// Connect + and - buttons
connect( plusButton, SIGNAL( clicked() ), this, SLOT( incrementVersion() ));
connect( minusButton, SIGNAL( clicked() ), this, SLOT( decrementVersion() ));
}
bool VersionedSaveDialog::changeFileNameVersion(QString &fileName, bool increment )
{
static QRegExp regexp( "-\\d+(\\.\\w+)?$" );
int idx = regexp.indexIn( fileName );
// For file names without extension (no ".mmpz")
int insertIndex = fileName.lastIndexOf( '.' );
if ( insertIndex < idx+1 )
insertIndex = fileName.size();
if ( idx == -1 )
{
// Can't decrement if there is no version number
if ( increment == false )
return false;
else
fileName.insert( insertIndex, "-01" );
}
else
{
// Find current version number
QString number = fileName.mid( idx+1, insertIndex - idx - 1 );
bool ok;
ushort version = number.toUShort( &ok );
Q_ASSERT( ok );
// Can't decrement 0
if ( !increment and version == 0 )
return false;
// Replace version number
version = increment ? version + 1 : version - 1;
QString newnumber = QString( "%1" ).arg( version, 2, 10, QChar( '0' ) );
fileName.replace( idx+1, number.length(), newnumber );
}
return true;
}
void VersionedSaveDialog::incrementVersion()
{
const QStringList& selected = selectedFiles();
if ( selected.size() != 1 )
return;
QString file = selected[0];
changeFileNameVersion( file, true );
clearSelection();
selectFile( file );
}
void VersionedSaveDialog::decrementVersion()
{
const QStringList& selected = selectedFiles();
if ( selected.size() != 1 )
return;
QString file = selected[0];
changeFileNameVersion( file, false );
clearSelection();
selectFile( file );
}
#include "moc_VersionedSaveDialog.cxx"