Moved all of the add/edit/delete record code from pyqrz.py to logbook.py.

pull/17/head
Christian Jacobs 2013-03-25 19:43:10 +00:00
rodzic f640aa2ead
commit 88f2a2edea
4 zmienionych plików z 125 dodań i 138 usunięć

Wyświetl plik

@ -21,8 +21,9 @@
from gi.repository import Gtk, GObject
import logging
from record import *
from adif import *
from record import *
from record_dialog import *
class Logbook(Gtk.ListStore):
@ -106,32 +107,127 @@ class Logbook(Gtk.ListStore):
return
def add_record(self, fields_and_data):
# Adds a record to the end of the logbook
# using data from the fields_and_data dictionary.
def add_record_callback(self, widget, parent):
logbook_entry = [len(self.records)] # Add the next available record index
field_names = self.SELECTED_FIELD_NAMES_ORDERED
for i in range(0, len(field_names)):
logbook_entry.append(fields_and_data[field_names[i]])
self.append(logbook_entry)
dialog = RecordDialog(parent, index=None)
all_valid = False # Are all the field entries valid?
record = Record(fields_and_data)
self.records.append(record)
while(not all_valid):
# This while loop gives the user infinite attempts at giving valid data.
# The add/edit record window will stay open until the user gives valid data,
# or until the Cancel button is clicked.
all_valid = True
response = dialog.run() #FIXME: Is it ok to call .run() multiple times on the same RecordDialog object?
if(response == Gtk.ResponseType.OK):
fields_and_data = {}
field_names = self.SELECTED_FIELD_NAMES_ORDERED
for i in range(0, len(field_names)):
#TODO: Validate user input!
fields_and_data[field_names[i]] = dialog.get_data(field_names[i])
if(not(dialog.is_valid(field_names[i], fields_and_data[field_names[i]]))):
# Data is not valid - inform the user.
message = Gtk.MessageDialog(parent, Gtk.DialogFlags.DESTROY_WITH_PARENT,
Gtk.MessageType.ERROR, Gtk.ButtonsType.OK,
"The data in field \"%s\" is not valid!" % field_names[i])
message.run()
message.destroy()
all_valid = False
break # Don't check the other data until the user has fixed the current one.
# Hopefully this won't change anything as check_consistency
# is also called in delete_record, but let's keep it
# here as a sanity check.
self.check_consistency()
if(all_valid):
# All data has been validated, so we can go ahead and add the new record.
logbook_entry = [len(self.records)] # Add the next available record index
field_names = self.SELECTED_FIELD_NAMES_ORDERED
for i in range(0, len(field_names)):
logbook_entry.append(fields_and_data[field_names[i]])
self.append(logbook_entry)
record = Record(fields_and_data)
self.records.append(record)
# Hopefully this won't change anything as check_consistency
# is also called in delete_record, but let's keep it
# here as a sanity check.
self.check_consistency()
# Select the new Record's row in the treeview.
parent.treeselection.select_path(self.get_number_of_records()-1)
dialog.destroy()
return
def delete_record(self, iter, index):
# Deletes the record with index 'index' from self.records.
# 'iter' is needed to remove the record from the ListStore itself.
self.records.pop(index)
self.remove(iter)
self.check_consistency()
def delete_record_callback(self, widget, parent):
# Get the selected row in the logbook
(model, path) = parent.treeselection.get_selected_rows()
try:
iter = model.get_iter(path[0])
index = model.get_value(iter,0)
except IndexError:
logging.debug("Trying to delete a record, but there are no records in the logbook!")
return
dialog = Gtk.MessageDialog(parent, Gtk.DialogFlags.DESTROY_WITH_PARENT,
Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO,
"Are you sure you want to delete record %d?" % index)
response = dialog.run()
if(response == Gtk.ResponseType.YES):
# Deletes the record with index 'index' from self.records.
# 'iter' is needed to remove the record from the ListStore itself.
self.records.pop(index)
self.remove(iter)
self.check_consistency()
dialog.destroy()
return
def edit_record_callback(self, widget, path, view_column, parent):
# Note: the path and view_column arguments need to be passed in
# since they associated with the row-activated signal.
# Get the selected row in the logbook
(model, path) = parent.treeselection.get_selected_rows()
try:
iter = model.get_iter(path[0])
row_index = model.get_value(iter,0)
except IndexError:
logging.debug("Could not find the selected row's index!")
return
dialog = RecordDialog(parent, index=row_index)
all_valid = False # Are all the field entries valid?
while(not all_valid):
# This while loop gives the user infinite attempts at giving valid data.
# The add/edit record window will stay open until the user gives valid data,
# or until the Cancel button is clicked.
all_valid = True
response = dialog.run() #FIXME: Is it ok to call .run() multiple times on the same RecordDialog object?
if(response == Gtk.ResponseType.OK):
fields_and_data = {}
field_names = self.SELECTED_FIELD_NAMES_ORDERED
for i in range(0, len(field_names)):
#TODO: Validate user input!
fields_and_data[field_names[i]] = dialog.get_data(field_names[i])
if(not(dialog.is_valid(field_names[i], fields_and_data[field_names[i]]))):
# Data is not valid - inform the user.
message = Gtk.MessageDialog(parent, Gtk.DialogFlags.DESTROY_WITH_PARENT,
Gtk.MessageType.ERROR, Gtk.ButtonsType.OK,
"The data in field \"%s\" is not valid!" % field_names[i])
message.run()
message.destroy()
all_valid = False
break # Don't check the other data until the user has fixed the current one.
if(all_valid):
for i in range(0, len(field_names)):
# All data has been validated, so we can go ahead and update the record.
# First update the Record object...
self.records[row_index].set_data(field_names[i], fields_and_data[field_names[i]])
# ...and then the Logbook.
# (we add 1 onto the column_index here because we don't want to consider the index column)
self[row_index][i+1] = fields_and_data[field_names[i]]
dialog.destroy()
return
def get_number_of_records(self):

Wyświetl plik

@ -78,19 +78,19 @@ class Menu(Gtk.MenuBar):
mitem_log.set_submenu(subm_log)
mitem_addrecord = Gtk.MenuItem("Add Record...")
mitem_addrecord.connect("activate", parent.add_record_callback)
mitem_addrecord.connect("activate", parent.logbook.add_record_callback, parent)
key, mod = Gtk.accelerator_parse("<Control>R")
mitem_addrecord.add_accelerator("activate", agrp, key, mod, Gtk.AccelFlags.VISIBLE)
subm_log.append(mitem_addrecord)
mitem_editrecord = Gtk.MenuItem("Edit Selected Record...")
mitem_editrecord.connect("activate", parent.edit_record_callback, None, None)
mitem_editrecord.connect("activate", parent.logbook.edit_record_callback, None, None, parent)
key, mod = Gtk.accelerator_parse("<Control>E")
mitem_editrecord.add_accelerator("activate", agrp, key, mod, Gtk.AccelFlags.VISIBLE)
subm_log.append(mitem_editrecord)
mitem_deleterecord = Gtk.MenuItem("Delete Selected Record...")
mitem_deleterecord.connect("activate", parent.delete_record_callback)
mitem_deleterecord.connect("activate", parent.logbook.delete_record_callback, parent)
key, mod = Gtk.accelerator_parse("Delete")
mitem_deleterecord.add_accelerator("activate", agrp, key, mod, Gtk.AccelFlags.VISIBLE)
subm_log.append(mitem_deleterecord)
@ -116,5 +116,3 @@ class Menu(Gtk.MenuBar):
return

Wyświetl plik

@ -27,7 +27,6 @@ from adif import *
from logbook import *
from menu import *
from toolbar import *
from record_dialog import *
# The PyQSO application class
class PyQSO(Gtk.Window):
@ -55,7 +54,7 @@ class PyQSO(Gtk.Window):
# Render the logbook
self.treeview = Gtk.TreeView(self.logbook)
self.treeview.set_grid_lines(Gtk.TreeViewGridLines.BOTH)
self.treeview.connect("row-activated", self.edit_record_callback)
self.treeview.connect("row-activated", self.logbook.edit_record_callback, self)
self.treeselection = self.treeview.get_selection()
self.treeselection.set_mode(Gtk.SelectionMode.SINGLE)
# Allow the Logbook to be scrolled up/down
@ -89,112 +88,6 @@ class PyQSO(Gtk.Window):
self.show_all()
return
def add_record_callback(self, widget):
dialog = RecordDialog(self, index=None)
all_valid = False # Are all the field entries valid?
while(not all_valid):
# This while loop gives the user infinite attempts at giving valid data.
# The add/edit record window will stay open until the user gives valid data,
# or until the Cancel button is clicked.
all_valid = True
response = dialog.run() #FIXME: Is it ok to call .run() multiple times on the same RecordDialog object?
if(response == Gtk.ResponseType.OK):
fields_and_data = {}
field_names = self.logbook.SELECTED_FIELD_NAMES_ORDERED
for i in range(0, len(field_names)):
#TODO: Validate user input!
fields_and_data[field_names[i]] = dialog.get_data(field_names[i])
if(not(dialog.is_valid(field_names[i], fields_and_data[field_names[i]]))):
# Data is not valid - inform the user.
message = Gtk.MessageDialog(self, Gtk.DialogFlags.DESTROY_WITH_PARENT,
Gtk.MessageType.ERROR, Gtk.ButtonsType.OK,
"The data in field \"%s\" is not valid!" % field_names[i])
message.run()
message.destroy()
all_valid = False
break # Don't check the other data until the user has fixed the current one.
if(all_valid):
# All data has been validated, so we can go ahead and add the new record.
self.logbook.add_record(fields_and_data)
# Select the new Record's row in the treeview.
self.treeselection.select_path(self.logbook.get_number_of_records()-1)
dialog.destroy()
return
def delete_record_callback(self, widget):
# Get the selected row in the logbook
(model, path) = self.treeselection.get_selected_rows()
try:
iter = model.get_iter(path[0])
index = model.get_value(iter,0)
except IndexError:
logging.debug("Trying to delete a record, but there are no records in the logbook!")
return
dialog = Gtk.MessageDialog(self, Gtk.DialogFlags.DESTROY_WITH_PARENT,
Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO,
"Are you sure you want to delete record %d?" % index)
response = dialog.run()
if(response == Gtk.ResponseType.YES):
self.logbook.delete_record(iter, index)
dialog.destroy()
return
def edit_record_callback(self, widget, path, view_column):
# Note: the path and view_column arguments need to be passed in
# since they associated with the row-activated signal.
# Get the selected row in the logbook
(model, path) = self.treeselection.get_selected_rows()
try:
iter = model.get_iter(path[0])
row_index = model.get_value(iter,0)
except IndexError:
logging.debug("Could not find the selected row's index!")
return
dialog = RecordDialog(self, index=row_index)
all_valid = False # Are all the field entries valid?
while(not all_valid):
# This while loop gives the user infinite attempts at giving valid data.
# The add/edit record window will stay open until the user gives valid data,
# or until the Cancel button is clicked.
all_valid = True
response = dialog.run() #FIXME: Is it ok to call .run() multiple times on the same RecordDialog object?
if(response == Gtk.ResponseType.OK):
fields_and_data = {}
field_names = self.logbook.SELECTED_FIELD_NAMES_ORDERED
for i in range(0, len(field_names)):
#TODO: Validate user input!
fields_and_data[field_names[i]] = dialog.get_data(field_names[i])
if(not(dialog.is_valid(field_names[i], fields_and_data[field_names[i]]))):
# Data is not valid - inform the user.
message = Gtk.MessageDialog(self, Gtk.DialogFlags.DESTROY_WITH_PARENT,
Gtk.MessageType.ERROR, Gtk.ButtonsType.OK,
"The data in field \"%s\" is not valid!" % field_names[i])
message.run()
message.destroy()
all_valid = False
break # Don't check the other data until the user has fixed the current one.
if(all_valid):
# All data has been validated, so we can go ahead and update the record.
# First update the Record object...
self.logbook.records[row_index].set_data(field_names[i], fields_and_data[field_names[i]])
# ...and then the Logbook.
# (we add 1 onto the column_index here because we don't want to consider the index column)
self.logbook[row_index][i+1] = fields_and_data[field_names[i]]
dialog.destroy()
return
def show_about(self, widget):
about = Gtk.AboutDialog()

Wyświetl plik

@ -34,7 +34,7 @@ class Toolbar(Gtk.HBox):
button = Gtk.Button()
button.add(icon)
button.set_tooltip_text('Add record')
button.connect("clicked", parent.add_record_callback)
button.connect("clicked", parent.logbook.add_record_callback, parent)
self.pack_start(button, False, False, 0)
# Edit record
@ -43,7 +43,7 @@ class Toolbar(Gtk.HBox):
button = Gtk.Button()
button.add(icon)
button.set_tooltip_text('Edit record')
button.connect("clicked", parent.edit_record_callback, None, None)
button.connect("clicked", parent.logbook.edit_record_callback, None, None, parent)
self.pack_start(button, False, False, 0)
# Delete record
@ -52,7 +52,7 @@ class Toolbar(Gtk.HBox):
button = Gtk.Button()
button.add(icon)
button.set_tooltip_text('Delete record')
button.connect("clicked", parent.delete_record_callback)
button.connect("clicked", parent.logbook.delete_record_callback, parent)
self.pack_start(button, False, False, 0)
vbox_parent.pack_start(self, False, False, 0)