Skip to content

Commit

Permalink
viewer: allow resizing while text gizmo is active
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmattkc committed Feb 22, 2023
1 parent 060c3b0 commit feb7d57
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 35 deletions.
12 changes: 12 additions & 0 deletions app/node/gizmo/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ TextGizmo::TextGizmo(QObject *parent)

}

void TextGizmo::SetRect(const QRectF &r)
{
rect_ = r;
emit RectChanged(rect_);
}

void TextGizmo::UpdateInputHtml(const QString &s, const rational &time)
{
if (input_.IsValid()) {
Expand All @@ -41,4 +47,10 @@ void TextGizmo::UpdateInputHtml(const QString &s, const rational &time)
}
}

void TextGizmo::SetVerticalAlignment(Qt::Alignment va)
{
valign_ = va;
emit VerticalAlignmentChanged(valign_);
}

}
15 changes: 4 additions & 11 deletions app/node/gizmo/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TextGizmo : public NodeGizmo
explicit TextGizmo(QObject *parent = nullptr);

const QRectF &GetRect() const { return rect_; }
void SetRect(const QRectF &r) { rect_ = r; }
void SetRect(const QRectF &r);

const QString &GetHtml() const { return text_; }
void SetHtml(const QString &t) { text_ = t; }
Expand All @@ -42,21 +42,14 @@ class TextGizmo : public NodeGizmo

void UpdateInputHtml(const QString &s, const rational &time);

Qt::Alignment GetVerticalAlignment() const
{
return valign_;
}

void SetVerticalAlignment(Qt::Alignment va)
{
valign_ = va;
emit VerticalAlignmentChanged(valign_);
}
Qt::Alignment GetVerticalAlignment() const { return valign_; }
void SetVerticalAlignment(Qt::Alignment va);

signals:
void Activated();
void Deactivated();
void VerticalAlignmentChanged(Qt::Alignment va);
void RectChanged(const QRectF &r);

private:
QRectF rect_;
Expand Down
70 changes: 46 additions & 24 deletions app/widget/viewer/viewerdisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,10 @@ QTransform ViewerDisplayWidget::GenerateGizmoTransform(NodeTraverser &gt, const

NodeGizmo *ViewerDisplayWidget::TryGizmoPress(const NodeValueRow &row, const QPointF &p)
{
if (!gizmos_) {
return nullptr;
}

for (auto it=gizmos_->GetGizmos().crbegin(); it!=gizmos_->GetGizmos().crend(); it++) {
NodeGizmo *gizmo = *it;
if (gizmo->IsVisible()) {
Expand Down Expand Up @@ -705,6 +709,7 @@ NodeGizmo *ViewerDisplayWidget::TryGizmoPress(const NodeValueRow &row, const QPo
void ViewerDisplayWidget::OpenTextGizmo(TextGizmo *text, QMouseEvent *event)
{
active_text_gizmo_ = text;
connect(active_text_gizmo_, &TextGizmo::RectChanged, this, &ViewerDisplayWidget::UpdateActiveTextGizmoSize);
text_transform_ = GenerateGizmoTransform();
text_transform_inverted_ = text_transform_.inverted();

Expand Down Expand Up @@ -736,9 +741,7 @@ void ViewerDisplayWidget::OpenTextGizmo(TextGizmo *text, QMouseEvent *event)
connect(text_edit_, &ViewerTextEditor::destroyed, this, &ViewerDisplayWidget::TextEditDestroyed);

// Set text editor's size to logical size
QRectF text_rect = text->GetRect();
text_edit_pos_ = text_rect.topLeft();
text_edit_->setGeometry(text_rect.toRect());
QRectF text_rect = UpdateActiveTextGizmoSize();

// Emit text gizmo activation signal
emit text->Activated();
Expand Down Expand Up @@ -808,9 +811,9 @@ bool ViewerDisplayWidget::OnMousePress(QMouseEvent *event)

return true;

} else if (text_edit_) {
} else if (text_edit_ && ForwardMouseEventToTextEdit(event, true)) {

return ForwardMouseEventToTextEdit(event, true);
return true;

} else if (event->button() == Qt::LeftButton) {

Expand All @@ -821,8 +824,7 @@ bool ViewerDisplayWidget::OnMousePress(QMouseEvent *event)
add_band_end_ = add_band_start_;
add_band_ = true;

} else if (gizmos_
&& (current_gizmo_ = TryGizmoPress(gizmo_db_, gizmo_last_draw_transform_inverted_.map(event->pos())))) {
} else if ((current_gizmo_ = TryGizmoPress(gizmo_db_, gizmo_last_draw_transform_inverted_.map(event->pos())))) {

// Handle gizmo click
gizmo_start_drag_ = event->pos();
Expand Down Expand Up @@ -856,18 +858,9 @@ bool ViewerDisplayWidget::OnMouseMove(QMouseEvent *event)

return true;

} else if (text_edit_) {
} else if (text_edit_ && ForwardMouseEventToTextEdit(event)) {

if (event->buttons() == Qt::NoButton) {
QPointF mapped = text_transform_inverted_.map(event->pos()) - text_edit_pos_;
if (mapped.x() >= 0 && mapped.y() >= 0 && mapped.x() < text_edit_->width() && mapped.y() < text_edit_->height()) {
inner_widget()->setCursor(Qt::IBeamCursor);
} else {
inner_widget()->unsetCursor();
}
}

return ForwardMouseEventToTextEdit(event);
return true;

} else if (add_band_) {

Expand Down Expand Up @@ -927,9 +920,9 @@ bool ViewerDisplayWidget::OnMouseRelease(QMouseEvent *e)

return true;

} else if (text_edit_) {
} else if (text_edit_ && ForwardMouseEventToTextEdit(e)) {

return ForwardMouseEventToTextEdit(e);
return true;

} else if (add_band_) {

Expand Down Expand Up @@ -964,8 +957,8 @@ bool ViewerDisplayWidget::OnMouseRelease(QMouseEvent *e)

bool ViewerDisplayWidget::OnMouseDoubleClick(QMouseEvent *event)
{
if (text_edit_) {
return ForwardMouseEventToTextEdit(event);
if (text_edit_ && ForwardMouseEventToTextEdit(event)) {
return true;
} else if (event->button() == Qt::LeftButton && gizmos_) {
QPointF ptr = TransformViewerSpaceToBufferSpace(event->pos());
foreach (NodeGizmo *g, gizmos_->GetGizmos()) {
Expand Down Expand Up @@ -1148,13 +1141,31 @@ void ViewerDisplayWidget::ForwardDragEventToTextEdit(T *e)

bool ViewerDisplayWidget::ForwardMouseEventToTextEdit(QMouseEvent *event, bool check_if_outside)
{
if (current_gizmo_) {
return false;
}

// Transform screen mouse coords to world mouse coords
QPointF local_pos = GetVirtualPosForTextEdit(event->pos());

if (event->type() == QEvent::MouseMove && event->buttons() == Qt::NoButton) {
QPointF mapped = text_transform_inverted_.map(event->pos()) - text_edit_pos_;
if (mapped.x() >= 0 && mapped.y() >= 0 && mapped.x() < text_edit_->width() && mapped.y() < text_edit_->height()) {
inner_widget()->setCursor(Qt::IBeamCursor);
} else {
inner_widget()->unsetCursor();
}
}

if (check_if_outside) {
if (local_pos.x() < 0 || local_pos.x() >= text_edit_->width() || local_pos.y() < 0 || local_pos.y() >= text_edit_->height()) {
CloseTextEditor();
return true;
// Allow clicking other gizmos so the user can resize while the text editor is active
if ((current_gizmo_ = TryGizmoPress(gizmo_db_, gizmo_last_draw_transform_inverted_.map(event->pos())))) {
return false;
} else {
CloseTextEditor();
return true;
}
}
}

Expand Down Expand Up @@ -1191,6 +1202,9 @@ void ViewerDisplayWidget::CloseTextEditor()
{
text_edit_->deleteLater();
text_edit_ = nullptr;

disconnect(active_text_gizmo_, &TextGizmo::RectChanged, this, &ViewerDisplayWidget::UpdateActiveTextGizmoSize);
active_text_gizmo_ = nullptr;
}

void ViewerDisplayWidget::GenerateGizmoTransforms()
Expand Down Expand Up @@ -1382,4 +1396,12 @@ void ViewerDisplayWidget::FocusChanged(QWidget *old, QWidget *now)
}
}

QRectF ViewerDisplayWidget::UpdateActiveTextGizmoSize()
{
QRectF text_rect = active_text_gizmo_->GetRect();
text_edit_pos_ = text_rect.topLeft();
text_edit_->setGeometry(text_rect.toRect());
return text_rect;
}

}
2 changes: 2 additions & 0 deletions app/widget/viewer/viewerdisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,8 @@ private slots:

void FocusChanged(QWidget *old, QWidget *now);

QRectF UpdateActiveTextGizmoSize();


};

Expand Down

0 comments on commit feb7d57

Please sign in to comment.