changeset 20731:83611b387bc5

provide a user preference for the encoding used by the editor (bug #45597) * file-editor-tab.cc (load_file/save_file): get the codec used for loading or saving the file from the settings file, default is system default encoding * settings-dialog.cc (settings_dialog): calling new function for initializing the new combo-box with the available encodings; (write_changed_settings): store the value from the new combo-box in settings; (init_combo_encoding): initializing the new combo-box with available encodings * settings-dialog.h: new function for initializing the new combo-box with all available encodings * settings-dialog.ui: new location of eol-mode in editor settings and new combo-box for file encoding used in the editor
author Torsten <ttl@justmail.de>
date Sat, 21 Nov 2015 14:47:22 +0100
parents 948f45a2931e
children 771d9fab5c24
files libgui/src/m-editor/file-editor-tab.cc libgui/src/settings-dialog.cc libgui/src/settings-dialog.h libgui/src/settings-dialog.ui
diffstat 4 files changed, 210 insertions(+), 105 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Thu Nov 19 16:57:51 2015 -0500
+++ b/libgui/src/m-editor/file-editor-tab.cc	Sat Nov 21 14:47:22 2015 +0100
@@ -49,6 +49,7 @@
 #include <QInputDialog>
 #include <QPrintDialog>
 #include <QDateTime>
+#include <QTextCodec>
 
 #include "file-editor-tab.h"
 #include "file-editor.h"
@@ -1354,8 +1355,15 @@
   if (!file.open (QFile::ReadOnly))
     return file.errorString ();
 
+  // read the file
   QTextStream in (&file);
-  in.setCodec("UTF-8");
+  // set the desired codec
+  QSettings *settings = resource_manager::get_settings ();
+  QString encoding = settings->value ("editor/default_encoding","SYSTEM")
+                               .toString ();
+  QTextCodec *codec = QTextCodec::codecForName (encoding.toAscii ());
+  in.setCodec(codec);
+
   QApplication::setOverrideCursor (Qt::WaitCursor);
   _edit_area->setText (in.readAll ());
   _edit_area->setEolMode (detect_eol_mode ());
@@ -1509,7 +1517,14 @@
 
   // save the contents into the file
   QTextStream out (&file);
-  out.setCodec("UTF-8");
+
+  // set the desired codec
+  QSettings *settings = resource_manager::get_settings ();
+  QString encoding = settings->value ("editor/default_encoding","SYSTEM")
+                               .toString ();
+  QTextCodec *codec = QTextCodec::codecForName (encoding.toAscii ());
+  out.setCodec(codec);
+
   QApplication::setOverrideCursor (Qt::WaitCursor);
   out << _edit_area->text ();
   out.flush ();
--- a/libgui/src/settings-dialog.cc	Thu Nov 19 16:57:51 2015 -0500
+++ b/libgui/src/settings-dialog.cc	Sat Nov 21 14:47:22 2015 +0100
@@ -34,6 +34,7 @@
 #include <QFileDialog>
 #include <QVector>
 #include <QHash>
+#include <QTextCodec>
 
 #ifdef HAVE_QSCINTILLA
 #include "octave-qscintilla.h"
@@ -365,7 +366,9 @@
   connect (ui->pb_octave_dir, SIGNAL (pressed ()),
            this, SLOT (get_octave_dir ()));
 
+  //
   // editor
+  //
   ui->useCustomFileEditor->setChecked (settings->value ("useCustomFileEditor",
                                                         false).toBool ());
   ui->customFileEditor->setText (
@@ -373,6 +376,8 @@
   ui->editor_showLineNumbers->setChecked (
     settings->value ("editor/showLineNumbers",true).toBool ());
 
+  init_combo_encoding (settings);
+
   default_var = QColor (240, 240, 240);
   QColor setting_color = settings->value ("editor/highlight_current_line_color",
                                           default_var).value<QColor> ();
@@ -798,6 +803,10 @@
                       ui->cb_show_hscrollbar->isChecked ());
   settings->setValue ("editor/default_eol_mode",
                       ui->combo_eol_mode->currentIndex ());
+  QString encoding = ui->editor_combo_encoding->currentText ();
+  if (encoding == tr ("System default"))
+    encoding = "SYSTEM";
+  settings->setValue ("editor/default_encoding", encoding);
   settings->setValue ("editor/auto_indent",
                       ui->editor_auto_ind_checkbox->isChecked ());
   settings->setValue ("editor/tab_indents_line",
@@ -1015,6 +1024,48 @@
     }
 }
 
+// initialize the combo box with possible text encodings
+void
+settings_dialog::init_combo_encoding (QSettings *settings)
+{
+  // get the codec name for each mib
+  QList<int> all_mibs = QTextCodec::availableMibs ();
+  QStringList all_codecs;
+  foreach (int mib, all_mibs)
+    {
+      QTextCodec *c = QTextCodec::codecForMib (mib);
+      all_codecs << c->name ().toUpper ();
+    }
+  all_codecs.removeDuplicates ();
+
+  // remove the "system" entry
+  int idx = all_codecs.indexOf ("SYSTEM");
+  if (idx >= 0)
+    all_codecs.removeAt (idx);
+
+  // sort and prepend meaningfull text for system's default codec
+  qSort (all_codecs);
+  all_codecs.prepend (tr ("System default"));
+
+  // get the value from the settings file (system is default)
+  QString encoding = settings->value ("editor/default_encoding","SYSTEM")
+                               .toString ();
+  if (encoding == "SYSTEM")
+    encoding = tr ("System default");
+
+  // fill the combo box and select the current item or system if
+  // current item from the settings file can not be found
+  foreach (QString c, all_codecs)
+    ui->editor_combo_encoding->addItem (c);
+  idx = ui->editor_combo_encoding->findText (encoding);
+  if (idx >= 0)
+    ui->editor_combo_encoding->setCurrentIndex (idx);
+  else
+    ui->editor_combo_encoding->setCurrentIndex (0);
+
+  ui->editor_combo_encoding->setMaxVisibleItems (12);
+}
+
 // slots for import/export of shortcut sets
 void
 settings_dialog::import_shortcut_set ()
--- a/libgui/src/settings-dialog.h	Thu Nov 19 16:57:51 2015 -0500
+++ b/libgui/src/settings-dialog.h	Sat Nov 21 14:47:22 2015 +0100
@@ -64,6 +64,8 @@
 
   void write_changed_settings (bool closing);
 
+  void init_combo_encoding (QSettings *settings);
+
   void read_workspace_colors (QSettings *settings);
   void write_workspace_colors (QSettings *settings);
 
--- a/libgui/src/settings-dialog.ui	Thu Nov 19 16:57:51 2015 -0500
+++ b/libgui/src/settings-dialog.ui	Sat Nov 21 14:47:22 2015 +0100
@@ -9,8 +9,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>678</width>
-    <height>474</height>
+    <width>700</width>
+    <height>673</height>
    </rect>
   </property>
   <property name="minimumSize">
@@ -32,7 +32,7 @@
       </size>
      </property>
      <property name="currentIndex">
-      <number>6</number>
+      <number>1</number>
      </property>
      <widget class="QWidget" name="tab_general">
       <property name="enabled">
@@ -52,8 +52,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>662</width>
-            <height>382</height>
+            <width>658</width>
+            <height>571</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_17">
@@ -498,9 +498,9 @@
           <property name="geometry">
            <rect>
             <x>0</x>
-            <y>0</y>
-            <width>690</width>
-            <height>783</height>
+            <y>-237</y>
+            <width>642</width>
+            <height>813</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_16">
@@ -827,61 +827,6 @@
                   </property>
                  </widget>
                 </item>
-                <item row="4" column="2">
-                 <layout class="QHBoxLayout" name="horizontalLayout_17">
-                  <item>
-                   <widget class="QLabel" name="label_22">
-                    <property name="text">
-                     <string>Default EOL mode</string>
-                    </property>
-                   </widget>
-                  </item>
-                  <item>
-                   <widget class="QComboBox" name="combo_eol_mode">
-                    <property name="sizePolicy">
-                     <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
-                      <horstretch>0</horstretch>
-                      <verstretch>0</verstretch>
-                     </sizepolicy>
-                    </property>
-                    <property name="sizeAdjustPolicy">
-                     <enum>QComboBox::AdjustToContents</enum>
-                    </property>
-                    <property name="minimumContentsLength">
-                     <number>7</number>
-                    </property>
-                    <item>
-                     <property name="text">
-                      <string>Windows (CRLF)</string>
-                     </property>
-                    </item>
-                    <item>
-                     <property name="text">
-                      <string>Mac (CR)</string>
-                     </property>
-                    </item>
-                    <item>
-                     <property name="text">
-                      <string>Unix (LF)</string>
-                     </property>
-                    </item>
-                   </widget>
-                  </item>
-                  <item>
-                   <spacer name="horizontalSpacer_17">
-                    <property name="orientation">
-                     <enum>Qt::Horizontal</enum>
-                    </property>
-                    <property name="sizeHint" stdset="0">
-                     <size>
-                      <width>40</width>
-                      <height>20</height>
-                     </size>
-                    </property>
-                   </spacer>
-                  </item>
-                 </layout>
-                </item>
                 <item row="9" column="0">
                  <widget class="QCheckBox" name="cb_show_hscrollbar">
                   <property name="enabled">
@@ -1335,37 +1280,129 @@
                  <number>0</number>
                 </property>
                 <item>
-                 <widget class="QCheckBox" name="editor_restoreSession">
-                  <property name="sizePolicy">
-                   <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
-                    <horstretch>0</horstretch>
-                    <verstretch>0</verstretch>
-                   </sizepolicy>
-                  </property>
-                  <property name="text">
-                   <string>Restore editor tabs from previous session on startup</string>
+                 <layout class="QGridLayout" name="gridLayout_11">
+                  <property name="topMargin">
+                   <number>0</number>
                   </property>
-                 </widget>
-                </item>
-                <item>
-                 <widget class="QCheckBox" name="editor_create_new file">
-                  <property name="sizePolicy">
-                   <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
-                    <horstretch>0</horstretch>
-                    <verstretch>0</verstretch>
-                   </sizepolicy>
-                  </property>
-                  <property name="text">
-                   <string>Create nonexistent files without prompting</string>
-                  </property>
-                 </widget>
-                </item>
-                <item>
-                 <widget class="QCheckBox" name="editor_reload_changed_files">
-                  <property name="text">
-                   <string>Reload externally changed files without prompt</string>
-                  </property>
-                 </widget>
+                  <item row="4" column="0">
+                   <widget class="QCheckBox" name="editor_reload_changed_files">
+                    <property name="text">
+                     <string>Reload externally changed files without prompt</string>
+                    </property>
+                   </widget>
+                  </item>
+                  <item row="1" column="0">
+                   <widget class="QCheckBox" name="editor_restoreSession">
+                    <property name="sizePolicy">
+                     <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
+                      <horstretch>0</horstretch>
+                      <verstretch>0</verstretch>
+                     </sizepolicy>
+                    </property>
+                    <property name="text">
+                     <string>Restore editor tabs from previous session on startup</string>
+                    </property>
+                   </widget>
+                  </item>
+                  <item row="3" column="0">
+                   <widget class="QCheckBox" name="editor_create_new file">
+                    <property name="sizePolicy">
+                     <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
+                      <horstretch>0</horstretch>
+                      <verstretch>0</verstretch>
+                     </sizepolicy>
+                    </property>
+                    <property name="text">
+                     <string>Create nonexistent files without prompting</string>
+                    </property>
+                   </widget>
+                  </item>
+                  <item row="5" column="0">
+                   <layout class="QHBoxLayout" name="horizontalLayout_2">
+                    <property name="topMargin">
+                     <number>0</number>
+                    </property>
+                    <item>
+                     <widget class="QLabel" name="label_22">
+                      <property name="text">
+                       <string>Default EOL mode</string>
+                      </property>
+                     </widget>
+                    </item>
+                    <item>
+                     <widget class="QComboBox" name="combo_eol_mode">
+                      <property name="sizePolicy">
+                       <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
+                        <horstretch>0</horstretch>
+                        <verstretch>0</verstretch>
+                       </sizepolicy>
+                      </property>
+                      <property name="sizeAdjustPolicy">
+                       <enum>QComboBox::AdjustToContents</enum>
+                      </property>
+                      <property name="minimumContentsLength">
+                       <number>7</number>
+                      </property>
+                      <item>
+                       <property name="text">
+                        <string>Windows (CRLF)</string>
+                       </property>
+                      </item>
+                      <item>
+                       <property name="text">
+                        <string>Mac (CR)</string>
+                       </property>
+                      </item>
+                      <item>
+                       <property name="text">
+                        <string>Unix (LF)</string>
+                       </property>
+                      </item>
+                     </widget>
+                    </item>
+                    <item>
+                     <spacer name="horizontalSpacer_18">
+                      <property name="orientation">
+                       <enum>Qt::Horizontal</enum>
+                      </property>
+                      <property name="sizeHint" stdset="0">
+                       <size>
+                        <width>40</width>
+                        <height>20</height>
+                       </size>
+                      </property>
+                     </spacer>
+                    </item>
+                   </layout>
+                  </item>
+                  <item row="7" column="0">
+                   <layout class="QHBoxLayout" name="horizontalLayout_16">
+                    <item>
+                     <widget class="QLabel" name="label_16">
+                      <property name="text">
+                       <string>Text encoding used for loading and saving</string>
+                      </property>
+                     </widget>
+                    </item>
+                    <item>
+                     <widget class="QComboBox" name="editor_combo_encoding"/>
+                    </item>
+                    <item>
+                     <spacer name="horizontalSpacer_32">
+                      <property name="orientation">
+                       <enum>Qt::Horizontal</enum>
+                      </property>
+                      <property name="sizeHint" stdset="0">
+                       <size>
+                        <width>40</width>
+                        <height>20</height>
+                       </size>
+                      </property>
+                     </spacer>
+                    </item>
+                   </layout>
+                  </item>
+                 </layout>
                 </item>
                </layout>
               </item>
@@ -1461,8 +1498,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>678</width>
-            <height>378</height>
+            <width>658</width>
+            <height>571</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_7">
@@ -1749,8 +1786,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>636</width>
-            <height>370</height>
+            <width>658</width>
+            <height>571</height>
            </rect>
           </property>
           <layout class="QGridLayout" name="gridLayout_8">
@@ -1893,8 +1930,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>678</width>
-            <height>378</height>
+            <width>658</width>
+            <height>571</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_19">
@@ -1962,8 +1999,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>678</width>
-            <height>378</height>
+            <width>658</width>
+            <height>571</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_25">
@@ -2161,8 +2198,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>678</width>
-            <height>378</height>
+            <width>658</width>
+            <height>571</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_20">