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'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
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'))
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
|
|
|
2015-05-18 21:04:20 +03:00
|
|
|
class MultipleInputDialog(BASE, WIDGET):
|
2013-10-02 20:04:57 +03:00
|
|
|
|
|
|
|
def __init__(self, options, selectedoptions=None):
|
2015-05-18 21:04:20 +03:00
|
|
|
super(MultipleInputDialog, self).__init__(None)
|
2013-10-02 20:04:57 +03:00
|
|
|
self.setupUi(self)
|
|
|
|
|
2014-11-08 15:55:38 +02:00
|
|
|
self.lstLayers.setSelectionMode(QAbstractItemView.NoSelection)
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
self.options = options
|
2014-04-14 08:56:00 +02:00
|
|
|
self.selectedoptions = selectedoptions or []
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-02 20:04:57 +03:00
|
|
|
# Additional buttons
|
2013-10-03 20:32:55 +03:00
|
|
|
self.btnSelectAll = QPushButton(self.tr('Select all'))
|
2013-10-02 20:04:57 +03:00
|
|
|
self.buttonBox.addButton(self.btnSelectAll,
|
|
|
|
QDialogButtonBox.ActionRole)
|
2013-10-03 20:32:55 +03:00
|
|
|
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)
|
2013-10-02 20:04:57 +03:00
|
|
|
|
2014-05-20 16:30:09 +02:00
|
|
|
self.btnSelectAll.clicked.connect(lambda: self.selectAll(True))
|
|
|
|
self.btnClearSelection.clicked.connect(lambda: self.selectAll(False))
|
2013-10-03 20:32:55 +03:00
|
|
|
self.btnToggleSelection.clicked.connect(self.toggleSelection)
|
2013-10-02 20:04:57 +03:00
|
|
|
|
2013-10-03 20:32:55 +03:00
|
|
|
self.populateList()
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-03 20:32:55 +03:00
|
|
|
def populateList(self):
|
2014-05-21 21:25:18 +02:00
|
|
|
model = QStandardItemModel()
|
2014-05-20 16:30:09 +02:00
|
|
|
for i, option in enumerate(self.options):
|
2014-05-21 21:25:18 +02:00
|
|
|
item = QStandardItem(option)
|
2014-05-20 16:30:09 +02:00
|
|
|
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
|
|
|
|
2013-10-02 20:04:57 +03:00
|
|
|
def accept(self):
|
2012-09-15 18:25:25 +03:00
|
|
|
self.selectedoptions = []
|
2014-05-20 16:30:09 +02:00
|
|
|
model = self.lstLayers.model()
|
2016-09-21 18:24:26 +02:00
|
|
|
for i in range(model.rowCount()):
|
2014-05-20 16:30:09 +02:00
|
|
|
item = model.item(i)
|
|
|
|
if item.checkState() == Qt.Checked:
|
|
|
|
self.selectedoptions.append(i)
|
2013-10-02 20:04:57 +03:00
|
|
|
QDialog.accept(self)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-02 20:04:57 +03:00
|
|
|
def reject(self):
|
2012-09-15 18:25:25 +03:00
|
|
|
self.selectedoptions = None
|
2013-10-02 20:04:57 +03:00
|
|
|
QDialog.reject(self)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2014-05-21 21:25:18 +02:00
|
|
|
def selectAll(self, value):
|
2014-05-20 16:30:09 +02:00
|
|
|
model = self.lstLayers.model()
|
2016-09-21 18:24:26 +02:00
|
|
|
for i in range(model.rowCount()):
|
2014-05-20 16:30:09 +02:00
|
|
|
item = model.item(i)
|
|
|
|
item.setCheckState(Qt.Checked if value else Qt.Unchecked)
|
2014-05-21 21:25:18 +02:00
|
|
|
|
|
|
|
def toggleSelection(self):
|
2014-05-20 16:30:09 +02:00
|
|
|
model = self.lstLayers.model()
|
2016-09-21 18:24:26 +02:00
|
|
|
for i in range(model.rowCount()):
|
2014-05-20 16:30:09 +02:00
|
|
|
item = model.item(i)
|
|
|
|
checked = item.checkState() == Qt.Checked
|
|
|
|
item.setCheckState(Qt.Unchecked if checked else Qt.Checked)
|