|
| 1 | +// Copyright(c) 2017-2018 Alejandro Sirgo Rica & Contributors |
| 2 | +// |
| 3 | +// This file is part of Flameshot. |
| 4 | +// |
| 5 | +// Flameshot is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// Flameshot is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with Flameshot. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +#include "capturelauncher.h" |
| 19 | +#include "src/core/controller.h" |
| 20 | +#include "src/widgets/imagelabel.h" |
| 21 | +#include "src/widgets/notificationwidget.h" |
| 22 | +#include "src/utils/screengrabber.h" |
| 23 | +#include <QCheckBox> |
| 24 | +#include <QPushButton> |
| 25 | +#include <QGridLayout> |
| 26 | +#include <QVBoxLayout> |
| 27 | +#include <QSpinBox> |
| 28 | +#include <QLabel> |
| 29 | +#include <QComboBox> |
| 30 | +#include <QMimeData> |
| 31 | +#include <QDrag> |
| 32 | +#include <QFormLayout> |
| 33 | + |
| 34 | +// https://github.com/KDE/spectacle/blob/941c1a517be82bed25d1254ebd735c29b0d2951c/src/Gui/KSWidget.cpp |
| 35 | +// https://github.com/KDE/spectacle/blob/941c1a517be82bed25d1254ebd735c29b0d2951c/src/Gui/KSMainWindow.cpp |
| 36 | + |
| 37 | +CaptureLauncher::CaptureLauncher(QWidget *parent) : |
| 38 | + QWidget(parent), m_id(0) |
| 39 | +{ |
| 40 | + setAttribute(Qt::WA_DeleteOnClose); |
| 41 | + connect(Controller::getInstance(), &Controller::captureTaken, |
| 42 | + this, &CaptureLauncher::captureTaken); |
| 43 | + connect(Controller::getInstance(), &Controller::captureFailed, |
| 44 | + this, &CaptureLauncher::captureFailed); |
| 45 | + |
| 46 | + m_imageLabel = new ImageLabel(this); |
| 47 | + bool ok; |
| 48 | + m_imageLabel->setScreenshot(ScreenGrabber().grabEntireDesktop(ok)); |
| 49 | + if (!ok) { |
| 50 | + |
| 51 | + } |
| 52 | + m_imageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
| 53 | + connect(m_imageLabel, &ImageLabel::dragInitiated, |
| 54 | + this, &CaptureLauncher::startDrag); |
| 55 | + |
| 56 | + QGridLayout *layout = new QGridLayout(this); |
| 57 | + layout->addWidget(m_imageLabel, 0, 0); |
| 58 | + |
| 59 | + m_CaptureModeLabel = new QLabel(tr("<b>Capture Mode</b>")); |
| 60 | + |
| 61 | + m_captureType = new QComboBox(); |
| 62 | + m_captureType->setMinimumWidth(240); |
| 63 | + // TODO remember number |
| 64 | + m_captureType->insertItem(1, tr("Rectangular Region"), CaptureRequest::GRAPHICAL_MODE); |
| 65 | + m_captureType->insertItem(2, tr("Full Screen (All Monitors)"), CaptureRequest::FULLSCREEN_MODE); |
| 66 | + //m_captureType->insertItem(3, tr("Single Screen"), CaptureRequest::SCREEN_MODE); |
| 67 | + |
| 68 | + m_delaySpinBox = new QSpinBox(); |
| 69 | + m_delaySpinBox->setSingleStep(1.0); |
| 70 | + m_delaySpinBox->setMinimum(0.0); |
| 71 | + m_delaySpinBox->setMaximum(999.0); |
| 72 | + m_delaySpinBox->setSpecialValueText(tr("No Delay")); |
| 73 | + m_delaySpinBox->setMinimumWidth(160); |
| 74 | + // with QT 5.7 qOverload<int>(&QSpinBox::valueChanged), |
| 75 | + connect(m_delaySpinBox, |
| 76 | + static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), |
| 77 | + this, [this](int val) |
| 78 | + { |
| 79 | + QString sufix = val == 1 ?tr(" second") : tr(" seconds"); |
| 80 | + this->m_delaySpinBox->setSuffix(sufix); |
| 81 | + }); |
| 82 | + |
| 83 | + m_launchButton = new QPushButton(tr("Take new screenshot")); |
| 84 | + m_launchButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 85 | + connect(m_launchButton, &QPushButton::pressed, |
| 86 | + this, &CaptureLauncher::startCapture); |
| 87 | + m_launchButton->setFocus(); |
| 88 | + |
| 89 | + QFormLayout *captureModeForm = new QFormLayout; |
| 90 | + captureModeForm->addRow(tr("Area:"), m_captureType); |
| 91 | + captureModeForm->addRow(tr("Delay:"), m_delaySpinBox); |
| 92 | + captureModeForm->setContentsMargins(24, 0, 0, 0); |
| 93 | + |
| 94 | + m_mainLayout = new QVBoxLayout(); |
| 95 | + m_mainLayout->addStretch(1); |
| 96 | + m_mainLayout->addWidget(m_CaptureModeLabel); |
| 97 | + m_mainLayout->addLayout(captureModeForm); |
| 98 | + m_mainLayout->addStretch(10); |
| 99 | + m_mainLayout->addWidget(m_launchButton, 1 , Qt::AlignCenter); |
| 100 | + m_mainLayout->setContentsMargins(10, 0, 0, 10); |
| 101 | + layout->addLayout(m_mainLayout, 0 ,1); |
| 102 | + layout->setColumnMinimumWidth(0, 320); |
| 103 | + layout->setColumnMinimumWidth(1, 320); |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +// HACK: https://github.com/KDE/spectacle/blob/fa1e780b8bf3df3ac36c410b9ece4ace041f401b/src/Gui/KSMainWindow.cpp#L70 |
| 108 | +void CaptureLauncher::startCapture() { |
| 109 | + hide(); |
| 110 | + auto mode = static_cast<CaptureRequest::CaptureMode>( |
| 111 | + m_captureType->currentData().toInt()); |
| 112 | + CaptureRequest req(mode, 600 + m_delaySpinBox->value() * 1000); |
| 113 | + m_id = req.id(); |
| 114 | + Controller::getInstance()->requestCapture(req); |
| 115 | +} |
| 116 | + |
| 117 | +void CaptureLauncher::startDrag() { |
| 118 | + QDrag *dragHandler = new QDrag(this); |
| 119 | + QMimeData *mimeData = new QMimeData; |
| 120 | + mimeData->setImageData(m_imageLabel->pixmap()); |
| 121 | + dragHandler->setMimeData(mimeData); |
| 122 | + |
| 123 | + dragHandler->setPixmap(m_imageLabel->pixmap() |
| 124 | + ->scaled(256, 256, Qt::KeepAspectRatioByExpanding, |
| 125 | + Qt::SmoothTransformation)); |
| 126 | + dragHandler->exec(); |
| 127 | +} |
| 128 | + |
| 129 | +void CaptureLauncher::captureTaken(uint id, QPixmap p) { |
| 130 | + if (id == m_id) { |
| 131 | + m_id = 0; |
| 132 | + m_imageLabel->setScreenshot(p); |
| 133 | + show(); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +void CaptureLauncher::captureFailed(uint id) { |
| 138 | + if (id == m_id) { |
| 139 | + m_id = 0; |
| 140 | + show(); |
| 141 | + } |
| 142 | +} |
0 commit comments