Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1.     // Set the cursor to the beginning of the first block.
  2.     textCursor().setPosition(textCursor().block().position(), QTextCursor::KeepAnchor);
  3.  
  4.     // Grab the start of the selection
  5.     int start_pos = textCursor().selectionStart();
  6.  
  7.     // Grab the selected text
  8.     QString text = textCursor().selection().toPlainText();
  9.  
  10.     if(indent)
  11.     {
  12.         if(m_indent_use_spaces)
  13.         {
  14.             text.prepend(QString(m_tab_size, ' '));
  15.             text.replace("\n", QString(m_tab_size, ' ').prepend("\n"));
  16.         }
  17.         else
  18.         {
  19.             text.prepend("\t");
  20.             text.replace("\n", "\n\t");
  21.         }
  22.     }
  23.     else
  24.     {
  25.         if(m_indent_use_spaces)
  26.         {
  27.             int num_remove = 0;
  28.             while(num_remove < text.length() && num_remove < m_tab_size && text[num_remove] == ' ')
  29.                 ++num_remove;
  30.  
  31.             text.remove(0, num_remove);
  32.  
  33.             text.replace(QRegExp(tr("\n {0,%1}").arg(m_tab_size)), "\n");
  34.         }
  35.         else
  36.         {
  37.             if(text.startsWith("\t"))
  38.                 text.remove(0, 1);
  39.  
  40.             text.replace("\n\t", "\n");
  41.         }
  42.     }
  43.  
  44.     textCursor().removeSelectedText();
  45.     textCursor().insertText(text);
  46.  
  47.     // We need to reset the selection
  48.     textCursor().setPosition(start_pos);
  49.     textCursor().setPosition(start_pos + text.length(), QTextCursor::KeepAnchor);
  50.  
  51.     // These numbers seem to be correct
  52.     qDebug(tr("%1, %2").arg(start_pos).arg(start_pos + text.length()).toUtf8().data());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement