#!/usr/bin/env python
# -*- coding: utf-8 -*-
import traceback
import wx
import wx.grid as gridlib
from library import i18n
from library.constants import *
from library.db.fax_db_archive import ArchiveDb
from opt_db import pnl_file,pnl_net,pnl_common
from opt_db.grid_fields import CustTableGrid
_ = i18n.i18n()
class NullPanel(pnl_common.CommonPanel):
""" A null panel
"""
class panel(wx.Panel):
"""
"""
def __init__(self, parent, val_pass):
"""
"""
self.paths, self.ren, self.opt_cls = val_pass
super(panel, self).__init__(parent, style=wx.TAB_TRAVERSAL)
self._choices_dict = {-1: "", 0:_("fac_lbl_pg_db"), 1:_("fac_lbl_mysql_db"), 2:_("fac_lbl_local_db")}
self._main_pnl = self
self._d_db_panels = {}
self._create_skeleton(self)
self._db = None
def _create_skeleton(self, parent):
""" Create the skeleton for the panel, without the internal (db) panel
"""
self._cb_use_fax_arch = wx.CheckBox(self._main_pnl, -1, _("fac_cb_use_archive"))
self._cb_use_fax_arch.SetValue(False)
self._cb_use_fax_arch.Bind(wx.EVT_CHECKBOX, self._onCheckBox)
# ctrls for db selection
lbl_db_type = wx.StaticText(self._main_pnl, -1, _("fac_lbl_db_type"))
#choices_list = [self._choices_dict[0], self._choices_dict[1],
# self._choices_dict[2]]
self._db_cbox = wx.ComboBox(self._main_pnl, -1, choices=[self._choices_dict[x] for x in sorted(self._choices_dict)])
self.Bind(wx.EVT_COMBOBOX, self._on_evt_text)
sz_db_cb = wx.BoxSizer(wx.HORIZONTAL)
sz_db_cb.Add(lbl_db_type, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTRE_VERTICAL | wx.ALL, 5)
sz_db_cb.Add(self._db_cbox, 0, wx.ALIGN_LEFT | wx.EXPAND | wx.ALL, 5)
# configuration sizer
self._sz_conf = wx.StaticBoxSizer(wx.StaticBox(self._main_pnl, -1,
_("fac_scbox_lbl_config")), wx.VERTICAL)
#Create a null panel
pnl = NullPanel(parent)
self._current_db_type_selected = DB_TYPE_NULL
self._d_db_panels[DB_TYPE_NULL] = pnl
# grid section
self._grid = CustTableGrid(self._main_pnl)
#gr_attr = gridlib.GridCellAttr()
#gr_attr.SetReadOnly(True)
#self._grid.SetColAttr(0, gr_attr)
#self._grid.SetRowAttr(0, gr_attr)
#self._grid.SetAttr(0, 3, gr_attr)
#self._grid.SetAttr(1, 3, gr_attr)
sz_buttons = self._create_buttons_sizer(self._main_pnl)
sz_grid = wx.BoxSizer(wx.HORIZONTAL)
sz_grid.Add(self._grid, 0, wx.ALIGN_CENTER | wx.EXPAND | wx.ALL)
sz_grid.Add(sz_buttons, 0, wx.ALIGN_CENTER | wx.EXPAND | wx.ALL)
self._sz_conf_flg = wx.ALIGN_LEFT | wx.EXPAND | wx.ALL
self._sz_conf.Add(sz_db_cb, 0, self._sz_conf_flg, 5)
self._sz_conf.Add(pnl, 0, self._sz_conf_flg, 5)
self._sz_conf.Add(sz_grid , 1, self._sz_conf_flg, 5)
# the main sizer
sz_main = wx.BoxSizer(wx.VERTICAL)
flg_sz_main = wx.ALIGN_CENTER | wx.EXPAND | wx.ALL
sz_main.Add(self._cb_use_fax_arch, 0, flg_sz_main, 10)
sz_main.Add(self._sz_conf, 1, flg_sz_main, 5)
#sz_main.Add(btn_save, 0, wx.ALIGN_CENTER | wx.ALL, 5)
# default item selecte: none
self._db_cbox.Select(0)
self._main_pnl.SetSizerAndFit(sz_main)
#self.SetClientSize(self._main_pnl.GetSize())
self._enable_all_ctrls(False, self._sz_conf)
def _create_buttons_sizer(self, parent):
""" creates buttons for get data, insert and delete rows in grid
"""
# ctrls creation
btn_get_data = wx.Button(parent, -1, _("fac_btn_get_data"))
btn_up_row = wx.Button(parent, -1, _("fac_btn_up_row"))
btn_down_row = wx.Button(parent, -1, _("fac_btn_down_row"))
btn_new_row = wx.Button(parent, -1, _("fac_btn_new_row"))
btn_del_row = wx.Button(parent, -1, _("fac_btn_del_row"))
btn_get_data.Bind(wx.EVT_BUTTON, self._onGetDataBtn)
btn_up_row.Bind(wx.EVT_BUTTON, self._onUpRowBtn)
btn_down_row.Bind(wx.EVT_BUTTON, self._onDownRowBtn)
btn_new_row.Bind(wx.EVT_BUTTON, self._onNewRowBtn)
btn_del_row.Bind(wx.EVT_BUTTON, self._onDelRowBtn)
# ctrls layout
sz_buttons = wx.BoxSizer(wx.VERTICAL)
sz_buttons.Add(btn_get_data, 0, wx.ALIGN_CENTER | wx.EXPAND | wx.ALL, 5)
sz_buttons.Add(btn_up_row, 0, wx.ALIGN_CENTER | wx.EXPAND | wx.ALL, 5)
sz_buttons.Add(btn_down_row, 0, wx.ALIGN_CENTER | wx.EXPAND | wx.ALL, 5)
sz_buttons.Add(btn_new_row, 0, wx.ALIGN_CENTER | wx.EXPAND | wx.ALL, 5)
sz_buttons.Add(btn_del_row, 0, wx.ALIGN_CENTER | wx.EXPAND | wx.ALL, 5)
return sz_buttons
def _substitute_sizer(self, db_type):
""" substitutes the current sizer with new_sz
"""
if not db_type in self._d_db_panels:
if db_type in DB_TYPE_L_NET:
pnl = pnl_net.panel(self._main_pnl)
elif db_type == DB_TYPE_FILE:
pnl = pnl_file.panel(self._main_pnl)
else:
raise ValueError("Why here? %s", db_type)
else:
pnl = self._d_db_panels[db_type]
pnl.Show()
self._d_db_panels[db_type] = pnl
# removing current sizer
old_pn = self._d_db_panels[self._current_db_type_selected]
self._sz_conf.Remove(old_pn)
old_pn.Hide()
# inserting new sizer
self._sz_conf.Insert(1, pnl, 0, self._sz_conf_flg, 5)
self._current_db_type_selected = db_type
# laying out all
self._sz_conf.Layout()
self._main_pnl.Layout()
def _on_evt_text(self, evt):
""" called when an item has been selected in the combo box
"""
evt_id = evt.GetInt()
if (evt_id in DB_TYPE_L_NET and self._current_db_type_selected != DB_TYPE_NET):
# net db selected
self._substitute_sizer(evt_id)
elif (evt_id == DB_TYPE_FILE and
self._current_db_type_selected != DB_TYPE_FILE):
# file db selected
self._substitute_sizer(DB_TYPE_FILE)
def _enable_all_ctrls(self, val, sizer):
"""
"""
for child in sizer.GetChildren():
sz = child.GetSizer()
if sz is not None:
self._enable_all_ctrls(val, sz)
else:
wind = child.GetWindow()
if wind is not None:
wind.Enable(val)
def _onGetDataBtn(self, evt):
""" Retrieve data
"""
if self._current_db_type_selected == DB_TYPE_NULL:
#no connect on no selection
return
if not self._db:
try:
self._db = ArchiveDb(self._d_db_panels[self._current_db_type_selected].get_parameters(),
self._current_db_type_selected)
except:
msg = traceback.format_exc()
print msg
wx.MessageBox(msg, _("error"), wx.ICON_ERROR)
return
ret = self._db.get_fields()
if not ret:
return
self._grid.clear()
#data_field =
gr_data = [ (x, x, 120, 1) for x in ret[:2] ]
self._grid.appendRowsData( gr_data + [ (x, x, 100, 0) for x in ret[2:] ] )
def _onUpRowBtn(self, evt):
"""
"""
self._grid.moveRowUp()
def _onDownRowBtn(self, evt):
"""
"""
self._grid.moveRowDown()
def _onNewRowBtn(self, evt):
""" Inserts a row int the grid
"""
self._grid.AppendRows(1)
def _onDelRowBtn(self, evt):
""" Removes a row from the grid
"""
self._grid.DeleteRows(self._grid.GetGridCursorRow())
def _onCheckBox(self, evt):
""" enables or disables all ctrls
"""
self._enable_all_ctrls(evt.IsChecked(), self._sz_conf)
def ctrlWidgetsStatus(self):
""" I'll load the default db, if any
"""
def get_parameters(self):
"""
"""
#usage and combo selection
pars = [self._cb_use_fax_arch.GetValue(), self._db_cbox.GetSelection()]
#if usage, fetch values from the right type
if self._cb_use_fax_arch.GetValue():
pnl = self._d_db_panels[self._current_db_type_selected]
vals = pnl.get_parameters()
#the grid values
gr_rows = []
for row in self._grid.getValues():
gr_rows.append( ",".join(row) )
gr_vals = ";".join(gr_rows)
else:
vals = "" ; gr_vals = ""
return pars + [vals, gr_vals]
def set_parameters(self, pars):
"""
"""
db_usage, db_type, db_vals, gr_vals = pars
if db_usage:
self._cb_use_fax_arch.SetValue(True)
self._db_cbox.SetSelection(db_type)
self._substitute_sizer(db_type)
pnl = self._d_db_panels[self._current_db_type_selected]
try:
pnl.set_parameters(db_vals)
except:
print "problem on load"(pars, pnl)
raise
#the grid values
gr_data = []
for row_data in gr_vals.split(";"):
t = []
for num, v in enumerate( row_data.split(",") ) :
#set the last column (check) to the right value
if num == 3:
v = int(v)
t.append(v)
gr_data.append(t)
if not gr_data[0][0]:
gr_data = []
self._grid.clear()
self._grid.appendRowsData(gr_data)
self._enable_all_ctrls(True, self._sz_conf)
|