File size: 9,894 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'    Table Graphic Object
'    Copyright 2008 Daniel Wagner O. de Medeiros
'
'    This file is part of DWSIM.
'
'    DWSIM is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 3 of the License, or
'    (at your option) any later version.
'
'    DWSIM is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.
'
'    You should have received a copy of the GNU General Public License
'    along with DWSIM.  If not, see <http://www.gnu.org/licenses/>.

Imports System.Linq
Imports DWSIM.Interfaces
Imports System.Xml
Imports DWSIM.SharedClasses.Extras
Imports DWSIM.DrawingTools.Point

Namespace GraphicObjects.Tables

    <Serializable()> Public Class SpreadsheetTableGraphic

        Inherits ShapeGraphic

        <Xml.Serialization.XmlIgnore> Public Property Flowsheet As Interfaces.IFlowsheet

        Public Property ClipboardData As String = ""

        Public Overrides Function SaveData() As System.Collections.Generic.List(Of System.Xml.Linq.XElement)

            Dim elements As System.Collections.Generic.List(Of System.Xml.Linq.XElement) = MyBase.SaveData()

            Return elements

        End Function

        Public Overrides Function LoadData(data As System.Collections.Generic.List(Of System.Xml.Linq.XElement)) As Boolean

            Return MyBase.LoadData(data)

        End Function

#Region "Constructors"

        Public Sub New(ByVal graphicPosition As Point)
            Me.New()
            Me.SetPosition(graphicPosition.X, graphicPosition.Y)
        End Sub

        Public Sub New(ByVal posX As Integer, ByVal posY As Integer)
            Me.New(New Point(posX, posY))
        End Sub

        Public Sub New()
            Me.ObjectType = Enums.GraphicObjects.ObjectType.GO_SpreadsheetTable
        End Sub

#End Region

#Region "Properties"

        Public Property Padding As Integer = 4

        Public Property SpreadsheetCellRange As String = ""

        <Xml.Serialization.XmlIgnore> Public Property SpreadsheetData As List(Of String())

        Public Property TextColor As SKColor = SKColors.Black

        Public Property BorderColor As SKColor = SKColors.Black

        Public Property TextColorDark As SKColor = SKColors.WhiteSmoke

        Public Property BorderColorDark As SKColor = SKColors.WhiteSmoke

#End Region

        Public Overrides Sub Draw(ByVal g As Object)

            Dim canvas As SKCanvas = DirectCast(g, SKCanvas)

            Dim tpaint As New SKPaint()
            Dim bpaint As New SKPaint()

            If DrawMode = 0 Then
                With tpaint
                    .TextSize = FontSize
                    .IsAntialias = GlobalSettings.Settings.DrawingAntiAlias
                    If GlobalSettings.Settings.DarkMode Then
                        .Color = TextColorDark
                    Else
                        .Color = TextColor
                    End If
                    .IsStroke = False
                    .Typeface = GetFont()
                End With

                With bpaint
                    .IsAntialias = GlobalSettings.Settings.DrawingAntiAlias
                    If GlobalSettings.Settings.DarkMode Then
                        .Color = BorderColorDark
                    Else
                        .Color = BorderColor
                    End If
                    .IsStroke = True
                    .StrokeWidth = LineWidth
                End With

            Else
                With tpaint
                    .TextSize = FontSize
                    .IsAntialias = GlobalSettings.Settings.DrawingAntiAlias
                    .Color = SKColors.Black
                    .IsStroke = False
                    .Typeface = GetFont()
                End With

                With bpaint
                    .IsAntialias = GlobalSettings.Settings.DrawingAntiAlias
                    .Color = SKColors.Black
                    .IsStroke = True
                    .StrokeWidth = LineWidth
                End With

            End If
            Dim maxW As New List(Of Integer)

            'find number of rows and columns by range

            If SpreadsheetCellRange <> "" Then

                Try

                    SpreadsheetData = Flowsheet.GetSpreadsheetData(SpreadsheetCellRange)

                    Dim formats = Flowsheet.GetSpreadsheetFormat(SpreadsheetCellRange)

                    'determinar comprimento das colunas e altura das linhas

                    Dim i, j, k, itemheight, itemwidth, n, m, leftmargin As Integer
                    Dim size As SKSize

                    k = 0
                    For j = 0 To SpreadsheetData(0).Count - 1
                        maxW.Add(1)
                        For i = 0 To SpreadsheetData.Count - 1
                            If formats IsNot Nothing Then
                                If formats(i)(j) <> "" Then
                                    If Double.TryParse(SpreadsheetData(i)(j), New Double) Then
                                        size = MeasureString(Convert.ToDouble(SpreadsheetData(i)(j)).ToString(formats(i)(j)), tpaint)
                                    Else
                                        size = MeasureString(SpreadsheetData(i)(j), tpaint)
                                    End If
                                Else
                                    size = MeasureString(SpreadsheetData(i)(j), tpaint)
                                End If
                            Else
                                size = MeasureString(SpreadsheetData(i)(j), tpaint)
                            End If
                            If size.Width > maxW(k) Then maxW(k) = size.Width
                        Next
                        maxW(k) += 4 * Padding
                        k += 1
                    Next

                    itemheight = MeasureString("AAA", tpaint).Height + 2 * Me.Padding

                    Me.Height = (SpreadsheetData.Count) * itemheight
                    Me.Width = maxW.Sum

                    'Dim rect As SKRect = GetRect(X, Y, Width, Height)
                    'canvas.DrawRect(rect, GetPaint(SKColors.White))

                    size = MeasureString("MEASURE", tpaint)

                    ClipboardData = ""

                    n = 0
                    leftmargin = 0
                    For j = 0 To SpreadsheetData(0).Count - 1
                        m = 0
                        For i = 0 To SpreadsheetData.Count - 1

                            If formats IsNot Nothing Then
                                If formats(i)(j) <> "" Then
                                    If Double.TryParse(SpreadsheetData(i)(j), New Double) Then
                                        Dim val = Convert.ToDouble(SpreadsheetData(i)(j)).ToString(formats(i)(j))
                                        itemwidth = MeasureString(val, tpaint).Width + 2 * Padding
                                        canvas.DrawText(val, X + Padding + leftmargin + maxW(n) - itemwidth, Y + Padding + m * itemheight + size.Height, tpaint)
                                    Else
                                        canvas.DrawText(SpreadsheetData(i)(j), X + Padding + leftmargin, Y + Padding + m * itemheight + size.Height, tpaint)
                                    End If
                                Else
                                    canvas.DrawText(SpreadsheetData(i)(j), X + Padding + leftmargin, Y + Padding + m * itemheight + size.Height, tpaint)
                                End If
                            Else
                                canvas.DrawText(SpreadsheetData(i)(j), X + Padding + leftmargin, Y + Padding + m * itemheight + size.Height, tpaint)
                            End If
                            ClipboardData += SpreadsheetData(i)(j) + vbTab
                            If i < SpreadsheetData.Count - 1 Then canvas.DrawLine(X + leftmargin, Y + (m + 1) * itemheight, X + leftmargin + maxW(n), Y + (m + 1) * itemheight, bpaint)
                            m += 1
                        Next
                        ClipboardData += vbCrLf
                        leftmargin += maxW(n)
                        If j < SpreadsheetData(0).Count - 1 Then canvas.DrawLine(X + leftmargin, Y, X + leftmargin, Y + (SpreadsheetData.Count) * itemheight, bpaint)
                        n += 1
                    Next

                    canvas.DrawRect(GetRect(Me.X, Me.Y, Me.Width, Me.Height), bpaint)

                Catch ex As Exception

                    Dim Size = MeasureString("Error: " + ex.Message.ToString, tpaint)
                    canvas.DrawText("Error: " + ex.Message.ToString, X + 10, Y + 40, tpaint)

                    Me.Width = 20 + Size.Width
                    Me.Height = 80 + Size.Height

                    canvas.DrawRect(GetRect(X, Y, Width, Height), bpaint)

                End Try

            Else

                Dim Size = MeasureString("Double-click to edit", tpaint)
                canvas.DrawText("Double-click to edit", X + 10, Y + 40, tpaint)

                Me.Width = 20 + Size.Width
                Me.Height = 80 + Size.Height

                canvas.DrawRect(GetRect(X, Y, Width, Height), bpaint)

            End If

            tpaint.Dispose()
            bpaint.Dispose()

            tpaint = Nothing
            bpaint = Nothing

        End Sub

    End Class

End Namespace