summaryrefslogtreecommitdiff
path: root/graphics/asymptote/GUI/Widg_addLabel.py
blob: a5a6503503bd562f4f2f62a9d15903382107c0be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3

from pyUIClass.widg_addLabel import Ui_Form
import PyQt5.QtWidgets as QtWidgets
import PyQt5.QtGui as QtGui

import labelEditor
import xasyUtils as xu


class Widg_addLabel(QtWidgets.QWidget):
    def __init__(self, info):
        super().__init__()
        self.ui = Ui_Form()
        self.info = info
        self.ui.setupUi(self)
        self.setFixedSize(self.size())

        if 'alignIndex' not in self.info.keys():
            self.info['alignIndex'] = 0

        if 'shift_x' not in self.info.keys():
            self.info['shift_x'] = None

        if 'shift_y' not in self.info.keys():
            self.info['shift_y'] = None

        if 'align' not in self.info.keys():
            self.info['align'] = (0, 0)

        if self.info['shift_x'] is not None:
            self.ui.txtShiftX.setText(str(self.info['shift_x']))

        if self.info['shift_y'] is not None:
            self.ui.txtShiftY.setText(str(self.info['shift_y']))


        self.ui.cmbFontSize.setCurrentText(str(self.info['fontSize']) if self.info['fontSize'] is not None else '-')
        self.ui.cmbAlign.setCurrentIndex(self.info['alignIndex'])

        validator = QtGui.QDoubleValidator()

        self.ui.txtShiftX.setValidator(validator)
        self.ui.txtShiftY.setValidator(validator)
        self.ui.cmbFontSize.setValidator(validator)

        self.ui.cmbAlign.currentTextChanged.connect(self.updateCheck)
        self.ui.cmbAlign.currentIndexChanged.connect(self.cmbIndexUpdate)
        self.ui.txtShiftX.textEdited.connect(self.shftXUpdate)
        self.ui.txtShiftY.textEdited.connect(self.shftYUpdate)
        self.ui.btnAdvancedEdit.clicked.connect(self.btnAdvancedEditClicked)
        self.ui.cmbFontSize.currentTextChanged.connect(self.cmbFontSizeTextChanged)

        self.updateCheck(self.ui.cmbAlign.currentText())

    def cmbFontSizeTextChanged(self, text: str):
        tryParseVal = xu.tryParse(text, float)
        self.info['fontSize'] = tryParseVal

    def btnAdvancedEditClicked(self):
        advancedEditDialog = labelEditor.labelEditor(self.ui.txtLabelText.text())
        advancedEditDialog.show()
        result = advancedEditDialog.exec_()
        if result == QtWidgets.QDialog.Accepted:
            self.ui.txtLabelText.setText(advancedEditDialog.getText())

    @property
    def labelText(self):
        return self.ui.txtLabelText.text()

    def updateCheck(self, a0):
        self.ui.txtShiftX.setEnabled(a0 == 'Custom')
        self.ui.txtShiftY.setEnabled(a0 == 'Custom')

    def shftXUpdate(self, text):
        if text:
            self.info['shift_x'] = float(text)
            self.updateAlign()

    def shftYUpdate(self, text):
        if text:
            self.info['shift_y'] = float(text)
            self.updateAlign()

    def updateAlign(self):
        index = self.ui.cmbAlign.currentIndex()
        self.info['alignIndex'] = index
        if self.ui.cmbAlign.currentText() == 'Custom':
            self.info['align'] = (self.info['shift_x'], self.info['shift_y'])
        elif self.ui.cmbAlign.currentText() == 'None':
            self.info['align'] = (0, 0)
        else:
            self.info['align'] = self.ui.cmbAlign.currentText()

    def cmbIndexUpdate(self, index):
        self.updateAlign()