~neon/kdeplasma-addons/trunk

« back to all changes in this revision

Viewing changes to containments/groupingdesktop/lib/groups/stackinggroup.cpp

  • Committer: asouza
  • Date: 2011-02-01 19:41:58 UTC
  • Revision ID: svn-v4:283d02a7-25f6-0310-bc7c-ecb5cbfe19da:trunk/KDE/kdeplasma-addons:1218275
Move kdeplasma-addons to git

- You can find information about the project in the link below:

https://projects.kde.org/projects/kde/kdeplasma-addons/repository

- And you can clone the repository using:

git clone git://anongit.kde.org/kdeplasma-addons


Thanks eean and all the sysadmins for the help.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
*   Copyright 2010 by Giulio Camuffo <giuliocamuffo@gmail.com>
3
 
*
4
 
*   This program is free software; you can redistribute it and/or modify
5
 
*   it under the terms of the GNU Library General Public License as
6
 
*   published by the Free Software Foundation; either version 2, or
7
 
*   (at your option) any later version.
8
 
*
9
 
*   This program is distributed in the hope that it will be useful,
10
 
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
*   GNU General Public License for more details
13
 
*
14
 
*   You should have received a copy of the GNU Library General Public
15
 
*   License along with this program; if not, write to the
16
 
*   Free Software Foundation, Inc.,
17
 
*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
 
*/
19
 
 
20
 
#include "stackinggroup.h"
21
 
 
22
 
#include <QtGui/QGraphicsSceneDragDropEvent>
23
 
#include <QPainter>
24
 
 
25
 
#include <Plasma/PaintUtils>
26
 
#include <Plasma/Corona>
27
 
 
28
 
#include "groupingcontainment.h"
29
 
#include "spacer.h"
30
 
 
31
 
REGISTER_GROUP(StackingGroup)
32
 
 
33
 
StackingGroup::StackingGroup(QGraphicsItem *parent, Qt::WindowFlags wFlags)
34
 
             : AbstractGroup(parent, wFlags),
35
 
               m_spacer(new Spacer(this))
36
 
{
37
 
    resize(200,200);
38
 
    setGroupType(AbstractGroup::ConstrainedGroup);
39
 
 
40
 
    m_spacer->hide();
41
 
 
42
 
    connect(this, SIGNAL(appletRemovedFromGroup(Plasma::Applet*,AbstractGroup*)),
43
 
            this, SLOT(onAppletRemoved(Plasma::Applet*,AbstractGroup*)));
44
 
    connect(this, SIGNAL(appletAddedInGroup(Plasma::Applet*,AbstractGroup*)),
45
 
            this, SLOT(onAppletAdded(Plasma::Applet*,AbstractGroup*)));
46
 
    connect(this, SIGNAL(subGroupRemovedFromGroup(AbstractGroup*,AbstractGroup*)),
47
 
            this, SLOT(onSubGroupRemoved(AbstractGroup*, AbstractGroup*)));
48
 
    connect(this, SIGNAL(subGroupAddedInGroup(AbstractGroup*,AbstractGroup*)),
49
 
            this, SLOT(onSubGroupAdded(AbstractGroup*,AbstractGroup*)));
50
 
}
51
 
 
52
 
StackingGroup::~StackingGroup()
53
 
{
54
 
 
55
 
}
56
 
 
57
 
QString StackingGroup::pluginName() const
58
 
{
59
 
    return QString("stacking");
60
 
}
61
 
 
62
 
void StackingGroup::restoreChildGroupInfo(QGraphicsWidget *child, const KConfigGroup &group)
63
 
{
64
 
    int from = m_children.indexOf(child);
65
 
    int to = group.readEntry("Index", 0);
66
 
 
67
 
    if (m_children.size() > to) {
68
 
        m_children.move(from, to);
69
 
 
70
 
        drawStack();
71
 
    }
72
 
}
73
 
 
74
 
void StackingGroup::saveChildGroupInfo(QGraphicsWidget *child, KConfigGroup group) const
75
 
{
76
 
    group.writeEntry("Index", m_children.indexOf(child));
77
 
}
78
 
 
79
 
void StackingGroup::layoutChild(QGraphicsWidget *child, const QPointF &)
80
 
{
81
 
    if (!m_children.contains(child)) {
82
 
        m_children << child;
83
 
    } else {
84
 
        m_children << m_children.takeAt(m_children.indexOf(child));
85
 
    }
86
 
 
87
 
    if (m_spacer->isVisible()) {
88
 
        m_children.removeOne(m_spacer);
89
 
        m_spacer->hide();
90
 
    }
91
 
 
92
 
    drawStack();
93
 
}
94
 
 
95
 
void StackingGroup::drawStack()
96
 
{
97
 
    int gap = 20;
98
 
    foreach (QGraphicsWidget *widget, m_children) {
99
 
        if (widget) {
100
 
            QRectF rect(QPointF(gap, gap), contentsRect().size());
101
 
            widget->setMaximumSize(rect.size());
102
 
            widget->setGeometry(rect);
103
 
            widget->setZValue(gap);
104
 
            gap = gap + 20;
105
 
        }
106
 
    }
107
 
}
108
 
 
109
 
void StackingGroup::resizeEvent(QGraphicsSceneResizeEvent *event)
110
 
{
111
 
    AbstractGroup::resizeEvent(event);
112
 
 
113
 
    drawStack();
114
 
}
115
 
 
116
 
void StackingGroup::wheelEvent(QGraphicsSceneWheelEvent *event)
117
 
{
118
 
    if (m_children.size() < 2) {
119
 
        return;
120
 
    }
121
 
 
122
 
    if (event->delta() > 0) {
123
 
        m_children.move(m_children.size() - 1, 0);
124
 
    } else {
125
 
        m_children << m_children.takeAt(0);
126
 
    }
127
 
 
128
 
    drawStack();
129
 
    saveChildren();
130
 
 
131
 
    event->accept();
132
 
}
133
 
 
134
 
void StackingGroup::releaseChild(QGraphicsWidget *child)
135
 
{
136
 
    m_children.removeOne(child);
137
 
}
138
 
 
139
 
void StackingGroup::onAppletAdded(Plasma::Applet *applet, AbstractGroup *)
140
 
{
141
 
    if (!m_children.contains(applet)) {
142
 
        m_children << applet;
143
 
        connect(applet, SIGNAL(activate()), this, SLOT(onAppletActivated()));
144
 
        applet->installEventFilter(this);
145
 
    }
146
 
}
147
 
 
148
 
void StackingGroup::onAppletRemoved(Plasma::Applet *applet, AbstractGroup *)
149
 
{
150
 
    m_children.removeOne(applet);
151
 
    applet->removeEventFilter(this);
152
 
 
153
 
    drawStack();
154
 
}
155
 
 
156
 
void StackingGroup::onSubGroupAdded(AbstractGroup *subGroup, AbstractGroup *)
157
 
{
158
 
    if (!m_children.contains(subGroup)) {
159
 
        m_children << subGroup;
160
 
        subGroup->installEventFilter(this);
161
 
    }
162
 
}
163
 
 
164
 
void StackingGroup::onSubGroupRemoved(AbstractGroup *subGroup, AbstractGroup *)
165
 
{
166
 
    m_children.removeOne(subGroup);
167
 
    subGroup->removeEventFilter(this);
168
 
 
169
 
    drawStack();
170
 
}
171
 
 
172
 
bool StackingGroup::showDropZone(const QPointF &pos)
173
 
{
174
 
    if (pos.isNull()) {
175
 
        m_spacer->hide();
176
 
        m_children.removeOne(m_spacer);
177
 
        return false;
178
 
    } else if (!m_spacer->isVisible()) {
179
 
        m_spacer->show();
180
 
        m_children << m_spacer;
181
 
    }
182
 
 
183
 
    drawStack();
184
 
 
185
 
    return true;
186
 
}
187
 
 
188
 
void StackingGroup::onAppletActivated()
189
 
{
190
 
    Plasma::Applet *applet = qobject_cast<Plasma::Applet *>(sender());
191
 
 
192
 
    if (applet && m_children.contains(applet)) {
193
 
        m_children << m_children.takeAt(m_children.indexOf(applet));
194
 
 
195
 
        drawStack();
196
 
    }
197
 
}
198
 
 
199
 
bool StackingGroup::eventFilter(QObject *obj, QEvent *event)
200
 
{
201
 
    if (event->type() == QEvent::GraphicsSceneMousePress) {
202
 
        QGraphicsWidget *widget = qobject_cast<QGraphicsWidget *>(obj);
203
 
        if (m_children.contains(widget)) {
204
 
            m_children << m_children.takeAt(m_children.indexOf(widget));
205
 
 
206
 
            drawStack();
207
 
        }
208
 
    }
209
 
 
210
 
    return AbstractGroup::eventFilter(obj, event);
211
 
}
212
 
 
213
 
GroupInfo StackingGroup::groupInfo()
214
 
{
215
 
    GroupInfo gi("stacking", i18n("Stacking Group"));
216
 
    gi.setIcon("object-order-raise"); //FIXME: isn't there a better one?
217
 
 
218
 
    return gi;
219
 
}
220
 
 
221
 
#include "stackinggroup.moc"