MultipleFileInputDialog.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """
  2. ***************************************************************************
  3. MultipleExternalInputDialog.py
  4. ---------------------
  5. Date : August 2012
  6. Copyright : (C) 2012 by Victor Olaya
  7. (C) 2013 by CS Systemes d'information (CS SI)
  8. Email : volayaf at gmail dot com
  9. otb at c-s dot fr (CS SI)
  10. Contributors : Victor Olaya - basis from MultipleInputDialog
  11. Alexia Mondot (CS SI) - new parameter
  12. ***************************************************************************
  13. * *
  14. * This program is free software; you can redistribute it and/or modify *
  15. * it under the terms of the GNU General Public License as published by *
  16. * the Free Software Foundation; either version 2 of the License, or *
  17. * (at your option) any later version. *
  18. * *
  19. ***************************************************************************
  20. """
  21. __author__ = 'Victor Olaya'
  22. __date__ = 'August 2012'
  23. __copyright__ = '(C) 2012, Victor Olaya'
  24. import os
  25. import warnings
  26. from qgis.core import QgsSettings
  27. from qgis.PyQt import uic
  28. from qgis.PyQt.QtCore import QByteArray
  29. from qgis.PyQt.QtWidgets import QDialog, QAbstractItemView, QPushButton, QDialogButtonBox, QFileDialog
  30. from qgis.PyQt.QtGui import QStandardItemModel, QStandardItem
  31. pluginPath = os.path.split(os.path.dirname(__file__))[0]
  32. with warnings.catch_warnings():
  33. warnings.filterwarnings("ignore", category=DeprecationWarning)
  34. WIDGET, BASE = uic.loadUiType(
  35. os.path.join(pluginPath, 'ui', 'DlgMultipleSelection.ui'))
  36. class MultipleFileInputDialog(BASE, WIDGET):
  37. def __init__(self, options):
  38. super().__init__(None)
  39. self.setupUi(self)
  40. self.lstLayers.setSelectionMode(QAbstractItemView.ExtendedSelection)
  41. self.selectedoptions = options
  42. # Additional buttons
  43. self.btnAdd = QPushButton(self.tr('Add file'))
  44. self.buttonBox.addButton(self.btnAdd,
  45. QDialogButtonBox.ActionRole)
  46. self.btnRemove = QPushButton(self.tr('Remove file(s)'))
  47. self.buttonBox.addButton(self.btnRemove,
  48. QDialogButtonBox.ActionRole)
  49. self.btnRemoveAll = QPushButton(self.tr('Remove all'))
  50. self.buttonBox.addButton(self.btnRemoveAll,
  51. QDialogButtonBox.ActionRole)
  52. self.btnAdd.clicked.connect(self.addFile)
  53. self.btnRemove.clicked.connect(lambda: self.removeRows())
  54. self.btnRemoveAll.clicked.connect(lambda: self.removeRows(True))
  55. self.settings = QgsSettings()
  56. self.restoreGeometry(self.settings.value("/Processing/multipleFileInputDialogGeometry", QByteArray()))
  57. self.populateList()
  58. self.finished.connect(self.saveWindowGeometry)
  59. def saveWindowGeometry(self):
  60. self.settings.setValue("/Processing/multipleInputDialogGeometry", self.saveGeometry())
  61. def populateList(self):
  62. model = QStandardItemModel()
  63. for option in self.selectedoptions:
  64. item = QStandardItem(option)
  65. model.appendRow(item)
  66. self.lstLayers.setModel(model)
  67. def accept(self):
  68. self.selectedoptions = []
  69. model = self.lstLayers.model()
  70. for i in range(model.rowCount()):
  71. item = model.item(i)
  72. self.selectedoptions.append(item.text())
  73. QDialog.accept(self)
  74. def reject(self):
  75. QDialog.reject(self)
  76. def addFile(self):
  77. settings = QgsSettings()
  78. if settings.contains('/Processing/LastInputPath'):
  79. path = settings.value('/Processing/LastInputPath')
  80. else:
  81. path = ''
  82. files, selected_filter = QFileDialog.getOpenFileNames(self,
  83. self.tr('Select File(s)'), path, self.tr('All files (*.*)'))
  84. if len(files) == 0:
  85. return
  86. model = self.lstLayers.model()
  87. for filePath in files:
  88. item = QStandardItem(filePath)
  89. model.appendRow(item)
  90. settings.setValue('/Processing/LastInputPath',
  91. os.path.dirname(files[0]))
  92. def removeRows(self, removeAll=False):
  93. if removeAll:
  94. self.lstLayers.model().clear()
  95. else:
  96. self.lstLayers.setUpdatesEnabled(False)
  97. indexes = sorted(self.lstLayers.selectionModel().selectedIndexes())
  98. for i in reversed(indexes):
  99. self.lstLayers.model().removeRow(i.row())
  100. self.lstLayers.setUpdatesEnabled(True)