From ae18b7e2c8c8e591d214b69c92eb0e08c65e027d Mon Sep 17 00:00:00 2001 From: Ted Lazaros Date: Mon, 12 Feb 2018 21:09:31 +0200 Subject: [PATCH] minor changes --- sofos/database.py | 4 ++ sofos/models.py | 8 ++-- sofos/qt.py | 88 +++++++++++++++++++++++++++------------ sofos/templates/forms.py | 17 ++++++++ sofos/templates/main.py | 19 ++++++++- sofos/templates/models.py | 1 + 6 files changed, 105 insertions(+), 32 deletions(-) create mode 100644 sofos/templates/forms.py diff --git a/sofos/database.py b/sofos/database.py index cf13a85..768a57e 100644 --- a/sofos/database.py +++ b/sofos/database.py @@ -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(): diff --git a/sofos/models.py b/sofos/models.py index cf49967..e033eb3 100644 --- a/sofos/models.py +++ b/sofos/models.py @@ -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): @@ -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 ['ΑΑ'] @@ -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)""" diff --git a/sofos/qt.py b/sofos/qt.py index ff0ba4b..9db6924 100644 --- a/sofos/qt.py +++ b/sofos/qt.py @@ -455,34 +455,42 @@ 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() @@ -490,18 +498,25 @@ def _create_fields(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: @@ -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) @@ -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] @@ -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 diff --git a/sofos/templates/forms.py b/sofos/templates/forms.py new file mode 100644 index 0000000..9f3de89 --- /dev/null +++ b/sofos/templates/forms.py @@ -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]) diff --git a/sofos/templates/main.py b/sofos/templates/main.py index a065eca..e03f4f1 100644 --- a/sofos/templates/main.py +++ b/sofos/templates/main.py @@ -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') @@ -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(): @@ -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") @@ -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") diff --git a/sofos/templates/models.py b/sofos/templates/models.py index e1477f0..2d7a359 100644 --- a/sofos/templates/models.py +++ b/sofos/templates/models.py @@ -103,6 +103,7 @@ class Eidikotita(models.Model): class Meta: table_label = "Ειδικότητα εργασίας" + repr_fields = ['eip'] class ApasxolisiType(models.Model):