multi-line edit for text in paramview

This commit is contained in:
itsmattkc
2020-11-16 12:27:59 +11:00
parent 9a1917d160
commit a0fb1981b4
2 changed files with 28 additions and 11 deletions
@@ -34,19 +34,20 @@ NodeParamViewRichText::NodeParamViewRichText(QWidget *parent) :
QHBoxLayout* layout = new QHBoxLayout(this); QHBoxLayout* layout = new QHBoxLayout(this);
layout->setMargin(0); layout->setMargin(0);
line_edit_ = new QLineEdit(); line_edit_ = new QTextEdit();
connect(line_edit_, &QLineEdit::textEdited, this, &NodeParamViewRichText::textEdited); connect(line_edit_, &QTextEdit::textChanged, this, &NodeParamViewRichText::InnerWidgetTextChanged);
layout->addWidget(line_edit_); layout->addWidget(line_edit_);
QPushButton* edit_btn = new QPushButton(); QPushButton* edit_btn = new QPushButton();
edit_btn->setIcon(icon::ToolEdit); edit_btn->setIcon(icon::ToolEdit);
edit_btn->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
layout->addWidget(edit_btn); layout->addWidget(edit_btn);
connect(edit_btn, &QPushButton::clicked, this, &NodeParamViewRichText::ShowRichTextDialog); connect(edit_btn, &QPushButton::clicked, this, &NodeParamViewRichText::ShowRichTextDialog);
} }
void NodeParamViewRichText::ShowRichTextDialog() void NodeParamViewRichText::ShowRichTextDialog()
{ {
RichTextDialog d(line_edit_->text(), this); RichTextDialog d(this->text(), this);
if (d.exec() == QDialog::Accepted) { if (d.exec() == QDialog::Accepted) {
QString s = d.text(); QString s = d.text();
@@ -55,4 +56,9 @@ void NodeParamViewRichText::ShowRichTextDialog()
} }
} }
void NodeParamViewRichText::InnerWidgetTextChanged()
{
emit textEdited(this->text());
}
OLIVE_NAMESPACE_EXIT OLIVE_NAMESPACE_EXIT
@@ -21,7 +21,7 @@
#ifndef NODEPARAMVIEWRICHTEXT_H #ifndef NODEPARAMVIEWRICHTEXT_H
#define NODEPARAMVIEWRICHTEXT_H #define NODEPARAMVIEWRICHTEXT_H
#include <QLineEdit> #include <QTextEdit>
#include <QWidget> #include <QWidget>
#include "common/define.h" #include "common/define.h"
@@ -36,31 +36,42 @@ public:
QString text() const QString text() const
{ {
return line_edit_->text(); return line_edit_->toPlainText().replace('\n', QStringLiteral("<br>"));
} }
public slots: public slots:
void setText(const QString &s) void setText(QString s)
{ {
line_edit_->setText(s); line_edit_->blockSignals(true);
line_edit_->setPlainText(s.replace(QStringLiteral("<br>"), QStringLiteral("\n")));
line_edit_->blockSignals(false);
} }
void setTextPreservingCursor(const QString &s) void setTextPreservingCursor(const QString &s)
{ {
int cursor_pos = line_edit_->cursorPosition(); // Save cursor position
line_edit_->setText(s); int cursor_pos = line_edit_->textCursor().position();
line_edit_->setCursorPosition(cursor_pos);
// Set text
this->setText(s);
// Get new text cursor
QTextCursor c = line_edit_->textCursor();
c.setPosition(cursor_pos);
line_edit_->setTextCursor(c);
} }
signals: signals:
void textEdited(const QString &); void textEdited(const QString &);
private: private:
QLineEdit* line_edit_; QTextEdit* line_edit_;
private slots: private slots:
void ShowRichTextDialog(); void ShowRichTextDialog();
void InnerWidgetTextChanged();
}; };
OLIVE_NAMESPACE_EXIT OLIVE_NAMESPACE_EXIT