From 8870fd1ed4ca94c02bb2a09a3d5377288f4927e5 Mon Sep 17 00:00:00 2001 From: lupoDharkael Date: Tue, 25 Jul 2017 00:05:31 +0200 Subject: [PATCH] Add delayed capture support Adding this option required to modify the dbus API, with this changed I've improved the logic which saves the captures. --- dbus/org.dharkael.Flameshot.xml | 47 +++++++------------- src/capture/capturewidget.cpp | 57 ++++++++++--------------- src/capture/capturewidget.h | 7 +-- src/capture/screenshot.cpp | 17 +++++--- src/capture/screenshot.h | 6 ++- src/controller.cpp | 18 +++----- src/controller.h | 11 ++--- src/flameshotdbusadapter.cpp | 31 ++++++-------- src/flameshotdbusadapter.h | 6 +-- src/main.cpp | 48 ++++++++++----------- src/utils/filenamehandler.cpp | 8 ++-- translation/Internationalization_es.qm | Bin 11284 -> 11553 bytes translation/Internationalization_es.ts | 53 ++++++++++++----------- 13 files changed, 141 insertions(+), 168 deletions(-) diff --git a/dbus/org.dharkael.Flameshot.xml b/dbus/org.dharkael.Flameshot.xml index b48d2adc..02c62c24 100644 --- a/dbus/org.dharkael.Flameshot.xml +++ b/dbus/org.dharkael.Flameshot.xml @@ -2,48 +2,31 @@ - - - - - - - + + - - - - - - + diff --git a/src/capture/capturewidget.cpp b/src/capture/capturewidget.cpp index 8fcc965c..c2da75f8 100644 --- a/src/capture/capturewidget.cpp +++ b/src/capture/capturewidget.cpp @@ -47,13 +47,11 @@ namespace { // size of the handlers at the corners of the selection const int HANDLE_SIZE = 9; } - -// http://doc.qt.io/qt-5/qwidget.html#setMask - -CaptureWidget::CaptureWidget(bool enableSaveWindow, QWidget *parent) : +// enableSaveWIndow +CaptureWidget::CaptureWidget(const QString &forcedSavePath, QWidget *parent) : QWidget(parent), m_mouseOverHandle(0), m_mouseIsClicked(false), m_rightClick(false), m_newSelection(false), m_grabbing(false), - m_onButton(false), m_enableSaveWindow(enableSaveWindow), + m_onButton(false), m_forcedSavePath(forcedSavePath), m_state(CaptureButton::TYPE_MOVESELECTION) { m_showInitialMsg = ConfigHandler().getShowHelp(); @@ -389,37 +387,29 @@ void CaptureWidget::keyPressEvent(QKeyEvent *e) { } QString CaptureWidget::saveScreenshot(bool toClipboard) { - hide(); - QString path; - if (m_selection.isNull()) { - path = m_screenshot->graphicalSave(QRect(), this); - } else { // save full screen when no selection - path = m_screenshot->graphicalSave(getExtendedSelection(), this); - } - if (!path.isEmpty()) { - QString saveMessage(tr("Capture saved in ")); - Q_EMIT newMessage(saveMessage + path); + QString savePath, saveMessage; + bool ok = true; + if(m_forcedSavePath.isEmpty()) { + if(isVisible()) { + hide(); + } + savePath = m_screenshot->graphicalSave(ok, getExtendedSelection(), this); + } else { + ConfigHandler config; + config.setSavePath(m_forcedSavePath); + savePath = m_screenshot->fileSave(ok, getExtendedSelection()); + if(!ok || config.getSavePath() != m_forcedSavePath) { + saveMessage = tr("Error trying to save in ") + savePath; + Q_EMIT newMessage(saveMessage); + } } if (toClipboard) { copyScreenshot(); } - close(); - return path; -} - -QString CaptureWidget::saveScreenshot(QString path, bool toClipboard) { - ConfigHandler().setSavePath(path); - QString savePath; - if (m_selection.isNull()) { - savePath = m_screenshot->fileSave(); - } else { // save full screen when no selection - savePath = m_screenshot->fileSave(getExtendedSelection()); + if(ok) { + saveMessage = tr("Capture saved in ") + savePath; + Q_EMIT newMessage(saveMessage); } - if (toClipboard) { - copyScreenshot(); - } - QString saveMessage(tr("Capture saved in ")); - Q_EMIT newMessage(saveMessage + savePath); close(); return savePath; } @@ -556,9 +546,7 @@ void CaptureWidget::handleButtonSignal(CaptureTool::Request r) { setCursor(Qt::CrossCursor); break; case CaptureTool::REQ_SAVE_SCREENSHOT: - m_enableSaveWindow ? - saveScreenshot() : - saveScreenshot(ConfigHandler().getSavePath()); + saveScreenshot(); break; case CaptureTool::REQ_SELECT_ALL: m_selection = rect(); @@ -649,6 +637,7 @@ QPoint CaptureWidget::limitPointToRect(const QPoint &p, const QRect &r) const { } QRect CaptureWidget::getExtendedSelection() const { + if(m_selection.isNull()) return QRect(); auto devicePixelRatio = m_screenshot->getScreenshot().devicePixelRatio(); return QRect(m_selection.left() * devicePixelRatio, diff --git a/src/capture/capturewidget.h b/src/capture/capturewidget.h index 00457876..1196cac4 100644 --- a/src/capture/capturewidget.h +++ b/src/capture/capturewidget.h @@ -45,13 +45,13 @@ class CaptureWidget : public QWidget { friend class CaptureButton; public: - explicit CaptureWidget(bool enableSaveWindow = true, QWidget *parent = nullptr); + explicit CaptureWidget(const QString &forcedSavePath = "", + QWidget *parent = nullptr); ~CaptureWidget(); void updateButtons(); public slots: QString saveScreenshot(bool toClipboard = false); - QString saveScreenshot(QString path, bool toClipboard = false); void handleButtonSignal(CaptureTool::Request r); signals: @@ -97,7 +97,8 @@ protected: bool m_grabbing; bool m_onButton; bool m_showInitialMsg; - bool m_enableSaveWindow; + + const QString m_forcedSavePath; // naming convention for handles // T top, B bottom, R Right, L left diff --git a/src/capture/screenshot.cpp b/src/capture/screenshot.cpp index bf0087a9..6e56e8cb 100644 --- a/src/capture/screenshot.cpp +++ b/src/capture/screenshot.cpp @@ -59,7 +59,10 @@ QPixmap Screenshot::getScreenshot() const { // graphicalSave generates a graphical window to ask about the save path and // saves the screenshot with all the modifications in such directory -QString Screenshot::graphicalSave(const QRect &selection, QWidget *parent) const { +QString Screenshot::graphicalSave(bool &ok, + const QRect &selection, + QWidget *parent) const +{ QString savePath = FileNameHandler().getAbsoluteSavePath(); // setup window QFileDialog fileDialog(parent, QObject::tr("Save As"), savePath); @@ -74,7 +77,6 @@ QString Screenshot::graphicalSave(const QRect &selection, QWidget *parent) const fileDialog.setDefaultSuffix("png"); fileDialog.setWindowIcon(QIcon(":img/flameshot.png")); - bool saved = false; QString fileName; do { if (fileDialog.exec() != QDialog::Accepted) { return ""; } @@ -89,8 +91,8 @@ QString Screenshot::graphicalSave(const QRect &selection, QWidget *parent) const } else { // save full screen when no selection pixToSave = m_modifiedScreenshot.copy(selection); } - saved = pixToSave.save(fileName); - if (!saved) { + ok = pixToSave.save(fileName); + if (!ok) { QMessageBox saveErrBox( QMessageBox::Warning, QObject::tr("Save Error"), @@ -99,11 +101,11 @@ QString Screenshot::graphicalSave(const QRect &selection, QWidget *parent) const saveErrBox.setWindowIcon(QIcon(":img/flameshot.png")); saveErrBox.exec(); } - } while(!saved); + } while(!ok); return savePath; } -QString Screenshot::fileSave(const QRect &selection) const { +QString Screenshot::fileSave(bool &ok, const QRect &selection) const { QString savePath = FileNameHandler().getAbsoluteSavePath(); QPixmap pixToSave; if (selection.isEmpty()) { @@ -111,7 +113,8 @@ QString Screenshot::fileSave(const QRect &selection) const { } else { // save full screen when no selection pixToSave = m_modifiedScreenshot.copy(selection); } - return pixToSave.save(savePath) ? savePath : ""; + ok = pixToSave.save(savePath); + return savePath; } // paintModification adds a new modification to the screenshot diff --git a/src/capture/screenshot.h b/src/capture/screenshot.h index 2e9b5e22..559ac81c 100644 --- a/src/capture/screenshot.h +++ b/src/capture/screenshot.h @@ -37,8 +37,10 @@ public: QPixmap getBaseScreenshot() const; QPixmap getScreenshot() const; - QString graphicalSave(const QRect &selection = QRect(), QWidget *parent = 0) const; - QString fileSave(const QRect &selection = QRect()) const; + QString graphicalSave(bool &ok, + const QRect &selection = QRect(), + QWidget *parent = 0) const; + QString fileSave(bool &ok, const QRect &selection = QRect()) const; void uploadToImgur(QNetworkAccessManager *, const QRect &selection = QRect()); QPixmap paintModification(const CaptureModification*); diff --git a/src/controller.cpp b/src/controller.cpp index 6f453607..29d1f70f 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -46,16 +46,12 @@ Controller::Controller(QObject *parent) : QObject(parent), } -QString Controller::saveScreenshot(bool toClipboard) { - QPointer w = createCapture(); +QString Controller::saveScreenshot(const QString &path, + bool const toClipboard) { + QPointer w = createCaptureWidget(path); return w->saveScreenshot(toClipboard); } -QString Controller::saveScreenshot(QString path, bool toClipboard) { - QPointer w = createCapture(); - return w->saveScreenshot(path, toClipboard); -} - // creates the items of the trayIcon void Controller::createActions() { m_configAction = new QAction(tr("&Configuration"), this); @@ -104,17 +100,17 @@ void Controller::trayIconActivated(QSystemTrayIcon::ActivationReason r) { } // creation of a new capture -QPointer Controller::createCapture(bool enableSaveWindow) { - QPointer w = new CaptureWidget(enableSaveWindow); +QPointer Controller::createCaptureWidget(const QString &forcedSavePath) { + QPointer w = new CaptureWidget(forcedSavePath); connect(w, &CaptureWidget::newMessage, this, &Controller::showDesktopNotification); return w; } // creation of a new capture in GUI mode -void Controller::createVisualCapture(bool enableSaveWindow) { +void Controller::createVisualCapture(const QString &forcedSavePath) { if (!m_captureWindow) { - m_captureWindow = createCapture(enableSaveWindow); + m_captureWindow = createCaptureWidget(forcedSavePath); m_captureWindow->showFullScreen(); } } diff --git a/src/controller.h b/src/controller.h index 26573c10..897651e3 100644 --- a/src/controller.h +++ b/src/controller.h @@ -35,12 +35,11 @@ class Controller : public QObject { public: explicit Controller(QObject *parent = nullptr); - QString saveScreenshot(bool toClipboard = false); - QString saveScreenshot(QString path, bool toClipboard = false); - public slots: - QPointer createCapture(bool enableSaveWindow = true); - void createVisualCapture(bool enableSaveWindow = true); + QString saveScreenshot(const QString &path = "", + bool const toClipboard = false); + void createVisualCapture(const QString &forcedSavePath = ""); + void openConfigWindow(); void openInfoWindow(); void showDesktopNotification(QString); @@ -50,6 +49,8 @@ private slots: void trayIconActivated(QSystemTrayIcon::ActivationReason r); private: + QPointer createCaptureWidget(const QString &forcedSavePath = ""); + void createActions(); void createTrayIcon(); diff --git a/src/flameshotdbusadapter.cpp b/src/flameshotdbusadapter.cpp index 403e4e66..38492e3a 100644 --- a/src/flameshotdbusadapter.cpp +++ b/src/flameshotdbusadapter.cpp @@ -17,6 +17,7 @@ #include "flameshotdbusadapter.h" #include "src/utils/confighandler.h" +#include FlameshotDBusAdapter::FlameshotDBusAdapter(Controller *parent) : QDBusAbstractAdaptor(parent) @@ -32,25 +33,19 @@ Controller *FlameshotDBusAdapter::parent() const { return static_cast(QObject::parent()); } -void FlameshotDBusAdapter::openCapture() { - parent()->createVisualCapture(); +void FlameshotDBusAdapter::graphicCapture(QString path, int delay) { + auto p = parent(); + auto f = [p, path, this]() { + p->createVisualCapture(path); + }; + QTimer::singleShot(delay, p, f); } -void FlameshotDBusAdapter::openCaptureWithPath(QString path) { - ConfigHandler().setSavePath(path); - parent()->createVisualCapture(false); -} +void FlameshotDBusAdapter::fullScreen(QString path, bool toClipboard, int delay) { + auto p = parent(); + auto f = [p, path, toClipboard, this]() { + p->saveScreenshot(path, toClipboard); + }; + QTimer::singleShot(delay, p, f); -void FlameshotDBusAdapter::fullScreen(bool toClipboard) { - QString path = parent()->saveScreenshot(toClipboard); - if (!path.isEmpty()) { - QString saveMessage(tr("Capture saved in ")); - parent()->showDesktopNotification(saveMessage + path); - } -} - -void FlameshotDBusAdapter::fullScreenWithPath(QString path, bool toClipboard) { - QString finalPath = parent()->saveScreenshot(path, toClipboard); - QString saveMessage(tr("Capture saved in ")); - parent()->showDesktopNotification(saveMessage + finalPath); } diff --git a/src/flameshotdbusadapter.h b/src/flameshotdbusadapter.h index 5b53de70..9ea38903 100644 --- a/src/flameshotdbusadapter.h +++ b/src/flameshotdbusadapter.h @@ -32,11 +32,9 @@ public: inline Controller *parent() const; public slots: - Q_NOREPLY void openCapture(); - Q_NOREPLY void openCaptureWithPath(QString path); + Q_NOREPLY void graphicCapture(QString path, int delay); + Q_NOREPLY void fullScreen(QString path, bool toClipboard, int delay); - Q_NOREPLY void fullScreen(bool toClipboard); - Q_NOREPLY void fullScreenWithPath(QString path, bool toClipboard); }; #endif // FLAMESHOTDBUSADAPTER_H diff --git a/src/main.cpp b/src/main.cpp index 8a5676af..3d30174c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -72,53 +72,53 @@ int main(int argc, char *argv[]) { "pathVal"); QCommandLineOption clipboardOption({{"c", "clipboard"}, QObject::tr("Save the capture to the clipboard")}); + QCommandLineOption delayOption(QStringList() << "d" << "delay", + QObject::tr("Delay time in milliseconds"), + "pathVal"); if (command == "full") { parser.clearPositionalArguments(); parser.addPositionalArgument( "full", fullDescription, "full [full_options]"); - parser.addOptions({ pathOption, clipboardOption }); + parser.addOptions({ pathOption, clipboardOption, delayOption }); } else if (command == "gui") { parser.clearPositionalArguments(); parser.addPositionalArgument( "gui", guiDescription, "gui [gui_options]"); - parser.addOption(pathOption); + parser.addOptions({ pathOption, delayOption }); } parser.process(app); + // obtain values QDBusMessage m; QString pathValue; - bool pathIsSetAndValid = parser.isSet("path"); - if (pathIsSetAndValid) { + if (parser.isSet("path")) { pathValue = QString::fromStdString(parser.value("path").toStdString()); - pathIsSetAndValid = QDir(pathValue).exists(); - if (!pathIsSetAndValid) { + if (!QDir(pathValue).exists()) { qWarning() << QObject::tr("Invalid path."); return 0; } } - - if (command == "gui") { - if (pathIsSetAndValid) { - m = QDBusMessage::createMethodCall("org.dharkael.Flameshot", - "/", "", "openCaptureWithPath"); - m << pathValue; - } else { - m = QDBusMessage::createMethodCall("org.dharkael.Flameshot", - "/", "", "openCapture"); + int delay = 0; + if (parser.isSet("delay")) { + delay = parser.value("delay").toInt(); + if (delay < 0) { + qWarning() << QObject::tr("Invalid negative delay."); + return 0; } + } + + // process commands + if (command == "gui") { + m = QDBusMessage::createMethodCall("org.dharkael.Flameshot", + "/", "", "graphicCapture"); + m << pathValue << delay; QDBusConnection::sessionBus().call(m); } else if (command == "full") { - if (pathIsSetAndValid) { - m = QDBusMessage::createMethodCall("org.dharkael.Flameshot", - "/", "", "fullScreenWithPath"); - m << pathValue; - } else { - m = QDBusMessage::createMethodCall("org.dharkael.Flameshot", - "/", "", "fullScreen"); - } - m << parser.isSet("clipboard"); + m = QDBusMessage::createMethodCall("org.dharkael.Flameshot", + "/", "", "fullScreen"); + m << pathValue << parser.isSet("clipboard") << delay; QDBusConnection::sessionBus().call(m); } return 0; diff --git a/src/utils/filenamehandler.cpp b/src/utils/filenamehandler.cpp index 88b828de..29041f39 100644 --- a/src/utils/filenamehandler.cpp +++ b/src/utils/filenamehandler.cpp @@ -58,14 +58,14 @@ QString FileNameHandler::getAbsoluteSavePath() { if (savePath.isEmpty() || !QDir(savePath).exists() || !QFileInfo(savePath).isWritable()) { changed = true; savePath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); - if (savePath.isEmpty()) { - savePath = QDir::currentPath(); - } } if(changed) { config.setSavePath(savePath); } - QString tempName = "/"+ FileNameHandler().getParsedPattern(); + // first add slash if needed + QString tempName = savePath.endsWith("/") ? "" : "/"; + // add the parsed pattern in a correct format for the filesystem + tempName += FileNameHandler().getParsedPattern().replace("/", "⁄"); // find unused name adding _n where n is a number QFileInfo checkFile(savePath + tempName + ".png"); if (checkFile.exists()) { diff --git a/translation/Internationalization_es.qm b/translation/Internationalization_es.qm index 1b21936f44322985a42824eb43eb504841947e09..ad702a8eea97acb89018f39a9a7c7961d21057f8 100644 GIT binary patch delta 1208 zcmZ`&e{54#6#m-R_uALj*N6K-ySa@w=-lRXOhZT!GRV>*6DAoU+Ze`J)|dLEKeVmY zB2qWymkiioEKa845S;{_v5;7e(alY*7;xxRg3jO|aku~)qaz~m^ey2ZW1D+V&pqdU z-*?Wr?P_|pxg#gx1nSd(wF-vU;{bOWhSLVJ*TGh~fm>`~myZBhZ-IS*tm)K@&F=#{ zJOGrJA^XY(p!6jaZrcZx4WgnwbKj18yN3bGVXWx!A@jMfq75*UuJgz zI*0zJ4^I&ASNco4RKRf;<8}8_K@TwfpH%}q!yGNUK|&;E;O}<8U|_}u_WGq`OY(d(8Ky`uLIdGHoj(zF6MDrXEu_cC%D^9 zRvg0TWyPx7Y(|mjMYr?skKjQ8M@}~KdHP?W`SNQK1b<#+u`0Ar5KaenXsqo%E$Ed?K!Vr6bM$;%< zN;DAB>|ZRx`xj|uDT_F4CI22v^@W!KCN6qYl{DK@@#*q)B#;w#b2Q`Phs6EtBnf;& zd@Xl^fc9Ir^phaRChOjD$}3!D{bENCy`hWN$rEw1^R3gLlmPjolFhM%Din}EFnvR`WT*%t3PsyJw0r4Xh7XU!LvgRR%UWDEW&R!a$IO2WIsX) zt+1d5>7n9O#Qs-Cru-PrYa%kGSKg$y)dpi8zv`8Piq8{OV~Xs}XsP@UZvobj@~Bpo L2A53RR_6Q*Th1+y delta 879 zcmXAndrXaS7{|Yz^Ipz--t)dMb#$RRr4xr-njOY)Tvj1r+T4|;ESFrCY;rp;g$P|} zY_VLj#AFgOHd4rS?lYE4Y?85I=2m>I_n&9)^E|)r^LZ`@XYQU}VE4L!$&G*>2H|Kn zASFS#C(v$#7#$0AmLbM90KWSn?xnTN?yy1S5bLV|R}}m{&josvA!KtA(6b&<`Ft-P z#h8+Mz*da0`_l3Mi;vm}$SzD+{XhR3rZ+O~m8-C_D4OBM4y%M?&v*q0?~jsQ1nZvT zb9#d>(8*;=cwP<|$4#?FoMC|{rrbfF0Ox7b%S$yBPBwkmz5s}Do6}+|*ieGG;<|^x zh2~0EJD~nGSN+Zh%thwMS9Su{454gWB)l-s5$_iU z9O+2`_2d@mV+#F3uJkcAlnr#1$N3!c0{+Y7-P4?amLs?RWaptbEP>iL`p;VWPUcEN z4p<6<2^etK@?1?O*d1j;I)$`X%DV4OoJ>K7bovj)uvu}$NXb9&0DY2G2G^Pdw{!%rH1aM>- zUj`E({3(B2x2hA(J$ CaptureWidget - + Select an area with the mouse, or press Esc to exit. Press Enter to capture the screen. Press Right Click to show the color picker. @@ -26,13 +26,17 @@ Presiona Enter para capturar la pantalla. Presiona click derecho para mostrar el selector de color. - - + + Error trying to save in + Error intentando guardar en + + + Capture saved in Captura guardada en - + Uploading image... Subiendo imagen... @@ -71,17 +75,17 @@ Presiona click derecho para mostrar el selector de color. Controller - + &Configuration &Configuración - + &Information &Información - + &Quit &Salir @@ -153,15 +157,6 @@ Presiona click derecho para mostrar el selector de color. captura - - FlameshotDBusAdapter - - - - Capture saved in - Captura guardada en - - GeneneralConf @@ -322,17 +317,17 @@ Presiona click derecho para mostrar el selector de color. QObject - + Save As Guardar Como - + Save Error Error al Guardar - + The image could not be saved to "%1". la imagen no pudo ser guardada en "%1". @@ -362,17 +357,27 @@ Presiona click derecho para mostrar el selector de color. Guarda la captura en el portapapeles - - Invalid path. - Rura inválida. + + Delay time in milliseconds + Tiempo de espera en milisegundos - + + Invalid path. + Ruta inválida. + + + + Invalid negative delay. + Valor negativo de espera inválido. + + + Resource Error Error de Recurso - + Unable to open the URL. No puede abrir la URL.