Showing with 304 additions and 43 deletions.
  1. +1 −1 src/app/composer/qgscomposer.cpp
  2. +149 −18 src/app/composer/qgscomposermanager.cpp
  3. +29 −2 src/app/composer/qgscomposermanager.h
  4. +2 −1 src/app/qgisapp.cpp
  5. +123 −21 src/ui/qgscomposermanagerbase.ui
2 changes: 1 addition & 1 deletion src/app/composer/qgscomposer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,7 @@ void QgsComposer::on_mActionSaveAsTemplate_triggered()
QString saveFileNameWithSuffix = saveFileName.append( ".qpt" );
saveFileInfo = QFileInfo( saveFileNameWithSuffix );
}
settings.setValue( "UI/LastComposerTemplateDir", saveFileInfo.absolutePath() );
settings.setValue( "UI/lastComposerTemplateDir", saveFileInfo.absolutePath() );

QFile templateFile( saveFileName );
if ( !templateFile.open( QIODevice::WriteOnly ) )
Expand Down
167 changes: 149 additions & 18 deletions src/app/composer/qgscomposermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
#include "qgscomposer.h"
#include "qgslogger.h"

#include <QDesktopServices>
#include <QDialog>
#include <QDir>
#include <QFileDialog>
#include <QInputDialog>
#include <QListWidgetItem>
#include <QMessageBox>
#include <QUrl>
#include <QSettings>

QgsComposerManager::QgsComposerManager( QWidget * parent, Qt::WindowFlags f ): QDialog( parent, f )
Expand Down Expand Up @@ -65,6 +68,7 @@ QgsComposerManager::~QgsComposerManager()

void QgsComposerManager::initialize()
{
QSettings settings;
QSet<QgsComposer*> composers = QgisApp::instance()->printComposers();
QSet<QgsComposer*>::const_iterator it = composers.constBegin();
for ( ; it != composers.constEnd(); ++it )
Expand All @@ -75,24 +79,42 @@ void QgsComposerManager::initialize()
}

mTemplate->addItem( tr( "Empty composer" ) );
mTemplate->addItem( tr( "Specific" ) );

mUserTemplatesDir = QgsApplication::qgisSettingsDirPath() + "/composer_templates";
QMap<QString, QString> userTemplateMap = defaultTemplates( true );
if ( userTemplateMap.size() > 0 )
{
mTemplate->insertSeparator( mTemplate->count() );
QMap<QString, QString>::const_iterator templateIt = userTemplateMap.constBegin();
for ( ; templateIt != userTemplateMap.constEnd(); ++templateIt )
{
mTemplate->addItem( templateIt.key(), templateIt.value() );
}
}

QMap<QString, QString> templateMap = defaultTemplates();
if ( templateMap.size() > 0 )
mDefaultTemplatesDir = QgsApplication::pkgDataPath() + "/composer_templates";
QMap<QString, QString> defaultTemplateMap = defaultTemplates( false );
if ( defaultTemplateMap.size() > 0 )
{
QMap<QString, QString>::const_iterator templateIt = templateMap.constBegin();
for ( ; templateIt != templateMap.constEnd(); ++templateIt )
mTemplate->insertSeparator( mTemplate->count() );
QMap<QString, QString>::const_iterator templateIt = defaultTemplateMap.constBegin();
for ( ; templateIt != defaultTemplateMap.constEnd(); ++templateIt )
{
mTemplate->addItem( templateIt.key(), templateIt.value() );
}
}

mTemplatePathLineEdit->setText( settings.value( "/UI/ComposerManager/templatePath", QString( "" ) ).toString() );
}

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

//search for default templates in $pkgDataPath/composer_templates
QDir defaultTemplateDir( QgsApplication::pkgDataPath() + "/composer_templates" );
// user templates in $qgisSettingsDirPath/composer_templates
QDir defaultTemplateDir( fromUser ? mUserTemplatesDir : mDefaultTemplatesDir );
if ( !defaultTemplateDir.exists() )
{
return templateMap;
Expand All @@ -109,7 +131,33 @@ QMap<QString, QString> QgsComposerManager::defaultTemplates() const

void QgsComposerManager::on_mAddButton_clicked()
{
QFile templateFile;
bool loadingTemplate = ( mTemplate->currentIndex() > 0 );
if ( loadingTemplate )
{
if ( mTemplate->currentIndex() == 1 )
{
templateFile.setFileName( mTemplatePathLineEdit->text() );
}
else
{
templateFile.setFileName( mTemplate->itemData( mTemplate->currentIndex() ).toString() );
}

if ( !templateFile.exists() )
{
QMessageBox::warning( this, tr( "Template error" ), tr( "Error, template file not found" ) );
return;
}
if ( !templateFile.open( QIODevice::ReadOnly ) )
{
QMessageBox::warning( this, tr( "Template error" ), tr( "Error, could not read file" ) );
return;
}
}

QgsComposer* newComposer = 0;
bool loadedOK = false;

QString title = QgisApp::instance()->uniqueComposerTitle( this, true );
if ( title.isNull() )
Expand All @@ -120,25 +168,94 @@ void QgsComposerManager::on_mAddButton_clicked()
newComposer = QgisApp::instance()->createNewComposer( title );
if ( !newComposer )
{
QMessageBox::warning( this, tr( "Composer error" ), tr( "Error, could not create composer" ) );
return;
}
else
{
loadedOK = true;
}

if ( mTemplate->currentIndex() > 0 )
if ( loadingTemplate )
{
QDomDocument templateDoc;
QFile templateFile( mTemplate->itemData( mTemplate->currentIndex() ).toString() );
if ( templateFile.open( QIODevice::ReadOnly ) )
if ( templateDoc.setContent( &templateFile, false ) )
{
if ( templateDoc.setContent( &templateFile, false ) )
{
newComposer->readXML( templateDoc );
}
// provide feedback, since composer will be hidden when loading template (much faster)
// (not needed for empty composer)
QDialog* dlg = newComposer->busyIndicatorDialog( tr( "Loading template into composer..." ), this );
dlg->show();
newComposer->hide();
loadedOK = newComposer->composition()->loadFromTemplate( templateDoc, 0, false );
newComposer->activate();
dlg->close();
}
}

if ( loadedOK )
{
// do not close on Add, since user may want to add multiple composers from templates
QListWidgetItem* item = new QListWidgetItem( newComposer->title(), mComposerListWidget );
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable );
mItemComposerMap.insert( item, newComposer );
mComposerListWidget->setCurrentItem( item );
mComposerListWidget->setFocus();
}
else
{
if ( newComposer )
{
newComposer->close();
QgisApp::instance()->deleteComposer( newComposer );
newComposer = 0;
}
QMessageBox::warning( this, tr( "Template error" ), tr( "Error, could not load template file" ) );
}
}

void QgsComposerManager::on_mTemplate_currentIndexChanged( int indx )
{
bool specific = ( indx == 1 ); // comes just after empty template
mTemplatePathLineEdit->setEnabled( specific );
mTemplatePathBtn->setEnabled( specific );
}

void QgsComposerManager::on_mTemplatePathBtn_pressed()
{
QSettings settings;
QString lastTmplDir = settings.value( "/UI/lastComposerTemplateDir", "." ).toString();
QString tmplPath = QFileDialog::getOpenFileName( this,
tr( "Choose template" ),
lastTmplDir,
tr( "Composer templates" ) + " (*.qpt)" );
if ( !tmplPath.isNull() )
{
mTemplatePathLineEdit->setText( tmplPath );
settings.setValue( "UI/ComposerManager/templatePath", tmplPath );
QFileInfo tmplFileInfo( tmplPath );
settings.setValue( "UI/lastComposerTemplateDir", tmplFileInfo.absolutePath() );
}
}

void QgsComposerManager::on_mTemplatesDefaultDirBtn_pressed()
{
openLocalDirectory( mDefaultTemplatesDir );
}

QListWidgetItem* item = new QListWidgetItem( newComposer->title(), mComposerListWidget );
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable );
mItemComposerMap.insert( item, newComposer );
void QgsComposerManager::on_mTemplatesUserDirBtn_pressed()
{
openLocalDirectory( mUserTemplatesDir );
}

void QgsComposerManager::openLocalDirectory( const QString& localDirPath )
{
QDir localDir;
if ( !localDir.mkpath( localDirPath ) )
{
QMessageBox::warning( this, tr( "File system error" ), tr( "Error, could not open or create local directory" ) );
return;
}
QDesktopServices::openUrl( QUrl::fromLocalFile( localDirPath ) );
}

void QgsComposerManager::remove_clicked()
Expand All @@ -150,7 +267,7 @@ void QgsComposerManager::remove_clicked()
}

//ask for confirmation
if ( QMessageBox::warning( 0, tr( "Remove composer" ), tr( "Do you really want to remove the map composer '%1'?" ).arg( item->text() ), QMessageBox::Ok | QMessageBox::Cancel ) != QMessageBox::Ok )
if ( QMessageBox::warning( this, tr( "Remove composer" ), tr( "Do you really want to remove the map composer '%1'?" ).arg( item->text() ), QMessageBox::Ok | QMessageBox::Cancel ) != QMessageBox::Ok )
{
return;
}
Expand Down Expand Up @@ -184,7 +301,17 @@ void QgsComposerManager::show_clicked()
c = it.value();
if ( c )
{
// extra activation steps for Windows
bool shown = c->isVisible();
hide();

c->activate();

// extra activation steps for Windows
if ( !shown )
{
c->on_mActionZoomAll_triggered();
}
}
}
}
Expand Down Expand Up @@ -261,6 +388,10 @@ void QgsComposerManager::duplicate_clicked()

if ( newComposer )
{
// extra activation steps for Windows
hide();
newComposer->activate();

// no need to add new composer to list widget, if just closing this->exec();
close();
}
Expand Down Expand Up @@ -291,7 +422,7 @@ void QgsComposerManager::rename_clicked()
{
return;
}
QString newTitle = QInputDialog::getText( 0, tr( "Change title" ), tr( "Title" ), QLineEdit::Normal, currentTitle );
QString newTitle = QgisApp::instance()->uniqueComposerTitle( this, false, currentTitle );
if ( newTitle.isNull() )
{
return;
Expand Down
31 changes: 29 additions & 2 deletions src/app/composer/qgscomposermanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,38 @@ class QgsComposerManager: public QDialog, private Ui::QgsComposerManagerBase
/**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;
/** Returns the default templates (key: template name, value: absolute path to template file)
* @param fromUser whether to return user templates from ~/.qgis/composer_templates (added in 1.9)
*/
QMap<QString, QString> defaultTemplates( bool fromUser = false ) const;

/** Open local directory with user's system, creating it if not present
* @note added in QGIS 1.9
*/
void openLocalDirectory( const QString& localDirPath );

QString mDefaultTemplatesDir;
QString mUserTemplatesDir;

private slots:
void on_mAddButton_clicked();
/** Slot to track combobox to use specific template path
* @note added in 1.9
*/
void on_mTemplate_currentIndexChanged( int indx );
/** Slot to choose path to template
* @note added in QGIS 1.9
*/
void on_mTemplatePathBtn_pressed();
/** Slot to open default templates dir with user's system
* @note added in QGIS 1.9
*/
void on_mTemplatesDefaultDirBtn_pressed();
/** Slot to open user templates dir with user's system
* @note added in QGIS 1.9
*/
void on_mTemplatesUserDirBtn_pressed();

void remove_clicked();
void show_clicked();
/** Duplicate composer
Expand Down
3 changes: 2 additions & 1 deletion src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4780,7 +4780,8 @@ bool QgisApp::loadComposersFromProject( const QDomDocument& doc )
mPrintComposers.insert( composer );
mPrintComposersMenu->addAction( composer->windowAction() );
#ifndef Q_OS_MACX
composer->showMinimized();
composer->setWindowState( Qt::WindowMinimized );
composer->show();
#endif
composer->zoomFull();
if ( composerNodes.at( i ).toElement().attribute( "visible", "1" ).toInt() < 1 )
Expand Down
Loading