From 6a2e81ca4b9941354b63af82a6eb5beec7ea1218 Mon Sep 17 00:00:00 2001 From: Gaute Lindkvist Date: Thu, 4 Apr 2019 15:58:32 +0200 Subject: [PATCH] #4282 Add a new cafShortenedQLabel class that provides a label that shortens itself when space is limited --- .../cafPdmUiFieldEditorHandle.cpp | 4 + .../cafTestApplication/MainWindow.cpp | 40 ++++++++ Fwk/AppFwk/cafUserInterface/CMakeLists.txt | 3 + .../cafPdmUiCheckBoxEditor.cpp | 3 +- .../cafPdmUiCheckBoxTristateEditor.cpp | 3 +- .../cafUserInterface/cafPdmUiColorEditor.cpp | 3 +- .../cafPdmUiComboBoxEditor.cpp | 3 +- .../cafUserInterface/cafPdmUiDateEditor.cpp | 3 +- .../cafPdmUiDoubleSliderEditor.cpp | 3 +- .../cafPdmUiDoubleValueEditor.cpp | 3 +- .../cafPdmUiFilePathEditor.cpp | 3 +- .../cafUserInterface/cafPdmUiLineEditor.cpp | 4 +- .../cafUserInterface/cafPdmUiListEditor.cpp | 3 +- .../cafUserInterface/cafPdmUiPropertyView.cpp | 6 +- .../cafPdmUiPushButtonEditor.cpp | 3 +- .../cafUserInterface/cafPdmUiSliderEditor.cpp | 3 +- .../cafPdmUiTableViewEditor.cpp | 5 +- .../cafUserInterface/cafPdmUiTextEditor.cpp | 3 +- .../cafPdmUiTreeSelectionEditor.cpp | 3 +- .../cafUserInterface/cafShortenedQLabel.cpp | 95 +++++++++++++++++++ .../cafUserInterface/cafShortenedQLabel.h | 22 +++++ 21 files changed, 198 insertions(+), 20 deletions(-) create mode 100644 Fwk/AppFwk/cafUserInterface/cafShortenedQLabel.cpp create mode 100644 Fwk/AppFwk/cafUserInterface/cafShortenedQLabel.h diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmUiCore/cafPdmUiFieldEditorHandle.cpp b/Fwk/AppFwk/cafProjectDataModel/cafPdmUiCore/cafPdmUiFieldEditorHandle.cpp index f994daf052..96723a2d70 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmUiCore/cafPdmUiFieldEditorHandle.cpp +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmUiCore/cafPdmUiFieldEditorHandle.cpp @@ -112,6 +112,10 @@ void PdmUiFieldEditorHandle::createWidgets(QWidget * parent) { connect(m_editorWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customMenuRequested(QPoint))); } + if (m_labelWidget) + { + m_labelWidget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed); + } } //-------------------------------------------------------------------------------------------------- diff --git a/Fwk/AppFwk/cafTests/cafTestApplication/MainWindow.cpp b/Fwk/AppFwk/cafTests/cafTestApplication/MainWindow.cpp index 4b51082a51..fb06147860 100644 --- a/Fwk/AppFwk/cafTests/cafTestApplication/MainWindow.cpp +++ b/Fwk/AppFwk/cafTests/cafTestApplication/MainWindow.cpp @@ -376,6 +376,43 @@ protected: CAF_PDM_SOURCE_INIT(SmallGridDemoPdmObject, "SmallGridDemoPdmObject"); +class SingleEditorPdmObject : public caf::PdmObject +{ + CAF_PDM_HEADER_INIT; + +public: + SingleEditorPdmObject() + { + CAF_PDM_InitObject("Single Editor Object", + "", + "This object is a demo of the CAF framework", + "This object is a demo of the CAF framework"); + + CAF_PDM_InitField(&m_intFieldStandard, + "Standard", + 0, + "Fairly Wide Label", + "", + "Enter some small number here", + "This is a place you can enter a small integer value if you want"); + + } + + // Outside group + caf::PdmField m_intFieldStandard; + +protected: + //-------------------------------------------------------------------------------------------------- + /// + //-------------------------------------------------------------------------------------------------- + void defineUiOrdering(QString uiConfigName, caf::PdmUiOrdering& uiOrdering) override + { + uiOrdering.add(&m_intFieldStandard); + } +}; + +CAF_PDM_SOURCE_INIT(SingleEditorPdmObject, "SingleEditorObject"); + class SmallDemoPdmObjectA: public caf::PdmObject { CAF_PDM_HEADER_INIT; @@ -873,6 +910,9 @@ void MainWindow::buildTestModel() SmallGridDemoPdmObject* smallGridObj = new SmallGridDemoPdmObject; m_testRoot->objects.push_back(smallGridObj); + SingleEditorPdmObject* singleEditorObj = new SingleEditorPdmObject; + m_testRoot->objects.push_back(singleEditorObj); + DemoPdmObject* demoObj2 = new DemoPdmObject; demoObject->m_textField = "Mitt Demo Obj"; diff --git a/Fwk/AppFwk/cafUserInterface/CMakeLists.txt b/Fwk/AppFwk/cafUserInterface/CMakeLists.txt index 64fff3d740..58a6ad9cf0 100644 --- a/Fwk/AppFwk/cafUserInterface/CMakeLists.txt +++ b/Fwk/AppFwk/cafUserInterface/CMakeLists.txt @@ -44,6 +44,7 @@ set (MOC_HEADER_FILES cafPdmUiTreeViewEditor.h cafUiProcess.h QMinimizePanel.h + cafShortenedQLabel.h cafPdmUiTreeSelectionEditor.h cafPdmUiTreeSelectionQModel.h cafPdmUiFormLayoutObjectEditor.h @@ -149,6 +150,8 @@ set( PROJECT_FILES cafUiProcess.h QMinimizePanel.cpp QMinimizePanel.h + cafShortenedQLabel.cpp + cafShortenedQLabel.h cafQTreeViewStateSerializer.h cafQTreeViewStateSerializer.cpp cafMemoryInspector.h diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiCheckBoxEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiCheckBoxEditor.cpp index 2d8f6c493e..e5bec7d85d 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiCheckBoxEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiCheckBoxEditor.cpp @@ -42,6 +42,7 @@ #include "cafPdmObject.h" #include "cafPdmUiFieldEditorHandle.h" #include "cafPdmUiOrdering.h" +#include "cafShortenedQLabel.h" #include "cafFactory.h" @@ -97,7 +98,7 @@ QWidget* PdmUiCheckBoxEditor::createEditorWidget(QWidget* parent) //-------------------------------------------------------------------------------------------------- QWidget* PdmUiCheckBoxEditor::createLabelWidget(QWidget* parent) { - m_label = new QLabel(parent); + m_label = new cafShortenedQLabel(parent); return m_label; } diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiCheckBoxTristateEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiCheckBoxTristateEditor.cpp index 428f461646..1a0a0e9c7d 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiCheckBoxTristateEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiCheckBoxTristateEditor.cpp @@ -8,6 +8,7 @@ #include "cafPdmUiFieldEditorHandle.h" #include "cafPdmUiOrdering.h" #include "cafPdmField.h" +#include "cafShortenedQLabel.h" #include "cafFactory.h" #include "cafTristate.h" @@ -66,7 +67,7 @@ QWidget* PdmUiCheckBoxTristateEditor::createEditorWidget(QWidget * parent) //-------------------------------------------------------------------------------------------------- QWidget* PdmUiCheckBoxTristateEditor::createLabelWidget(QWidget * parent) { - m_label = new QLabel(parent); + m_label = new cafShortenedQLabel(parent); return m_label; } diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiColorEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiColorEditor.cpp index 287a3bd3f9..e68140a2d4 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiColorEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiColorEditor.cpp @@ -43,6 +43,7 @@ #include "cafPdmUiFieldEditorHandle.h" #include "cafPdmUiOrdering.h" #include "cafPdmField.h" +#include "cafShortenedQLabel.h" #include "cafFactory.h" @@ -119,7 +120,7 @@ QWidget* PdmUiColorEditor::createEditorWidget(QWidget * parent) //-------------------------------------------------------------------------------------------------- QWidget* PdmUiColorEditor::createLabelWidget(QWidget * parent) { - m_label = new QLabel(parent); + m_label = new cafShortenedQLabel(parent); return m_label; } diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.cpp index 81c805e864..7d7797b4dc 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.cpp @@ -42,6 +42,7 @@ #include "cafPdmField.h" #include "cafFactory.h" +#include "cafShortenedQLabel.h" #include #include @@ -382,7 +383,7 @@ QWidget* PdmUiComboBoxEditor::createEditorWidget(QWidget * parent) //-------------------------------------------------------------------------------------------------- QWidget* PdmUiComboBoxEditor::createLabelWidget(QWidget * parent) { - m_label = new QLabel(parent); + m_label = new cafShortenedQLabel(parent); return m_label; } diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiDateEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiDateEditor.cpp index 1684341c47..cd75423948 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiDateEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiDateEditor.cpp @@ -44,6 +44,7 @@ #include "cafPdmUiFieldEditorHandle.h" #include "cafPdmUiOrdering.h" #include "cafSelectionManager.h" +#include "cafShortenedQLabel.h" #include #include @@ -106,7 +107,7 @@ QWidget* PdmUiDateEditor::createEditorWidget(QWidget* parent) //-------------------------------------------------------------------------------------------------- QWidget* PdmUiDateEditor::createLabelWidget(QWidget* parent) { - m_label = new QLabel(parent); + m_label = new cafShortenedQLabel(parent); return m_label; } diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleSliderEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleSliderEditor.cpp index 877195441c..317a0eb33b 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleSliderEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleSliderEditor.cpp @@ -40,6 +40,7 @@ #include "cafPdmField.h" #include "cafPdmUiFieldHandle.h" #include "cafPdmUiObjectHandle.h" +#include "cafShortenedQLabel.h" #include #include @@ -146,7 +147,7 @@ QWidget* PdmUiDoubleSliderEditor::createEditorWidget(QWidget * parent) //-------------------------------------------------------------------------------------------------- QWidget* PdmUiDoubleSliderEditor::createLabelWidget(QWidget * parent) { - m_label = new QLabel(parent); + m_label = new cafShortenedQLabel(parent); return m_label; } diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleValueEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleValueEditor.cpp index d6c3ff589a..49d42f8a23 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleValueEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleValueEditor.cpp @@ -42,6 +42,7 @@ #include "cafPdmUiFieldEditorHandle.h" #include "cafFactory.h" +#include "cafShortenedQLabel.h" #include #include @@ -118,7 +119,7 @@ QWidget* PdmUiDoubleValueEditor::createEditorWidget(QWidget* parent) //-------------------------------------------------------------------------------------------------- QWidget* PdmUiDoubleValueEditor::createLabelWidget(QWidget* parent) { - m_label = new QLabel(parent); + m_label = new cafShortenedQLabel(parent); return m_label; } diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiFilePathEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiFilePathEditor.cpp index 49c5ddd022..383eb5d9c9 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiFilePathEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiFilePathEditor.cpp @@ -43,6 +43,7 @@ #include "cafPdmUiDefaultObjectEditor.h" #include "cafPdmUiFieldEditorHandle.h" #include "cafPdmUiOrdering.h" +#include "cafShortenedQLabel.h" #include #include @@ -116,7 +117,7 @@ QWidget* PdmUiFilePathEditor::createEditorWidget(QWidget * parent) //-------------------------------------------------------------------------------------------------- QWidget* PdmUiFilePathEditor::createLabelWidget(QWidget * parent) { - m_label = new QLabel(parent); + m_label = new cafShortenedQLabel(parent); return m_label; } diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiLineEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiLineEditor.cpp index 7cb9f1c102..b5f666e04d 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiLineEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiLineEditor.cpp @@ -38,6 +38,7 @@ #include "cafPdmUiLineEditor.h" #include "cafFactory.h" +#include "cafShortenedQLabel.h" #include "cafPdmField.h" #include "cafPdmObject.h" #include "cafPdmUiDefaultObjectEditor.h" @@ -56,7 +57,6 @@ #include #include - namespace caf { @@ -78,7 +78,7 @@ QWidget* PdmUiLineEditor::createEditorWidget(QWidget * parent) //-------------------------------------------------------------------------------------------------- QWidget* PdmUiLineEditor::createLabelWidget(QWidget * parent) { - m_label = new QLabel(parent); + m_label = new cafShortenedQLabel(parent); return m_label; } diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiListEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiListEditor.cpp index e1d92aeb70..f89c627cae 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiListEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiListEditor.cpp @@ -42,6 +42,7 @@ #include "cafPdmField.h" #include "cafFactory.h" +#include "cafShortenedQLabel.h" #include #include @@ -312,7 +313,7 @@ QWidget* PdmUiListEditor::createEditorWidget(QWidget * parent) //-------------------------------------------------------------------------------------------------- QWidget* PdmUiListEditor::createLabelWidget(QWidget * parent) { - m_label = new QLabel(parent); + m_label = new cafShortenedQLabel(parent); return m_label; } diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiPropertyView.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiPropertyView.cpp index c3b57ebf47..2a8cc36a52 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiPropertyView.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiPropertyView.cpp @@ -167,9 +167,9 @@ void PdmUiPropertyView::showProperties( PdmObjectHandle* object) // Add stretch to make sure the property widget is not stretched this->m_placeHolderLayout->insertStretch(-1, 1); - int minimumWidth = propertyWidget->minimumSizeHint().width() + m_scrollArea->verticalScrollBar()->width(); - m_scrollArea->setMinimumWidth(minimumWidth); - m_scrollArea->adjustSize(); + //int minimumWidth = propertyWidget->minimumSizeHint().width() + m_scrollArea->verticalScrollBar()->width(); + //m_scrollArea->setMinimumWidth(minimumWidth); + //m_scrollArea->adjustSize(); } m_defaultObjectEditor->setPdmObject(object); diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiPushButtonEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiPushButtonEditor.cpp index 4e3596ee2a..60c43e3c5b 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiPushButtonEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiPushButtonEditor.cpp @@ -45,6 +45,7 @@ #include "cafPdmField.h" #include "cafFactory.h" +#include "cafShortenedQLabel.h" #include @@ -157,7 +158,7 @@ QWidget* PdmUiPushButtonEditor::createEditorWidget(QWidget * parent) //-------------------------------------------------------------------------------------------------- QWidget* PdmUiPushButtonEditor::createLabelWidget(QWidget * parent) { - m_label = new QLabel(parent); + m_label = new cafShortenedQLabel(parent); return m_label; } diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiSliderEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiSliderEditor.cpp index 6ddeff9280..ef53242804 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiSliderEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiSliderEditor.cpp @@ -43,6 +43,7 @@ #include "cafPdmField.h" #include "cafFactory.h" +#include "cafShortenedQLabel.h" #include #include @@ -125,7 +126,7 @@ QWidget* PdmUiSliderEditor::createEditorWidget(QWidget * parent) //-------------------------------------------------------------------------------------------------- QWidget* PdmUiSliderEditor::createLabelWidget(QWidget * parent) { - m_label = new QLabel(parent); + m_label = new cafShortenedQLabel(parent); return m_label; } diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiTableViewEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiTableViewEditor.cpp index 0c1e327aab..97b8ef5e6e 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiTableViewEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiTableViewEditor.cpp @@ -43,6 +43,7 @@ #include "cafPdmUiTableViewDelegate.h" #include "cafPdmUiTableViewQModel.h" #include "cafSelectionManager.h" +#include "cafShortenedQLabel.h" #include #include @@ -115,12 +116,12 @@ QWidget* PdmUiTableViewEditor::createLabelWidget(QWidget * parent) { if (m_tableHeading.isNull()) { - m_tableHeading = new QLabel(parent); + m_tableHeading = new cafShortenedQLabel(parent); } if (m_tableHeadingIcon.isNull()) { - m_tableHeadingIcon = new QLabel(parent); + m_tableHeadingIcon = new cafShortenedQLabel(parent); } QHBoxLayout* layoutForIconLabel = new QHBoxLayout(); diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiTextEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiTextEditor.cpp index 97179e45fd..ac5f279125 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiTextEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiTextEditor.cpp @@ -42,6 +42,7 @@ #include "cafPdmUiDefaultObjectEditor.h" #include "cafPdmUiFieldEditorHandle.h" #include "cafPdmUiOrdering.h" +#include "cafShortenedQLabel.h" #include #include @@ -195,7 +196,7 @@ QWidget* PdmUiTextEditor::createEditorWidget(QWidget * parent) //-------------------------------------------------------------------------------------------------- QWidget* PdmUiTextEditor::createLabelWidget(QWidget * parent) { - m_label = new QLabel(parent); + m_label = new cafShortenedQLabel(parent); return m_label; } diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionEditor.cpp index a227c04153..46673d5349 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionEditor.cpp @@ -40,6 +40,7 @@ #include "cafPdmObject.h" #include "cafPdmUiCommandSystemProxy.h" #include "cafPdmUiTreeSelectionQModel.h" +#include "cafShortenedQLabel.h" #include #include @@ -406,7 +407,7 @@ QWidget* PdmUiTreeSelectionEditor::createEditorWidget(QWidget* parent) //-------------------------------------------------------------------------------------------------- QWidget* PdmUiTreeSelectionEditor::createLabelWidget(QWidget * parent) { - m_label = new QLabel(parent); + m_label = new cafShortenedQLabel(parent); return m_label; } diff --git a/Fwk/AppFwk/cafUserInterface/cafShortenedQLabel.cpp b/Fwk/AppFwk/cafUserInterface/cafShortenedQLabel.cpp new file mode 100644 index 0000000000..3b309cf12f --- /dev/null +++ b/Fwk/AppFwk/cafUserInterface/cafShortenedQLabel.cpp @@ -0,0 +1,95 @@ +#include "cafShortenedQLabel.h" + +#include +#include +#include + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +cafShortenedQLabel::cafShortenedQLabel(QWidget* parent /*= nullptr*/, Qt::WindowFlags f /*= Qt::WindowFlags()*/) + : QLabel(parent, f) +{ +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QSize cafShortenedQLabel::minimumSizeHint() const +{ + int minimumWidth = 0; + + QFontMetrics fontMetrics = QApplication::fontMetrics(); + QString fullLabelText = fullText(); + if (!fullLabelText.isEmpty()) + { + minimumWidth = 10; + + QStringList words = fullLabelText.split(" "); + if (!words.empty()) + { + int textMinimumWidth = std::min(fontMetrics.width(fullLabelText), fontMetrics.width(words.front() + "...")); + minimumWidth = std::max(minimumWidth, textMinimumWidth); + } + } + QSize minimumSize = QLabel::minimumSizeHint(); + minimumSize.setWidth(minimumWidth); + return minimumSize; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QSize cafShortenedQLabel::sizeHint() const +{ + QFontMetrics fontMetrics = QApplication::fontMetrics(); + QString labelText = fullText(); + QSize size = QLabel::sizeHint(); + size.setWidth(fontMetrics.width(labelText)); + return size; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void cafShortenedQLabel::resizeEvent(QResizeEvent* event) +{ + QString labelText = fullText(); + QFontMetrics fontMetrics = QApplication::fontMetrics(); + + if (fontMetrics.width(labelText) < event->size().width()) + { + setDisplayText(labelText); + } + else + { + int width = std::max(minimumSizeHint().width(), event->size().width()); + QString elidedText = fontMetrics.elidedText(labelText, Qt::ElideRight, width); + setDisplayText(elidedText); + } +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void cafShortenedQLabel::setDisplayText(const QString& shortText) +{ + // Store original text if we haven't already done so. + if (m_fullLengthText.isEmpty()) + { + m_fullLengthText = text(); + } + setText(shortText); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QString cafShortenedQLabel::fullText() const +{ + if (!m_fullLengthText.isEmpty()) + { + return m_fullLengthText; + } + return text(); +} diff --git a/Fwk/AppFwk/cafUserInterface/cafShortenedQLabel.h b/Fwk/AppFwk/cafUserInterface/cafShortenedQLabel.h new file mode 100644 index 0000000000..da1d31473d --- /dev/null +++ b/Fwk/AppFwk/cafUserInterface/cafShortenedQLabel.h @@ -0,0 +1,22 @@ +#pragma once + +#include + +class cafShortenedQLabel : public QLabel +{ + Q_OBJECT +public: + explicit cafShortenedQLabel(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()); + + QSize minimumSizeHint() const override; + QSize sizeHint() const override; +protected: + void resizeEvent(QResizeEvent *event) override; + void setDisplayText(const QString& shortText); + QString fullText() const; + QString firstWord() const; +private: + QString m_fullLengthText; +}; + +