[gnome-commander] advrename: add support for auto width counters



commit bc0a9bae28354a322d62760024627c2d183b1e5a
Author: Piotr Eljasiak <epiotr src gnome org>
Date:   Sun Dec 4 23:41:50 2011 +0100

    advrename: add support for auto width counters

 src/dialogs/gnome-cmd-advrename-dialog.cc    |    4 +++
 src/gnome-cmd-advrename-lexer.h              |    2 +-
 src/gnome-cmd-advrename-lexer.ll             |   29 ++++++++++++++++++++----
 src/gnome-cmd-advrename-profile-component.cc |   31 ++++++++++++++++---------
 src/gnome-cmd-data.cc                        |    3 +-
 5 files changed, 51 insertions(+), 18 deletions(-)
---
diff --git a/src/dialogs/gnome-cmd-advrename-dialog.cc b/src/dialogs/gnome-cmd-advrename-dialog.cc
index 85c95c0..a444795 100644
--- a/src/dialogs/gnome-cmd-advrename-dialog.cc
+++ b/src/dialogs/gnome-cmd-advrename-dialog.cc
@@ -20,6 +20,7 @@
 
 #include <config.h>
 #include <sys/types.h>
+#include <math.h>
 #include <regex.h>
 #include <unistd.h>
 #include <errno.h>
@@ -618,8 +619,11 @@ inline GtkWidget *create_files_view ()
 
 void GnomeCmdAdvrenameDialog::update_new_filenames()
 {
+    gint n = gtk_tree_model_iter_n_children (files, NULL);
+
     gnome_cmd_advrename_reset_counter (defaults.default_profile.counter_start,
                                        defaults.default_profile.counter_width,
+                                       n ? log10(n)+1 : 1,
                                        defaults.default_profile.counter_step);
     GtkTreeIter i;
 
diff --git a/src/gnome-cmd-advrename-lexer.h b/src/gnome-cmd-advrename-lexer.h
index dbeec1b..c220fd4 100644
--- a/src/gnome-cmd-advrename-lexer.h
+++ b/src/gnome-cmd-advrename-lexer.h
@@ -27,7 +27,7 @@
 #define NAME_MAX (FILENAME_MAX)
 #endif
 
-void gnome_cmd_advrename_reset_counter(long start=1, int precision=-1, int step=1);
+void gnome_cmd_advrename_reset_counter(long start=1, int precision=-1, int auto_precision=1, int step=1);
 void gnome_cmd_advrename_parse_template(const char *template_string, gboolean &has_counters);
 char *gnome_cmd_advrename_gen_fname(GnomeCmdFile *f, size_t new_fname_size=NAME_MAX);
 
diff --git a/src/gnome-cmd-advrename-lexer.ll b/src/gnome-cmd-advrename-lexer.ll
index 6825084..8be4948 100644
--- a/src/gnome-cmd-advrename-lexer.ll
+++ b/src/gnome-cmd-advrename-lexer.ll
@@ -50,6 +50,7 @@ enum {TEXT=1,NAME,EXTENSION,FULL_NAME,COUNTER,XRANDOM,XXRANDOM,PARENT_DIR,GRANDP
 
 const int GLOBAL_COUNTER_STEP = -1;
 const int GLOBAL_COUNTER_PREC = -1;
+const int AUTO_COUNTER_PREC = 0;
 
 const int MAX_PRECISION = 16;
 const int MAX_XRANDOM_PRECISION = 8;
@@ -185,7 +186,7 @@ tag_name    {ape}|{audio}|{doc}|{exif}|{file}|{flac}|{id3}|{image}|{iptc}|{pdf}|
                                   fname_template.push_back(p);
                                 }
 
-\$[c]\({uint}\)                 {
+\$c\(({uint}|a)\)               {
                                   CHUNK *p = g_new0 (CHUNK,1);
 
                                   int precision = 1;
@@ -196,7 +197,7 @@ tag_name    {ape}|{audio}|{doc}|{exif}|{file}|{flac}|{id3}|{image}|{iptc}|{pdf}|
                                   p->counter.n = p->counter.start = 1;      //  default counter value
                                   p->counter.init_step = GLOBAL_COUNTER_STEP;
                                   p->counter.step = 1;                      //  default counter step
-                                  p->counter.prec = p->counter.init_prec = min (precision, MAX_PRECISION);
+                                  p->counter.prec = p->counter.init_prec = yytext[3]=='a' ? AUTO_COUNTER_PREC : min (precision, MAX_PRECISION);
                                   sprintf(p->counter.fmt,"%%0%ili",p->counter.prec);
 
                                   fname_template.push_back(p);
@@ -312,16 +313,34 @@ tag_name    {ape}|{audio}|{doc}|{exif}|{file}|{flac}|{id3}|{image}|{iptc}|{pdf}|
 %%
 
 
-void gnome_cmd_advrename_reset_counter(long start, int precision, int step)
+void gnome_cmd_advrename_reset_counter(long start, int precision, int auto_precision, int step)
 {
-  precision = precision<MAX_PRECISION ? precision : MAX_PRECISION;
+  auto_precision = CLAMP(auto_precision,1,MAX_PRECISION);
+  if (!precision)
+    precision = auto_precision;
+  else
+    if (precision>MAX_PRECISION)
+      precision = MAX_PRECISION;
 
   for (vector<CHUNK *>::iterator i=fname_template.begin(); i!=fname_template.end(); ++i)
     if ((*i)->type==COUNTER)
     {
       (*i)->counter.n = (*i)->counter.start = start;
       (*i)->counter.step = (*i)->counter.init_step==GLOBAL_COUNTER_STEP ? step : (*i)->counter.init_step;
-      (*i)->counter.prec = (*i)->counter.init_prec==GLOBAL_COUNTER_PREC ? precision : (*i)->counter.init_prec;
+      switch ((*i)->counter.init_prec)
+      {
+        case GLOBAL_COUNTER_PREC:
+          (*i)->counter.prec = precision;
+          break;
+
+        case AUTO_COUNTER_PREC:
+          (*i)->counter.prec = auto_precision;
+          break;
+
+        default:
+          (*i)->counter.prec = (*i)->counter.init_prec;
+          break;
+      }
       sprintf((*i)->counter.fmt,"%%0%ili",(*i)->counter.prec);
     }
 }
diff --git a/src/gnome-cmd-advrename-profile-component.cc b/src/gnome-cmd-advrename-profile-component.cc
index 6c4c579..072a6e4 100644
--- a/src/gnome-cmd-advrename-profile-component.cc
+++ b/src/gnome-cmd-advrename-profile-component.cc
@@ -60,7 +60,7 @@ struct GnomeCmdAdvrenameProfileComponent::Private
 
     GtkWidget *counter_start_spin;
     GtkWidget *counter_step_spin;
-    GtkWidget *counter_digits_spin;
+    GtkWidget *counter_digits_combo;
 
     GtkTreeModel *regex_model;
     GtkWidget *regex_view;
@@ -107,7 +107,7 @@ struct GnomeCmdAdvrenameProfileComponent::Private
 
     static void on_counter_start_spin_value_changed (GtkWidget *spin, GnomeCmdAdvrenameProfileComponent *component);
     static void on_counter_step_spin_value_changed (GtkWidget *spin, GnomeCmdAdvrenameProfileComponent *component);
-    static void on_counter_digits_spin_value_changed (GtkWidget *spin, GnomeCmdAdvrenameProfileComponent *component);
+    static void on_counter_digits_combo_value_changed (GtkWidget *spin, GnomeCmdAdvrenameProfileComponent *component);
 
     static void on_regex_model_row_deleted (GtkTreeModel *treemodel, GtkTreePath *path, GnomeCmdAdvrenameProfileComponent *component);
     static void on_regex_add_btn_clicked (GtkButton *button, GnomeCmdAdvrenameProfileComponent *component);
@@ -135,6 +135,7 @@ GtkItemFactoryEntry GnomeCmdAdvrenameProfileComponent::Private::name_items[] =
 GtkItemFactoryEntry GnomeCmdAdvrenameProfileComponent::Private::counter_items[] =
     {{N_("/Counter"), NULL, (GtkItemFactoryCallback) GnomeCmdAdvrenameProfileComponent::Private::insert_text_tag, 5},
      {N_("/Counter (width)"), NULL, (GtkItemFactoryCallback) GnomeCmdAdvrenameProfileComponent::Private::insert_text_tag, 6},
+     {N_("/Counter (auto)"), NULL, (GtkItemFactoryCallback) GnomeCmdAdvrenameProfileComponent::Private::insert_text_tag, 27},
      {N_("/Hexadecimal random number (width)"), NULL, (GtkItemFactoryCallback) GnomeCmdAdvrenameProfileComponent::Private::insert_text_tag, 7}};
 
 GtkItemFactoryEntry GnomeCmdAdvrenameProfileComponent::Private::date_items[] =
@@ -546,8 +547,9 @@ void GnomeCmdAdvrenameProfileComponent::Private::insert_text_tag(GnomeCmdAdvrena
                                          "%H%M%S",      // 23
                                          "%H",          // 24
                                          "%M",          // 25
-                                         "%S"};         // 26
-
+                                         "%S",          // 26
+                                         "$c(a)"};      // 27
+    
     g_return_if_fail (n < G_N_ELEMENTS(placeholder));
 
     priv->insert_tag(placeholder[n]);
@@ -703,9 +705,10 @@ void GnomeCmdAdvrenameProfileComponent::Private::on_counter_step_spin_value_chan
 }
 
 
-void GnomeCmdAdvrenameProfileComponent::Private::on_counter_digits_spin_value_changed (GtkWidget *spin, GnomeCmdAdvrenameProfileComponent *component)
+void GnomeCmdAdvrenameProfileComponent::Private::on_counter_digits_combo_value_changed (GtkWidget *combo, GnomeCmdAdvrenameProfileComponent *component)
 {
-    component->profile.counter_width = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (spin));
+    component->profile.counter_width = gtk_combo_box_get_active (GTK_COMBO_BOX (combo));
+    component->profile.counter_width = CLAMP(component->profile.counter_width, 0, 16);
     g_signal_emit (component, signals[COUNTER_CHANGED], 0);
 }
 
@@ -957,10 +960,16 @@ static void gnome_cmd_advrename_profile_component_init (GnomeCmdAdvrenameProfile
 
         label = gtk_label_new_with_mnemonic (_("Di_gits:"));
         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
-        component->priv->counter_digits_spin = spin = gtk_spin_button_new_with_range (1, 16, 1);
-        gtk_label_set_mnemonic_widget (GTK_LABEL (label), spin);
+        component->priv->counter_digits_combo = combo = gtk_combo_box_new_text ();
+
+        static const char *digit_widths[] = {N_("auto"),"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"};
+
+        for (const char **i=digit_widths; *i; ++i)
+            gtk_combo_box_append_text (GTK_COMBO_BOX (combo), *i);
+
+        gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
         gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 2, 3);
-        gtk_table_attach_defaults (GTK_TABLE (table), spin, 1, 2, 2, 3);
+        gtk_table_attach_defaults (GTK_TABLE (table), combo, 1, 2, 2, 3);
     }
 
 
@@ -1130,7 +1139,7 @@ GnomeCmdAdvrenameProfileComponent::GnomeCmdAdvrenameProfileComponent(GnomeCmdDat
     // Counter
     g_signal_connect (priv->counter_start_spin, "value-changed", G_CALLBACK (Private::on_counter_start_spin_value_changed), this);
     g_signal_connect (priv->counter_step_spin, "value-changed", G_CALLBACK (Private::on_counter_step_spin_value_changed), this);
-    g_signal_connect (priv->counter_digits_spin, "value-changed", G_CALLBACK (Private::on_counter_digits_spin_value_changed), this);
+    g_signal_connect (priv->counter_digits_combo, "changed", G_CALLBACK (Private::on_counter_digits_combo_value_changed), this);
 
     // Regex
     priv->regex_model = create_regex_model ();
@@ -1249,7 +1258,7 @@ void GnomeCmdAdvrenameProfileComponent::update()
 
     gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->counter_start_spin), profile.counter_start);
     gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->counter_step_spin), profile.counter_step);
-    gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->counter_digits_spin), profile.counter_width);
+    gtk_combo_box_set_active (GTK_COMBO_BOX (priv->counter_digits_combo), profile.counter_width);
 
     if (!model_is_empty(priv->regex_model))
     {
diff --git a/src/gnome-cmd-data.cc b/src/gnome-cmd-data.cc
index ee286af..221e69e 100644
--- a/src/gnome-cmd-data.cc
+++ b/src/gnome-cmd-data.cc
@@ -187,7 +187,8 @@ void GnomeCmdData::AdvrenameConfig::Profile::reset()
     name.clear();
     template_string = "$N";
     regexes.clear();
-    counter_start = counter_width = counter_step = 1;
+    counter_start = counter_step = 1;
+    counter_width = 0;
     case_conversion = 0;
     trim_blanks = 3;
 }



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