Move timeline preview button in its own class for re-use (could be nice for render button)

parent e0431d21
......@@ -49,7 +49,6 @@ namespace Mlt {
* @class ProjectClip
* @brief Represents a clip in the project (not timeline).
*
*/
class ProjectClip : public AbstractProjectItem
......
......@@ -64,6 +64,7 @@
#include "timeline/timelinesearch.h"
#include <config-kdenlive.h>
#include "utils/thememanager.h"
#include "utils/progressbutton.h"
#include "utils/KoIconUtils.h"
#ifdef USE_JOGSHUTTLE
#include "jogshuttle/jogmanager.h"
......@@ -110,94 +111,6 @@
#include <QVBoxLayout>
#include <QtGlobal>
MyToolButton::MyToolButton(QWidget *parent) : QToolButton(parent)
, m_defaultAction(NULL)
{
setPopupMode(MenuButtonPopup);
m_progress = width() - 6;
m_pix = new QPixmap(1,1);
m_pix->fill(Qt::transparent);
m_dummyAction = new QAction(QIcon(*m_pix), i18n("Rendering preview"), this);
}
MyToolButton::~MyToolButton()
{
delete m_pix;
delete m_dummyAction;
}
void MyToolButton::setProgress(int progress)
{
int prog = (width() - 6) * (double) progress / 1000;
if (m_timer.isValid() && progress > 0) {
// calculate remaining time
qint64 ms = m_timer.elapsed() * (1000.0 / progress - 1);
if (ms < 60000)
// xgettext:no-c-format
m_remainingTime = i18nc("s as seconds", "%1s", ms / 1000);
else if (ms < 3600000)
// xgettext:no-c-format
m_remainingTime = i18nc("m as minutes", "%1m", ms / 60000);
else {
m_remainingTime = i18nc("h as hours", "%1h", qMin(99, (int) (ms / 3600000)));
}
}
if (progress < 0) {
if (m_defaultAction)
setDefaultAction(m_defaultAction);
m_remainingTime.clear();
m_timer.invalidate();
m_progress = -1;
update();
return;
}
if (prog == m_progress)
return;
if (!m_timer.isValid() || progress == 0) {
if (!m_defaultAction) {
m_defaultAction = defaultAction();
}
setDefaultAction(m_dummyAction);
m_timer.start();
}
if (progress == 1000) {
if (m_defaultAction)
setDefaultAction(m_defaultAction);
m_remainingTime.clear();
m_timer.invalidate();
}
m_progress = prog;
update();
}
int MyToolButton::progress() const
{
return m_progress;
}
void MyToolButton::paintEvent(QPaintEvent *event)
{
QToolButton::paintEvent(event);
if (m_progress < width() - 6) {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
if (m_progress < 0)
painter.fillRect(3, height() - 5, (width() - 6), 3, Qt::red);
else {
if (m_progress > 0) {
// draw remaining time
painter.drawText(rect(), Qt::AlignLeft, m_remainingTime);
}
QColor w(Qt::white);
w.setAlpha(40);
painter.fillRect(3, height() - 5, m_progress, 3, palette().highlight().color());
painter.fillRect(3 + m_progress, height() - 5, width() - 6 - m_progress, 3, w);
}
painter.setPen(palette().shadow().color());
painter.drawRoundedRect(2, height() - 6, width() - 4, 5, 2, 2);
}
}
static const char version[] = KDENLIVE_VERSION;
namespace Mlt
......@@ -532,10 +445,10 @@ MainWindow::MainWindow(const QString &MltPath, const QUrl &Url, const QString &
connect(toolButtonAction, SIGNAL(triggered(bool)), basketButton, SLOT(showMenu()));
// Timeline preview button
MyToolButton *timelinePreview = new MyToolButton(this);
ProgressButton *timelinePreview = new ProgressButton(i18n("Rendering preview"), this);
QMenu *tlMenu = new QMenu(this);
timelinePreview->setMenu(tlMenu);
connect(this, &MainWindow::setPreviewProgress, timelinePreview, &MyToolButton::setProgress);
connect(this, &MainWindow::setPreviewProgress, timelinePreview, &ProgressButton::setProgress);
QWidgetAction* previewButtonAction = new QWidgetAction(this);
previewButtonAction->setText(i18n("Timeline Preview"));
previewButtonAction->setIcon(KoIconUtils::themedIcon(QStringLiteral("preview-render-on")));
......@@ -543,6 +456,8 @@ MainWindow::MainWindow(const QString &MltPath, const QUrl &Url, const QString &
addAction(QStringLiteral("timeline_preview_button"), previewButtonAction);
setupGUI();
timelinePreview->setToolButtonStyle(m_timelineToolBar->toolButtonStyle());
connect(m_timelineToolBar, &QToolBar::toolButtonStyleChanged, timelinePreview, &ProgressButton::setToolButtonStyle);
/*ScriptingPart* sp = new ScriptingPart(this, QStringList());
guiFactory()->addClient(sp);*/
......@@ -610,6 +525,8 @@ MainWindow::MainWindow(const QString &MltPath, const QUrl &Url, const QString &
slotConnectMonitors();
m_timelineToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
// TODO: let user select timeline toolbar toolbutton style
//connect(toolBar(), &QToolBar::toolButtonStyleChanged, m_timelineToolBar, &QToolBar::setToolButtonStyle);
m_timelineToolBar->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_timelineToolBar, &QWidget::customContextMenuRequested, this, &MainWindow::showTimelineToolbarMenu);
......@@ -1469,7 +1386,7 @@ void MainWindow::setupActions()
disableEffects->setData("disable_timeline_effects");
disableEffects->setCheckable(true);
disableEffects->setChecked(false);
QAction *locateClip = addAction(QStringLiteral("locate_clip"), i18n("Locate Clip..."), pCore->bin(), SLOT(slotLocateClip()), KoIconUtils::themedIcon(QStringLiteral("edit-file")));
locateClip->setData("locate_clip");
locateClip->setEnabled(false);
......
......@@ -60,29 +60,6 @@ class KIconLoader;
#define EXIT_RESTART (42)
class MyToolButton: public QToolButton
{
Q_PROPERTY(int progress READ progress WRITE setProgress)
Q_OBJECT
public:
explicit MyToolButton(QWidget *parent = 0);
~MyToolButton();
int progress() const;
void setProgress(int);
protected:
virtual void paintEvent(QPaintEvent *event);
private:
int m_progress;
QPixmap *m_pix;
QElapsedTimer m_timer;
QString m_remainingTime;
QAction *m_defaultAction;
/** @brief While rendering, replace real action by a fake on so that rendering is not triggered when clicking again. */
QAction *m_dummyAction;
};
class /*KDENLIVECORE_EXPORT*/ MainWindow : public KXmlGuiWindow
{
Q_OBJECT
......
......@@ -8,6 +8,7 @@ set(kdenlive_SRCS
utils/flowlayout.cpp
utils/thememanager.cpp
utils/KoIconUtils.cpp
utils/progressbutton.cpp
PARENT_SCOPE
)
......
/*
Copyright (C) 2016 Jean-Baptiste Mardelle <jb@kdenlive.org>
This file is part of Kdenlive. See www.kdenlive.org.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License or (at your option) version 3 or any later version
accepted by the membership of KDE e.V. (or its successor approved
by the membership of KDE e.V.), which shall act as a proxy
defined in Section 14 of version 3 of the license.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "progressbutton.h"
#include <klocalizedstring.h>
#include <QAction>
#include <QPainter>
ProgressButton::ProgressButton(const QString text, QWidget *parent) : QToolButton(parent)
, m_defaultAction(NULL)
{
setPopupMode(MenuButtonPopup);
m_progress = width() - 6;
QPixmap pix(1,1);
pix.fill(Qt::transparent);
m_dummyAction = new QAction(QIcon(pix), text, this);
initStyleOption(&m_buttonStyle);
m_iconSize = height() - 4;
m_progressFont = font();
m_progressFont.setPixelSize(m_iconSize / 2);
}
ProgressButton::~ProgressButton()
{
delete m_dummyAction;
}
void ProgressButton::setProgress(int progress)
{
int prog = (m_iconSize) * (double) progress / 1000;
QString remaining;
if (m_timer.isValid() && progress > 0) {
// calculate remaining time
qint64 ms = m_timer.elapsed() * (1000.0 / progress - 1);
if (ms < 60000)
// xgettext:no-c-format
remaining = i18nc("s as seconds", "%1s", ms / 1000);
else if (ms < 3600000)
// xgettext:no-c-format
remaining = i18nc("m as minutes", "%1m", ms / 60000);
else {
remaining = i18nc("h as hours", "%1h", qMin(99, (int) (ms / 3600000)));
}
}
if (progress < 0) {
if (m_defaultAction)
setDefaultAction(m_defaultAction);
m_remainingTime.clear();
m_timer.invalidate();
m_progress = -1;
update();
return;
}
if (!m_timer.isValid() || progress == 0) {
if (!m_defaultAction) {
m_defaultAction = defaultAction();
}
setDefaultAction(m_dummyAction);
m_timer.start();
}
if (progress == 1000) {
if (m_defaultAction)
setDefaultAction(m_defaultAction);
m_remainingTime.clear();
m_timer.invalidate();
}
m_progress = prog;
if (remaining != m_remainingTime) {
m_remainingTime = remaining;
update();
}
}
int ProgressButton::progress() const
{
return m_progress;
}
void ProgressButton::paintEvent(QPaintEvent *event)
{
QToolButton::paintEvent(event);
if (m_progress < m_iconSize) {
QPainter painter(this);
painter.setFont(m_progressFont);
painter.setRenderHint(QPainter::Antialiasing, true);
QRectF rect(3, (height() - m_iconSize) / 2, m_iconSize, m_iconSize);
// draw remaining time
painter.drawText(rect, Qt::AlignHCenter, m_remainingTime);
if (m_progress < 0) {
painter.fillRect(rect.x(), rect.bottom() - 5, rect.width(), 3, Qt::red);
} else {
QColor w(Qt::white);
w.setAlpha(40);
painter.fillRect(rect.x(), rect.bottom() - 6, m_progress, 4, palette().highlight().color());
painter.fillRect(rect.x(), rect.bottom() - 6, rect.width(), 4, w);
}
painter.setPen(palette().shadow().color());
painter.drawRoundedRect(rect.x(), rect.bottom() - 7, rect.width(), 6, 2, 2);
}
}
/*
Copyright (C) 2016 Jean-Baptiste Mardelle <jb@kdenlive.org>
This file is part of Kdenlive. See www.kdenlive.org.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License or (at your option) version 3 or any later version
accepted by the membership of KDE e.V. (or its successor approved
by the membership of KDE e.V.), which shall act as a proxy
defined in Section 14 of version 3 of the license.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PROGRESSBUTTON_H
#define PROGRESSBUTTON_H
#include <QToolButton>
#include <QElapsedTimer>
#include <QStyleOptionToolButton>
class QAction;
/**
* @class ProgressButton
* @brief A Toolbar button with a small progress bar.
*
*/
class ProgressButton: public QToolButton
{
Q_PROPERTY(int progress READ progress WRITE setProgress)
Q_OBJECT
public:
explicit ProgressButton(const QString text, QWidget *parent = 0);
~ProgressButton();
int progress() const;
void setProgress(int);
protected:
virtual void paintEvent(QPaintEvent *event);
private:
int m_progress;
QElapsedTimer m_timer;
QString m_remainingTime;
int m_iconSize;
QFont m_progressFont;
QAction *m_defaultAction;
QStyleOptionToolButton m_buttonStyle;
/** @brief While rendering, replace real action by a fake on so that rendering is not triggered when clicking again. */
QAction *m_dummyAction;
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment