Skip to content

Commit

Permalink
- More better & robust implementation for elidable QLabel.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@18876 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adeas31 committed Jan 31, 2014
1 parent 5e1120f commit c137cbc
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 91 deletions.
1 change: 0 additions & 1 deletion OMEdit/OMEditGUI/GUI/Containers/ModelWidgetContainer.cpp
Expand Up @@ -1907,7 +1907,6 @@ ModelWidget::ModelWidget(bool newClass, bool extendsClass, LibraryTreeNode *pLib
mpModelicaTypeLabel = new Label(StringHandler::getModelicaClassType(pLibraryTreeNode->getType()));
mpViewTypeLabel = new Label(StringHandler::getViewType(StringHandler::Diagram));
mpModelFilePathLabel = new Label(pLibraryTreeNode->getFileName());
mpModelFilePathLabel->setWordWrap(true);
// documentation view tool button
mpFileLockToolButton = new QToolButton;
mpFileLockToolButton->setText(mpLibraryTreeNode->isReadOnly() ? tr("Make writable") : tr("File is writable"));
Expand Down
14 changes: 2 additions & 12 deletions OMEdit/OMEditGUI/GUI/Dialogs/SimulationDialog.cpp
Expand Up @@ -413,25 +413,15 @@ void SimulationDialog::initializeFields()
{
setWindowTitle(QString(Helper::applicationName).append(" - ").append(Helper::interactiveSimulation)
.append(" - ").append(mpLibraryTreeNode->getNameStructure()));
QString headingStr = QString(Helper::interactiveSimulation).append(" - ").append(mpLibraryTreeNode->getNameStructure());
QFont font = mpSimulationHeading->font();
QFontMetrics fontMetrics = QFontMetrics(font);
int maxWidth = mpSimulationHeading->width();
QString elidedHeadingStr = fontMetrics.elidedText(headingStr, Qt::ElideMiddle, maxWidth);
mpSimulationHeading->setText(elidedHeadingStr);
mpSimulationHeading->setText(QString(Helper::interactiveSimulation).append(" - ").append(mpLibraryTreeNode->getNameStructure()));
mpSimulationIntervalGroupBox->setDisabled(true);
return;
}
else
{
setWindowTitle(QString(Helper::applicationName).append(" - ").append(Helper::simulation)
.append(" - ").append(mpLibraryTreeNode->getNameStructure()));
QString headingStr = QString(Helper::simulation).append(" - ").append(mpLibraryTreeNode->getNameStructure());
QFont font = mpSimulationHeading->font();
QFontMetrics fontMetrics = QFontMetrics(font);
int maxWidth = mpSimulationHeading->width();
QString elidedHeadingStr = fontMetrics.elidedText(headingStr, Qt::ElideMiddle, maxWidth);
mpSimulationHeading->setText(elidedHeadingStr);
mpSimulationHeading->setText(QString(Helper::simulation).append(" - ").append(mpLibraryTreeNode->getNameStructure()));
mpSimulationIntervalGroupBox->setDisabled(false);
}
// if the class has experiment annotation then read it.
Expand Down
26 changes: 13 additions & 13 deletions OMEdit/OMEditGUI/GUI/Widgets/TransformationsWidget.cpp
Expand Up @@ -439,15 +439,15 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
pReloadToolButton->setIcon(QIcon(":/Resources/icons/refresh.png"));
connect(pReloadToolButton, SIGNAL(clicked()), SLOT(reloadTransformations()));
/* info xml file path label */
ElidedLabel *pInfoXMLFilePathLabel = new ElidedLabel(mInfoXMLFullFileName);
Label *pInfoXMLFilePathLabel = new Label(mInfoXMLFullFileName, this);
/* create status bar */
QStatusBar *pStatusBar = new QStatusBar;
pStatusBar->setObjectName("ModelStatusBar");
pStatusBar->setSizeGripEnabled(false);
pStatusBar->addPermanentWidget(pReloadToolButton, 0);
pStatusBar->addPermanentWidget(pInfoXMLFilePathLabel, 1);
/* Variables Heading */
ElidedLabel *pVariablesBrowserLabel = new ElidedLabel(Helper::variablesBrowser);
Label *pVariablesBrowserLabel = new Label(Helper::variablesBrowser);
pVariablesBrowserLabel->setObjectName("LabelWithBorder");
// create the find text box
mpFindVariablesTextBox = new QLineEdit(Helper::findVariables);
Expand Down Expand Up @@ -494,7 +494,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
QFrame *pVariablesFrame = new QFrame;
pVariablesFrame->setLayout(pVariablesGridLayout);
/* Defined in tree widget */
ElidedLabel *pDefinedInLabel = new ElidedLabel(tr("Defined In Equations"));
Label *pDefinedInLabel = new Label(tr("Defined In Equations"));
pDefinedInLabel->setObjectName("LabelWithBorder");
mpDefinedInEquationsTreeWidget = new EquationTreeWidget(this);
QGridLayout *pDefinedInGridLayout = new QGridLayout;
Expand All @@ -505,7 +505,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
QFrame *pDefinedInFrame = new QFrame;
pDefinedInFrame->setLayout(pDefinedInGridLayout);
/* Used in tree widget */
ElidedLabel *pUsedInLabel = new ElidedLabel(tr("Used In Equations"));
Label *pUsedInLabel = new Label(tr("Used In Equations"));
pUsedInLabel->setObjectName("LabelWithBorder");
mpUsedInEquationsTreeWidget = new EquationTreeWidget(this);
QGridLayout *pUsedInGridLayout = new QGridLayout;
Expand All @@ -516,7 +516,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
QFrame *pUsedInFrame = new QFrame;
pUsedInFrame->setLayout(pUsedInGridLayout);
/* variable operations tree widget */
ElidedLabel *pOperationsLabel = new ElidedLabel(tr("Variable Operations"));
Label *pOperationsLabel = new Label(tr("Variable Operations"));
pOperationsLabel->setObjectName("LabelWithBorder");
mpVariableOperationsTreeWidget = new QTreeWidget;
mpVariableOperationsTreeWidget->setItemDelegate(new ItemDelegate(mpVariableOperationsTreeWidget));
Expand All @@ -533,7 +533,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
QFrame *pVariableOperationsFrame = new QFrame;
pVariableOperationsFrame->setLayout(pVariableOperationsGridLayout);
/* Equations Heading */
ElidedLabel *pEquationsBrowserLabel = new ElidedLabel(tr("Equations Browser"));
Label *pEquationsBrowserLabel = new Label(tr("Equations Browser"));
pEquationsBrowserLabel->setObjectName("LabelWithBorder");
/* Equations tree widget */
mpEquationsTreeWidget = new EquationTreeWidget(this);
Expand All @@ -545,7 +545,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
QFrame *pEquationsFrame = new QFrame;
pEquationsFrame->setLayout(pEquationsGridLayout);
/* defines tree widget */
ElidedLabel *pDefinesLabel = new ElidedLabel(tr("Defines"));
Label *pDefinesLabel = new Label(tr("Defines"));
pDefinesLabel->setObjectName("LabelWithBorder");
mpDefinesVariableTreeWidget = new QTreeWidget;
mpDefinesVariableTreeWidget->setItemDelegate(new ItemDelegate(mpDefinesVariableTreeWidget));
Expand All @@ -566,7 +566,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
QFrame *pDefinesFrame = new QFrame;
pDefinesFrame->setLayout(pDefinesGridLayout);
/* depends tree widget */
ElidedLabel *pDependsLabel = new ElidedLabel(tr("Depends"));
Label *pDependsLabel = new Label(tr("Depends"));
pDependsLabel->setObjectName("LabelWithBorder");
mpDependsVariableTreeWidget = new QTreeWidget;
mpDependsVariableTreeWidget->setItemDelegate(new ItemDelegate(mpDependsVariableTreeWidget));
Expand All @@ -585,7 +585,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
QFrame *pDependsFrame = new QFrame;
pDependsFrame->setLayout(pDependsGridLayout);
/* operations tree widget */
ElidedLabel *pEquationOperationsLabel = new ElidedLabel(tr("Equation Operations"));
Label *pEquationOperationsLabel = new Label(tr("Equation Operations"));
pEquationOperationsLabel->setObjectName("LabelWithBorder");
mpEquationOperationsTreeWidget = new QTreeWidget;
mpEquationOperationsTreeWidget->setItemDelegate(new ItemDelegate(mpEquationOperationsTreeWidget));
Expand All @@ -602,9 +602,9 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
QFrame *pEquationOperationsFrame = new QFrame;
pEquationOperationsFrame->setLayout(pEquationOperationsGridLayout);
/* TSourceEditor */
ElidedLabel *pTSourceEditorBrowserLabel = new ElidedLabel(tr("Source Browser"));
Label *pTSourceEditorBrowserLabel = new Label(tr("Source Browser"));
pTSourceEditorBrowserLabel->setObjectName("LabelWithBorder");
mpTSourceEditorFileLabel = new ElidedLabel("");
mpTSourceEditorFileLabel = new Label("");
mpTSourceEditorFileLabel->hide();
mpTSourceEditorInfoBar = new InfoBar(mpMainWindow);
mpTSourceEditorInfoBar->hide();
Expand Down Expand Up @@ -647,7 +647,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
mpVariablesHorizontalSplitter->setContentsMargins(0, 0, 0, 0);
mpVariablesHorizontalSplitter->addWidget(pVariablesFrame);
mpVariablesHorizontalSplitter->addWidget(mpVariablesNestedVerticalSplitter);
ElidedLabel *pVariablesHeadingLabel = new ElidedLabel(Helper::variables);
Label *pVariablesHeadingLabel = new Label(Helper::variables);
pVariablesHeadingLabel->setObjectName("LabelWithBorder");
QVBoxLayout *pVariablesMainLayout = new QVBoxLayout;
pVariablesMainLayout->setSpacing(1);
Expand Down Expand Up @@ -681,7 +681,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
mpEquationsHorizontalSplitter->setContentsMargins(0, 0, 0, 0);
mpEquationsHorizontalSplitter->addWidget(pEquationsFrame);
mpEquationsHorizontalSplitter->addWidget(mpEquationsNestedVerticalSplitter);
ElidedLabel *pEquationsHeadingLabel = new ElidedLabel(tr("Equations"));
Label *pEquationsHeadingLabel = new Label(tr("Equations"));
pEquationsHeadingLabel->setObjectName("LabelWithBorder");
QVBoxLayout *pEquationsMainLayout = new QVBoxLayout;
pEquationsMainLayout->setSpacing(1);
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditGUI/GUI/Widgets/TransformationsWidget.h
Expand Up @@ -196,7 +196,7 @@ class TransformationsWidget : public QWidget
QTreeWidget *mpDefinesVariableTreeWidget;
QTreeWidget *mpDependsVariableTreeWidget;
QTreeWidget *mpEquationOperationsTreeWidget;
ElidedLabel *mpTSourceEditorFileLabel;
Label *mpTSourceEditorFileLabel;
InfoBar *mpTSourceEditorInfoBar;
TSourceEditor *mpTSourceEditor;
QSplitter *mpVariablesNestedHorizontalSplitter;
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditGUI/Resources/css/stylesheet.qss
Expand Up @@ -92,7 +92,7 @@ QToolButton#ShapePointsButton {
padding: 3px;
}

QFrame#LabelWithBorder {
QLabel#LabelWithBorder {
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #d2d2d2, stop: 1 lightGray);
border: 1px solid gray;
}
79 changes: 37 additions & 42 deletions OMEdit/OMEditGUI/Util/Utilities.cpp
Expand Up @@ -53,56 +53,51 @@ MainWindow* MdiArea::getMainWindow()
return mpMainWindow;
}

ElidedLabel::ElidedLabel(const QString &text, Qt::TextElideMode elideMode, QWidget *parent)
: QFrame(parent), mElideMode(elideMode), elided(false), content(text)
Label::Label(QWidget *parent, Qt::WindowFlags flags)
: QLabel(parent, flags), mElideMode(Qt::ElideMiddle)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
setMinimumHeight(18); /* set minimum height for single line. */
setTextInteractionFlags(Qt::TextSelectableByMouse);
}

void ElidedLabel::setText(const QString &newText)
Label::Label(const QString &text, QWidget *parent, Qt::WindowFlags flags)
: QLabel(text, parent, flags), mElideMode(Qt::ElideMiddle), mText(text)
{
content = newText;
update();
setTextInteractionFlags(Qt::TextSelectableByMouse);
setToolTip(text);
}

void ElidedLabel::paintEvent(QPaintEvent *event)
QSize Label::minimumSizeHint() const
{
QFrame::paintEvent(event);
QPainter painter(this);
QFontMetrics fontMetrics = painter.fontMetrics();
bool didElide = false;
int lineSpacing = fontMetrics.lineSpacing();
int y = 0;
QTextLayout textLayout(content, painter.font());
textLayout.beginLayout();
forever {
QTextLine line = textLayout.createLine();
if (!line.isValid())
break;
if (pixmap() != NULL || textFormat() == Qt::RichText)
return QLabel::minimumSizeHint();
const QFontMetrics &fm = fontMetrics();
QSize size(fm.width("..."), fm.height()+5);
return size;
}

line.setLineWidth(width());
int nextLineY = y + lineSpacing;
if (height() >= nextLineY + lineSpacing)
{
line.draw(&painter, QPoint(0, y));
y = nextLineY;
}
else
{
QString lastLine = content.mid(line.textStart());
QString elidedLastLine = fontMetrics.elidedText(lastLine, mElideMode, width());
if (y == 0) y = height() / 6;
painter.drawText(QPoint(2, y + fontMetrics.ascent()), elidedLastLine);
line = textLayout.createLine();
didElide = line.isValid();
break;
}
}
textLayout.endLayout();
if (didElide != elided)
QSize Label::sizeHint() const
{
if (pixmap() != NULL || textFormat() == Qt::RichText)
return QLabel::sizeHint();
const QFontMetrics& fm = fontMetrics();
QSize size(fm.width(mText), fm.height()+5);
return size;
}

void Label::setText(const QString &text)
{
mText = text;
setToolTip(text);
QLabel::setText(text);
}

void Label::resizeEvent(QResizeEvent *event)
{
if (textFormat() != Qt::RichText)
{
elided = didElide;
emit elisionChanged(didElide);
QFontMetrics fm(fontMetrics());
QString str = fm.elidedText(mText, mElideMode, event->size().width());
QLabel::setText(str);
}
QLabel::resizeEvent(event);
}
34 changes: 13 additions & 21 deletions OMEdit/OMEditGUI/Util/Utilities.h
Expand Up @@ -67,33 +67,25 @@ class Sleep : public QThread
void run() {}
};

/*!
\class Label
\brief Creates a QLabel with elidable text. The default elide mode is Qt::ElideMiddle.Allows text selection via mouse.
*/
class Label : public QLabel
{
public:
Label(QWidget *parent = 0, Qt::WindowFlags f = 0) : QLabel(parent, f) {init();}
Label(const QString &text, QWidget *parent = 0, Qt::WindowFlags f = 0) : QLabel(text, parent, f) {init();}
private:
void init() {setTextInteractionFlags(Qt::TextSelectableByMouse);}
};

class ElidedLabel : public QFrame
{
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText)
Q_PROPERTY(bool isElided READ isElided)
public:
ElidedLabel(const QString &text, Qt::TextElideMode elideMode = Qt::ElideMiddle, QWidget *parent = 0);
Label(QWidget *parent = 0, Qt::WindowFlags flags = 0);
Label(const QString &text, QWidget *parent = 0, Qt::WindowFlags flags = 0);
Qt::TextElideMode elideMode() const {return mElideMode;}
void setElideMode(Qt::TextElideMode elideMode) {mElideMode = elideMode;}
virtual QSize minimumSizeHint() const;
virtual QSize sizeHint() const;
void setText(const QString &text);
const QString & text() const { return content; }
bool isElided() const { return elided; }
protected:
void paintEvent(QPaintEvent *event);
signals:
void elisionChanged(bool elided);
private:
bool elided;
QString content;
Qt::TextElideMode mElideMode;
QString mText;
protected:
virtual void resizeEvent(QResizeEvent *event);
};

//! @class DoubleSpinBox
Expand Down

0 comments on commit c137cbc

Please sign in to comment.