Skip to content

Commit

Permalink
[composer] Prevent renaming to duplicate name in manager (fix #11051)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 19, 2014
1 parent 5d10f3c commit a7d708f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/app/composer/qgscomposermanager.cpp
Expand Up @@ -41,6 +41,8 @@ QgsComposerManager::QgsComposerManager( QWidget * parent, Qt::WindowFlags f ): Q
QSettings settings;
restoreGeometry( settings.value( "/Windows/ComposerManager/geometry" ).toByteArray() );

mComposerListWidget->setItemDelegate( new QgsComposerNameDelegate( mComposerListWidget ) );

connect( mButtonBox, SIGNAL( rejected() ), this, SLOT( close() ) );
connect( QgisApp::instance(), SIGNAL( composerAdded( QgsComposerView* ) ), this, SLOT( refreshComposers() ) );
connect( QgisApp::instance(), SIGNAL( composerRemoved( QgsComposerView* ) ), this, SLOT( refreshComposers() ) );
Expand Down Expand Up @@ -487,3 +489,61 @@ void QgsComposerManager::on_mComposerListWidget_itemChanged( QListWidgetItem * i
}
mComposerListWidget->sortItems();
}


//
// QgsComposerNameDelegate
//

QgsComposerNameDelegate::QgsComposerNameDelegate( QObject *parent )
: QItemDelegate( parent )
{

}

QWidget *QgsComposerNameDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
Q_UNUSED( option );
Q_UNUSED( index );

//create a line edit
QLineEdit *lineEdit = new QLineEdit( parent );
return lineEdit;
}

void QgsComposerNameDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
{
QString text = index.model()->data( index, Qt::EditRole ).toString();
QLineEdit *lineEdit = static_cast<QLineEdit*>( editor );
lineEdit->setText( text );
}

void QgsComposerNameDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
{
QLineEdit *lineEdit = static_cast<QLineEdit*>( editor );
QString value = lineEdit->text();

//has name changed?
bool changed = model->data( index, Qt::EditRole ).toString() != value;

//check if name already exists
QStringList cNames;
foreach ( QgsComposer* c, QgisApp::instance()->printComposers() )
{
cNames << c->title();
}
if ( changed && cNames.contains( value ) )
{
//name exists!
QMessageBox::warning( 0, tr( "Rename composer" ), tr( "There is already a composer named \"%1\"" ).arg( value ) );
return;
}

model->setData( index, QVariant( value ), Qt::EditRole );
}

void QgsComposerNameDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
Q_UNUSED( index );
editor->setGeometry( option.rect );
}
21 changes: 21 additions & 0 deletions src/app/composer/qgscomposermanager.h
Expand Up @@ -17,11 +17,32 @@
#ifndef QGSCOMPOSERMANAGER_H
#define QGSCOMPOSERMANAGER_H

#include <QItemDelegate>

#include "ui_qgscomposermanagerbase.h"

class QListWidgetItem;
class QgsComposer;

/**Delegate for a line edit for renaming a composer. Prevents entry of duplicate composer names.*/
class QgsComposerNameDelegate : public QItemDelegate
{
Q_OBJECT

public:
QgsComposerNameDelegate( QObject *parent = 0 );

QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index ) const;

void setEditorData( QWidget *editor, const QModelIndex &index ) const;
void setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index ) const;

void updateEditorGeometry( QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index ) const;
};

/**A dialog that shows the existing composer instances. Lets the user add new
instances and change title of existing ones*/
class QgsComposerManager: public QDialog, private Ui::QgsComposerManagerBase
Expand Down

0 comments on commit a7d708f

Please sign in to comment.