Skip to content

Commit 8203fa1

Browse files
committed
[StyleManager] fix minor memory leaks leaving orphaned QMenus and QActions
Build menus once within the ctor rather than on every event within the slot.
1 parent 0243201 commit 8203fa1

File tree

2 files changed

+42
-39
lines changed

2 files changed

+42
-39
lines changed

src/gui/symbology-ng/qgsstylev2managerdialog.cpp

+33-39
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ QgsStyleV2ManagerDialog::QgsStyleV2ManagerDialog( QgsStyleV2* style, QWidget* pa
4949
#endif
5050

5151
QSettings settings;
52+
QAction* a; // used as a temporary variable before passing ownership of a created action.
53+
5254
restoreGeometry( settings.value( "/Windows/StyleV2Manager/geometry" ).toByteArray() );
5355
mSplitter->setSizes( QList<int>() << 170 << 540 );
5456
mSplitter->restoreState( settings.value( "/Windows/StyleV2Manager/splitter" ).toByteArray() );
@@ -67,8 +69,10 @@ QgsStyleV2ManagerDialog::QgsStyleV2ManagerDialog( QgsStyleV2* style, QWidget* pa
6769
QMenu *shareMenu = new QMenu( tr( "Share menu" ), this );
6870
QAction *exportAsPNGAction = shareMenu->addAction( tr( "Export selected symbols as PNG" ) );
6971
QAction *exportAsSVGAction = shareMenu->addAction( tr( "Export selected symbols as SVG" ) );
70-
QAction *exportAction = shareMenu->addAction( tr( "Export..." ) );
71-
QAction *importAction = shareMenu->addAction( tr( "Import..." ) );
72+
QAction *exportAction = new QAction( tr( "Export..." ), this );
73+
shareMenu->addAction( exportAction );
74+
QAction *importAction = new QAction( tr( "Import..." ), this );
75+
shareMenu->addAction( importAction );
7276
exportAsPNGAction->setIcon( QIcon( QgsApplication::iconPath( "mActionSharingExport.svg" ) ) );
7377
exportAsSVGAction->setIcon( QIcon( QgsApplication::iconPath( "mActionSharingExport.svg" ) ) );
7478
exportAction->setIcon( QIcon( QgsApplication::iconPath( "mActionSharingExport.svg" ) ) );
@@ -126,6 +130,24 @@ QgsStyleV2ManagerDialog::QgsStyleV2ManagerDialog( QgsStyleV2* style, QWidget* pa
126130
connect( listItems, SIGNAL( customContextMenuRequested( const QPoint& ) ),
127131
this, SLOT( listitemsContextMenu( const QPoint& ) ) );
128132

133+
// Menu for the "Add item" toolbutton when in colorramp mode
134+
QStringList rampTypes;
135+
rampTypes << tr( "Gradient" ) << tr( "Random" ) << tr( "ColorBrewer" );
136+
rampTypes << tr( "cpt-city" ); // todo, only for rasters?
137+
mMenuBtnAddItemColorRamp = new QMenu( this );
138+
Q_FOREACH ( const QString& rampType, rampTypes )
139+
mMenuBtnAddItemColorRamp->addAction( new QAction( rampType, this ) );
140+
connect( mMenuBtnAddItemColorRamp, SIGNAL( triggered( QAction* ) ),
141+
this, SLOT( addColorRamp( QAction* ) ) );
142+
143+
// Context menu for symbols/colorramps. The menu entries for every group are created when displaying the menu.
144+
mGroupMenu = new QMenu( this );
145+
mGroupListMenu = new QMenu( mGroupMenu );
146+
mGroupListMenu->setTitle( tr( "Apply Group" ) );
147+
mGroupMenu->addMenu( mGroupListMenu );
148+
a = new QAction( tr( "Un-group" ), mGroupMenu );
149+
a->setData( 0 );
150+
mGroupMenu->addAction( a );
129151
}
130152

131153
void QgsStyleV2ManagerDialog::onFinished()
@@ -183,36 +205,11 @@ void QgsStyleV2ManagerDialog::populateTypes()
183205

184206
void QgsStyleV2ManagerDialog::on_tabItemType_currentChanged( int )
185207
{
186-
// when in Color Ramp tab, add menu to add item button
187-
if ( currentItemType() == 3 )
188-
{
189-
btnShare->menu()->actions().at( 0 )->setVisible( false );
190-
btnShare->menu()->actions().at( 1 )->setVisible( false );
191-
192-
QStringList rampTypes;
193-
rampTypes << tr( "Gradient" ) << tr( "Random" ) << tr( "ColorBrewer" );
194-
rampTypes << tr( "cpt-city" ); // todo, only for rasters?
195-
QMenu* menu = new QMenu( btnAddItem );
196-
Q_FOREACH ( const QString& rampType, rampTypes )
197-
{
198-
menu->addAction( rampType );
199-
}
200-
btnAddItem->setMenu( menu );
201-
connect( menu, SIGNAL( triggered( QAction* ) ),
202-
this, SLOT( addColorRamp( QAction* ) ) );
203-
}
204-
else
205-
{
206-
btnShare->menu()->actions().at( 0 )->setVisible( true );
207-
btnShare->menu()->actions().at( 1 )->setVisible( true );
208-
209-
if ( btnAddItem->menu() )
210-
{
211-
disconnect( btnAddItem->menu(), SIGNAL( triggered( QAction* ) ),
212-
this, SLOT( addColorRamp( QAction* ) ) );
213-
btnAddItem->setMenu( nullptr );
214-
}
215-
}
208+
// when in Color Ramp tab, add menu to add item button and hide "Export symbols as PNG/SVG"
209+
bool flag = currentItemType() != 3;
210+
btnAddItem->setMenu( flag ? nullptr : mMenuBtnAddItemColorRamp );
211+
btnShare->menu()->actions().at( 0 )->setVisible( flag );
212+
btnShare->menu()->actions().at( 1 )->setVisible( flag );
216213

217214
// set icon and grid size, depending on type
218215
if ( currentItemType() == 1 || currentItemType() == 3 )
@@ -1327,19 +1324,16 @@ void QgsStyleV2ManagerDialog::listitemsContextMenu( const QPoint& point )
13271324
{
13281325
QPoint globalPos = listItems->viewport()->mapToGlobal( point );
13291326

1330-
QMenu *groupMenu = new QMenu( this );
1331-
QMenu *groupList = new QMenu( this );
1332-
groupList->setTitle( tr( "Apply Group" ) );
1327+
// Clear all actions and create new actions for every group
1328+
mGroupListMenu->clear();
13331329

13341330
QStringList groups = mStyle->groupNames();
13351331
Q_FOREACH ( const QString& group, groups )
13361332
{
1337-
groupList->addAction( group );
1333+
mGroupListMenu->addAction( new QAction( group, mGroupListMenu ) );
13381334
}
1339-
groupMenu->addMenu( groupList );
1340-
groupMenu->addAction( tr( "Un-group" ) );
13411335

1342-
QAction* selectedItem = groupMenu->exec( globalPos );
1336+
QAction* selectedItem = mGroupMenu->exec( globalPos );
13431337

13441338
if ( selectedItem )
13451339
{

src/gui/symbology-ng/qgsstylev2managerdialog.h

+9
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ class GUI_EXPORT QgsStyleV2ManagerDialog : public QDialog, private Ui::QgsStyleV
146146

147147
//! space to store symbol tags
148148
QStringList mTagList;
149+
150+
//! Context menu for the symbols/colorramps
151+
QMenu *mGroupMenu;
152+
153+
//! Sub-menu of @c mGroupMenu, dynamically filled to show one entry for every group
154+
QMenu *mGroupListMenu;
155+
156+
//! Menu for the "Add item" toolbutton when in colorramp mode
157+
QMenu* mMenuBtnAddItemColorRamp;
149158
};
150159

151160
#endif

0 commit comments

Comments
 (0)