From 4277af806fa6ec1532e85a1f1c422feda3ffea77 Mon Sep 17 00:00:00 2001 From: lupoDharkael Date: Thu, 15 Jun 2017 20:31:06 +0200 Subject: [PATCH] Unified binary with new DBus API --- dbus/org.dharkael.Flameshot.xml | 20 +++- src/capture/capturewidget.cpp | 43 +++++--- src/capture/capturewidget.h | 9 +- src/capture/screenshot.cpp | 46 ++++++++- src/capture/screenshot.h | 1 + src/controller.cpp | 26 ++++- src/controller.h | 7 +- src/flameshotdbusadapter.cpp | 26 ++++- src/flameshotdbusadapter.h | 14 ++- src/main.cpp | 100 ++++++++++++++++-- tools/flameshot-cli/flameshot-cli.pro | 24 ----- tools/flameshot-cli/main.cpp | 22 ---- translation/Internationalization_es.ts | 136 +++++++++++++------------ 13 files changed, 314 insertions(+), 160 deletions(-) delete mode 100644 tools/flameshot-cli/flameshot-cli.pro delete mode 100644 tools/flameshot-cli/main.cpp diff --git a/dbus/org.dharkael.Flameshot.xml b/dbus/org.dharkael.Flameshot.xml index d7cf8bff..069aa876 100644 --- a/dbus/org.dharkael.Flameshot.xml +++ b/dbus/org.dharkael.Flameshot.xml @@ -1,7 +1,21 @@ - - + + + - + + + + + + + + + + + + + + diff --git a/src/capture/capturewidget.cpp b/src/capture/capturewidget.cpp index df7e3e30..00794d26 100644 --- a/src/capture/capturewidget.cpp +++ b/src/capture/capturewidget.cpp @@ -49,10 +49,11 @@ namespace { // http://doc.qt.io/qt-5/qwidget.html#setMask -CaptureWidget::CaptureWidget(QWidget *parent) : +CaptureWidget::CaptureWidget(bool enableSaveWindow, QWidget *parent) : QWidget(parent), m_mouseOverHandle(0), m_mouseIsClicked(false), m_rightClick(false), m_newSelection(false), m_grabbing(false), - m_onButton(false), m_state(Button::Type::move) + m_onButton(false), m_enableSaveWindow(enableSaveWindow), + m_state(Button::Type::move) { m_showInitialMsg = QSettings().value("showHelp").toBool(); @@ -74,17 +75,12 @@ CaptureWidget::CaptureWidget(QWidget *parent) : | Qt::FramelessWindowHint | Qt::Tool); -// setWindowFlags( Qt::WindowStaysOnTopHint -// | Qt::FramelessWindowHint -// | Qt::Popup); - - setMouseTracking(true); setCursor(Qt::CrossCursor); initShortcuts(); // create buttons m_buttonHandler = new ButtonHandler(this); - redefineButtons(); + updateButtons(); m_buttonHandler->hide(); // init screenshot createCapture(); @@ -92,10 +88,9 @@ CaptureWidget::CaptureWidget(QWidget *parent) : // we need to increase by 1 the size to reach to the end of the screen resize(size.width()+1, size.height()+1); - showFullScreen(); - // init interface color m_colorPicker = new ColorPicker(this); + m_colorPicker->hide(); } CaptureWidget::~CaptureWidget() { @@ -104,7 +99,7 @@ CaptureWidget::~CaptureWidget() { // redefineButtons retrieves the buttons configured to be shown with the // selection in the capture -void CaptureWidget::redefineButtons() { +void CaptureWidget::updateButtons() { QSettings settings; m_uiColor = settings.value("uiColor").value(); m_contrastUiColor = settings.value("contastUiColor").value(); @@ -392,7 +387,7 @@ void CaptureWidget::keyPressEvent(QKeyEvent *e) { update(); } -void CaptureWidget::saveScreenshot() { +QString CaptureWidget::saveScreenshot(bool toClipboard) { hide(); QString path; if (m_selection.isNull()) { @@ -404,7 +399,27 @@ void CaptureWidget::saveScreenshot() { QString saveMessage(tr("Capture saved in ")); Q_EMIT newMessage(saveMessage + path); } + if (toClipboard) { + copyScreenshot(); + } close(); + return path; +} + +QString CaptureWidget::saveScreenshot(QString path, bool toClipboard) { + QSettings().setValue("savePath", path); + if (m_selection.isNull()) { + m_screenshot->fileSave(); + } else { // save full screen when no selection + m_screenshot->fileSave(getExtendedSelection()); + } + if (toClipboard) { + copyScreenshot(); + } + QString saveMessage(tr("Capture saved in ")); + Q_EMIT newMessage(saveMessage + path); + close(); + return path; } void CaptureWidget::copyScreenshot() { @@ -496,7 +511,9 @@ void CaptureWidget::setState(Button *b) { newState = Button::Type::move; } if (t == Button::Type::save) { - saveScreenshot(); + m_enableSaveWindow ? + saveScreenshot() : + saveScreenshot(QSettings().value("savePath").toString()); } else if (t == Button::Type::copy) { copyScreenshot(); } else if (t == Button::Type::exit) { diff --git a/src/capture/capturewidget.h b/src/capture/capturewidget.h index 42e2fab1..b3686c85 100644 --- a/src/capture/capturewidget.h +++ b/src/capture/capturewidget.h @@ -44,16 +44,18 @@ class CaptureWidget : public QWidget { friend class Button; public: - explicit CaptureWidget(QWidget *parent = 0); + explicit CaptureWidget(bool enableSaveWindow = true, QWidget *parent = 0); ~CaptureWidget(); - void redefineButtons(); + void updateButtons(); +public slots: + QString saveScreenshot(bool toClipboard = false); + QString saveScreenshot(QString path, bool toClipboard = false); signals: void newMessage(QString); private slots: - void saveScreenshot(); void copyScreenshot(); void openURL(QNetworkReply *reply); void leaveButton(); @@ -93,6 +95,7 @@ protected: bool m_grabbing; bool m_onButton; bool m_showInitialMsg; + bool m_enableSaveWindow; // 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 c1b625c9..42dba7c6 100644 --- a/src/capture/screenshot.cpp +++ b/src/capture/screenshot.cpp @@ -64,7 +64,6 @@ QPixmap Screenshot::getScreenshot() const { // saves the screenshot with all the modifications in such directory QString Screenshot::graphicalSave(const QRect &selection, QWidget *parent) const { const QString format = "png"; - QSettings settings; QString savePath = settings.value("savePath").toString(); if (savePath.isEmpty() || !QDir(savePath).exists()) { @@ -104,12 +103,12 @@ QString Screenshot::graphicalSave(const QRect &selection, QWidget *parent) const fileDialog.setWindowIcon(QIcon(":img/flameshot.png")); bool saved = false; - QString fileName; + QString fileName, pathNoFile; do { - if (fileDialog.exec() != QDialog::Accepted) { return fileName; } + if (fileDialog.exec() != QDialog::Accepted) { return ""; } fileName = fileDialog.selectedFiles().first(); - const QString pathNoFile = fileName.left(fileName.lastIndexOf("/")); + pathNoFile = fileName.left(fileName.lastIndexOf("/")); settings.setValue("savePath", pathNoFile); QPixmap pixToSave; @@ -131,7 +130,44 @@ QString Screenshot::graphicalSave(const QRect &selection, QWidget *parent) const saveErrBox.exec(); } } while(!saved); - return fileName; + + return pathNoFile; +} + +QString Screenshot::fileSave(const QRect &selection) const { + const QString format = "png"; + QSettings settings; + QString savePath = settings.value("savePath").toString(); + if (savePath.isEmpty() || !QDir(savePath).exists()) { + savePath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); + if (savePath.isEmpty()) { + savePath = QDir::currentPath(); + } + } + // find unused name adding _n where n is a number + QString tempName = QObject::tr("/screenshot"); + QFileInfo checkFile(savePath + tempName + "." + format); + if (checkFile.exists()) { + tempName += "_"; + int i = 1; + while (true) { + checkFile.setFile( + savePath + tempName + QString::number(i) + "." + format); + if (!checkFile.exists()) { + tempName += QString::number(i); + break; + } + ++i; + } + } + savePath += tempName + "." + format; + QPixmap pixToSave; + if (selection.isEmpty()) { + pixToSave = m_modifiedScreenshot; + } else { // save full screen when no selection + pixToSave = m_modifiedScreenshot.copy(selection); + } + return pixToSave.save(savePath) ? savePath : ""; } // paintModification adds a new modification to the screenshot diff --git a/src/capture/screenshot.h b/src/capture/screenshot.h index 1fbfb888..1d32f4b3 100644 --- a/src/capture/screenshot.h +++ b/src/capture/screenshot.h @@ -38,6 +38,7 @@ public: QPixmap getScreenshot() const; QString graphicalSave(const QRect &selection = QRect(), QWidget *parent = 0) const; + QString fileSave(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 5919dbcc..648ac9bc 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -46,6 +46,14 @@ Controller::Controller(QObject *parent) : QObject(parent), } +QString Controller::saveScreenshot(bool toClipboard) { + return m_captureWindow->saveScreenshot(toClipboard); +} + +QString Controller::saveScreenshot(QString path, bool toClipboard) { + return m_captureWindow->saveScreenshot(path, toClipboard); +} + // creates the items of the trayIcon void Controller::createActions() { m_configAction = new QAction(tr("&Configuration"), this); @@ -113,16 +121,24 @@ void Controller::initDefaults() { void Controller::trayIconActivated(QSystemTrayIcon::ActivationReason r) { if (r == QSystemTrayIcon::Trigger) { - createCapture(); + createVisualCapture(); } } // creation of a new capture -void Controller::createCapture() { +void Controller::createCapture(bool enableSaveWindow) { if (!m_captureWindow) { - m_captureWindow = new CaptureWidget(); + m_captureWindow = new CaptureWidget(enableSaveWindow); connect(m_captureWindow, &CaptureWidget::newMessage, - this, &Controller::showMessage); + this, &Controller::showDesktopNotification); + } +} + +// creation of a new capture in GUI mode +void Controller::createVisualCapture(bool enableSaveWindow) { + createCapture(enableSaveWindow); + if (m_captureWindow && !m_captureWindow->isVisible()) { + m_captureWindow->showFullScreen(); } } @@ -140,7 +156,7 @@ void Controller::openInfoWindow() { } } -void Controller::showMessage(QString msg) { +void Controller::showDesktopNotification(QString msg) { bool showMessages = QSettings().value("showDesktopNotification").toBool(); if (showMessages) { m_trayIcon->showMessage("Flameshot Info", msg); diff --git a/src/controller.h b/src/controller.h index 4948aa43..01682576 100644 --- a/src/controller.h +++ b/src/controller.h @@ -34,13 +34,16 @@ class Controller : public QObject { public: explicit Controller(QObject *parent = 0); + QString saveScreenshot(bool toClipboard = false); + QString saveScreenshot(QString path, bool toClipboard = false); public slots: - void createCapture(); + void createCapture(bool enableSaveWindow = true); + void createVisualCapture(bool enableSaveWindow = true); void openConfigWindow(); void openInfoWindow(); + void showDesktopNotification(QString); private slots: - void showMessage(QString); void initDefaults(); void trayIconActivated(QSystemTrayIcon::ActivationReason r); diff --git a/src/flameshotdbusadapter.cpp b/src/flameshotdbusadapter.cpp index 2ba87f32..39f7de9a 100644 --- a/src/flameshotdbusadapter.cpp +++ b/src/flameshotdbusadapter.cpp @@ -16,6 +16,7 @@ // along with Flameshot. If not, see . #include "flameshotdbusadapter.h" +#include FlameshotDBusAdapter::FlameshotDBusAdapter(Controller *parent) : QDBusAbstractAdaptor(parent) @@ -31,6 +32,27 @@ Controller *FlameshotDBusAdapter::parent() const { return static_cast(QObject::parent()); } -void FlameshotDBusAdapter::createCapture() { - parent()->createCapture(); +void FlameshotDBusAdapter::openCapture() { + parent()->createVisualCapture(); +} + +void FlameshotDBusAdapter::openCaptureWithPath(QString path) { + QSettings().setValue("savePath", path); + parent()->createVisualCapture(false); +} + +void FlameshotDBusAdapter::fullScreen(bool toClipboard) { + parent()->createCapture(); + 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) { + parent()->createCapture(); + 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 2806030a..63d6b02d 100644 --- a/src/flameshotdbusadapter.h +++ b/src/flameshotdbusadapter.h @@ -25,20 +25,18 @@ class FlameshotDBusAdapter : public QDBusAbstractAdaptor { Q_OBJECT Q_CLASSINFO("D-Bus Interface", "org.dharkael.Flameshot") - Q_CLASSINFO("D-Bus Introspection", "" - "\n" - " \n" - " \n" - "\n" - "" - ) + public: FlameshotDBusAdapter(Controller *parent = 0); virtual ~FlameshotDBusAdapter(); inline Controller *parent() const; public slots: - Q_NOREPLY void createCapture(); + Q_NOREPLY void openCapture(); + Q_NOREPLY void openCaptureWithPath(QString path); + + 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 213e5e76..72eeb9ed 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,25 +20,107 @@ #include "src/flameshotdbusadapter.h" #include #include +#include #include +#include +#include +#include +#include int main(int argc, char *argv[]) { QTranslator translator; translator.load(QLocale::system().language(), "Internationalization", "_", "/usr/share/flameshot/translations/"); - SingleApplication app(argc, argv); - app.installTranslator(&translator); - app.setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true); + // no arguments, just launch Flameshot + if (argc == 1) { + SingleApplication app(argc, argv); + app.installTranslator(&translator); + app.setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true); + app.setApplicationName("flameshot"); + app.setOrganizationName("Dharkael"); + Controller c; + new FlameshotDBusAdapter(&c); + QDBusConnection dbus = QDBusConnection::sessionBus(); + dbus.registerObject("/", &c); + dbus.registerService("org.dharkael.Flameshot"); + return app.exec(); + } + + QCoreApplication app(argc, argv); + app.installTranslator(&translator); app.setApplicationName("flameshot"); app.setOrganizationName("Dharkael"); - Controller c; - new FlameshotDBusAdapter(&c); - QDBusConnection dbus = QDBusConnection::sessionBus(); - dbus.registerObject("/", &c); - dbus.registerService("org.dharkael.Flameshot"); + QCommandLineParser parser; + parser.addHelpOption(); + parser.setApplicationDescription( + QObject::tr("Powerfull yet simple to use screenshot software.")); - return app.exec(); + QString fullDescription = QObject::tr("Capture the entire desktop."); + QString guiDescription = QObject::tr("Start a manual capture in GUI mode."); + parser.addPositionalArgument("mode", "full\t"+fullDescription+"\n" + "gui\t"+guiDescription, + "mode [mode_options]"); + parser.parse(app.arguments()); + + const QStringList args = parser.positionalArguments(); + const QString command = args.isEmpty() ? QString() : args.first(); + + QCommandLineOption pathOption(QStringList() << "p" << "path", + QObject::tr("Path where the capture will be saved"), + "pathVal"); + QCommandLineOption clipboardOption({{"c", "clipboard"}, + QObject::tr("Save the capture to the clipboard")}); + if (command == "full") { + parser.clearPositionalArguments(); + parser.addPositionalArgument( + "full", fullDescription, "full [full_options]"); + parser.addOptions({ pathOption, clipboardOption }); + } else if (command == "gui") { + parser.clearPositionalArguments(); + parser.addPositionalArgument( + "gui", guiDescription, "gui [gui_options]"); + + parser.addOption(pathOption); + } + parser.process(app); + + QDBusMessage m; + QString pathValue; + bool pathIsSetAndValid = parser.isSet("path"); + if (pathIsSetAndValid) { + pathValue = QString::fromStdString(parser.value("path").toStdString()); + pathIsSetAndValid = QDir(pathValue).exists(); + if (!pathIsSetAndValid) { + 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"); + } + 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"); + QDBusConnection::sessionBus().call(m); + } + return 0; } diff --git a/tools/flameshot-cli/flameshot-cli.pro b/tools/flameshot-cli/flameshot-cli.pro deleted file mode 100644 index 8c8d8b00..00000000 --- a/tools/flameshot-cli/flameshot-cli.pro +++ /dev/null @@ -1,24 +0,0 @@ -QT += core -QT += dbus -QT -= gui - -CONFIG += c++11 - -TARGET = flameshot-cli -CONFIG += console -CONFIG -= app_bundle - -TEMPLATE = app - -SOURCES += main.cpp - -# The following define makes your compiler emit warnings if you use -# any feature of Qt which as been marked deprecated (the exact warnings -# depend on your compiler). Please consult the documentation of the -# deprecated API in order to know how to port your code away from it. -DEFINES += QT_DEPRECATED_WARNINGS - -# You can also make your code fail to compile if you use deprecated APIs. -# In order to do so, uncomment the following line. -# You can also select to disable deprecated APIs only up to a certain version of Qt. -#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 diff --git a/tools/flameshot-cli/main.cpp b/tools/flameshot-cli/main.cpp deleted file mode 100644 index 0c812d1c..00000000 --- a/tools/flameshot-cli/main.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include -#include -//#include - -int main(int argc, char *argv[]) { - Q_UNUSED(argc); - Q_UNUSED(argv); - //QCoreApplication a(argc, argv); - //QCommandLineParser parser; - QDBusMessage m = QDBusMessage::createMethodCall("org.dharkael.Flameshot", - "/", - "", - "createCapture" - ); - - QDBusConnection::sessionBus().call(m); - - - //return a.exec(); - -} diff --git a/translation/Internationalization_es.ts b/translation/Internationalization_es.ts index 23a05cda..b4945216 100644 --- a/translation/Internationalization_es.ts +++ b/translation/Internationalization_es.ts @@ -28,34 +28,6 @@ Opens the save image window Abre la ventana de guardar imagen - - Sets the paint tool to a pencil - Establece el lápiz como herramienta de dibujo - - - Sets the paint tool to a line drawer - Establece la línea como herramienta de dibujo - - - Sets the paint tool to an arrow drawer - Establece la flecha como herramienta de dibujo - - - Sets the paint tool to a rectagle drawer - Establece el rectángulo como herramienta de dibujo - - - Sets the paint tool to a circle drawer - Establece el círculo como herramienta de dibujo - - - Sets the paint tool to a marker - Establece el marcador como herramienta de dibujo - - - Sets the paint tool to a text creator - Establece el texto como herramienta de dibujo - Opens the color picker widget @@ -66,10 +38,6 @@ Undo the last modification Deshace la última modificación - - Upload the selection to Imgur - Sube la selección a Imgur - Sets the Pencil as the paint tool @@ -209,16 +177,22 @@ CaptureWidget - + Select an area with the mouse, or press Esc to exit. Press Enter to capture the screen. -Press Right Click to choose the tool color. - Selecciona un área con el ratón, or presiona Esc para salir. +Press Right Click to show the color picker. + Selecciona un área con el ratón, o presiona Esc para salir. Presiona Enter para capturar la pantalla. -Presiona Click Derecho para elegir color de herramienta. +Presiona click derecho para mostrar el selector de color. - + + + Capture saved in + Captura guardada en + + + Uploading image... Subiendo imagen... @@ -245,41 +219,52 @@ Presiona Click Derecho para elegir color de herramienta. Button selection Selección de botones - - Select which buttons will appear arround the capture's selection by clicking on its checkbox. - Selecciona qué botones aparecerán alrededor de la selección de captura clicando en su casilla. - Controller - + &Configuration &Configuración - + &Information &Información - + &Quit &Salir + + FlameshotDBusAdapter + + + + Capture saved in + Captura guardada en + + GeneneralConf - + Show help message Mostrar mensaje de ayuda - + Show the help message at the beginning in the capture mode. Muestra el mensaje de ayuda al iniciar el modo de captura. + + + + Show desktop notifications + Mostrar notificaciones del escritorio + InfoWindow @@ -357,63 +342,86 @@ Presiona Click Derecho para elegir color de herramienta. QObject - + + /screenshot /captura - + 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". + + + Powerfull yet simple to use screenshot software. + Potente pero simple de usar software de capturas. + + + + Capture the entire desktop. + Captura el escritorio completo. + + + + Start a manual capture in GUI mode. + Inicia una captura manual en modo gráfico. + + + + Path where the capture will be saved + Ruta donde la captura será guardada + + + + Save the capture to the clipboard + Guarda la captura en el portapapeles + + + + Invalid path. + Rura inválida. + UIcolorEditor - Select a Button to change the color - Selecciona un Botón para cambiar el color - - - Color edition component, click on the color wheel and change the UI colors! - Componente de edición de color, ¡clica en él para cambiar el color de la interfaz! - - - + Change the color moving the selectors and see the changes in the preview buttons. Cambia el color moviendo los selectores y observa los cambios en los botones de previsualización. - + Select a Button to modify it Selecciona un Botón para modificarlo - + Main Color Color Principal - + Click on this button to set the edition mode of the main color. Clica en este botón para aplicar el modo edición para el color primario. - + Contrast Color Color de Contraste - + Click on this button to set the edition mode of the contrast color. Clica en este botón para aplicar el modo edición para el color de contraste.