Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自前実装よりもできるだけStandardItemModelを使うほうが賢い #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 29 additions & 55 deletions PySideLib/QCdtWidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,64 +275,33 @@ def doLayout(self, rect, testOnly):
return y + lineHeight - rect.y()


TListItem = TypeVar('TListItem', bound=QAbstractListModel)
TListItem = TypeVar('TListItem', bound=QStandardItemModel)


class QListModel(QAbstractListModel, Generic[TListItem]):
class QCommonItemModel(QStandardItemModel, Generic[TListItem]):

def __init__(self, parent):
# type: (QObject) -> NoReturn
super(QListModel, self).__init__(parent)
self.__items = [] # type: List[TListItem]
super(QCommonItemModel, self).__init__(parent)

def append(self, item):
# type: (TListItem) -> NoReturn
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount() + 1)
self.__items.append(item)
self.endInsertRows()

def extend(self, items):
# type: (List[TListItem]) -> NoReturn
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount() + len(items))
self.__items.extend(items)
self.endInsertRows()

def remove(self, item):
# type: (TListItem) -> NoReturn
index = self.__items.index(item)
self.beginRemoveRows(QModelIndex(), index, index + 1)
self.__items.remove(item)
self.endRemoveRows()

def reset(self, items):
def replaceRows(self, items):
# type: (List[TListItem]) -> NoReturn
self.beginResetModel()
self.__items = items.copy()
self.endResetModel()

def clear(self):
# type: () -> NoReturn
self.reset([])

def itemFromIndex(self, index):
# type: (Union[int, QModelIndex]) -> TListItem
if isinstance(index, QModelIndex):
index = index.row()
if isinstance(index, int):
return self.__items[index]
raise RuntimeError('the type of "index" must be QModelIndex or int')

def items(self):
# type: () -> List[TListItem]
return self.__items

def rowCount(self, parent=QModelIndex()):
# type: (QModelIndex) -> int
return len(self.__items)
self.clear()
self.appendColumn(items)

def refresh(self):
# type: () -> NoReturn
self.dataChanged.emit(self.index(0), self.index(self.rowCount()))
self.dataChanged.emit(self.index(0, 0), self.index(self.rowCount(), self.columnCount()))

def isIndexValid(self, index):
# type: (QModelIndex) -> bool
if not index.isValid():
return False
if not 0 <= index.row() < self.rowCount():
return False
if not 0 <= index.column() < self.columnCount():
return False
return True


class QFlowView(QListView):
Expand All @@ -351,9 +320,10 @@ class QFlowDirection(object):
TopToBottom = 'TopToBottom'


class QImageFlowItem(object):
class QImageFlowItem(QStandardItem):

def __init__(self):
super().__init__()
self.__image = None # type: Optional[QImage]

def setImage(self, image):
Expand All @@ -376,13 +346,13 @@ def __init__(self, parent):
TImageFlowItem = TypeVar('TImageFlowItem', bound=QImageFlowItem)


class QImageFlowModel(QListModel, Generic[TImageFlowItem]):
class QImageFlowModel(QCommonItemModel, Generic[TImageFlowItem]):

def data(self, index, role=Qt.DisplayRole):
# type: (QModelIndex, int) -> Any
if role != Qt.DecorationRole:
return None
if not index.isValid() or not 0 <= index.row() < self.rowCount():
if not self.isIndexValid(index):
return None

return self.itemFromIndex(index).image()
Expand Down Expand Up @@ -475,7 +445,7 @@ def appendItem(self, item):
model = self.model()
if isinstance(model, QAbstractProxyModel):
model = model.sourceModel()
model.append(item)
model.appendRow(item)
return item

def appendImage(self, image):
Expand Down Expand Up @@ -715,12 +685,16 @@ def setViewMode(self, mode):
return


class QFileListModel(QListModel, Generic[TFileListItem]):
class QFileListModel(QCommonItemModel, Generic[TFileListItem]):

def __init__(self, parent):
# type: (QObject) -> NoReturn
super(QFileListModel, self).__init__(parent)

def columnCount(self, parent=None):
# type: (QModelIndex) -> int
return 1

def setDirectoryPath(self, path):
# type: (Union[str, pathlib.Path]) -> None
if isinstance(path, str):
Expand All @@ -732,15 +706,15 @@ def setDirectoryPath(self, path):
for filePath in [p for p in path.glob('*') if p.is_file()]:
items.append(self.createItem(filePath))

self.reset(items)
self.replaceRows(items)

def createItem(self, path):
# type: (pathlib.Path) -> TFileListItem
return QFileListItem(path)

def data(self, index, role=Qt.DisplayRole):
# type: (QModelIndex, int) -> Any
if not index.isValid() or not 0 <= index.row() < self.rowCount():
if not self.isIndexValid(index):
return None

item = self.itemFromIndex(index)
Expand Down