diff libgui/src/m-editor/file-editor-tab.cc @ 25895:bb0c58796275

allow to change the encoding when editor detects decoding errors (bug #54607) * file-editor-tab.cc (load_file): set tab contents to read only during user dialog on decoding errors, add button for changing encoding and reloading the file; (handle_decode_warning_answer): add dialog box for changing the encoding and reloading the file, reload file if desired by the user; (handle_current_enc_changed): save new encoding if a new encoding is selected in the dialogs combo box * file-editor-tab.h: update signal for opening a file together with the desired encoding, new slot for selecting the encoding in a combo box * file-editor.cc (add_file_editor_tab): update the connection between new file editor tab signal for opening a file with desired encoding and the related slot
author Torsten <mttl@mailbox.org>
date Mon, 24 Sep 2018 21:45:36 +0200
parents cfdc768b661c
children b33d4fbce33e
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Sat Sep 22 22:29:15 2018 +0200
+++ b/libgui/src/m-editor/file-editor-tab.cc	Mon Sep 24 21:45:36 2018 +0200
@@ -1751,6 +1751,9 @@
     // Decoding with invalid characters?
     if (st.invalidChars > 0)
       {
+        // Set read only
+        _edit_area->setReadOnly (true);
+
         // Message box for user decision
         QString msg = tr ("There were problems reading the file\n"
                           "%1\n"
@@ -1763,7 +1766,7 @@
         msg_box->setText (msg);
         msg_box->setWindowTitle (tr ("Octave Editor"));
         msg_box->addButton (tr ("&Edit anyway"), QMessageBox::YesRole);
-        //msg_box->addButton (tr ("&Change encoding"), QMessageBox::AcceptRole);
+        msg_box->addButton (tr ("Chan&ge encoding"), QMessageBox::AcceptRole);
         msg_box->addButton (tr ("&Close"), QMessageBox::RejectRole);
 
         connect (msg_box, SIGNAL (buttonClicked (QAbstractButton *)),
@@ -1812,7 +1815,60 @@
     QString txt = btn->text ();
 
     if (txt == tr ("&Close"))
-      close ();
+      {
+        // Just close the file
+        close ();
+        return;
+      }
+
+    if (txt == tr ("Chan&ge encoding"))
+      {
+        // Dialog for reloading the file with another encoding
+        QDialog dlg;
+        dlg.setWindowTitle (tr ("Select new default encoding"));
+
+        QLabel *text
+          = new QLabel (tr ("Please select a new encoding\n"
+                            "for reloading the current file.\n\n"
+                            "This does not change the default encoding.\n"));
+
+        QComboBox *enc_combo = new QComboBox ();
+        resource_manager::combo_encoding (enc_combo);
+        _new_encoding = enc_combo->currentText ();
+        connect (enc_combo, SIGNAL (currentTextChanged (const QString&)),
+                 this , SLOT (handle_current_enc_changed (const QString&)));
+
+        QDialogButtonBox *buttons
+          = new QDialogButtonBox (QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
+                                  Qt::Horizontal);
+        connect (buttons, SIGNAL (accepted ()), &dlg, SLOT (accept ()));
+        connect (buttons, SIGNAL (rejected ()), &dlg, SLOT (reject ()));
+
+        QGridLayout *main_layout = new QGridLayout;
+        main_layout->setSizeConstraint (QLayout::SetFixedSize);
+        main_layout->addWidget (text, 0, 0);
+        main_layout->addWidget (enc_combo, 1, 0);
+        main_layout->addWidget (buttons, 2, 0);
+        dlg.setLayout (main_layout);
+
+        int answer = dlg.exec ();
+
+        if (answer == QDialog::Accepted)
+          {
+            // Reload the file with new encoding but using the same tab
+            QString reload_file_name = _file_name;  // store file name
+            _file_name = "";  // force reuse of this tab when opening a new file
+            emit request_open_file (reload_file_name, _new_encoding);
+          }
+      }
+
+    // Continue editing, set writable again
+    _edit_area->setReadOnly (false);
+  }
+
+  void file_editor_tab::handle_current_enc_changed (const QString& enc)
+  {
+    _new_encoding = enc;
   }
 
   QsciScintilla::EolMode file_editor_tab::detect_eol_mode (void)