Allow creation of new lots from lot viewer. (#420543)

Patch by Klee Dienes <klee@mit.edu>

        * src/engine/Account.c (xaccAccountRemoveLot): Generate a
        QOF_EVENT_REMOVE for the lot and QOF_EVENT_MODIFY for the account.
        (xaccAccountInsertLot): Ditto, but QOF_EVENT_ADD.
        * src/engine/gnc-lot.c (gnc_lot_make_default): Move from
        cap-gains.c.
        * src/engine/gnc-lot.h (gnc_lot_make_default): Add prototype.
        * src/engine/cap-gains.c (MakeDefaultLot): Move to gnc-lot.c as
        gnc_lot_make_default().
        * src/gnome/lot-viewer.c: Add code to support lot creation:
        (RESPONSE_NEW_LOT): Add enumerated value.
        (lv_save_current_row): New function.
        (lv_select_row): Add call to lv_save_current_row.
        (lv_unselect_row): Use lv_save_current_row.
        (lv_close_handler): Use lv_save_current_row.
        (lv_response_cb): Handle RESPONSE_NEW_LOT.
        (gnc_lot_viewer_dialog): Watch for QOF_EVENT_ADD and
        QOF_EVENT_REMOVE for lots.
        * src/gnome/glade/lots.glade: Add "new lot" button.


git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@16725 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Derek Atkins 2007-12-26 02:14:55 +00:00
parent 480bb412c7
commit fec3d53296
6 changed files with 140 additions and 50 deletions

View File

@ -1650,6 +1650,8 @@ xaccAccountRemoveLot (Account *acc, GNCLot *lot)
ENTER ("(acc=%p, lot=%p)", acc, lot);
priv->lots = g_list_remove(priv->lots, lot);
qof_event_gen (&lot->inst, QOF_EVENT_REMOVE, NULL);
qof_event_gen (&acc->inst, QOF_EVENT_MODIFY, NULL);
LEAVE ("(acc=%p, lot=%p)", acc, lot);
}
@ -1685,6 +1687,9 @@ xaccAccountInsertLot (Account *acc, GNCLot *lot)
* called from gnc_book_close_period since xaccAccountInsertSplit
* will try to balance capital gains and things aren't ready for that. */
qof_event_gen (&lot->inst, QOF_EVENT_ADD, NULL);
qof_event_gen (&acc->inst, QOF_EVENT_MODIFY, NULL);
LEAVE ("(acc=%p, lot=%p)", acc, lot);
}

View File

@ -571,25 +571,6 @@ xaccSplitAssignToLot (Split *split, GNCLot *lot)
/* ============================================================== */
static GNCLot *
MakeDefaultLot (Account *acc)
{
GNCLot * lot;
gint64 id;
char buff[200];
lot = gnc_lot_new (qof_instance_get_book(acc));
/* Provide a reasonable title for the new lot */
id = kvp_frame_get_gint64 (xaccAccountGetSlots (acc), "/lot-mgmt/next-id");
snprintf (buff, 200, ("%s %" G_GINT64_FORMAT), _("Lot"), id);
kvp_frame_set_str (gnc_lot_get_slots (lot), "/title", buff);
id ++;
kvp_frame_set_gint64 (xaccAccountGetSlots (acc), "/lot-mgmt/next-id", id);
return lot;
}
/* Accounting-policy callback. Given an account and an amount,
* this routine should return a lot. By implementing this as
* a callback, we can 'easily' add other accounting policies.
@ -632,7 +613,7 @@ xaccSplitAssign (Split *split)
lot = pcy->PolicyGetLot (pcy, split);
if (!lot)
{
lot = MakeDefaultLot (acc);
lot = gnc_lot_make_default (acc);
PINFO ("start new lot (%s)", gnc_lot_get_title(lot));
}
split = xaccSplitAssignToLot (split, lot);

View File

@ -39,8 +39,12 @@
* Copyright (c) 2002,2003 Linas Vepstas <linas@linas.org>
*/
#include <glib.h>
#include <glib/gi18n.h>
#include "config.h"
#include "Account.h"
#include "AccountP.h"
#include "gnc-lot.h"
#include "gnc-lot-p.h"
#include "cap-gains.h"
@ -454,4 +458,22 @@ gboolean gnc_lot_register (void)
return qof_object_register(&gncLotDesc);
}
GNCLot * gnc_lot_make_default (Account *acc)
{
GNCLot * lot;
gint64 id;
char buff[200];
lot = gnc_lot_new (qof_instance_get_book(acc));
/* Provide a reasonable title for the new lot */
id = kvp_frame_get_gint64 (xaccAccountGetSlots (acc), "/lot-mgmt/next-id");
snprintf (buff, 200, ("%s %" G_GINT64_FORMAT), _("Lot"), id);
kvp_frame_set_str (gnc_lot_get_slots (lot), "/title", buff);
id ++;
kvp_frame_set_gint64 (xaccAccountGetSlots (acc), "/lot-mgmt/next-id", id);
return lot;
}
/* ========================== END OF FILE ========================= */

View File

@ -156,6 +156,9 @@ void gnc_lot_set_notes (GNCLot *, const char *);
* */
KvpFrame * gnc_lot_get_slots (const GNCLot *);
/** XXX: Document? */
GNCLot * gnc_lot_make_default (Account * acc);
#define gnc_lot_get_guid(X) qof_entity_get_guid(QOF_INSTANCE(X))
#define LOT_IS_CLOSED "is-closed?"

View File

@ -34,6 +34,81 @@
<property name="visible">True</property>
<property name="layout_style">GTK_BUTTONBOX_END</property>
<child>
<widget class="GtkButton" id="new lot button">
<property name="visible">True</property>
<property name="can_default">True</property>
<property name="can_focus">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<property name="response_id">5</property>
<child>
<widget class="GtkAlignment" id="alignment3">
<property name="visible">True</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xscale">0</property>
<property name="yscale">0</property>
<property name="top_padding">0</property>
<property name="bottom_padding">0</property>
<property name="left_padding">0</property>
<property name="right_padding">0</property>
<child>
<widget class="GtkHBox" id="hbox4">
<property name="visible">True</property>
<property name="homogeneous">False</property>
<property name="spacing">2</property>
<child>
<widget class="GtkImage" id="image3">
<property name="visible">True</property>
<property name="stock">gtk-new</property>
<property name="icon_size">4</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
<child>
<widget class="GtkLabel" id="label21">
<property name="visible">True</property>
<property name="label" translatable="yes">_New Lot</property>
<property name="use_underline">True</property>
<property name="use_markup">False</property>
<property name="justify">GTK_JUSTIFY_LEFT</property>
<property name="wrap">False</property>
<property name="selectable">False</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
<property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
<property name="width_chars">-1</property>
<property name="single_line_mode">False</property>
<property name="angle">0</property>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
</widget>
</child>
</widget>
</child>
</widget>
</child>
<child>
<widget class="GtkButton" id="scrub account button">
<property name="visible">True</property>

View File

@ -64,6 +64,7 @@ enum lot_cols {
#define RESPONSE_DELETE 2
#define RESPONSE_SCRUB_LOT 3
#define RESPONSE_SCRUB_ACCOUNT 4
#define RESPONSE_NEW_LOT 5
#define GCONF_SECTION "dialogs/lot_viewer"
#define GCONF_KEY_HPOSITION "hpane_position"
@ -77,6 +78,7 @@ struct _GNCLotViewer
#endif
GtkButton * delete_button;
GtkButton * scrub_lot_button;
GtkButton * new_lot_button;
GtkPaned * lot_hpaned;
GtkPaned * lot_vpaned;
GtkTreeView * lot_view;
@ -210,6 +212,26 @@ lv_clear_splits (GNCLotViewer *lv)
gtk_clist_clear (lv->mini_clist);
}
static void
lv_save_current_row (GNCLotViewer *lv)
{
GNCLot *lot = lv->selected_lot;
const char * str;
char * notes;
if (lot)
{
/* Get the title, save_the_title */
str = gtk_entry_get_text (lv->title_entry);
gnc_lot_set_title (lot, str);
/* Get the notes, save the notes */
notes = xxxgtk_textview_get_text (lv->lot_notes);
gnc_lot_set_notes (lot, notes);
g_free(notes);
}
}
/* ======================================================================== */
/* Callback for selecting a row the the list-of-list clist */
@ -219,6 +241,8 @@ lv_select_row (GNCLotViewer *lv,
{
const char * str;
lv_save_current_row (lv);
str = gnc_lot_get_title (lot);
if (!str) str = "";
gtk_entry_set_text (lv->title_entry, str);
@ -273,21 +297,7 @@ lv_unset_lot (GNCLotViewer *lv)
static void
lv_unselect_row (GNCLotViewer *lv)
{
GNCLot *lot = lv->selected_lot;
const char * str;
char * notes;
if (lot)
{
/* Get the title, save_the_title */
str = gtk_entry_get_text (lv->title_entry);
gnc_lot_set_title (lot, str);
/* Get the notes, save the notes */
notes = xxxgtk_textview_get_text (lv->lot_notes);
gnc_lot_set_notes (lot, notes);
g_free(notes);
}
lv_save_current_row (lv);
lv_unset_lot (lv);
}
@ -488,20 +498,7 @@ lv_close_handler (gpointer user_data)
GNCLotViewer *lv = user_data;
GNCLot *lot = lv->selected_lot;
if (lot)
{
const char * str;
char *notes;
/* Get the title, save the title */
str = gtk_entry_get_text (lv->title_entry);
gnc_lot_set_title (lot, str);
/* Get the notes, save the notes */
notes = xxxgtk_textview_get_text (lv->lot_notes);
gnc_lot_set_notes (lot, notes);
g_free(notes);
}
lv_save_current_row (lv);
gnc_save_window_size(GCONF_SECTION, GTK_WINDOW(lv->window));
gtk_widget_destroy (lv->window);
@ -582,6 +579,12 @@ lv_response_cb (GtkDialog *dialog, gint response, gpointer data)
gnc_lot_viewer_fill (lv);
lv_show_splits (lv);
break;
case RESPONSE_NEW_LOT:
lv_save_current_row (lv);
lot = gnc_lot_make_default (lv->account);
xaccAccountInsertLot (lv->account, lot);
break;
}
}
@ -659,6 +662,7 @@ lv_create (GNCLotViewer *lv)
#endif
lv->delete_button = GTK_BUTTON(glade_xml_get_widget (xml, "delete button"));
lv->scrub_lot_button = GTK_BUTTON(glade_xml_get_widget (xml, "scrub lot button"));
lv->new_lot_button = GTK_BUTTON(glade_xml_get_widget (xml, "new lot button"));
lv->lot_view = GTK_TREE_VIEW(glade_xml_get_widget (xml, "lot view"));
lv_init_lot_view(lv);
@ -709,7 +713,7 @@ gnc_lot_viewer_dialog (Account *account)
gnc_gui_component_watch_entity_type (component_id,
GNC_ID_LOT,
QOF_EVENT_CREATE | QOF_EVENT_MODIFY | QOF_EVENT_DESTROY);
QOF_EVENT_CREATE | QOF_EVENT_ADD | QOF_EVENT_REMOVE | QOF_EVENT_MODIFY | QOF_EVENT_DESTROY);
gtk_widget_show_all (lv->window);
gnc_window_adjust_for_screen (GTK_WINDOW(lv->window));