summaryrefslogtreecommitdiff
path: root/graphics/asymptote/GUI/ContextWindow.py
blob: 2711cfeca739449ab23a2282e3788dec5a4d18ad (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import PyQt5.QtWidgets as Qw
import PyQt5.QtGui as Qg
import PyQt5.QtCore as Qc
import xasyVersion

import xasyUtils as xu
import xasy2asy as x2a
import xasyFile as xf
import xasyOptions as xo
import UndoRedoStack as Urs
import xasyArgs as xa
import xasyBezierInterface as xbi
from xasyTransform import xasyTransform as xT
import xasyStrings as xs

import PrimitiveShape
import InplaceAddObj

import CustMatTransform
import SetCustomAnchor
import GuidesManager
import time

class AnotherWindow(Qw.QWidget):
    def __init__(self, shape, parent):
        super().__init__()
        self.shape = shape
        self.parent = parent
        self.newShape = self.shape
        self.layout = Qw.QVBoxLayout(self)

        # Initialize tab screen
        self.tabs = Qw.QTabWidget()
        self.fillTab = Qw.QWidget()
        self.lineTab = Qw.QWidget()
        self.arrowTab = Qw.QWidget()
        self.othersTab = Qw.QWidget()
        self.tabs.resize(300,200)
        self.fillTab.layout = Qw.QVBoxLayout(self.fillTab)
        self.lineTab.layout = Qw.QVBoxLayout(self.lineTab)
        self.arrowTab.layout = Qw.QVBoxLayout(self.arrowTab)
        self.othersTab.layout = Qw.QVBoxLayout(self.othersTab)
        self.tabs.addTab(self.fillTab,"Fill Options")
        self.tabs.addTab(self.lineTab,"Line Options")
        self.tabs.addTab(self.arrowTab,"Arrow Options")
        self.tabs.addTab(self.othersTab,"Misc. Options")

        self.layout.addWidget(self.tabs)
        self.setLayout(self.layout)
        self.setWindowTitle("Shape Options Window")

        self.label = Qw.QLabel("Fill:")
        self.fillTab.layout.addWidget(self.label)
        self.fillButton = Qw.QComboBox()
        self.fillButton.addItem("Unfilled")
        self.fillButton.addItem("Filled")
        self.fillButton.currentIndexChanged.connect(self.fillChange)
        self.fillTab.layout.addWidget(self.fillButton)

        if isinstance(self.shape, x2a.asyArrow):
            self.colorButton = Qw.QPushButton("Set Line Colour")
            self.colorButton.clicked.connect(self.pickColor)
            self.fillTab.layout.addWidget(self.colorButton)

            self.colorButton = Qw.QPushButton("Set Fill Colour")
            self.colorButton.clicked.connect(self.pickFillColor)
            self.fillTab.layout.addWidget(self.colorButton)

        elif isinstance(self.shape, x2a.xasyShape):
            self.colorButton = Qw.QPushButton("Set Colour")
            self.colorButton.clicked.connect(self.pickColor)
            self.fillTab.layout.addWidget(self.colorButton)

        self.label = Qw.QLabel("Reflection:")
        self.othersTab.layout.addWidget(self.label)
        self.reflectionButton = Qw.QComboBox()
        self.reflectionButton.addItem("None")
        self.reflectionButton.addItem("Horizontal")
        self.reflectionButton.addItem("Vertical")
        self.reflectionButton.currentIndexChanged.connect(self.reflectionChange)
        self.othersTab.layout.addWidget(self.reflectionButton)

        self.label = Qw.QLabel("Opacity:")
        self.othersTab.layout.addWidget(self.label)
        self.opacityBox = Qw.QLineEdit()
        self.othersTab.layout.addWidget(self.opacityBox)
        self.opacityBox.setPlaceholderText(str(self.shape.pen.opacity))

        self.label = Qw.QLabel("Arrowhead:")
        self.arrowTab.layout.addWidget(self.label)
        self.arrowheadButton = Qw.QComboBox()
        self.arrowList = ["None","Arrow","ArcArrow"]
        for arrowMode in self.arrowList:
            self.arrowheadButton.addItem(arrowMode)
        self.arrowheadButton.currentIndexChanged.connect(self.arrowheadChange)
        self.arrowTab.layout.addWidget(self.arrowheadButton)

        self.label = Qw.QLabel("Line Style:")
        self.lineTab.layout.addWidget(self.label)
        self.linestyleButton = Qw.QComboBox()
        self.lineList = ["solid","dashed","dotted","dashdotted"]

        for lineMode in self.lineList:
            self.linestyleButton.addItem(lineMode)
        self.linestyleButton.currentIndexChanged.connect(self.linestyleChange)
        self.lineTab.layout.addWidget(self.linestyleButton)
        self.linestyleButton.setCurrentIndex(self.lineList.index(self.shape.pen.style))

        self.label = Qw.QLabel("Line Cap Style:")
        self.lineTab.layout.addWidget(self.label)
        self.lineCapStyleButton = Qw.QComboBox()
        self.lineCapListStrings = ["extendcap","flatcap","roundcap"] #Is there a way to pull these directly
        self.lineCapList = [Qc.Qt.PenCapStyle.SquareCap,Qc.Qt.PenCapStyle.FlatCap,Qc.Qt.PenCapStyle.RoundCap]

        for lineMode in self.lineCapListStrings:
            self.lineCapStyleButton.addItem(lineMode)
        self.lineCapStyleButton.currentIndexChanged.connect(self.lineCapStyleChange)
        self.lineTab.layout.addWidget(self.lineCapStyleButton)
        self.lineCapStyleButton.setCurrentIndex(self.lineCapList.index(self.shape.pen.capStyle))

        #TODO: Make this a function.
        if not isinstance(self.shape, x2a.xasyShape):
            self.fillButton.setCurrentIndex(int(self.shape.arrowSettings["fill"]))
            if isinstance(self.shape, x2a.asyArrow):
                self.arrowheadButton.setCurrentIndex(int(self.shape.arrowSettings["active"]))
            else:
                self.arrowheadButton.setDisabled(True)
        else:
            self.fillButton.setCurrentIndex(int(self.shape.path.fill))

        if isinstance(self.shape, x2a.asyArrow) and self.shape.arrowSettings["active"]: #Make these all a list or something.
            self.label = Qw.QLabel("Arrow Style:")
            self.arrowTab.layout.addWidget(self.label)
            self.arrowstyleButton = Qw.QComboBox()
            for arrowStyle in self.shape.arrowStyleList:
                self.arrowstyleButton.addItem(arrowStyle if arrowStyle else "(default)")
            self.arrowstyleButton.currentIndexChanged.connect(self.arrowstyleChange)
            self.arrowTab.layout.addWidget(self.arrowstyleButton)

            self.label = Qw.QLabel("Arrow Size:")
            self.arrowTab.layout.addWidget(self.label)
            self.arrowSizeBox = Qw.QLineEdit()
            self.arrowTab.layout.addWidget(self.arrowSizeBox)
            self.arrowSizeBox.setPlaceholderText(self.getInfo("DefaultHead.size(currentpen)"))

            self.label = Qw.QLabel("Arrow Angle:")
            self.arrowTab.layout.addWidget(self.label)
            self.arrowAngleBox = Qw.QLineEdit()
            self.arrowTab.layout.addWidget(self.arrowAngleBox)
            self.arrowAngleBox.setPlaceholderText(self.getInfo("arrowangle"))

            self.label = Qw.QLabel("Arrow Fill:")
            self.arrowTab.layout.addWidget(self.label)
            self.arrowFillButton = Qw.QComboBox()
            for arrowFillStyle in self.shape.arrowFillList:
                self.arrowFillButton.addItem(arrowFillStyle if arrowFillStyle else "(default)")
            self.arrowFillButton.currentIndexChanged.connect(self.arrowFillChange)
            self.arrowTab.layout.addWidget(self.arrowFillButton)

            self.arrowstyleButton.setCurrentIndex(int(self.shape.arrowSettings["style"]))
            self.arrowFillButton.setCurrentIndex(int(self.shape.arrowSettings["fill"]))

        self.fillTab.setLayout(self.fillTab.layout)
        self.lineTab.setLayout(self.lineTab.layout)
        self.arrowTab.setLayout(self.arrowTab.layout)
        self.othersTab.setLayout(self.othersTab.layout)

        self.confirmButton = Qw.QPushButton("Render")
        self.confirmButton.clicked.connect(self.renderChanges)
        self.layout.addWidget(self.confirmButton)

    def arrowheadChange(self, i):
        #None, {Arrow, ArcArrow} x {(),(SimpleHead),(HookHead),(TeXHead)}
        if isinstance(self.shape, x2a.xasyShape):
            if i != 0:
                if isinstance(self.newShape,x2a.asyArrow):
                    self.newShape.arrowSettings["active"] = i
                else:
                    self.newShape = self.shape.arrowify(arrowhead=i)
        else:
            self.newShape.arrowSettings["active"] = i #Simplify the logic

    def arrowstyleChange(self, i):
        self.newShape.arrowSettings["style"] = i

    def linestyleChange(self, i): #I think add an attribute to asyPen
        self.shape.pen.setStyle(self.lineList[i])

    def lineCapStyleChange(self, i): #I think add an attribute to asyPen
        self.shape.pen.setCapStyle(self.lineCapList[i])

    def fillChange(self, i):
        if isinstance(self.shape, x2a.asyArrow):
            self.shape.arrowSettings["fill"] = bool(i)
        elif (self.shape.path.fill != bool(i)) and not isinstance(self.newShape, x2a.asyArrow):
            if self.newShape:
                self.newShape = self.newShape.swapFill()
        if isinstance(self.newShape, x2a.asyArrow):
            self.newShape.arrowSettings["fill"] = bool(i)

    def reflectionChange(self, i): #TODO: Modernize this.
        reflectionList = [[1,1],[1,-1],[-1,1]]
        self.parent.newTransform = xT.makeScaleTransform(*reflectionList[i], self.parent.currentAnchor).toQTransform()
        self.parent.currentlySelectedObj['selectedIndex'] = self.parent.mostRecentObject
        self.parent.releaseTransform()
        self.parent.newTransform = Qg.QTransform()

    def sizeChange(self):
        try:
            newSize = self.arrowSizeBox.text()
            self.newShape.arrowSettings["size"] = float(newSize)
        except:
            return #TODO: Show error message.

    def angleChange(self): #Refactor this with the above.
        try:
            newAngle = self.arrowAngleBox.text()
            self.newShape.arrowSettings["angle"] = float(newAngle)
        except:
            return #TODO: Show error message.

    def arrowFillChange(self, i): #Can I lambda this?
        self.newShape.arrowSettings["fill"] = i

    def opacityChange(self):
        newOpacity = self.opacityBox.text()
        try:
            newOpacity = int(newOpacity)
            if newOpacity >= 0 and newOpacity <= 255:
                self.shape.pen.setOpacity(newOpacity)
                self.newShape.pen.setOpacity(newOpacity)
        except:
            pass

    def renderChanges(self): #Pull from text boxes here.
        self.opacityChange()
        if isinstance(self.shape, x2a.asyArrow) and self.shape.arrowSettings["active"]:
            self.sizeChange()
            self.angleChange()
        elif (not isinstance(self.shape, x2a.asyArrow)):
            self.renderLineStyle()
        if self.newShape:
            self.parent.replaceObject(self.parent.contextWindowObject,self.newShape)
        self.parent.terminateContextWindow()

    def getInfo(self,value):
        """ Find out the size of an arbitrary Asymptote pen """
        self.asyEngine = self.parent.asyEngine
        assert isinstance(self.asyEngine, x2a.AsymptoteEngine)
        assert self.asyEngine.active

        fout = self.asyEngine.ostream
        fin = self.asyEngine.istream

        fout.write("write(_outpipe,{},endl);\n".format(value))
        fout.write(self.asyEngine.xasy)
        fout.flush()

        return fin.readline()

    def getPattern(self,pattern,path):
        """ Find out the adjusted pattern of an Asymptote pen """
        self.asyEngine = self.parent.asyEngine
        assert isinstance(self.asyEngine, x2a.AsymptoteEngine)
        assert self.asyEngine.active

        fout = self.asyEngine.ostream
        fin = self.asyEngine.istream

        #fout.write("pen p=adjust({pattern},arclength({path}),cyclic({path}));\n")
        #print(f"write(_outpipe,adjust({pattern},arclength({path}),cyclic({path})),endl);\n")
        fout.write(f"write(_outpipe,adjust({pattern},arclength({path}),cyclic({path})),endl);\n")
        fout.write(self.asyEngine.xasy)
        fout.flush()

        return fin.readline()

    def renderLineStyle(self):
        #Should only get called with asy shapes
        if not self.newShape:
            self.newShape=self.shape
        if not isinstance(self.newShape,x2a.asyArrow):
            rawPattern = self.getPattern(self.lineList[self.linestyleButton.currentIndex()],self.newShape.path.getCode())
        else:
            #self.newShape.updateCode() #idk if this is necessary.
            rawPattern = self.getPattern(self.lineList[self.linestyleButton.currentIndex()],self.newShape.code)

        pattern = []
        if len(rawPattern) == 5:
            pattern=[1,0]
        else:
            for value in rawPattern[2:-3].split(' '):
                pattern.append(float(value)+1)

        try:
            self.newShape.pen.setDashPattern(pattern) #pen is going to be a asyPen, add as an attribute
        except:
            print("Pen format error")

    def pickColor(self):
        self.colorDialog = Qw.QColorDialog(x2a.asyPen.convertToQColor(self.shape.pen.color), self)
        self.colorDialog.show()
        result = self.colorDialog.exec()
        if result == Qw.QDialog.Accepted:
            self.shape.pen.setColorFromQColor(self.colorDialog.selectedColor())
            self.parent.updateFrameDispColor()

    def pickFillColor(self): #This is a copy of the above, how do you set the var as it is set?
        self.colorDialog = Qw.QColorDialog(x2a.asyPen.convertToQColor(self.shape.fillPen.color), self)
        self.colorDialog.show()
        result = self.colorDialog.exec()
        if result == Qw.QDialog.Accepted:
            self.shape.fillPen.setColorFromQColor(self.colorDialog.selectedColor())
            self.parent.updateFrameDispColor()

    @Qc.pyqtSlot()
    def on_click(self):
        print("\n")
        for currentQTableWidgetItem in self.tableWidget.selectedItems():
            print(currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text())