[gnumeric] SheetFilter: avoid doing attach-remove-attach when first creating filters.



commit 70e5c389e5d0cd4a2f2aa6c3b6182501384537e1
Author: Morten Welinder <terra gnome org>
Date:   Sun May 31 18:46:19 2020 -0400

    SheetFilter: avoid doing attach-remove-attach when first creating filters.

 ChangeLog                            |  6 ++++++
 configure.ac                         |  1 +
 plugins/excel/excel-xml-read.c       |  2 +-
 plugins/excel/ms-excel-read.c        |  2 +-
 plugins/excel/xlsx-read.c            |  2 +-
 plugins/openoffice/openoffice-read.c |  2 +-
 src/commands.c                       |  3 +--
 src/sheet-filter.c                   | 28 ++++++++++++++++------------
 src/sheet-filter.h                   |  4 +++-
 src/xml-sax-read.c                   |  2 +-
 10 files changed, 32 insertions(+), 20 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 3d4c125ea..fe2d28003 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2020-05-31  Morten Welinder  <terra gnome org>
 
+       * src/sheet-filter.c (gnm_filter_new): New argument attach.  All
+       callers changed.
+
+       * src/commands.c (cmd_autofilter_add_remove): Don't
+       attach-remove-attach.
+
        * src/sheet-object.c (cb_create_views): Maintain just one idle
        source to handle view creating for all objects that need it.
 
diff --git a/configure.ac b/configure.ac
index 163de7a7d..bf48948a3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -556,6 +556,7 @@ LIBS="$GNUMERIC_LIBS $LIBS"
 
 AC_CHECK_FUNCS(gtk_cell_renderer_text_get_background_set gtk_cell_renderer_text_get_foreground_set 
gtk_cell_renderer_text_get_editable)
 AC_CHECK_FUNCS(gtk_entry_set_editing_cancelled gtk_icon_theme_add_resource_path)
+AC_CHECK_FUNCS(g_ptr_array_insert)
 
 AC_MSG_CHECKING([for OO_NS_LOCALC_EXT])
 AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <gsf/gsf.h>]],
diff --git a/plugins/excel/excel-xml-read.c b/plugins/excel/excel-xml-read.c
index c29d84b56..8c46b493c 100644
--- a/plugins/excel/excel-xml-read.c
+++ b/plugins/excel/excel-xml-read.c
@@ -1027,7 +1027,7 @@ xl_xml_auto_filter_start (GsfXMLIn *xin, G_GNUC_UNUSED xmlChar const **attrs)
                if (end != range)
                {
                        range_init_rangeref (&r, &rr);
-                       filter = gnm_filter_new (state->sheet, &r);
+                       filter = gnm_filter_new (state->sheet, &r, TRUE);
                        gnm_filter_reapply (filter);
                }
        }
diff --git a/plugins/excel/ms-excel-read.c b/plugins/excel/ms-excel-read.c
index 7ec3d490d..f0c4f46cb 100644
--- a/plugins/excel/ms-excel-read.c
+++ b/plugins/excel/ms-excel-read.c
@@ -3929,7 +3929,7 @@ excel_prepare_autofilter (GnmXLImporter *importer, GnmNamedExpr *nexpr)
                                GnmFilter *filter;
                                ExcelReadSheet *esheet;
 
-                               filter = gnm_filter_new (r.sheet, &r.range);
+                               filter = gnm_filter_new (r.sheet, &r.range, TRUE);
                                expr_name_remove (nexpr);
 
                                for (i = 0 ; i < importer->excel_sheets->len; i++) {
diff --git a/plugins/excel/xlsx-read.c b/plugins/excel/xlsx-read.c
index 4a70b0ca5..37b808fa2 100644
--- a/plugins/excel/xlsx-read.c
+++ b/plugins/excel/xlsx-read.c
@@ -2313,7 +2313,7 @@ xlsx_CT_AutoFilter_begin (GsfXMLIn *xin, xmlChar const **attrs)
 
        for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
                if (attr_range (xin, attrs, "ref", &r))
-                       state->filter = gnm_filter_new (state->sheet, &r);
+                       state->filter = gnm_filter_new (state->sheet, &r, TRUE);
        }
 }
 
diff --git a/plugins/openoffice/openoffice-read.c b/plugins/openoffice/openoffice-read.c
index 5c5e9436d..e97aad6e3 100644
--- a/plugins/openoffice/openoffice-read.c
+++ b/plugins/openoffice/openoffice-read.c
@@ -8027,7 +8027,7 @@ oo_db_range_start (GsfXMLIn *xin, xmlChar const **attrs)
                    ref.b.sheet != invalid_sheet) {
                        range_init_rangeref (&r, &ref);
                        if (buttons)
-                               state->filter = gnm_filter_new (ref.a.sheet, &r);
+                               state->filter = gnm_filter_new (ref.a.sheet, &r, TRUE);
                        expr = gnm_expr_new_constant
                                (value_new_cellrange_r (ref.a.sheet, &r));
                } else
diff --git a/src/commands.c b/src/commands.c
index 216ccb3ec..a639f404e 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -7838,7 +7838,7 @@ cmd_autofilter_add_remove (WorkbookControl *wbc)
                                return TRUE;
                        }
                }
-               f = gnm_filter_new (sv->sheet, &region);
+               f = gnm_filter_new (sv->sheet, &region, FALSE);
                if (f == NULL) {
                        go_cmd_context_error_invalid
                                (GO_CMD_CONTEXT (wbc),
@@ -7849,7 +7849,6 @@ cmd_autofilter_add_remove (WorkbookControl *wbc)
                        return TRUE;
                }
 
-               gnm_filter_remove (f);
                if (f_old)
                        gnm_filter_attach (f_old, sv->sheet);
 
diff --git a/src/sheet-filter.c b/src/sheet-filter.c
index 4f4f11ea1..f34c0cedc 100644
--- a/src/sheet-filter.c
+++ b/src/sheet-filter.c
@@ -633,7 +633,6 @@ gnm_filter_add_field (GnmFilter *filter, int i)
 {
        /* pretend to fill the cell, then clip the X start later */
        static double const a_offsets[4] = { .0, .0, 1., 1. };
-       int n;
        GnmRange tmp;
        SheetObjectAnchor anchor;
        GnmFilterCombo *fcombo = g_object_new (GNM_FILTER_COMBO_TYPE, NULL);
@@ -646,11 +645,11 @@ gnm_filter_add_field (GnmFilter *filter, int i)
        sheet_object_set_anchor (GNM_SO (fcombo), &anchor);
        sheet_object_set_sheet (GNM_SO (fcombo), filter->sheet);
 
-       g_ptr_array_add (filter->fields, NULL);
-       for (n = filter->fields->len; --n > i ; )
-               g_ptr_array_index (filter->fields, n) =
-                       g_ptr_array_index (filter->fields, n - 1);
-       g_ptr_array_index (filter->fields, n) = fcombo;
+#ifdef HAVE_G_PTR_ARRAY_INSERT
+       g_ptr_array_insert (filter->fields, i, fcombo);
+#else
+       go_ptr_array_insert (filter->fields, fcombo, i);
+#endif
        /* We hold a reference to fcombo */
 }
 
@@ -676,13 +675,14 @@ gnm_filter_attach (GnmFilter *filter, Sheet *sheet)
 
 /**
  * gnm_filter_new:
- * @sheet:
- * @r:
+ * @sheet: #Sheet for which to create the filter.
+ * @r: #GnmRange that the filter covers.
+ * @attach: whether to attach the filter.
  *
- * Init a filter and add it to @sheet
+ * Returns: (transfer full): A new filter.
  **/
 GnmFilter *
-gnm_filter_new (Sheet *sheet, GnmRange const *r)
+gnm_filter_new (Sheet *sheet, GnmRange const *r, gboolean attach)
 {
        GnmFilter       *filter;
 
@@ -695,8 +695,12 @@ gnm_filter_new (Sheet *sheet, GnmRange const *r)
        filter->r = *r;
        filter->fields = g_ptr_array_new ();
 
-       /* This creates the initial ref.  */
-       gnm_filter_attach (filter, sheet);
+       if (attach) {
+               /* This creates the initial ref.  */
+               gnm_filter_attach (filter, sheet);
+       } else {
+               gnm_filter_ref (filter);
+       }
 
        return filter;
 }
diff --git a/src/sheet-filter.h b/src/sheet-filter.h
index e0aea5b1f..76b09dd3e 100644
--- a/src/sheet-filter.h
+++ b/src/sheet-filter.h
@@ -77,7 +77,9 @@ GnmFilterCondition *gnm_filter_condition_new_bucket (gboolean top,
                                                     double n);
 
 GType                     gnm_filter_get_type       (void);
-GnmFilter               *gnm_filter_new            (Sheet *sheet, GnmRange const *r);
+GnmFilter               *gnm_filter_new            (Sheet *sheet,
+                                                    GnmRange const *r,
+                                                    gboolean attach);
 GnmFilter               *gnm_filter_dup            (GnmFilter const *src,
                                                     Sheet *sheet);
 GnmFilter *               gnm_filter_ref            (GnmFilter *filter);
diff --git a/src/xml-sax-read.c b/src/xml-sax-read.c
index fd241fa3a..355c87f2d 100644
--- a/src/xml-sax-read.c
+++ b/src/xml-sax-read.c
@@ -2417,7 +2417,7 @@ xml_sax_filter_start (GsfXMLIn *xin, xmlChar const **attrs)
        for (i = 0; attrs != NULL && attrs[i] && attrs[i + 1] ; i += 2)
                if (attr_eq (attrs[i], "Area") &&
                    range_parse (&r, CXML2C (attrs[i + 1]), gnm_sheet_get_size (state->sheet)))
-                       state->filter = gnm_filter_new (state->sheet, &r);
+                       state->filter = gnm_filter_new (state->sheet, &r, TRUE);
        if (NULL == state->filter)
                go_io_warning (state->context, _("Invalid filter, missing Area"));
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]