From fe6b92513f5c715a52ac92be91837bc152e86b4a Mon Sep 17 00:00:00 2001 From: Craig Watson Date: Sat, 28 Jan 2017 17:32:35 -0500 Subject: [PATCH] Fix for text items' buttons overflowing from frame This was observed in some cases on low-resolution screens, at least on Linux and Windows. The previously hardcoded value for the width of the text items' titlebar (consisting of the buttons for formatting text) was replaced by a method calculating its width (which varies based on screen resolution). --- src/board/UBBoardView.cpp | 1 + src/domain/UBGraphicsTextItem.cpp | 8 ++++++-- src/domain/UBGraphicsTextItemDelegate.cpp | 25 +++++++++++++++++++++++ src/domain/UBGraphicsTextItemDelegate.h | 1 + 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/board/UBBoardView.cpp b/src/board/UBBoardView.cpp index 48cc5932..4ca060dd 100644 --- a/src/board/UBBoardView.cpp +++ b/src/board/UBBoardView.cpp @@ -1335,6 +1335,7 @@ void UBBoardView::mouseReleaseEvent (QMouseEvent *event) textItem->setTextInteractionFlags(Qt::TextEditorInteraction); textItem->setSelected (true); + textItem->setTextWidth(0); textItem->setFocus(); } } diff --git a/src/domain/UBGraphicsTextItem.cpp b/src/domain/UBGraphicsTextItem.cpp index 5ef3b738..f2326bb9 100644 --- a/src/domain/UBGraphicsTextItem.cpp +++ b/src/domain/UBGraphicsTextItem.cpp @@ -306,8 +306,12 @@ QPainterPath UBGraphicsTextItem::shape() const void UBGraphicsTextItem::setTextWidth(qreal width) { - qreal strictMin = 155; // the size of the font customization panel - qreal newWidth = qMax(strictMin, width); + qreal titleBarWidth = 0; + UBGraphicsTextItemDelegate * del = dynamic_cast(Delegate()); + if (del) + titleBarWidth = del->titleBarWidth(); + + qreal newWidth = qMax(titleBarWidth, width); QGraphicsTextItem::setTextWidth(newWidth); } diff --git a/src/domain/UBGraphicsTextItemDelegate.cpp b/src/domain/UBGraphicsTextItemDelegate.cpp index 4e4a5c50..34b866e6 100644 --- a/src/domain/UBGraphicsTextItemDelegate.cpp +++ b/src/domain/UBGraphicsTextItemDelegate.cpp @@ -204,6 +204,31 @@ void UBGraphicsTextItemDelegate::createControls() } +/** + * @brief Calculate the width of the toolbar containing the text item-related buttons + * @return The space between the left-most and right-most buttons in pixels + */ +qreal UBGraphicsTextItemDelegate::titleBarWidth() +{ + if (!mFontButton) + return 0; + + // refresh the frame and buttons' positions + positionHandles(); + + qreal titleBarWidth(0); + qreal frameLeftCoordinate = mFontButton->pos().x(); + qreal frameRightCoordinate = frameLeftCoordinate; + + foreach(DelegateButton* button, mButtons) { + if (button->getSection() == Qt::TitleBarArea) { + frameLeftCoordinate = qMin(button->pos().x(), frameLeftCoordinate); + frameRightCoordinate = qMax(button->pos().x() + button->boundingRect().width(), frameRightCoordinate); + } + } + + return frameRightCoordinate - frameLeftCoordinate; +} void UBGraphicsTextItemDelegate::freeButtons() { diff --git a/src/domain/UBGraphicsTextItemDelegate.h b/src/domain/UBGraphicsTextItemDelegate.h index bfd99229..bc10f367 100644 --- a/src/domain/UBGraphicsTextItemDelegate.h +++ b/src/domain/UBGraphicsTextItemDelegate.h @@ -114,6 +114,7 @@ class UBGraphicsTextItemDelegate : public UBGraphicsItemDelegate void scaleTextSize(qreal multiplyer); virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value); virtual void createControls(); + qreal titleBarWidth(); public slots: void contentsChanged();