Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE][composer] Add page number combo box to atlas toolbar
(fix #13136)
  • Loading branch information
nyalldawson committed Jul 23, 2015
1 parent b431187 commit d07dadf
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 59 deletions.
11 changes: 10 additions & 1 deletion python/core/composer/qgsatlascomposition.sip
Expand Up @@ -216,6 +216,11 @@ public:

/** Returns the current atlas feature. Must be called after prepareForFeature( i ). */
QgsFeature* currentFeature();

/** Returns the current feature number.
* @note added in QGIS 2.12
*/
int currentFeatureNumber() const;

/** Recalculates the bounds of an atlas driven map */
void prepareMap( QgsComposerMap* map );
Expand Down Expand Up @@ -253,5 +258,9 @@ public:

/**Is emitted when the current atlas feature changes*/
void featureChanged( QgsFeature* feature );


/** Is emitted when the number of features for the atlas changes.
* @note added in QGIS 2.12
*/
void numberFeaturesChanged( int numFeatures );
};
52 changes: 50 additions & 2 deletions src/app/composer/qgscomposer.cpp
Expand Up @@ -407,6 +407,17 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
atlasExportToolButton->addAction( mActionExportAtlasAsPDF );
atlasExportToolButton->setDefaultAction( mActionExportAtlasAsImage );
mAtlasToolbar->insertWidget( mActionAtlasSettings, atlasExportToolButton );
mAtlasPageComboBox = new QComboBox();
mAtlasPageComboBox->setEditable( true );
mAtlasPageComboBox->addItem( QString::number( 1 ) );
mAtlasPageComboBox->setCurrentIndex( 0 );
mAtlasPageComboBox->setMinimumHeight( mAtlasToolbar->height() );
mAtlasPageComboBox->setMinimumContentsLength( 6 );
mAtlasPageComboBox->setMaxVisibleItems( 20 );
mAtlasPageComboBox->setInsertPolicy( QComboBox::NoInsert );
connect( mAtlasPageComboBox->lineEdit(), SIGNAL( editingFinished() ), this, SLOT( atlasPageComboEditingFinished() ) );
connect( mAtlasPageComboBox, SIGNAL( currentIndexChanged( QString ) ), this, SLOT( atlasPageComboEditingFinished() ) );
mAtlasToolbar->insertWidget( mActionAtlasNext, mAtlasPageComboBox );

QMenu *settingsMenu = menuBar()->addMenu( tr( "&Settings" ) );
settingsMenu->addAction( mActionOptions );
Expand Down Expand Up @@ -613,12 +624,14 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
mActionAtlasNext->setEnabled( false );
mActionAtlasPrev->setEnabled( false );
mActionPrintAtlas->setEnabled( false );
mAtlasPageComboBox->setEnabled( false );
mActionExportAtlasAsImage->setEnabled( false );
mActionExportAtlasAsSVG->setEnabled( false );
mActionExportAtlasAsPDF->setEnabled( false );
QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
connect( atlasMap, SIGNAL( toggled( bool ) ), this, SLOT( toggleAtlasControls( bool ) ) );
connect( atlasMap, SIGNAL( coverageLayerChanged( QgsVectorLayer* ) ), this, SLOT( updateAtlasMapLayerAction( QgsVectorLayer * ) ) );
connect( atlasMap, SIGNAL( numberFeaturesChanged( int ) ), this, SLOT( updateAtlasPageComboBox( int ) ) );

//default printer page setup
setPrinterPageDefaults();
Expand Down Expand Up @@ -968,6 +981,7 @@ void QgsComposer::toggleAtlasControls( bool atlasEnabled )
mActionAtlasLast->setEnabled( false );
mActionAtlasNext->setEnabled( false );
mActionAtlasPrev->setEnabled( false );
mAtlasPageComboBox->setEnabled( false );
mActionAtlasPreview->blockSignals( false );
mActionAtlasPreview->setEnabled( atlasEnabled );
mActionPrintAtlas->setEnabled( atlasEnabled );
Expand All @@ -978,6 +992,20 @@ void QgsComposer::toggleAtlasControls( bool atlasEnabled )
updateAtlasMapLayerAction( atlasEnabled );
}

void QgsComposer::updateAtlasPageComboBox( int pageCount )
{
if ( pageCount == mAtlasPageComboBox->count() )
return;

mAtlasPageComboBox->blockSignals( true );
mAtlasPageComboBox->clear();
for ( int i = 1; i <= pageCount && i < 500; ++i )
{
mAtlasPageComboBox->addItem( QString::number( i ), i );
}
mAtlasPageComboBox->blockSignals( false );
}

void QgsComposer::on_mActionAtlasPreview_triggered( bool checked )
{
QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
Expand All @@ -1002,6 +1030,7 @@ void QgsComposer::on_mActionAtlasPreview_triggered( bool checked )
mActionAtlasLast->setEnabled( checked );
mActionAtlasNext->setEnabled( checked );
mActionAtlasPrev->setEnabled( checked );
mAtlasPageComboBox->setEnabled( checked );

if ( checked )
{
Expand All @@ -1022,6 +1051,7 @@ void QgsComposer::on_mActionAtlasPreview_triggered( bool checked )
mActionAtlasLast->setEnabled( false );
mActionAtlasNext->setEnabled( false );
mActionAtlasPrev->setEnabled( false );
mAtlasPageComboBox->setEnabled( false );
mActionAtlasPreview->blockSignals( false );
mStatusAtlasLabel->setText( QString() );
return;
Expand All @@ -1036,10 +1066,8 @@ void QgsComposer::on_mActionAtlasPreview_triggered( bool checked )
{
mStatusAtlasLabel->setText( QString() );
}

}


void QgsComposer::on_mActionAtlasNext_triggered()
{
QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
Expand Down Expand Up @@ -1100,6 +1128,23 @@ void QgsComposer::on_mActionAtlasLast_triggered()
emit atlasPreviewFeatureChanged();
}

void QgsComposer::atlasPageComboEditingFinished()
{
QString text = mAtlasPageComboBox->lineEdit()->text();
bool ok = false;
int page = text.toInt( &ok );
if ( !ok || page >= mComposition->atlasComposition().numFeatures() )
{
mAtlasPageComboBox->blockSignals( true );
mAtlasPageComboBox->setCurrentIndex( mComposition->atlasComposition().currentFeatureNumber() );
mAtlasPageComboBox->blockSignals( false );
}
else if ( page != mComposition->atlasComposition().currentFeatureNumber() + 1 )
{
mComposition->atlasComposition().prepareForFeature( page - 1 );
}
}

QgsMapCanvas *QgsComposer::mapCanvas( void )
{
return mQgis->mapCanvas();
Expand Down Expand Up @@ -1415,6 +1460,7 @@ void QgsComposer::setComposition( QgsComposition* composition )
toggleAtlasControls( atlasMap->enabled() );
connect( atlasMap, SIGNAL( toggled( bool ) ), this, SLOT( toggleAtlasControls( bool ) ) );
connect( atlasMap, SIGNAL( coverageLayerChanged( QgsVectorLayer* ) ), this, SLOT( updateAtlasMapLayerAction( QgsVectorLayer * ) ) );
connect( atlasMap, SIGNAL( numberFeaturesChanged( int ) ), this, SLOT( updateAtlasPageComboBox( int ) ) );

//default printer page setup
setPrinterPageDefaults();
Expand Down Expand Up @@ -3233,6 +3279,7 @@ void QgsComposer::readXML( const QDomElement& composerElem, const QDomDocument&
toggleAtlasControls( atlasMap->enabled() );
connect( atlasMap, SIGNAL( toggled( bool ) ), this, SLOT( toggleAtlasControls( bool ) ) );
connect( atlasMap, SIGNAL( coverageLayerChanged( QgsVectorLayer* ) ), this, SLOT( updateAtlasMapLayerAction( QgsVectorLayer * ) ) );
connect( atlasMap, SIGNAL( numberFeaturesChanged( int ) ), this, SLOT( updateAtlasPageComboBox( int ) ) );

//default printer page setup
setPrinterPageDefaults();
Expand Down Expand Up @@ -3716,6 +3763,7 @@ void QgsComposer::setAtlasFeature( QgsMapLayer* layer, const QgsFeature& feat )
mActionAtlasLast->setEnabled( true );
mActionAtlasNext->setEnabled( true );
mActionAtlasPrev->setEnabled( true );
mAtlasPageComboBox->setEnabled( true );
}

//bring composer window to foreground
Expand Down
52 changes: 30 additions & 22 deletions src/app/composer/qgscomposer.h
Expand Up @@ -343,6 +343,9 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
//!Last atlas feature
void on_mActionAtlasLast_triggered();

//!Jump to a specific atlas page
void atlasPageComboEditingFinished();

//! Print the atlas
void on_mActionPrintAtlas_triggered();

Expand All @@ -367,40 +370,40 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
//! Save window state
void saveWindowState();

/**Add a composer arrow to the item/widget map and creates a configuration widget for it*/
/** Add a composer arrow to the item/widget map and creates a configuration widget for it*/
void addComposerArrow( QgsComposerArrow* arrow );

/**Add a composer map to the item/widget map and creates a configuration widget for it*/
/** Add a composer map to the item/widget map and creates a configuration widget for it*/
void addComposerMap( QgsComposerMap* map );

/**Adds a composer label to the item/widget map and creates a configuration widget for it*/
/** Adds a composer label to the item/widget map and creates a configuration widget for it*/
void addComposerLabel( QgsComposerLabel* label );

/**Adds a composer scale bar to the item/widget map and creates a configuration widget for it*/
/** Adds a composer scale bar to the item/widget map and creates a configuration widget for it*/
void addComposerScaleBar( QgsComposerScaleBar* scalebar );

/**Adds a composer legend to the item/widget map and creates a configuration widget for it*/
/** Adds a composer legend to the item/widget map and creates a configuration widget for it*/
void addComposerLegend( QgsComposerLegend* legend );

/**Adds a composer picture to the item/widget map and creates a configuration widget*/
/** Adds a composer picture to the item/widget map and creates a configuration widget*/
void addComposerPicture( QgsComposerPicture* picture );

/**Adds a composer shape to the item/widget map and creates a configuration widget*/
/** Adds a composer shape to the item/widget map and creates a configuration widget*/
void addComposerShape( QgsComposerShape* shape );

/**Adds a composer table to the item/widget map and creates a configuration widget*/
/** Adds a composer table to the item/widget map and creates a configuration widget*/
void addComposerTable( QgsComposerAttributeTable* table );

/**Adds a composer table v2 to the item/widget map and creates a configuration widget*/
/** Adds a composer table v2 to the item/widget map and creates a configuration widget*/
void addComposerTableV2( QgsComposerAttributeTableV2* table, QgsComposerFrame* frame );

/**Adds composer html and creates a configuration widget*/
/** Adds composer html and creates a configuration widget*/
void addComposerHtmlFrame( QgsComposerHtml* html, QgsComposerFrame* frame );

/**Removes item from the item/widget map and deletes the configuration widget. Does not delete the item itself*/
/** Removes item from the item/widget map and deletes the configuration widget. Does not delete the item itself*/
void deleteItem( QgsComposerItem* item );

/**Shows the configuration widget for a composer item*/
/** Shows the configuration widget for a composer item*/
void showItemOptions( QgsComposerItem* i );

//XML, usually connected with QgsProject::readProject and QgsProject::writeProject
Expand Down Expand Up @@ -438,19 +441,19 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase

private:

/**Establishes the signal slot connections from the QgsComposerView to the composer*/
/** Establishes the signal slot connections from the QgsComposerView to the composer*/
void connectViewSlots();

/**Establishes the signal slot connections from the QgsComposition to the composer*/
/** Establishes the signal slot connections from the QgsComposition to the composer*/
void connectCompositionSlots();

/**Establishes other signal slot connections for the composer*/
/** Establishes other signal slot connections for the composer*/
void connectOtherSlots();

/**Creates the composition widget*/
/** Creates the composition widget*/
void createCompositionWidget();

/**Sets up the compositions undo/redo connections*/
/** Sets up the compositions undo/redo connections*/
void setupUndoView();

//! True if a composer map contains a WMS layer
Expand Down Expand Up @@ -510,19 +513,19 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase

QPrinter* printer();

/**Composer title*/
/** Composer title*/
QString mTitle;

/**Labels in status bar which shows current mouse position*/
/** Labels in status bar which shows current mouse position*/
QLabel* mStatusCursorXLabel;
QLabel* mStatusCursorYLabel;
QLabel* mStatusCursorPageLabel;
/**Combobox in status bar which shows/adjusts current zoom level*/
/** Combobox in status bar which shows/adjusts current zoom level*/
QComboBox* mStatusZoomCombo;
QList<double> mStatusZoomLevelsList;
/**Label in status bar which shows messages from the composition*/
/** Label in status bar which shows messages from the composition*/
QLabel* mStatusCompositionLabel;
/**Label in status bar which shows atlas details*/
/** Label in status bar which shows atlas details*/
QLabel* mStatusAtlasLabel;

//! Pointer to composer view
Expand Down Expand Up @@ -570,6 +573,8 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
QAction *mActionPreviewProtanope;
QAction *mActionPreviewDeuteranope;

QComboBox* mAtlasPageComboBox;

//! We load composer map content from project xml only on demand. Therefore we need to store the real preview mode type
QMap< QgsComposerMap*, int > mMapsToRestore;

Expand Down Expand Up @@ -649,6 +654,9 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase

void dockVisibilityChanged( bool visible );

/** Repopulates the atlas page combo box with valid items.
*/
void updateAtlasPageComboBox( int pageCount );
};

#endif
Expand Down
6 changes: 6 additions & 0 deletions src/core/composer/qgsatlascomposition.cpp
Expand Up @@ -263,6 +263,7 @@ int QgsAtlasComposition::updateFeatures()
}

QgsExpression::setSpecialColumn( "$numfeatures", QVariant(( int )mFeatureIds.size() ) );
emit numberFeaturesChanged( mFeatureIds.size() );

//jump to first feature if currently using an atlas preview
//need to do this in case filtering/layer change has altered matching features
Expand Down Expand Up @@ -396,6 +397,11 @@ bool QgsAtlasComposition::prepareForFeature( const int featureI, const bool upda
return false;
}

if ( featureI >= mFeatureIds.size() )
{
return false;
}

mCurrentFeatureNo = featureI;

// retrieve the next feature, based on its id
Expand Down

0 comments on commit d07dadf

Please sign in to comment.