Browse Source

implement text input

Sonnix 6 years ago
parent
commit
435cf5127f
4 changed files with 47 additions and 20 deletions
  1. 14 0
      mainwindow.cpp
  2. 2 5
      mainwindow.h
  3. 23 8
      qspinputbox.cpp
  4. 8 7
      qspinputbox.h

+ 14 - 0
mainwindow.cpp

@@ -570,6 +570,8 @@ void MainWindow::CreateDockWindows()
     addDockWidget(Qt::BottomDockWidgetArea, _inputWidget, Qt::Vertical);
     _inputTextBox = new QspInputBox(this);
     _inputWidget->setWidget(_inputTextBox);
+    connect(_inputTextBox, SIGNAL(textChanged()), this, SLOT(OnInputTextChange()));
+    connect(_inputTextBox, SIGNAL(InputTextEnter()), this, SLOT(OnInputTextEnter()));
 
     splitDockWidget(_actionsWidget, _inputWidget, Qt::Vertical);
 }
@@ -855,3 +857,15 @@ void MainWindow::OnMenu(QAction* action)
 {
     m_menuIndex = action->data().toInt();
 }
+
+void MainWindow::OnInputTextChange()
+{
+    QSPSetInputStrText(qspStringFromQString(_inputTextBox->GetText()));
+}
+
+void MainWindow::OnInputTextEnter()
+{
+    QSPSetInputStrText(qspStringFromQString(_inputTextBox->GetText()));
+    if (!QSPExecUserInput(QSP_TRUE))
+        ShowError();
+}

+ 2 - 5
mainwindow.h

@@ -92,8 +92,6 @@ private:
     void ReCreateGUI();
     void RefreshUI();
     void ApplyFont(const QFont& new_font);
-//    bool ApplyFontSize(int size);
-//    bool ApplyFontName(const wxString& name);
     bool ApplyFontColor(const QColor& color);
     bool ApplyBackColor(const QColor& color);
     bool ApplyLinkColor(const QColor& color);
@@ -162,6 +160,8 @@ private slots:
     void OnObjectChange(int currentRow);
     void OnActionChange(int currentRow);
     void OnMenu(QAction* action);
+    void OnInputTextChange();
+    void OnInputTextEnter();
 
     // Events
 //    void OnInit(); //TODO: add autorun event
@@ -176,9 +176,6 @@ private slots:
 //    void OnToggleCaptions(wxCommandEvent& event);
 //    void OnToggleHotkeys(wxCommandEvent& event);
 //    void OnVolume(wxCommandEvent& event);
-//    void OnAbout(wxCommandEvent& event);
-//    void OnInputTextChange(wxCommandEvent& event);
-//    void OnInputTextEnter(wxCommandEvent& event);
 //    void OnDropFiles(wxDropFilesEvent& event);
 };
 

+ 23 - 8
qspinputbox.cpp

@@ -1,6 +1,6 @@
 #include "qspinputbox.h"
 
-QspInputBox::QspInputBox(QWidget *parent) : QTextEdit(parent)
+QspInputBox::QspInputBox(QWidget *parent) : QPlainTextEdit(parent)
 {
     m_selIndex = -1;
 }
@@ -10,14 +10,29 @@ QspInputBox::~QspInputBox()
 
 }
 
-void QspInputBox::SetText(const QString& text, bool isChangeValue)
+void QspInputBox::SetText(const QString& text)
 {
-    if (m_text != text)
+    bool oldState = blockSignals(true);
+    setPlainText(text);
+    blockSignals(oldState);
+}
+
+QString QspInputBox::GetText()
+{
+    return toPlainText();
+}
+
+void QspInputBox::keyPressEvent(QKeyEvent *event)
+{
+    if ((event->key()==Qt::Key_Return) && (event->modifiers()==Qt::ControlModifier))
+    {
+        appendPlainText("\n");
+        return;
+    }
+    else if (event->key()==Qt::Key_Return)
     {
-        m_text = text;
-        //if (isChangeValue) ChangeValue(m_text); It also marks the control as not-modified which means that IsModified() would return false immediately after the call to ChangeValue().
-        //The insertion point is set to the start of the control (i.e. position 0) by this function.
-        //This functions does not generate the wxEVT_TEXT event but otherwise is identical to SetValue().
-        if (isChangeValue) setText(m_text);
+        emit InputTextEnter();
+        return;
     }
+    QPlainTextEdit::keyPressEvent(event);
 }

+ 8 - 7
qspinputbox.h

@@ -2,7 +2,7 @@
 #define QSPINPUTBOX_H
 
 #include <QWidget>
-#include <QTextEdit>
+#include <QPlainTextEdit>
 #include <QString>
 #include <QStringList>
 
@@ -10,22 +10,23 @@ namespace Ui {
 class QspInputBox;
 }
 
-//TODO: implement
-class QspInputBox : public QTextEdit
+class QspInputBox : public QPlainTextEdit
 {
     Q_OBJECT
 
+signals:
+   void InputTextEnter();
+
 public:
     explicit QspInputBox(QWidget *parent = 0);
     ~QspInputBox();
-
     // Accessors
-    void SetText(const QString& text, bool isChangeValue = true);
-    QString GetText() const { return m_text; }
+    void SetText(const QString& text);
+    QString GetText();
 
 private:
+    void keyPressEvent(QKeyEvent *event);
     // Fields
-    QString m_text;
     QStringList m_strings;
     int m_selIndex;
 };