Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tedlaz committed Feb 12, 2018
1 parent 27dbca8 commit ae18b7e
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 32 deletions.
4 changes: 4 additions & 0 deletions sofos/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def __init__(self, models, dbf=None):
self.dbf = dbf if self.set_database(dbf) else None

def integrity_dict(self):
"""Integrity dictionary
:return: Dictionary {parent1: {child1: fld1, child2: fld2, ...}, ...}
"""
in_dic = {}
for table_name, table_object in self.table_objects().items():
for fld_name, fld_obj in table_object.field_objects().items():
Expand Down
8 changes: 4 additions & 4 deletions sofos/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,13 @@ def _unique_together(cls):

@classmethod
def repr_fields(cls):
"""Representation fields"""
"""Representation fields
"""
if 'Meta' in cls.__dict__.keys():
meta = getattr(cls, 'Meta')
if hasattr(meta, 'repr_fields'):
return cls.Meta.repr_fields
return None
return cls.field_names()

@classmethod
def sql_create(cls):
Expand Down Expand Up @@ -273,7 +274,7 @@ def sql_select_all_deep(cls, field_list=None, label_list=None,
"""
table_name = cls.__name__.lower()
sqt = "SELECT %s\nFROM %s\n%s"
flds = cls.repr_fields() or cls.field_names()
flds = cls.repr_fields()
fld_dic = {table_name: []}
field_list = field_list or ['%s.id' % table_name]
label_list = label_list or ['ΑΑ']
Expand Down Expand Up @@ -341,7 +342,6 @@ def delete(cls, idv):
sql = "DELETE FROM %s WHERE id='%s';" % (cls.table_name(), idv)
return df.delete(cls.__dbf__, sql)


@classmethod
def select_all(cls):
"""Select all(To be corrected)"""
Expand Down
88 changes: 62 additions & 26 deletions sofos/qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,53 +455,68 @@ class AutoForm(Qw.QDialog):
def __init__(self, model, idv=None, parent=None):
super().__init__(parent)
self.setAttribute(Qc.Qt.WA_DeleteOnClose)
self._parent = parent
self._set(model, idv)
self._wtitle()
self._create_layouts()
self._create_Buttons()
self._create_fields()
self.populate()

def _wtitle(self):
self.setWindowTitle('{}: {}'.format(
self.model.table_label(), self._id if self._id else 'New record'))

def _set(self, model, idv):
self._id = idv
self.model = model
self.setWindowTitle('{}: {}'.format(model.table_label(),
idv if idv else 'New record'))
self.widgets = {}
main_layout = Qw.QVBoxLayout()
self.setLayout(main_layout)

def _create_layouts(self):
self.main_layout = Qw.QVBoxLayout()
self.setLayout(self.main_layout)
self.fld_layout = Qw.QFormLayout()
main_layout.addLayout(self.fld_layout)
# Create buttons
buttonlayout = Qw.QHBoxLayout()
main_layout.addLayout(buttonlayout)
# Create buttons here
self.main_layout.addLayout(self.fld_layout)
self.buttonlayout = Qw.QHBoxLayout()
self.main_layout.addLayout(self.buttonlayout)

def _create_Buttons(self):
self.bcancel = Qw.QPushButton(u'Cancel', self)
self.bsave = Qw.QPushButton(u'Save', self)
# Make them loose focus
self.bcancel.setFocusPolicy(Qc.Qt.NoFocus)
self.bsave.setFocusPolicy(Qc.Qt.NoFocus)
# Add them to buttonlayout
buttonlayout.addWidget(self.bcancel)
buttonlayout.addWidget(self.bsave)
# Make connections here
self.buttonlayout.addWidget(self.bcancel)
self.buttonlayout.addWidget(self.bsave)
# Make connections
self.bcancel.clicked.connect(self.close)
self.bsave.clicked.connect(self._save)
self._create_fields() # Δημιουργία widgets
if self._id: # Γέμισμα με τιμές
self._fill()
self.bsave.clicked.connect(self.save)

def _create_fields(self):
lbs = self.model.field_labels()
self.widgets['id'] = TIntegerKey(parent=self)
self.widgets['id'].setVisible(False)
for i, fld in enumerate(self.model.field_names()):
self.widgets[fld] = wselector(self.model.field_object(fld), self)
self.fld_layout.insertRow(i, Qw.QLabel(lbs[fld]),
self.widgets[fld])
self.fld_layout.insertRow(
i, Qw.QLabel(lbs[fld]), self.widgets[fld])

def _fill(self):
def populate(self):
if not self._id:
return
self.vals = self.model.search_by_id(self._id)
for key in self.vals:
for key in self.widgets:
self.widgets[key].set(self.vals[key])

def _save(self):
@property
def get_data(self):
data = {}
for fld in self.widgets:
data[fld] = self.widgets[fld].get()
return data

def save(self):
data = self.get_data
status, lid = self.model.save(data)
if status:
if lid:
Expand Down Expand Up @@ -539,14 +554,15 @@ def __init__(self, model, parent=None):
super().__init__(parent)
# self.setAttribute(Qc.Qt.WA_DeleteOnClose)
self.resize(550, 400)
self._parent = parent
# self._dbf = dbf
self.model = model
self.setWindowTitle('{}'.format(self.model.table_label()))
self._wtitle()
self._create_gui()
self._make_connections()
self._populate()

def _wtitle(self):
self.setWindowTitle('{}'.format(self.model.table_label()))

def _make_connections(self):
self.bedit.clicked.connect(self._edit_record)
self.bnew.clicked.connect(self._new_record)
Expand Down Expand Up @@ -600,7 +616,6 @@ def _populate(self):
self.tbl.setRowCount(data['rownum'])
self.tbl.setColumnCount(data['colnum'])
self.tbl.setHorizontalHeaderLabels(data['labels'])

for i, row in enumerate(data['rows']):
for j, qt_widget in enumerate(data['qt_widgets_types']):
val = row[j]
Expand Down Expand Up @@ -686,6 +701,27 @@ def table_label(self):
return self.model.table_label()


class AutoFormTableWidget(AutoFormTable):

def _populate(self):
_, data = self.model.select_all()
self.tbl.setRowCount(data['rownum'])
self.tbl.setColumnCount(data['colnum'])
# self.tbl.setHorizontalHeaderLabels(data['labels'])
fields = self.model.field_objects()
for i, row in enumerate(data['rows']):
item = TIntegerKey(parent=self)
item.set(row[0])
self.tbl.setCellWidget(i, 0, item)
for j, col in enumerate(fields):
val = row[j+1]
item = wselector(fields[col], parent=self)
print(val)
item.set(val)
self.tbl.setCellWidget(i, j+1, item)
self.tbl.resizeColumnsToContents()


class AutoFormTableFound(AutoFormTable):
def __init__(self, model, search_string, parent=None):
self.search_string = search_string
Expand Down
17 changes: 17 additions & 0 deletions sofos/templates/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from sofos import qt


class Form1(qt.AutoForm):

def _wtitle(self):
self.setWindowTitle('Custom Form')

def _create_fields(self):
lbs = self.model.field_labels()
self.widgets['id'] = qt.TIntegerKey(parent=self)
self.widgets['id'].setVisible(False)
# for i, fld in enumerate(self.model.field_names()):
for i, fld in enumerate(['epo', 'ono', 'pat']):
self.widgets[fld] = qt.wselector(self.model.field_object(fld), self)
self.fld_layout.insertRow(
i, qt.Qw.QLabel(lbs[fld]), self.widgets[fld])
19 changes: 17 additions & 2 deletions sofos/templates/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sofos import database
from settings import setup
import models as md
# from forms import Form1
qt.CONFIRMATIONS = setup['confirmations']
BDIR = os.path.dirname(md.__file__)
INIT_DB = os.path.join(BDIR, 'init_db.sql')
Expand Down Expand Up @@ -237,6 +238,12 @@ def createActions(self):
statusTip="Show the Qt library's About box",
triggered=Qw.QApplication.instance().aboutQt)

# self.form1_action = Qw.QAction(
# "Form1",
# self,
# statusTip="Open form1",
# triggered=self.open_form1)

self.tblact = {}
self.mapper = {}
for tbl, lbl in self.database.table_labels(True).items():
Expand All @@ -247,14 +254,19 @@ def createActions(self):
self.tblact[tbl].triggered.connect(self.mapper[tbl].map)
self.mapper[tbl].mapped['QString'].connect(self.createAutoFormTbl)

# def open_form1(self):
# child = Form1(self.database.table_object('erg'), parent=self)
# self.mdiArea.addSubWindow(child)
# child.show()

def createMenus(self):
self.fileMenu = self.menuBar().addMenu("&File")
self.fileMenu.addAction(self.newAct)
self.fileMenu.addAction(self.openAct)
self.fileMenu.addSeparator()
action = self.fileMenu.addAction("Switch layout direction")
# action = self.fileMenu.addAction("Switch layout direction")
# action.triggered.connect(self.switchLayoutDirection)
self.fileMenu.addAction(self.backupAct)
action.triggered.connect(self.switchLayoutDirection)
self.fileMenu.addAction(self.exitAct)

self.windowMenu = self.menuBar().addMenu("&Window")
Expand All @@ -265,6 +277,9 @@ def createMenus(self):
for act in self.tblact:
self.tablemenu.addAction(self.tblact[act])

# self.custom_forms_menu = self.menuBar().addMenu("&Custom Forms")
# self.custom_forms_menu.addAction(self.form1_action)

self.menuBar().addSeparator()

self.helpMenu = self.menuBar().addMenu("&Help")
Expand Down
1 change: 1 addition & 0 deletions sofos/templates/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class Eidikotita(models.Model):

class Meta:
table_label = "Ειδικότητα εργασίας"
repr_fields = ['eip']


class ApasxolisiType(models.Model):
Expand Down

0 comments on commit ae18b7e

Please sign in to comment.