MessageBarProgress.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """
  2. ***************************************************************************
  3. MessageBarProgress.py
  4. ---------------------
  5. Date : April 2013
  6. Copyright : (C) 2013 by Victor Olaya
  7. Email : volayaf at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Victor Olaya'
  18. __date__ = 'April 2013'
  19. __copyright__ = '(C) 2013, Victor Olaya'
  20. from qgis.PyQt.QtCore import Qt, QCoreApplication
  21. from qgis.PyQt.QtWidgets import QProgressBar
  22. from qgis.utils import iface
  23. from qgis.core import QgsProcessingFeedback, Qgis
  24. from processing.gui.MessageDialog import MessageDialog
  25. class MessageBarProgress(QgsProcessingFeedback):
  26. def __init__(self, algname=None):
  27. QgsProcessingFeedback.__init__(self)
  28. self.msg = []
  29. self.progressMessageBar = \
  30. iface.messageBar().createMessage(self.tr('Executing algorithm <i>{}</i>'.format(algname if algname else '')))
  31. self.progress = QProgressBar()
  32. self.progressChanged.connect(self.set_progress_bar_value)
  33. self.progress.setMaximum(100)
  34. self.progress.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
  35. self.progressMessageBar.layout().addWidget(self.progress)
  36. self.message_bar_item = iface.messageBar().pushWidget(self.progressMessageBar,
  37. Qgis.Info)
  38. def set_progress_bar_value(self, progress: float):
  39. """
  40. Sets the progress bar value to a rounded int of the algorithm's
  41. progress
  42. """
  43. self.progress.setValue(int(progress))
  44. def reportError(self, msg, fatalError=False):
  45. self.msg.append(msg)
  46. def close(self):
  47. if self.msg:
  48. dlg = MessageDialog()
  49. dlg.setTitle(QCoreApplication.translate('MessageBarProgress', 'Problem executing algorithm'))
  50. dlg.setMessage("<br>".join(self.msg))
  51. dlg.exec_()
  52. iface.messageBar().popWidget(self.message_bar_item)
  53. def tr(self, string, context=''):
  54. if context == '':
  55. context = 'MessageBarProgress'
  56. return QCoreApplication.translate(context, string)