2015-07-17 22:06:55 +00:00
|
|
|
/* progress_frame.cpp
|
2012-07-19 21:49:52 +00:00
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
2018-04-30 07:47:58 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2012-07-19 21:49:52 +00:00
|
|
|
|
2012-09-20 02:03:38 +00:00
|
|
|
#include "config.h"
|
2012-07-19 21:49:52 +00:00
|
|
|
|
2015-07-17 22:06:55 +00:00
|
|
|
#include "progress_frame.h"
|
|
|
|
#include <ui_progress_frame.h>
|
2012-07-19 21:49:52 +00:00
|
|
|
|
2014-01-14 01:26:51 +00:00
|
|
|
#include "ui/progress_dlg.h"
|
|
|
|
|
2015-08-24 19:33:49 +00:00
|
|
|
#include <QDialogButtonBox>
|
2012-07-19 22:33:47 +00:00
|
|
|
#include <QGraphicsOpacityEffect>
|
2015-08-24 19:33:49 +00:00
|
|
|
#include <QBoxLayout>
|
2012-07-19 21:49:52 +00:00
|
|
|
#include <QPropertyAnimation>
|
|
|
|
|
2017-07-25 14:15:18 +00:00
|
|
|
#include <ui/qt/widgets/stock_icon_tool_button.h>
|
2022-02-01 03:30:09 +00:00
|
|
|
#include "main_application.h"
|
2015-06-19 23:56:44 +00:00
|
|
|
|
|
|
|
// To do:
|
2017-04-05 19:15:27 +00:00
|
|
|
// - Add an NSProgressIndicator to the dock icon on macOS.
|
2015-06-19 23:56:44 +00:00
|
|
|
// - Start adding the progress bar to dialogs.
|
|
|
|
// - Don't complain so loudly when the user stops a capture.
|
2013-09-03 16:30:38 +00:00
|
|
|
|
2016-01-05 12:06:36 +00:00
|
|
|
progdlg_t *
|
2020-03-28 20:10:39 +00:00
|
|
|
create_progress_dlg(gpointer top_level_window, const gchar *task_title, const gchar *item_title,
|
2015-07-03 20:52:49 +00:00
|
|
|
gboolean terminate_is_stop, gboolean *stop_flag) {
|
2015-07-17 22:06:55 +00:00
|
|
|
ProgressFrame *pf;
|
2012-07-19 21:49:52 +00:00
|
|
|
QWidget *main_window;
|
|
|
|
|
|
|
|
if (!top_level_window) {
|
2020-03-28 20:10:39 +00:00
|
|
|
return nullptr;
|
2012-07-19 21:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main_window = qobject_cast<QWidget *>((QObject *)top_level_window);
|
|
|
|
|
|
|
|
if (!main_window) {
|
2020-03-28 20:10:39 +00:00
|
|
|
return nullptr;
|
2012-07-19 21:49:52 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 22:06:55 +00:00
|
|
|
pf = main_window->findChild<ProgressFrame *>();
|
2012-07-19 21:49:52 +00:00
|
|
|
|
2015-07-17 22:06:55 +00:00
|
|
|
if (!pf) {
|
2020-03-28 20:10:39 +00:00
|
|
|
return nullptr;
|
2012-07-19 21:49:52 +00:00
|
|
|
}
|
2020-03-28 20:10:39 +00:00
|
|
|
|
|
|
|
QString title = task_title;
|
|
|
|
if (item_title && strlen(item_title) > 0) {
|
|
|
|
title.append(" ").append(item_title);
|
|
|
|
}
|
|
|
|
return pf->showProgress(title, true, terminate_is_stop, stop_flag, 0);
|
2015-07-03 20:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
progdlg_t *
|
2016-01-05 12:06:36 +00:00
|
|
|
delayed_create_progress_dlg(gpointer top_level_window, const gchar *task_title, const gchar *item_title,
|
2015-07-03 20:52:49 +00:00
|
|
|
gboolean terminate_is_stop, gboolean *stop_flag,
|
2019-07-16 22:11:59 +00:00
|
|
|
gfloat progress)
|
2015-07-03 20:52:49 +00:00
|
|
|
{
|
|
|
|
progdlg_t *progress_dialog = create_progress_dlg(top_level_window, task_title, item_title, terminate_is_stop, stop_flag);
|
|
|
|
update_progress_dlg(progress_dialog, progress, item_title);
|
|
|
|
return progress_dialog;
|
2012-07-19 21:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the progress information of the progress bar box.
|
|
|
|
*/
|
|
|
|
void
|
2015-07-07 22:13:24 +00:00
|
|
|
update_progress_dlg(progdlg_t *dlg, gfloat percentage, const gchar *)
|
2012-07-19 21:49:52 +00:00
|
|
|
{
|
2015-09-18 16:09:38 +00:00
|
|
|
if (!dlg) return;
|
2012-07-19 21:49:52 +00:00
|
|
|
|
2020-03-28 20:10:39 +00:00
|
|
|
dlg->progress_frame->setValue((int)(percentage * 100));
|
2012-07-19 21:49:52 +00:00
|
|
|
|
|
|
|
/*
|
2012-10-26 18:31:00 +00:00
|
|
|
* Flush out the update and process any input events.
|
|
|
|
*/
|
2022-02-01 03:30:09 +00:00
|
|
|
MainApplication::processEvents();
|
2012-07-19 21:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy the progress bar.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
destroy_progress_dlg(progdlg_t *dlg)
|
|
|
|
{
|
2015-06-19 23:56:44 +00:00
|
|
|
dlg->progress_frame->hide();
|
2012-07-19 21:49:52 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 22:06:55 +00:00
|
|
|
ProgressFrame::ProgressFrame(QWidget *parent) :
|
2015-06-19 23:56:44 +00:00
|
|
|
QFrame(parent),
|
2015-07-17 22:06:55 +00:00
|
|
|
ui(new Ui::ProgressFrame)
|
2015-06-19 23:56:44 +00:00
|
|
|
, terminate_is_stop_(false)
|
2020-03-28 20:10:39 +00:00
|
|
|
, stop_flag_(nullptr)
|
2015-07-17 22:06:55 +00:00
|
|
|
, show_timer_(-1)
|
2020-03-28 20:10:39 +00:00
|
|
|
, effect_(nullptr)
|
|
|
|
, animation_(nullptr)
|
2014-08-13 15:10:44 +00:00
|
|
|
#ifdef QWINTASKBARPROGRESS_H
|
2015-08-24 19:33:49 +00:00
|
|
|
, update_taskbar_(false)
|
2015-06-19 23:56:44 +00:00
|
|
|
, taskbar_progress_(NULL)
|
2014-08-13 15:10:44 +00:00
|
|
|
#endif
|
2012-07-19 21:49:52 +00:00
|
|
|
{
|
2015-06-19 23:56:44 +00:00
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
progress_dialog_.progress_frame = this;
|
2013-09-03 16:14:38 +00:00
|
|
|
progress_dialog_.top_level_window = window();
|
2012-08-01 00:06:21 +00:00
|
|
|
|
2020-03-28 20:10:39 +00:00
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
ui->label->setAttribute(Qt::WA_MacSmallSize, true);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ui->label->setStyleSheet(QString(
|
|
|
|
"QLabel {"
|
|
|
|
" background: transparent;"
|
|
|
|
"}"));
|
|
|
|
|
2015-06-19 23:56:44 +00:00
|
|
|
ui->progressBar->setStyleSheet(QString(
|
|
|
|
"QProgressBar {"
|
2012-08-01 00:06:21 +00:00
|
|
|
" max-width: 20em;"
|
2015-08-24 19:33:49 +00:00
|
|
|
" min-height: 0.5em;"
|
2012-08-01 00:06:21 +00:00
|
|
|
" max-height: 1em;"
|
2015-06-19 23:56:44 +00:00
|
|
|
" border-bottom: 0px;"
|
|
|
|
" border-top: 0px;"
|
2012-08-01 16:17:47 +00:00
|
|
|
" background: transparent;"
|
2012-08-01 00:06:21 +00:00
|
|
|
"}"));
|
|
|
|
|
2015-09-10 21:42:07 +00:00
|
|
|
ui->stopButton->setStockIcon("x-filter-clear");
|
|
|
|
ui->stopButton->setIconSize(QSize(14, 14));
|
|
|
|
ui->stopButton->setStyleSheet(
|
|
|
|
"QToolButton {"
|
|
|
|
" border: none;"
|
|
|
|
" background: transparent;" // Disables platform style on Windows.
|
|
|
|
" padding: 0px;"
|
|
|
|
" margin: 0px;"
|
2015-06-19 23:56:44 +00:00
|
|
|
" min-height: 0.8em;"
|
|
|
|
" max-height: 1em;"
|
|
|
|
" min-width: 0.8em;"
|
|
|
|
" max-width: 1em;"
|
|
|
|
"}"
|
2015-09-10 21:42:07 +00:00
|
|
|
);
|
2015-09-11 18:13:23 +00:00
|
|
|
|
|
|
|
effect_ = new QGraphicsOpacityEffect(this);
|
|
|
|
animation_ = new QPropertyAnimation(effect_, "opacity", this);
|
2015-08-24 19:33:49 +00:00
|
|
|
connect(this, SIGNAL(showRequested(bool,bool,gboolean*)),
|
|
|
|
this, SLOT(show(bool,bool,gboolean*)));
|
2012-07-19 21:49:52 +00:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
2015-07-17 22:06:55 +00:00
|
|
|
ProgressFrame::~ProgressFrame()
|
2015-06-19 23:56:44 +00:00
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
2012-07-19 21:49:52 +00:00
|
|
|
|
2020-03-28 20:10:39 +00:00
|
|
|
struct progdlg *ProgressFrame::showProgress(const QString &title, bool animate, bool terminate_is_stop, gboolean *stop_flag, int value)
|
2015-06-19 23:56:44 +00:00
|
|
|
{
|
2015-08-24 19:33:49 +00:00
|
|
|
setMaximumValue(100);
|
2015-07-17 22:06:55 +00:00
|
|
|
ui->progressBar->setValue(value);
|
2020-03-28 20:10:39 +00:00
|
|
|
QString elided_title = title;
|
|
|
|
int max_w = fontMetrics().height() * 20; // em-widths, arbitrary
|
2020-03-31 18:29:53 +00:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
|
|
|
|
int title_w = fontMetrics().horizontalAdvance(title);
|
|
|
|
#else
|
|
|
|
int title_w = fontMetrics().width(title);
|
|
|
|
#endif
|
|
|
|
if (title_w > max_w) {
|
2020-03-28 20:10:39 +00:00
|
|
|
elided_title = fontMetrics().elidedText(title, Qt::ElideRight, max_w);
|
|
|
|
}
|
|
|
|
// If we're in the main status bar, should we push this as a status message instead?
|
|
|
|
ui->label->setText(elided_title);
|
2015-08-24 19:33:49 +00:00
|
|
|
emit showRequested(animate, terminate_is_stop, stop_flag);
|
|
|
|
return &progress_dialog_;
|
2015-07-17 22:06:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
progdlg *ProgressFrame::showBusy(bool animate, bool terminate_is_stop, gboolean *stop_flag)
|
|
|
|
{
|
2015-08-24 19:33:49 +00:00
|
|
|
setMaximumValue(0);
|
|
|
|
emit showRequested(animate, terminate_is_stop, stop_flag);
|
|
|
|
return &progress_dialog_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgressFrame::addToButtonBox(QDialogButtonBox *button_box, QObject *main_window)
|
|
|
|
{
|
|
|
|
// We have a ProgressFrame in the main status bar which is controlled
|
|
|
|
// from the capture file and other parts of the application via
|
|
|
|
// create_progress_dlg and delayed_create_progress_dlg.
|
|
|
|
// Create a new ProgressFrame and pair it with the main instance.
|
|
|
|
ProgressFrame *main_progress_frame = main_window->findChild<ProgressFrame *>();
|
|
|
|
if (!button_box || !main_progress_frame) return;
|
|
|
|
|
|
|
|
QBoxLayout *layout = qobject_cast<QBoxLayout *>(button_box->layout());
|
|
|
|
if (!layout) return;
|
|
|
|
|
|
|
|
ProgressFrame *progress_frame = new ProgressFrame(button_box);
|
|
|
|
|
|
|
|
// Insert ourselves after the first spacer we find, otherwise the
|
|
|
|
// far right of the button box.
|
|
|
|
int idx = layout->count();
|
|
|
|
for (int i = 0; i < layout->count(); i++) {
|
|
|
|
if (layout->itemAt(i)->spacerItem()) {
|
|
|
|
idx = i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
layout->insertWidget(idx, progress_frame);
|
|
|
|
|
|
|
|
int one_em = progress_frame->fontMetrics().height();
|
|
|
|
progress_frame->setMaximumWidth(one_em * 8);
|
|
|
|
connect(main_progress_frame, SIGNAL(showRequested(bool,bool,gboolean*)),
|
|
|
|
progress_frame, SLOT(show(bool,bool,gboolean*)));
|
|
|
|
connect(main_progress_frame, SIGNAL(maximumValueChanged(int)),
|
|
|
|
progress_frame, SLOT(setMaximumValue(int)));
|
|
|
|
connect(main_progress_frame, SIGNAL(valueChanged(int)),
|
|
|
|
progress_frame, SLOT(setValue(int)));
|
|
|
|
connect(main_progress_frame, SIGNAL(setHidden()),
|
|
|
|
progress_frame, SLOT(hide()));
|
|
|
|
|
|
|
|
connect(progress_frame, SIGNAL(stopLoading()),
|
|
|
|
main_progress_frame, SIGNAL(stopLoading()));
|
2015-07-17 22:06:55 +00:00
|
|
|
}
|
2012-07-19 21:49:52 +00:00
|
|
|
|
2015-08-28 19:57:57 +00:00
|
|
|
void ProgressFrame::captureFileClosing()
|
|
|
|
{
|
|
|
|
// Hide any paired ProgressFrames and disconnect from them.
|
|
|
|
emit setHidden();
|
|
|
|
disconnect(SIGNAL(showRequested(bool,bool,gboolean*)));
|
|
|
|
disconnect(SIGNAL(maximumValueChanged(int)));
|
|
|
|
disconnect(SIGNAL(valueChanged(int)));
|
|
|
|
|
|
|
|
connect(this, SIGNAL(showRequested(bool,bool,gboolean*)),
|
|
|
|
this, SLOT(show(bool,bool,gboolean*)));
|
|
|
|
}
|
|
|
|
|
2015-07-17 22:06:55 +00:00
|
|
|
void ProgressFrame::setValue(int value)
|
|
|
|
{
|
2015-06-19 23:56:44 +00:00
|
|
|
ui->progressBar->setValue(value);
|
2015-08-24 19:33:49 +00:00
|
|
|
emit valueChanged(value);
|
2015-07-17 22:06:55 +00:00
|
|
|
}
|
2012-07-19 21:49:52 +00:00
|
|
|
|
2015-07-17 22:06:55 +00:00
|
|
|
void ProgressFrame::timerEvent(QTimerEvent *event)
|
|
|
|
{
|
|
|
|
if (event->timerId() == show_timer_) {
|
|
|
|
killTimer(show_timer_);
|
|
|
|
show_timer_ = -1;
|
|
|
|
|
2015-09-11 18:13:23 +00:00
|
|
|
this->setGraphicsEffect(effect_);
|
2012-07-19 22:33:47 +00:00
|
|
|
|
2022-06-27 14:02:44 +00:00
|
|
|
animation_->setDuration(200);
|
2015-09-11 18:13:23 +00:00
|
|
|
animation_->setStartValue(0.1);
|
|
|
|
animation_->setEndValue(1.0);
|
|
|
|
animation_->setEasingCurve(QEasingCurve::InOutQuad);
|
|
|
|
animation_->start();
|
2015-07-17 22:06:55 +00:00
|
|
|
|
|
|
|
QFrame::show();
|
2016-03-15 16:49:48 +00:00
|
|
|
} else {
|
|
|
|
QFrame::timerEvent(event);
|
2015-07-17 22:06:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgressFrame::hide()
|
|
|
|
{
|
|
|
|
show_timer_ = -1;
|
2015-08-24 19:33:49 +00:00
|
|
|
emit setHidden();
|
2015-07-17 22:06:55 +00:00
|
|
|
QFrame::hide();
|
|
|
|
#ifdef QWINTASKBARPROGRESS_H
|
|
|
|
if (taskbar_progress_) {
|
2016-07-19 16:26:24 +00:00
|
|
|
disconnect(this, SIGNAL(valueChanged(int)), taskbar_progress_, SLOT(setValue(int)));
|
2015-07-17 22:06:55 +00:00
|
|
|
taskbar_progress_->reset();
|
|
|
|
taskbar_progress_->hide();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-09-10 21:42:07 +00:00
|
|
|
void ProgressFrame::on_stopButton_clicked()
|
2015-07-17 22:06:55 +00:00
|
|
|
{
|
|
|
|
emit stopLoading();
|
|
|
|
}
|
|
|
|
|
2022-06-27 14:02:44 +00:00
|
|
|
const int show_delay_ = 150; // ms
|
2015-09-23 21:49:27 +00:00
|
|
|
|
2015-08-24 19:33:49 +00:00
|
|
|
void ProgressFrame::show(bool animate, bool terminate_is_stop, gboolean *stop_flag)
|
2015-07-17 22:06:55 +00:00
|
|
|
{
|
|
|
|
terminate_is_stop_ = terminate_is_stop;
|
|
|
|
stop_flag_ = stop_flag;
|
|
|
|
|
|
|
|
if (stop_flag) {
|
2015-09-10 21:42:07 +00:00
|
|
|
ui->stopButton->show();
|
2015-07-17 22:06:55 +00:00
|
|
|
} else {
|
2015-09-10 21:42:07 +00:00
|
|
|
ui->stopButton->hide();
|
2015-07-17 22:06:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (animate) {
|
|
|
|
show_timer_ = startTimer(show_delay_);
|
|
|
|
} else {
|
|
|
|
QFrame::show();
|
2012-07-19 21:49:52 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 15:10:44 +00:00
|
|
|
#ifdef QWINTASKBARPROGRESS_H
|
|
|
|
// windowHandle() is picky about returning a non-NULL value so we check it
|
|
|
|
// each time.
|
2015-08-24 19:33:49 +00:00
|
|
|
if (update_taskbar_ && !taskbar_progress_ && window()->windowHandle()) {
|
2014-08-13 15:10:44 +00:00
|
|
|
QWinTaskbarButton *taskbar_button = new QWinTaskbarButton(this);
|
|
|
|
if (taskbar_button) {
|
|
|
|
taskbar_button->setWindow(window()->windowHandle());
|
|
|
|
taskbar_progress_ = taskbar_button->progress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (taskbar_progress_) {
|
|
|
|
taskbar_progress_->show();
|
2016-07-19 16:26:24 +00:00
|
|
|
taskbar_progress_->reset();
|
|
|
|
connect(this, SIGNAL(valueChanged(int)), taskbar_progress_, SLOT(setValue(int)));
|
2014-08-13 15:10:44 +00:00
|
|
|
}
|
|
|
|
#endif
|
2015-08-24 19:33:49 +00:00
|
|
|
}
|
2014-08-13 15:10:44 +00:00
|
|
|
|
2015-08-24 19:33:49 +00:00
|
|
|
void ProgressFrame::setMaximumValue(int value)
|
|
|
|
{
|
|
|
|
ui->progressBar->setMaximum(value);
|
|
|
|
emit maximumValueChanged(value);
|
2012-07-19 21:49:52 +00:00
|
|
|
}
|