FluentUI/src/Screenshot.cpp

71 lines
2.7 KiB
C++
Raw Normal View History

2023-08-16 18:05:49 +08:00
#include "Screenshot.h"
#include <QGuiApplication>
#include <QScreen>
#include <QQuickWindow>
2023-08-17 22:06:26 +08:00
#include <QDir>
2023-08-17 17:14:31 +08:00
#include <QtMath>
2023-08-22 18:19:40 +08:00
#include <QDateTime>
2023-08-18 12:45:16 +08:00
#include <QThreadPool>
2023-08-22 18:19:40 +08:00
#include "Def.h"
2023-09-13 15:11:22 +08:00
Screenshot::Screenshot(QQuickItem* parent):QQuickPaintedItem(parent){
2023-08-17 17:14:31 +08:00
_desktopGeometry = qApp->primaryScreen()->virtualGeometry();
2023-09-08 13:54:40 +08:00
maskColor(QColor(0,0,0,150));
2023-08-16 18:05:49 +08:00
start(QPoint(0,0));
end(QPoint(0,0));
connect(this,&Screenshot::startChanged,this,[=]{update();});
connect(this,&Screenshot::endChanged,this,[=]{update();});
}
2023-09-13 15:11:22 +08:00
void Screenshot::paint(QPainter* painter){
2023-08-16 18:05:49 +08:00
painter->save();
2023-08-16 22:23:58 +08:00
painter->fillRect(_desktopGeometry,_maskColor);
2023-08-16 18:05:49 +08:00
painter->setCompositionMode(QPainter::CompositionMode_Clear);
painter->fillRect(QRect(_start.x(),_start.y(),_end.x()-_start.x(),_end.y()-_start.y()), Qt::black);
painter->restore();
}
2023-08-16 23:25:52 +08:00
2023-09-13 15:11:22 +08:00
ScreenshotBackground::ScreenshotBackground(QQuickItem* parent) : QQuickPaintedItem(parent){
2023-08-17 17:14:31 +08:00
_devicePixelRatio = qApp->primaryScreen()->devicePixelRatio();
2023-08-16 23:25:52 +08:00
_desktopGeometry = qApp->primaryScreen()->virtualGeometry();
_desktopPixmap = qApp->primaryScreen()->grabWindow(0, _desktopGeometry.x(), _desktopGeometry.y(), _desktopGeometry.width(), _desktopGeometry.height());
2023-08-17 17:14:31 +08:00
int w = qApp->primaryScreen()->geometry().width();
int h = qApp->primaryScreen()->geometry().height();
foreach (auto item, qApp->screens()) {
if(item != qApp->primaryScreen()){
w = w + item->geometry().width()/qApp->primaryScreen()->devicePixelRatio();
}
}
setWidth(w);
setHeight(h);
2023-08-16 23:25:52 +08:00
}
2023-09-13 15:11:22 +08:00
void ScreenshotBackground::paint(QPainter* painter){
2023-08-16 23:25:52 +08:00
painter->save();
2023-08-18 12:45:16 +08:00
_sourcePixmap = _desktopPixmap.copy();
painter->drawPixmap(_desktopGeometry,_sourcePixmap);
2023-08-16 23:25:52 +08:00
painter->restore();
}
2023-08-17 17:14:31 +08:00
void ScreenshotBackground::capture(const QPoint& start,const QPoint& end){
2023-08-30 18:44:07 +08:00
update();
2023-08-18 12:45:16 +08:00
auto pixelRatio = qApp->primaryScreen()->devicePixelRatio();
auto x = qMin(start.x(),end.x()) * pixelRatio;
auto y = qMin(start.y(),end.y()) * pixelRatio;
auto w = qAbs(end.x()-start.x()) * pixelRatio;
auto h = qAbs(end.y()-start.y()) * pixelRatio;
2023-08-17 17:14:31 +08:00
_captureRect = QRect(x,y,w,h);
2023-08-17 22:06:26 +08:00
if(_captureMode == FluScreenshotType::CaptrueMode::Pixmap){
Q_EMIT captrueToPixmapCompleted(_sourcePixmap.copy(_captureRect));
}
if(_captureMode == FluScreenshotType::CaptrueMode::File){
2023-08-17 23:03:00 +08:00
QDir dir = _saveFolder;
if (!dir.exists(_saveFolder)){
dir.mkpath(_saveFolder);
}
auto filePath = _saveFolder.append("/").append(QString::number(QDateTime::currentDateTime().toMSecsSinceEpoch())).append(".png");
2023-08-17 22:06:26 +08:00
_sourcePixmap.copy(_captureRect).save(filePath);
Q_EMIT captrueToFileCompleted(QUrl::fromLocalFile(filePath));
}
2023-08-17 17:14:31 +08:00
}