desktop.cpp 6.1 KB
Newer Older
1
2
/*
*   Copyright 2007 by Aaron Seigo <aseigo@kde.org>
Alexis Ménard's avatar
Alexis Ménard committed
3
*   Copyright 2008 by Alexis Ménard <darktears31@gmail.com>
4
*
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
*
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU Library General Public License version 2,
*   or (at your option) any later version.
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details
*
*   You should have received a copy of the GNU Library General Public
*   License along with this program; if not, write to the
*   Free Software Foundation, Inc.,
*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "desktop.h"

23
24
#include "krunner_interface.h"

25
26
#include <KDebug>

27
#include <Plasma/Corona>
Patrick Spendrin's avatar
Patrick Spendrin committed
28

Laurent Montel's avatar
Laurent Montel committed
29
30
#include <QTimer>

31
using namespace Plasma;
32

33
34
DefaultDesktop::DefaultDesktop(QObject *parent, const QVariantList &args)
    : Containment(parent, args),
35
      m_refreshFails(0),
36
37
      dropping(false),
      m_startupCompleted(false)
38
{
39
40
    qRegisterMetaType<QImage>("QImage");
    qRegisterMetaType<QPersistentModelIndex>("QPersistentModelIndex");
41

42
43
44
    m_delayedRefreshTimer = new QTimer(this);
    m_delayedRefreshTimer->setSingleShot(true);

45
46
47
    m_layout = new DesktopLayout;
    m_layout->setAlignment(Qt::AlignTop|Qt::AlignLeft);
    m_layout->setPlacementSpacing(20);
48
    m_layout->setScreenSpacing(0);
49
    m_layout->setShiftingSpacing(0);
Ambroz Bizjak's avatar
Ambroz Bizjak committed
50
    m_layout->setTemporaryPlacement(true);
51
    m_layout->setVisibilityTolerance(0.5);
52

53
    resize(800, 600);
54
55

    setHasConfigurationInterface(true);
56
    //kDebug() << "!!! loading desktop";
57
58
}

59
60
61
62
DefaultDesktop::~DefaultDesktop()
{
}

Marco Martin's avatar
Marco Martin committed
63
void DefaultDesktop::constraintsEvent(Plasma::Constraints constraints)
64
{
65
    if (constraints & Plasma::StartupCompletedConstraint) {
66
67
68
        if (m_startupCompleted) {
            return;
        }
69

70
        m_startupCompleted = true;
71

72
73
74
        connect(corona(), SIGNAL(availableScreenRegionChanged()),
                this, SLOT(refreshWorkingArea()));
        refreshWorkingArea();
75

Laurent Montel's avatar
Laurent Montel committed
76
77
78
79
        connect(this, SIGNAL(appletAdded(Plasma::Applet*,QPointF)),
                this, SLOT(onAppletAdded(Plasma::Applet*,QPointF)));
        connect(this, SIGNAL(appletRemoved(Plasma::Applet*)),
                this, SLOT(onAppletRemoved(Plasma::Applet*)));
80
81
82
83
84
85
86
87

        foreach (Applet *applet, applets()) {
            m_layout->addItem(applet, true, false);
            connect(applet, SIGNAL(appletTransformedByUser()), this, SLOT(onAppletTransformedByUser()));
            connect(applet, SIGNAL(appletTransformedItself()), this, SLOT(onAppletTransformedItself()));
        }

        m_layout->adjustPhysicalPositions();
88
89
    } else if ((constraints & Plasma::SizeConstraint) || (constraints & Plasma::ScreenConstraint)) {
        refreshWorkingArea();
90
    }
91
92
}

93
94
void DefaultDesktop::onAppletAdded(Plasma::Applet *applet, const QPointF &pos)
{
95
96
    if (dropping || pos != QPointF(-1,-1) || applet->geometry().topLeft() != QPointF(0,0)) {
        // add item to the layout using the current position
97
        m_layout->addItem(applet, true, false);
98
    } else {
99
100
        // auto-position
        m_layout->addItem(applet, true, true);
101
102
    }

103
104
105
106
    m_layout->adjustPhysicalPositions();

    connect(applet, SIGNAL(appletTransformedByUser()), this, SLOT(onAppletTransformedByUser()));
    connect(applet, SIGNAL(appletTransformedItself()), this, SLOT(onAppletTransformedItself()));
107
108
}

109
void DefaultDesktop::onAppletRemoved(Plasma::Applet *applet)
110
{
111
    for (int i=0; i < m_layout->count(); i++) {
112
        if (applet == m_layout->itemAt(i)) {
113
            m_layout->removeAt(i);
114
            m_layout->adjustPhysicalPositions();
115
            return;
116
117
118
119
        }
    }
}

120
121
void DefaultDesktop::onAppletTransformedByUser()
{
122
123
124
    Plasma::Applet *applet = static_cast<Plasma::Applet *>(sender());
    m_layout->itemTransformed(applet, DesktopLayout::ItemTransformUser);
    m_layout->adjustPhysicalPositions(applet);
125
126
127
}

void DefaultDesktop::onAppletTransformedItself()
128
{
129
130
131
    Plasma::Applet *applet = static_cast<Plasma::Applet *>(sender());
    m_layout->itemTransformed(applet, DesktopLayout::ItemTransformUser);
    m_layout->adjustPhysicalPositions(applet);
132
133
}

134
void DefaultDesktop::refreshWorkingArea()
135
{
136
137
    m_delayedRefreshTimer->stop();

138
139
140
    Corona *c = corona();
    if (!c) {
        //kDebug() << "no corona?!";
141
        m_delayedRefreshTimer->start(DELAYED_REFRESH_WAIT);
142
143
144
        return;
    }

145
    QRectF workingGeom;
146
    if (screen() != -1 && screen() < c->numScreens()) {
147
        // we are associated with a screen, make sure not to overlap panels
148
149
        workingGeom = c->availableScreenRegion(screen()).boundingRect();
        //kDebug() << "got" << workingGeom;
150
        // From screen coordinates to containment coordinates
151
        workingGeom.translate(-c->screenGeometry(screen()).topLeft());
152
    } else {
153
        workingGeom = mapFromScene(geometry()).boundingRect();
154
155
156
        //kDebug() << "defaults due to no screen; got:" << workingGeom;
    }

157
    if (workingGeom.isValid()) {
158
        //kDebug() << "!!!!!!!!!!!!! workingGeom is" << workingGeom;
159
        m_refreshFails = 0;
160
        m_layout->setWorkingArea(workingGeom);
161
        m_layout->adjustPhysicalPositions();
162
163
164
    } else if (m_refreshFails < MAX_REFRESH_FAILS) {
        ++m_refreshFails;
        m_delayedRefreshTimer->start(DELAYED_REFRESH_WAIT);
165
166
167
    }
}

168
169
void DefaultDesktop::dropEvent(QGraphicsSceneDragDropEvent *event)
{
170
    dropping = true;
171
    Containment::dropEvent(event);
172
    dropping = false;
173
174
}

175
176
void DefaultDesktop::keyPressEvent(QKeyEvent *event)
{
177
178
179
    // Only printable characters may trigger KRunner, otherwise a white-space
    // character is introduced and the term you've entered is not recognized
    if (focusItem() == this && !event->text().isEmpty() && event->text()[0].isPrint()) {
180
        const QString interface("org.kde.krunner");
181
182
183
184
185
186
187
188
        org::kde::krunner::App krunner(interface, "/App", QDBusConnection::sessionBus());
        krunner.query(event->text());
        event->accept();
    } else {
        event->ignore();
    }
}

189
190
191
K_EXPORT_PLASMA_APPLET(desktop, DefaultDesktop)

#include "desktop.moc"