From 04d88cbacc13fd4c710492e8d7c11aaa1024a8bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B5=D0=BB=D0=B8=D0=BF=D0=B5=D0=BD=D0=BA=D0=BE=20?= =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD?= Date: Sun, 2 Dec 2018 17:50:22 +0000 Subject: [PATCH] git-svn-id: svn://db.shs.com.ru/libs@464 a8b55f48-bf90-11e4-a774-851b48703e85 --- qad/application/ribbon.cpp | 2 +- qad/utils/qad_types.cpp | 8 ++--- qad/utils/qad_types.h | 8 ++--- qad/widgets/iconedlabel.cpp | 59 +++++++++++++++++++++++++++++++++++++ qad/widgets/iconedlabel.h | 34 ++++++--------------- 5 files changed, 77 insertions(+), 34 deletions(-) create mode 100644 qad/widgets/iconedlabel.cpp diff --git a/qad/application/ribbon.cpp b/qad/application/ribbon.cpp index c062f12..d54e8e7 100644 --- a/qad/application/ribbon.cpp +++ b/qad/application/ribbon.cpp @@ -61,7 +61,7 @@ void Ribbon::_resize() { void Ribbon::_setIconsSize() { - qDebug() << "resize" << preferredIconSize() << QApplication::font(); + //qDebug() << "resize" << preferredIconSize() << QApplication::font(); setTabIconSize(preferredIconSize(2, this)); setIconSize(preferredIconSize(3, this)); } diff --git a/qad/utils/qad_types.cpp b/qad/utils/qad_types.cpp index 8e4e62e..7d21251 100644 --- a/qad/utils/qad_types.cpp +++ b/qad/utils/qad_types.cpp @@ -130,7 +130,7 @@ QString uniqueName(QString n, const QStringList & names) { } -int fontHeight(QWidget * w) { +int fontHeight(const QWidget * w) { #ifdef Q_OS_ANDROID static int ret = QApplication::fontMetrics().size(0, "0").height(); return ret; @@ -160,12 +160,12 @@ int fontHeight(QWidget * w) { } -int lineThickness(QWidget * w) { +int lineThickness(const QWidget * w) { return qMax(qRound(fontHeight(w) / 15.), 1); } -QSize preferredIconSize(float x, QWidget * w) { +QSize preferredIconSize(float x, const QWidget * w) { int s = qMax(8, qRound(fontHeight(w) * x)); #ifdef Q_OS_MACOS s /= 1.25; @@ -174,6 +174,6 @@ QSize preferredIconSize(float x, QWidget * w) { } -double appScale(QWidget * w) { +double appScale(const QWidget * w) { return qMax(fontHeight(w) / 15., 1.); } diff --git a/qad/utils/qad_types.h b/qad/utils/qad_types.h index 8ef986b..18f5bdc 100644 --- a/qad/utils/qad_types.h +++ b/qad/utils/qad_types.h @@ -118,10 +118,10 @@ inline QRectF enlargedRect(const QRectF & r, qreal dx, qreal dy, qreal v) { QVariant::Type typeFromLetter(const QString & l); QString uniqueName(QString n, const QStringList & names); -int fontHeight(QWidget * w = 0); -int lineThickness(QWidget * w = 0); -QSize preferredIconSize(float x = 1.f, QWidget * w = 0); -double appScale(QWidget * w = 0); +int fontHeight(const QWidget * w = 0); +int lineThickness(const QWidget * w = 0); +QSize preferredIconSize(float x = 1.f, const QWidget * w = 0); +double appScale(const QWidget * w = 0); #endif // QAD_TYPES_H diff --git a/qad/widgets/iconedlabel.cpp b/qad/widgets/iconedlabel.cpp new file mode 100644 index 0000000..e374b6c --- /dev/null +++ b/qad/widgets/iconedlabel.cpp @@ -0,0 +1,59 @@ +#include "iconedlabel.h" +#include "qad_types.h" +#include +#include + + +IconedLabel::IconedLabel(QWidget * parent): QFrame(parent) { + label_.setAlignment(Qt::AlignCenter); + icon_.setAlignment(Qt::AlignCenter); + icon_.setScaledContents(true); + setIconSize(QSize()); + setDirection(RightToLeft); +} + + +QIcon IconedLabel::icon() const { + return icon_.pixmap() == 0 ? QIcon() : QIcon(*icon_.pixmap()); +} + + +bool IconedLabel::event(QEvent * e) { + if (e->type() == QEvent::FontChange || e->type() == QEvent::Polish) + setIconSize(iconSize()); + return QFrame::event(e); +} + + +QSize IconedLabel::realIconSize() const { + return size_.isValid() ? size_ : preferredIconSize(1.f, this); +} + + +void IconedLabel::setIcon(const QIcon & i) { + sicon_ = i; + setIconSize(iconSize()); +} + + +void IconedLabel::setIconSize(const QSize & s) { + size_ = s; + QSize sz = realIconSize(); + icon_.setPixmap(sicon_.pixmap(sz)); + icon_.setFixedSize(sz); +} + + +void IconedLabel::setDirection(IconedLabel::Direction d) { + dir_ = d; + if (layout() != 0) + delete layout(); + QLayout * lay = new QBoxLayout((QBoxLayout::Direction)dir_); + lay->addItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding)); + lay->addWidget(&label_); + lay->addWidget(&icon_); + lay->addItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding)); + lay->setContentsMargins(0, 0, 0, 0); + setLayout(lay); + update(); +} diff --git a/qad/widgets/iconedlabel.h b/qad/widgets/iconedlabel.h index dc5cea0..ab2317d 100644 --- a/qad/widgets/iconedlabel.h +++ b/qad/widgets/iconedlabel.h @@ -3,7 +3,6 @@ #include #include -#include #include "qad_types.h" QT_BEGIN_HEADER @@ -21,42 +20,27 @@ class IconedLabel: public QFrame public: enum Direction {LeftToRight = 0, RightToLeft = 1, TopToBottom = 2, BottomToTop = 3}; - explicit IconedLabel(QWidget * parent = 0): QFrame(parent) { - label_.setAlignment(Qt::AlignCenter); - icon_.setAlignment(Qt::AlignCenter); - size_ = preferredIconSize(-1, this); - setDirection(LeftToRight); - } + explicit IconedLabel(QWidget * parent = 0); QString text() const {return label_.text();} - QIcon icon() const {return icon_.pixmap() == 0 ? QIcon() : QIcon(*icon_.pixmap());} + QIcon icon() const; QSize iconSize() const {return size_;} Direction direction() const {return dir_;} protected: - QLabel label_; - QLabel icon_; + virtual bool event(QEvent * e); + QSize realIconSize() const; + + QLabel label_, icon_; QIcon sicon_; QSize size_; Direction dir_; public slots: void setText(const QString & t) {label_.setText(t);} - void setIcon(const QIcon & i) {sicon_ = i; icon_.setPixmap(i.pixmap(size_));} - void setIconSize(const QSize & s) {size_ = s; icon_.setPixmap(sicon_.pixmap(size_));} - void setDirection(Direction d) { - dir_ = d; - if (layout() != 0) - delete layout(); - QLayout * lay = new QBoxLayout((QBoxLayout::Direction)dir_); - lay->addItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding)); - lay->addWidget(&label_); - lay->addWidget(&icon_); - lay->addItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding)); - lay->setContentsMargins(0, 0, 0, 0); - setLayout(lay); - update(); - } + void setIcon(const QIcon & i); + void setIconSize(const QSize & s); + void setDirection(Direction d); signals: