changeset 20774:5c5e085a1ae6

allow selecting an encoding when saving a file (bug #45688) * file-editor-tab.cc (file_editor_tab): rearrange the widgets in the status bar and add the current file encoding which is stored in a class variable; (load_file): get current encoding from class variable; (save_file): get current/new encoding from class variable, update status bar; (save_file_as): add combo box for file encoding to file selection dialog; (handle_combo_enc_current_index): new slot for changes in this combo box; * file-editor-tab.h: new class variable, new slot for changing encoding in save-file-as dialog, new indocator in status bar * resource-manager.cc (do_combo_encoding): moved function from settings dialog since it is also used from the editor tabs * resource-manager.h: function for generating encoding entries togehter with its static wrapper * settings-dialog.cc (settings_dialog): call function for generating encoding entries for combo box in the resource manager; (init_combo_encoding): function for generating encoding entries for combo box moved into the resource manager * settings-dialog.h: removed function for encoding entries in combo box
author Torsten <ttl@justmail.de>
date Sun, 29 Nov 2015 00:01:21 +0100
parents 4d78e076a592
children cd50ce72fa24
files libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/file-editor-tab.h libgui/src/resource-manager.cc libgui/src/resource-manager.h libgui/src/settings-dialog.cc libgui/src/settings-dialog.h
diffstat 6 files changed, 128 insertions(+), 86 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Sat Nov 28 14:07:09 2015 -0500
+++ b/libgui/src/m-editor/file-editor-tab.cc	Sun Nov 29 00:01:21 2015 +0100
@@ -95,25 +95,32 @@
   // create statusbar for row/col indicator and eol mode
   _status_bar = new QStatusBar (this);
 
-  // eol mode
-  QLabel *eol_label = new QLabel (tr ("eol:"), this);
-  _eol_indicator = new QLabel ("",this);
-  QFontMetrics fm = eol_label->fontMetrics ();
-  _eol_indicator->setMinimumSize (5*fm.averageCharWidth (),0);
-  _status_bar->addPermanentWidget (eol_label, 0);
-  _status_bar->addPermanentWidget (_eol_indicator, 0);
-
   // row- and col-indicator
   _row_indicator = new QLabel ("", this);
-  _row_indicator->setMinimumSize (5*fm.averageCharWidth (),0);
+  QFontMetrics fm = _row_indicator->fontMetrics ();
+  _row_indicator->setMinimumSize (4.5*fm.averageCharWidth (),0);
   QLabel *row_label = new QLabel (tr ("line:"), this);
   _col_indicator = new QLabel ("", this);
   _col_indicator->setMinimumSize (4*fm.averageCharWidth (),0);
   QLabel *col_label = new QLabel (tr ("col:"), this);
-  _status_bar->addPermanentWidget (row_label, 0);
-  _status_bar->addPermanentWidget (_row_indicator, 0);
-  _status_bar->addPermanentWidget (col_label, 0);
-  _status_bar->addPermanentWidget (_col_indicator, 0);
+  _status_bar->addWidget (row_label, 0);
+  _status_bar->addWidget (_row_indicator, 0);
+  _status_bar->addWidget (col_label, 0);
+  _status_bar->addWidget (_col_indicator, 0);
+
+  // status bar: encoding
+  QLabel *enc_label = new QLabel (tr ("encoding:"), this);
+  _enc_indicator = new QLabel ("",this);
+  _status_bar->addWidget (enc_label, 0);
+  _status_bar->addWidget (_enc_indicator, 0);
+  _status_bar->addWidget (new QLabel (" ", this), 0);
+
+  // status bar: eol mode
+  QLabel *eol_label = new QLabel (tr ("eol:"), this);
+  _eol_indicator = new QLabel ("",this);
+  _status_bar->addWidget (eol_label, 0);
+  _status_bar->addWidget (_eol_indicator, 0);
+  _status_bar->addWidget (new QLabel (" ", this), 0);
 
   // Leave the find dialog box out of memory until requested.
   _find_dialog = 0;
@@ -171,6 +178,18 @@
     notice_settings (settings, true);
 
   setFocusProxy (_edit_area);
+
+  // encoding, not updated with the settings
+#if defined (Q_OS_WIN32)
+  _encoding = settings->value ("editor/default_encoding","SYSTEM")
+                               .toString ();
+#else
+  _encoding = settings->value ("editor/default_encoding","UTF-8")
+                               .toString ();
+#endif
+  _enc_indicator->setText (_encoding);
+  // no changes in encoding yet
+  _new_encoding = _encoding;
 }
 
 file_editor_tab::~file_editor_tab (void)
@@ -1358,15 +1377,7 @@
   // read the file
   QTextStream in (&file);
   // set the desired codec
-  QSettings *settings = resource_manager::get_settings ();
-#if defined (Q_OS_WIN32)
-  QString encoding = settings->value ("editor/default_encoding","SYSTEM")
-                               .toString ();
-#else
-  QString encoding = settings->value ("editor/default_encoding","UTF-8")
-                               .toString ();
-#endif
-  QTextCodec *codec = QTextCodec::codecForName (encoding.toAscii ());
+  QTextCodec *codec = QTextCodec::codecForName (_encoding.toAscii ());
   in.setCodec(codec);
 
   QApplication::setOverrideCursor (Qt::WaitCursor);
@@ -1523,16 +1534,10 @@
   // save the contents into the file
   QTextStream out (&file);
 
+  // consider a possible new encoding (from the save-file-as dialog)
+  _encoding = _new_encoding;
   // set the desired codec
-  QSettings *settings = resource_manager::get_settings ();
-#if defined (Q_OS_WIN32)
-  QString encoding = settings->value ("editor/default_encoding","SYSTEM")
-                               .toString ();
-#else
-  QString encoding = settings->value ("editor/default_encoding","UTF-8")
-                               .toString ();
-#endif
-  QTextCodec *codec = QTextCodec::codecForName (encoding.toAscii ());
+  QTextCodec *codec = QTextCodec::codecForName (_encoding.toAscii ());
   out.setCodec(codec);
 
   QApplication::setOverrideCursor (Qt::WaitCursor);
@@ -1552,8 +1557,9 @@
   // set the window title to actual filename (not modified)
   update_window_title (false);
 
-  // files is save -> not modified
+  // files is save -> not modified, update encoding in statusbar
   _edit_area->setModified (false);
+  _enc_indicator->setText (_encoding);
 
   if (remove_on_success)
     {
@@ -1568,6 +1574,9 @@
   // Simply put up the file chooser dialog box with a slot connection
   // then return control to the system waiting for a file selection.
 
+  // reset _new_encoding
+  _new_encoding = _encoding;
+
   // If the tab is removed in response to a QFileDialog signal, the tab
   // can't be a parent.
   QFileDialog* fileDialog;
@@ -1586,14 +1595,8 @@
   // it had/has no effect on Windows, though)
   fileDialog->setOption(QFileDialog::DontUseNativeDialog, true);
 
-  // get the dialog's layout for adding extra elements
-  QGridLayout *dialog_layout = dynamic_cast<QGridLayout*> (fileDialog->layout ());
-  int rows = dialog_layout->rowCount ();
-
   // define a new grid layout with the extra elements
   QGridLayout *extra = new QGridLayout (fileDialog);
-  QSpacerItem *spacer = new QSpacerItem (1,1,QSizePolicy::Expanding,
-                                             QSizePolicy::Fixed);
   QFrame *separator = new QFrame (fileDialog);
   separator->setFrameShape (QFrame::HLine);   // horizontal line as separator
   separator->setFrameStyle (QFrame::Sunken);
@@ -1607,18 +1610,32 @@
   _save_as_desired_eol = _edit_area->eolMode ();      // init with current eol
   combo_eol->setCurrentIndex (_save_as_desired_eol);
 
-  // track changes in the combo box
+  // combo box for encoding
+  QLabel *label_enc = new QLabel (tr ("File Encoding:"));
+  QComboBox *combo_enc = new QComboBox ();
+  resource_manager::combo_encoding (combo_enc, _encoding);
+
+  // track changes in the combo boxes
   connect (combo_eol, SIGNAL (currentIndexChanged (int)),
            this, SLOT (handle_combo_eol_current_index (int)));
+  connect (combo_enc, SIGNAL (currentIndexChanged (QString)),
+           this, SLOT (handle_combo_enc_current_index (QString)));
 
   // build the extra grid layout
-  extra->addWidget (separator,0,0,1,3);
+  extra->addWidget (separator,0,0,1,6);
   extra->addWidget (label_eol,1,0);
   extra->addWidget (combo_eol,1,1);
-  extra->addItem   (spacer,   1,2);
+  extra->addItem   (new QSpacerItem (1,20,QSizePolicy::Fixed,
+                                          QSizePolicy::Fixed), 1,2);
+  extra->addWidget (label_enc,1,3);
+  extra->addWidget (combo_enc,1,4);
+  extra->addItem   (new QSpacerItem (1,20,QSizePolicy::Expanding,
+                                          QSizePolicy::Fixed), 1,5);
 
   // and add the extra grid layout to the dialog's layout
-  dialog_layout->addLayout (extra,rows,0,1,dialog_layout->columnCount ());
+  QGridLayout *dialog_layout = dynamic_cast<QGridLayout*> (fileDialog->layout ());
+  dialog_layout->addLayout (extra,dialog_layout->rowCount (),0,
+                                  1,dialog_layout->columnCount ());
 
   // add the possible filters and the default suffix
   QStringList filters;
@@ -1678,6 +1695,12 @@
 }
 
 void
+file_editor_tab::handle_combo_enc_current_index (QString text)
+{
+  _new_encoding = text;
+}
+
+void
 file_editor_tab::handle_save_as_filter_selected (const QString& filter)
 {
   QFileDialog *file_dialog = qobject_cast<QFileDialog *> (sender ());
--- a/libgui/src/m-editor/file-editor-tab.h	Sat Nov 28 14:07:09 2015 -0500
+++ b/libgui/src/m-editor/file-editor-tab.h	Sun Nov 29 00:01:21 2015 +0100
@@ -165,6 +165,7 @@
   void handle_save_file_as_answer_cancel ();
   void handle_save_as_filter_selected (const QString& filter);
   void handle_combo_eol_current_index (int index);
+  void handle_combo_enc_current_index (QString text);
 
   // When apis preparation has finished and is ready to save
   void save_apis_info ();
@@ -228,12 +229,15 @@
   QLabel *_row_indicator;
   QLabel *_col_indicator;
   QLabel *_eol_indicator;
+  QLabel *_enc_indicator;
 
   QsciScintilla::EolMode _save_as_desired_eol;
 
   QString _file_name;
   QString _file_name_short;
   QString _ced;
+  QString _encoding;
+  QString _new_encoding;
 
   bool _long_title;
   bool _copy_available;
--- a/libgui/src/resource-manager.cc	Sat Nov 28 14:07:09 2015 -0500
+++ b/libgui/src/resource-manager.cc	Sun Nov 29 00:01:21 2015 +0100
@@ -31,6 +31,7 @@
 #include <QNetworkProxy>
 #include <QLibraryInfo>
 #include <QMessageBox>
+#include <QTextCodec>
 
 #include "error.h"
 #include "file-ops.h"
@@ -325,3 +326,52 @@
   else
     return QIcon::fromTheme (icon_name);
 }
+
+// initialize a given combo box with available text encodings
+void
+resource_manager::do_combo_encoding (QComboBox *combo, QString current)
+{
+  // 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 ();
+  qSort (all_codecs);
+
+  // the default encoding
+#if defined (Q_OS_WIN32)
+  QString def_enc = "SYSTEM";
+#else
+  QString def_enc = "UTF-8";
+#endif
+
+  // get the value from the settings file if no current encoding is given
+  QString enc = current;
+  if (enc.isEmpty ())
+    {
+      enc = settings->value ("editor/default_encoding",def_enc).toString ();
+      if (enc.isEmpty ())  // still empty?
+        enc = def_enc;     // take default
+    }
+
+  // fill the combo box
+  foreach (QString c, all_codecs)
+    combo->addItem (c);
+
+  // prepend the default item
+  combo->insertSeparator (0);
+  combo->insertItem (0, def_enc);
+
+  // select the current/default item
+  int idx = combo->findText (enc);
+  if (idx >= 0)
+    combo->setCurrentIndex (idx);
+  else
+    combo->setCurrentIndex (0);
+
+  combo->setMaxVisibleItems (12);
+}
--- a/libgui/src/resource-manager.h	Sat Nov 28 14:07:09 2015 -0500
+++ b/libgui/src/resource-manager.h	Sun Nov 29 00:01:21 2015 +0100
@@ -25,6 +25,7 @@
 
 #include <QDesktopServices>
 #include <QIcon>
+#include <QComboBox>
 #include <QMap>
 #include <QSettings>
 #include <QTranslator>
@@ -76,6 +77,12 @@
       instance->do_set_settings (file);
   }
 
+  static void combo_encoding (QComboBox *combo, QString current = QString ())
+  {
+    if (instance_ok ())
+      instance->do_combo_encoding (combo, current);
+  }
+
   static QString get_gui_translation_dir (void);
 
   static void config_translators (QTranslator*, QTranslator*, QTranslator*);
@@ -139,6 +146,7 @@
 
   QIcon do_icon (const QString& icon, bool fallback);
 
+  void do_combo_encoding (QComboBox *combo, QString current);
 };
 
 #endif // RESOURCEMANAGER_H
--- a/libgui/src/settings-dialog.cc	Sat Nov 28 14:07:09 2015 -0500
+++ b/libgui/src/settings-dialog.cc	Sun Nov 29 00:01:21 2015 +0100
@@ -376,7 +376,7 @@
   ui->editor_showLineNumbers->setChecked (
     settings->value ("editor/showLineNumbers",true).toBool ());
 
-  init_combo_encoding (settings);
+  resource_manager::combo_encoding (ui->editor_combo_encoding);
 
   default_var = QColor (240, 240, 240);
   QColor setting_color = settings->value ("editor/highlight_current_line_color",
@@ -1022,47 +1022,6 @@
     }
 }
 
-// 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 ();
-
-  // sort and prepend meaningfull text for system's default codec
-#if defined (Q_OS_WIN32)
-  QString def_enc = "SYSTEM";
-#else
-  QString def_enc = "UTF-8";
-#endif
-  qSort (all_codecs);
-  ui->editor_combo_encoding->insertSeparator (0);
-  ui->editor_combo_encoding->insertItem (0, def_enc);
-
-  // get the value from the settings file (system is default)
-  QString encoding = settings->value ("editor/default_encoding",def_enc)
-                               .toString ();
-
-  // 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);
-  int 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	Sat Nov 28 14:07:09 2015 -0500
+++ b/libgui/src/settings-dialog.h	Sun Nov 29 00:01:21 2015 +0100
@@ -64,8 +64,6 @@
 
   void write_changed_settings (bool closing);
 
-  void init_combo_encoding (QSettings *settings);
-
   void read_workspace_colors (QSettings *settings);
   void write_workspace_colors (QSettings *settings);