提交 8e9b508a 编写于 作者: 之一Yo's avatar 之一Yo

fixes #106

上级 e0a949f0
......@@ -5,6 +5,7 @@ QTreeView {
outline: 0;
padding-right: 5px;
font: 13px 'Segoe UI', 'Microsoft YaHei';
selection-background-color: transparent;
}
QTreeView::item {
......
......@@ -5,6 +5,7 @@ QTreeView {
outline: 0;
padding-right: 5px;
font: 13px 'Segoe UI', 'Microsoft YaHei';
selection-background-color: transparent;
}
QTreeView::item {
......
此差异已折叠。
# coding:utf-8
from typing import Dict, Union, List, Iterable
from typing import Union, List, Iterable
from PyQt5.QtCore import Qt, pyqtSignal, QRect, QRectF, QPoint, QObject, QEvent
from PyQt5.QtGui import QColor, QPainter, QCursor, QIcon
from PyQt5.QtWidgets import QAction, QPushButton, QWidget, QStyledItemDelegate, QStyle
from PyQt5.QtCore import Qt, pyqtSignal, QRectF, QPoint, QObject, QEvent
from PyQt5.QtGui import QPainter, QCursor, QIcon
from PyQt5.QtWidgets import QAction, QPushButton, QStyledItemDelegate, QStyle
from .menu import RoundMenu
from .line_edit import LineEdit, LineEditButton
......@@ -59,7 +59,6 @@ class ComboBoxBase(QObject):
self.isHover = False
self.isPressed = False
self.items = [] # type: List[ComboItem]
self.itemMap = {} # type: Dict[str, ComboItem]
self._currentIndex = -1
self.dropMenu = None
......@@ -89,11 +88,7 @@ class ComboBoxBase(QObject):
icon: str | QIcon | FluentIconBase
"""
if not text or text in self.itemMap:
return
item = ComboItem(text, icon, userData)
self.itemMap[text] = item
self.items.append(item)
def addItems(self, texts: Iterable[str]):
......@@ -114,9 +109,7 @@ class ComboBoxBase(QObject):
if not 0 <= index < len(self.items):
return
item = self.items[index]
self.items.pop(index)
self.itemMap.pop(item.text)
if index < self.currentIndex():
self._onItemClicked(self._currentIndex - 1)
......@@ -170,12 +163,14 @@ class ComboBoxBase(QObject):
text: str
text displayed in combo box
"""
if text not in self.itemMap or text == self.currentText():
if text == self.currentText():
return
self.setCurrentIndex(self.items.index(self.itemMap[text]))
index = self.findText(text)
if index >= 0:
self.setCurrentIndex(index)
def setItemText(self, index, text):
def setItemText(self, index: int, text: str):
""" set the text of item
Parameters
......@@ -186,12 +181,10 @@ class ComboBoxBase(QObject):
text: str
new text of item
"""
if text in self.itemMap or not 0 <= index < len(self.items):
if not 0 <= index < len(self.items):
return
item = self.itemMap.pop()
item.text = text
self.itemMap[text] = item
self.items[index].text = text
if self.currentIndex() == index:
self.setText(text)
......@@ -236,10 +229,11 @@ class ComboBoxBase(QObject):
def findText(self, text: str):
""" Returns the index of the item containing the given text; otherwise returns -1. """
if text not in self.itemMap:
return -1
for i, item in enumerate(self.items):
if item.text == text:
return i
return self.items.index(self.itemMap[text])
return -1
def clear(self):
""" Clears the combobox, removing all items. """
......@@ -247,7 +241,6 @@ class ComboBoxBase(QObject):
self.setText('')
self.items.clear()
self.itemMap.clear()
self._currentIndex = -1
def count(self):
......@@ -256,12 +249,8 @@ class ComboBoxBase(QObject):
def insertItem(self, index: int, text: str, icon: Union[str, QIcon, FluentIconBase] = None, userData=None):
""" Inserts item into the combobox at the given index. """
if not text or text in self.itemMap:
return
item = ComboItem(text, icon, userData)
self.items.insert(index, item)
self.itemMap[text] = item
if index <= self.currentIndex():
self._onItemClicked(self.currentIndex() + 1)
......@@ -270,12 +259,8 @@ class ComboBoxBase(QObject):
""" Inserts items into the combobox, starting at the index specified. """
pos = index
for text in texts:
if not text or text in self.itemMap:
continue
item = ComboItem(text)
self.items.insert(pos, item)
self.itemMap[text] = item
pos += 1
if index <= self.currentIndex():
......@@ -381,16 +366,30 @@ class EditableComboBox(LineEdit, ComboBoxBase):
self.dropButton.clicked.connect(self._toggleComboMenu)
self.textEdited.connect(self._onTextEdited)
self.returnPressed.connect(lambda: self.addItem(self.text()))
self.returnPressed.connect(self._onReturnPressed)
def _onTextEdited(self, text: str):
if text not in self.itemMap:
self._currentIndex = -1
else:
self._currentIndex = self.items.index(self.itemMap[text])
def _onReturnPressed(self):
if not self.text():
return
index = self.findText(self.text())
if index >= 0 and index != self.currentIndex():
self._currentIndex = index
self.currentIndexChanged.emit(index)
elif index == -1:
self.addItem(self.text())
self.setCurrentIndex(self.count() - 1)
def _onTextEdited(self, text: str):
self._currentIndex = -1
self.currentTextChanged.emit(text)
for i, item in enumerate(self.items):
if item.text == text:
self._currentIndex = i
self.currentIndexChanged.emit(i)
return
def _onDropMenuClosed(self):
self.dropMenu = None
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册