MultilineTextPanel.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """
  2. ***************************************************************************
  3. MultilineTextPanel.py
  4. ---------------------
  5. Date : January 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__ = 'January 2013'
  19. __copyright__ = '(C) 2013, Victor Olaya'
  20. from qgis.PyQt.QtWidgets import QComboBox, QPlainTextEdit, QSizePolicy, QVBoxLayout, QWidget
  21. class MultilineTextPanel(QWidget):
  22. USE_TEXT = 0
  23. def __init__(self, options, parent=None):
  24. super().__init__(parent)
  25. self.options = options
  26. self.verticalLayout = QVBoxLayout(self)
  27. self.verticalLayout.setSpacing(2)
  28. self.verticalLayout.setMargin(0)
  29. self.combo = QComboBox()
  30. self.combo.addItem(self.tr('[Use text below]'))
  31. for option in options:
  32. self.combo.addItem(option[0], option[1])
  33. self.combo.setSizePolicy(QSizePolicy.Expanding,
  34. QSizePolicy.Expanding)
  35. self.verticalLayout.addWidget(self.combo)
  36. self.textBox = QPlainTextEdit()
  37. self.verticalLayout.addWidget(self.textBox)
  38. self.setLayout(self.verticalLayout)
  39. def setText(self, text):
  40. self.textBox.setPlainText(text)
  41. def getOption(self):
  42. return self.combo.currentIndex()
  43. def getValue(self):
  44. if self.combo.currentIndex() == 0:
  45. return str(self.textBox.toPlainText())
  46. else:
  47. return self.combo.currentData()
  48. def setValue(self, value):
  49. items = [self.combo.itemData(i) for i in range(1, self.combo.count())]
  50. for idx, item in enumerate(items):
  51. if item == value:
  52. self.combo.setCurrentIndex(idx)
  53. return
  54. self.combo.setCurrentIndex(0)
  55. if value:
  56. self.textBox.setPlainText(value)