Skip to content

Commit

Permalink
- Added a Label with elide feature.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@18868 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adeas31 committed Jan 30, 2014
1 parent 49b1f34 commit 78acc07
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 18 deletions.
29 changes: 14 additions & 15 deletions OMEdit/OMEditGUI/GUI/Widgets/TransformationsWidget.cpp
Expand Up @@ -421,16 +421,15 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
pReloadToolButton->setIcon(QIcon(":/Resources/icons/refresh.png"));
connect(pReloadToolButton, SIGNAL(clicked()), SLOT(reloadTransformations()));
/* info xml file path label */
Label *pInfoXMLFilePathLabel = new Label(mInfoXMLFullFileName);
pInfoXMLFilePathLabel->setWordWrap(true);
ElidedLabel *pInfoXMLFilePathLabel = new ElidedLabel(mInfoXMLFullFileName);
/* create status bar */
QStatusBar *pStatusBar = new QStatusBar;
pStatusBar->setObjectName("ModelStatusBar");
pStatusBar->setSizeGripEnabled(false);
pStatusBar->addPermanentWidget(pReloadToolButton, 0);
pStatusBar->addPermanentWidget(pInfoXMLFilePathLabel, 1);
/* Variables Heading */
Label *pVariablesBrowserLabel = new Label(Helper::variablesBrowser);
ElidedLabel *pVariablesBrowserLabel = new ElidedLabel(Helper::variablesBrowser);
pVariablesBrowserLabel->setObjectName("LabelWithBorder");
// create the find text box
mpFindVariablesTextBox = new QLineEdit(Helper::findVariables);
Expand Down Expand Up @@ -477,7 +476,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
QFrame *pVariablesFrame = new QFrame;
pVariablesFrame->setLayout(pVariablesGridLayout);
/* Defined in tree widget */
Label *pDefinedInLabel = new Label(tr("Defined In Equations"));
ElidedLabel *pDefinedInLabel = new ElidedLabel(tr("Defined In Equations"));
pDefinedInLabel->setObjectName("LabelWithBorder");
mpDefinedInEquationsTreeWidget = new QTreeWidget;
mpDefinedInEquationsTreeWidget->setItemDelegate(new ItemDelegate(mpDefinedInEquationsTreeWidget));
Expand All @@ -501,7 +500,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
QFrame *pDefinedInFrame = new QFrame;
pDefinedInFrame->setLayout(pDefinedInGridLayout);
/* Used in tree widget */
Label *pUsedInLabel = new Label(tr("Used In Equations"));
ElidedLabel *pUsedInLabel = new ElidedLabel(tr("Used In Equations"));
pUsedInLabel->setObjectName("LabelWithBorder");
mpUsedInEquationsTreeWidget = new QTreeWidget;
mpUsedInEquationsTreeWidget->setItemDelegate(new ItemDelegate(mpUsedInEquationsTreeWidget));
Expand All @@ -525,7 +524,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
QFrame *pUsedInFrame = new QFrame;
pUsedInFrame->setLayout(pUsedInGridLayout);
/* variable operations tree widget */
Label *pOperationsLabel = new Label(tr("Variable Operations"));
ElidedLabel *pOperationsLabel = new ElidedLabel(tr("Variable Operations"));
pOperationsLabel->setObjectName("LabelWithBorder");
mpVariableOperationsTreeWidget = new QTreeWidget;
mpVariableOperationsTreeWidget->setItemDelegate(new ItemDelegate(mpVariableOperationsTreeWidget));
Expand All @@ -542,7 +541,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
QFrame *pVariableOperationsFrame = new QFrame;
pVariableOperationsFrame->setLayout(pVariableOperationsGridLayout);
/* Equations Heading */
Label *pEquationsBrowserLabel = new Label(tr("Equations Browser"));
ElidedLabel *pEquationsBrowserLabel = new ElidedLabel(tr("Equations Browser"));
pEquationsBrowserLabel->setObjectName("LabelWithBorder");
/* Equations tree widget */
mpEquationsTreeWidget = new QTreeWidget;
Expand All @@ -567,7 +566,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
QFrame *pEquationsFrame = new QFrame;
pEquationsFrame->setLayout(pEquationsGridLayout);
/* defines tree widget */
Label *pDefinesLabel = new Label(tr("Defines"));
ElidedLabel *pDefinesLabel = new ElidedLabel(tr("Defines"));
pDefinesLabel->setObjectName("LabelWithBorder");
mpDefinesVariableTreeWidget = new QTreeWidget;
mpDefinesVariableTreeWidget->setItemDelegate(new ItemDelegate(mpDefinesVariableTreeWidget));
Expand All @@ -588,7 +587,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
QFrame *pDefinesFrame = new QFrame;
pDefinesFrame->setLayout(pDefinesGridLayout);
/* depends tree widget */
Label *pDependsLabel = new Label(tr("Depends"));
ElidedLabel *pDependsLabel = new ElidedLabel(tr("Depends"));
pDependsLabel->setObjectName("LabelWithBorder");
mpDependsVariableTreeWidget = new QTreeWidget;
mpDependsVariableTreeWidget->setItemDelegate(new ItemDelegate(mpDependsVariableTreeWidget));
Expand All @@ -607,7 +606,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
QFrame *pDependsFrame = new QFrame;
pDependsFrame->setLayout(pDependsGridLayout);
/* operations tree widget */
Label *pEquationOperationsLabel = new Label(tr("Equation Operations"));
ElidedLabel *pEquationOperationsLabel = new ElidedLabel(tr("Equation Operations"));
pEquationOperationsLabel->setObjectName("LabelWithBorder");
mpEquationOperationsTreeWidget = new QTreeWidget;
mpEquationOperationsTreeWidget->setItemDelegate(new ItemDelegate(mpEquationOperationsTreeWidget));
Expand All @@ -624,9 +623,9 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
QFrame *pEquationOperationsFrame = new QFrame;
pEquationOperationsFrame->setLayout(pEquationOperationsGridLayout);
/* TSourceEditor */
Label *pTSourceEditorBrowserLabel = new Label(tr("Source Browser"));
ElidedLabel *pTSourceEditorBrowserLabel = new ElidedLabel(tr("Source Browser"));
pTSourceEditorBrowserLabel->setObjectName("LabelWithBorder");
mpTSourceEditorFileLabel = new Label;
mpTSourceEditorFileLabel = new ElidedLabel("");
mpTSourceEditorFileLabel->hide();
mpTSourceEditorInfoBar = new InfoBar(mpMainWindow);
mpTSourceEditorInfoBar->hide();
Expand Down Expand Up @@ -669,7 +668,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
mpVariablesHorizontalSplitter->setContentsMargins(0, 0, 0, 0);
mpVariablesHorizontalSplitter->addWidget(pVariablesFrame);
mpVariablesHorizontalSplitter->addWidget(mpVariablesNestedVerticalSplitter);
Label *pVariablesHeadingLabel = new Label(Helper::variables);
ElidedLabel *pVariablesHeadingLabel = new ElidedLabel(Helper::variables);
pVariablesHeadingLabel->setObjectName("LabelWithBorder");
QVBoxLayout *pVariablesMainLayout = new QVBoxLayout;
pVariablesMainLayout->setSpacing(1);
Expand Down Expand Up @@ -703,7 +702,7 @@ TransformationsWidget::TransformationsWidget(QString infoXMLFullFileName, MainWi
mpEquationsHorizontalSplitter->setContentsMargins(0, 0, 0, 0);
mpEquationsHorizontalSplitter->addWidget(pEquationsFrame);
mpEquationsHorizontalSplitter->addWidget(mpEquationsNestedVerticalSplitter);
Label *pEquationsHeadingLabel = new Label(tr("Equations"));
ElidedLabel *pEquationsHeadingLabel = new ElidedLabel(tr("Equations"));
pEquationsHeadingLabel->setObjectName("LabelWithBorder");
QVBoxLayout *pEquationsMainLayout = new QVBoxLayout;
pEquationsMainLayout->setSpacing(1);
Expand Down Expand Up @@ -1033,7 +1032,7 @@ void TransformationsWidget::fetchVariableData(const QModelIndex &index)
return;
QModelIndex modelIndex = mpTVariableTreeProxyModel->mapToSource(index);
TVariablesTreeItem *pTVariableTreeItem = static_cast<TVariablesTreeItem*>(modelIndex.internalPointer());
if (!pTVariableTreeItem)
if (!pTVariableTreeItem || pTVariableTreeItem->getChildren().size() != 0)
return;

OMVariable variable = mpInfoXMLFileHandler->variables.value(pTVariableTreeItem->getVariableName());
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditGUI/GUI/Widgets/TransformationsWidget.h
Expand Up @@ -189,7 +189,7 @@ class TransformationsWidget : public QWidget
QTreeWidget *mpDefinesVariableTreeWidget;
QTreeWidget *mpDependsVariableTreeWidget;
QTreeWidget *mpEquationOperationsTreeWidget;
Label *mpTSourceEditorFileLabel;
ElidedLabel *mpTSourceEditorFileLabel;
InfoBar *mpTSourceEditorInfoBar;
TSourceEditor *mpTSourceEditor;
QSplitter *mpVariablesNestedHorizontalSplitter;
Expand Down
3 changes: 1 addition & 2 deletions OMEdit/OMEditGUI/Resources/css/stylesheet.qss
Expand Up @@ -92,8 +92,7 @@ QToolButton#ShapePointsButton {
padding: 3px;
}

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

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

void ElidedLabel::setText(const QString &newText)
{
content = newText;
update();
}

void ElidedLabel::paintEvent(QPaintEvent *event)
{
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;

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)
{
elided = didElide;
emit elisionChanged(didElide);
}
}
20 changes: 20 additions & 0 deletions OMEdit/OMEditGUI/Util/Utilities.h
Expand Up @@ -76,6 +76,26 @@ class Label : public QLabel
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);
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;
};

//! @class DoubleSpinBox
/*! \brief Only keeping this class so that if in future we need to change the way QDoubleSpinBox works then we don't have to change the
* forms controls again.
Expand Down

0 comments on commit 78acc07

Please sign in to comment.