Skip to content

Commit

Permalink
Rewrote launcher to use a qt designer file. Removed some code related…
Browse files Browse the repository at this point in the history
… to (#2338)

dragging the launcher corner as it didnt seem to do anything. Cleaned up
clang format suggestions.
  • Loading branch information
borgmanJeremy authored Jan 29, 2022
1 parent 3553dea commit af1b895
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 115 deletions.
1 change: 1 addition & 0 deletions src/widgets/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ target_sources(
flameshot
PRIVATE
infowindow.ui
capturelauncher.ui

capturelauncher.h
draggablewidgetmaker.h
Expand Down
137 changes: 42 additions & 95 deletions src/widgets/capturelauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,141 +2,83 @@
// SPDX-FileCopyrightText: 2017-2018 Alejandro Sirgo Rica & Contributors

#include "capturelauncher.h"
#include "./ui_capturelauncher.h"
#include "src/core/controller.h"
#include "src/utils/globalvalues.h"
#include "src/utils/screengrabber.h"
#include "src/utils/screenshotsaver.h"
#include "src/widgets/imagelabel.h"
#include <QComboBox>
#include <QDrag>
#include <QFormLayout>
#include <QGridLayout>
#include <QLabel>
#include <QMimeData>
#include <QPushButton>
#include <QSpinBox>

// https://github.com/KDE/spectacle/blob/941c1a517be82bed25d1254ebd735c29b0d2951c/src/Gui/KSWidget.cpp
// https://github.com/KDE/spectacle/blob/941c1a517be82bed25d1254ebd735c29b0d2951c/src/Gui/KSMainWindow.cpp

CaptureLauncher::CaptureLauncher(QDialog* parent)
: QDialog(parent)
, ui(new Ui::CaptureLauncher)
{
qApp->installEventFilter(this); // see eventFilter()
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
setWindowIcon(QIcon(GlobalValues::iconPath()));
m_imageLabel = new ImageLabel(this);
bool ok;
m_imageLabel->setScreenshot(ScreenGrabber().grabEntireDesktop(ok));
if (!ok) {
}
m_imageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(m_imageLabel,
&ImageLabel::dragInitiated,
this,
&CaptureLauncher::startDrag);

QGridLayout* layout = new QGridLayout(this);
layout->addWidget(m_imageLabel, 0, 0);

m_CaptureModeLabel = new QLabel(tr("<b>Capture Mode</b>"));
ui->imagePreview->setScreenshot(ScreenGrabber().grabEntireDesktop(ok));
ui->imagePreview->setSizePolicy(QSizePolicy::Expanding,
QSizePolicy::Expanding);

m_captureType = new QComboBox();
m_captureType->setMinimumWidth(240);
// TODO remember number
m_captureType->insertItem(
ui->captureType->insertItem(
1, tr("Rectangular Region"), CaptureRequest::GRAPHICAL_MODE);

#if defined(Q_OS_MACOS)
// Following to MacOS philosophy (one application cannot be displayed on
// more than one display)
m_captureType->insertItem(
ui->captureType->insertItem(
2, tr("Full Screen (Current Display)"), CaptureRequest::FULLSCREEN_MODE);
#else
m_captureType->insertItem(
ui->captureType->insertItem(
2, tr("Full Screen (All Monitors)"), CaptureRequest::FULLSCREEN_MODE);
#endif
// m_captureType->insertItem(3, tr("Single Screen"),
// CaptureRequest::SCREEN_MODE);

m_delaySpinBox = new QSpinBox();
m_delaySpinBox->setSingleStep(1.0);
m_delaySpinBox->setMinimum(0.0);
m_delaySpinBox->setMaximum(999.0);
m_delaySpinBox->setSpecialValueText(tr("No Delay"));
m_delaySpinBox->setMinimumWidth(160);
// with QT 5.7 qOverload<int>(&QSpinBox::valueChanged),
connect(m_delaySpinBox,

ui->delayTime->setSpecialValueText(tr("No Delay"));
ui->launchButton->setFocus();

// Function to add or remove plural to seconds
connect(ui->delayTime,
static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
this,
[this](int val) {
QString sufix = val == 1 ? tr(" second") : tr(" seconds");
this->m_delaySpinBox->setSuffix(sufix);
QString suffix = val == 1 ? tr(" second") : tr(" seconds");
this->ui->delayTime->setSuffix(suffix);
});

m_launchButton = new QPushButton(tr("Take new screenshot"));
m_launchButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(m_launchButton,
connect(ui->launchButton,
&QPushButton::clicked,
this,
&CaptureLauncher::startCapture);
m_launchButton->setFocus();

QFormLayout* captureModeForm = new QFormLayout;
captureModeForm->addRow(tr("Area:"), m_captureType);
captureModeForm->addRow(tr("Delay:"), m_delaySpinBox);
captureModeForm->setContentsMargins(24, 0, 0, 0);

m_mainLayout = new QVBoxLayout();
m_mainLayout->addStretch(1);
m_mainLayout->addWidget(m_CaptureModeLabel);
m_mainLayout->addLayout(captureModeForm);
m_mainLayout->addStretch(10);
m_mainLayout->addWidget(m_launchButton, 1, Qt::AlignCenter);
m_mainLayout->setContentsMargins(10, 0, 0, 10);
layout->addLayout(m_mainLayout, 0, 1);
layout->setColumnMinimumWidth(0, 320);
layout->setColumnMinimumWidth(1, 320);

show();
}

// HACK:
// https://github.com/KDE/spectacle/blob/fa1e780b8bf3df3ac36c410b9ece4ace041f401b/src/Gui/KSMainWindow.cpp#L70
void CaptureLauncher::startCapture()
{
m_launchButton->setEnabled(false);
ui->launchButton->setEnabled(false);
hide();

auto const additionalDelayToHideUI = 600;
auto const secondsToMilliseconds = 1000;
auto mode = static_cast<CaptureRequest::CaptureMode>(
m_captureType->currentData().toInt());
CaptureRequest req(mode, 600 + m_delaySpinBox->value() * 1000);
ui->captureType->currentData().toInt());
CaptureRequest req(mode,
additionalDelayToHideUI +
ui->delayTime->value() * secondsToMilliseconds);
connectCaptureSlots();
Controller::getInstance()->requestCapture(req);
}

void CaptureLauncher::startDrag()
{
QDrag* dragHandler = new QDrag(this);
QMimeData* mimeData = new QMimeData;
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
mimeData->setImageData(m_imageLabel->pixmap(Qt::ReturnByValue));
#else
mimeData->setImageData(m_imageLabel->pixmap());
#endif
dragHandler->setMimeData(mimeData);

#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
dragHandler->setPixmap(
m_imageLabel->pixmap(Qt::ReturnByValue)
.scaled(
256, 256, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
dragHandler->exec();
#else
dragHandler->setPixmap(m_imageLabel->pixmap()->scaled(
256, 256, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
dragHandler->exec();
#endif
}

void CaptureLauncher::connectCaptureSlots()
void CaptureLauncher::connectCaptureSlots() const
{
connect(Controller::getInstance(),
&Controller::captureTaken,
Expand All @@ -148,12 +90,12 @@ void CaptureLauncher::connectCaptureSlots()
&CaptureLauncher::captureFailed);
}

void CaptureLauncher::disconnectCaptureSlots()
void CaptureLauncher::disconnectCaptureSlots() const
{
// Hack for MacOS
// for some strange reasons MacOS sends multiple "captureTaken" signals
// (random number, usually from 1 up to 20).
// So no it enables signal on "Capture new screenshot" button and disables
// So now it enables signal on "Capture new screenshot" button and disables
// on first success of fail.
disconnect(Controller::getInstance(),
&Controller::captureTaken,
Expand All @@ -165,27 +107,32 @@ void CaptureLauncher::disconnectCaptureSlots()
&CaptureLauncher::captureFailed);
}

void CaptureLauncher::captureTaken(QPixmap p, const QRect&)
void CaptureLauncher::captureTaken(QPixmap screenshot)
{
// MacOS specific, more details in the function disconnectCaptureSlots()
disconnectCaptureSlots();

m_imageLabel->setScreenshot(p);
ui->imagePreview->setScreenshot(screenshot);
show();

auto mode = static_cast<CaptureRequest::CaptureMode>(
m_captureType->currentData().toInt());
ui->captureType->currentData().toInt());

if (mode == CaptureRequest::FULLSCREEN_MODE) {
ScreenshotSaver().saveToFilesystemGUI(p);
ScreenshotSaver().saveToFilesystemGUI(screenshot);
}
m_launchButton->setEnabled(true);
ui->launchButton->setEnabled(true);
}

void CaptureLauncher::captureFailed()
{
// MacOS specific, more details in the function disconnectCaptureSlots()
disconnectCaptureSlots();
show();
m_launchButton->setEnabled(true);
ui->launchButton->setEnabled(true);
}

CaptureLauncher::~CaptureLauncher()
{
delete ui;
}
32 changes: 12 additions & 20 deletions src/widgets/capturelauncher.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,27 @@

#include <QDialog>

class QCheckBox;
class QPushButton;
class QVBoxLayout;
class QComboBox;
class QSpinBox;
class QLabel;
class ImageLabel;
QT_BEGIN_NAMESPACE
namespace Ui {
class CaptureLauncher;
}
QT_END_NAMESPACE

class CaptureLauncher : public QDialog
{
Q_OBJECT

public:
explicit CaptureLauncher(QDialog* parent = nullptr);
~CaptureLauncher();

private:
void connectCaptureSlots();
void disconnectCaptureSlots();
Ui::CaptureLauncher* ui;
void connectCaptureSlots() const;
void disconnectCaptureSlots() const;

private slots:
void startCapture();
void startDrag();
void captureTaken(QPixmap p, const QRect& selection);
void captureTaken(QPixmap p);
void captureFailed();

private:
QSpinBox* m_delaySpinBox;
QComboBox* m_captureType;
QVBoxLayout* m_mainLayout;
QPushButton* m_launchButton;
QLabel* m_CaptureModeLabel;
ImageLabel* m_imageLabel;
};
};
113 changes: 113 additions & 0 deletions src/widgets/capturelauncher.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CaptureLauncher</class>
<widget class="QDialog" name="CaptureLauncher">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>619</width>
<height>148</height>
</rect>
</property>
<property name="windowTitle">
<string>Capture Launcher</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="ImageLabel" name="imagePreview">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="modeLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Capture Mode</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<property name="margin">
<number>-1</number>
</property>
</widget>
</item>
<item>
<layout class="QGridLayout" name="gridLayout_3">
<item row="1" column="0">
<widget class="QLabel" name="areaLabel">
<property name="text">
<string>Area:</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="delayLabel">
<property name="text">
<string>Delay:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="captureType">
<property name="currentText">
<string/>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QSpinBox" name="delayTime">
<property name="suffix">
<string> seconds</string>
</property>
<property name="minimum">
<number>0</number>
</property>
<property name="maximum">
<number>999</number>
</property>
</widget>
</item>
</layout>
</item>
<item alignment="Qt::AlignHCenter">
<widget class="QPushButton" name="launchButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Take new screenshot</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>ImageLabel</class>
<extends>QLabel</extends>
<header>imagelabel.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

0 comments on commit af1b895

Please sign in to comment.