version 1.5.0
IconedLabel now remove spacings in case of empty text or icon
This commit is contained in:
@@ -2,9 +2,9 @@ cmake_minimum_required(VERSION 3.0)
|
|||||||
cmake_policy(SET CMP0017 NEW) # need include() with .cmake
|
cmake_policy(SET CMP0017 NEW) # need include() with .cmake
|
||||||
project(qad)
|
project(qad)
|
||||||
set(_QAD_MAJOR 1)
|
set(_QAD_MAJOR 1)
|
||||||
set(_QAD_MINOR 4)
|
set(_QAD_MINOR 5)
|
||||||
set(_QAD_REVISION 0)
|
set(_QAD_REVISION 0)
|
||||||
set(_QAD_SUFFIX alpha)
|
set(_QAD_SUFFIX )
|
||||||
set(_QAD_COMPANY SHS)
|
set(_QAD_COMPANY SHS)
|
||||||
set(_QAD_DOMAIN org.SHS)
|
set(_QAD_DOMAIN org.SHS)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "iconedlabel.h"
|
#include "iconedlabel.h"
|
||||||
#include "qad_types.h"
|
#include "qad_types.h"
|
||||||
#include <QHBoxLayout>
|
#include <QHBoxLayout>
|
||||||
|
#include <QStyle>
|
||||||
#include <QEvent>
|
#include <QEvent>
|
||||||
|
|
||||||
|
|
||||||
@@ -8,13 +9,23 @@ IconedLabel::IconedLabel(QWidget * parent): QFrame(parent) {
|
|||||||
label_.setAlignment(Qt::AlignCenter);
|
label_.setAlignment(Qt::AlignCenter);
|
||||||
icon_.setAlignment(Qt::AlignCenter);
|
icon_.setAlignment(Qt::AlignCenter);
|
||||||
icon_.setScaledContents(true);
|
icon_.setScaledContents(true);
|
||||||
|
icon_.setHidden(true);
|
||||||
setIconSize(QSize());
|
setIconSize(QSize());
|
||||||
setDirection(RightToLeft);
|
setDirection(RightToLeft);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QString IconedLabel::text() const {
|
||||||
|
return label_.text();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
QIcon IconedLabel::icon() const {
|
QIcon IconedLabel::icon() const {
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
|
||||||
|
return icon_.pixmap(Qt::ReturnByValue).isNull() ? QIcon() : QIcon(icon_.pixmap(Qt::ReturnByValue));
|
||||||
|
#else
|
||||||
return icon_.pixmap() == 0 ? QIcon() : QIcon(*icon_.pixmap());
|
return icon_.pixmap() == 0 ? QIcon() : QIcon(*icon_.pixmap());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -25,14 +36,36 @@ bool IconedLabel::event(QEvent * e) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void IconedLabel::checkSpacing() {
|
||||||
|
label_.setHidden(label_.text().isEmpty());
|
||||||
|
if (!layout()) return;
|
||||||
|
if (label_.isHidden() || icon_.isHidden()) {
|
||||||
|
layout()->setSpacing(0);
|
||||||
|
} else {
|
||||||
|
QStyle * s = style();
|
||||||
|
if (s)
|
||||||
|
layout()->setSpacing(s->layoutSpacing(QSizePolicy::Label, QSizePolicy::Label,
|
||||||
|
dir_ <= Direction::RightToLeft ? Qt::Horizontal : Qt::Vertical));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
QSize IconedLabel::realIconSize() const {
|
QSize IconedLabel::realIconSize() const {
|
||||||
return size_.isValid() ? size_ : preferredIconSize(1.f, this);
|
return size_.isValid() ? size_ : preferredIconSize(1.f, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void IconedLabel::setText(const QString & t) {
|
||||||
|
label_.setText(t);
|
||||||
|
checkSpacing();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void IconedLabel::setIcon(const QIcon & i) {
|
void IconedLabel::setIcon(const QIcon & i) {
|
||||||
sicon_ = i;
|
sicon_ = i;
|
||||||
setIconSize(iconSize());
|
setIconSize(iconSize());
|
||||||
|
icon_.setHidden(icon().isNull());
|
||||||
|
checkSpacing();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -55,5 +88,6 @@ void IconedLabel::setDirection(IconedLabel::Direction d) {
|
|||||||
lay->addItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding));
|
lay->addItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding));
|
||||||
lay->setContentsMargins(0, 0, 0, 0);
|
lay->setContentsMargins(0, 0, 0, 0);
|
||||||
setLayout(lay);
|
setLayout(lay);
|
||||||
|
checkSpacing();
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,13 +38,16 @@ public:
|
|||||||
|
|
||||||
explicit IconedLabel(QWidget * parent = 0);
|
explicit IconedLabel(QWidget * parent = 0);
|
||||||
|
|
||||||
QString text() const {return label_.text();}
|
QString text() const;
|
||||||
QIcon icon() const;
|
QIcon icon() const;
|
||||||
QSize iconSize() const {return size_;}
|
QSize iconSize() const {return size_;}
|
||||||
Direction direction() const {return dir_;}
|
Direction direction() const {return dir_;}
|
||||||
|
|
||||||
|
QLabel * textLabel() {return &label_;}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual bool event(QEvent * e);
|
virtual bool event(QEvent * e);
|
||||||
|
void checkSpacing();
|
||||||
QSize realIconSize() const;
|
QSize realIconSize() const;
|
||||||
|
|
||||||
QLabel label_, icon_;
|
QLabel label_, icon_;
|
||||||
@@ -53,7 +56,7 @@ protected:
|
|||||||
Direction dir_;
|
Direction dir_;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void setText(const QString & t) {label_.setText(t);}
|
void setText(const QString & t);
|
||||||
void setIcon(const QIcon & i);
|
void setIcon(const QIcon & i);
|
||||||
void setIconSize(const QSize & s);
|
void setIconSize(const QSize & s);
|
||||||
void setDirection(Direction d);
|
void setDirection(Direction d);
|
||||||
|
|||||||
Reference in New Issue
Block a user