db_manager_plugin.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """
  2. /***************************************************************************
  3. Name : DB Manager
  4. Description : Database manager plugin for QGIS
  5. Date : May 23, 2011
  6. copyright : (C) 2011 by Giuseppe Sucameli
  7. email : brush.tyler@gmail.com
  8. ***************************************************************************/
  9. /***************************************************************************
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. ***************************************************************************/
  17. """
  18. from qgis.PyQt.QtCore import Qt
  19. from qgis.PyQt.QtWidgets import QAction, QApplication
  20. from qgis.PyQt.QtGui import QIcon
  21. from qgis.core import (
  22. QgsProject,
  23. QgsMapLayerType,
  24. QgsDataSourceUri,
  25. QgsApplication
  26. )
  27. from . import resources_rc # NOQA
  28. class DBManagerPlugin:
  29. def __init__(self, iface):
  30. self.iface = iface
  31. self.dlg = None
  32. def initGui(self):
  33. self.action = QAction(QgsApplication.getThemeIcon('dbmanager.svg'), QApplication.translate("DBManagerPlugin", "DB Manager…"),
  34. self.iface.mainWindow())
  35. self.action.setObjectName("dbManager")
  36. self.action.triggered.connect(self.run)
  37. # Add toolbar button and menu item
  38. if hasattr(self.iface, 'addDatabaseToolBarIcon'):
  39. self.iface.addDatabaseToolBarIcon(self.action)
  40. else:
  41. self.iface.addToolBarIcon(self.action)
  42. if hasattr(self.iface, 'addPluginToDatabaseMenu'):
  43. self.iface.addPluginToDatabaseMenu(QApplication.translate("DBManagerPlugin", None), self.action)
  44. else:
  45. self.iface.addPluginToMenu(QApplication.translate("DBManagerPlugin", "DB Manager"), self.action)
  46. def unload(self):
  47. # Remove the plugin menu item and icon
  48. if hasattr(self.iface, 'databaseMenu'):
  49. self.iface.databaseMenu().removeAction(self.action)
  50. else:
  51. self.iface.removePluginMenu(QApplication.translate("DBManagerPlugin", "DB Manager"), self.action)
  52. if hasattr(self.iface, 'removeDatabaseToolBarIcon'):
  53. self.iface.removeDatabaseToolBarIcon(self.action)
  54. else:
  55. self.iface.removeToolBarIcon(self.action)
  56. if self.dlg is not None:
  57. self.dlg.close()
  58. def onUpdateSqlLayer(self):
  59. # Be able to update every Db layer from Postgres, Spatialite and Oracle
  60. l = self.iface.activeLayer()
  61. if l.dataProvider().name() in ['postgres', 'spatialite', 'oracle']:
  62. self.run()
  63. self.dlg.runSqlLayerWindow(l)
  64. # virtual has QUrl source
  65. # url = QUrl(QUrl.fromPercentEncoding(l.source()))
  66. # url.queryItemValue('query')
  67. # url.queryItemValue('uid')
  68. # url.queryItemValue('geometry')
  69. def run(self):
  70. # keep opened only one instance
  71. if self.dlg is None:
  72. from .db_manager import DBManager
  73. self.dlg = DBManager(self.iface)
  74. self.dlg.destroyed.connect(self.onDestroyed)
  75. self.dlg.show()
  76. self.dlg.raise_()
  77. self.dlg.setWindowState(self.dlg.windowState() & ~Qt.WindowMinimized)
  78. self.dlg.activateWindow()
  79. def onDestroyed(self, obj):
  80. self.dlg = None