make Badge geometry follow parent widget's geometry
This commit is contained in:
parent
976472048a
commit
5d0f4fd535
|
@ -1,13 +1,17 @@
|
|||
#include "badge.h"
|
||||
#include <QPainter>
|
||||
#include <QIcon>
|
||||
#include <QEvent>
|
||||
#include <QDebug>
|
||||
#include "badge_p.h"
|
||||
#include "lib/style.h"
|
||||
|
||||
BadgePrivate::BadgePrivate(Badge *q)
|
||||
: q_ptr(q),
|
||||
padding(10),
|
||||
useThemeColors(true)
|
||||
useThemeColors(true),
|
||||
x(0),
|
||||
y(0)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -92,10 +96,61 @@ QColor Badge::backgroundColor() const
|
|||
}
|
||||
}
|
||||
|
||||
void Badge::setRelativePosition(const QPointF &pos)
|
||||
{
|
||||
setRelativePosition(pos.x(), pos.y());
|
||||
}
|
||||
|
||||
void Badge::setRelativePosition(qreal x, qreal y)
|
||||
{
|
||||
Q_D(Badge);
|
||||
|
||||
d->x = x;
|
||||
d->y = y;
|
||||
update();
|
||||
}
|
||||
|
||||
QPointF Badge::relativePosition() const
|
||||
{
|
||||
Q_D(const Badge);
|
||||
|
||||
return QPointF(d->x, d->y);
|
||||
}
|
||||
|
||||
void Badge::setRelativeXPosition(qreal x)
|
||||
{
|
||||
Q_D(Badge);
|
||||
|
||||
d->x = x;
|
||||
update();
|
||||
}
|
||||
|
||||
qreal Badge::relativeXPosition() const
|
||||
{
|
||||
Q_D(const Badge);
|
||||
|
||||
return d->x;
|
||||
}
|
||||
|
||||
void Badge::setRelativeYPosition(qreal y)
|
||||
{
|
||||
Q_D(Badge);
|
||||
|
||||
d->y = y;
|
||||
update();
|
||||
}
|
||||
|
||||
qreal Badge::relativeYPosition() const
|
||||
{
|
||||
Q_D(const Badge);
|
||||
|
||||
return d->y;
|
||||
}
|
||||
|
||||
QSize Badge::sizeHint() const
|
||||
{
|
||||
const int s = getDiameter();
|
||||
return QSize(s, s);
|
||||
return QSize(s+4, s+4);
|
||||
}
|
||||
|
||||
void Badge::setIcon(const QIcon &icon)
|
||||
|
@ -122,6 +177,51 @@ void Badge::setText(const QString &text)
|
|||
update();
|
||||
}
|
||||
|
||||
bool Badge::event(QEvent *event)
|
||||
{
|
||||
switch (event->type())
|
||||
{
|
||||
case QEvent::ParentChange:
|
||||
{
|
||||
if (!parent())
|
||||
break;
|
||||
|
||||
parent()->installEventFilter(this);
|
||||
|
||||
QWidget *widget;
|
||||
if ((widget = parentWidget())) {
|
||||
setGeometry(widget->rect());
|
||||
}
|
||||
break;
|
||||
}
|
||||
case QEvent::ParentAboutToChange:
|
||||
{
|
||||
if (!parent())
|
||||
break;
|
||||
|
||||
parent()->removeEventFilter(this);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return QWidget::event(event);
|
||||
}
|
||||
|
||||
bool Badge::eventFilter(QObject *obj, QEvent *event)
|
||||
{
|
||||
QEvent::Type type = event->type();
|
||||
|
||||
if (QEvent::Move == type || QEvent::Resize == type)
|
||||
{
|
||||
QWidget *widget;
|
||||
if ((widget = parentWidget())) {
|
||||
setGeometry(widget->rect());
|
||||
}
|
||||
}
|
||||
return QWidget::eventFilter(obj, event);
|
||||
}
|
||||
|
||||
void Badge::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
Q_UNUSED(event)
|
||||
|
@ -131,6 +231,8 @@ void Badge::paintEvent(QPaintEvent *event)
|
|||
QPainter painter(this);
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
painter.translate(d->x, d->y);
|
||||
|
||||
QBrush brush;
|
||||
brush.setStyle(Qt::SolidPattern);
|
||||
brush.setColor(backgroundColor());
|
||||
|
|
|
@ -11,6 +11,7 @@ class Badge : public QWidget
|
|||
|
||||
Q_PROPERTY(QColor textColor WRITE setTextColor READ textColor)
|
||||
Q_PROPERTY(QColor backgroundColor WRITE setBackgroundColor READ backgroundColor)
|
||||
Q_PROPERTY(QPointF relativePosition WRITE setRelativePosition READ relativePosition)
|
||||
|
||||
public:
|
||||
explicit Badge(QWidget *parent = 0);
|
||||
|
@ -25,12 +26,24 @@ public:
|
|||
void setBackgroundColor(const QColor &color);
|
||||
QColor backgroundColor() const;
|
||||
|
||||
void setRelativePosition(const QPointF &pos);
|
||||
void setRelativePosition(qreal x, qreal y);
|
||||
QPointF relativePosition() const;
|
||||
|
||||
void setRelativeXPosition(qreal x);
|
||||
qreal relativeXPosition() const;
|
||||
|
||||
void setRelativeYPosition(qreal y);
|
||||
qreal relativeYPosition() const;
|
||||
|
||||
QSize sizeHint() const Q_DECL_OVERRIDE;
|
||||
|
||||
void setIcon(const QIcon &icon);
|
||||
void setText(const QString &text);
|
||||
|
||||
protected:
|
||||
bool event(QEvent *event) Q_DECL_OVERRIDE;
|
||||
bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
|
||||
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
||||
int getDiameter() const;
|
||||
|
||||
|
|
|
@ -24,6 +24,8 @@ public:
|
|||
bool useThemeColors;
|
||||
QColor textColor;
|
||||
QColor backgroundColor;
|
||||
qreal x;
|
||||
qreal y;
|
||||
};
|
||||
|
||||
#endif // BADGE_P_H
|
||||
|
|
|
@ -12,6 +12,8 @@ MenuItem::MenuItem(QWidget *parent)
|
|||
font.setPointSize(11);
|
||||
font.setStyleName("Regular");
|
||||
setFont(font);
|
||||
|
||||
setShowHalo(false);
|
||||
}
|
||||
|
||||
MenuItem::MenuItem(const QString &text, QWidget *parent)
|
||||
|
@ -22,6 +24,8 @@ MenuItem::MenuItem(const QString &text, QWidget *parent)
|
|||
font.setPointSize(11);
|
||||
font.setStyleName("Regular");
|
||||
setFont(font);
|
||||
|
||||
setShowHalo(false);
|
||||
}
|
||||
|
||||
MenuItem::~MenuItem()
|
||||
|
@ -46,11 +50,11 @@ Menu::Menu(QWidget *parent)
|
|||
policy.setVerticalPolicy(QSizePolicy::Maximum);
|
||||
setSizePolicy(policy);
|
||||
|
||||
QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
|
||||
effect->setBlurRadius(9);
|
||||
effect->setOffset(QPoint(0, 0));
|
||||
effect->setColor(QColor(0, 0, 0, 100));
|
||||
setGraphicsEffect(effect);
|
||||
//QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
|
||||
//effect->setBlurRadius(9);
|
||||
//effect->setOffset(QPoint(0, 0));
|
||||
//effect->setColor(QColor(0, 0, 0, 100));
|
||||
//setGraphicsEffect(effect);
|
||||
}
|
||||
|
||||
Menu::~Menu()
|
||||
|
@ -71,7 +75,6 @@ void Menu::addMenuItem(const QString &text)
|
|||
void Menu::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
QPainter painter(this);
|
||||
|
||||
painter.fillRect(rect(), Qt::white);
|
||||
|
||||
QWidget::paintEvent(event);
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <QIcon>
|
||||
#include "avatarexamples.h"
|
||||
#include "components/avatar.h"
|
||||
#include "components/badge.h"
|
||||
#include "exampleview.h"
|
||||
#include "frame.h"
|
||||
|
||||
|
@ -27,6 +28,28 @@ AvatarExamples::AvatarExamples(QWidget *parent)
|
|||
|
||||
layout->addWidget(frame);
|
||||
}
|
||||
{
|
||||
Avatar *avatar = new Avatar;
|
||||
Badge *badge = new Badge;
|
||||
badge->setText("5");
|
||||
badge->setRelativePosition(13, 13);
|
||||
|
||||
avatar->setLetter(QChar('M'));
|
||||
|
||||
ExampleView *view = new ExampleView;
|
||||
view->setWidget(avatar);
|
||||
|
||||
//view->setWidget(badge);
|
||||
badge->setParent(avatar);
|
||||
|
||||
Frame *frame = new Frame;
|
||||
frame->setCodeSnippet(
|
||||
"hello"
|
||||
);
|
||||
frame->setWidget(view);
|
||||
|
||||
layout->addWidget(frame);
|
||||
}
|
||||
{
|
||||
Avatar *avatar = new Avatar;
|
||||
|
||||
|
|
Loading…
Reference in New Issue