Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions src/screenshooter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,25 @@
#include <utils/freedesktopdbusresponse.h>
#endif

static QRect virtualDesktopRect()
{
QRect r;
for (QScreen *s : QGuiApplication::screens())
r = r.united(s->geometry());
return r;
}

const QImage ScreenShooter::captureFullscreen(bool captureMultipleMonitors)
{
QImage allMonitorsScreenshot = ScreenShooter::captureAllMonitors();

//If capturing only a single monitor, crop the image
if(!captureMultipleMonitors)
Comment thread
olav-st marked this conversation as resolved.
{
#if QT_VERSION >= QT_VERSION_CHECK(5,10,0)
QScreen* screen = QGuiApplication::screenAt(QCursor::pos());
#else
int screenNumber = QApplication::desktop()->screenNumber(QCursor::pos());
QScreen* screen = QApplication::screens().at(screenNumber);
#endif
return allMonitorsScreenshot.copy(screen->geometry());
QScreen* screen = QGuiApplication::screenAt(QCursor::pos());
if (!screen) screen = QApplication::primaryScreen();
QRect virt = virtualDesktopRect();
return allMonitorsScreenshot.copy(screen->geometry().translated(-virt.topLeft()));
}

//Otherwise return the image as is
Expand All @@ -49,9 +54,9 @@ const QImage ScreenShooter::captureFullscreen(bool captureMultipleMonitors)

const QImage ScreenShooter::captureSelection(const QRect &area)
{
QRect virt = virtualDesktopRect();
QImage allMonitorsScreenshot = ScreenShooter::captureAllMonitors();
QImage areaScreenshot = allMonitorsScreenshot.copy(area);
return areaScreenshot;
return allMonitorsScreenshot.copy(area.translated(-virt.topLeft()));
}

const QImage ScreenShooter::captureWindow(WId windowID, bool captureWindowBorders)
Expand Down Expand Up @@ -187,10 +192,19 @@ const QImage ScreenShooter::captureAllMonitors()
}
#endif
//If we are on Win/Mac, or not running on Wayland, use the Qt API to take a screenshot
// Grab each screen individually and composite them into one image.
INFO(QObject::tr("Capturing the screen using the Qt API"));
QScreen* screen = QApplication::primaryScreen();
QRect screenGeometry = screen->virtualGeometry();
QPixmap pixmap = screen->grabWindow(0, screenGeometry.x(), screenGeometry.y(), screenGeometry.width(), screenGeometry.height());

return pixmap.toImage();
QRect virt = virtualDesktopRect();
QImage result(virt.size(), QImage::Format_RGB32);
result.fill(Qt::black);
QPainter painter(&result);
for (QScreen *s : QGuiApplication::screens()) {
QImage screenImg = s->grabWindow(0).toImage();
QRect target = s->geometry().translated(-virt.topLeft());
if (screenImg.size() != target.size())
screenImg = screenImg.scaled(target.size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
painter.drawImage(target.topLeft(), screenImg);
}
painter.end();
return result;
}
2 changes: 2 additions & 0 deletions src/screenshooter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
#include <QObject>
#include <QPixmap>
#include <QApplication>
#include <QGuiApplication>
#include <QSettings>
#include <QScreen>
#include <QImage>
#include <QPainter>

class ScreenShooter
{
Expand Down
Loading