class FormTabWidget(QWidget): def __init__(self, datalist, comment="", parent=None): QWidget.__init__(self, parent) layout = QHBoxLayout() self.contentsWidget = QListWidget() self.contentsWidget.setViewMode(QListView.ListMode) self.contentsWidget.setMovement(QListView.Static) self.contentsWidget.setMaximumWidth(128) self.pagesWidget = QStackedWidget() layout.addWidget(self.contentsWidget) layout.addWidget(self.pagesWidget) self.setLayout(layout) self.widgetlist = [] for elem in datalist: if len(elem) == 4: data, title, comment, icon = elem else: data, title, comment = elem icon = None if len(data[0]) == 3: widget = FormComboWidget(data, comment=comment, parent=self) else: widget = FormWidget(data, comment=comment, parent=self) #index = self.tabwidget.addTab(widget, title) #self.tabwidget.setTabToolTip(index, comment) self.pagesWidget.addWidget(widget) contentItem = QListWidgetItem(self.contentsWidget) if icon: contentItem.setIcon(icon) contentItem.setText(title) contentItem.setToolTip(comment) contentItem.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled) self.contentsWidget.addItem(contentItem) self.widgetlist.append(widget) self.contentsWidget.currentRowChanged.connect(self.changePage) def changePage(self, current): if current != -1: self.pagesWidget.setCurrentIndex(current) def setup(self): for widget in self.widgetlist: widget.setup() def get(self): return [ widget.get() for widget in self.widgetlist]
def createListOption(self): def addItem(label, icon_pixmap, width, height): item = QListWidgetItem(list_option) item.setText(label) item.setTextAlignment(Qt.AlignHCenter) item.setIcon(QIcon(icon_pixmap)) item.setSizeHint(QSize(width, height)) list_option = QListWidget() list_option.setAutoFillBackground(True) list_option.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) list_option.setTextElideMode(Qt.ElideNone) list_option.setMovement(QListView.Static) list_option.setFlow(QListView.TopToBottom) list_option.setProperty("isWrapping", QVariant(False)) list_option.setSpacing(3) list_option.setViewMode(QListView.IconMode) items = [] items.append((QApplication.translate("option", "Connections"), "connections.png")) items.append((QApplication.translate("option", "Accounts"), "accounts.png")) items.append((QApplication.translate("option", "Aliases"), "aliases.png")) items.append((QApplication.translate("option", "Macros"), "macros.png")) items.append((QApplication.translate("option", "Keypad"), "keypad.png")) items.append((QApplication.translate("option", "Triggers"), "triggers.png")) items.append((QApplication.translate("option", "Preferences"), "preferences.png")) max_width = 0 for label, icon_name in items: w = list_option.fontMetrics().boundingRect(label).width() if w > max_width: max_width = w # An empiric method to align element in the center of the QListWidget max_width += 15 total_height = 0 for label, icon_name in items: icon_pixmap = QPixmap(":/images/" + icon_name) height = icon_pixmap.height() + list_option.fontMetrics().height() + 3 total_height += height + 5 addItem(label, icon_pixmap, max_width, height) list_option.setUniformItemSizes(True) list_option.setFixedWidth(max_width + 10) list_option.setMinimumHeight(total_height) return list_option
class ConfigDialog(QDialog): def __init__(self, parent=None): QDialog.__init__(self, parent) self.contents_widget = QListWidget() self.contents_widget.setMovement(QListView.Static) self.contents_widget.setMinimumWidth(120) self.contents_widget.setMaximumWidth(120) self.contents_widget.setSpacing(1) bbox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Apply |QDialogButtonBox.Cancel) self.apply_btn = bbox.button(QDialogButtonBox.Apply) self.connect(bbox, SIGNAL("accepted()"), SLOT("accept()")) self.connect(bbox, SIGNAL("rejected()"), SLOT("reject()")) self.connect(bbox, SIGNAL("clicked(QAbstractButton*)"), self.button_clicked) self.pages_widget = QStackedWidget() self.connect(self.pages_widget, SIGNAL("currentChanged(int)"), self.current_page_changed) self.connect(self.contents_widget, SIGNAL("currentRowChanged(int)"), self.pages_widget.setCurrentIndex) self.contents_widget.setCurrentRow(0) hlayout = QHBoxLayout() hlayout.addWidget(self.contents_widget) hlayout.addWidget(self.pages_widget) hlayout.setStretch(1,1) btnlayout = QHBoxLayout() btnlayout.addStretch(1) btnlayout.addWidget(bbox) vlayout = QVBoxLayout() vlayout.addLayout(hlayout) vlayout.addLayout(btnlayout) self.setLayout(vlayout) self.setWindowTitle("Preferences") self.setWindowIcon(get_icon("configure.png")) def get_current_index(self): """Return current page index""" return self.contents_widget.currentRow() def set_current_index(self, index): """Set current page index""" self.contents_widget.setCurrentRow(index) def accept(self): """Reimplement Qt method""" for index in range(self.pages_widget.count()): configpage = self.pages_widget.widget(index) if not configpage.is_valid(): return configpage.apply_changes() QDialog.accept(self) def button_clicked(self, button): if button is self.apply_btn: # Apply button was clicked configpage = self.pages_widget.currentWidget() if not configpage.is_valid(): return configpage.apply_changes() def current_page_changed(self, index): widget = self.pages_widget.widget(index) self.apply_btn.setVisible(widget.apply_callback is not None) self.apply_btn.setEnabled(widget.is_modified) def add_page(self, widget): self.connect(self, SIGNAL('check_settings()'), widget.check_settings) self.connect(widget, SIGNAL('show_this_page()'), lambda row=self.contents_widget.count(): self.contents_widget.setCurrentRow(row)) self.connect(widget, SIGNAL("apply_button_enabled(bool)"), self.apply_btn.setEnabled) self.pages_widget.addWidget(widget) item = QListWidgetItem(self.contents_widget) item.setIcon(widget.get_icon()) item.setText(widget.get_name()) item.setFlags(Qt.ItemIsSelectable|Qt.ItemIsEnabled) item.setSizeHint(QSize(0, 25)) def check_all_settings(self): """This method is called to check all configuration page settings after configuration dialog has been shown""" self.emit(SIGNAL('check_settings()'))
class ConfigDialog(QDialog): def __init__(self, parent=None): QDialog.__init__(self, parent) self.contents_widget = QListWidget() self.contents_widget.setMovement(QListView.Static) self.contents_widget.setMinimumWidth(120) self.contents_widget.setMaximumWidth(120) self.contents_widget.setSpacing(1) bbox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Apply | QDialogButtonBox.Cancel) self.apply_btn = bbox.button(QDialogButtonBox.Apply) self.connect(bbox, SIGNAL("accepted()"), SLOT("accept()")) self.connect(bbox, SIGNAL("rejected()"), SLOT("reject()")) self.connect(bbox, SIGNAL("clicked(QAbstractButton*)"), self.button_clicked) self.pages_widget = QStackedWidget() self.connect(self.pages_widget, SIGNAL("currentChanged(int)"), self.current_page_changed) self.connect(self.contents_widget, SIGNAL("currentRowChanged(int)"), self.pages_widget.setCurrentIndex) self.contents_widget.setCurrentRow(0) hlayout = QHBoxLayout() hlayout.addWidget(self.contents_widget) hlayout.addWidget(self.pages_widget) hlayout.setStretch(1, 1) btnlayout = QHBoxLayout() btnlayout.addStretch(1) btnlayout.addWidget(bbox) vlayout = QVBoxLayout() vlayout.addLayout(hlayout) vlayout.addLayout(btnlayout) self.setLayout(vlayout) self.setWindowTitle("Preferences") self.setWindowIcon(get_icon("configure.png")) def get_current_index(self): """Return current page index""" return self.contents_widget.currentRow() def set_current_index(self, index): """Set current page index""" self.contents_widget.setCurrentRow(index) def accept(self): """Reimplement Qt method""" for index in range(self.pages_widget.count()): configpage = self.pages_widget.widget(index) if not configpage.is_valid(): return configpage.apply_changes() QDialog.accept(self) def button_clicked(self, button): if button is self.apply_btn: # Apply button was clicked configpage = self.pages_widget.currentWidget() if not configpage.is_valid(): return configpage.apply_changes() def current_page_changed(self, index): widget = self.pages_widget.widget(index) self.apply_btn.setVisible(widget.apply_callback is not None) self.apply_btn.setEnabled(widget.is_modified) def add_page(self, widget): self.connect(self, SIGNAL('check_settings()'), widget.check_settings) self.connect(widget, SIGNAL('show_this_page()'), lambda row=self.contents_widget.count(): self. contents_widget.setCurrentRow(row)) self.connect(widget, SIGNAL("apply_button_enabled(bool)"), self.apply_btn.setEnabled) self.pages_widget.addWidget(widget) item = QListWidgetItem(self.contents_widget) item.setIcon(widget.get_icon()) item.setText(widget.get_name()) item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled) item.setSizeHint(QSize(0, 25)) def check_all_settings(self): """This method is called to check all configuration page settings after configuration dialog has been shown""" self.emit(SIGNAL('check_settings()'))
class ConfigDialog(QDialog): """Spyder configuration ('Preferences') dialog box""" def __init__(self, parent=None): QDialog.__init__(self, parent) # Destroying the C++ object right after closing the dialog box, # otherwise it may be garbage-collected in another QThread # (e.g. the editor's analysis thread in Spyder), thus leading to # a segmentation fault on UNIX or an application crash on Windows self.setAttribute(Qt.WA_DeleteOnClose) self.contents_widget = QListWidget() self.contents_widget.setMovement(QListView.Static) self.contents_widget.setSpacing(1) bbox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Apply |QDialogButtonBox.Cancel) self.apply_btn = bbox.button(QDialogButtonBox.Apply) self.connect(bbox, SIGNAL("accepted()"), SLOT("accept()")) self.connect(bbox, SIGNAL("rejected()"), SLOT("reject()")) self.connect(bbox, SIGNAL("clicked(QAbstractButton*)"), self.button_clicked) self.pages_widget = QStackedWidget() self.connect(self.pages_widget, SIGNAL("currentChanged(int)"), self.current_page_changed) self.connect(self.contents_widget, SIGNAL("currentRowChanged(int)"), self.pages_widget.setCurrentIndex) self.contents_widget.setCurrentRow(0) hsplitter = QSplitter() hsplitter.addWidget(self.contents_widget) hsplitter.addWidget(self.pages_widget) btnlayout = QHBoxLayout() btnlayout.addStretch(1) btnlayout.addWidget(bbox) vlayout = QVBoxLayout() vlayout.addWidget(hsplitter) vlayout.addLayout(btnlayout) self.setLayout(vlayout) self.setWindowTitle(_("Preferences")) self.setWindowIcon(get_icon("configure.png")) def get_current_index(self): """Return current page index""" return self.contents_widget.currentRow() def set_current_index(self, index): """Set current page index""" self.contents_widget.setCurrentRow(index) def get_page(self, index=None): """Return page widget""" if index is None: widget = self.pages_widget.currentWidget() else: widget = self.pages_widget.widget(index) return widget.widget() def accept(self): """Reimplement Qt method""" for index in range(self.pages_widget.count()): configpage = self.get_page(index) if not configpage.is_valid(): return configpage.apply_changes() QDialog.accept(self) def button_clicked(self, button): if button is self.apply_btn: # Apply button was clicked configpage = self.get_page() if not configpage.is_valid(): return configpage.apply_changes() def current_page_changed(self, index): widget = self.get_page(index) self.apply_btn.setVisible(widget.apply_callback is not None) self.apply_btn.setEnabled(widget.is_modified) def add_page(self, widget): self.connect(self, SIGNAL('check_settings()'), widget.check_settings) self.connect(widget, SIGNAL('show_this_page()'), lambda row=self.contents_widget.count(): self.contents_widget.setCurrentRow(row)) self.connect(widget, SIGNAL("apply_button_enabled(bool)"), self.apply_btn.setEnabled) scrollarea = QScrollArea(self) scrollarea.setWidgetResizable(True) scrollarea.setWidget(widget) self.pages_widget.addWidget(scrollarea) item = QListWidgetItem(self.contents_widget) item.setIcon(widget.get_icon()) item.setText(widget.get_name()) item.setFlags(Qt.ItemIsSelectable|Qt.ItemIsEnabled) item.setSizeHint(QSize(0, 25)) def check_all_settings(self): """This method is called to check all configuration page settings after configuration dialog has been shown""" self.emit(SIGNAL('check_settings()')) def resizeEvent(self, event): """ Reimplement Qt method to be able to save the widget's size from the main application """ QDialog.resizeEvent(self, event) self.emit(SIGNAL("size_change(QSize)"), self.size())
class LayerImportBrowser(QDialog): class VectorPage(QWidget): def __init__(self, parent=None, filters="", encodings=[]): QWidget.__init__(self, parent) self.filters = filters self.layerLineEdit = QLineEdit() self.browseToolButton = QToolButton() self.browseToolButton.setAutoRaise(True) self.browseToolButton.setIcon(QIcon(":document-open")) layerLabel = QLabel("&Dataset:") layerLabel.setBuddy(self.layerLineEdit) self.encodingComboBox = QComboBox() self.encodingComboBox.addItems(encodings) encodingLabel = QLabel("&Encoding:") encodingLabel.setBuddy(self.encodingComboBox) hbox = QHBoxLayout() hbox.addWidget(layerLabel) hbox.addWidget(self.layerLineEdit) hbox.addWidget(self.browseToolButton) vbox = QVBoxLayout() vbox.addLayout(hbox) hbox = QHBoxLayout() hbox.addWidget(encodingLabel) hbox.addWidget(self.encodingComboBox) vbox.addLayout(hbox) self.setLayout(vbox) self.encodingComboBox.setCurrentIndex(self.encodingComboBox.findText("System")) self.connect(self.browseToolButton, SIGNAL("clicked()"), self.browseToFile) self.connect(self.encodingComboBox, SIGNAL("currentIndexChanged(QString)"), self.changeEncoding) def browseToFile(self): dialog = QFileDialog(self, "manageR - Open Vector File", unicode(robjects.r.getwd()[0]), self.filters) if not dialog.exec_() == QDialog.Accepted: return files = dialog.selectedFiles() file = files.first().trimmed() self.layerLineEdit.setText(file) self.emit(SIGNAL("filePathChanged(QString)"), file) def changeEncoding(self, text): self.emit(SIGNAL("encodingChanged(QString)"), text) class RasterPage(QWidget): def __init__(self, parent=None, filters=""): QWidget.__init__(self, parent) self.parent = parent self.filters = filters self.layerLineEdit = QLineEdit() self.browseToolButton = QToolButton() self.browseToolButton.setAutoRaise(True) self.browseToolButton.setIcon(QIcon(":document-open")) layerLabel = QLabel("&Dataset:") layerLabel.setBuddy(self.layerLineEdit) hbox = QHBoxLayout() hbox.addWidget(layerLabel) hbox.addWidget(self.layerLineEdit) hbox.addWidget(self.browseToolButton) vbox = QVBoxLayout() vbox.addLayout(hbox) self.setLayout(vbox) self.connect(self.browseToolButton, SIGNAL("clicked()"), self.browseToFile) def browseToFile(self): dialog = QFileDialog(self, "manageR - Open Raster File", unicode(robjects.r.getwd()[0]), self.filters) if not dialog.exec_() == QDialog.Accepted: return files = dialog.selectedFiles() file = files.first().trimmed() self.layerLineEdit.setText(file) self.emit(SIGNAL("filePathChanged(QString)"), file) def __init__(self, parent=None, vectors="", rasters="", encodings=[]): QDialog.__init__(self, parent) self.contentsWidget = QListWidget() self.setWindowIcon(QIcon(":icon")) self.contentsWidget.setViewMode(QListView.IconMode) self.contentsWidget.setIconSize(QSize(76, 66)) self.contentsWidget.setMovement(QListView.Static) self.contentsWidget.setMaximumWidth(106) self.contentsWidget.setMinimumWidth(106) self.contentsWidget.setMinimumHeight(220) self.contentsWidget.setSpacing(12) self.__filePath = "" self.__encoding = "System" self.__type = 0 self.pagesWidget = QStackedWidget() vectorPage = self.VectorPage(self, vectors, encodings) self.pagesWidget.addWidget(vectorPage) rasterPage = self.RasterPage(self, rasters) self.pagesWidget.addWidget(rasterPage) buttons = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel, Qt.Horizontal, self) self.connect(buttons, SIGNAL("accepted()"), self.accept) self.connect(buttons, SIGNAL("rejected()"), self.reject) self.connect(vectorPage, SIGNAL("filePathChanged(QString)"), self.setFilePath) self.connect(vectorPage, SIGNAL("encodingChanged(QString)"), self.setEncoding) self.connect(rasterPage, SIGNAL("filePathChanged(QString)"), self.setFilePath) self.createIcons() self.contentsWidget.setCurrentRow(0) horizontalLayout = QHBoxLayout() horizontalLayout.addWidget(self.contentsWidget) horizontalLayout.addWidget(self.pagesWidget, 1) mainLayout = QVBoxLayout() mainLayout.addLayout(horizontalLayout) mainLayout.addStretch(1) mainLayout.addSpacing(12) mainLayout.addWidget(buttons) self.setLayout(mainLayout) self.setWindowTitle("manageR - Import Layer") def createIcons(self): vectorButton = QListWidgetItem(self.contentsWidget) vectorButton.setIcon(QIcon(":custom-vector.svg")) vectorButton.setText("Vector Layer") vectorButton.setTextAlignment(Qt.AlignHCenter) vectorButton.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled) rasterButton = QListWidgetItem(self.contentsWidget) rasterButton.setIcon(QIcon(":custom-raster.svg")) rasterButton.setText("Raster Layer") rasterButton.setTextAlignment(Qt.AlignHCenter) rasterButton.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled) self.connect(self.contentsWidget, SIGNAL("currentItemChanged(QListWidgetItem*, QListWidgetItem*)"), self.changePage) def changePage(self, current, previous): if not current: current = previous self.pagesWidget.setCurrentIndex(self.contentsWidget.row(current)) def filePath(self): return self.__filePath def setFilePath(self, filePath): self.__filePath = filePath def encoding(self): return self.__encoding def setEncoding(self, encoding): self.__encoding = encoding def layerType(self): return self.__type def setLayerType(self, type): self.__type = type def accept(self): self.__type = self.pagesWidget.currentIndex() QDialog.accept(self)