File size: 8,182 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ***************************************************************************
# *   Copyright (c) 2025 Stefan Tröger <stefantroeger@gmx.net>              *
# *                                                                         *
# *   This file is part of the FreeCAD CAx development system.              *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program 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 Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************

__title__ = "FreeCAD table view widget to visualize vtkTable"
__author__ = "Stefan Tröger"
__url__ = "https://www.freecad.org"

## @package vtk_table_view
#  \ingroup FEM
#  \brief A Qt widget to show a vtkTable

from PySide import QtGui
from PySide import QtCore

import FreeCAD
import FreeCADGui

from vtkmodules.vtkIOCore import vtkDelimitedTextWriter

translate = FreeCAD.Qt.translate


class VtkTableModel(QtCore.QAbstractTableModel):
    # Simple table model. Only supports single component columns
    # One can supply a header_names dict to replace the table column names
    # in the header. It is a dict "column_idx (int)" to "new name"" or
    # "orig_name (str)" to "new name"

    def __init__(self, header_names=None):
        super().__init__()
        self._table = None
        if header_names:
            self._header = header_names
        else:
            self._header = {}

    def setTable(self, table, header_names=None):
        self.beginResetModel()
        self._table = table
        if header_names:
            self._header = header_names
        self.endResetModel()

    def rowCount(self, index):

        if not self._table:
            return 0

        return self._table.GetNumberOfRows()

    def columnCount(self, index):

        if not self._table:
            return 0

        return self._table.GetNumberOfColumns()

    def data(self, index, role):

        if not self._table:
            return None

        if role == QtCore.Qt.DisplayRole:
            col = self._table.GetColumn(index.column())
            return col.GetTuple(index.row())[0]

        return None

    def headerData(self, section, orientation, role):

        if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
            if section in self._header:
                return self._header[section]

            name = self._table.GetColumnName(section)
            if name in self._header:
                return self._header[name]

            return name

        if orientation == QtCore.Qt.Vertical and role == QtCore.Qt.DisplayRole:
            return section

        return None

    def getTable(self):
        return self._table


class VtkTableSummaryModel(QtCore.QAbstractTableModel):
    # Simple model showing a summary of the table.
    # Only supports single component columns

    def __init__(self):
        super().__init__()
        self._table = None

    def setTable(self, table):
        self.beginResetModel()
        self._table = table
        self.endResetModel()

    def rowCount(self, index):

        if not self._table:
            return 0

        return self._table.GetNumberOfColumns()

    def columnCount(self, index):
        return 2  # min, max

    def data(self, index, role):

        if not self._table:
            return None

        if role == QtCore.Qt.DisplayRole:
            col = self._table.GetColumn(index.row())
            range = col.GetRange()
            return range[index.column()]

        return None

    def headerData(self, section, orientation, role):

        if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
            return ["Min", "Max"][section]

        if orientation == QtCore.Qt.Vertical and role == QtCore.Qt.DisplayRole:
            return self._table.GetColumnName(section)

        return None

    def getTable(self):
        return self._table


class VtkTableView(QtGui.QWidget):

    def __init__(self, model):
        super().__init__()

        self.model = model

        layout = QtGui.QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)

        # start with the toolbar
        self.toolbar = QtGui.QToolBar()
        csv_action = QtGui.QAction(self)
        csv_action.triggered.connect(self.exportCsv)
        csv_action.setIcon(FreeCADGui.getIcon("Std_Export"))
        csv_action.setToolTip(translate("FEM", "Export to CSV"))
        self.toolbar.addAction(csv_action)

        copy_action = QtGui.QAction(self)
        copy_action.triggered.connect(self.copyToClipboard)
        copy_action.setIcon(FreeCADGui.getIcon("edit-copy"))
        shortcut = QtGui.QKeySequence(QtGui.QKeySequence.Copy)
        copy_action.setToolTip(
            translate("FEM", "Copy selection to clipboard ({})".format(shortcut.toString()))
        )
        copy_action.setShortcut(shortcut)
        self.toolbar.addAction(copy_action)

        layout.addWidget(self.toolbar)

        # now the table view
        self.table_view = QtGui.QTableView()
        self.table_view.setModel(model)
        self.model.modelReset.connect(self.modelReset)

        # fast initial resize and manual resizing still allowed!
        header = self.table_view.horizontalHeader()
        header.setResizeContentsPrecision(10)
        self.table_view.resizeColumnsToContents()

        layout.addWidget(self.table_view)
        self.setLayout(layout)

    @QtCore.Slot()
    def modelReset(self):
        # The model is reset, make sure the header visibility is working
        # This is needed in case new data was added
        self.table_view.resizeColumnsToContents()

    @QtCore.Slot(bool)
    def exportCsv(self, state):

        file_path, filter = QtGui.QFileDialog.getSaveFileName(
            None, translate("FEM", "Save as csv file"), "", "CSV (*.csv)"
        )
        if not file_path:
            FreeCAD.Console.PrintMessage(
                translate("FEM", "CSV file export aborted: no filename selected")
            )
            return

        writer = vtkDelimitedTextWriter()
        writer.SetFileName(file_path)
        writer.SetInputData(self.model.getTable())
        writer.Write()

    @QtCore.Slot()
    def copyToClipboard(self):

        sel_model = self.table_view.selectionModel()
        selection = sel_model.selectedIndexes()

        if len(selection) < 1:
            return

        copy_table = ""
        previous = selection.pop(0)
        for current in selection:

            data = self.model.data(previous, QtCore.Qt.DisplayRole)
            copy_table += str(data)

            if current.row() != previous.row():
                copy_table += "\n"
            else:
                copy_table += "\t"

            previous = current

        copy_table += str(self.model.data(selection[-1], QtCore.Qt.DisplayRole))
        copy_table += "\n"

        clipboard = QtGui.QApplication.instance().clipboard()
        clipboard.setText(copy_table)