Skip to content

Commit a7d708f

Browse files
committedOct 19, 2014
[composer] Prevent renaming to duplicate name in manager (fix #11051)
1 parent 5d10f3c commit a7d708f

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
 

‎src/app/composer/qgscomposermanager.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ QgsComposerManager::QgsComposerManager( QWidget * parent, Qt::WindowFlags f ): Q
4141
QSettings settings;
4242
restoreGeometry( settings.value( "/Windows/ComposerManager/geometry" ).toByteArray() );
4343

44+
mComposerListWidget->setItemDelegate( new QgsComposerNameDelegate( mComposerListWidget ) );
45+
4446
connect( mButtonBox, SIGNAL( rejected() ), this, SLOT( close() ) );
4547
connect( QgisApp::instance(), SIGNAL( composerAdded( QgsComposerView* ) ), this, SLOT( refreshComposers() ) );
4648
connect( QgisApp::instance(), SIGNAL( composerRemoved( QgsComposerView* ) ), this, SLOT( refreshComposers() ) );
@@ -487,3 +489,61 @@ void QgsComposerManager::on_mComposerListWidget_itemChanged( QListWidgetItem * i
487489
}
488490
mComposerListWidget->sortItems();
489491
}
492+
493+
494+
//
495+
// QgsComposerNameDelegate
496+
//
497+
498+
QgsComposerNameDelegate::QgsComposerNameDelegate( QObject *parent )
499+
: QItemDelegate( parent )
500+
{
501+
502+
}
503+
504+
QWidget *QgsComposerNameDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
505+
{
506+
Q_UNUSED( option );
507+
Q_UNUSED( index );
508+
509+
//create a line edit
510+
QLineEdit *lineEdit = new QLineEdit( parent );
511+
return lineEdit;
512+
}
513+
514+
void QgsComposerNameDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
515+
{
516+
QString text = index.model()->data( index, Qt::EditRole ).toString();
517+
QLineEdit *lineEdit = static_cast<QLineEdit*>( editor );
518+
lineEdit->setText( text );
519+
}
520+
521+
void QgsComposerNameDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
522+
{
523+
QLineEdit *lineEdit = static_cast<QLineEdit*>( editor );
524+
QString value = lineEdit->text();
525+
526+
//has name changed?
527+
bool changed = model->data( index, Qt::EditRole ).toString() != value;
528+
529+
//check if name already exists
530+
QStringList cNames;
531+
foreach ( QgsComposer* c, QgisApp::instance()->printComposers() )
532+
{
533+
cNames << c->title();
534+
}
535+
if ( changed && cNames.contains( value ) )
536+
{
537+
//name exists!
538+
QMessageBox::warning( 0, tr( "Rename composer" ), tr( "There is already a composer named \"%1\"" ).arg( value ) );
539+
return;
540+
}
541+
542+
model->setData( index, QVariant( value ), Qt::EditRole );
543+
}
544+
545+
void QgsComposerNameDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const
546+
{
547+
Q_UNUSED( index );
548+
editor->setGeometry( option.rect );
549+
}

‎src/app/composer/qgscomposermanager.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,32 @@
1717
#ifndef QGSCOMPOSERMANAGER_H
1818
#define QGSCOMPOSERMANAGER_H
1919

20+
#include <QItemDelegate>
21+
2022
#include "ui_qgscomposermanagerbase.h"
2123

2224
class QListWidgetItem;
2325
class QgsComposer;
2426

27+
/**Delegate for a line edit for renaming a composer. Prevents entry of duplicate composer names.*/
28+
class QgsComposerNameDelegate : public QItemDelegate
29+
{
30+
Q_OBJECT
31+
32+
public:
33+
QgsComposerNameDelegate( QObject *parent = 0 );
34+
35+
QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option,
36+
const QModelIndex &index ) const;
37+
38+
void setEditorData( QWidget *editor, const QModelIndex &index ) const;
39+
void setModelData( QWidget *editor, QAbstractItemModel *model,
40+
const QModelIndex &index ) const;
41+
42+
void updateEditorGeometry( QWidget *editor,
43+
const QStyleOptionViewItem &option, const QModelIndex &index ) const;
44+
};
45+
2546
/**A dialog that shows the existing composer instances. Lets the user add new
2647
instances and change title of existing ones*/
2748
class QgsComposerManager: public QDialog, private Ui::QgsComposerManagerBase

0 commit comments

Comments
 (0)
Please sign in to comment.