瀏覽代碼

ENH: ctkConsole: Extend API adding cursorPosition, cursorColumn and cursorLine

Jean-Christophe Fillion-Robin 8 年之前
父節點
當前提交
4202e73666
共有 3 個文件被更改,包括 84 次插入3 次删除
  1. 30 3
      Applications/ctkSimplePythonShell/ctkSimplePythonShellMain.cpp
  2. 35 0
      Libs/Widgets/ctkConsole.cpp
  3. 19 0
      Libs/Widgets/ctkConsole.h

+ 30 - 3
Applications/ctkSimplePythonShell/ctkSimplePythonShellMain.cpp

@@ -20,6 +20,9 @@
 
 // Qt includes
 #include <QApplication>
+#include <QLabel>
+#include <QMainWindow>
+#include <QStatusBar>
 #include <QTextStream>
 #include <QTimer>
 
@@ -59,6 +62,19 @@ void executeScripts(void * data)
     }
 }
 
+//-----------------------------------------------------------------------------
+void onCursorPositionChanged(void *data)
+{
+  ctkPythonConsole* pythonConsole = reinterpret_cast<ctkPythonConsole*>(data);
+  QMainWindow* mainWindow = qobject_cast<QMainWindow*>(
+        pythonConsole->parentWidget());
+  QLabel * label = mainWindow->statusBar()->findChild<QLabel*>();
+  label->setText(QString("Position %1, Column %2, Line %3")
+                 .arg(pythonConsole->cursorPosition())
+                 .arg(pythonConsole->cursorColumn())
+                 .arg(pythonConsole->cursorLine()));
+}
+
 } // end of anonymous namespace
 
 //-----------------------------------------------------------------------------
@@ -106,9 +122,20 @@ int main(int argc, char** argv)
 
     ctkPythonConsole console;
     console.initialize(&pythonManager);
-    console.setAttribute(Qt::WA_QuitOnClose, true);
-    console.resize(600, 280);
-    console.show();
+
+    QMainWindow mainWindow;
+    mainWindow.setCentralWidget(&console);
+    mainWindow.resize(600, 280);
+    mainWindow.show();
+
+    QLabel cursorPositionLabel;
+    mainWindow.statusBar()->addWidget(&cursorPositionLabel);
+
+    ctkCallback cursorPositionChangedCallback;
+    cursorPositionChangedCallback.setCallbackData(&console);
+    cursorPositionChangedCallback.setCallback(onCursorPositionChanged);
+    QObject::connect(&console, SIGNAL(cursorPositionChanged()),
+                     &cursorPositionChangedCallback, SLOT(invoke()));
 
     console.setProperty("isInteractive", parsedArgs.contains("interactive"));
 

+ 35 - 0
Libs/Widgets/ctkConsole.cpp

@@ -60,6 +60,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <QMimeData>
 #include <QPointer>
 #include <QPushButton>
+#include <QTextBlock>
 #include <QTextCursor>
 #include <QVBoxLayout>
 #include <QScrollBar>
@@ -157,6 +158,8 @@ void ctkConsolePrivate::init()
           SLOT(onScrollBarValueChanged(int)));
   connect(this, SIGNAL(textChanged()), SLOT(onTextChanged()));
   connect(this->RunFileButton, SIGNAL(clicked()), q, SLOT(runFile()));
+  connect(this, SIGNAL(cursorPositionChanged()),
+          q, SIGNAL(cursorPositionChanged()));
 }
 
 //-----------------------------------------------------------------------------
@@ -962,6 +965,38 @@ CTK_GET_CPP(ctkConsole, ctkConsole::EditorHints, editorHints, EditorHints);
 CTK_SET_CPP(ctkConsole, const ctkConsole::EditorHints&, setEditorHints, EditorHints);
 
 //-----------------------------------------------------------------------------
+int ctkConsole::cursorPosition() const
+{
+  Q_D(const ctkConsole);
+  return d->textCursor().position();
+}
+
+//-----------------------------------------------------------------------------
+int ctkConsole::cursorColumn() const
+{
+  Q_D(const ctkConsole);
+  QTextCursor cursor = d->textCursor();
+  cursor.movePosition(QTextCursor::StartOfLine);
+  return d->textCursor().position() - cursor.position();
+}
+
+//-----------------------------------------------------------------------------
+int ctkConsole::cursorLine() const
+{
+  Q_D(const ctkConsole);
+  QTextCursor cursor = d->textCursor();
+  cursor.movePosition(QTextCursor::StartOfLine);
+  int lines = 1;
+  QTextBlock block = cursor.block().previous();
+  while(block.isValid())
+    {
+    lines += block.lineCount();
+    block = block.previous();
+    }
+  return lines;
+}
+
+//-----------------------------------------------------------------------------
 Qt::ScrollBarPolicy ctkConsole::scrollBarPolicy()const
 {
   Q_D(const ctkConsole);

+ 19 - 0
Libs/Widgets/ctkConsole.h

@@ -76,6 +76,9 @@ class CTK_WIDGETS_EXPORT ctkConsole : public QWidget
   Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor)
   Q_PROPERTY(QString ps1 READ ps1 WRITE setPs1)
   Q_PROPERTY(QString ps2 READ ps2 WRITE setPs2)
+  Q_PROPERTY(int cursorPosition READ cursorPosition)
+  Q_PROPERTY(int cursorColumn READ cursorColumn)
+  Q_PROPERTY(int cursorLine READ cursorLine)
   Q_FLAGS(EditorHint EditorHints)
   Q_PROPERTY(EditorHints editorHints READ editorHints WRITE setEditorHints)
   Q_ENUMS(Qt::ScrollBarPolicy)
@@ -184,6 +187,18 @@ public:
   /// Set the string used as secondary prompt
   virtual void setPs2(const QString& newPs2);
 
+  /// Returns the absolute position of the cursor within the console.
+  /// \sa cursorPositionChanged(int)
+  int cursorPosition() const;
+
+  /// Returns the column of the cursor within the console.
+  /// \sa cursorPositionChanged(int)
+  int cursorColumn() const;
+
+  /// Returns the line of the cursor within the console.
+  /// \sa cursorPositionChanged(int)
+  int cursorLine() const;
+
   static QString stdInRedirectCallBack(void * callData);
 
   /// Get the list of shortcuts that trigger the completion options.
@@ -213,6 +228,10 @@ Q_SIGNALS:
   void aboutToExecute(const QString&);
   void executed(const QString&);
 
+  /// This signal is emitted whenever the position of the cursor changed.
+  /// \sa cursorPosition()
+  void cursorPositionChanged();
+
 public Q_SLOTS:
 
   /// Clears the contents of the console