Guest User

http://stackoverflow.com/questions/11453256

a guest
Jul 31st, 2012
870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.19 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. #imports:
  3. import os
  4. import platform
  5. import sys
  6. from PyQt4 import QtCore, QtGui
  7. from PyQt4.QtCore import *
  8. from PyQt4.QtGui import *
  9. from newchilddlg import NewChildDlg
  10.  
  11. #version:
  12. __version__ = "1.0.0"
  13.  
  14.  
  15. class MainWindow(QtGui.QMainWindow):
  16.  
  17.     COLUMNS = (
  18.         'am', 'courtdate', 'board_number', 'namesurname', 'fathersname',
  19.         'mothersname', 'birthday', 'placeofbirth', 'nationality', 'address',
  20.         'tel', 'job', 'praxis', 'dayofdoing', 'placeofdoing'
  21.     )
  22.  
  23.     def __init__(self, parent=None):
  24.         super(MainWindow, self).__init__(parent)
  25.  
  26.         #Window size:
  27.         self.resize(800,600)
  28.         self.centralwidget = QtGui.QWidget(self)
  29.         self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
  30.  
  31.         #Table Widget:
  32.         self.tableWidget = QtGui.QTableWidget(self.centralwidget)
  33.         self.vBar = self.tableWidget.verticalScrollBar()
  34.         self._vBar_lastVal = self.vBar.value()
  35.         self.horizontalLayout.addWidget(self.tableWidget)
  36.         self.vBar.valueChanged.connect(self.scrollbarChanged)
  37.         self.tableWidget.setGridStyle(QtCore.Qt.SolidLine)
  38.         self.tableWidget.setRowCount(100)
  39.         self.tableWidget.setColumnCount(15)
  40.  
  41.         for i in xrange(15):
  42.             item = QtGui.QTableWidgetItem()
  43.             self.tableWidget.setHorizontalHeaderItem(i, item)
  44.  
  45.         self.tableWidget.horizontalHeader().setDefaultSectionSize(140)
  46.         self.tableWidget.horizontalHeader().setHighlightSections(True)
  47.         self.tableWidget.horizontalHeader().setStretchLastSection(False)
  48.         self.tableWidget.verticalHeader().setDefaultSectionSize(30)
  49.         self.tableWidget.verticalHeader().setStretchLastSection(False)
  50.         self.horizontalLayout.addWidget(self.tableWidget)
  51.         self.setCentralWidget(self.centralwidget)
  52.  
  53.         #Window Title:
  54.         self.setWindowTitle(self.tr("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
  55.         self.tableWidget.setSortingEnabled(False)
  56.  
  57.         #Collumns'names:
  58.         item = self.tableWidget.horizontalHeaderItem(0)
  59.         item.setText(self.tr("MainWindow", "Α.Μ.", None, QtGui.QApplication.UnicodeUTF8))
  60.         item = self.tableWidget.horizontalHeaderItem(1)
  61.         item.setText(self.tr("MainWindow", "Ημερομηνία Δικάσιμου", None, QtGui.QApplication.UnicodeUTF8))
  62.         item = self.tableWidget.horizontalHeaderItem(2)
  63.         item.setText(self.tr("MainWindow", "Αριθμός Πινακίου", None, QtGui.QApplication.UnicodeUTF8))
  64.         item = self.tableWidget.horizontalHeaderItem(3)
  65.         item.setText(self.tr("MainWindow", "Ονοματεπώνυμο", None, QtGui.QApplication.UnicodeUTF8))
  66.         item = self.tableWidget.horizontalHeaderItem(4)
  67.         item.setText(self.tr("MainWindow", "Όνομα Πατρός", None, QtGui.QApplication.UnicodeUTF8))
  68.         item = self.tableWidget.horizontalHeaderItem(5)
  69.         item.setText(self.tr("MainWindow", "Όνομα Μητρός", None, QtGui.QApplication.UnicodeUTF8))
  70.         item = self.tableWidget.horizontalHeaderItem(6)
  71.         item.setText(self.tr("MainWindow", "Ημερομηνία Γέννησης", None, QtGui.QApplication.UnicodeUTF8))
  72.         item = self.tableWidget.horizontalHeaderItem(7)
  73.         item.setText(self.tr("MainWindow", "Τόπος Γέννησης", None, QtGui.QApplication.UnicodeUTF8))
  74.         item = self.tableWidget.horizontalHeaderItem(8)
  75.         item.setText(self.tr("MainWindow", "Εθνικότητα", None, QtGui.QApplication.UnicodeUTF8))
  76.         item = self.tableWidget.horizontalHeaderItem(9)
  77.         item.setText(self.tr("MainWindow", "Διεύθυνση Κατοικίας", None, QtGui.QApplication.UnicodeUTF8))
  78.         item = self.tableWidget.horizontalHeaderItem(10)
  79.         item.setText(self.tr("MainWindow", "Τηλέφωνο", None, QtGui.QApplication.UnicodeUTF8))
  80.         item = self.tableWidget.horizontalHeaderItem(11)
  81.         item.setText(self.tr("MainWindow", "Επάγγελμα-Ιδιότητα", None, QtGui.QApplication.UnicodeUTF8))
  82.         item = self.tableWidget.horizontalHeaderItem(12)
  83.         item.setText(self.tr("MainWindow", "Πράξη", None, QtGui.QApplication.UnicodeUTF8))
  84.         item = self.tableWidget.horizontalHeaderItem(13)
  85.         item.setText(self.tr("MainWindow", "Ημερομηνία Τέλεσης", None, QtGui.QApplication.UnicodeUTF8))
  86.         item = self.tableWidget.horizontalHeaderItem(14)
  87.         item.setText(self.tr("MainWindow", "Τόπος Τέλεσης", None, QtGui.QApplication.UnicodeUTF8))
  88.  
  89.         #menubar:
  90.         self.menubar = QtGui.QMenuBar(self)
  91.         self.menubar.setEnabled(True)
  92.         self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
  93.         self.menuFile = self.menubar.addMenu("&File")
  94.         self.menuFile.setSizeIncrement(QtCore.QSize(0, 0))
  95.         self.menuEdit = self.menubar.addMenu("&Edit")
  96.         self.setMenuBar(self.menubar)
  97.         self.menuFile.setTitle(self.tr("MainWindow", "File", None, QtGui.QApplication.UnicodeUTF8))
  98.         self.menuEdit.setTitle(self.tr("MainWindow", "Edit", None, QtGui.QApplication.UnicodeUTF8))
  99.  
  100.         #Creating Action filenewchild:
  101.         self.filenewchild = self.createAction("&Εισαγωγή Ανηλίκου", self.doupdate, "Ctrl+C", "" ,"Εισαγωγή Δεδομέων Ανηλίκου")
  102.         self.menuFile.addAction(self.filenewchild)
  103.         self.filenewchild.setText(self.tr("MainWindow", "Εισαγωγή Ανηλίκου", None, QtGui.QApplication.UnicodeUTF8))
  104.  
  105.         # initialize the data structure
  106.         self.children = dict(
  107.             am = [],
  108.             courtdate = [],
  109.             board_number = [],
  110.             namesurname = [],
  111.             fathersname = [],
  112.             mothersname = [],
  113.             birthday = [],
  114.             placeofbirth = [],
  115.             nationality = [],
  116.             address = [],
  117.             tel = [],
  118.             job = [],
  119.             praxis = [],
  120.             dayofdoing = [],
  121.             placeofdoing = [],
  122.         )
  123.  
  124.         #Adding objects from lists to the Table Widget:
  125.         self.updatetable()
  126.  
  127.  
  128.     # Growing rows while scrolling down....
  129.     def scrollbarChanged(self, val):
  130.         bar = self.vBar
  131.         minVal, maxVal = bar.minimum(), bar.maximum()
  132.         avg = (minVal+maxVal)/2
  133.         rowCount = self.tableWidget.rowCount()
  134.         if val > self._vBar_lastVal and val >= avg:
  135.             self.tableWidget.insertRow(rowCount)
  136.         elif val < self._vBar_lastVal:
  137.             lastRow = rowCount-30
  138.             empty = True
  139.             for col in xrange(self.tableWidget.columnCount()):
  140.                 item = self.tableWidget.item(lastRow, col)
  141.                 if item and item.text():
  142.                     empty=False
  143.                     break
  144.             if empty:
  145.                 self.tableWidget.removeRow(lastRow)
  146.         self._vBar_lastVal = val    
  147.  
  148.  
  149.     #A function to help us create Actions faster:
  150.     def createAction(self,text, slot=None, shortcut=None, icon=None,
  151.                      tip=None, checkable=False, signal="triggered()"):
  152.         action = QAction(text, self)
  153.         if icon is not None:
  154.             action.setIcon(QIcon(":/%s.png" % icon))
  155.         if shortcut is not None:
  156.             action.setShortcut(shortcut)
  157.         if tip is not None:
  158.             action.setToolTip(tip)
  159.         if slot is not None:
  160.             self.connect(action, SIGNAL(signal), slot)
  161.         if checkable:
  162.             action.setCheckable(True)
  163.         return action    
  164.  
  165.  
  166.     #A function that updates the table:
  167.     def updatetable(self):
  168.         self.tableWidget.clear()
  169.         for col, col_name in enumerate(self.COLUMNS):
  170.             col_object = self.children[col_name]
  171.             for row, value in enumerate(col_object):
  172.                 newItem = QtGui.QTableWidgetItem(value)
  173.                 self.tableWidget.setItem(row, col, newItem)
  174.  
  175.     #Handles NewChildDlg actions:
  176.     def doupdate(self):
  177.         d = NewChildDlg(self)
  178.         if d.exec_():
  179.             text = d.lineEdit.text()
  180.             self.am.append(text)
  181.             self.updatetable()
  182.  
  183. #Main loop:
  184. if __name__ == '__main__':
  185.     app = QApplication(sys.argv)
  186.     myapp = MainWindow()
  187.     myapp.show()
  188.     sys.exit(app.exec_())
Add Comment
Please, Sign In to add comment