changeset 20793:f7b0572fff6e

allow to select an encoding when opening a file * file-editor-tab.cc (set_encoding): new slot for changing the encoding; * file-editor-tab.h: new slot for changing the encoding; * file-editor.cc (file_editor): initialize new class variable for the encoding; (request_open_file): add a combo box for the encoding to the open dialog; (handle_combo_enc_current_index): new slot for storing selected encoding; (request_open_files): reset class variable for encoding after loading files; (request_open_file (QString)): set encoding before loading the file; * file-editor.h: new slot for storing selected encoding, new class variable for storing a selected encoding
author Torsten <ttl@justmail.de>
date Thu, 03 Dec 2015 23:02:48 +0100
parents d0991cbd6141
children e870a68742a6
files libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/file-editor-tab.h libgui/src/m-editor/file-editor.cc libgui/src/m-editor/file-editor.h
diffstat 4 files changed, 59 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Thu Dec 03 10:46:28 2015 -0800
+++ b/libgui/src/m-editor/file-editor-tab.cc	Thu Dec 03 23:02:48 2015 +0100
@@ -212,6 +212,18 @@
 }
 
 void
+file_editor_tab::set_encoding (const QString& new_encoding)
+{
+  if (new_encoding.isEmpty ())
+    return;
+
+  _encoding = new_encoding;
+  _enc_indicator->setText (_encoding);
+  if (! _edit_area->text ().isEmpty ())
+    set_modified (true);
+}
+
+void
 file_editor_tab::closeEvent (QCloseEvent *e)
 {
   _cancelled = false;  // prevent unwanted interaction of previous
--- a/libgui/src/m-editor/file-editor-tab.h	Thu Dec 03 10:46:28 2015 -0800
+++ b/libgui/src/m-editor/file-editor-tab.h	Thu Dec 03 23:02:48 2015 +0100
@@ -120,7 +120,10 @@
 
   void set_modified (bool modified = true);
 
+  void set_encoding (const QString& new_encoding);
+
   QString load_file (const QString& fileName);
+
   void new_file (const QString& commands = QString ());
 
   void file_has_changed (const QString& fileName);
--- a/libgui/src/m-editor/file-editor.cc	Thu Dec 03 10:46:28 2015 -0800
+++ b/libgui/src/m-editor/file-editor.cc	Thu Dec 03 23:02:48 2015 +0100
@@ -57,8 +57,9 @@
   construct ();
 
   setVisible (false);
+  setAcceptDrops(true);
 
-  setAcceptDrops(true);
+  _file_encoding = QString ();  // for selecting an encoding in open dialog
 }
 
 file_editor::~file_editor (void)
@@ -230,6 +231,33 @@
   // it had/has no effect on Windows, though)
   fileDialog->setOption(QFileDialog::DontUseNativeDialog, true);
 
+  // define a new grid layout with the extra elements
+  QGridLayout *extra = new QGridLayout (fileDialog);
+  QFrame *separator = new QFrame (fileDialog);
+  separator->setFrameShape (QFrame::HLine);   // horizontal line as separator
+  separator->setFrameStyle (QFrame::Sunken);
+
+  // combo box for encoding
+  QLabel *label_enc = new QLabel (tr ("File Encoding:"));
+  QComboBox *combo_enc = new QComboBox ();
+  resource_manager::combo_encoding (combo_enc);
+
+  // track changes in the combo boxes
+  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 (label_enc,1,0);
+  extra->addWidget (combo_enc,1,1);
+  extra->addItem   (new QSpacerItem (1,20,QSizePolicy::Expanding,
+                                          QSizePolicy::Fixed), 1,2);
+
+  // and add the extra grid layout to the dialog's layout
+  QGridLayout *dialog_layout = dynamic_cast<QGridLayout*> (fileDialog->layout ());
+  dialog_layout->addLayout (extra,dialog_layout->rowCount (),0,
+                                  1,dialog_layout->columnCount ());
+
   fileDialog->setAcceptMode (QFileDialog::AcceptOpen);
   fileDialog->setViewMode (QFileDialog::Detail);
   fileDialog->setFileMode (QFileDialog::ExistingFiles);
@@ -243,6 +271,12 @@
   fileDialog->show ();
 }
 
+void
+file_editor::handle_combo_enc_current_index (QString new_encoding)
+{
+  _file_encoding = new_encoding;
+}
+
 // Check whether this file is already open in the editor.
 QWidget *
 file_editor::find_tab_widget (const QString& file) const
@@ -321,11 +355,15 @@
   return false;
 }
 
+// The following slot is called after files have been selected in the
+// open file dialog, possibly with a new selected encoding. After loading
+// all files, _file_encoding is reset.
 void
 file_editor::request_open_files (const QStringList& open_file_names)
 {
   for (int i = 0; i < open_file_names.count (); i++)
     request_open_file (open_file_names.at (i));
+  _file_encoding = QString ();  // reset: no special encoding
 }
 
 void
@@ -377,6 +415,7 @@
           file_editor_tab *fileEditorTab = new file_editor_tab ();
           if (fileEditorTab)
             {
+              fileEditorTab->set_encoding (_file_encoding);
               QString result = fileEditorTab->load_file (openFileName);
               if (result == "")
                 {
--- a/libgui/src/m-editor/file-editor.h	Thu Dec 03 10:46:28 2015 -0800
+++ b/libgui/src/m-editor/file-editor.h	Thu Dec 03 23:02:48 2015 +0100
@@ -236,6 +236,8 @@
   void request_styles_preferences (bool);
   void restore_create_file_setting ();
 
+  void handle_combo_enc_current_index (QString new_encoding);
+
   void show_line_numbers (bool);
   void show_white_space (bool);
   void show_eol_chars (bool);
@@ -377,6 +379,8 @@
 
   int _marker_breakpoint;
 
+  QString _file_encoding;
+
   enum { MaxMRUFiles = 10 };
   QMenu *_mru_file_menu;
   QAction *_mru_file_actions[MaxMRUFiles];