diff --git a/app/node/generator/text/textv3.cpp b/app/node/generator/text/textv3.cpp index 958bf94db..a723e67aa 100644 --- a/app/node/generator/text/textv3.cpp +++ b/app/node/generator/text/textv3.cpp @@ -24,9 +24,10 @@ #include #include -#include "common/functiontimer.h" #include "common/html.h" +#include "core.h" #include "node/project/project.h" +#include "widget/nodeparamview/nodeparamviewundo.h" namespace olive { @@ -44,14 +45,15 @@ const QString TextGeneratorV3::kUseArgsInput = QStringLiteral("use_args_in"); const QString TextGeneratorV3::kArgsInput = QStringLiteral("args_in"); TextGeneratorV3::TextGeneratorV3() : - ShapeNodeBase(false) + ShapeNodeBase(false), + dont_emit_valign_(false) { AddInput(kTextInput, NodeValue::kText, QStringLiteral("

%1

").arg(tr("Sample Text"))); SetInputProperty(kTextInput, QStringLiteral("vieweronly"), true); SetStandardValue(kSizeInput, QVector2D(400, 300)); - AddInput(kVerticalAlignmentInput, NodeValue::kCombo); + AddInput(kVerticalAlignmentInput, NodeValue::kCombo, InputFlags(kInputFlagHidden | kInputFlagStatic)); AddInput(kUseArgsInput, NodeValue::kBoolean, true, InputFlags(kInputFlagHidden | kInputFlagStatic)); @@ -181,6 +183,33 @@ void TextGeneratorV3::UpdateGizmoPositions(const NodeValueRow &row, const NodeGl text_gizmo_->SetHtml(row[kTextInput].toString()); } +Qt::Alignment TextGeneratorV3::GetQtAlignmentFromOurs(VerticalAlignment v) +{ + switch (v) { + case kVAlignTop: + return Qt::AlignTop; + case kVAlignMiddle: + return Qt::AlignVCenter; + case kVAlignBottom: + return Qt::AlignBottom; + } + return Qt::Alignment(); +} + +TextGeneratorV3::VerticalAlignment TextGeneratorV3::GetOurAlignmentFromQts(Qt::Alignment v) +{ + switch (v) { + case Qt::AlignTop: + return kVAlignTop; + case Qt::AlignVCenter: + return kVAlignMiddle; + case Qt::AlignBottom: + return kVAlignBottom; + } + + return kVAlignTop; +} + QString TextGeneratorV3::FormatString(const QString &input, const QStringList &args) { QString output; @@ -219,14 +248,32 @@ QString TextGeneratorV3::FormatString(const QString &input, const QStringList &a return output; } +void TextGeneratorV3::InputValueChangedEvent(const QString &input, int element) +{ + if (input == kVerticalAlignmentInput && !dont_emit_valign_) { + text_gizmo_->SetVerticalAlignment(GetQtAlignmentFromOurs(GetVerticalAlignment())); + } + + super::InputValueChangedEvent(input, element); +} + void TextGeneratorV3::GizmoActivated() { SetStandardValue(kUseArgsInput, false); + connect(text_gizmo_, &TextGizmo::VerticalAlignmentChanged, this, &TextGeneratorV3::SetVerticalAlignmentUndoable); + dont_emit_valign_ = true; } void TextGeneratorV3::GizmoDeactivated() { SetStandardValue(kUseArgsInput, true); + disconnect(text_gizmo_, &TextGizmo::VerticalAlignmentChanged, this, &TextGeneratorV3::SetVerticalAlignmentUndoable); + dont_emit_valign_ = true; +} + +void TextGeneratorV3::SetVerticalAlignmentUndoable(Qt::Alignment a) +{ + Core::instance()->undo_stack()->push(new NodeParamSetStandardValueCommand(NodeInput(this, kVerticalAlignmentInput), GetOurAlignmentFromQts(a))); } } diff --git a/app/node/generator/text/textv3.h b/app/node/generator/text/textv3.h index a815fbb43..b7d6a5674 100644 --- a/app/node/generator/text/textv3.h +++ b/app/node/generator/text/textv3.h @@ -54,6 +54,14 @@ public: kVAlignBottom }; + VerticalAlignment GetVerticalAlignment() const + { + return static_cast(GetStandardValue(kVerticalAlignmentInput).toInt()); + } + + static Qt::Alignment GetQtAlignmentFromOurs(VerticalAlignment v); + static VerticalAlignment GetOurAlignmentFromQts(Qt::Alignment v); + static const QString kTextInput; static const QString kVerticalAlignmentInput; static const QString kUseArgsInput; @@ -61,12 +69,18 @@ public: static QString FormatString(const QString &input, const QStringList &args); +protected: + virtual void InputValueChangedEvent(const QString &input, int element) override; + private: TextGizmo *text_gizmo_; + bool dont_emit_valign_; + private slots: void GizmoActivated(); void GizmoDeactivated(); + void SetVerticalAlignmentUndoable(Qt::Alignment a); }; diff --git a/app/node/gizmo/text.cpp b/app/node/gizmo/text.cpp index 6e2b97c5b..6d6816948 100644 --- a/app/node/gizmo/text.cpp +++ b/app/node/gizmo/text.cpp @@ -26,7 +26,8 @@ namespace olive { TextGizmo::TextGizmo(QObject *parent) - : NodeGizmo{parent} + : NodeGizmo{parent}, + valign_(Qt::AlignTop) { } diff --git a/app/node/gizmo/text.h b/app/node/gizmo/text.h index 30ea62f7f..5c3218671 100644 --- a/app/node/gizmo/text.h +++ b/app/node/gizmo/text.h @@ -42,9 +42,21 @@ public: void UpdateInputHtml(const QString &s, const rational &time); + Qt::Alignment GetVerticalAlignment() const + { + return valign_; + } + + void SetVerticalAlignment(Qt::Alignment va) + { + valign_ = va; + emit VerticalAlignmentChanged(valign_); + } + signals: void Activated(); void Deactivated(); + void VerticalAlignmentChanged(Qt::Alignment va); private: QRectF rect_; @@ -53,6 +65,8 @@ private: NodeKeyframeTrackReference input_; + Qt::Alignment valign_; + }; } diff --git a/app/widget/viewer/viewerdisplay.cpp b/app/widget/viewer/viewerdisplay.cpp index 05178a69f..72d7a0150 100644 --- a/app/widget/viewer/viewerdisplay.cpp +++ b/app/widget/viewer/viewerdisplay.cpp @@ -455,7 +455,7 @@ void ViewerDisplayWidget::OnPaint() pm.fill(Qt::transparent); QPainter pixp(&pm); - text_edit_->Paint(&pixp); + text_edit_->Paint(&pixp, active_text_gizmo_->GetVerticalAlignment()); p.drawPixmap(text_edit_pos_, pm); } @@ -696,6 +696,7 @@ NodeGizmo *ViewerDisplayWidget::TryGizmoPress(const NodeValueRow &row, const QPo void ViewerDisplayWidget::OpenTextGizmo(TextGizmo *text, QMouseEvent *event) { + active_text_gizmo_ = text; text_transform_ = GenerateGizmoTransform(); // Create text editor @@ -736,6 +737,9 @@ void ViewerDisplayWidget::OpenTextGizmo(TextGizmo *text, QMouseEvent *event) // Create toolbar text_toolbar_ = new ViewerTextEditorToolBar(text_edit_); text_toolbar_->setWindowFlags(Qt::Window | Qt::WindowStaysOnTopHint); + connect(text_toolbar_, &ViewerTextEditorToolBar::VerticalAlignmentChanged, text, &TextGizmo::SetVerticalAlignment); + connect(text, &TextGizmo::VerticalAlignmentChanged, text_toolbar_, &ViewerTextEditorToolBar::SetVerticalAlignment); + text_toolbar_->SetVerticalAlignment(text->GetVerticalAlignment()); text_edit_->ConnectToolBar(text_toolbar_); text_transform_inverted_ = text_transform_.inverted(); @@ -1122,6 +1126,18 @@ bool ViewerDisplayWidget::ForwardMouseEventToTextEdit(QMouseEvent *event, bool c } } + switch (active_text_gizmo_->GetVerticalAlignment()) { + case Qt::AlignTop: + // Do nothing + break; + case Qt::AlignVCenter: + local_pos.setY(local_pos.y() - text_edit_->height()/2 + text_edit_->document()->size().height()/2); + break; + case Qt::AlignBottom: + local_pos.setY(local_pos.y() - text_edit_->height() + text_edit_->document()->size().height()); + break; + } + event->setLocalPos(local_pos); return ForwardEventToTextEdit(event); } diff --git a/app/widget/viewer/viewerdisplay.h b/app/widget/viewer/viewerdisplay.h index 4507d96c0..5e7c8dbe2 100644 --- a/app/widget/viewer/viewerdisplay.h +++ b/app/widget/viewer/viewerdisplay.h @@ -400,6 +400,7 @@ private: bool queue_starved_; + TextGizmo *active_text_gizmo_; QPointF text_edit_pos_; ViewerTextEditor *text_edit_; ViewerTextEditorToolBar *text_toolbar_; diff --git a/app/widget/viewer/viewertexteditor.cpp b/app/widget/viewer/viewertexteditor.cpp index 0fd51ef8c..c8155879b 100644 --- a/app/widget/viewer/viewertexteditor.cpp +++ b/app/widget/viewer/viewertexteditor.cpp @@ -97,12 +97,12 @@ void ViewerTextEditor::ConnectToolBar(ViewerTextEditorToolBar *toolbar) toolbars_.append(toolbar); } -void ViewerTextEditor::Paint(QPainter *p, const QRect &clip) +void ViewerTextEditor::Paint(QPainter *p, Qt::Alignment valign) { QAbstractTextDocumentLayout::PaintContext ctx; - if (clip.isValid()) - p->setClipRect(clip, Qt::IntersectClip); + QRect clip = this->rect(); + p->setClipRect(clip, Qt::IntersectClip); ctx.clip = clip; ctx.cursorPosition = this->textCursor().position(); @@ -127,6 +127,17 @@ void ViewerTextEditor::Paint(QPainter *p, const QRect &clip) if (transparent_clone_) { transparent_clone_->documentLayout()->draw(p, ctx); + + switch (valign) { + case Qt::AlignTop: + // Do nothing + break; + case Qt::AlignVCenter: + p->translate(0, clip.height()/2-document()->size().height()/2); + break; + case Qt::AlignBottom: + p->translate(0, clip.height()-document()->size().height()); + break; } } @@ -477,6 +488,13 @@ void ViewerTextEditorToolBar::SetAlignment(Qt::Alignment a) align_justify_btn_->setChecked(a == Qt::AlignJustify); } +void ViewerTextEditorToolBar::SetVerticalAlignment(Qt::Alignment a) +{ + align_top_btn_->setChecked(a == Qt::AlignTop); + align_middle_btn_->setChecked(a == Qt::AlignVCenter); + align_bottom_btn_->setChecked(a == Qt::AlignBottom); +} + void ViewerTextEditorToolBar::SetColor(const QColor &c) { color_btn_->setProperty("color", c); diff --git a/app/widget/viewer/viewertexteditor.h b/app/widget/viewer/viewertexteditor.h index 4abd300b4..8bef901b9 100644 --- a/app/widget/viewer/viewertexteditor.h +++ b/app/widget/viewer/viewertexteditor.h @@ -68,6 +68,7 @@ public slots: void SetUnderline(bool e) { underline_btn_->setChecked(e); } void SetStrikethrough(bool e) { strikethrough_btn_->setChecked(e); } void SetAlignment(Qt::Alignment a); + void SetVerticalAlignment(Qt::Alignment a); void SetColor(const QColor &c); void SetSmallCaps(bool e) { small_caps_btn_->setChecked(e); } void SetStretch(int i) { stretch_slider_->SetValue(i); } @@ -152,7 +153,7 @@ public: void SetListenToFocusEvents(bool e) { listen_to_focus_events_ = e; } - void Paint(QPainter *p, const QRect &clip = QRect()); + void Paint(QPainter *p, Qt::Alignment valign); virtual void dragEnterEvent(QDragEnterEvent *e) override { return QTextEdit::dragEnterEvent(e); } virtual void dragMoveEvent(QDragMoveEvent *e) override { return QTextEdit::dragMoveEvent(e); }