changeset 24669:15fe766fbaf5

set initial size of variable editor columns based on data * variable-editor-model.h, variable-editor-model.cc: (variable_editor_model::impl::column_width, variable_editor_model::column_width): Compute column width (in characters) for model based on given data. * variable-editor.cc (variable_editor::variable_editor): Set initial column widths based on model data. Delete all variables and functions related to automatic resizing of column widths when model is updated.
author John W. Eaton <jwe@octave.org>
date Thu, 01 Feb 2018 06:37:34 -0500
parents d4dd741b2794
children 62a05d23cd00
files libgui/src/variable-editor-model.cc libgui/src/variable-editor-model.h libgui/src/variable-editor.cc libgui/src/variable-editor.h
diffstat 4 files changed, 54 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/variable-editor-model.cc	Thu Feb 01 06:36:50 2018 -0500
+++ b/libgui/src/variable-editor-model.cc	Thu Feb 01 06:37:34 2018 -0500
@@ -263,6 +263,30 @@
   const cell& elem (int r, int c) const { return elem (index (r, c)); }
   const cell& elem (const QModelIndex& idx) const { return elem (index (idx)); }
 
+  int column_width (void) const
+  {
+    int width = 0;
+
+    float_format r_fmt = m_display_fmt.real_format ();
+    float_format i_fmt = m_display_fmt.imag_format ();
+
+    int rw = r_fmt.fw;
+    int iw = i_fmt.fw;
+
+    if (rw > 0)
+      {
+        if (m_value.iscomplex ())
+          {
+            if (iw > 0)
+              width = rw + iw + 5;
+          }
+        else
+          width = rw + 2;
+      }
+
+    return width;
+  }
+
   char quote_char (int r, int c) const
   {
     if (m_value.is_string ())
@@ -623,6 +647,12 @@
 }
 
 int
+variable_editor_model::column_width (void) const
+{
+  return m_d->column_width  ();
+}
+
+int
 variable_editor_model::rowCount (const QModelIndex&) const
 {
   return m_d->m_validity ? m_d->rows () : 1;
--- a/libgui/src/variable-editor-model.h	Thu Feb 01 06:36:50 2018 -0500
+++ b/libgui/src/variable-editor-model.h	Thu Feb 01 06:37:34 2018 -0500
@@ -59,6 +59,8 @@
 
   octave_value value_at (const QModelIndex& idx) const;
 
+  int column_width (void) const;
+
   int rowCount (const QModelIndex& = QModelIndex ()) const;
 
   int columnCount (const QModelIndex& = QModelIndex ()) const;
--- a/libgui/src/variable-editor.cc	Thu Feb 01 06:36:50 2018 -0500
+++ b/libgui/src/variable-editor.cc	Thu Feb 01 06:37:34 2018 -0500
@@ -84,9 +84,8 @@
 variable_editor::variable_editor (QWidget *p)
   : octave_dock_widget (p), m_main (new QMainWindow ()),
     m_tool_bar (new QToolBar (m_main)),
-    m_tab_widget (new QTabWidget (m_main)),
-    m_default_width (30), m_default_height (100), m_add_font_height (0),
-    m_autofit (false), m_autofit_max (false), m_use_terminal_font (true),
+    m_tab_widget (new QTabWidget (m_main)), m_default_width (30),
+    m_default_height (100), m_add_font_height (0), m_use_terminal_font (true),
     m_alternate_rows (true), m_stylesheet (""), m_font (), m_sel_font (),
     m_table_colors ()
 {
@@ -205,8 +204,6 @@
   if (m_tab_widget->count () == 1)
     m_tool_bar->setEnabled (true);
 
-  maybe_resize_columns ();
-
   table->setFont (m_font);
   table->setStyleSheet (m_stylesheet);
   table->setAlternatingRowColors (m_alternate_rows);
@@ -217,6 +214,24 @@
 #endif
   table->verticalHeader ()->setDefaultSectionSize (m_default_height
                                                    + m_add_font_height);
+
+  int col_width = model->column_width ();
+
+  if (col_width > 0)
+    {
+#if defined (HAVE_QT4)
+      table->horizontalHeader ()->setResizeMode (QHeaderView::Interactive);
+#else
+      table->horizontalHeader ()->setSectionResizeMode (QHeaderView::Interactive);
+#endif
+
+      // col_width is in characters.  The font should be a fixed-width
+      // font, so any character will do.  If not, you lose!
+
+      QFontMetrics fm (m_font);
+      int w = col_width * fm.width ('0');
+      table->horizontalHeader ()->setDefaultSectionSize (w);
+    }
 }
 
 void
@@ -287,8 +302,6 @@
 void
 variable_editor::callUpdate (const QModelIndex&, const QModelIndex&)
 {
-  maybe_resize_columns ();
-
   emit updated ();
 }
 
@@ -300,17 +313,6 @@
   m_default_width = settings->value ("variable_editor/column_width",
                                      100).toInt ();
 
-  m_autofit = settings->value ("variable_editor/autofit_column_width",
-                               false).toBool ();
-
-  // FIXME: Magic Number 1 here, why not use enum?
-
-  if (m_autofit)
-    {
-      if (settings->value ("variable_editor/autofit_type", 0).toInt () == 1)
-        m_autofit_max = true;
-    }
-
   m_default_height = settings->value ("variable_editor/row_height",
                                       10).toInt ();
 
@@ -343,14 +345,9 @@
 
   m_font = QFont (font_name, font_size);
 
-  if (settings->value ("variable_editor/autofit_row_height", true).toBool ())
-    {
-      QFontMetrics fm (m_font);
+  QFontMetrics fm (m_font);
 
-      m_add_font_height = fm.height ();
-    }
-  else
-    m_add_font_height = 0;
+  m_add_font_height = fm.height ();
 
   for (int i = 0; i < class_chars.length (); i++)
     {
@@ -406,30 +403,6 @@
 }
 
 void
-variable_editor::maybe_resize_columns (void)
-{
-  QTableView *table = get_table_data (m_tab_widget).m_table;
-
-  if (m_autofit)
-    {
-      table->resizeColumnsToContents ();
-
-      if (m_autofit_max)
-        {
-          int mx = 0;
-
-          for (int i = 0; i < table->model ()->columnCount (); i++)
-            {
-              if (table->columnWidth (i) > mx)
-                mx = table->columnWidth (i);
-            }
-
-          table->horizontalHeader ()->setDefaultSectionSize (mx);
-        }
-    }
-}
-
-void
 variable_editor::contextmenu_requested (const QPoint& qpos)
 {
   QTableView *view = get_table_data (m_tab_widget).m_table;
--- a/libgui/src/variable-editor.h	Thu Feb 01 06:36:50 2018 -0500
+++ b/libgui/src/variable-editor.h	Thu Feb 01 06:37:34 2018 -0500
@@ -70,8 +70,6 @@
 
   void notice_settings (const QSettings *);
 
-  void maybe_resize_columns (void);
-
 protected slots:
 
   void closeEvent (QCloseEvent *);
@@ -132,10 +130,6 @@
 
   int m_add_font_height;
 
-  bool m_autofit;
-
-  bool m_autofit_max;
-
   bool m_use_terminal_font;
 
   bool m_alternate_rows;