def resetItems(self): from qtpy.QtGui import QStandardItem from qtpy.QtCore import Qt # Empty the model before reset self.readingListModel.clear() # Reset index = 0 todayIndex = None filterEntry = self.filterEntry.text() for key, value in self.plan.items(): checked, passages = value if not (self.hideCheckedItems and checked) and ( filterEntry == "" or (filterEntry != "" and filterEntry.lower() in passages.lower())): item = QStandardItem("{0}. {1}".format(key, passages)) item.setToolTip("{0}{1}{2}".format(self.translation[8], key, self.translation[9])) if key == self.todayNo: todayIndex = index item.setCheckable(True) item.setCheckState(Qt.CheckState.Checked if checked else Qt. CheckState.Unchecked) self.readingListModel.appendRow(item) index += 1 if todayIndex is not None: self.readingList.setCurrentIndex( self.readingListModel.index(todayIndex, 0))
def add_model(self, model): """ Adds a model to the internal Qt model. Parameters ---------- model : :class:`astropy.modeling.FittableModel` The model instance to add. Returns ------- :class:`qtpy.QtCore.QModelIndex` The index in the Qt model where the new model been added. """ model_name = model.name or model.__class__.name model_count = len([self.item(idx) for idx in range(self.rowCount()) if model_name in self.item(idx).text()]) model_name = model_name + "_" + str(model_count) \ if model_count > 0 else model_name model_item = QStandardItem(model_name) model_item.setData(model, Qt.UserRole + 1) for para_name in model.param_names: # Retrieve the parameter object from the model parameter = getattr(model, para_name) # Store the name value param_name = QStandardItem(parameter.name) param_name.setData(parameter.name, Qt.UserRole + 1) param_name.setEditable(False) # Store the data value of the parameter param_value = QStandardItem("{:.5g}".format(parameter.value)) param_value.setData(parameter.value, Qt.UserRole + 1) # Store the unit information # param_unit = QStandardItem("{}".format(parameter.unit)) param_unit = QStandardItem("Plot Units") param_unit.setData(parameter.unit, Qt.UserRole + 1) param_unit.setEditable(False) # Store the fixed state of the unit param_fixed = QStandardItem() param_fixed.setData(parameter.fixed, Qt.UserRole + 1) param_fixed.setCheckable(True) param_fixed.setEditable(False) model_item.appendRow([param_name, param_value, param_unit, param_fixed]) self.appendRow([model_item, None, None, None]) # Add this model to the model equation string. By default, all models # are simply added together self._equation += " + {}".format(model_name) \ if len(self._equation) > 0 else "{}".format(model_name) return model_item.index()
def populate(self, names): """ Add a list of names to be displayed as list items in the dialog Parameters ---------- names : `list` The list of names to be populated in the dialog """ for s in names: item = QStandardItem(s) item.setCheckable(True) item.setCheckState(Qt.Checked) self._model.appendRow(item)
def selectRendition(self, rendition): from util.GithubUtil import GithubUtil self.selectedRendition = rendition self.downloadTable.setEnabled(True) self.selectedText, self.selectedRepo, self.selectedDirectory = self.bibles[ self.selectedRendition] self.github = GithubUtil(self.selectedRepo) self.repoData = self.github.getRepoData() self.settingBibles = True self.dataViewModel.clear() rowCount = 0 for file in self.repoData.keys(): if len(str(file)) > 3: engFullBookName = file[3:] else: engFullBookName = BibleBooks().eng[str(int(file))][1] item = QStandardItem(file[:3].strip()) folder = os.path.join("audio", "bibles", self.selectedText, self.selectedDirectory, file) folderWithName = os.path.join("audio", "bibles", self.selectedText, self.selectedDirectory, file + " " + engFullBookName) if os.path.exists(folder) or os.path.exists(folderWithName): item.setCheckable(False) item.setCheckState(Qt.Unchecked) item.setEnabled(False) else: item.setCheckable(True) item.setCheckState(Qt.Checked) item.setEnabled(True) self.dataViewModel.setItem(rowCount, 0, item) item = QStandardItem(engFullBookName) self.dataViewModel.setItem(rowCount, 1, item) if os.path.exists(folder) or os.path.exists(folderWithName): item = QStandardItem("Installed") self.dataViewModel.setItem(rowCount, 2, item) else: item = QStandardItem("") self.dataViewModel.setItem(rowCount, 2, item) rowCount += 1 self.dataViewModel.setHorizontalHeaderLabels([ config.thisTranslation["menu_book"], config.thisTranslation["name"], "" ]) self.downloadTable.setColumnWidth(0, 90) self.downloadTable.setColumnWidth(1, 125) self.downloadTable.setColumnWidth(2, 125) # self.downloadTable.resizeColumnsToContents() self.settingBibles = False
def __init__(self, parent, info, title="Channel Properties"): super().__init__(parent) self.setWindowTitle(title) self.model = QStandardItemModel(info["nchan"], 4) self.model.setHorizontalHeaderLabels(["#", "Label", "Type", "Bad"]) for index, ch in enumerate(info["chs"]): item = QStandardItem() item.setData(index, Qt.DisplayRole) item.setFlags(item.flags() & ~Qt.ItemIsEditable) self.model.setItem(index, 0, item) self.model.setItem(index, 1, QStandardItem(ch["ch_name"])) kind = channel_type(info, index).upper() self.model.setItem(index, 2, QStandardItem(str(kind))) bad = QStandardItem() bad.setData(ch["ch_name"] in info["bads"], Qt.UserRole) bad.setCheckable(True) bad.setEditable(False) checked = ch["ch_name"] in info["bads"] bad.setCheckState(Qt.Checked if checked else Qt.Unchecked) self.model.setItem(index, 3, bad) self.model.itemChanged.connect(bad_changed) self.proxymodel = MySortFilterProxyModel() self.proxymodel.setDynamicSortFilter(False) self.proxymodel.setSourceModel(self.model) self.view = QTableView() self.view.setModel(self.proxymodel) self.view.setItemDelegateForColumn(2, ComboBoxDelegate(self.view)) self.view.setEditTriggers(QAbstractItemView.AllEditTriggers) self.view.verticalHeader().setVisible(False) self.view.horizontalHeader().setStretchLastSection(True) self.view.setShowGrid(False) self.view.setSelectionMode(QAbstractItemView.NoSelection) self.view.setSortingEnabled(True) self.view.sortByColumn(0, Qt.AscendingOrder) vbox = QVBoxLayout(self) vbox.addWidget(self.view) self.buttonbox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) vbox.addWidget(self.buttonbox) self.buttonbox.accepted.connect(self.accept) self.buttonbox.rejected.connect(self.reject) self.resize(475, 650) self.view.setColumnWidth(0, 70) self.view.setColumnWidth(1, 155) self.view.setColumnWidth(2, 90)
def add_model(self, model): model_name = model.__class__.name model_count = len([ self.item(idx) for idx in range(self.rowCount()) if model.__class__.name in self.item(idx).text() ]) model_name = model_name + str( model_count) if model_count > 0 else model_name model_item = QStandardItem(model_name) model_item.setData(model, Qt.UserRole + 1) for para_name in model.param_names: # Retrieve the parameter object from the model parameter = getattr(model, para_name) # Store the name value param_name = QStandardItem(parameter.name) param_name.setData(parameter.name, Qt.UserRole + 1) param_name.setEditable(False) # Store the data value of the parameter param_value = QStandardItem("{:.5g}".format(parameter.value)) param_value.setData(parameter.value, Qt.UserRole + 1) # Store the unit information # param_unit = QStandardItem("{}".format(parameter.unit)) param_unit = QStandardItem("Plot Units") param_unit.setData(parameter.unit, Qt.UserRole + 1) param_unit.setEditable(False) # Store the fixed state of the unit param_fixed = QStandardItem() param_fixed.setData(parameter.fixed, Qt.UserRole + 1) param_fixed.setCheckable(True) param_fixed.setEditable(False) model_item.appendRow( [param_name, param_value, param_unit, param_fixed]) self.appendRow([model_item, None, None, None]) # Add this model to the model equation string. By default, all models # are simply added together self._equation += " + {}".format(model_name) \ if len(self._equation) > 0 else "{}".format(model_name) return model_item.index()
def reloadFilters(self): self.filters = self.db.getAll() self.dataViewModel.clear() rowCount = 0 for bible, description in self.filters: item = QStandardItem(bible) item.setToolTip(bible) item.setCheckable(True) self.dataViewModel.setItem(rowCount, 0, item) item = QStandardItem(description) self.dataViewModel.setItem(rowCount, 1, item) rowCount += 1 self.dataViewModel.setHorizontalHeaderLabels([ config.thisTranslation["filter2"], config.thisTranslation["pattern"] ]) self.filtersTable.resizeColumnsToContents()
def populatePortList(self, model, tv, ports): tv.setModel(model) root = model.invisibleRootItem() portsdict = {} for port in ports: if port.client not in portsdict: portsdict[port.client] = [] portsdict[port.client].append(port) for client in humansorted(portsdict): clientitem = QStandardItem(client) for port in humansorted(portsdict[client], key=attrgetter("group", "order", "name")): portspec = (port.client, port.name) if port.pretty_name: label = "%s (%s)" % (port.pretty_name, port.name) else: label = port.name portitem = QStandardItem(label) portitem.setData(portspec) portitem.setCheckable(True) portitem.setUserTristate(False) # Check box toggling is done in the treeview clicked handler "on_port_clicked" portitem.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled) portitem.setToolTip(self.makePortTooltip(port)) if portspec in self.rec_sources: portitem.setCheckState(2) clientitem.appendRow(portitem) root.appendRow(clientitem) tv.expandAll()
def resetItems(self): self.isUpdating = True # Empty the model before reset self.dataViewModel.clear() # Reset self.data = self.getOptions() filterEntry = self.filterEntry.text().lower() rowCount = 0 for flag, value in self.data.items(): configValue, default, tooltip, *_ = value if filterEntry == "" or (filterEntry != "" and (filterEntry in flag.lower() or filterEntry in tooltip.lower())): # 1st column item = QStandardItem(flag) item.setToolTip(tooltip) item.setCheckable(True) item.setCheckState(Qt.CheckState.Checked if configValue else Qt.CheckState.Unchecked) self.dataViewModel.setItem(rowCount, 0, item) # 2nd column item = QStandardItem(str(default)) self.dataViewModel.setItem(rowCount, 1, item) # 3rd column tooltip = tooltip.replace("\n", " ") item = QStandardItem(tooltip) item.setToolTip(tooltip) self.dataViewModel.setItem(rowCount, 2, item) # add row count rowCount += 1 self.dataViewModel.setHorizontalHeaderLabels([ config.thisTranslation["flag"], config.thisTranslation["default"], config.thisTranslation["description"] ]) self.dataView.resizeColumnsToContents() self.isUpdating = False
def loadBibleSelection(self): self.settingBibles = True self.dataViewModel.clear() biblesInCollection = [] if self.selectedCollection is not None: biblesInCollection = config.bibleCollections[ self.selectedCollection] rowCount = 0 for bible, description in self.bibles: item = QStandardItem(bible) item.setToolTip(bible) item.setCheckable(True) if bible in biblesInCollection: item.setCheckState(Qt.Checked) self.dataViewModel.setItem(rowCount, 0, item) item = QStandardItem(description) self.dataViewModel.setItem(rowCount, 1, item) rowCount += 1 self.dataViewModel.setHorizontalHeaderLabels([ config.thisTranslation["bible"], config.thisTranslation["description"] ]) self.biblesTable.resizeColumnsToContents() self.settingBibles = False
def setupUI(self): mainLayout = QVBoxLayout() mainLayout.addWidget(QLabel(self.translation[0])) subLayout = QHBoxLayout() layout = QVBoxLayout() layout.addWidget(QLabel(self.translation[1])) dataView1 = QListView() dataView1.setEditTriggers(QAbstractItemView.NoEditTriggers) dataViewModel1 = QStandardItemModel(dataView1) dataView1.setModel(dataViewModel1) for plugin in FileUtil.fileNamesWithoutExtension( os.path.join("plugins", "startup"), "py"): item = QStandardItem(plugin) item.setToolTip(plugin) item.setCheckable(True) item.setCheckState( Qt.CheckState.Unchecked if plugin in config.excludeStartupPlugins else Qt.CheckState.Checked) dataViewModel1.appendRow(item) dataViewModel1.itemChanged.connect(self.itemChanged1) layout.addWidget(dataView1) subLayout.addLayout(layout) layout = QVBoxLayout() layout.addWidget(QLabel(self.translation[2])) dataView2 = QListView() dataView2.setEditTriggers(QAbstractItemView.NoEditTriggers) dataViewModel2 = QStandardItemModel(dataView2) dataView2.setModel(dataViewModel2) for plugin in FileUtil.fileNamesWithoutExtension( os.path.join("plugins", "menu"), "py"): item = QStandardItem(plugin) item.setToolTip(plugin) item.setCheckable(True) item.setCheckState(Qt.CheckState.Unchecked if plugin in config. excludeMenuPlugins else Qt.CheckState.Checked) dataViewModel2.appendRow(item) dataViewModel2.itemChanged.connect(self.itemChanged2) layout.addWidget(dataView2) subLayout.addLayout(layout) layout = QVBoxLayout() layout.addWidget(QLabel(self.translation[3])) dataView3 = QListView() dataView3.setEditTriggers(QAbstractItemView.NoEditTriggers) dataViewModel3 = QStandardItemModel(dataView3) dataView3.setModel(dataViewModel3) for plugin in FileUtil.fileNamesWithoutExtension( os.path.join("plugins", "context"), "py"): item = QStandardItem(plugin) item.setToolTip(plugin) item.setCheckable(True) item.setCheckState( Qt.CheckState.Unchecked if plugin in config.excludeContextPlugins else Qt.CheckState.Checked) dataViewModel3.appendRow(item) dataViewModel3.itemChanged.connect(self.itemChanged3) layout.addWidget(dataView3) subLayout.addLayout(layout) layout = QVBoxLayout() layout.addWidget(QLabel(self.translation[4])) dataView4 = QListView() dataView4.setEditTriggers(QAbstractItemView.NoEditTriggers) dataViewModel4 = QStandardItemModel(dataView4) dataView4.setModel(dataViewModel4) for plugin in FileUtil.fileNamesWithoutExtension( os.path.join("plugins", "shutdown"), "py"): item = QStandardItem(plugin) item.setToolTip(plugin) item.setCheckable(True) item.setCheckState( Qt.CheckState.Unchecked if plugin in config.excludeShutdownPlugins else Qt.CheckState.Checked) dataViewModel4.appendRow(item) dataViewModel4.itemChanged.connect(self.itemChanged4) layout.addWidget(dataView4) subLayout.addLayout(layout) mainLayout.addLayout(subLayout) self.setLayout(mainLayout)
else: flags = super(DerivedDataModel, self).flags(index) return flags if __name__ == "__main__": from qtpy.QtWidgets import QApplication, QMainWindow, QAction app = QApplication([]) window = QMainWindow() layout = QVBoxLayout() model = DerivedDataModel() from xicam.plugins.hints import PlotHint, ImageHint, CoPlotHint parentItem = QStandardItem("blah") parentItem.setCheckable(True) import numpy as np for i in range(3): hint = PlotHint(np.arange(10), np.random.random((10, )), name=f"1-Time") item = QStandardItem(hint.group) item.setData(hint, Qt.UserRole) item.setCheckable(True) parentItem.appendRow(item) hint = ImageHint(np.random.random((100, 100)), xlabel="x", ylabel="y", name="2-Time") item = QStandardItem(hint.group) item.setData(hint, Qt.UserRole)