KShare/screenshotutil.cpp

64 lines
1.8 KiB
C++
Raw Normal View History

2017-04-23 15:05:48 +02:00
#include "screenshotutil.hpp"
#include <QApplication>
#include <QClipboard>
#include <QDebug>
2017-05-13 23:33:36 +02:00
#include <QPainter>
2017-04-23 15:05:48 +02:00
#include <QPixmap>
#include <QScreen>
2017-05-13 23:33:36 +02:00
#include <platformbackend.hpp>
2017-04-23 15:05:48 +02:00
QPixmap screenshotutil::fullscreen(bool cursor) {
2017-07-03 16:14:19 +02:00
QPixmap image;
2017-07-03 16:18:56 +02:00
QPainter painter;
2017-07-03 16:14:19 +02:00
// Hack for https://bugreports.qt.io/browse/QTBUG-58110
static QStringList qVer = QString(qVersion()).split('.');
2017-07-03 16:25:54 +02:00
#ifdef Q_OS_LINUX
2017-07-03 16:14:19 +02:00
if (qVer.at(0).toInt() == 5 && qVer.at(1).toInt() < 9) {
2017-07-03 16:23:44 +02:00
image = window(0);
painter.begin(&image);
} else {
2017-07-03 16:25:54 +02:00
#endif
2017-07-03 16:14:19 +02:00
int height = 0, width = 0;
for (QScreen *screen : QApplication::screens()) {
QRect geo = screen->geometry();
width = qMax(geo.left() + geo.width(), width);
height = qMax(geo.top() + geo.height(), height);
}
image = QPixmap(width, height);
image.fill(Qt::transparent);
width = 0;
2017-07-03 16:18:56 +02:00
painter.begin(&image);
2017-07-03 16:14:19 +02:00
for (QScreen *screen : QApplication::screens()) {
QPixmap currentScreen = window(0, screen);
painter.drawPixmap(screen->geometry().topLeft(), currentScreen);
width += screen->size().width();
}
2017-07-03 16:25:54 +02:00
#ifdef Q_OS_LINUX
2017-07-03 16:18:56 +02:00
}
2017-07-03 16:25:54 +02:00
#endif
#ifdef PLATFORM_CAPABILITY_CURSOR
if (cursor) {
auto cursorData = PlatformBackend::inst().getCursor();
painter.drawPixmap(QCursor::pos() - std::get<0>(cursorData), std::get<1>(cursorData));
}
#endif
painter.end();
2017-07-01 17:24:03 +02:00
return image;
}
2017-04-23 15:05:48 +02:00
QPixmap screenshotutil::window(WId wid, QScreen *w) {
return w->grabWindow(wid);
2017-04-23 15:05:48 +02:00
}
2017-05-06 13:21:12 +02:00
void screenshotutil::toClipboard(QString value) {
QApplication::clipboard()->setText(value);
2017-04-23 15:05:48 +02:00
}
2017-05-19 22:32:23 +02:00
QPixmap screenshotutil::fullscreenArea(bool cursor, qreal x, qreal y, qreal w, qreal h) {
return fullscreen(cursor).copy(x, y, w, h);
2017-05-19 22:32:23 +02:00
}