class TextField(QLineEdit, WidgetMixin): changed = Signal() activated = Signal() def __init__(self, parent, text="", read_only=False): QLineEdit.__init__(self, text, parent.get_container()) # Widget.__init__(self, parent) self.init_widget(parent) self.setReadOnly(read_only) self.textChanged.connect(self.__text_changed) self.returnPressed.connect(self.__return_pressed) def get_text(self): return self.text() def set_text(self, text): self.setText(text) def set_cursor_position(self, position): self.setCursorPosition(position) def on_changed(self): pass def __text_changed(self, _): self.changed.emit() self.on_changed() def select_all(self): self.selectAll() def __return_pressed(self): self.activated.emit()
class PagedDialog(fsui.Window): page_changed = Signal() def __init__(self, parent, title): super().__init__(parent, title, minimizable=False, maximizable=False) # buttons, layout = fsui.DialogButtons.create_with_layout(self) # buttons.create_close_button() self.layout = fsui.VerticalLayout() # self.layout.set_padding(20) layout = self.layout hor_layout = fsui.HorizontalLayout() layout.add(hor_layout, fill=True, expand=True) layout_2 = fsui.VerticalLayout() # layout_2.padding_top = 20 # layout_2.padding_bottom = 20 # layout_2.padding_left = 20 self.list_view = fsui.ListView(self, border=(not Skin.fws())) self.list_view.set_min_width(240) self.list_view.item_selected.connect(self.on_select_item) if Skin.fws(): from workspace.ui.theme import Theme theme = Theme.instance() self.list_view.set_row_height(28) # self.list_view.set_background_color(fsui.Color(0xeb, 0xeb, 0xeb)) self.list_view.setStyleSheet(""" QListView {{ padding-top: 20px; background-color: {0}; outline: none; }} QListView::item {{ padding-left: 20px; border: 0px; }} QListView::item:selected {{ background-color: {1}; }} """.format(theme.sidebar_background.to_hex(), theme.selection_background.to_hex())) layout_2.add(self.list_view, fill=True, expand=True) else: layout_2.add(self.list_view, fill=True, expand=True, margin=20, margin_right=0) hor_layout.add(layout_2, fill=True) hor_layout.add_spacer(20) self.page_container = fsui.Panel(self) self.page_container.layout = fsui.VerticalLayout() hor_layout.add(self.page_container, fill=True, expand=True, margin_top=20, margin_right=20, margin_bottom=20) # self.layout.add_spacer(20) hor_layout = fsui.HorizontalLayout() layout.add(hor_layout, fill=True) self.page_titles = [] self.pages = [] # self.add_page(_("Hardware"), HardwarePage) # self.add_page(_("Hard Drives"), HardDrivesPage) # elf.add_page(_("Custom Options"), CustomOptionsPage) self.current_page = None self.set_size((840, 640)) self.center_on_parent() # def on_close_button(self): # self.end_modal(0) def on_select_item(self, index): self.set_page(index) def get_index(self): return self.list_view.get_index() def get_page_title(self, index): return self.page_titles[index][0] def get_page_index_by_title(self, title): for index, t in enumerate(self.page_titles): if t[0] == title: return index return None def add_page(self, title, function, icon=None): self.page_titles.append((title, icon)) self.pages.append(function) self.list_view.set_items(self.page_titles) def set_page(self, index): if self.current_page: self.current_page.hide() self.page_container.layout.remove(self.current_page) if callable(self.pages[index]): page = self.pages[index](self.page_container) self.pages[index] = page else: page = self.pages[index] self.page_container.layout.add(page, fill=True, expand=True) self.current_page = page page.show() # print("calling self.layout.update") self.page_container.layout.update() self.layout.update() self.page_changed.emit() def set_page_by_title(self, title): index = self.get_page_index_by_title(title) if index is not None: # self.set_page(index) self.list_view.set_index(index) return index
class ListView(QListView, WidgetMixin): item_selected = Signal(int) item_activated = Signal(int) def __init__(self, parent, border=True): # self = QListView(parent.get_container()) QListView.__init__(self, parent.get_container()) # Widget.__init__(self, parent) # self.setAutoFillBackground(True) self.init_widget(parent) self.viewport().installEventFilter(self.get_window()) self.verticalScrollBar().installEventFilter(self.get_window()) if not border: self.setFrameStyle(0) # self.setSelectionModel() self._model = QStandardItemModel(self) # self.setModel(self._model) self.setModel(self._model) # self.itemSelectionChanged.connect(self._on_selection_changed) selection_model = self.selectionModel() print("QListView selectionModel", selection_model) selection_model.selectionChanged.connect(self.__selection_changed) self.setEditTriggers(QListView.NoEditTriggers) self.doubleClicked.connect(self.__double_clicked) # self.returnPressed.connect(self.__double_clicked) # self.activated.connect(self.__double_clicked) self._row_height = 26 def set_row_height(self, height): self._row_height = height def keyPressEvent(self, event): if event.key() == Qt.Key_Return: self.__double_clicked() else: super().keyPressEvent(event) def __double_clicked(self): index = self.get_index() if index is not None: self.on_activate_item(index) self.item_activated.emit(index) def on_activate_item(self, index): pass def __selection_changed(self): index = self.get_index() self.on_select_item(index) self.item_selected.emit(index) def set_default_icon(self, image): pass def set_items(self, items): self._model.clear() for item in items: if isinstance(item, str): self.add_item(item) else: label, icon = item self.add_item(label, icon) def clear(self): self._model.clear() def add_item(self, label, icon=None): item = QStandardItem(label) if icon: try: item.setIcon(icon.qicon(16)) except TypeError: item.setIcon(icon.qicon) item.setSizeHint(QSize(-1, self._row_height)) self._model.appendRow(item) def get_item(self, index): return self._model.item(index).text() def get_item_count(self): return self._model.rowCount() def get_index(self): indices = self.selectionModel().selectedIndexes() if len(indices) == 0: return None return indices[0].row() def set_index(self, index): if index is None: index = -1 # print(self.rootIndex) # idx = QModelIndex.createIndex(index) idx = self._model.index(index, 0) self.scrollTo(idx) self.setCurrentIndex(idx) def select_item(self, index): self.set_index(index) def on_select_item(self, index): print("calling item_selected.emit") self.item_selected.emit(index)
class VerticalItemView(QListView, WidgetMixin): item_selected = Signal(int) item_activated = Signal(int) def __init__(self, parent, border=True): QListView.__init__(self, parent.get_container()) # Widget.__init__(self, parent) self.init_widget(parent) self.viewport().installEventFilter(self.get_window()) self.verticalScrollBar().installEventFilter(self.get_window()) if not border: self.setFrameStyle(0) # self.setSelectionModel() # self.model = QStandardItemModel(self) self.model = Model(self) self.setModel(self.model) # self.itemSelectionChanged.connect(self.__selection_changed) selection_model = self.selectionModel() print("VerticalItemView selectionModel = ", selection_model) selection_model.selectionChanged.connect(self.__selection_changed) self.doubleClicked.connect(self.__double_clicked) # self.returnPressed.connect(self.__double_clicked) # self.activated.connect(self.__double_clicked) self._row_height = 26 def set_row_height(self, height): self._row_height = height def keyPressEvent(self, event): if event.key() == Qt.Key_Return: self.__double_clicked() else: QListView.keyPressEvent(self, event) def __double_clicked(self): index = self.get_index() if index is not None: self.on_activate_item(index) self.item_activated.emit(index) def __selection_changed(self): index = self.get_index() self.on_select_item(index) self.item_selected.emit(index) def on_activate_item(self, index): pass def get_item_icon(self, index): return None def get_item_text(self, index): return "" def get_item_text_color(self, index): return None # def set_item_count(self, count): # #self.model.rowCoun # self.model.set_item_count(count) # #self.update() # #self.invalidate() # self.dataChanged(self.model.createIndex(0, 0), # self.model.createIndex(count, 0)) def set_default_icon(self, image): pass # def set_items(self, items): # #print("set_items", items) # self.model.clear() # for label in items: # item = QStandardItem(label) # self.model.appendRow(item) def get_index(self): indices = self.selectionModel().selectedIndexes() if len(indices) == 0: return None return indices[0].row() def set_index(self, index): if index is None: index = -1 # print(self.rootIndex) # idx = QModelIndex.createIndex(index) idx = self.model.index(index, 0) self.scrollTo(idx) self.setCurrentIndex(idx) def select_item(self, index): self.set_index(index) def on_select_item(self, index): pass def update(self): # self.model.rowCoun count = self.get_item_count() # self.model.set_item_count(count) # self.update() # self.invalidate() self.dataChanged(self.model.createIndex(0, 0), self.model.createIndex(count, 0))
class LegacyDialog(QDialog): closed = Signal() def __init__(self, parent=None, title=""): QDialog.__init__(self, QParent(parent)) self._window = weakref.ref(self) self.layout = None self.setWindowTitle(title) # self.container = wx.Panel(self) # self.container.get_window = self.get_window # self.Bind(wx.EVT_SIZE, self.__resize_event) # self.Bind(wx.EVT_WINDOW_DESTROY, self.__destroy_event) # self.Bind(wx.EVT_CLOSE, self.__close_event) self.destroy_listeners = [] self.close_listeners = [] def get_parent(self): return None def closeEvent(self, event): print("Dialog.closeEvent") self.closed.emit() self.on_close() for function in self.close_listeners: print(function) function() event.accept() # remove close listeners so they will not keep the object alive self.close_listeners = [] def add_close_listener(self, function): self.close_listeners.append(function) def get_window(self): return self def get_container(self): return self # def show(self): # self.Show() # def close(self): # self.Close() def show_modal(self): # self.setModal(True) # return self.showModal() return self.exec_() def end_modal(self, value): # self.EndModal(value) self.done(value) def center_on_parent(self): real_parent = self.parent() if real_parent: pp = real_parent.x(), real_parent.y() ps = real_parent.width(), real_parent.height() ss = self.get_size() self.move(pp[0] + (ps[0] - ss[0]) // 2, pp[1] + (ps[1] - ss[1]) // 2) # elif self.default_center: # x, y = self.default_center # ss = self.get_size() # self.move(x - ss[0] // 2, y - ss[1] // 2,) # def destroy(self): # #self.Destroy() # print("FIXME: Dialog.destroy does nothing") def set_title(self, title): self.setWindowTitle(title) def set_size(self, size): # self.SetClientSize(size) # print("FIXME:\n\nDialog.set_size") self.resize(size[0], size[1]) def on_create(self): pass def on_close(self): pass def on_destroy(self): pass def __destroy_event(self, event): self.on_destroy() def __close_event(self, event): print("__close_event") for function in self.close_listeners: function() self.on_close() self.Destroy() def showEvent(self, event): self.on_resize() def get_size(self): return self.width(), self.height() def resizeEvent(self, event): print("resized..") self.on_resize() def on_resize(self): if self.layout: self.layout.set_size(self.get_size()) self.layout.update() def raise_and_activate(self): self.raise_() self.activateWindow()