Example #1
0
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() )
  {
    return;
  }

  newComposer = QgisApp::instance()->createNewComposer( title );
  if ( !newComposer )
  {
    QMessageBox::warning( this, tr( "Composer error" ), tr( "Error, could not create composer" ) );
    return;
  }
  else
  {
    loadedOK = true;
  }

  if ( loadingTemplate )
  {
    QDomDocument templateDoc;
    if ( templateDoc.setContent( &templateFile, false ) )
    {
      // 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->sortItems();
    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" ) );
  }
}
Example #2
0
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() )
  {
    return;
  }

  newComposer = QgisApp::instance()->createNewComposer( title );
  if ( !newComposer )
  {
    QMessageBox::warning( this, tr( "Composer error" ), tr( "Error, could not create composer" ) );
    return;
  }
  else
  {
    loadedOK = true;
  }

  if ( loadingTemplate )
  {
    QDomDocument templateDoc;
    if ( templateDoc.setContent( &templateFile, false ) )
    {
      // provide feedback, since composer will be hidden when loading template (much faster)
      // (not needed for empty composer)
      QDialog* dlg = new QgsBusyIndicatorDialog( tr( "Loading template into composer..." ) );
      dlg->setStyleSheet( QgisApp::instance()->styleSheet() );
      dlg->show();

      newComposer->hide();
      loadedOK = newComposer->composition()->loadFromTemplate( templateDoc, 0, false );
      newComposer->activate();

      dlg->close();
      delete dlg;
      dlg = 0;
    }
  }

  if ( !loadedOK )
  {
    newComposer->close();
    QgisApp::instance()->deleteComposer( newComposer );
    newComposer = 0;
    QMessageBox::warning( this, tr( "Template error" ), tr( "Error, could not load template file" ) );
  }
}