kopia lustrzana https://github.com/ctjacobs/pyqso
- Added "Save As" functionality.
- Check for log modification. If the log is modified, a "*" is placed after the log's name until it is saved. - On closing a log, check that it is saved. If not, ask the user if they are sure they want to close the log. - Make sure users cannot open the same log multiple times.pull/17/head
rodzic
9e0c5249c1
commit
a72fc67305
25
src/log.py
25
src/log.py
|
@ -28,7 +28,7 @@ from record_dialog import *
|
|||
class Log(Gtk.ListStore):
|
||||
''' A Log object can store multiple Record objects. '''
|
||||
|
||||
def __init__(self, records=None, name="Untitled"):
|
||||
def __init__(self, records=None, name=None, path=None):
|
||||
|
||||
# FIXME: Allow the user to select the field names. By default, let's select them all.
|
||||
self.SELECTED_FIELD_NAMES_TYPES = AVAILABLE_FIELD_NAMES_TYPES
|
||||
|
@ -49,7 +49,14 @@ class Log(Gtk.ListStore):
|
|||
# Call the constructor of the super class (Gtk.ListStore)
|
||||
Gtk.ListStore.__init__(self, *data_types)
|
||||
|
||||
self.name = name
|
||||
if(name is None):
|
||||
self.name = "Untitled*"
|
||||
self.path = None
|
||||
self.modified = True
|
||||
else:
|
||||
self.name = name
|
||||
self.path = path
|
||||
self.modified = False
|
||||
|
||||
if(records is None):
|
||||
# Begin with no records.
|
||||
|
@ -91,6 +98,7 @@ class Log(Gtk.ListStore):
|
|||
# is also called in delete_record, but let's keep it
|
||||
# here as a sanity check.
|
||||
self.check_consistency()
|
||||
self.set_modified(True)
|
||||
return
|
||||
|
||||
def delete_record(self, index, iter):
|
||||
|
@ -98,10 +106,12 @@ class Log(Gtk.ListStore):
|
|||
self.records.pop(index)
|
||||
self.remove(iter)
|
||||
self.check_consistency()
|
||||
self.set_modified(True)
|
||||
return
|
||||
|
||||
def edit_record(self, index, field_name, data):
|
||||
self.records[index].set_data(field_name, data)
|
||||
self.set_modified(True)
|
||||
return
|
||||
|
||||
def get_record(self, index):
|
||||
|
@ -110,3 +120,14 @@ class Log(Gtk.ListStore):
|
|||
def get_number_of_records(self):
|
||||
return len(self.records)
|
||||
|
||||
def set_modified(self, modified):
|
||||
if(modified and self.modified):
|
||||
return # Already modified. Nothing to do here.
|
||||
elif(modified and (not self.modified)):
|
||||
self.modified = True
|
||||
self.name = self.name + "*"
|
||||
return
|
||||
else:
|
||||
self.modified = modified
|
||||
return
|
||||
|
||||
|
|
|
@ -46,7 +46,7 @@ class Logbook(Gtk.Notebook):
|
|||
self.render_log(l)
|
||||
return
|
||||
|
||||
def open_log(self, widget=None):
|
||||
def open_log(self, widget, parent):
|
||||
dialog = Gtk.FileChooserDialog("Open File",
|
||||
None,
|
||||
Gtk.FileChooserAction.OPEN,
|
||||
|
@ -67,11 +67,20 @@ class Logbook(Gtk.Notebook):
|
|||
if(path is None):
|
||||
logging.debug("No file path specified.")
|
||||
return
|
||||
|
||||
for log in self.logs:
|
||||
if(log.path == path):
|
||||
dialog = Gtk.MessageDialog(parent, Gtk.DialogFlags.DESTROY_WITH_PARENT,
|
||||
Gtk.MessageType.ERROR, Gtk.ButtonsType.OK,
|
||||
"Log %s is already open." % path)
|
||||
response = dialog.run()
|
||||
dialog.destroy()
|
||||
return
|
||||
|
||||
adif = ADIF()
|
||||
records = adif.read(path)
|
||||
|
||||
l = Log(records, path)
|
||||
l = Log(records, path, path)
|
||||
self.logs.append(l)
|
||||
self.render_log(l)
|
||||
|
||||
|
@ -83,43 +92,77 @@ class Logbook(Gtk.Notebook):
|
|||
logging.debug("No log files to save!")
|
||||
return
|
||||
|
||||
log = self.logs[current]
|
||||
if(log.path is None):
|
||||
self.save_log_as()
|
||||
else:
|
||||
# Log is already saved somewhere.
|
||||
adif = ADIF()
|
||||
adif.write(log.records, log.path)
|
||||
if(log.modified):
|
||||
log.name = log.path
|
||||
self.set_tab_label_text(self.get_nth_page(current), log.name)
|
||||
log.set_modified(False)
|
||||
return
|
||||
|
||||
def save_log_as(self, widget=None):
|
||||
|
||||
current = self.get_current_page() # Gets the index of the selected tab in the logbook
|
||||
if(current == -1):
|
||||
logging.debug("No log files to save!")
|
||||
return
|
||||
|
||||
log = self.logs[current]
|
||||
|
||||
dialog = Gtk.FileChooserDialog("Save File",
|
||||
None,
|
||||
Gtk.FileChooserAction.SAVE,
|
||||
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
|
||||
Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
|
||||
|
||||
|
||||
response = dialog.run()
|
||||
if(response == Gtk.ResponseType.OK):
|
||||
path = dialog.get_filename()
|
||||
else:
|
||||
path = None
|
||||
dialog.destroy()
|
||||
|
||||
|
||||
if(path is None):
|
||||
logging.debug("No file path specified.")
|
||||
return
|
||||
|
||||
log = self.logs[current]
|
||||
|
||||
adif = ADIF()
|
||||
adif.write(log.records, path)
|
||||
|
||||
#current.set_tab_label(path) #FIXME: Need to change the tab's label once the log is saved.
|
||||
|
||||
if(log.modified):
|
||||
log.path = path
|
||||
log.name = path
|
||||
self.set_tab_label_text(self.get_nth_page(current), log.name)
|
||||
log.set_modified(False)
|
||||
return
|
||||
|
||||
def close_log(self, widget=None):
|
||||
def close_log(self, widget, parent):
|
||||
current = self.get_current_page() # Gets the index of the selected tab in the logbook
|
||||
if(current == -1):
|
||||
logging.debug("No log files to close!")
|
||||
return
|
||||
|
||||
if(not self.logs[current].saved):
|
||||
dialog = Gtk.MessageDialog(parent, Gtk.DialogFlags.DESTROY_WITH_PARENT,
|
||||
Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO,
|
||||
"Log %d is not saved. Are you sure you want to close it?" % current)
|
||||
response = dialog.run()
|
||||
dialog.destroy()
|
||||
if(response == Gtk.ResponseType.NO):
|
||||
return
|
||||
|
||||
self.logs.pop(current)
|
||||
# Remove the log from the renderers too
|
||||
self.treeview.pop(current)
|
||||
self.treeselection.pop(current)
|
||||
# And finally remove the tab in the Logbook
|
||||
self.remove_page(current)
|
||||
|
||||
return
|
||||
|
||||
def render_log(self, log):
|
||||
|
@ -205,6 +248,7 @@ class Logbook(Gtk.Notebook):
|
|||
self.treeselection[current].select_path(log.get_number_of_records()-1)
|
||||
|
||||
dialog.destroy()
|
||||
self.set_tab_label_text(self.get_nth_page(current), log.name)
|
||||
return
|
||||
|
||||
def delete_record_callback(self, widget, parent):
|
||||
|
@ -230,7 +274,7 @@ class Logbook(Gtk.Notebook):
|
|||
self.logs[current].delete_record(index, iter)
|
||||
|
||||
dialog.destroy()
|
||||
|
||||
self.set_tab_label_text(self.get_nth_page(current), self.logs[current].name)
|
||||
return
|
||||
|
||||
def edit_record_callback(self, widget, path, view_column, parent):
|
||||
|
@ -285,6 +329,7 @@ class Logbook(Gtk.Notebook):
|
|||
log[row_index][i+1] = fields_and_data[field_names[i]]
|
||||
|
||||
dialog.destroy()
|
||||
self.set_tab_label_text(self.get_nth_page(current), log.name)
|
||||
return
|
||||
|
||||
def search_log_callback(self, widget):
|
||||
|
|
12
src/menu.py
12
src/menu.py
|
@ -47,7 +47,7 @@ class Menu(Gtk.MenuBar):
|
|||
|
||||
# Open (for opening and reading ADIF files)
|
||||
mitem_open = Gtk.MenuItem("Open Log File...")
|
||||
mitem_open.connect("activate", parent.logbook.open_log)
|
||||
mitem_open.connect("activate", parent.logbook.open_log, parent)
|
||||
key, mod = Gtk.accelerator_parse("<Control>O")
|
||||
mitem_open.add_accelerator("activate", agrp, key, mod, Gtk.AccelFlags.VISIBLE)
|
||||
subm_file.append(mitem_open)
|
||||
|
@ -59,9 +59,17 @@ class Menu(Gtk.MenuBar):
|
|||
mitem_save.add_accelerator("activate", agrp, key, mod, Gtk.AccelFlags.VISIBLE)
|
||||
subm_file.append(mitem_save)
|
||||
|
||||
# Save as (for writing ADIF files)
|
||||
mitem_save = Gtk.MenuItem("Save Log File As...")
|
||||
mitem_save.connect("activate", parent.logbook.save_log_as)
|
||||
key, mod = Gtk.accelerator_parse("<Shift><Control>S")
|
||||
mitem_save.add_accelerator("activate", agrp, key, mod, Gtk.AccelFlags.VISIBLE)
|
||||
subm_file.append(mitem_save)
|
||||
|
||||
# Close the current log
|
||||
mitem_close = Gtk.MenuItem("Close Log")
|
||||
mitem_close.connect("activate", parent.logbook.close_log)
|
||||
mitem_close.connect("activate", parent.logbook.close_log, parent)
|
||||
key, mod = Gtk.accelerator_parse("<Control>W")
|
||||
mitem_close.add_accelerator("activate", agrp, key, mod, Gtk.AccelFlags.VISIBLE)
|
||||
subm_file.append(mitem_close)
|
||||
|
||||
|
|
|
@ -43,7 +43,7 @@ class Toolbar(Gtk.HBox):
|
|||
button = Gtk.Button()
|
||||
button.add(icon)
|
||||
button.set_tooltip_text('Open log')
|
||||
button.connect("clicked", parent.logbook.open_log)
|
||||
button.connect("clicked", parent.logbook.open_log, parent)
|
||||
self.pack_start(button, False, False, 0)
|
||||
|
||||
# Save log
|
||||
|
@ -61,7 +61,7 @@ class Toolbar(Gtk.HBox):
|
|||
button = Gtk.Button()
|
||||
button.add(icon)
|
||||
button.set_tooltip_text('Close log')
|
||||
button.connect("clicked", parent.logbook.close_log)
|
||||
button.connect("clicked", parent.logbook.close_log, parent)
|
||||
self.pack_start(button, False, False, 0)
|
||||
|
||||
self.pack_start(Gtk.SeparatorMenuItem(), False, False, 0)
|
||||
|
|
Ładowanie…
Reference in New Issue