def change_text_colour(self, value): ColorD = QColorDialog(self) if value is "Text": ColorD.colorSelected.connect(self.textEdit.setTextColor) elif value is "Background": ColorD.colorSelected.connect(self.textEdit.setTextBackgroundColor) ColorD.open()
class Demo(QWidget): def __init__(self): super().__init__() self.resize(300, 300) self.setWindowTitle('QFontDialog') self.label = QLabel('塘沽五中', self) self.label.move(100, 10) self.cd = QColorDialog(QColor(250, 0, 0), self) # 创建颜色对话框--但不显示 # 参数1 默认颜色 self.pl = QPalette() # 创建调色板 btn = QPushButton('按钮', self) btn.move(100, 250) btn.clicked.connect(self.AA) def color(self): self.pl.setColor(QPalette.Background, self.cd.selectedColor()) # 给调色板设置颜色 # QPalette.Background 表示设置背景色 # QPalette.WindowText 表示设置文本颜色 # selectedColor() 返回最终选择的颜色 self.setPalette(self.pl) # 给控件设置颜色 pass def AA(self): self.cd.open(self.color) # 点击确定按钮时会自动执行槽函数
def open_color_dialog(self, attr_name, mouse_event): d = QColorDialog(self) d.setCurrentColor(getattr(self, attr_name)) f = partial(self.set_color, attr_name) d.colorSelected.connect( f) # d.open(f) doesn't pass color for some reason d.open()
def edit(self, row, column): self._status = (row, self.listbox.item(row, column).background()) win = QColorDialog(self) win.setCurrentColor(self.listbox.item(row, column).background().color()) win.currentColorChanged.connect( self.intermediateColor) win.rejected.connect(self.setColor) win.open()
def tableWidgetCellDoubleClicked(self, row, column): if column==1: self.selected_row = row selected_item = self.tableWidget.item(self.selected_row, 1) color_dialog = QColorDialog(selected_item.data(Qt.BackgroundRole), self) color_dialog.currentColorChanged.connect(self.currentColorChanged) color_dialog.open()
def tableWidgetCellDoubleClicked(self, row, column): if column == 1: self.selected_row = row selected_item = self.tableWidget.item(self.selected_row, 1) color_dialog = QColorDialog(selected_item.data(Qt.BackgroundRole), self) color_dialog.currentColorChanged.connect(self.currentColorChanged) color_dialog.open()
class ColorInput(QPushButton, Input): def __init__(self, dtype): super().__init__(dtype=dtype) self._color_dialog = QColorDialog() self._palette = QPalette() self._init_widget() def set_value(self, value: list): color = QColor.fromRgbF(*value) self._set_display_color(color) self._color_dialog.setCurrentColor(color) def get_gl_value(self) -> np.ndarray: if self._dtype == DataType.Vec3_RGB: return np.array((self._color.getRgbF()[:3]), dtype=np.float32) else: raise TypeError( "Internal type {} not supported for ColorInput!".format( self._dtype)) def get_value(self) -> typing.Any: return self._color def _init_widget(self): self._color_dialog.currentColorChanged.connect( self._current_color_changed) self._color_dialog.setOption(QColorDialog.ShowAlphaChannel, True) self.clicked.connect(self._open_color_dialog) @pyqtSlot(QColor) def _current_color_changed(self, color: QColor): self._set_display_color(color) self.input_changed.emit() def _open_color_dialog(self): self._color_dialog.open() def _set_display_color(self, color: QColor): self._palette.setColor(QPalette.Button, color) self.setAutoFillBackground(True) self.setFlat(True) self.setPalette(self._palette) self.update() self._color = color
def colorSelection(self): dialog = QColorDialog(self) dialog.colorSelected.connect(self.colorSelectionSlot) dialog.open()
class LineSettingsQWidgetAction(QtGui.QWidgetAction): """Class customizes the QWidgetAction object for the line settings menu This class sets up a block in the line settings menu corresponding to one line object. It provides an interface between the clickable elements in the submenu block and the TimePlotDataItem it is connected to set settings like line width, line color, and alpha value. Parameter --------- line_settings_menu : QMenu QMenu which this class will be ammended to time_plot_gui : TimePlotGui object Gui Widget that displayes the plot lines id_nr : int reference number to access TimePlotDataItem from data_table in the TimePlotGui object Attribute --------- name : str Object name. This name is also used as Qlabel in the line_settings_menu id_nr : int id number of the corresponding TimePlotDataItem object tpg : TimePlotGui Gui Widget that displayes the plot lines data_item : TimePlotDataItem represents the data line inside PlotItem object in the TimePlotGui line_settings : dict contains layout settings like line width, line color, and alpha value of the plot line object line_settings_menu : QMenu submenu from the context menu hosting where the this QWidgetAction is connected to """ def __init__(self, line_settings_menu, time_plot_gui, id_nr): super(LineSettingsQWidgetAction, self).__init__(line_settings_menu) self.id_nr = id_nr self.tpg = time_plot_gui self.data_item = self.tpg.data_table[id_nr] self.line_settings = self.tpg.settings['line_settings'][str(id_nr)] self.line_settings_menu = line_settings_menu self._compose_name() self.mainlabel = QLabel(self.name) self.mainlabel.setAlignment(QtCore.Qt.AlignCenter) # widthintermediate = QtGui.QWidgetAction(self.line_settings_menu) self.width_widget = QtGui.QWidget() self.widthlabel = QLabel("Line Width:") self.spinbox = QSpinBox() self.spinbox.setValue(self.line_settings['line_width']) self.spinbox.setRange(1, 15) self.spinbox.setSingleStep(1) self.alpha_label = QLabel("Alpha") self.alpha_slider = QtGui.QSlider(self.line_settings_menu) self.alpha_slider.setOrientation(QtCore.Qt.Horizontal) self.alpha_slider.setMaximum(255) self.alpha_slider.setValue(self.line_settings['line_alpha']*255) self.color_button = QPushButton("Change line color") self.width_layout = QGridLayout() self.width_layout.addWidget(self.mainlabel, 0, 0, 1, 2) self.width_layout.addWidget(self.alpha_label, 1, 0, 1, 1) self.width_layout.addWidget(self.alpha_slider, 1, 1, 1, 1) self.width_layout.addWidget(self.widthlabel, 2, 0, 1, 1) self.width_layout.addWidget(self.spinbox, 2, 1, 1, 1) self.width_layout.addWidget(self.color_button, 3, 0, 1, 2) self.width_widget.setLayout(self.width_layout) self.setDefaultWidget(self.width_widget) # connect signals & slots self.spinbox.valueChanged.connect(self.data_item.set_width) self.alpha_slider.valueChanged.connect(self.data_item.set_alpha) self.color_button.clicked.connect(self._open_color_dialog) def _compose_name(self): """create the name object attribute based in naming convetion defined in here """ self.name = 'Line {}'.format(str(self.id_nr)) def get_name(self): """returns object name Return ------ str object name. This name is also used as Qlabel in line_settings_menu """ return self.name def get_alpha(self): """get alpha value from QSlider""" return self.alpha_slider.value() def set_alpha(self, alpha): """sets alpha value in QSlider Parameter --------- alpha : int alpha value needs to be provided as int in range 0 to 255 """ self.alpha_slider.setValue(alpha) def get_width(self): """get width value from spinbox QObject""" return self.spinbox.value() def set_width(self, width): """set width value in spinbox QObject Parameter --------- width : int width can take int values between 0 and 15 """ self.spinbox.setValue(width) def get_line_settings(self): """returns line settings Since current implementation does not store the color value in this object rather in the TimePlotDataItem object, the color value is retrieved by calling the corresponding TimePlotDataItem getter function Return ------ dict dictionary contains alpha, width and color value from line settings object """ dct = { 'line_alpha': self.get_alpha(), 'line_width': self.get_width(), 'line_color': self.data_item.get_color(), } return dct def update_line_menu(self): """updates the values in line settings submenu with values from line_settings dictionary. Note: alpha value needs to be converted to RGB value for display in QSlider therefore the factor of 255. """ self.set_alpha( 255*self.line_settings['line_alpha'] ) self.set_width( self.line_settings['line_width'] ) def update_line_settings(self): """update the values in line settings dictionary with values from Qbject in line settings submenu Note: alpha value needs to be converted to value in [0,1] to be applied to Plot Item. Therefore the conversion factor of 1./255. """ line_settings = self.get_line_settings() line_settings['line_alpha'] *= 1./255 self.line_settings.update(line_settings) def _open_color_dialog(self): """opens a QColorDialog window to select color for PlotDataItem This function generates and open QColorDialog. Button signals are connected to functions which directly change color value of PlotDataItem """ self.restorable_color = self.data_item.get_color(rgb=False) self.color_dialog = QColorDialog() self.color_dialog.currentColorChanged.connect(self._set_color_from_dialog) self.color_dialog.rejected.connect(self._cancel_color_dialog) self.color_dialog.open() def _set_color_from_dialog(self): """updates QColor in PlotDataITem. Part of QColorDialog mechanism """ self.data_item.update_color_from_dialog(self.color_dialog) def _cancel_color_dialog(self): """restores old QColor in PlotDataITem. Part of QColorDialog mechanism """ self.data_item.update_color_from_dialog(self.restorable_color)
class Example(QWidget): def __init__(self): super(Example, self).__init__() self.initUI() #To determine whether the square is clicked self.mouseClicked = False def initUI(self): #initializing the color dialog self.qcd = QColorDialog(self) self.qcd.colorSelected.connect(self.changeSquareColor) #initializing the square's dimension self.x = 20 self.y = 20 self.d = 50 #start the timer self.qt = QTimer() self.qt.timeout.connect(self.update) self.qt.start(1) #initializing the color self.qc = QColor(255, 0, 0) #initializing the widget's dimension self.setGeometry(300, 300, 600, 400) self.setWindowTitle('Square') self.show() def paintEvent(self, e): qp = QPainter() qp.begin(self) self.drawRectangles(qp) qp.end() def drawRectangles(self, qp): qp.setPen(QColor(0, 0, 0, 255)) #draw the widget qp.setBrush(QColor(255, 255, 255)) qp.drawRect(0, 0, self.width(), self.height()) #draw the square qp.setBrush(self.qc) qp.drawRect(self.x, self.y, self.d, self.d) def mouseDoubleClickEvent(self, e): if self.x <= e.x() and e.x() <= self.x + self.d and ( self.y <= e.y() and e.y() <= self.y + self.d): self.qcd.open() def mousePressEvent(self, e): #if we click in the square, determine the offset if self.x <= e.x() and e.x() <= self.x + self.d and ( self.y <= e.y() and e.y() <= self.y + self.d): self.deltax = e.x() - self.x self.deltay = e.y() - self.y self.mouseClicked = True def mouseMoveEvent(self, e): #drag the square when it is moved if self.mouseClicked: self.x = e.x() - self.deltax self.y = e.y() - self.deltay def mouseReleaseEvent(self, e): self.mouseClicked = False def changeSquareColor(self, color): self.qc = self.qcd.currentColor()
class WritingModeRLDocker(DockWidget): def __init__(self): super().__init__() self.setWindowTitle("Create Vertical Writing") mainWidget = QWidget(self) self.setWidget(mainWidget) convertButtons = QWidget(mainWidget) buttonCopyToClipboard = QPushButton("Copy to Clipboard", convertButtons) buttonCopyToClipboard.clicked.connect(self.copyToClipboard) buttonOutputSVG = QPushButton("Output SVG", convertButtons) buttonOutputSVG.clicked.connect(self.outputSVG) buttonBoth = QPushButton("Both", convertButtons) buttonBoth.clicked.connect(self.both) convertTextLabels = QWidget(mainWidget) inputTextLabel = QLabel("Input Text", convertTextLabels) outputTextLabel = QLabel("Output SVG", convertTextLabels) convertTexts = QWidget(mainWidget) self.inputText = QPlainTextEdit(convertTexts) self.outputText = QPlainTextEdit( "text converted by \"Output SVG\" from left input textarea", convertTexts) self.outputText.setReadOnly(True) fontNames = QWidget(mainWidget) fontSelectorLabel = QLabel("font family", fontNames) self.fontSelector = QFontComboBox(fontNames) fontOptionDatas = QWidget(mainWidget) fontColorParts = QWidget(mainWidget) fontColorParts.setLayout(QHBoxLayout()) fontColorLabel = QLabel("font color", fontColorParts) self.fontColorButton = QPushButton("■", fontColorParts) self.fontColorButton.clicked.connect(self.openTextColorDialog) self.fontColorDialog = QColorDialog(mainWidget) self.fontColorDialog.colorSelected.connect(self.selectedColor) self.fontColorCombo = QComboBox(fontOptionDatas) self.fontColorCombo.setEditable(True) self.fontColorCombo.addItems([ "#000000", "#7f7f7f", "#ffffff", "#ff0000", "#ffff00", "#00ff00", "#00ffff", "#0000ff", "#ff00ff" ]) self.fontColorCombo.editTextChanged.connect(self.colorChanged) self.colorChanged(self.fontColorCombo.currentText()) fontColorParts.layout().addWidget(fontColorLabel) fontColorParts.layout().addWidget(self.fontColorButton) fontSizeLabel = QLabel("font size(pt)", fontOptionDatas) self.fontSizeCombo = QComboBox(fontOptionDatas) self.fontSizeCombo.setEditable(True) self.fontSizeCombo.addItems([ "6", "7", "8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28", "36", "48", "72" ]) self.fontSizeCombo.setEditText("8") fontNames.setLayout(QHBoxLayout()) fontNames.layout().addWidget(fontSelectorLabel) fontNames.layout().addWidget(self.fontSelector) fontOptionDatas.setLayout(QHBoxLayout()) fontOptionDatas.layout().addWidget(fontColorParts) fontOptionDatas.layout().addWidget(self.fontColorCombo) fontOptionDatas.layout().addWidget(fontSizeLabel) fontOptionDatas.layout().addWidget(self.fontSizeCombo) convertTextLabels.setLayout(QHBoxLayout()) convertTextLabels.layout().addWidget(inputTextLabel) convertTextLabels.layout().addWidget(outputTextLabel) convertTexts.setLayout(QHBoxLayout()) convertTexts.layout().addWidget(self.inputText) convertTexts.layout().addWidget(self.outputText) convertButtons.setLayout(QHBoxLayout()) convertButtons.layout().addWidget(buttonOutputSVG) convertButtons.layout().addWidget(buttonCopyToClipboard) convertButtons.layout().addWidget(buttonBoth) mainWidget.setLayout(QVBoxLayout()) mainWidget.layout().addWidget(fontNames) mainWidget.layout().addWidget(fontOptionDatas) mainWidget.layout().addWidget(convertButtons) mainWidget.layout().addWidget(convertTextLabels) mainWidget.layout().addWidget(convertTexts) def canvasChanged(self, canvas): pass def openTextColorDialog(self): self.fontColorDialog.open() def selectedColor(self, color): self.fontColorCombo.setEditText(color.name()) def colorChanged(self, text): self.fontColorButton.setStyleSheet("color:" + text + ";") def both(self): self.outputSVG() self.copyToClipboard() def copyToClipboard(self): ccText = self.outputText.toPlainText() QApplication.clipboard().setText(ccText) def outputSVG(self): putText = self.__transSVG(self.inputText.toPlainText()) self.outputText.setPlainText(putText) def __transSVG(self, originText): beforeText = copy.deepcopy(originText) width = len(beforeText.split("\n")) editingText = "" outChars = list(beforeText) y = 0 x = (width - 1) * 1.2 for part in outChars: if part == "\n": y = 0 x -= 1.2 else: editedTextX = DefineLocate.getEditedX( part, self.fontSizeCombo.currentText()) editedTextY = DefineLocate.getEditedY( part, self.fontSizeCombo.currentText()) resultX = x + editedTextX resultY = y + editedTextY editingText = editingText + " <tspan x=\"" + \ str(resultX) + "em\" y=\"" + str(resultY) + "em\">" + \ html.escape(part) + "</tspan>\n" y += 1.2 outText = "<text style=\"font-family:" + \ self.fontSelector.currentText() + \ ";font-size:"+self.fontSizeCombo.currentText() + \ ";fill:" + self.fontColorCombo.currentText() + "\">\n" + \ editingText + "</text>" return outText
class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setupUi = Ui_PlotterMainWindow.setupUi self.retranslateUi = lambda e: Ui_PlotterMainWindow.retranslateUi( self, e) self.setupUi(self, self) # uic.loadUi('ui/MainView.ui', self) self.fontDB = QFontDatabase() self.fontDB.addApplicationFont("OpenSansEmoji.ttf") self.chart = ChartWidget(self.chart_label, self.home_button, self.zoom_button, self.update_chart) self.legend = QLabel(self.legend_widget) self.screenshot_button.clicked.connect(self.handle_screenshot_button) self.add_dataset_button = self.add_dataset_button_3 self.line_combo_box = self.line_combo_box_3 self.add_dataset_button.clicked.connect(self.open_data_file) self.edit_datasets_button.clicked.connect(self.open_edit_dataset) self.add_file_action.triggered.connect(self.open_data_file) self.add_page_action.triggered.connect(self.add_page) self.edit_page_name_action.triggered.connect(self.edit_page_name) self.delete_current_page_action.triggered.connect(self.delete_page) # self.add_page_button.setPosition( self.datasets = [] self.pages = [] self.page = {} self.add_page() self.add_page_button.clicked.connect(self.add_page) self.add_line_button.clicked.connect(self.handle_add_line_button) self.add_line_button_2.clicked.connect(self.handle_add_line_button) self.edit_lines_button.clicked.connect(self.toggle_edit_lines) self.line_combo_box.currentIndexChanged.connect( self.set_line_to_edit_from_combo_box) self.dataset_combo_box.currentIndexChanged.connect( self.update_data_set) self.name_input.textChanged.connect(self.update_edited_line_data) self.sort_by_x_checkbox.clicked.connect(self.update_edited_line_data) self.x_key_combo_box.currentIndexChanged.connect( self.update_edited_line_data) self.y_key_combo_box.currentIndexChanged.connect( self.update_edited_line_data) self.display_line_checkbox.clicked.connect( self.update_edited_line_pixmap) self.line_width_slider.valueChanged.connect( self.update_edited_line_pixmap) self.line_opacity_slider.valueChanged.connect( self.update_edited_line_pixmap) self.display_point_checkbox.clicked.connect( self.update_edited_line_pixmap) self.point_size_slider.valueChanged.connect( self.update_edited_line_pixmap) self.point_opacity_slider.valueChanged.connect( self.update_edited_line_pixmap) self.point_shape_combobox.currentIndexChanged.connect( self.update_edited_line_pixmap) for e in list(PointShape): self.point_shape_combobox.addItem(e.name.title()) self.point_shape_combobox.setCurrentIndex(0) self.line_colour_label.mousePressEvent = lambda _: self.open_colour_dialog( self.update_line_colour) self.line_pattern_label.mousePressEvent = self.open_style_dialog self.point_colour_label.mousePressEvent = lambda _: self.open_colour_dialog( self.update_point_colour) self.line_settings_widget.setVisible(False) self.line_to_edit = None self.remove_line_button.clicked.connect(self.handle_remove_line_button) self.line_count_label.setText('{} Line{}'.format( len(self.page['lines']), "s" if len(self.page['lines']) != 1 else "")) self.dataset_count_label.setText('{} Dataset{}'.format( len(self.datasets), "s" if len(self.datasets) != 1 else "")) self.show() self.legend.setFixedSize(self.legend_widget.size()) self.chart.fit_data() def open_edit_dataset(self, event): self.dataset_edit_dialog = DataSourceListDialog( self.datasets, self.add_data_source, self.delete_data_source) def edit_page_name(self, event): self.text_entry_dialog = TextEntryDialog('Enter Page Name:', text=self.page['name'], window_title="Edit Page Name") self.text_entry_dialog.textSelected.connect(self.update_page_name) self.text_entry_dialog.open() def update_page_name(self, new_name): self.page['name'] = new_name self.page['button'].setText(new_name) def delete_page(self, event, page=None): if page is None: page = self.page self.pages.remove(page) try: self.tab_widget.layout().removeWidget(page['button']) except: pass page['button'].hide() del page['button'] if self.pages: self.select_page(self.pages[0]['id']) else: self.add_page() def add_page(self): self.line_to_edit = None page_button = QPushButton() page_button.setCheckable(True) page_button.setStyleSheet( self.add_page_button.styleSheet() + "\nQPushButton:checked{\nbackground: #ddd;\n}") self.tab_widget.layout().addWidget(page_button) page_button.setChecked(False) new_id = 'page_1' while new_id in [p['id'] for p in self.pages]: new_id = 'page_' + str(int(new_id[5:]) + 1) page = { 'id': new_id, 'name': new_id.replace("_", " ").title(), 'lines': [], 'button': page_button } page_button.clicked.connect(lambda: self.select_page(new_id)) page_button.setText(page['name']) page_button.setMinimumSize(QSize(60, 30)) self.pages.append(page) self.select_page(new_id) def select_page(self, page_id): if self.page: self.page['button'].setChecked(False) page_ids = [e['id'] for e in self.pages] if page_id in page_ids: self.line_to_edit = None self.line_settings_widget.setVisible(False) self.legend.setVisible(True) page = self.pages[page_ids.index(page_id)] if self.page is page: return self.page = page self.page['button'].setChecked(True) self.line_combo_box.clear() [self.line_combo_box.addItem(l.name) for l in self.page['lines']] if not self.page['lines'] and self.datasets: self.handle_add_line_button() else: self.line_combo_box.setCurrentIndex(0) self.update_chart() def toggle_edit_lines(self, event): if self.line_to_edit is None and self.datasets: if self.page['lines']: self.line_combo_box.setCurrentIndex(0) else: self.handle_add_line_button() self.line_settings_widget.setVisible(True) self.legend.setVisible(False) else: self.line_to_edit = None self.line_settings_widget.setVisible(False) self.legend.setVisible(True) def handle_screenshot_button(self, event): pixmap = self.chart_label.pixmap() if pixmap: filename = QFileDialog.getSaveFileName(None, 'Save Screenshot', '', '*.png')[0] file = QFile(filename) file.open(QIODevice.WriteOnly) pixmap.save(file, "PNG") def open_style_dialog(self, event): self.style_dialog = StyleSelectDialog() self.style_dialog.styleSelected.connect(self.update_line_style) self.style_dialog.open() def open_colour_dialog(self, target): self.colour_dialog = QColorDialog() self.colour_dialog.colorSelected.connect(target) self.colour_dialog.open() def set_line_to_edit_from_combo_box(self): if self.page['lines']: self.line_to_edit = self.page['lines'][ self.line_combo_box.currentIndex()] self.name_input.setText(self.line_to_edit.name) self.dataset_combo_box.setCurrentIndex( self.datasets.index(self.line_to_edit.dataset)) self.x_key_combo_box.setCurrentIndex(self.line_to_edit.x_key) self.y_key_combo_box.setCurrentIndex(self.line_to_edit.y_key) self.sort_by_x_checkbox.setChecked(self.line_to_edit.sort_by_x) self.display_line_checkbox.setChecked( self.line_to_edit.display_line) self.dataset_combo_box.setCurrentIndex( self.datasets.index(self.line_to_edit.dataset)) self.line_width_slider.setValue( int(self.line_to_edit.line_width * 10)) self.draw_line_label_colour(self.line_to_edit.line_colour) self.draw_line_label_pattern(self.line_to_edit.line_style) self.draw_point_label_colour(self.line_to_edit.point_colour) self.point_shape_combobox.setCurrentIndex( self.line_to_edit.point_shape.value) if self.datasets: self.line_settings_widget.setVisible(True) self.legend.setVisible(False) else: self.line_settings_widget.setVisible(False) self.legend.setVisible(True) def open_data_file(self): self.add_file_window = AddFileWindow(self.add_data_source, lambda: 0) def add_data_source(self, dataset): self.datasets.append(dataset) self.dataset_combo_box.addItem(dataset.filepath.split("/")[-1]) self.dataset_count_label.setText('{} Dataset{}'.format( len(self.datasets), "s" if len(self.datasets) != 1 else "")) self.update_data_set() if not self.page['lines']: self.handle_add_line_button() def delete_data_source(self, data_source): for page in self.pages: for line in page['lines']: if line['dataset'] is data_source: if line is self.line_to_edit: self.handle_remove_line_button(None) else: page['lines'].remove(line) self.datasets.remove(data_source) self.dataset_combo_box.clear() [self.dataset_combo_box.addItem(d.filepath) for d in self.datasets] self.dataset_count_label.setText('{} Dataset{}'.format( len(self.datasets), "s" if len(self.datasets) != 1 else "")) self.update_data_set() self.update_chart(False) def handle_add_line_button(self): if self.datasets: default_name = 'Line 1' while default_name in [e.name for e in self.page['lines']]: default_name = 'Line ' + str( int(default_name.split(' ')[-1]) + 1) line = Line(default_name, self.datasets[0]) self.page['lines'].append(line) self.line_combo_box.addItem(line.name) self.line_combo_box.setCurrentIndex(len(self.page['lines']) - 1) self.set_line_to_edit_from_combo_box() self.line_count_label.setText('{} Line{}'.format( len(self.page['lines']), "s" if len(self.page['lines']) != 1 else "")) self.update_data_set() self.chart.paint_lines(self.page['lines'], update_axis=False) self.chart.fit_data() def handle_remove_line_button(self, event): self.page['lines'].remove(self.line_to_edit) self.line_to_edit = None self.line_combo_box.clear() if len(self.page['lines']) < 1: self.line_settings_widget.setVisible(False) self.legend.setVisible(True) else: [self.line_combo_box.addItem(e.name) for e in self.page['lines']] self.line_combo_box.setCurrentIndex(0) self.line_count_label.setText('{} Line{}'.format( len(self.page['lines']), "s" if len(self.page['lines']) != 1 else "")) self.update_chart(False) def draw_line_label_colour(self, colour): pixmap = QPixmap(self.line_colour_label.size()) pixmap.fill(colour) self.line_colour_label.setPixmap(pixmap) self.line_to_edit.set_pixmap_to_update() def draw_point_label_colour(self, colour): pixmap = QPixmap(self.point_colour_label.size()) pixmap.fill(colour) self.point_colour_label.setPixmap(pixmap) self.line_to_edit.set_pixmap_to_update() def draw_line_label_pattern(self, style, pattern=[]): pixmap = QPixmap(self.line_pattern_label.size()) pixmap.fill(QColor(0, 0, 0, 0)) qp = QPainter() qp.begin(pixmap) pen = QPen() pen.setColor(QColor(50, 50, 50)) pen.setWidth(4) pen.setStyle(style) if pattern and style is Qt.CustomDashLine: pen.setDashPattern(pattern) qp.setPen(pen) qp.drawLine(0, pixmap.height() / 2, pixmap.width(), pixmap.height() / 2) qp.end() self.line_pattern_label.setPixmap(pixmap) self.line_to_edit.set_pixmap_to_update() def update_edited_line_data(self): if self.line_to_edit: self.line_to_edit.name = self.name_input.text() self.line_combo_box.setItemText(self.line_combo_box.currentIndex(), self.line_to_edit.name) self.line_to_edit.sort_by_x = self.sort_by_x_checkbox.isChecked() self.line_to_edit.x_key = self.x_key_combo_box.currentIndex() self.line_to_edit.y_key = self.y_key_combo_box.currentIndex() self.line_to_edit.set_data_to_update() self.update_edited_line_pixmap() self.update_chart(False) def update_edited_line_pixmap(self): if self.line_to_edit: self.line_to_edit.name = self.name_input.text() self.line_to_edit.display_line = self.display_line_checkbox.isChecked( ) self.line_to_edit.line_width = self.line_width_slider.value() / 10 self.line_to_edit.line_colour.setAlpha( self.line_opacity_slider.value()) self.line_to_edit.display_points = self.display_point_checkbox.isChecked( ) self.line_to_edit.point_size = self.point_size_slider.value() self.line_to_edit.point_colour.setAlpha( self.point_opacity_slider.value()) self.line_to_edit.point_shape = PointShape.from_val( self.point_shape_combobox.currentIndex()) self.line_to_edit.set_pixmap_to_update() self.update_chart(False) def update_line_colour(self, colour): if self.line_to_edit: self.line_to_edit.line_colour = colour self.line_to_edit.line_colour.setAlpha( self.line_opacity_slider.value()) self.draw_line_label_colour(colour) self.update_chart(False) def update_point_colour(self, colour): if self.line_to_edit: self.line_to_edit.point_colour = colour self.line_to_edit.point_colour.setAlpha( self.point_opacity_slider.value()) self.line_to_edit.set_all_to_update() self.draw_point_label_colour(colour) self.update_chart(False) def update_line_style(self, result): if self.line_to_edit: self.line_to_edit.line_style = result[0] self.line_to_edit.line_pattern = result[1] self.line_to_edit.set_all_to_update() self.draw_line_label_pattern(*result) self.update_chart(False) def update_data_set(self): if self.line_to_edit is not None: dataset = self.datasets[self.dataset_combo_box.currentIndex()] # dataset = self.line_to_edit.dataset if dataset.header_row: keys = dataset.data[0] else: keys = map(str, range(len(dataset.data[0]))) self.x_key_combo_box.clear() self.y_key_combo_box.clear() self.x_key_combo_box.addItem('Row Number') self.y_key_combo_box.addItem('Row Number') for key in keys: self.x_key_combo_box.addItem(key) self.y_key_combo_box.addItem(key) if self.line_to_edit is not None: self.line_to_edit.dataset = dataset self.line_to_edit.x_key = 0 self.line_to_edit.y_key = 1 self.x_key_combo_box.setCurrentIndex(0) self.y_key_combo_box.setCurrentIndex(1) self.line_to_edit.dataset = dataset self.line_to_edit.x_key = self.x_key_combo_box.currentIndex() self.line_to_edit.y_key = self.y_key_combo_box.currentIndex() self.line_to_edit.set_all_to_update() self.update_chart(False) def paintEvent(self, event): pass def update_chart(self, update_axis=True, update_lines=True): pixmap = QPixmap(self.legend.size()) pixmap.fill(QColor(0, 0, 0, 0)) qp = QPainter() qp.begin(pixmap) y_spacing = 30 legend_pen = QPen() qp.setPen(legend_pen) for i in range(len(self.page['lines'])): line = self.page['lines'][i] qp.setPen(legend_pen) qp.drawText(QRect(10, 12 + i * y_spacing, 120, 20), Qt.AlignLeft, line.name) line_pen = QPen() line_pen.setWidth(4) line_pen.setColor(line.line_colour) line_pen.setStyle(line.line_style) if line.line_pattern and line.line_style is Qt.CustomDashLine: line_pen.setDashPattern(line.line_pattern) qp.setPen(line_pen) qp.drawLine(140, 10 + i * y_spacing + 10, 190, 10 + i * y_spacing + 10) qp.end() self.legend.setPixmap(pixmap) if self.page['lines'] and self.datasets: self.chart.paint_lines(self.page['lines'], update_lines, update_axis)
def changeColor(self): ColorD = QColorDialog(self) ColorD.colorSelected.connect(self.textEdit.setTextColor) ColorD.open()