a dialog has margins, take them into account

This commit is contained in:
Marco Martin 2011-06-08 17:53:19 +02:00
parent 9cd8721f61
commit 42f8d04f6c
2 changed files with 98 additions and 0 deletions

View File

@ -30,11 +30,62 @@
#include <Plasma/Dialog>
DialogMargins::DialogMargins(Plasma::Dialog *dialog, QObject *parent)
: QObject(parent),
m_dialog(dialog)
{
checkMargins();
}
void DialogMargins::checkMargins()
{
int left, top, right, bottom;
m_dialog->getContentsMargins(&left, &top, &right, &bottom);
if (left != m_left) {
m_left = left;
emit leftChanged();
}
if (top != m_top) {
m_top = top;
emit topChanged();
}
if (right != m_right) {
m_right = right;
emit rightChanged();
}
if (bottom != m_bottom) {
m_bottom = bottom;
emit bottomChanged();
}
}
int DialogMargins::left() const
{
return m_left;
}
int DialogMargins::top() const
{
return m_top;
}
int DialogMargins::right() const
{
return m_right;
}
int DialogMargins::bottom() const
{
return m_bottom;
}
DialogProxy::DialogProxy(QObject *parent)
: QObject(parent),
m_declarativeItemContainer(0)
{
m_dialog = new Plasma::Dialog();
m_margins = new DialogMargins(m_dialog, this);
m_dialog->installEventFilter(this);
m_flags = m_dialog->windowFlags();
}
@ -177,6 +228,11 @@ void DialogProxy::setWindowFlags(const int flags)
m_dialog->setWindowFlags((Qt::WindowFlags)flags);
}
QObject *DialogProxy::margins() const
{
return m_margins;
}
bool DialogProxy::eventFilter(QObject *watched, QEvent *event)
{
if (watched == m_dialog && event->type() == QEvent::Move) {
@ -187,6 +243,9 @@ bool DialogProxy::eventFilter(QObject *watched, QEvent *event)
if (me->oldPos().y() != me->pos().y()) {
emit yChanged();
}
if ((me->oldPos().x() != me->pos().x()) || (me->oldPos().y() != me->pos().y())) {
m_margins->checkMargins();
}
} else if (watched == m_dialog && event->type() == QEvent::Resize) {
QResizeEvent *re = static_cast<QResizeEvent *>(event);
if (re->oldSize().width() != re->size().width()) {

View File

@ -32,6 +32,41 @@ namespace Plasma
class DeclarativeItemContainer;
class DialogMargins : public QObject
{
Q_OBJECT
Q_PROPERTY(int left READ left NOTIFY leftChanged)
Q_PROPERTY(int top READ top NOTIFY topChanged)
Q_PROPERTY(int right READ right NOTIFY rightChanged)
Q_PROPERTY(int bottom READ bottom NOTIFY bottomChanged)
public:
DialogMargins(Plasma::Dialog *dialog, QObject *parent = 0);
int left() const;
int top() const;
int right() const;
int bottom() const;
Q_SIGNALS:
void leftChanged();
void rightChanged();
void topChanged();
void bottomChanged();
protected:
void checkMargins();
private:
int m_left;
int m_top;
int m_right;
int m_bottom;
Plasma::Dialog *m_dialog;
friend class DialogProxy;
};
class DialogProxy : public QObject
{
Q_OBJECT
@ -44,6 +79,7 @@ class DialogProxy : public QObject
Q_PROPERTY(int width READ width NOTIFY widthChanged)
Q_PROPERTY(int height READ width NOTIFY heightChanged)
Q_PROPERTY(int windowFlags READ windowFlags WRITE setWindowFlags)
Q_PROPERTY(QObject *margins READ margins CONSTANT)
public:
enum WidgetAttribute {
@ -72,6 +108,8 @@ public:
int windowFlags() const;
void setWindowFlags(const int);
QObject *margins() const;
//FIXME: alignment should be Qt::AlignmentFlag
Q_INVOKABLE QPoint popupPosition(QGraphicsObject *item, int alignment=Qt::AlignLeft) const;
//FIXME:: Qt::WidgetAttribute should be already
@ -96,6 +134,7 @@ private:
Qt::WindowFlags m_flags;
DeclarativeItemContainer *m_declarativeItemContainer;
QWeakPointer<QGraphicsObject> m_mainItem;
DialogMargins *m_margins;
};
#endif