1
/* This file is part of the KDE libraries
3
Copyright (C) 2009 Marco Martin <notmart@gmail.com>
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.
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.
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.
21
#include "screenpreviewwidget.h"
23
#include <QResizeEvent>
24
#include <QPaintEvent>
29
#include "Plasma/FrameSvg"
30
#include "Plasma/Wallpaper"
33
class ScreenPreviewWidgetPrivate
36
ScreenPreviewWidgetPrivate(ScreenPreviewWidget *screen)
42
~ScreenPreviewWidgetPrivate()
45
void updateRect(const QRectF& rect)
47
q->update(rect.toRect());
50
void updateScreenGraphics()
52
int bottomElements = screenGraphics->elementSize("base").height() + screenGraphics->marginSize(Plasma::BottomMargin);
53
QRect bounds(QPoint(0,0), QSize(q->size().width(), q->height() - bottomElements));
55
QSize monitorSize(q->size().width(), q->size().width()/ratio);
56
monitorSize.scale(bounds.size(), Qt::KeepAspectRatio);
58
if (monitorSize.isEmpty()) {
62
monitorRect = QRect(QPoint(0,0), monitorSize);
63
monitorRect.moveCenter(bounds.center());
65
screenGraphics->resizeFrame(monitorRect.size());
67
previewRect = screenGraphics->contentsRect().toRect();
68
previewRect.moveCenter(bounds.center());
70
if (wallpaper && !previewRect.isEmpty()) {
71
wallpaper->setBoundingRect(previewRect);
75
void wallpaperDeleted()
80
ScreenPreviewWidget *q;
81
Plasma::Wallpaper* wallpaper;
82
Plasma::FrameSvg *screenGraphics;
89
ScreenPreviewWidget::ScreenPreviewWidget(QWidget *parent)
91
d(new ScreenPreviewWidgetPrivate(this))
93
d->screenGraphics = new Plasma::FrameSvg(this);
94
d->screenGraphics->setImagePath("widgets/monitor");
95
d->updateScreenGraphics();
98
ScreenPreviewWidget::~ScreenPreviewWidget()
103
void ScreenPreviewWidget::setPreview(const QPixmap &preview)
105
d->preview = preview;
108
disconnect(d->wallpaper, 0, this, 0);
114
const QPixmap ScreenPreviewWidget::preview() const
119
void ScreenPreviewWidget::setPreview(Plasma::Wallpaper* wallpaper)
121
d->preview = QPixmap();
124
disconnect(d->wallpaper, 0, this, 0);
127
d->wallpaper = 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();
135
update(d->previewRect);
138
void ScreenPreviewWidget::setRatio(const qreal ratio)
141
d->updateScreenGraphics();
144
qreal ScreenPreviewWidget::ratio() const
149
QRect ScreenPreviewWidget::previewRect() const
151
return d->previewRect;
154
void ScreenPreviewWidget::resizeEvent(QResizeEvent *e)
157
d->updateScreenGraphics();
160
void ScreenPreviewWidget::paintEvent(QPaintEvent *event)
162
if (d->monitorRect.size().isEmpty()) {
166
QPainter painter(this);
167
QPoint standPosition(d->monitorRect.center().x() - d->screenGraphics->elementSize("base").width()/2, d->previewRect.bottom());
169
d->screenGraphics->paint(&painter, QRect(standPosition, d->screenGraphics->elementSize("base")), "base");
170
d->screenGraphics->paintFrame(&painter, d->monitorRect.topLeft());
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());
181
d->screenGraphics->paint(&painter, d->previewRect, "glass");
184
void ScreenPreviewWidget::dropEvent(QDropEvent *e)
186
if (!KUrl::List::canDecode(e->mimeData()))
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());
197
#include "screenpreviewwidget.moc"