2013-09-29 08:09:28 +00:00
|
|
|
/* This file is part of the KDE libraries
|
|
|
|
|
2020-08-02 22:10:35 +00:00
|
|
|
SPDX-FileCopyrightText: 2009 Marco Martin <notmart@gmail.com>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: LGPL-2.0-or-later
|
2013-09-29 08:09:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "screenpreviewwidget.h"
|
|
|
|
|
|
|
|
#include <QResizeEvent>
|
|
|
|
#include <QPaintEvent>
|
|
|
|
#include <QPainter>
|
|
|
|
|
2013-11-01 11:29:05 +00:00
|
|
|
#include <QDebug>
|
2013-12-05 19:22:31 +00:00
|
|
|
#include <QMimeData>
|
2013-09-29 08:09:28 +00:00
|
|
|
|
2013-09-29 08:20:49 +00:00
|
|
|
#include <Plasma/FrameSvg>
|
2013-12-05 19:22:31 +00:00
|
|
|
#include <kurlmimedata.h>
|
2013-09-29 08:09:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ScreenPreviewWidgetPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ScreenPreviewWidgetPrivate(ScreenPreviewWidget *screen)
|
|
|
|
: q(screen),
|
|
|
|
ratio(1)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~ScreenPreviewWidgetPrivate()
|
|
|
|
{}
|
|
|
|
|
|
|
|
void updateRect(const QRectF& rect)
|
|
|
|
{
|
|
|
|
q->update(rect.toRect());
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateScreenGraphics()
|
|
|
|
{
|
2013-09-29 08:20:49 +00:00
|
|
|
int bottomElements = screenGraphics->elementSize("base").height() + screenGraphics->marginSize(Plasma::Types::BottomMargin);
|
2013-09-29 08:09:28 +00:00
|
|
|
QRect bounds(QPoint(0,0), QSize(q->size().width(), q->height() - bottomElements));
|
|
|
|
|
|
|
|
QSize monitorSize(q->size().width(), q->size().width()/ratio);
|
|
|
|
monitorSize.scale(bounds.size(), Qt::KeepAspectRatio);
|
|
|
|
|
|
|
|
if (monitorSize.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
monitorRect = QRect(QPoint(0,0), monitorSize);
|
|
|
|
monitorRect.moveCenter(bounds.center());
|
|
|
|
|
|
|
|
screenGraphics->resizeFrame(monitorRect.size());
|
|
|
|
|
|
|
|
previewRect = screenGraphics->contentsRect().toRect();
|
|
|
|
previewRect.moveCenter(bounds.center());
|
|
|
|
}
|
|
|
|
|
|
|
|
ScreenPreviewWidget *q;
|
|
|
|
Plasma::FrameSvg *screenGraphics;
|
|
|
|
QPixmap preview;
|
|
|
|
QRect monitorRect;
|
|
|
|
qreal ratio;
|
|
|
|
QRect previewRect;
|
|
|
|
};
|
|
|
|
|
|
|
|
ScreenPreviewWidget::ScreenPreviewWidget(QWidget *parent)
|
|
|
|
: QWidget(parent),
|
|
|
|
d(new ScreenPreviewWidgetPrivate(this))
|
|
|
|
{
|
|
|
|
d->screenGraphics = new Plasma::FrameSvg(this);
|
|
|
|
d->screenGraphics->setImagePath("widgets/monitor");
|
|
|
|
d->updateScreenGraphics();
|
|
|
|
}
|
|
|
|
|
|
|
|
ScreenPreviewWidget::~ScreenPreviewWidget()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenPreviewWidget::setPreview(const QPixmap &preview)
|
|
|
|
{
|
|
|
|
d->preview = preview;
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
const QPixmap ScreenPreviewWidget::preview() const
|
|
|
|
{
|
|
|
|
return d->preview;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenPreviewWidget::setRatio(const qreal ratio)
|
|
|
|
{
|
|
|
|
d->ratio = ratio;
|
|
|
|
d->updateScreenGraphics();
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal ScreenPreviewWidget::ratio() const
|
|
|
|
{
|
|
|
|
return d->ratio;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect ScreenPreviewWidget::previewRect() const
|
|
|
|
{
|
|
|
|
return d->previewRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenPreviewWidget::resizeEvent(QResizeEvent *e)
|
|
|
|
{
|
|
|
|
Q_UNUSED(e)
|
|
|
|
d->updateScreenGraphics();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenPreviewWidget::paintEvent(QPaintEvent *event)
|
|
|
|
{
|
2014-02-03 10:36:21 +00:00
|
|
|
Q_UNUSED(event)
|
2013-09-29 08:09:28 +00:00
|
|
|
if (d->monitorRect.size().isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPainter painter(this);
|
|
|
|
QPoint standPosition(d->monitorRect.center().x() - d->screenGraphics->elementSize("base").width()/2, d->previewRect.bottom());
|
|
|
|
|
|
|
|
d->screenGraphics->paint(&painter, QRect(standPosition, d->screenGraphics->elementSize("base")), "base");
|
|
|
|
d->screenGraphics->paintFrame(&painter, d->monitorRect.topLeft());
|
|
|
|
|
|
|
|
painter.save();
|
2013-09-29 08:20:49 +00:00
|
|
|
if (!d->preview.isNull()) {
|
2013-09-29 08:09:28 +00:00
|
|
|
painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
|
|
|
painter.drawPixmap(d->previewRect, d->preview, d->preview.rect());
|
|
|
|
}
|
|
|
|
painter.restore();
|
|
|
|
|
|
|
|
d->screenGraphics->paint(&painter, d->previewRect, "glass");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenPreviewWidget::dropEvent(QDropEvent *e)
|
|
|
|
{
|
2013-12-05 19:22:31 +00:00
|
|
|
if (!e->mimeData()->hasUrls())
|
2013-09-29 08:09:28 +00:00
|
|
|
return;
|
|
|
|
|
2013-12-05 19:22:31 +00:00
|
|
|
QList<QUrl> uris(KUrlMimeData::urlsFromMimeData(e->mimeData()));
|
2013-09-29 08:09:28 +00:00
|
|
|
if (!uris.isEmpty()) {
|
|
|
|
// TODO: Download remote file
|
|
|
|
if (uris.first().isLocalFile())
|
|
|
|
emit imageDropped(uris.first().path());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-29 08:20:49 +00:00
|
|
|
#include "moc_screenpreviewwidget.cpp"
|