Skip to content

Commit e13b618

Browse files
committedJul 12, 2017
Add ElidedLabel
A label that adjusts its text based on Qt::TextElideMode.
1 parent cd1b894 commit e13b618

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
 

‎src/gui/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ set(client_SRCS
9595
notificationconfirmjob.cpp
9696
servernotificationhandler.cpp
9797
guiutility.cpp
98+
elidedlabel.cpp
9899
creds/credentialsfactory.cpp
99100
creds/httpcredentialsgui.cpp
100101
creds/oauth.cpp

‎src/gui/elidedlabel.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (C) by Christian Kamm <mail@ckamm.de>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but
10+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* for more details.
13+
*/
14+
15+
#include "elidedlabel.h"
16+
17+
#include <QResizeEvent>
18+
19+
namespace OCC {
20+
21+
ElidedLabel::ElidedLabel(const QString &text, QWidget *parent)
22+
: QLabel(text, parent)
23+
, _text(text)
24+
, _elideMode(Qt::ElideNone)
25+
{
26+
}
27+
28+
void ElidedLabel::setText(const QString &text)
29+
{
30+
_text = text;
31+
QLabel::setText(text);
32+
update();
33+
}
34+
35+
void ElidedLabel::setElideMode(Qt::TextElideMode elideMode)
36+
{
37+
_elideMode = elideMode;
38+
update();
39+
}
40+
41+
void ElidedLabel::resizeEvent(QResizeEvent *event)
42+
{
43+
QLabel::resizeEvent(event);
44+
45+
QFontMetrics fm = fontMetrics();
46+
QString elided = fm.elidedText(_text, _elideMode, event->size().width());
47+
QLabel::setText(elided);
48+
}
49+
}

‎src/gui/elidedlabel.h

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) by Christian Kamm <mail@ckamm.de>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but
10+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* for more details.
13+
*/
14+
15+
#ifndef ELIDEDLABEL_H
16+
#define ELIDEDLABEL_H
17+
18+
#include <QLabel>
19+
20+
namespace OCC {
21+
22+
/// Label that can elide its text
23+
class ElidedLabel : public QLabel
24+
{
25+
Q_OBJECT
26+
public:
27+
explicit ElidedLabel(const QString &text, QWidget *parent = 0);
28+
29+
void setText(const QString &text);
30+
const QString &text() const { return _text; }
31+
32+
void setElideMode(Qt::TextElideMode elideMode);
33+
Qt::TextElideMode elideMode() const { return _elideMode; }
34+
35+
protected:
36+
void resizeEvent(QResizeEvent *event) override;
37+
38+
private:
39+
QString _text;
40+
Qt::TextElideMode _elideMode;
41+
};
42+
}
43+
44+
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.