~arjunak234-deactivatedaccount/kde-workspace/fix125114

« back to all changes in this revision

Viewing changes to libs/kworkspace/screenpreviewwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE libraries
 
2
 
 
3
   Copyright (C) 2009 Marco Martin <notmart@gmail.com>
 
4
 
 
5
   This library is free software; you can redistribute it and/or
 
6
   modify it under the terms of the GNU Library General Public
 
7
   License as published by the Free Software Foundation; either
 
8
   version 2 of the License, or (at your option) any later version.
 
9
 
 
10
   This library is distributed in the hope that it will be useful,
 
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
   Library General Public License for more details.
 
14
 
 
15
   You should have received a copy of the GNU Library General Public License
 
16
   along with this library; see the file COPYING.LIB.  If not, write to
 
17
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
18
   Boston, MA 02110-1301, USA.
 
19
*/
 
20
 
 
21
#include "screenpreviewwidget.h"
 
22
 
 
23
#include <QResizeEvent>
 
24
#include <QPaintEvent>
 
25
#include <QPainter>
 
26
 
 
27
#include <KDebug>
 
28
 
 
29
#include "Plasma/FrameSvg"
 
30
#include "Plasma/Wallpaper"
 
31
 
 
32
 
 
33
class ScreenPreviewWidgetPrivate
 
34
{
 
35
public:
 
36
    ScreenPreviewWidgetPrivate(ScreenPreviewWidget *screen)
 
37
          : q(screen),
 
38
            wallpaper(0),
 
39
            ratio(1)
 
40
    {}
 
41
 
 
42
    ~ScreenPreviewWidgetPrivate()
 
43
    {}
 
44
 
 
45
    void updateRect(const QRectF& rect)
 
46
    {
 
47
        q->update(rect.toRect());
 
48
    }
 
49
 
 
50
    void updateScreenGraphics()
 
51
    {
 
52
        int bottomElements = screenGraphics->elementSize("base").height() + screenGraphics->marginSize(Plasma::BottomMargin);
 
53
        QRect bounds(QPoint(0,0), QSize(q->size().width(), q->height() - bottomElements));
 
54
 
 
55
        QSize monitorSize(q->size().width(), q->size().width()/ratio);
 
56
        monitorSize.scale(bounds.size(), Qt::KeepAspectRatio);
 
57
 
 
58
        if (monitorSize.isEmpty()) {
 
59
            return;
 
60
        }
 
61
 
 
62
        monitorRect = QRect(QPoint(0,0), monitorSize);
 
63
        monitorRect.moveCenter(bounds.center());
 
64
 
 
65
        screenGraphics->resizeFrame(monitorRect.size());
 
66
 
 
67
        previewRect = screenGraphics->contentsRect().toRect();
 
68
        previewRect.moveCenter(bounds.center());
 
69
 
 
70
        if (wallpaper && !previewRect.isEmpty()) {
 
71
            wallpaper->setBoundingRect(previewRect);
 
72
        }
 
73
    }
 
74
 
 
75
    void wallpaperDeleted()
 
76
    {
 
77
        wallpaper = 0;
 
78
    }
 
79
 
 
80
    ScreenPreviewWidget *q;
 
81
    Plasma::Wallpaper* wallpaper;
 
82
    Plasma::FrameSvg *screenGraphics;
 
83
    QPixmap preview;
 
84
    QRect monitorRect;
 
85
    qreal ratio;
 
86
    QRect previewRect;
 
87
};
 
88
 
 
89
ScreenPreviewWidget::ScreenPreviewWidget(QWidget *parent)
 
90
    : QWidget(parent),
 
91
      d(new ScreenPreviewWidgetPrivate(this))
 
92
{
 
93
    d->screenGraphics = new Plasma::FrameSvg(this);
 
94
    d->screenGraphics->setImagePath("widgets/monitor");
 
95
    d->updateScreenGraphics();
 
96
}
 
97
 
 
98
ScreenPreviewWidget::~ScreenPreviewWidget()
 
99
{
 
100
   delete d;
 
101
}
 
102
 
 
103
void ScreenPreviewWidget::setPreview(const QPixmap &preview)
 
104
{
 
105
    d->preview = preview;
 
106
 
 
107
    if (d->wallpaper) {
 
108
        disconnect(d->wallpaper, 0, this, 0);
 
109
        d->wallpaper = 0;
 
110
    }
 
111
    update();
 
112
}
 
113
 
 
114
const QPixmap ScreenPreviewWidget::preview() const
 
115
{
 
116
    return d->preview;
 
117
}
 
118
 
 
119
void ScreenPreviewWidget::setPreview(Plasma::Wallpaper* wallpaper)
 
120
{
 
121
    d->preview = QPixmap();
 
122
 
 
123
    if (d->wallpaper) {
 
124
        disconnect(d->wallpaper, 0, this, 0);
 
125
    }
 
126
 
 
127
    d->wallpaper = wallpaper;
 
128
 
 
129
    if (d->wallpaper) {
 
130
        connect(d->wallpaper, SIGNAL(update(QRectF)), this, SLOT(updateRect(QRectF)));
 
131
        connect(d->wallpaper, SIGNAL(destroyed(QObject*)), this, SLOT(wallpaperDeleted()));
 
132
        d->updateScreenGraphics();
 
133
    }
 
134
 
 
135
    update(d->previewRect);
 
136
}
 
137
 
 
138
void ScreenPreviewWidget::setRatio(const qreal ratio)
 
139
{
 
140
    d->ratio = ratio;
 
141
    d->updateScreenGraphics();
 
142
}
 
143
 
 
144
qreal ScreenPreviewWidget::ratio() const
 
145
{
 
146
    return d->ratio;
 
147
}
 
148
 
 
149
QRect ScreenPreviewWidget::previewRect() const
 
150
{
 
151
    return d->previewRect;
 
152
}
 
153
 
 
154
void ScreenPreviewWidget::resizeEvent(QResizeEvent *e)
 
155
{
 
156
    Q_UNUSED(e)
 
157
    d->updateScreenGraphics();
 
158
}
 
159
 
 
160
void ScreenPreviewWidget::paintEvent(QPaintEvent *event)
 
161
{
 
162
    if (d->monitorRect.size().isEmpty()) {
 
163
        return;
 
164
    }
 
165
 
 
166
    QPainter painter(this);
 
167
    QPoint standPosition(d->monitorRect.center().x() - d->screenGraphics->elementSize("base").width()/2, d->previewRect.bottom());
 
168
 
 
169
    d->screenGraphics->paint(&painter, QRect(standPosition, d->screenGraphics->elementSize("base")), "base");
 
170
    d->screenGraphics->paintFrame(&painter, d->monitorRect.topLeft());
 
171
 
 
172
    painter.save();
 
173
    if (d->wallpaper) {
 
174
        d->wallpaper->paint(&painter, event->rect().intersected(d->previewRect));
 
175
    } else if (!d->preview.isNull()) {
 
176
        painter.setRenderHint(QPainter::SmoothPixmapTransform);
 
177
        painter.drawPixmap(d->previewRect, d->preview, d->preview.rect());
 
178
    }
 
179
    painter.restore();
 
180
 
 
181
    d->screenGraphics->paint(&painter, d->previewRect, "glass");
 
182
}
 
183
 
 
184
void ScreenPreviewWidget::dropEvent(QDropEvent *e)
 
185
{
 
186
    if (!KUrl::List::canDecode(e->mimeData()))
 
187
        return;
 
188
 
 
189
    const KUrl::List uris(KUrl::List::fromMimeData(e->mimeData()));
 
190
    if (!uris.isEmpty()) {
 
191
        // TODO: Download remote file
 
192
        if (uris.first().isLocalFile())
 
193
           emit imageDropped(uris.first().path());
 
194
    }
 
195
}
 
196
 
 
197
#include "screenpreviewwidget.moc"