changeset 13654:c67f7d390a1a

editor: comment/uncomment selected text
author ttl <ttl@justmail.de>
date Thu, 25 Aug 2011 20:12:31 +0200
parents 0ee1b81a0538
children 7df7650492e8
files gui/src/FileEditorMdiSubWindow.cpp gui/src/FileEditorMdiSubWindow.h
diffstat 2 files changed, 52 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/gui/src/FileEditorMdiSubWindow.cpp	Wed Aug 24 00:16:53 2011 +0200
+++ b/gui/src/FileEditorMdiSubWindow.cpp	Thu Aug 25 20:12:31 2011 +0200
@@ -247,6 +247,45 @@
   //m_terminalEmulation->setFocus ();
 }
 
+
+// (un)comment selected text
+void
+FileEditorMdiSubWindow::commentSelectedText ()
+{
+  doCommentSelectedText (true);
+}
+void
+FileEditorMdiSubWindow::uncommentSelectedText ()
+{
+  doCommentSelectedText (false);
+}
+void
+FileEditorMdiSubWindow::doCommentSelectedText (bool comment)
+{
+  if ( m_editor->hasSelectedText() )
+    {
+      int lineFrom, lineTo, colFrom, colTo, i;
+      m_editor->getSelection (&lineFrom,&colFrom,&lineTo,&colTo);
+      if ( colTo == 0 )  // the beginning of last line is not selected
+        lineTo--;        // stop at line above
+      for ( i=lineFrom; i<=lineTo; i++ )
+        {
+          if ( comment )
+            m_editor->insertAt("%",i,0);
+          else
+            {
+              QString line(m_editor->text(i));
+              if ( line.startsWith("%") )
+                {
+                  m_editor->setSelection(i,0,i,1);
+                  m_editor->removeSelectedText();
+                }
+            }
+        }
+    }
+}
+
+
 // remove bookmarks
 void
 FileEditorMdiSubWindow::removeBookmark ()
@@ -414,7 +453,9 @@
   QAction *nextBookmarkAction = new QAction (tr("&Next Bookmark"),m_toolBar);
   QAction *prevBookmarkAction = new QAction (tr("Pre&vious Bookmark"),m_toolBar);
   QAction *toggleBookmarkAction = new QAction (tr("Toggle &Bookmark"),m_toolBar);
-  QAction *removeBookmarkAction = new QAction (tr("&Remove All &Bookmarks"),m_toolBar);
+  QAction *removeBookmarkAction = new QAction (tr("&Remove All Bookmarks"),m_toolBar);
+  QAction *commentSelectedAction = new QAction (tr("&Comment Selected Text"),m_toolBar);
+  QAction *uncommentSelectedAction = new QAction (tr("&Uncomment Selected Text"),m_toolBar);
   QAction *runAction = new QAction (
         QIcon::fromTheme("media-play",style->standardIcon (QStyle::SP_MediaPlay)),
         tr("&Run File"), m_toolBar);
@@ -438,6 +479,8 @@
   nextBookmarkAction->setShortcut(Qt::Key_F2);
   prevBookmarkAction->setShortcut(Qt::SHIFT + Qt::Key_F2);
   toggleBookmarkAction->setShortcut(Qt::Key_F7);
+  commentSelectedAction->setShortcut(Qt::CTRL + Qt::Key_T);
+  uncommentSelectedAction->setShortcut(Qt::CTRL + Qt::Key_U);
 
   // toolbar
   m_toolBar->setIconSize(QSize(16,16)); // smaller icons (make configurable in user settings?)
@@ -472,6 +515,9 @@
   editMenu->addAction(m_cutAction);
   editMenu->addAction(pasteAction);
   editMenu->addSeparator();
+  editMenu->addAction(commentSelectedAction);
+  editMenu->addAction(uncommentSelectedAction);
+  editMenu->addSeparator();
   editMenu->addAction(toggleBookmarkAction);
   editMenu->addAction(nextBookmarkAction);
   editMenu->addAction(prevBookmarkAction);
@@ -505,6 +551,8 @@
   connect (nextBookmarkAction, SIGNAL (triggered ()), this, SLOT (nextBookmark ()));
   connect (prevBookmarkAction, SIGNAL (triggered ()), this, SLOT (prevBookmark ()));
   connect (removeBookmarkAction, SIGNAL (triggered ()), this, SLOT (removeBookmark ()));
+  connect (commentSelectedAction, SIGNAL (triggered ()), this, SLOT (commentSelectedText ()));
+  connect (uncommentSelectedAction, SIGNAL (triggered ()), this, SLOT (uncommentSelectedText ()));
 
   // TODO: Do we still need tool tips in the status bar? Tool tips are now
   //       shown directly at the theme icons
--- a/gui/src/FileEditorMdiSubWindow.h	Wed Aug 24 00:16:53 2011 +0200
+++ b/gui/src/FileEditorMdiSubWindow.h	Thu Aug 25 20:12:31 2011 +0200
@@ -69,6 +69,7 @@
 private:
   int checkFileModified (QString msg);
   void construct ();
+  void doCommentSelectedText (bool comment);
   QMenuBar *m_menuBar;
   QToolBar *m_toolBar;
   QsciScintilla *m_editor;
@@ -89,6 +90,8 @@
   void toggleBookmark ();
   void nextBookmark();
   void prevBookmark();
+  void commentSelectedText();
+  void uncommentSelectedText();
 
 };