diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -33,6 +33,7 @@ Gui/KSWidget.cpp Gui/KSImageWidget.cpp Gui/ExportMenu.cpp + Gui/ProgressButton.cpp Gui/SmartSpinBox.cpp Gui/SettingsDialog/SettingsDialog.cpp Gui/SettingsDialog/SettingsPage.cpp diff --git a/src/Gui/KSMainWindow.cpp b/src/Gui/KSMainWindow.cpp --- a/src/Gui/KSMainWindow.cpp +++ b/src/Gui/KSMainWindow.cpp @@ -293,13 +293,15 @@ connect(delayAnimation, &QVariantAnimation::valueChanged, this, [=] { const double progress = delayAnimation->currentValue().toDouble(); const double timeoutInSeconds = theTimeout / 1000.0; + mKSWidget->setProgress(progress); unityUpdate({ {QStringLiteral("progress"), progress} }); setWindowTitle(i18ncp("@title:window", "%1 second", "%1 seconds", qMin(int(timeoutInSeconds), qCeil((1 - progress) * timeoutInSeconds)))); }); connect(timer, &QTimer::timeout, this, [=] { this->hide(); timer->deleteLater(); + mKSWidget->setProgress(0); unityUpdate({ {QStringLiteral("progress-visible"), false} }); emit newScreenshotRequest(theCaptureMode, 0, theIncludePointer, theIncludeDecorations); }); diff --git a/src/Gui/KSWidget.h b/src/Gui/KSWidget.h --- a/src/Gui/KSWidget.h +++ b/src/Gui/KSWidget.h @@ -35,9 +35,9 @@ class QComboBox; class QCheckBox; class QLabel; -class QToolButton; class KSImageWidget; +class ProgressButton; class SmartSpinBox; class KSWidget : public QWidget @@ -49,6 +49,7 @@ explicit KSWidget(const Platform::GrabModes &theGrabModes, QWidget *parent = nullptr); virtual ~KSWidget() = default; + enum class State { TakeNewScreenshot, Cancel @@ -68,7 +69,8 @@ void lockOnClickDisabled(); void lockOnClickEnabled(); void setButtonState(State state); - + void setProgress(double progress); + private Q_SLOTS: void newScreenshotClicked(); @@ -83,7 +85,7 @@ QFormLayout *mCaptureModeForm { nullptr }; QVBoxLayout *mContentOptionsForm { nullptr }; KSImageWidget *mImageWidget { nullptr }; - QToolButton *mTakeScreenshotButton; + ProgressButton*mTakeScreenshotButton; QComboBox *mCaptureArea { nullptr }; SmartSpinBox *mDelayMsec { nullptr }; QCheckBox *mCaptureOnClick { nullptr }; diff --git a/src/Gui/KSWidget.cpp b/src/Gui/KSWidget.cpp --- a/src/Gui/KSWidget.cpp +++ b/src/Gui/KSWidget.cpp @@ -23,6 +23,7 @@ #include "KSImageWidget.h" #include "SmartSpinBox.h" #include "SpectacleConfig.h" +#include "ProgressButton.h" #include #include @@ -135,7 +136,7 @@ }); // the take a new screenshot button - mTakeScreenshotButton = new QToolButton(this); + mTakeScreenshotButton = new ProgressButton(this); mTakeScreenshotButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); mTakeScreenshotButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); setButtonState(State::TakeNewScreenshot); @@ -271,10 +272,17 @@ case State::TakeNewScreenshot: mTakeScreenshotButton->removeAction(mCancelAction); mTakeScreenshotButton->setDefaultAction(mTakeNewScreenshotAction); + mTakeScreenshotButton->setProgress(0); break; case State::Cancel: mTakeScreenshotButton->removeAction(mTakeNewScreenshotAction); mTakeScreenshotButton->setDefaultAction(mCancelAction); break; } } + +void KSWidget::setProgress(double progress) +{ + mTakeScreenshotButton->setProgress(progress); +} + diff --git a/src/Gui/ProgressButton.h b/src/Gui/ProgressButton.h new file mode 100644 --- /dev/null +++ b/src/Gui/ProgressButton.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2019 David Redondo + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef PROGRESSBUTTON_H +#define PROGRESSBUTTON_H + +#include + +/** + * @todo write docs + */ +class ProgressButton : public QToolButton +{ +public: + + ProgressButton(QWidget* parent); + + void setProgress(double progress); + +protected: + void paintEvent(QPaintEvent* event) override; + + double mProgress; +}; + +#endif // PROGRESSBUTTON_H diff --git a/src/Gui/ProgressButton.cpp b/src/Gui/ProgressButton.cpp new file mode 100644 --- /dev/null +++ b/src/Gui/ProgressButton.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2019 David Redondo + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "ProgressButton.h" + +#include +#include + +#include + +ProgressButton::ProgressButton(QWidget* parent) + : QToolButton{parent} + , mProgress(0) +{ +} + +void ProgressButton::setProgress(double progress) +{ + mProgress = progress; + repaint(); +} + +void ProgressButton::paintEvent(QPaintEvent* event) +{ + //Draw Button without text and icon, note the missing text and icon in options + QStylePainter painter(this); + QStyleOption toolbuttonOptions; + toolbuttonOptions.initFrom(this); + if (isDown()) { + toolbuttonOptions.state.setFlag(QStyle::State_Sunken); + } else { + toolbuttonOptions.state.setFlag(QStyle::State_Raised); + } + painter.drawPrimitive(QStyle::PE_PanelButtonTool, toolbuttonOptions); + auto pal = palette(); + if (!qFuzzyIsNull(mProgress)) { + //Draw overlay + KColorScheme::adjustForeground(pal, KColorScheme::PositiveText, QPalette::Button, KColorScheme::Button); + QStyleOption overlayOption; + overlayOption.rect = layoutDirection() == Qt::LeftToRight + ? QRect(0, 0, width() * mProgress, height()) + : QRect(width() * (1-mProgress), 0, width(), height()); + overlayOption.palette = pal; + overlayOption.state.setFlag(QStyle::State_Sunken, isDown()); + painter.setCompositionMode(QPainter::CompositionMode_SourceAtop); + painter.setOpacity(0.5); + painter.drawPrimitive(QStyle::PE_PanelButtonTool, overlayOption); + } + //Finally draw text and icon and outline + QStyleOptionToolButton labelOptions; + labelOptions.initFrom(this); + labelOptions.text = text(); + labelOptions.icon = icon(); + labelOptions.toolButtonStyle = Qt::ToolButtonTextBesideIcon; + labelOptions.iconSize = iconSize(); + labelOptions.state.setFlag(QStyle::State_Sunken, isDown()); + painter.setOpacity(1); + painter.drawControl(QStyle::CE_ToolButtonLabel, labelOptions); +}