From 5c186eba0e96a8248cc22c3a1e8edfff62382560 Mon Sep 17 00:00:00 2001 From: Lukas W Date: Fri, 24 Jan 2014 15:33:50 +0100 Subject: [PATCH] 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") --- include/FileDialog.h | 2 + include/VersionedSaveDialog.h | 53 +++++++++ src/gui/MainWindow.cpp | 5 +- src/gui/dialogs/FileDialog.cpp | 8 ++ src/gui/dialogs/VersionedSaveDialog.cpp | 139 ++++++++++++++++++++++++ 5 files changed, 204 insertions(+), 3 deletions(-) create mode 100644 include/VersionedSaveDialog.h create mode 100644 src/gui/dialogs/VersionedSaveDialog.cpp diff --git a/include/FileDialog.h b/include/FileDialog.h index 4a0c678ce..ddc231546 100644 --- a/include/FileDialog.h +++ b/include/FileDialog.h @@ -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 diff --git a/include/VersionedSaveDialog.h b/include/VersionedSaveDialog.h new file mode 100644 index 000000000..8041f00f1 --- /dev/null +++ b/include/VersionedSaveDialog.h @@ -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 + * + * 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 diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 82a8a6169..002237f63 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -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 != "" ) { diff --git a/src/gui/dialogs/FileDialog.cpp b/src/gui/dialogs/FileDialog.cpp index d506a8209..6e57329c1 100644 --- a/src/gui/dialogs/FileDialog.cpp +++ b/src/gui/dialogs/FileDialog.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include "FileDialog.h" @@ -53,5 +54,12 @@ FileDialog::FileDialog( QWidget *parent, const QString &caption, setSidebarUrls(urls); } +void FileDialog::clearSelection() +{ + static QListView *view = findChild(); + Q_ASSERT( view ); + view->clearSelection(); +} + #include "moc_FileDialog.cxx" diff --git a/src/gui/dialogs/VersionedSaveDialog.cpp b/src/gui/dialogs/VersionedSaveDialog.cpp new file mode 100644 index 000000000..c52f47dc2 --- /dev/null +++ b/src/gui/dialogs/VersionedSaveDialog.cpp @@ -0,0 +1,139 @@ +/* + * VersionedSaveDialog.cpp - implementation of class VersionedSaveDialog + * + * Copyright (c) 2014 Lukas W + * + * 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 +#include +#include +#include + +#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( this->layout() ); + QWidget *lineEdit = findChild(); + 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"