#! /usr/bin/env python # Copyright (c) 2011, Walter Vargas # Copyright (c) 2011, Joel Davila #This program is free software: you can redistribute it and/or modify #it under the terms of the GNU General Public License as published by #the Free Software Foundation, either version 3 of the License, or #(at your option) any later version. # #This program is distributed in the hope that it will be useful, #but WITHOUT ANY WARRANTY; without even the implied warranty of #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #GNU General Public License for more details. # #You should have received a copy of the GNU General Public License #along with this program. If not, see . from gettext import gettext as _ import gtk import pygtk import os.path import sqlite3 import sql_class as conn_db class Contactos(object): '''Clase Principal''' def __init__(self): '''Ventana Principal''' ################### Ventana e Interface #################### self.window = gtk.Window() self.window.connect("delete-event", self._delete_event) self.window.set_title(_('CONTACTOS')) self.window.set_size_request(1200, 800) ################### Caja de Texto #################### self.display_now = [] self.pulse = 0 self.fname = "" self.lname = "" self.id = "" self.lista_name = [] ################## Ventana con scroll ################## self.scrolled_window = gtk.ScrolledWindow() self.scrolled_window.set_border_width(10) self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS) ################## Contenedor Principal ################## self.container = gtk.HBox() self.h2 = gtk.HBox() ###### Vertical 1 ###### self.vb1 = gtk.VBox() ###### Aca van la raiz y los datos formales. ###### creamos el treestore con la columna tipo string como modelo self.treestore = gtk.TreeStore(str) db = conn_db.Conexion() self.lista = [] self.categorias = ['familiares', 'amigos', 'companeros', 'otros'] for cat in self.categorias: citer = self.treestore.append(None, ['%s' % cat]) lista = db._display_on_tree(cat) for i in lista: self.lista.append(i) for i in range(len(lista)): self.treestore.append(citer, ['%s' % lista[i][0] + " " + lista[i][1]]) #for i in lista: # self.lista.append(i) #for i in range(len(lista)): # self.treestore.append(citer, ['%s' % lista[i]]) ###### Creamos el treeview, contenedor de treeviewcolumn y cellrender ###### encargado de mostrar la vista del treestore self.treeview = gtk.TreeView(self.treestore) self.treeview.connect("row-activated", self._clicar) ###### Creamos el treeviewcolumn que mostrara el dato self.tv_column = gtk.TreeViewColumn('Categoria') ###### Agregamos al treeview la columna self.treeview.append_column(self.tv_column) ###### Vamos a crear un objeto para renderizar los datos. self.cell = gtk.CellRendererText() ###### Add the cell al tv_column self.tv_column.pack_start(self.cell, True) self.tv_column.add_attribute(self.cell, 'text', 0) self.treeview.set_search_column(0) self.tv_column.set_sort_column_id(0) self.treeview.set_reorderable(True) self.scrolled_window.add_with_viewport(self.treeview) self.vb1.pack_start(self.scrolled_window, True,True, 0) self.h2.pack_start(self.vb1, True, True, 0) self.scrolled_window.show() self.treeview.show() self.container.pack_start(self.h2, True, True, 0) #--------------CENTER OPTIONS CONTAINER-------------# self.opcionbox = gtk.VBox() self.opcionlabel = gtk.Label(" Opciones ") self.add = gtk.Button("Nuevo Contacto") self.add.connect("clicked", self._add_user) self.update = gtk.Button("Editar Contacto") self.update.connect("clicked", self._update_user) self.delete = gtk.Button("Eliminar Contacto") self.delete.connect("clicked", self._del_user) self.opcionbox.pack_start(self.opcionlabel, False, False, 20) self.opcionbox.pack_start(self.add, False, False, 40) self.opcionbox.pack_start(self.update, False, False, 40) self.opcionbox.pack_start(self.delete, False, False, 40) self.container.pack_start(self.opcionbox, False, False, 0) #---------------RIGHT CONTAINER-------------# self.c_right = gtk.VBox() self.contacto = gtk.Label(" CONTACTO ") self.c_right.pack_start(self.contacto, False, False, 20) self.hr1 = gtk.HBox() self.vr_2 = gtk.VBox() self.containerl1 = gtk.HBox() self.lf_name = gtk.Label("Nombres") self.containerl1.pack_start(self.lf_name, False, False, 10) self.ef_name = gtk.Entry() self.ef_name.set_size_request(300, 30) self.containerl1.pack_start(self.ef_name, False, False, 10) self.vr_2.pack_start(self.containerl1, False, False, 30) self.containerl2 = gtk.HBox() self.ll_name = gtk.Label("Apellidos ") self.containerl2.pack_start(self.ll_name, False, False, 10) self.el_name = gtk.Entry() self.el_name.set_size_request(300, 30) self.containerl2.pack_start(self.el_name, False, False, 10) self.vr_2.pack_start(self.containerl2, False, False, 30) self.hr1.pack_start(self.vr_2, False, False, 20) self.pic = gtk.Image() #self.pic.set_from_file("./scrimmage1.jpg") self.hr1.pack_start(self.pic, False, False, 0) self.c_right.pack_start(self.hr1, False, False, 10) self.hr2 = gtk.HBox() self.l_address = gtk.Label(" Direccion") self.e_address = gtk.Entry() self.e_address.set_size_request(400, 30) self.hr2.pack_start(self.l_address, False, False, 20) self.hr2.pack_start(self.e_address, False, False, 30) self.c_right.pack_start(self.hr2, False, False, 30) self.hr3 = gtk.HBox() self.l_phone = gtk.Label(" Telefono ") self.e_phone = gtk.Entry() self.e_phone.set_size_request(80, 30) self.hr3.pack_start(self.l_phone, False, False, 20) self.hr3.pack_start(self.e_phone, False, False, 20) self.c_right.pack_start(self.hr3, False, False, 30) self.hr2 = gtk.HBox() self.l_ocupation = gtk.Label(" Ocupacion") self.e_ocupation = gtk.Entry() self.e_ocupation.set_size_request(200, 30) self.hr2.pack_start(self.l_ocupation, False, False, 20) self.hr2.pack_start(self.e_ocupation, False, False, 20) self.c_right.pack_start(self.hr2, False, False, 30) self.hr2 = gtk.HBox() self.l_email = gtk.Label("Email ") self.e_email = gtk.Entry() self.e_email.set_size_request(200, 30) self.hr2.pack_start(self.l_email, False, False, 25) self.hr2.pack_start(self.e_email, False, False, 25) self.l_email = gtk.Label("por Ej: midireccion@gmail.com") self.hr2.pack_start(self.l_email, False, False, 20) self.c_right.pack_start(self.hr2, False, False, 20) self.hr2 = gtk.HBox() self.l_category = gtk.Label("Categoria ") self.e_category = gtk.Entry() self.e_category.set_size_request(200, 30) self.hr2.pack_start(self.l_category, False, False, 20) self.hr2.pack_start(self.e_category, False, False, 20) self.c_right.pack_start(self.hr2, False, False, 20) ##-----------confirmation--------------------# self.confirmation = gtk.HBox() self.label_dialog = gtk.Label() #self.label_dialog.set_text('HLA') self.aceptar = gtk.Button("Guardar Cambios") self.aceptar.connect("clicked", self._aceptar) self.aceptar.set_sensitive(False) self.aceptar.hide() self.cancelar = gtk.Button("Cancelar") self.cancelar.connect("clicked", self._cancelar) self.cancelar.set_sensitive(False) self.cancelar.hide() self.confirmation.pack_start(self.label_dialog, False, False, 0) self.confirmation.pack_start(self.aceptar, False, False, 0) self.confirmation.pack_start(self.cancelar, False, False, 0) self.c_right.pack_start(self.confirmation, False, False, 0) self.container.pack_start(self.c_right, False, False, 0) #-----cargar contacto default-----------# self._load_contact(1) #----------------------- displaying all--------------------------------# self.window.add(self.container) self.window.show_all() #--------------finaliza __INIT__----------------------------------------# #---- metodo que presentara el elemento clickeado en la lista de arbol ----# def _clicar(self, tvcolumn, path, iter): opcion = self.treestore[path][0] if opcion in self.lista: # No tendria porque ir self.lista_name = opcion.split(" ") print opcion[0] self._load_contact(metodo=0) #pass def _load_contact(self, metodo=1): """Conect to sql bd file and return a lista""" lista=[] if metodo: db = conn_db.Conexion(1) lista = db._show_default(1) else: #pass db = conn_db.Conexion(self.lista_name[0], self.lista_name[1]) lista = db._show_default(0) self.display_now = lista self._load_on_entry(self.display_now) def _load_on_entry(self, lista): """load on entry values to display""" self._editable_entry(False) self.ef_name.set_text(lista[0]) self.el_name.set_text(lista[1]) self.e_address.set_text(lista[2]) self.e_phone.set_text(lista[3]) self.e_ocupation.set_text(lista[4]) self.e_email.set_text(lista[5]) self.e_category.set_text(lista[6]) self.pic.set_from_file('./images/show.jpg') def _sensitive_options(self, i): self.add.set_sensitive(i) self.update.set_sensitive(i) self.delete.set_sensitive(i) self.opcionlabel.set_sensitive(i) self.scrolled_window.set_sensitive(i) def _sensitive_confirmation(self, i): self.aceptar.set_sensitive(i) self.cancelar.set_sensitive(i) def _read_datas(self): f_name = self.ef_name.get_text().lower() l_name = self.el_name.get_text().lower() e_address = self.e_address.get_text().lower() e_phone = self.e_phone.get_text().lower() e_ocupation = self.e_ocupation.get_text().lower() e_email = self.e_email.get_text().lower() e_category = self.e_category.get_text().lower() if self.pulse==1: db = conn_db.Conexion(f_name=f_name, l_name=l_name, address=e_address, phone=e_phone, ocupation=e_ocupation, email=e_email, category=e_category) db._add_user() self._hiddeshow(0) self._sensitive_options(1) self._load_contact(1) self._sensitive_confirmation(0) self.label_dialog.set_text("register saved succefull") self.pulse = 0 elif self.pulse == 2: #pass db = conn_db.Conexion(self.id, f_name, l_name, e_address, e_phone, e_ocupation, e_email, e_category) db._update_user() self._hiddeshow(0) self._sensitive_options(1) self._load_contact(1) self._sensitive_confirmation(0) self.label_dialog.set_text("register update succefull") self.pulse = 0 else: #pass """aqui paso nombre y apellido para extraer el id""" db = conn_db.Conexion(self.id) db._del_user() self._hiddeshow(0) self._sensitive_options(1) self._load_contact(1) self._sensitive_confirmation(0) self.label_dialog.set_text("delete succefull") self.pulse = 0 def _aceptar(self, widget, data=None): if self.ef_name.get_text()!= "" and self.el_name.get_text()!="" and self.e_category.get_text()!="": f = self.ef_name.get_text().lower() l = self.el_name.get_text().lower() c = self.e_category.get_text().lower() name = f + " " + l db = conn_db.Conexion() if self.pulse == 1: if db._exists_user(name) or (c not in self.categorias): self.label_dialog.set_text("dont save contact on self-name or distinct cat") else: self._read_datas() elif self.pulse==2: #pass if (c not in self.categorias): self.label_dialog.set_text("dont save contact on distinct category") else: self.id = db._return_id(self.fname, self.lname) self._read_datas() else: #pass self.id = db._return_id(self.fname, self.lname) self._read_datas() def _hiddeshow(self, i): if i: self.add.hide() self.update.hide() self.delete.hide() self.aceptar.show() self.cancelar.show() else: self.add.show() self.update.show() self.delete.show() self.aceptar.hide() self.cancelar.hide() def _cancelar(self, widget, data=None): self.pulse = 0 self._sensitive_options(1) self._load_on_entry(self.display_now) self._sensitive_confirmation(0) self._hiddeshow(0) self.label_dialog.set_text("") def _add_user(self, widget, data=None): self._clear_entry() self.pic.set_from_file('./noavailable.png') self.pulse = 1 self._sensitive_options(0) self._sensitive_confirmation(1) self._hiddeshow(1) self._editable_entry(1) def _update_user(self, widget, data=None): self.pulse = 2 self.fname = self.ef_name.get_text().lower() self.lname = self.el_name.get_text().lower() self._editable_entry(True) self._sensitive_options(0) self._sensitive_confirmation(1) self._hiddeshow(1) def _del_user(self, widget, data=None): #pass self.pulse = 3 self.fname = self.ef_name.get_text().lower() self.lname = self.el_name.get_text().lower() self._sensitive_options(0) self._sensitive_confirmation(1) self._hiddeshow(1) def _clear_entry(self): self.ef_name.set_text("") self.el_name.set_text("") self.e_address.set_text("") self.e_phone.set_text("") self.e_ocupation.set_text("") self.e_email.set_text("") self.e_category.set_text("") def _editable_entry(self, valor): if valor: self.ef_name.set_editable(True) self.el_name.set_editable(True) self.e_address.set_editable(True) self.e_phone.set_editable(True) self.e_ocupation.set_editable(True) self.e_email.set_editable(True) self.e_category.set_editable(True) else: self.ef_name.set_editable(False) self.el_name.set_editable(False) self.e_address.set_editable(False) self.e_phone.set_editable(False) self.e_ocupation.set_editable(False) self.e_email.set_editable(False) self.e_category.set_editable(False) def _delete_event(self, widget, event, data=None): gtk.main_quit() return False def main(self): gtk.main() return 0 if __name__=="__main__": c = Contactos() c.main()