예제 #1
0
class bands_pairing2(QWidget):
    def __init__(self, dataview):
        super().__init__()
        """a tree widget to create a list of datasets and select bands from each dataset"""

        self.dataview = dataview
        self.firstband = QTreeView()
        self.secondband = QTreeView()
        self.firstband.setModel(self.dataview.model)
        self.secondband.setModel(self.dataview.model)
        self.pairs = treewidget()
        self.pairs.setColumnCount(2)
        self.pairs.setHeaderLabels(['first band', 'second band'])
        self.pairbutton = QPushButton('Pair')

        self.pairbutton.clicked.connect(self.add_pair)
        mainlayout = QVBoxLayout()
        sublayout = QHBoxLayout()
        sublayout.addWidget(self.firstband)
        sublayout.addWidget(self.secondband)
        firstsecondgroup = QGroupBox()
        firstsecondgroup.setLayout(sublayout)
        mainlayout.addWidget(firstsecondgroup)
        mainlayout.addWidget(self.pairbutton)
        mainlayout.addWidget(self.pairs)
        self.maingroup = QGroupBox()
        self.maingroup.setLayout(mainlayout)

    def additems(self):
        """when the widget is activated, a list of selected bands will be added"""
        dict = self.dataview.collect_user_input()
        for i in dict['bandsdict'].keys():
            name = os.path.basename(i)
            for j in dict['bandsdict'][name].keys():
                band = QListWidgetItem(j)
                band2 = QListWidgetItem(j)
                self.firstband.addItem(band)
                self.secondband.addItem(band2)

    def add_pair(self):
        """when pair button is pressed the paire will be add to the list"""
        # get selected index
        firstitem = self.firstband.selectedIndexes()
        # item text (example:band1)
        firstitemtext = self.firstband.model().itemData(firstitem[0])[0]
        # parent text (example: rastername.tif)
        firstparent = self.firstband.model().itemFromIndex(firstitem[0]).parent().data(0)
        #text to appear on the pairing widget
        firstpairtext = os.path.basename(firstparent) + ' - ' + firstitemtext

        # get selected index
        seconditem = self.secondband.selectedIndexes()
        # item text (example:band1)
        seconditemtext = self.secondband.model().itemData(seconditem[0])[0]
        # parent text (example: rastername.tif)
        secondparent = self.firstband.model().itemFromIndex(firstitem[0]).parent().data(0)
        # text to appear on the pairing widget
        secondpairtext = os.path.basename(secondparent) + ' - ' + seconditemtext

        print(firstitemtext)
        print(os.path.basename(firstparent))

        pairitem = QTreeWidgetItem()
        pairitem.setText(0, firstpairtext)
        pairitem.setText(1, secondpairtext)
        self.pairs.addTopLevelItem(pairitem)
        #self.get_pairs()

    def get_pairs(self):
        """returns  1. a dictionary of selected bands created with the datatreeview widget, 2. a list of featurebands,
         3. a list of texts from the items in the widgets (dataset and band number) """
        bandsdict = self.dataview.collectinput()[0]
        print (bandsdict)
        datasets = self.dataview.collectinput()[1]
        selectedbands = self.dataview.collectinput()[2]
        print(selectedbands)
        featurebands = []
        feturebandstext = []
        for i in range(self.pairs.topLevelItemCount()):
            item = self.pairs.topLevelItem(i)
            item1text = item.text(0)
            item2text = item.text(1)
            # get the list of the items from the widget
            feturebandstext += [[item1text, item2text]]
            band1 = bandsdict[item1text]
            band2 = bandsdict[item2text]
            # get the pair of bands by indexes
            pair = [selectedbands.index(band1), selectedbands.index(band2)]
            featurebands += [pair]
        print(selectedbands)
        print(featurebands)
        return [selectedbands, featurebands,feturebandstext]