Implement single method capture request
The core now has a method requestCapture, which receives a CaptureRequest object with all the needed information. This reduces code duplication in future features.
This commit is contained in:
@@ -21,6 +21,8 @@
|
||||
#include "src/widgets/infowindow.h"
|
||||
#include "src/config/configwindow.h"
|
||||
#include "src/widgets/capture/capturebutton.h"
|
||||
#include "src/utils/systemnotification.h"
|
||||
#include "src/utils/screengrabber.h"
|
||||
#include <QFile>
|
||||
#include <QApplication>
|
||||
#include <QSystemTrayIcon>
|
||||
@@ -53,6 +55,11 @@ Controller::Controller() : m_captureWindow(nullptr) {
|
||||
|
||||
QString StyleSheet = CaptureButton::globalStyleSheet();
|
||||
qApp->setStyleSheet(StyleSheet);
|
||||
|
||||
connect(this, &Controller::captureTaken,
|
||||
this, &Controller::handleCaptureTaken);
|
||||
connect(this, &Controller::captureFailed,
|
||||
this, &Controller::handleCaptureFailed);
|
||||
}
|
||||
|
||||
Controller *Controller::getInstance() {
|
||||
@@ -60,8 +67,30 @@ Controller *Controller::getInstance() {
|
||||
return &c;
|
||||
}
|
||||
|
||||
void Controller::requestCapture(const CaptureRequest &request) {
|
||||
uint id = request.id();
|
||||
m_requestMap.insert(id, request);
|
||||
|
||||
switch (request.captureMode()) {
|
||||
case CaptureRequest::FULLSCREEN_MODE:
|
||||
doLater(request.delay(), this, [this, id](){
|
||||
this->startFullscreenCapture(id);
|
||||
});
|
||||
break;
|
||||
case CaptureRequest::GRAPHICAL_MODE: {
|
||||
QString &&path = request.path();
|
||||
doLater(request.delay(), this, [this, id, path](){
|
||||
this->startVisualCapture(id, path);
|
||||
});
|
||||
break;
|
||||
} default:
|
||||
emit captureFailed(id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// creation of a new capture in GUI mode
|
||||
void Controller::createVisualCapture(const uint id, const QString &forcedSavePath) {
|
||||
void Controller::startVisualCapture(const uint id, const QString &forcedSavePath) {
|
||||
if (!m_captureWindow) {
|
||||
QWidget *modalWidget = nullptr;
|
||||
do {
|
||||
@@ -77,12 +106,15 @@ void Controller::createVisualCapture(const uint id, const QString &forcedSavePat
|
||||
this, &Controller::captureFailed);
|
||||
connect(m_captureWindow, &CaptureWidget::captureTaken,
|
||||
this, &Controller::captureTaken);
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
m_captureWindow->show();
|
||||
#else
|
||||
m_captureWindow->showFullScreen();
|
||||
//m_captureWindow->show(); // Debug
|
||||
#endif
|
||||
} else {
|
||||
emit captureFailed(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,7 +141,7 @@ void Controller::enableTrayIcon() {
|
||||
QAction *captureAction = new QAction(tr("&Take Screenshot"), this);
|
||||
connect(captureAction, &QAction::triggered, this, [this](){
|
||||
// Wait 400 ms to hide the QMenu
|
||||
doLater(400, this, [this](){ this->createVisualCapture(); });
|
||||
doLater(400, this, [this](){ this->startVisualCapture(); });
|
||||
});
|
||||
QAction *configAction = new QAction(tr("&Configuration"), this);
|
||||
connect(configAction, &QAction::triggered, this,
|
||||
@@ -136,7 +168,7 @@ void Controller::enableTrayIcon() {
|
||||
|
||||
auto trayIconActivated = [this](QSystemTrayIcon::ActivationReason r){
|
||||
if (r == QSystemTrayIcon::Trigger) {
|
||||
createVisualCapture();
|
||||
startVisualCapture();
|
||||
}
|
||||
};
|
||||
connect(m_trayIcon, &QSystemTrayIcon::activated, this, trayIconActivated);
|
||||
@@ -168,7 +200,33 @@ void Controller::updateConfigComponents() {
|
||||
}
|
||||
}
|
||||
|
||||
void doLater(int msec, QObject *receiver, lambda func) {
|
||||
void Controller::startFullscreenCapture(const uint id) {
|
||||
bool ok = true;
|
||||
QPixmap p(ScreenGrabber().grabEntireDesktop(ok));
|
||||
if (ok) {
|
||||
emit captureTaken(id, p);
|
||||
} else {
|
||||
emit captureFailed(id);
|
||||
}
|
||||
}
|
||||
|
||||
void Controller::handleCaptureTaken(uint id, QPixmap p) {
|
||||
auto it = m_requestMap.find(id);
|
||||
if (it != m_requestMap.end()) {
|
||||
it.value().exportCapture(p);
|
||||
m_requestMap.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
void Controller::handleCaptureFailed(uint id) {
|
||||
m_requestMap.remove(id);
|
||||
}
|
||||
|
||||
void Controller::doLater(int msec, QObject *receiver, lambda func) {
|
||||
if (msec == 0) {
|
||||
func();
|
||||
return;
|
||||
}
|
||||
QTimer *timer = new QTimer(receiver);
|
||||
QObject::connect(timer, &QTimer::timeout, receiver,
|
||||
[timer, func](){ func(); timer->deleteLater(); });
|
||||
|
||||
Reference in New Issue
Block a user