mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#4282 Add a new cafShortenedQLabel class that provides a label that shortens itself when space is limited
This commit is contained in:
parent
aa72b384f1
commit
6a2e81ca4b
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
@ -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<int> 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";
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -42,6 +42,7 @@
|
||||
#include "cafPdmField.h"
|
||||
|
||||
#include "cafFactory.h"
|
||||
#include "cafShortenedQLabel.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QComboBox>
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
@ -44,6 +44,7 @@
|
||||
#include "cafPdmUiFieldEditorHandle.h"
|
||||
#include "cafPdmUiOrdering.h"
|
||||
#include "cafSelectionManager.h"
|
||||
#include "cafShortenedQLabel.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QDate>
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
@ -40,6 +40,7 @@
|
||||
#include "cafPdmField.h"
|
||||
#include "cafPdmUiFieldHandle.h"
|
||||
#include "cafPdmUiObjectHandle.h"
|
||||
#include "cafShortenedQLabel.h"
|
||||
|
||||
#include <QHBoxLayout>
|
||||
#include <QDoubleValidator>
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
@ -42,6 +42,7 @@
|
||||
#include "cafPdmUiFieldEditorHandle.h"
|
||||
|
||||
#include "cafFactory.h"
|
||||
#include "cafShortenedQLabel.h"
|
||||
|
||||
#include <QDoubleValidator>
|
||||
#include <QHBoxLayout>
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
@ -43,6 +43,7 @@
|
||||
#include "cafPdmUiDefaultObjectEditor.h"
|
||||
#include "cafPdmUiFieldEditorHandle.h"
|
||||
#include "cafPdmUiOrdering.h"
|
||||
#include "cafShortenedQLabel.h"
|
||||
|
||||
#include <QBoxLayout>
|
||||
#include <QDir>
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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 <QStatusBar>
|
||||
#include <QString>
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -42,6 +42,7 @@
|
||||
#include "cafPdmField.h"
|
||||
|
||||
#include "cafFactory.h"
|
||||
#include "cafShortenedQLabel.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QBoxLayout>
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -45,6 +45,7 @@
|
||||
#include "cafPdmField.h"
|
||||
|
||||
#include "cafFactory.h"
|
||||
#include "cafShortenedQLabel.h"
|
||||
|
||||
#include <QBoxLayout>
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
@ -43,6 +43,7 @@
|
||||
#include "cafPdmField.h"
|
||||
|
||||
#include "cafFactory.h"
|
||||
#include "cafShortenedQLabel.h"
|
||||
|
||||
#include <QLineEdit>
|
||||
#include <QLabel>
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
@ -43,6 +43,7 @@
|
||||
#include "cafPdmUiTableViewDelegate.h"
|
||||
#include "cafPdmUiTableViewQModel.h"
|
||||
#include "cafSelectionManager.h"
|
||||
#include "cafShortenedQLabel.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QEvent>
|
||||
@ -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();
|
||||
|
@ -42,6 +42,7 @@
|
||||
#include "cafPdmUiDefaultObjectEditor.h"
|
||||
#include "cafPdmUiFieldEditorHandle.h"
|
||||
#include "cafPdmUiOrdering.h"
|
||||
#include "cafShortenedQLabel.h"
|
||||
|
||||
#include <QTextEdit>
|
||||
#include <QLabel>
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
@ -40,6 +40,7 @@
|
||||
#include "cafPdmObject.h"
|
||||
#include "cafPdmUiCommandSystemProxy.h"
|
||||
#include "cafPdmUiTreeSelectionQModel.h"
|
||||
#include "cafShortenedQLabel.h"
|
||||
|
||||
#include <QBoxLayout>
|
||||
#include <QCheckBox>
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
95
Fwk/AppFwk/cafUserInterface/cafShortenedQLabel.cpp
Normal file
95
Fwk/AppFwk/cafUserInterface/cafShortenedQLabel.cpp
Normal file
@ -0,0 +1,95 @@
|
||||
#include "cafShortenedQLabel.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QFontMetrics>
|
||||
#include <QResizeEvent>
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
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();
|
||||
}
|
22
Fwk/AppFwk/cafUserInterface/cafShortenedQLabel.h
Normal file
22
Fwk/AppFwk/cafUserInterface/cafShortenedQLabel.h
Normal file
@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <QLabel>
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user