Skip to content

Commit

Permalink
Update TabView for catalogs
Browse files Browse the repository at this point in the history
  • Loading branch information
ihumphrey committed Nov 4, 2019
1 parent d727f70 commit cea06d1
Showing 1 changed file with 38 additions and 23 deletions.
61 changes: 38 additions & 23 deletions xicam/gui/widgets/tabview.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from warnings import warn

from qtpy.QtWidgets import QTabBar, QMenu, QAction, QTabWidget
from qtpy.QtGui import QStandardItemModel, QMouseEvent
from qtpy.QtCore import QItemSelectionModel, QObject, Qt
Expand All @@ -8,13 +10,14 @@

class TabView(QTabWidget):
def __init__(
self,
headermodel: QStandardItemModel = None,
selectionmodel: QItemSelectionModel = None,
widgetcls=None,
field=None,
bindings: List[tuple] = [],
**kwargs,
self,
catalogmodel: QStandardItemModel = None,
selectionmodel: QItemSelectionModel = None,
widgetcls=None,
stream=None,
field=None,
bindings: List[tuple] = [],
**kwargs,
):
"""
Expand All @@ -33,38 +36,48 @@ def __init__(
self.kwargs = kwargs

self.setWidgetClass(widgetcls)
self.headermodel = None
self.catalogmodel = None
self.selectionmodel = None
if selectionmodel:
self.setSelectionModel(selectionmodel) # type: TabItemSelectionModel

if headermodel:
self.setHeaderModel(headermodel)
if catalogmodel:
self.setCatalogModel(catalogmodel)
self.stream = stream
self.field = field
self.bindings = bindings

self.setTabsClosable(True)
self.setDocumentMode(True)
self.tabCloseRequested.connect(self.closeTab)

def setHeaderModel(self, model: QStandardItemModel):
self.headermodel = model
def setCatalogModel(self, model: QStandardItemModel):
self.catalogmodel = model
if not self.selectionmodel:
self.selectionmodel = TabItemSelectionModel(self)
model.dataChanged.connect(self.dataChanged)

def setHeaderModel(self, model):
self.setCatalogModel(model)
warn(DeprecationWarning())

def setSelectionModel(self, model: QItemSelectionModel):
self.selectionmodel = model
self.selectionmodel.currentChanged.connect(lambda current, previous: self.setCurrentIndex(current.row()))
self.currentChanged.connect(
lambda i: self.selectionmodel.setCurrentIndex(self.headermodel.index(i, 0), QItemSelectionModel.Rows)
lambda i: self.selectionmodel.setCurrentIndex(self.catalogmodel.index(i, 0), QItemSelectionModel.Rows)
)
self.currentChanged.connect(
lambda _: self.selectionmodel.setCurrentIndex(self.headermodel.index(self.currentIndex(), 0),
lambda _: self.selectionmodel.setCurrentIndex(self.catalogmodel.index(self.currentIndex(), 0),
QItemSelectionModel.ClearAndSelect))

def dataChanged(self, start, end):
for i in range(self.headermodel.rowCount()):
for i in range(self.catalogmodel.rowCount()):
itemdata = None
if hasattr(self.catalogmodel.item(i), "header"):
itemdata = self.catalogmodel.item(i).header
else:
itemdata = self.catalogmodel.item(i).data(Qt.UserRole)

if self.widget(i):
if self.widget(i).header == self.headermodel.item(i).header:
Expand All @@ -76,7 +89,7 @@ def dataChanged(self, start, end):
f"A widget of type {self.widgetcls} could not be initialized with args: {self.headermodel.item(i).header, self.field, self.kwargs}"
)
msg.logError(ex)
self.headermodel.removeRow(i)
self.catalogmodel.removeRow(i)
self.dataChanged(0, 0)
return

Expand All @@ -88,25 +101,27 @@ def dataChanged(self, start, end):
receiver = getattr(newwidget, receiver)
sender.connect(receiver)

for i in reversed(range(self.headermodel.rowCount(), self.count())):
for i in reversed(range(self.catalogmodel.rowCount(), self.count())):
self.removeTab(i)

def setWidgetClass(self, cls):
self.widgetcls = cls

def currentCatalog(self):
return self.catalogmodel.item(self.currentIndex())

def currentHeader(self):
return self.headermodel.item(self.currentIndex())
self.currentCatalog()
warn(DeprecationWarning())

def closeTab(self, i):
newindex = self.currentIndex()
if i <= self.currentIndex():
newindex -= 1

self.removeTab(i)
index = self.headermodel.item(i, 0).index()
self.headermodel.removeRow(i)
self.headermodel.dataChanged.emit(index, index)
self.selectionmodel.setCurrentIndex(self.headermodel.index(newindex, 0), QItemSelectionModel.Rows)
self.catalogmodel.removeRow(i)
self.selectionmodel.setCurrentIndex(self.catalogmodel.index(newindex, 0), QItemSelectionModel.Rows)


class TabViewSynchronizer(QObject):
Expand Down Expand Up @@ -160,7 +175,7 @@ def mousePressEvent(self, event: QMouseEvent):

class TabItemSelectionModel(QItemSelectionModel):
def __init__(self, tabview: TabView):
super(TabItemSelectionModel, self).__init__(tabview.headermodel)
super(TabItemSelectionModel, self).__init__(tabview.catalogmodel)
self.tabview = tabview

def currentIndex(self):
Expand Down

0 comments on commit cea06d1

Please sign in to comment.