Skip to content

Commit 2e9bd8f

Browse files
author
g_j_m
committed
Fix for ticket #112
Moved the plugin add/remove menu item api and mechanism to Qt4 QActions. Plugin menu items are now ordered alphabetically Was unable to test the grass plugin, so that may no work until yet :) git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@5450 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent afcb258 commit 2e9bd8f

25 files changed

+113
-153
lines changed

src/gui/Makefile.am

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ lib_LTLIBRARIES = libqgis_gui.la
4848

4949
qgis_SOURCES = main.cpp
5050

51-
qgis_LDADD = ../raster/libqgis_raster.la ../legend/libqgis_legend.la ../composer/libqgis_composer.la $(LDADD) $(QT_LDADD) $(GDAL_LDADD) $(PG_LIB) $(GEOS_LDADD) $(PYTHON_LIB) -lproj -lsqlite3 libqgis_gui.la ../core/libqgis_core.la
51+
qgis_LDADD = ../raster/libqgis_raster.la ../legend/libqgis_legend.la ../composer/libqgis_composer.la $(GEOS_LDADD) $(GDAL_LDADD) $(LDADD) $(QT_LDADD) $(PG_LIB) $(PYTHON_LIB) -lproj -lsqlite3 libqgis_gui.la ../core/libqgis_core.la
5252
if HAVE_QTMAC
5353
qgis_LDFLAGS = -framework ApplicationServices
5454
else
5555
qgis_LDFLAGS = -rdynamic
5656
endif
57-
qgis_CXXFLAGS = $(PREFIX) $(PLUGINPATH) $(PKGDATAPATH) $(GDAL_CFLAGS) $(CXXFLAGS) $(EXTRA_CXXFLAGS) $(QT_CXXFLAGS) $(PG_INC) $(DEBUG_QGIS) $(HAVE_PYTHON) $(GEOS_CFLAGS) $(PYTHON_INCLUDE_DIR) -I../ui/ -I../widgets/projectionselector/ -I../legend/ -I../raster/ -I../composer/ -I../core/ -I../plugins/
57+
qgis_CXXFLAGS = $(GEOS_CFLAGS) $(GDAL_CFLAGS) $(PREFIX) $(PLUGINPATH) $(PKGDATAPATH) $(CXXFLAGS) $(EXTRA_CXXFLAGS) $(QT_CXXFLAGS) $(PG_INC) $(DEBUG_QGIS) $(HAVE_PYTHON) $(PYTHON_INCLUDE_DIR) -I../ui/ -I../widgets/projectionselector/ -I../legend/ -I../raster/ -I../composer/ -I../core/ -I../plugins/
5858

5959

6060
##
@@ -294,10 +294,10 @@ nodist_libqgis_gui_la_SOURCES = $(libqgis_gui_la_MOC)
294294
BUILT_SOURCES = $(libqgis_gui_la_MOC) $(qgis_YACC) $(postgresMOC)
295295

296296

297-
libqgis_gui_la_LIBADD = ../core/libqgis_core.la ../raster/libqgis_raster.la ../legend/libqgis_legend.la ../composer/libqgis_composer.la ../widgets/projectionselector/libqgsprojectionselector.la $(QT_LDADD) $(GEOS_LDADD) $(GDAL_LDADD) $(PYTHON_LIB) -lsqlite3
297+
libqgis_gui_la_LIBADD = ../core/libqgis_core.la ../raster/libqgis_raster.la ../legend/libqgis_legend.la ../composer/libqgis_composer.la ../widgets/projectionselector/libqgsprojectionselector.la $(QT_LDADD) $(GEOS_LDADD) $(GDAL_LDADD) $(PYTHON_LIB) -lsqlite3
298298
libqgis_gui_la_LDFLAGS = -version-info $(INTERFACE_VERSION)
299299

300-
libqgis_gui_la_CXXFLAGS = $(PREFIX) $(PLUGINPATH) $(PKGDATAPATH) $(GDAL_CFLAGS) $(CXXFLAGS) $(EXTRA_CXXFLAGS) $(QT_CXXFLAGS) $(PG_INC) $(DEBUG_QGIS) $(GEOS_CFLAGS) $(PYTHON_INCLUDE_DIR) $(HAVE_PYTHON) -I../ui/ -I../widgets/projectionselector/ -I../legend/ -I../raster/ -I../composer/ -I../core/ -I../plugins/
300+
libqgis_gui_la_CXXFLAGS = $(GDAL_CFLAGS) $(PREFIX) $(PLUGINPATH) $(PKGDATAPATH) $(CXXFLAGS) $(EXTRA_CXXFLAGS) $(QT_CXXFLAGS) $(PG_INC) $(DEBUG_QGIS) $(GEOS_CFLAGS) $(PYTHON_INCLUDE_DIR) $(HAVE_PYTHON) -I../ui/ -I../widgets/projectionselector/ -I../legend/ -I../raster/ -I../composer/ -I../core/ -I../plugins/
301301

302302
##
303303
## ----------------------------------------------------------------------

src/gui/qgisapp.cpp

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4388,41 +4388,57 @@ void QgisApp::populateMenuMaps()
43884388
}
43894389
while(menuId != -1);
43904390
}
4391-
int QgisApp::addPluginMenu(QString menuText, QMenu *menu)
4392-
{
4393-
return mPluginMenu->insertItem(menuText, menu);
4394-
}
43954391

43964392
QMenu* QgisApp::getPluginMenu(QString menuName)
43974393
{
4398-
for (unsigned int i = 0; i < mPluginMenu->count(); ++i)
4399-
if (mPluginMenu->text(mPluginMenu->idAt(i)) == menuName)
4394+
// This is going to record the menu item that the potentially new
4395+
// menu item is going to be inserted before. A value of 0 will a new
4396+
// menu item to be appended.
4397+
QAction* before = 0;
4398+
4399+
QList<QAction*> actions = mPluginMenu->actions();
4400+
// Avoid 1 because the first item (number 0) is 'Plugin Manager',
4401+
// which we want to stay first. Search in reverse order as that
4402+
// makes it easier to find out where which item a new menu item
4403+
// should go before (since the insertMenu() function requires a
4404+
// 'before' argument).
4405+
for (unsigned int i = actions.count()-1; i > 0; --i)
4406+
{
4407+
if (actions.at(i)->text() == menuName)
44004408
{
4401-
QMenuItem* item = mPluginMenu->findItem(mPluginMenu->idAt(i));
4402-
return item->menu();
4409+
return actions.at(i)->menu();
44034410
}
4411+
// Find out where to put the menu item, assuming that it is a new one
4412+
//
4413+
// This bit of code assumes that the menu items are already in
4414+
// alphabetical order, which they will be if the menus are all
4415+
// created using this function.
4416+
if (QString::localeAwareCompare(menuName, actions.at(i)->text()) <= 0)
4417+
before = actions.at(i);
4418+
}
4419+
4420+
// It doesn't exist, so create
4421+
QMenu* menu = new QMenu(menuName, this);
4422+
// Where to put it? - we worked that out above...
4423+
mPluginMenu->insertMenu(before, menu);
44044424

4405-
// It doesn't exist, so create one
4406-
Q3PopupMenu* menu = new Q3PopupMenu(mPluginMenu);
4407-
mPluginMenu->insertItem(menuName, menu);
44084425
return menu;
44094426
}
44104427

4411-
void QgisApp::removePluginMenuItem(QString name, int menuId)
4428+
void QgisApp::addPluginMenu(QString name, QAction* action)
44124429
{
4413-
// TODO: Qt4 will have to do this a different way...
4414-
#if QT_VERSION < 0x040000
4415-
for (int i = 0; i < mPluginMenu->count(); ++i)
4416-
if (mPluginMenu->text(mPluginMenu->idAt(i)) == name)
4430+
QMenu* menu = getPluginMenu(name);
4431+
menu->addAction(action);
4432+
}
4433+
4434+
void QgisApp::removePluginMenu(QString name, QAction* action)
4435+
{
4436+
QMenu* menu = getPluginMenu(name);
4437+
menu->removeAction(action);
4438+
if (menu->actions().count() == 0)
44174439
{
4418-
QMenuItem* item = mPluginMenu->findItem(mPluginMenu->idAt(i));
4419-
Q3PopupMenu* popup = item->popup();
4420-
popup->removeItem(menuId);
4421-
if (popup->count() == 0)
4422-
mPluginMenu->removeItem(mPluginMenu->idAt(i));
4423-
break;
4440+
mPluginMenu->removeAction(menu->menuAction());
44244441
}
4425-
#endif
44264442
}
44274443

44284444
int QgisApp::addPluginToolBarIcon (QAction * qAction)

src/gui/qgisapp.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,12 @@ public slots:
198198
void showPluginManager();
199199
//! plugin loader
200200
void loadPlugin(QString name, QString description, QString mFullPath);
201-
//! Add a plugin menu to the main Plugins menu
202-
int addPluginMenu(QString menuText, QMenu *menu);
203-
//! Get the menu that holds teh list of loaded plugins
201+
//! Find the QMenu with the given name (ie the user visible text on the menu item)
204202
QMenu* getPluginMenu(QString menuName);
205-
//! Remove an item from the qgis main app menus
206-
void removePluginMenuItem(QString name, int menuId);
203+
//! Add the action to the submenu with the given name under the plugin menu
204+
void addPluginMenu(QString name, QAction* action);
205+
//! Remove the action to the submenu with the given name under the plugin menu
206+
void removePluginMenu(QString name, QAction* action);
207207
//! Add an icon to the plugin toolbar
208208
int addPluginToolBarIcon (QAction * qAction);
209209
//! Remove an icon from the plugin toolbar

src/gui/qgisiface.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ QString QgisIface::activeLayerSource()
8989
return qgis->activeLayerSource();
9090
}
9191

92-
QMenu* QgisIface::getPluginMenu(QString menuName)
92+
void QgisIface::addPluginMenu(QString name, QAction* action)
9393
{
94-
return qgis->getPluginMenu(menuName);
94+
qgis->addPluginMenu(name, action);
9595
}
9696

97-
void QgisIface::removePluginMenuItem(QString name, int menuId)
97+
void QgisIface::removePluginMenu(QString name, QAction* action)
9898
{
99-
qgis->removePluginMenuItem(name, menuId);
99+
qgis->removePluginMenu(name, action);
100100
}
101101

102102
int QgisIface::addToolBarIcon(QAction * qAction)

src/gui/qgisiface.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class QgisIface : public QgisInterface
6363
//! Get source of the active layer
6464
QString activeLayerSource();
6565

66-
QMenu* getPluginMenu(QString menuName);
66+
void addPluginMenu(QString name, QAction* action);
67+
void removePluginMenu(QString name, QAction* action);
6768

6869
void removePluginMenuItem(QString name, int menuId);
6970

src/plugins/copyright_label/plugin.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,8 @@ QgsCopyrightLabelPlugin::~QgsCopyrightLabelPlugin()
8282
*/
8383
void QgsCopyrightLabelPlugin::initGui()
8484
{
85-
QMenu *pluginMenu = qGisInterface->getPluginMenu(tr("&Decorations"));
86-
menuId = pluginMenu->insertItem(QIcon(icon),tr("&CopyrightLabel"), this, SLOT(run()));
87-
88-
pluginMenu->setWhatsThis(menuId, tr("Creates a copyright label that is displayed on the map canvas."));
89-
9085
// Create the action for tool
91-
myQActionPointer = new QAction(QIcon(icon), tr("Copyright Label"), this);
86+
myQActionPointer = new QAction(QIcon(icon), tr("&Copyright Label"), this);
9287
myQActionPointer->setWhatsThis(tr("Creates a copyright label that is displayed on the map canvas."));
9388
// Connect the action to the run
9489
connect(myQActionPointer, SIGNAL(activated()), this, SLOT(run()));
@@ -99,6 +94,7 @@ void QgsCopyrightLabelPlugin::initGui()
9994

10095
// Add the icon to the toolbar
10196
qGisInterface->addToolBarIcon(myQActionPointer);
97+
qGisInterface->addPluginMenu(tr("&Decorations"), myQActionPointer);
10298
//initialise default values in the gui
10399
projectRead();
104100
}
@@ -208,7 +204,7 @@ void QgsCopyrightLabelPlugin::renderLabel(QPainter * theQPainter)
208204
void QgsCopyrightLabelPlugin::unload()
209205
{
210206
// remove the GUI
211-
qGisInterface->removePluginMenuItem(tr("&Decorations"),menuId);
207+
qGisInterface->removePluginMenu(tr("&Decorations"),myQActionPointer);
212208
qGisInterface->removeToolBarIcon(myQActionPointer);
213209
// remove the copyright from the canvas
214210
disconnect(qGisInterface->getMapCanvas(), SIGNAL(renderComplete(QPainter *)),

src/plugins/copyright_label/plugin.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ class QgsCopyrightLabelPlugin:public QObject, public QgisPlugin
8383
bool mEnable;
8484

8585
int pluginType;
86-
//! Id of the plugin's menu. Used for unloading
87-
int menuId;
8886
//! Pionter to QGIS main application object
8987
QgisApp *qgisMainWindowPointer;
9088
//! Pointer to the QGIS interface object

src/plugins/delimited_text/qgsdelimitedtextplugin.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,8 @@ void QgsDelimitedTextPlugin::help()
101101
*/
102102
void QgsDelimitedTextPlugin::initGui()
103103
{
104-
QMenu *pluginMenu = qGisInterface->getPluginMenu(tr("&Delimited text"));
105-
menuId = pluginMenu->insertItem(QIcon(icon),tr("&Add Delimited Text Layer"), this, SLOT(run()));
106-
107-
pluginMenu->setWhatsThis(menuId, tr("Add a delimited text file as a map layer. ")+
108-
tr("The file must have a header row containing the field names. ")+
109-
tr("X and Y fields are required and must contain coordinates in decimal units."));
110-
111104
// Create the action for tool
112-
myQActionPointer = new QAction(QIcon(icon), tr("Add Delimited Text Layer"), this);
105+
myQActionPointer = new QAction(QIcon(icon), tr("&Add Delimited Text Layer"), this);
113106

114107
myQActionPointer->setWhatsThis(tr("Add a delimited text file as a map layer. ")+
115108
tr("The file must have a header row containing the field names. ")+
@@ -118,6 +111,7 @@ void QgsDelimitedTextPlugin::initGui()
118111
connect(myQActionPointer, SIGNAL(activated()), this, SLOT(run()));
119112
// Add the icon to the toolbar
120113
qGisInterface->addToolBarIcon(myQActionPointer);
114+
qGisInterface->addPluginMenu(tr("&Delimited text"), myQActionPointer);
121115

122116
}
123117

@@ -154,7 +148,7 @@ void QgsDelimitedTextPlugin::drawVectorLayer(QString thePathNameQString,
154148
void QgsDelimitedTextPlugin::unload()
155149
{
156150
// remove the GUI
157-
qGisInterface->removePluginMenuItem(tr("&Delimited text"),menuId);
151+
qGisInterface->removePluginMenu(tr("&Delimited text"),myQActionPointer);
158152
qGisInterface->removeToolBarIcon(myQActionPointer);
159153
delete myQActionPointer;
160154
}

src/plugins/delimited_text/qgsdelimitedtextplugin.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ class QgsDelimitedTextPlugin:public QObject, public QgisPlugin, private Ui::QgsD
8080
QString pluginDescriptionQString;
8181
//! Plugin type as defined in Plugin::PLUGINTYPE
8282
int pluginType;
83-
//! Id of the plugin's menu. Used for unloading
84-
int menuId;
8583
//! Pionter to QGIS main application object
8684
QgisApp *qgisMainWindowPointer;
8785
//! Pointer to the QGIS interface object

src/plugins/geoprocessing/qgspggeoprocessing.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,16 @@ QgsPgGeoprocessing::~QgsPgGeoprocessing()
7272
*/
7373
void QgsPgGeoprocessing::initGui()
7474
{
75-
QMenu *pluginMenu = qI->getPluginMenu(tr("&Geoprocessing"));
76-
menuId = pluginMenu->insertItem(QIcon(icon_buffer),tr("&Buffer Features"), this, SLOT(buffer()));
77-
78-
pluginMenu->setWhatsThis(menuId, tr("Create a buffer for a PostgreSQL layer. " +
79-
tr("A new layer is created in the database with the buffered features.")));
80-
8175
// Create the action for tool
82-
bufferAction = new QAction(QIcon(icon_buffer), tr("Buffer features"), this);
76+
bufferAction = new QAction(QIcon(icon_buffer), tr("&Buffer features"), this);
8377
bufferAction->setWhatsThis(tr("Create a buffer for a PostgreSQL layer. " +
8478
tr("A new layer is created in the database with the buffered features.")));
8579
// Connect the action to the buffer slot
8680
connect(bufferAction, SIGNAL(activated()), this, SLOT(buffer()));
8781

8882
// Add the icon to the toolbar
8983
qI->addToolBarIcon(bufferAction);
84+
qI->addPluginMenu(tr("&Geoprocessing"), bufferAction);
9085

9186
}
9287

@@ -416,7 +411,7 @@ bool QgsPgGeoprocessing::hasPROJ(PGconn *connection){
416411
void QgsPgGeoprocessing::unload()
417412
{
418413
// remove the GUI
419-
qI->removePluginMenuItem(tr("&Geoprocessing"),menuId);
414+
qI->removePluginMenu(tr("&Geoprocessing"),bufferAction);
420415
qI->removeToolBarIcon(bufferAction);
421416
delete bufferAction;
422417
}

src/plugins/geoprocessing/qgspggeoprocessing.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ class QgsPgGeoprocessing:public QObject, public QgisPlugin
7373
bool gistAvailable;
7474
bool projAvailable;
7575

76-
//! Id of the plugin's menu. Used for unloading
77-
int menuId;
7876
//! Pionter to QGIS main application object
7977
QgisApp *qgisMainWindow;
8078
//! Pointer to the QGIS interface object

src/plugins/georeferencer/plugin.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,9 @@ void QgsGeorefPlugin::initGui()
113113
// Connect the action to the run
114114
connect(mQActionPointer, SIGNAL(activated()), this, SLOT(run()));
115115

116-
// add to the plugin menu
117-
QMenu *pluginMenu = mQGisIface->getPluginMenu(tr("&Georeferencer"));
118-
pluginMenu->addAction(mQActionPointer);
119-
120-
// Add to the toolbar
116+
// Add to the toolbar & menu
121117
mQGisIface->addToolBarIcon(mQActionPointer);
118+
mQGisIface->addPluginMenu(tr("&Georeferencer"), mQActionPointer);
122119

123120
}
124121
//method defined in interface
@@ -137,9 +134,8 @@ void QgsGeorefPlugin::run()
137134
// Unload the plugin by cleaning up the GUI
138135
void QgsGeorefPlugin::unload()
139136
{
140-
// TODO: make it work in Qt4 way
141137
// remove the GUI
142-
mQGisIface->removePluginMenuItem(tr("&Georeferencer"),mMenuId);
138+
mQGisIface->removePluginMenu(tr("&Georeferencer"),mQActionPointer);
143139
mQGisIface->removeToolBarIcon(mQActionPointer);
144140
delete mQActionPointer;
145141
}

src/plugins/georeferencer/plugin.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ public slots:
103103
////////////////////////////////////////////////////////////////////
104104

105105
int mPluginType;
106-
//! Id of the plugin's menu. Used for unloading
107-
int mMenuId;
108106
//! Pointer to our toolbar
109107
Q3ToolBar *mToolBarPointer;
110108
//! Pionter to QGIS main application object

src/plugins/gps_importer/qgsgpsplugin.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,18 @@ QgsGPSPlugin::~QgsGPSPlugin()
103103
*/
104104
void QgsGPSPlugin::initGui()
105105
{
106-
QMenu *pluginMenu = mQGisInterface->getPluginMenu(tr("&Gps"));
107-
mMenuIdGPS = pluginMenu->insertItem(QIcon(icon),tr("&Gps Tools"), this, SLOT(run()));
108-
mMenuIdGPX = pluginMenu->insertItem(QIcon(icon),tr("&Create new GPX layer"), this, SLOT(createGPX()));
109-
110-
pluginMenu->setWhatsThis(mMenuIdGPX, tr("Creates a new GPX layer and displays it on the map canvas"));
111-
112106
// add an action to the toolbar
113-
mQActionPointer = new QAction(QIcon(icon), tr("Gps Tools"), this);
107+
mQActionPointer = new QAction(QIcon(icon), tr("&Gps Tools"), this);
108+
mCreateGPXAction = new QAction(QIcon(icon), tr("&Create new GPX layer"), this);
114109

115110
mQActionPointer->setWhatsThis(tr("Creates a new GPX layer and displays it on the map canvas"));
111+
mCreateGPXAction->setWhatsThis(tr("Creates a new GPX layer and displays it on the map canvas"));
116112
connect(mQActionPointer, SIGNAL(activated()), this, SLOT(run()));
113+
connect(mCreateGPXAction, SIGNAL(activated()), this, SLOT(createGPX()));
114+
117115
mQGisInterface->addToolBarIcon(mQActionPointer);
116+
mQGisInterface->addPluginMenu(tr("&Gps"), mQActionPointer);
117+
mQGisInterface->addPluginMenu(tr("&Gps"), mCreateGPXAction);
118118
}
119119

120120
//method defined in interface
@@ -202,8 +202,8 @@ void QgsGPSPlugin::drawVectorLayer(QString thePathNameQString,
202202
void QgsGPSPlugin::unload()
203203
{
204204
// remove the GUI
205-
mQGisInterface->removePluginMenuItem(tr("&Gps"),mMenuIdGPS);
206-
mQGisInterface->removePluginMenuItem(tr("&Gps"),mMenuIdGPX);
205+
mQGisInterface->removePluginMenu(tr("&Gps"),mQActionPointer);
206+
mQGisInterface->removePluginMenu(tr("&Gps"),mCreateGPXAction);
207207
mQGisInterface->removeToolBarIcon(mQActionPointer);
208208
delete mQActionPointer;
209209
}

src/plugins/gps_importer/qgsgpsplugin.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,14 @@ public slots:
8080
//! Initializes all variables needed to run GPSBabel.
8181
void setupBabel();
8282

83-
//! Id of the plugin's menu. Used for unloading
84-
int mMenuIdGPS, mMenuIdGPX;
8583
//! Pointer to our menu
8684
QgisApp *mMainWindowPointer;
8785
//! Pointer to the QGIS interface object
8886
QgisIface *mQGisInterface;
8987
//! Pointer to the QAction object used in the menu and toolbar
9088
QAction *mQActionPointer;
91-
89+
//! Pointer to the QAction used for creating a new GPX layer
90+
QAction *mCreateGPXAction;
9291
//! The path to the GPSBabel program
9392
QString mBabelPath;
9493
//! Importers for external GPS data file formats

0 commit comments

Comments
 (0)