Added combobox to the main view for displaying unique labels individually

This commit is contained in:
Hakan Karaoguz 2019-07-16 13:52:57 +02:00 committed by darrenl
parent bf8cc1d575
commit e51bfce9a7
2 changed files with 58 additions and 1 deletions

25
combobox.py Normal file
View File

@ -0,0 +1,25 @@
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class ComboBox(QWidget):
def __init__(self, parent=None, items=[]):
super(ComboBox, self).__init__(parent)
layout = QHBoxLayout()
self.cb = QComboBox()
self.items = items
self.cb.addItems(self.items)
self.cb.currentIndexChanged.connect(parent.comboSelectionChanged)
layout.addWidget(self.cb)
self.setLayout(layout)
def update_items(self, items):
self.items = items
self.cb.clear()
self.cb.addItems(self.items)

View File

@ -26,6 +26,7 @@ except ImportError:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from combobox import ComboBox
from libs.resources import *
from libs.constants import *
from libs.utils import *
@ -136,6 +137,10 @@ class MainWindow(QMainWindow, WindowMixin):
listLayout.addWidget(self.diffcButton)
listLayout.addWidget(useDefaultLabelContainer)
# Create and add combobox for showing unique labels in group
self.comboBox = ComboBox(self)
listLayout.addWidget(self.comboBox)
# Create and add a widget for showing current label items
self.labelList = QListWidget()
labelListContainer = QWidget()
@ -147,6 +152,8 @@ class MainWindow(QMainWindow, WindowMixin):
self.labelList.itemChanged.connect(self.labelItemChanged)
listLayout.addWidget(self.labelList)
self.dock = QDockWidget(getStr('boxLabelText'), self)
self.dock.setObjectName(getStr('labels'))
self.dock.setWidget(labelListContainer)
@ -573,6 +580,7 @@ class MainWindow(QMainWindow, WindowMixin):
self.labelFile = None
self.canvas.resetState()
self.labelCoordinates.clear()
self.comboBox.cb.clear()
def currentItem(self):
items = self.labelList.selectedItems()
@ -670,6 +678,7 @@ class MainWindow(QMainWindow, WindowMixin):
item.setText(text)
item.setBackground(generateColorByText(text))
self.setDirty()
self.updateComboBox()
# Tzutalin 20160906 : Add file list and dock to move faster
def fileitemDoubleClicked(self, item=None):
@ -733,6 +742,7 @@ class MainWindow(QMainWindow, WindowMixin):
self.labelList.addItem(item)
for action in self.actions.onShapesPresent:
action.setEnabled(True)
self.updateComboBox()
def remLabel(self, shape):
if shape is None:
@ -742,6 +752,7 @@ class MainWindow(QMainWindow, WindowMixin):
self.labelList.takeItem(self.labelList.row(item))
del self.shapesToItems[shape]
del self.itemsToShapes[item]
self.updateComboBox()
def loadLabels(self, shapes):
s = []
@ -770,9 +781,20 @@ class MainWindow(QMainWindow, WindowMixin):
shape.fill_color = generateColorByText(label)
self.addLabel(shape)
self.updateComboBox()
self.canvas.loadShapes(s)
def updateComboBox(self):
# Get the unique labels and add them to the Combobox.
itemsTextList = [str(self.labelList.item(i).text()) for i in range(self.labelList.count())]
uniqueTextList = list(set(itemsTextList))
# Add a null row for showing all the labels
uniqueTextList.append("")
uniqueTextList.sort()
self.comboBox.update_items(uniqueTextList)
def saveLabels(self, annotationFilePath):
annotationFilePath = ustr(annotationFilePath)
if self.labelFile is None:
@ -814,6 +836,16 @@ class MainWindow(QMainWindow, WindowMixin):
# fix copy and delete
self.shapeSelectionChanged(True)
def comboSelectionChanged(self, index):
text = self.comboBox.cb.itemText(index)
for i in range(self.labelList.count()):
if text == "":
self.labelList.item(i).setCheckState(2)
elif text != self.labelList.item(i).text():
self.labelList.item(i).setCheckState(0)
else:
self.labelList.item(i).setCheckState(2)
def labelSelectionChanged(self):
item = self.currentItem()
if item and self.canvas.editing():