Skip to content
Permalink
Browse files

Ask user what template to use if default composer templates are found

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13283 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent
mhugent committed Apr 8, 2010
1 parent 3f860ec commit f01d8dd8921d645576118953d12e6c7da36eb8d9
Showing with 100 additions and 63 deletions.
  1. +97 −60 src/app/composer/qgscomposermanager.cpp
  2. +3 −3 src/app/composer/qgscomposermanager.h
@@ -51,26 +51,26 @@ void QgsComposerManager::initialize()
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable );
mItemComposerMap.insert( item, *it );
}
}

QMap<QString, QString> QgsComposerManager::defaultTemplates() const
{
QMap<QString, QString> templateMap;

//search for default templates in $pkgDataPath/composer_templates
QDir defaultTemplateDir( QgsApplication::pkgDataPath() + "/composer_templates" );
if ( !defaultTemplateDir.exists() )
{
return;
return templateMap;
}

QFileInfoList defaultTemplateFiles = defaultTemplateDir.entryInfoList( QDir::Files );
QFileInfoList::const_iterator fileIt = defaultTemplateFiles.constBegin();

for ( ; fileIt != defaultTemplateFiles.constEnd(); ++fileIt )
QFileInfoList fileInfoList = defaultTemplateDir.entryInfoList( QDir::Files );
QFileInfoList::const_iterator infoIt = fileInfoList.constBegin();
for ( ; infoIt != fileInfoList.constEnd(); ++infoIt )
{
mDefaultTemplateMap.insert( fileIt->baseName(), fileIt->absoluteFilePath() );
if ( mComposerListWidget->findItems( fileIt->baseName(), Qt::MatchExactly ).size() < 1 )
{
QListWidgetItem* item = new QListWidgetItem( fileIt->baseName(), mComposerListWidget );
mItemComposerMap.insert( item, 0 );
}
templateMap.insert( infoIt->baseName(), infoIt->absoluteFilePath() );
}
return templateMap;
}

void QgsComposerManager::on_mAddButton_clicked()
@@ -79,11 +79,56 @@ void QgsComposerManager::on_mAddButton_clicked()
{
return;
}
QgsComposer* newComposer = mQgisApp->createNewComposer();

QMap<QString, QString> templateMap = defaultTemplates();
QString composerTemplate;
QgsComposer* newComposer = 0;

if ( templateMap.size() > 0 )
{
//show template dialog
QStringList templateNameList;
templateNameList.append( tr( "Empty composer" ) );
QMap<QString, QString>::const_iterator templateIt = templateMap.constBegin();
for ( ; templateIt != templateMap.constEnd(); ++templateIt )
{
templateNameList.append( templateIt.key() );
}

QInputDialog templateDialog;
templateDialog.setLabelText( tr( "Select a composer template" ) );
templateDialog.setComboBoxItems( templateNameList );
if ( templateDialog.exec() == QDialog::Rejected )
{
return;
}

QMap<QString, QString>::const_iterator selectedTemplate = templateMap.find( templateDialog.textValue() );
if ( selectedTemplate != templateMap.constEnd() )
{
composerTemplate = selectedTemplate.value();
}
}

newComposer = mQgisApp->createNewComposer();
if ( !newComposer )
{
return;
}

if ( !composerTemplate.isEmpty() ) //create composer from template
{
QDomDocument templateDoc;
QFile templateFile( composerTemplate );
if ( templateFile.open( QIODevice::ReadOnly ) )
{
if ( templateDoc.setContent( &templateFile, false ) )
{
newComposer->readXML( templateDoc );
}
}
}

QListWidgetItem* item = new QListWidgetItem( newComposer->title(), mComposerListWidget );
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable );
mItemComposerMap.insert( item, newComposer );
@@ -140,45 +185,55 @@ void QgsComposerManager::on_mShowPushButton_clicked()
if ( it.value() ) //a normal composer
{
c = it.value();
it.value()->show();
}
else //create composer from default template
{
QMap<QString, QString>::const_iterator templateIt = mDefaultTemplateMap.find( it.key()->text() );
if ( templateIt == mDefaultTemplateMap.constEnd() )
{
return;
}

QDomDocument templateDoc;
QFile templateFile( templateIt.value() );
if ( !templateFile.open( QIODevice::ReadOnly ) )
{
return;
}

if ( !templateDoc.setContent( &templateFile, false ) )
{
return;
}
c = mQgisApp->createNewComposer();
c->setTitle( it.key()->text() );
if ( c )
{
c->readXML( templateDoc );
mItemComposerMap.insert( it.key(), c );
c->show();
c->activate();
c->stackUnder( this );
raise();
activateWindow();
}
}
}
#if 0
else //create composer from default template
{
QMap<QString, QString>::const_iterator templateIt = mDefaultTemplateMap.find( it.key()->text() );
if ( templateIt == mDefaultTemplateMap.constEnd() )
{
return;
}

QDomDocument templateDoc;
QFile templateFile( templateIt.value() );
if ( !templateFile.open( QIODevice::ReadOnly ) )
{
return;
}

if ( !templateDoc.setContent( &templateFile, false ) )
{
return;
}
c = mQgisApp->createNewComposer();
c->setTitle( it.key()->text() );
if ( c )
{
c->show();
c->activate();
c->stackUnder( this );
raise();
activateWindow();
c->readXML( templateDoc );
mItemComposerMap.insert( it.key(), c );
}
}

if ( c )
{
c->show();
c->activate();
c->stackUnder( this );
raise();
activateWindow();
}
}
#endif //0
}

void QgsComposerManager::on_mRenamePushButton_clicked()
@@ -218,21 +273,3 @@ void QgsComposerManager::on_mComposerListWidget_itemChanged( QListWidgetItem * i
it.value()->setTitle( item->text() );
}
}

void QgsComposerManager::on_mComposerListWidget_currentItemChanged( QListWidgetItem* current, QListWidgetItem* previous )
{
if ( !current )
{
return;
}
if ( mDefaultTemplateMap.contains( current->text() ) )
{
mRenamePushButton->setEnabled( false );
mRemoveButton->setEnabled( false );
}
else
{
mRenamePushButton->setEnabled( true );
mRemoveButton->setEnabled( true );
}
}
@@ -38,19 +38,19 @@ class QgsComposerManager: public QDialog, private Ui::QgsComposerManagerBase
/**Stores the relation between items and composer pointers. A 0 pointer for the composer means that
this composer needs to be created from a default template*/
QMap<QListWidgetItem*, QgsComposer*> mItemComposerMap;
/**Key: name of the default template (=filename without suffix). Value: absolute path of the template*/
QMap<QString, QString > mDefaultTemplateMap;

/**Enters the composer instances and created the item-composer map*/
void initialize();

/**Returns the default templates (key: template name, value: absolute path to template file)*/
QMap<QString, QString> defaultTemplates() const;

private slots:
void on_mAddButton_clicked();
void on_mRemoveButton_clicked();
void on_mShowPushButton_clicked();
void on_mRenamePushButton_clicked();
void on_mComposerListWidget_itemChanged( QListWidgetItem * item );
void on_mComposerListWidget_currentItemChanged( QListWidgetItem* current, QListWidgetItem* previous );
};

#endif // QGSCOMPOSERMANAGER_H

0 comments on commit f01d8dd

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