1 from PyQt4 import QtGui, QtCore
2 from PyQt4.QtGui import QWizard, QWizardPage, QToolBar, QFileDialog, QPushButton, QTableView, QFont, QVBoxLayout, QGridLayout, QLabel, QComboBox, QItemDelegate, QStandardItemModel, QColor, QCheckBox
3 from PyQt4.QtCore import QString, QAbstractTableModel, QVariant, Qt, QAbstractListModel, QModelIndex, QStringList, QPoint
4 from camelot.view import art
5 from camelot.view.art import Icon
6 from camelot.action import createAction, addActions
7 from camelot.view.elixir_admin import EntityAdmin
8 from camelot.view.model_thread import get_model_thread
9 from camelot.view.controls.exception import model_thread_exception_message_box
10 from camelot.view.controls.delegates.comboboxdelegate import ComboBoxEditorDelegate, TestComboBoxDelegate
11 from camelot.view.controls.editors.choiceseditor import ChoicesEditor
12 import csv, itertools
13
14 _ = lambda x: x
15
17 """Import wizard GUI"""
19 QWizard.__init__(self)
20 self.parent = parent
21
22 self.attributes = attributes
23
24 """ Make a wizard and the pages """
26 self.qWizard = QWizard()
27 self.qWizard.setEnabled(True)
28
29 self.qPage = ImportWizardPage(self.qWizard)
30 self.qPage.setTitle(QString('import wizard'))
31
32 self.makeToolBarToSearchFile()
33
34
35 self.makeGridLayout()
36
37
38 self.qPage.setLayout(self.grid)
39 self.qWizard.addPage(self.qPage)
40
41
42 self.qTablePage = QWizardPage(self.qWizard)
43 self.qTablePage.setTitle(QString('Data from file'))
44
45 self.qWizard.addPage(self.qTablePage)
46
47 cancelButton = QPushButton(QString('cancel'), self.qWizard)
48 self.qWizard.setButton(QWizard.CancelButton, cancelButton)
49
50 finishButton = QWizard.FinishButton
51 self.qWizard.setButtonText(finishButton, QString('import'))
52
53 self.qWizard.show()
54 self.qWizard.exec_()
55
56 """
57 makes toolbar with button to open a csv-file. this is added at the qpage of a qwizard
58 """
65
67 self.checkBox = QCheckBox('first row of data is column name')
68 action = QtGui.QAction('CheckBox', self.checkBox)
69 self.checkBox.connect(self.checkBox, QtCore.SIGNAL("clicked()"), self.repaintTable)
70 self.checkBox.addAction(action)
71
72 checkBox = QCheckBox('first row of data is column name')
73 action = QtGui.QAction('CheckBox', checkBox)
74 checkBox.connect(action, QtCore.SIGNAL('clicked()'), self.repaintTable)
75 checkBox.addAction(action)
76 return checkBox
77
78 """
79 depending on the checkbox the table must be drawn again
80 """
82 print 'repaint table'
83 if self.checkBox.checkState() == Qt.Unchecked:
84 newTable = self.makeTable(self.data, self.attributes)
85 self.updateTable(newTable)
86 else:
87 dataWithoutFirstRow = self.data[1:]
88 newTable = self.makeTable(dataWithoutFirstRow, self.attributes)
89 self.updateTable(newTable)
90
91 dataWithoutFirstRow = self.data[1:]
92 self.makeTable(dataWithoutFirstRow, self.attributes)
93
94
95 """
96 makes the openfiledialog: when the file is committed, the table is shown.
97 the method prepares also the table to show
98 """
100 filename = QtGui.QFileDialog.getOpenFileName(None, 'Open file', '/')
101
102 self.label.clear()
103 self.label = QtGui.QLabel(filename, self.qPage)
104 self.qPage.initializePath(filename)
105 self.grid.addWidget(self.label, 2, 0)
106
107
108 file=open(filename)
109 csvreader = csv.reader(file)
110 array = list(csvreader)
111 self.data = array
112
113 self.makeCheckBoxForFirstRow()
114
115 self.tableView = self.makeTable(array, self.attributes, False)
116 self.setTableViewLayout()
117
118 """ layout of the page with the table
119 above a checkbox, below the table
120 """
122 self.vLayout = QVBoxLayout()
123 self.vLayout.addWidget(self.checkBox)
124 self.vLayout.addWidget(self.tableView)
125 self.qTablePage.setLayout(self.vLayout)
126
127 """
128 if the checkbox is checked the first row isn't drawn anymore but used for the delegate
129 """
131 self.vLayout.removeWidget(self.tableView)
132 self.vLayout.addWidget(newTable)
133 self.tableView = newTable
134 self.vLayout.update()
135
136 """ the layout for the wizard """
138 self.grid = QtGui.QGridLayout()
139 self.grid.setSpacing(10)
140 self.grid.addWidget(self.openToolBar, 1, 0)
141 self.label = QtGui.QLabel('select file', self.qPage)
142 self.grid.addWidget(self.label, 2, 0)
143
144 """ make the table for the page"""
145 - def makeTable(self, data, headerData, firstRow=False):
146
147 tv = QTableView()
148
149
150 tm = InputTableModel(data, self.attributes, self.qTablePage)
151
152 tv.setModel(tm)
153 CHOICES = self.makeChoices(headerData)
154
155 delegate = ComboBoxEditorDelegate(choices=lambda o:CHOICES, parent=tv )
156 tv.setItemDelegateForRow(0,delegate)
157
158
159 self.setMinimumSize(800, 600)
160
161
162 tv.setShowGrid(True)
163
164
165 font = QFont("Courier New", 20)
166 tv.setFont(font)
167
168
169 vh = tv.verticalHeader()
170 vh.setVisible(False)
171
172
173 hh = tv.horizontalHeader()
174 hh.setVisible(False)
175
176
177
178
179
180
181 nrows = len(list(data))
182 for row in xrange(nrows):
183 tv.setRowHeight(row, 18)
184 return tv
185
186 """" method for initializing the choices of the delegate. a tuple is returned"""
188 CHOICES = []
189 for i in range(len(choices)):
190 CHOICES = CHOICES + [(str(i) , choices[i])]
191 return tuple(CHOICES)
192
194 for column in range(len(header)):
195 index = model.index(0, column, QModelIndex())
196 model.setData(index, QVariant(header[column]))
197
198
199
200 - def makeBody(self, model, data):
201 for row in range(len(data)):
202 for column in range(len(self.attributes)):
203 index = model.index((row+1), column, QModelIndex())
204 model.setData(index, QVariant(self.data[row][column]))
205
206 """method returning the imported data"""
208 return list(self.data)
209
210
218
227
228
229
234
248
249
253
256
275
282
283
286
287 """every item is editable, so no need to keep it for each object """
290
291
292 -class ImportWizardPage(QtGui.QWizardPage):
293 """
294 class for the page shown in the wizard
295 """
296 - def __init__(self, parent=None, path=None, *args):
297 QWizardPage.__init__(self, parent, *args)
298 self.path = path
299
300 - def initializePath(self, path):
301 self.path = path
302 self.emit(QtCore.SIGNAL('completeChanged()'))
303
304 - def isComplete(self):
305 return self.path != None
306