coding utf-8 Form implementation generated from reading ui file xa4a_t

  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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'd:\xa4a_table1.ui'
#
# Created: Thu Oct 30 16:45:58 2008
# by: PyQt4 UI code generator 4.4.3
#
# WARNING! All changes made in this file will be lost!
import sys
from operator import itemgetter
from PyQt4 import QtCore, QtGui, QtSql
import psycopg2
connection = None
TEST_CLIENT = {
'id': 5,
'name': 'test client',
'person': 'me',
'country': 'ua',
'city': 'kyiv',
'address1': 'a1',
'address2': 'a2',
'phone': '123-456',
'zip': '04053',
'comment': 'comment',
}
def createConnection():
global connection
connection = psycopg2.connect('dbname=university user=xa4a')
db = QtSql.QSqlDatabase.addDatabase("QPSQL")
db.setHostName("localhost")
db.setDatabaseName("university")
db.setUserName("xa4a")
db.setPassword("")
if not db.open():
QtGui.QMessageBox.critical(0, QtGui.qApp.tr("Cannot open database"),
QtGui.qApp.tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information "
"how to build it.\n\nClick Cancel to exit."),
QtGui.QMessageBox.Cancle, QtGui.QMessageBox.NoButton)
return False
else:
return True
def fill_dialog(dialog, client):
dialog.client_id = client['id']
dialog.comment.setPlainText(client['comment'])
fields = ['name', 'person', 'country', 'city', 'address1',
'address2', 'phone','zip']
for field in fields:
getattr(dialog, field).setText(client[field])
def get_client(dialog):
client = {}
client['id'] = getattr(dialog, 'client_id', None)
fields = ['name', 'person', 'country', 'city', 'address1',
'address2', 'phone','zip']
for field in fields:
client[field] = getattr(dialog, field).text()
client['comment'] = dialog.comment.toPlainText()
return client
def get_client_by_id(id):
cursor = connection.cursor()
cursor.execute("SELECT * FROM client_full WHERE id = %d" % int(id))
res = cursor.fetchone()
client = dict(zip(map(itemgetter(0), cursor.description), res))
unicode_fields = ['name', 'person', 'address1', 'address2', 'country',
'city', 'comment', 'phone']
for u_f in unicode_fields:
client[u_f] = unicode(client[u_f], 'utf-8')
return client
def add_edit_client(parent, ok_cb, cancel_cb, client=None):
dialog = QtGui.QDialog(parent)
dialog.setWindowTitle ("Add client")
layout = QtGui.QGridLayout(dialog)
name_label = QtGui.QLabel(dialog)
layout.addWidget(name_label, 0, 0)
name_label.setText("Client name")
dialog.name = name = QtGui.QLineEdit(dialog)
layout.addWidget(name, 0, 1)
person_label = QtGui.QLabel(dialog)
layout.addWidget(person_label, 1, 0)
person_label.setText("Contact person")
dialog.person = person = QtGui.QLineEdit(dialog)
layout.addWidget(person, 1, 1)
address_frame = QtGui.QGroupBox("Address", dialog)
layout.addWidget(address_frame,2,0,1,2)
addr_layout = QtGui.QGridLayout(address_frame)
country_label = QtGui.QLabel(address_frame)
addr_layout.addWidget(country_label, 0, 0)
country_label.setText("Country")
dialog.country = country = QtGui.QLineEdit(address_frame)
addr_layout.addWidget(country, 0, 1)
city_label = QtGui.QLabel(address_frame)
addr_layout.addWidget(city_label, 1, 0)
city_label.setText("City")
dialog.city = city = QtGui.QLineEdit(address_frame)
addr_layout.addWidget(city, 1, 1)
address1_label = QtGui.QLabel(address_frame)
addr_layout.addWidget(address1_label, 2, 0)
address1_label.setText("Address1")
dialog.address1 = address1 = QtGui.QLineEdit(address_frame)
addr_layout.addWidget(address1, 2, 1)
address2_label = QtGui.QLabel(address_frame)
addr_layout.addWidget(address2_label, 3, 0)
address2_label.setText("Address2")
dialog.address2 = address2 = QtGui.QLineEdit(address_frame)
addr_layout.addWidget(address2, 3, 1)
zip_label = QtGui.QLabel(address_frame)
addr_layout.addWidget(zip_label, 4, 0)
zip_label.setText("Zip")
dialog.zip = zip = QtGui.QLineEdit(address_frame)
addr_layout.addWidget(zip, 4, 1)
phone_label = QtGui.QLabel(dialog)
addr_layout.addWidget(phone_label, 5, 0)
phone_label.setText("Phone")
dialog.phone = phone = QtGui.QLineEdit(address_frame)
addr_layout.addWidget(phone, 5, 1)
comment_label = QtGui.QLabel(dialog)
layout.addWidget(comment_label, 5, 0, 1, 2)
comment_label.setText("Comment")
dialog.comment = comment = QtGui.QPlainTextEdit(dialog)
layout.addWidget(comment, 6, 0, 1, 2)
ok_button = QtGui.QPushButton(dialog)
layout.addWidget(ok_button, 7, 0)
ok_button.setText("Ok")
cancel_button = QtGui.QPushButton(dialog)
layout.addWidget(cancel_button, 7, 1)
cancel_button.setText("Cancel")
QtCore.QObject.connect(ok_button, QtCore.SIGNAL("clicked()"), ok_cb)
QtCore.QObject.connect(cancel_button, QtCore.SIGNAL("clicked()"), cancel_cb)
dialog.show()
parent.dialog = dialog
if client:
fill_dialog(dialog, client)
def save_client(client):
cursor = connection.cursor()
client_fields = ['name', 'person', 'comment']
address_fields = ['country', 'city', 'address1',
'address2', 'phone', 'zip']
# if editing existing client
if client['id']:
query = """UPDATE "client" SET %s WHERE id=%%s""" % (
', '.join('%s=%%s' % c_field for c_field in client_fields))
params = map(lambda x: unicode(client[x]),client_fields) \
+ [client['id']]
cursor.execute(query, params)
query = """UPDATE "address" SET %s WHERE id=(
SELECT address_id FROM client WHERE id=%%s)""" % (
', '.join('%s=%%s' % a_field for a_field in address_fields))
params = map(lambda x: unicode(client[x]), address_fields) \
+ [client['id']]
cursor.execute(query, params)
else:
query = """INSERT INTO "address" (country, city, address1, address2,
phone, zip) VALUES (%s, %s, %s, %s, %s, %s)"""
params = map(lambda x: unicode(client[x]), address_fields)
cursor.execute(query, params)
query = """INSERT INTO "client" (name, person, comment, address_id)
VALUES (%s, %s, %s, (SELECT MAX(id) FROM "address"))"""
params = map(lambda x: unicode(client[x]), client_fields)
cursor.execute(query, params)
connection.commit()
class client_window_class (object):
def draw_interface(self, workspace):
self.client_window = QtGui.QMainWindow()
self.client_window.setWindowTitle("Clients")
self.client_window.tabWidget = QtGui.QTabWidget(self.client_window)
self.client_window.tab = QtGui.QWidget()
top_layout = self.client_window.top_layout = QtGui.QVBoxLayout(self.client_window.tab)
self.client_window.client_table = QtGui.QTableView(self.client_window.tab)
top_layout.addWidget(self.client_window.client_table)
self.client_window.button_layout = QtGui.QHBoxLayout()
btn_layout = self.client_window.button_layout
top_layout.addLayout(btn_layout)
self.client_window.tab.add_client = QtGui.QToolButton(self.client_window.tab)
btn_layout.addWidget(self.client_window.tab.add_client)
self.client_window.tab.add_client.setText("Add client")
self.client_window.tab.edit_client = QtGui.QToolButton(self.client_window.tab)
btn_layout.addWidget(self.client_window.tab.edit_client)
self.client_window.tab.edit_client.setText("Edit client")
self.client_window.tab.delete_client = QtGui.QToolButton(self.client_window.tab)
btn_layout.addWidget(self.client_window.tab.delete_client)
self.client_window.tab.delete_client.setText("Delete client")
self.client_window.tab.search_client = QtGui.QToolButton(self.client_window.tab)
btn_layout.addWidget(self.client_window.tab.search_client)
self.client_window.tab.search_client.setText("Search")
self.client_window.tab.refresh_client = QtGui.QToolButton(self.client_window.tab)
btn_layout.addWidget(self.client_window.tab.refresh_client)
self.client_window.tab.refresh_client.setText("Refresh")
self.client_window.tabWidget.addTab(self.client_window.tab, "Clients")
self.client_window.setCentralWidget(self.client_window.tabWidget)
QtCore.QObject.connect(self.client_window.tab.add_client, QtCore.SIGNAL("clicked()"),self.click_add_client)
QtCore.QObject.connect(self.client_window.tab.edit_client, QtCore.SIGNAL("clicked()"),self.click_edit_client)
QtCore.QObject.connect(self.client_window.tab.delete_client, QtCore.SIGNAL("clicked()"),self.click_delete_client)
QtCore.QObject.connect(self.client_window.tab.search_client, QtCore.SIGNAL("clicked()"),self.click_search_client)
QtCore.QObject.connect(self.client_window.tab.refresh_client, QtCore.SIGNAL("clicked()"),self.refresh)
QtCore.QMetaObject.connectSlotsByName(workspace)
self.client_window.show()
self.refresh()
def click_add_client (self):
print "Add client button been pressed"
add_edit_client(self.client_window, self.edit_client_ok, self.edit_client_cancel)
def click_edit_client (self):
print "Edit client button been pressed"
t = self.client_window.client_table
row_num = t.currentIndex().row()
# sel_m = t.selectionModel()
# selection = sel_m.selection()
if row_num == -1:
return None
# row_num = selection.first().top()
client_id = int(t.model().data(t.model().index(row_num,0)).toString())
client = get_client_by_id(client_id)
add_edit_client(self.client_window, self.edit_client_ok, self.edit_client_cancel, client)
def click_delete_client (self):
print "Delete client button been pressed"
self.client_window.delete_client_sure = QtGui.QDialog(self.client_window)
self.client_window.delete_client_sure.setWindowTitle("Delete client")
self.client_window.delete_client_sure.resize(213, 78)
self.client_window.delete_client_sure.setMinimumSize(QtCore.QSize(213, 78))
self.client_window.delete_client_sure.setMaximumSize(QtCore.QSize(213, 78))
self.client_window.delete_client_sure.yes_button = QtGui.QPushButton(self.client_window.delete_client_sure)
self.client_window.delete_client_sure.yes_button.setGeometry(QtCore.QRect(20, 40, 75, 23))
self.client_window.delete_client_sure.yes_button.setText("Yes")
self.client_window.delete_client_sure.no_button = QtGui.QPushButton(self.client_window.delete_client_sure)
self.client_window.delete_client_sure.no_button.setGeometry(QtCore.QRect(120, 40, 75, 23))
self.client_window.delete_client_sure.no_button.setText("No")
self.client_window.delete_client_sure.are_you_sure_label = QtGui.QLabel(self.client_window.delete_client_sure)
self.client_window.delete_client_sure.are_you_sure_label.setGeometry(QtCore.QRect(70, 10, 81, 16))
self.client_window.delete_client_sure.are_you_sure_label.setText("Are you sure???")
self.client_window.delete_client_sure.show()
QtCore.QObject.connect(self.client_window.delete_client_sure.yes_button, QtCore.SIGNAL("clicked()"),self.delete_client_sure_yes_pressed)
QtCore.QObject.connect(self.client_window.delete_client_sure.no_button, QtCore.SIGNAL("clicked()"),self.delete_client_sure_no_pressed)
QtCore.QMetaObject.connectSlotsByName(self.client_window)
def delete_client_sure_yes_pressed (self):
print "'Yes delete client are you sure' button been pressed"
self.client_window.delete_client_sure.hide()
def delete_client_sure_no_pressed (self):
print "'No delete client are yot sure' button been pressed"
self.client_window.delete_client_sure.hide()
def click_search_client (self):
print "Search client button been pressed"
def edit_client_ok(self):
print "Ok add client button been pressed"
client = get_client(self.client_window.dialog)
save_client(client)
self.client_window.dialog.hide()
def edit_client_cancel(self):
print "Cancel add client button been pressed"
self.client_window.dialog.hide()
def refresh(self):
model = self._client_model = QtSql.QSqlRelationalTableModel()
model.setTable('client_full')
model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
fields = ['id', 'name', 'person', 'country', 'city', 'address1',
'address2', 'phone', 'zip', 'comment']
#fields = ['name', 'person', 'country', 'city', 'address1',
# 'address2', 'phone', 'zip', 'comment']
for i,field in enumerate(fields):
model.setHeaderData(i, QtCore.Qt.Horizontal,
QtCore.QVariant(QtCore.QObject.tr(model, field)))
model.select()
view = self.client_window.client_table
view.setModel(model)
view.setItemDelegate(QtSql.QSqlRelationalDelegate(view))
view.show()
def main():
app = QtGui.QApplication(sys.argv)
app.setStyle('Cleanlooks')
createConnection()
my_form = client_window_class()
my_form.draw_interface(app)
app.exec_()
if __name__=="__main__":
main()