utilities.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # coding=utf-8
  2. """Common functionality used by regression tests."""
  3. import sys
  4. import logging
  5. LOGGER = logging.getLogger('QGIS')
  6. QGIS_APP = None # Static variable used to hold hand to running QGIS app
  7. CANVAS = None
  8. PARENT = None
  9. IFACE = None
  10. def get_qgis_app():
  11. """ Start one QGIS application to test against.
  12. :returns: Handle to QGIS app, canvas, iface and parent. If there are any
  13. errors the tuple members will be returned as None.
  14. :rtype: (QgsApplication, CANVAS, IFACE, PARENT)
  15. If QGIS is already running the handle to that app will be returned.
  16. """
  17. try:
  18. from PyQt5 import QtGui, QtCore
  19. from qgis.core import QgsApplication
  20. from qgis.gui import QgsMapCanvas
  21. from .qgis_interface import QgisInterface
  22. except ImportError:
  23. return None, None, None, None
  24. global QGIS_APP # pylint: disable=W0603
  25. if QGIS_APP is None:
  26. gui_flag = True # All test will run qgis in gui mode
  27. #noinspection PyPep8Naming
  28. QGIS_APP = QgsApplication(sys.argv, gui_flag)
  29. # Make sure QGIS_PREFIX_PATH is set in your env if needed!
  30. QGIS_APP.initQgis()
  31. s = QGIS_APP.showSettings()
  32. LOGGER.debug(s)
  33. global PARENT # pylint: disable=W0603
  34. if PARENT is None:
  35. #noinspection PyPep8Naming
  36. PARENT = QtGui.QWidget()
  37. global CANVAS # pylint: disable=W0603
  38. if CANVAS is None:
  39. #noinspection PyPep8Naming
  40. CANVAS = QgsMapCanvas(PARENT)
  41. CANVAS.resize(QtCore.QSize(400, 400))
  42. global IFACE # pylint: disable=W0603
  43. if IFACE is None:
  44. # QgisInterface is a stub implementation of the QGIS plugin interface
  45. #noinspection PyPep8Naming
  46. IFACE = QgisInterface(CANVAS)
  47. return QGIS_APP, CANVAS, IFACE, PARENT