Skip to content
Permalink
Browse files

[composer] Prevent renaming to duplicate name in manager (fix #11051)

  • Loading branch information
nyalldawson committed Oct 19, 2014
1 parent 5d10f3c commit a7d708f95ea7c55f11b3c2f2dacf335b2747bc9e
Showing with 81 additions and 0 deletions.
  1. +60 −0 src/app/composer/qgscomposermanager.cpp
  2. +21 −0 src/app/composer/qgscomposermanager.h
@@ -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() ) );
@@ -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 );
}
@@ -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

0 comments on commit a7d708f

Please sign in to comment.
You can’t perform that action at this time.