diff --git a/sources/titleblock/templateview.cpp b/sources/titleblock/templateview.cpp index efdacf6e3..8f580bf6e 100644 --- a/sources/titleblock/templateview.cpp +++ b/sources/titleblock/templateview.cpp @@ -465,6 +465,8 @@ void TitleBlockTemplateView::applyColumnsWidths(bool animate) { ).arg(total_applied_width - preview_width_); total_width_helper_cell_ -> split_size = total_applied_width - preview_width_; } + + updateDisplayedMinMaxWidth(); } /** @@ -735,6 +737,35 @@ void TitleBlockTemplateView::columnsDimensionsChanged() { applyColumnsWidths(); } +/** + Update the tooltip that displays the minimum and/or maximum width of the + template. +*/ +void TitleBlockTemplateView::updateDisplayedMinMaxWidth() { + if (!tbtemplate_) return; + int min_width = tbtemplate_ -> minimumWidth(); + int max_width = tbtemplate_ -> maximumWidth(); + + QString min_max_width_sentence; + if (max_width != -1) { + min_max_width_sentence = QString( + tr( + "Minimum width: %1px\nMaximum width: %2px\n", + "tooltip showing the minimum and/or maximum width of the edited template" + ) + ).arg(min_width).arg(max_width); + } else { + min_max_width_sentence = QString( + tr( + "Minimum width: %1px\n", + "tooltip showing the minimum width of the edited template" + ) + ).arg(min_width); + } + + total_width_helper_cell_ -> setToolTip(makePrettyToolTip(min_max_width_sentence)); +} + /** @param read_only whether this view should be read only. @@ -848,6 +879,20 @@ TitleBlockTemplateCellsSet TitleBlockTemplateView::makeCellsSetFromGraphicsItems return(set); } +/* + @param a text string + @return an HTML string that can be passed to setToolTip() +*/ +QString TitleBlockTemplateView::makePrettyToolTip(const QString &string) { + QString css_style = QString("white-space: pre;"); + + QString final_tooltip_content = QString( + "
%2
" + ).arg(css_style).arg(string); + + return(final_tooltip_content); +} + /** Stores \a last_context_menu_cell as being the last helper cell the context menu was triggered on. diff --git a/sources/titleblock/templateview.h b/sources/titleblock/templateview.h index cf99aac9a..cf091ab54 100644 --- a/sources/titleblock/templateview.h +++ b/sources/titleblock/templateview.h @@ -73,6 +73,7 @@ class TitleBlockTemplateView : public QGraphicsView { void updateLayout(); void rowsDimensionsChanged(); void columnsDimensionsChanged(); + void updateDisplayedMinMaxWidth(); void setReadOnly(bool); protected slots: @@ -105,6 +106,7 @@ class TitleBlockTemplateView : public QGraphicsView { int indexOf(QGraphicsLayoutItem *); void removeItem(QGraphicsLayoutItem *); TitleBlockTemplateCellsSet makeCellsSetFromGraphicsItems(const QList &) const; + QString makePrettyToolTip(const QString &); private slots: void updateLastContextMenuCell(HelperCell *); diff --git a/sources/titleblocktemplate.cpp b/sources/titleblocktemplate.cpp index 7ab32dce8..69efaf67c 100644 --- a/sources/titleblocktemplate.cpp +++ b/sources/titleblocktemplate.cpp @@ -794,6 +794,69 @@ QList TitleBlockTemplate::rowsHeights() const { return(rows_heights_); } +/** + @param a column type + @return the count of \a type columns +*/ +int TitleBlockTemplate::columnTypeCount(QET::TitleBlockColumnLength type) { + int count = 0; + + for (int i = 0 ; i < columns_width_.count() ; ++ i) { + if (columns_width_.at(i).type == type) ++ count; + } + + return(count); +} + +/** + @param a column type + @return the sum of values attached to \a type columns +*/ +int TitleBlockTemplate::columnTypeTotal(QET::TitleBlockColumnLength type) { + int total = 0; + + for (int i = 0 ; i < columns_width_.count() ; ++ i) { + if (columns_width_.at(i).type == type) { + total += columns_width_.at(i).value; + } + } + + return(total); +} + +/** + @return the minimum width for this template +*/ +int TitleBlockTemplate::minimumWidth() { + // Abbreviations: ABS: absolute, RTT: relative to total, RTR: relative to + // remaining, TOT: total diagram/TBT width (variable). + + // Minimum size may be enforced by ABS and RTT widths: + // TOT >= ((sum(REL)/100)*TOT)+sum(ABS) + // => (1 - (sum(REL)/100))TOT >= sum(ABS) + // => TOT >= sum(ABS) / (1 - (sum(REL)/100)) + // => TOT >= sum(ABS) / ((100 - sum(REL))/100)) + return( + qRound( + columnTypeTotal(QET::Absolute) + / + ((100.0 - columnTypeTotal(QET::RelativeToTotalLength)) / 100.0) + ) + ); +} + +/** + @return the maximum width for this template, or -1 if it does not have any. +*/ +int TitleBlockTemplate::maximumWidth() { + if (columnTypeCount(QET::Absolute) == columns_width_.count()) { + // The template is composed of absolute widths only, + // therefore it may not extend beyond their sum. + return(columnTypeTotal(QET::Absolute)); + } + return(-1); +} + /** @return the total effective width of this template @param total_width The total width initially planned for the rendering diff --git a/sources/titleblocktemplate.h b/sources/titleblocktemplate.h index 76f08d1dd..ba0bf52e1 100644 --- a/sources/titleblocktemplate.h +++ b/sources/titleblocktemplate.h @@ -59,6 +59,10 @@ class TitleBlockTemplate : public QObject { int rowsCount() const; QList columnsWidth(int) const; QList rowsHeights() const; + int columnTypeCount(QET::TitleBlockColumnLength); + int columnTypeTotal(QET::TitleBlockColumnLength); + int minimumWidth(); + int maximumWidth(); int width(int); int height() const;