changeset 23181:78f04a9dfeee

allow line breaking in the editor (bug #41555) * file-editor-tab.cc (file_editor_tab): connect scintillas signal for a new character with the new slot handle_char_added; (notice_settings): get the users settings on breaking lines; (handle_char_added): new slot for added characters, checking for adding line breaks if desired * file-editor-tab.h: new slot handle_char_added, new class variable _line_break * settings-dialog.cc (settings_dialog): initialize checkbox for line breaking with the state read from the settings file; (write_changed_settings): write the current state of the checkbox for line breaking into the settings file * settings-dialog.ui: enable the checkbox for automatic line breaks
author Torsten <mttl@mailbox.org>
date Mon, 13 Feb 2017 18:53:01 +0100
parents 7520aab218a0
children 8a37443f1a7b
files libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/file-editor-tab.h libgui/src/settings-dialog.cc libgui/src/settings-dialog.ui
diffstat 4 files changed, 78 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Sun Feb 12 21:12:54 2017 -0600
+++ b/libgui/src/m-editor/file-editor-tab.cc	Mon Feb 13 18:53:01 2017 +0100
@@ -112,6 +112,9 @@
   connect (_edit_area, SIGNAL (cursorPositionChanged (int, int)),
            this, SLOT (handle_cursor_moved (int,int)));
 
+  connect (_edit_area, SIGNAL (SCN_CHARADDED (int)),
+           this, SLOT (handle_char_added (int)));
+
   connect (_edit_area, SIGNAL (linesChanged ()),
            this, SLOT (handle_lines_changed ()));
 
@@ -2351,21 +2354,27 @@
   _long_title = settings->value ("editor/longWindowTitle", false).toBool ();
   update_window_title (_edit_area->isModified ());
 
-  _edit_area->setEdgeColumn (
-              settings->value ("editor/long_line_column",80).toInt ());
+  int line_length = settings->value ("editor/long_line_column",80).toInt ();
+  _edit_area->setEdgeColumn (line_length);
   if (settings->value ("editor/long_line_marker",true).toBool ())
     _edit_area->setEdgeMode (QsciScintilla::EdgeLine);
   else
     _edit_area->setEdgeMode (QsciScintilla::EdgeNone);
 
-  // line wrappping
+  // line wrappping and breaking
   _edit_area->setWrapVisualFlags (QsciScintilla::WrapFlagByBorder);
   _edit_area->setWrapIndentMode (QsciScintilla::WrapIndentSame);
+
   if (settings->value ("editor/wrap_lines",false).toBool ())
     _edit_area->setWrapMode (QsciScintilla::WrapWord);
   else
     _edit_area->setWrapMode (QsciScintilla::WrapNone);
 
+  if (settings->value ("editor/break_lines",false).toBool ())
+    _line_break = line_length;
+  else
+    _line_break = 0;
+
   // reload changed files
   _always_reload_changed_files =
         settings->value ("editor/always_reload_changed_files",false).toBool ();
@@ -2653,6 +2662,53 @@
   _col_indicator->setNum (col+1);
 }
 
+// Slot that is entered each time a new character was typed.
+// It is used for handling line breaking if this is desired.
+void
+file_editor_tab::handle_char_added (int character)
+{
+  if (_line_break)
+  {
+    // if line breaking is desired, get the current line and column
+    int line, col;
+    _edit_area->getCursorPosition (&line, &col);
+
+    // immediately return if line has not reached the max. line length
+    if (col < _line_break)
+      return;
+
+    if (character == ' ' || character == '\t')
+      {
+        // the new character is space or tab, break already here
+        _edit_area->insertAt (QString ("\n"), line, col);
+      }
+    else
+      {
+        // search backward for the first space or tab
+        int col_space = col - 1;
+        int c = 0, pos;
+
+        while (c != ' ' && c != '\t' && col_space-- > 0)
+          {
+            pos = _edit_area->positionFromLineIndex (line, col_space);
+            c = _edit_area->SendScintilla (QsciScintillaBase::SCI_GETCHARAT,
+                                           pos);
+          }
+
+        // if a space or tab was found, break after that char;
+        // otherwise break at cursor position
+        int col_newline = col - 1;
+        if (c == ' ' || c == '\t')
+          col_newline = col_space + 1;
+        // insert a newline char for breaking the line
+        _edit_area->insertAt (QString ("\n"), line, col_newline);
+      }
+
+    // automatically indent new line to the indentation of previous line
+    _edit_area->setIndentation (line + 1, _edit_area->indentation (line));
+  }
+}
+
 void
 file_editor_tab::do_smart_indent ()
 {
--- a/libgui/src/m-editor/file-editor-tab.h	Sun Feb 12 21:12:54 2017 -0600
+++ b/libgui/src/m-editor/file-editor-tab.h	Mon Feb 13 18:53:01 2017 +0100
@@ -215,6 +215,7 @@
   void auto_margin_width ();
 
   void handle_cursor_moved (int line, int col);
+  void handle_char_added (int character);
   void handle_lines_changed (void);
 
 private:
@@ -299,6 +300,7 @@
 
   static bool _cancelled;
 
+  int _line_break;
   int _line;
   int _col;
   bool _lines_changed;
--- a/libgui/src/settings-dialog.cc	Sun Feb 12 21:12:54 2017 -0600
+++ b/libgui/src/settings-dialog.cc	Mon Feb 13 18:53:01 2017 +0100
@@ -403,6 +403,8 @@
     settings->value ("editor/long_line_marker",true).toBool ());
   ui->editor_long_line_column->setValue (
     settings->value ("editor/long_line_column",80).toInt ());
+  ui->editor_break_checkbox->setChecked (
+    settings->value ("editor/break_lines",false).toBool ());
   ui->editor_wrap_checkbox->setChecked (
     settings->value ("editor/wrap_lines",false).toBool ());
   ui->cb_edit_status_bar->setChecked (
@@ -788,6 +790,8 @@
                       ui->editor_long_line_marker->isChecked ());
   settings->setValue ("editor/long_line_column",
                       ui->editor_long_line_column->value ());
+  settings->setValue ("editor/break_lines",
+                      ui->editor_break_checkbox->isChecked ());
   settings->setValue ("editor/wrap_lines",
                       ui->editor_wrap_checkbox->isChecked ());
   settings->setValue ("editor/code_folding",
--- a/libgui/src/settings-dialog.ui	Sun Feb 12 21:12:54 2017 -0600
+++ b/libgui/src/settings-dialog.ui	Mon Feb 13 18:53:01 2017 +0100
@@ -52,8 +52,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>573</height>
+            <width>570</width>
+            <height>377</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_17">
@@ -1042,7 +1042,7 @@
                 <item row="1" column="5">
                  <widget class="QCheckBox" name="editor_break_checkbox">
                   <property name="enabled">
-                   <bool>false</bool>
+                   <bool>true</bool>
                   </property>
                   <property name="sizePolicy">
                    <sizepolicy hsizetype="Minimum" vsizetype="Preferred">
@@ -1644,8 +1644,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>573</height>
+            <width>488</width>
+            <height>230</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_7">
@@ -1932,8 +1932,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>573</height>
+            <width>474</width>
+            <height>196</height>
            </rect>
           </property>
           <layout class="QGridLayout" name="gridLayout_8">
@@ -2076,8 +2076,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>573</height>
+            <width>200</width>
+            <height>77</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_19">
@@ -2145,8 +2145,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>573</height>
+            <width>364</width>
+            <height>210</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_25">
@@ -2344,8 +2344,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>658</width>
-            <height>573</height>
+            <width>529</width>
+            <height>199</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_20">