~sil2100/unity-2d/precise-security

« back to all changes in this revision

Viewing changes to panel/lib/panel.cpp

  • Committer: Aurelien Gateau
  • Date: 2010-11-10 08:57:29 UTC
  • mto: This revision was merged to the branch mainline in revision 284.
  • Revision ID: aurelien.gateau@canonical.com-20101110085729-fl1ye7impkqhm0w6
Added a section about const correct-ness

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of unity-qt
 
3
 *
 
4
 * Copyright 2010 Canonical Ltd.
 
5
 *
 
6
 * Authors:
 
7
 * - Aurélien Gâteau <aurelien.gateau@canonical.com>
 
8
 *
 
9
 * License: LGPL v3
 
10
 */
 
11
// Self
 
12
#include "panel.h"
 
13
 
 
14
// Local
 
15
#include "applet.h"
 
16
 
 
17
// Qt
 
18
#include <QApplication>
 
19
#include <QDebug>
 
20
#include <QDesktopWidget>
 
21
#include <QPainter>
 
22
#include <QHBoxLayout>
 
23
#include <QX11Info>
 
24
 
 
25
// X
 
26
#include <X11/Xlib.h>
 
27
#include <X11/Xatom.h>
 
28
 
 
29
namespace UnityQt
 
30
{
 
31
 
 
32
struct PanelPrivate
 
33
{
 
34
    Panel* q;
 
35
    Panel::Edge m_edge;
 
36
    QHBoxLayout* m_layout;
 
37
 
 
38
    void updateStrut()
 
39
    {
 
40
        QDesktopWidget* desktop = QApplication::desktop();
 
41
        const QRect screen = desktop->screenGeometry(q);
 
42
        const QRect available = desktop->availableGeometry(q);
 
43
        QRect rect;
 
44
 
 
45
        Atom atom = XInternAtom(QX11Info::display(), "_NET_WM_STRUT_PARTIAL", False);
 
46
 
 
47
        ulong struts[12];
 
48
        switch (m_edge) {
 
49
        case Panel::LeftEdge:
 
50
            rect = QRect(screen.left(), available.top(), q->width(), available.height());
 
51
            struts = {
 
52
                q->width(), 0, 0, 0,
 
53
                available.top(), available.bottom(), 0, 0,
 
54
                0, 0, 0, 0
 
55
                };
 
56
            break;
 
57
        case Panel::TopEdge:
 
58
            rect = QRect(screen.left(), screen.top(), screen.width(), q->height());
 
59
            struts = {
 
60
                0, 0, q->height(), 0,
 
61
                0, 0, 0, 0,
 
62
                screen.left(), screen.right(), 0, 0
 
63
                };
 
64
            break;
 
65
        }
 
66
 
 
67
        q->setGeometry(rect);
 
68
 
 
69
        XChangeProperty(QX11Info::display(), q->effectiveWinId(), atom,
 
70
                        XA_CARDINAL, 32, PropModeReplace,
 
71
                        (unsigned char *) &struts, 12);
 
72
    }
 
73
 
 
74
    void updateLayoutDirection()
 
75
    {
 
76
        QBoxLayout::Direction direction;
 
77
        switch(m_edge) {
 
78
        case Panel::TopEdge:
 
79
            direction = QApplication::isRightToLeft() ? QBoxLayout::RightToLeft : QBoxLayout::LeftToRight;
 
80
            break;
 
81
        case Panel::LeftEdge:
 
82
            direction = QBoxLayout::TopToBottom;
 
83
            break;
 
84
        }
 
85
        m_layout->setDirection(direction);
 
86
    }
 
87
 
 
88
    void updateEdge()
 
89
    {
 
90
        updateStrut();
 
91
        updateLayoutDirection();
 
92
    }
 
93
};
 
94
 
 
95
Panel::Panel(QWidget* parent)
 
96
: QWidget(parent)
 
97
, d(new PanelPrivate)
 
98
{
 
99
    d->q = this;
 
100
    d->m_edge = Panel::TopEdge;
 
101
    d->m_layout = new QHBoxLayout(this);
 
102
    d->m_layout->setMargin(0);
 
103
    d->m_layout->setSpacing(0);
 
104
    setAttribute(Qt::WA_X11NetWmWindowTypeDock);
 
105
    setAutoFillBackground(true);
 
106
}
 
107
 
 
108
Panel::~Panel()
 
109
{
 
110
    delete d;
 
111
}
 
112
 
 
113
void Panel::setEdge(Panel::Edge edge)
 
114
{
 
115
    d->m_edge = edge;
 
116
    if (isVisible()) {
 
117
        d->updateEdge();
 
118
    }
 
119
}
 
120
 
 
121
Panel::Edge Panel::edge() const
 
122
{
 
123
    return d->m_edge;
 
124
}
 
125
 
 
126
void Panel::showEvent(QShowEvent* event)
 
127
{
 
128
    QWidget::showEvent(event);
 
129
    d->updateEdge();
 
130
}
 
131
 
 
132
void Panel::paintEvent(QPaintEvent* event)
 
133
{
 
134
    // Necessary because Oxygen thinks it knows better what to paint in the background
 
135
    QPainter painter(this);
 
136
    painter.setCompositionMode(QPainter::CompositionMode_Source);
 
137
    painter.fillRect(rect(), palette().brush(QPalette::Background));
 
138
}
 
139
 
 
140
void Panel::addWidget(QWidget* widget)
 
141
{
 
142
    d->m_layout->addWidget(widget);
 
143
}
 
144
 
 
145
void Panel::addSpacer()
 
146
{
 
147
    d->m_layout->addStretch();
 
148
}
 
149
 
 
150
} // namespace
 
151
 
 
152
#include "panel.moc"