QGIS/python/plugins/processing/gui/MultipleInputDialog.py

104 lines
3.8 KiB
Python
Raw Normal View History

2012-10-05 23:28:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
MultipleInputDialog.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
2016-09-21 18:24:26 +02:00
from builtins import range
2012-10-05 23:28:47 +02:00
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
2012-10-05 23:28:47 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2012-10-05 23:28:47 +02:00
__revision__ = '$Format:%H$'
2015-05-18 21:04:20 +03:00
import os
2016-04-29 11:39:26 +02:00
from qgis.PyQt import uic
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import QDialog, QAbstractItemView, QPushButton, QDialogButtonBox
from qgis.PyQt.QtGui import QStandardItemModel, QStandardItem
2012-09-15 18:25:25 +03:00
2015-05-18 21:04:20 +03:00
pluginPath = os.path.split(os.path.dirname(__file__))[0]
WIDGET, BASE = uic.loadUiType(
os.path.join(pluginPath, 'ui', 'DlgMultipleSelection.ui'))
2015-05-18 21:04:20 +03:00
class MultipleInputDialog(BASE, WIDGET):
def __init__(self, options, selectedoptions=None):
2015-05-18 21:04:20 +03:00
super(MultipleInputDialog, self).__init__(None)
self.setupUi(self)
self.lstLayers.setSelectionMode(QAbstractItemView.NoSelection)
2012-09-15 18:25:25 +03:00
self.options = options
self.selectedoptions = selectedoptions or []
2012-09-15 18:25:25 +03:00
# Additional buttons
self.btnSelectAll = QPushButton(self.tr('Select all'))
self.buttonBox.addButton(self.btnSelectAll,
QDialogButtonBox.ActionRole)
self.btnClearSelection = QPushButton(self.tr('Clear selection'))
self.buttonBox.addButton(self.btnClearSelection,
QDialogButtonBox.ActionRole)
self.btnToggleSelection = QPushButton(self.tr('Toggle selection'))
self.buttonBox.addButton(self.btnToggleSelection,
QDialogButtonBox.ActionRole)
self.btnSelectAll.clicked.connect(lambda: self.selectAll(True))
self.btnClearSelection.clicked.connect(lambda: self.selectAll(False))
self.btnToggleSelection.clicked.connect(self.toggleSelection)
self.populateList()
2012-09-15 18:25:25 +03:00
def populateList(self):
2014-05-21 21:25:18 +02:00
model = QStandardItemModel()
for i, option in enumerate(self.options):
2014-05-21 21:25:18 +02:00
item = QStandardItem(option)
item.setCheckState(Qt.Checked if i in self.selectedoptions else Qt.Unchecked)
item.setCheckable(True)
model.appendRow(item)
self.lstLayers.setModel(model)
2012-09-15 18:25:25 +03:00
def accept(self):
2012-09-15 18:25:25 +03:00
self.selectedoptions = []
model = self.lstLayers.model()
2016-09-21 18:24:26 +02:00
for i in range(model.rowCount()):
item = model.item(i)
if item.checkState() == Qt.Checked:
self.selectedoptions.append(i)
QDialog.accept(self)
2012-09-15 18:25:25 +03:00
def reject(self):
2012-09-15 18:25:25 +03:00
self.selectedoptions = None
QDialog.reject(self)
2012-09-15 18:25:25 +03:00
2014-05-21 21:25:18 +02:00
def selectAll(self, value):
model = self.lstLayers.model()
2016-09-21 18:24:26 +02:00
for i in range(model.rowCount()):
item = model.item(i)
item.setCheckState(Qt.Checked if value else Qt.Unchecked)
2014-05-21 21:25:18 +02:00
def toggleSelection(self):
model = self.lstLayers.model()
2016-09-21 18:24:26 +02:00
for i in range(model.rowCount()):
item = model.item(i)
checked = item.checkState() == Qt.Checked
item.setCheckState(Qt.Unchecked if checked else Qt.Checked)