3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later*/
11 #include "progress_frame.h"
12 #include <ui_progress_frame.h>
14 #include "ui/progress_dlg.h"
16 #include <QDialogButtonBox>
17 #include <QGraphicsOpacityEffect>
19 #include <QPropertyAnimation>
21 #include <ui/qt/widgets/stock_icon_tool_button.h>
22 #include "wireshark_application.h"
25 // - Add an NSProgressIndicator to the dock icon on macOS.
26 // - Start adding the progress bar to dialogs.
27 // - Don't complain so loudly when the user stops a capture.
30 create_progress_dlg(gpointer top_level_window, const gchar *, const gchar *,
31 gboolean terminate_is_stop, gboolean *stop_flag) {
35 if (!top_level_window) {
39 main_window = qobject_cast<QWidget *>((QObject *)top_level_window);
45 pf = main_window->findChild<ProgressFrame *>();
50 return pf->showProgress(true, terminate_is_stop, stop_flag, 0);
54 delayed_create_progress_dlg(gpointer top_level_window, const gchar *task_title, const gchar *item_title,
55 gboolean terminate_is_stop, gboolean *stop_flag,
56 const GTimeVal *, gfloat progress)
58 progdlg_t *progress_dialog = create_progress_dlg(top_level_window, task_title, item_title, terminate_is_stop, stop_flag);
59 update_progress_dlg(progress_dialog, progress, item_title);
60 return progress_dialog;
64 * Update the progress information of the progress bar box.
67 update_progress_dlg(progdlg_t *dlg, gfloat percentage, const gchar *)
71 dlg->progress_frame->setValue(percentage * 100);
74 * Flush out the update and process any input events.
76 WiresharkApplication::processEvents();
80 * Destroy the progress bar.
83 destroy_progress_dlg(progdlg_t *dlg)
85 dlg->progress_frame->hide();
88 ProgressFrame::ProgressFrame(QWidget *parent) :
90 ui(new Ui::ProgressFrame)
91 , terminate_is_stop_(false)
96 #ifdef QWINTASKBARPROGRESS_H
97 , update_taskbar_(false)
98 , taskbar_progress_(NULL)
103 progress_dialog_.progress_frame = this;
104 progress_dialog_.top_level_window = window();
106 ui->progressBar->setStyleSheet(QString(
109 " min-height: 0.5em;"
111 " border-bottom: 0px;"
113 " background: transparent;"
116 ui->stopButton->setStockIcon("x-filter-clear");
117 ui->stopButton->setIconSize(QSize(14, 14));
118 ui->stopButton->setStyleSheet(
121 " background: transparent;" // Disables platform style on Windows.
124 " min-height: 0.8em;"
131 effect_ = new QGraphicsOpacityEffect(this);
132 animation_ = new QPropertyAnimation(effect_, "opacity", this);
133 connect(this, SIGNAL(showRequested(bool,bool,gboolean*)),
134 this, SLOT(show(bool,bool,gboolean*)));
138 ProgressFrame::~ProgressFrame()
143 struct progdlg *ProgressFrame::showProgress(bool animate, bool terminate_is_stop, gboolean *stop_flag, int value)
145 setMaximumValue(100);
146 ui->progressBar->setValue(value);
147 emit showRequested(animate, terminate_is_stop, stop_flag);
148 return &progress_dialog_;
151 progdlg *ProgressFrame::showBusy(bool animate, bool terminate_is_stop, gboolean *stop_flag)
154 emit showRequested(animate, terminate_is_stop, stop_flag);
155 return &progress_dialog_;
158 void ProgressFrame::addToButtonBox(QDialogButtonBox *button_box, QObject *main_window)
160 // We have a ProgressFrame in the main status bar which is controlled
161 // from the capture file and other parts of the application via
162 // create_progress_dlg and delayed_create_progress_dlg.
163 // Create a new ProgressFrame and pair it with the main instance.
164 ProgressFrame *main_progress_frame = main_window->findChild<ProgressFrame *>();
165 if (!button_box || !main_progress_frame) return;
167 QBoxLayout *layout = qobject_cast<QBoxLayout *>(button_box->layout());
170 ProgressFrame *progress_frame = new ProgressFrame(button_box);
172 // Insert ourselves after the first spacer we find, otherwise the
173 // far right of the button box.
174 int idx = layout->count();
175 for (int i = 0; i < layout->count(); i++) {
176 if (layout->itemAt(i)->spacerItem()) {
181 layout->insertWidget(idx, progress_frame);
183 int one_em = progress_frame->fontMetrics().height();
184 progress_frame->setMaximumWidth(one_em * 8);
185 connect(main_progress_frame, SIGNAL(showRequested(bool,bool,gboolean*)),
186 progress_frame, SLOT(show(bool,bool,gboolean*)));
187 connect(main_progress_frame, SIGNAL(maximumValueChanged(int)),
188 progress_frame, SLOT(setMaximumValue(int)));
189 connect(main_progress_frame, SIGNAL(valueChanged(int)),
190 progress_frame, SLOT(setValue(int)));
191 connect(main_progress_frame, SIGNAL(setHidden()),
192 progress_frame, SLOT(hide()));
194 connect(progress_frame, SIGNAL(stopLoading()),
195 main_progress_frame, SIGNAL(stopLoading()));
198 void ProgressFrame::captureFileClosing()
200 // Hide any paired ProgressFrames and disconnect from them.
202 disconnect(SIGNAL(showRequested(bool,bool,gboolean*)));
203 disconnect(SIGNAL(maximumValueChanged(int)));
204 disconnect(SIGNAL(valueChanged(int)));
206 connect(this, SIGNAL(showRequested(bool,bool,gboolean*)),
207 this, SLOT(show(bool,bool,gboolean*)));
210 void ProgressFrame::setValue(int value)
212 ui->progressBar->setValue(value);
213 emit valueChanged(value);
216 void ProgressFrame::timerEvent(QTimerEvent *event)
218 if (event->timerId() == show_timer_) {
219 killTimer(show_timer_);
222 this->setGraphicsEffect(effect_);
224 animation_->setDuration(750);
225 animation_->setStartValue(0.1);
226 animation_->setEndValue(1.0);
227 animation_->setEasingCurve(QEasingCurve::InOutQuad);
232 QFrame::timerEvent(event);
236 void ProgressFrame::hide()
241 #ifdef QWINTASKBARPROGRESS_H
242 if (taskbar_progress_) {
243 disconnect(this, SIGNAL(valueChanged(int)), taskbar_progress_, SLOT(setValue(int)));
244 taskbar_progress_->reset();
245 taskbar_progress_->hide();
250 void ProgressFrame::on_stopButton_clicked()
255 const int show_delay_ = 500; // ms
257 void ProgressFrame::show(bool animate, bool terminate_is_stop, gboolean *stop_flag)
259 terminate_is_stop_ = terminate_is_stop;
260 stop_flag_ = stop_flag;
263 ui->stopButton->show();
265 ui->stopButton->hide();
269 show_timer_ = startTimer(show_delay_);
274 #ifdef QWINTASKBARPROGRESS_H
275 // windowHandle() is picky about returning a non-NULL value so we check it
277 if (update_taskbar_ && !taskbar_progress_ && window()->windowHandle()) {
278 QWinTaskbarButton *taskbar_button = new QWinTaskbarButton(this);
279 if (taskbar_button) {
280 taskbar_button->setWindow(window()->windowHandle());
281 taskbar_progress_ = taskbar_button->progress();
284 if (taskbar_progress_) {
285 taskbar_progress_->show();
286 taskbar_progress_->reset();
287 connect(this, SIGNAL(valueChanged(int)), taskbar_progress_, SLOT(setValue(int)));
292 void ProgressFrame::setMaximumValue(int value)
294 ui->progressBar->setMaximum(value);
295 emit maximumValueChanged(value);
304 * indent-tabs-mode: nil
307 * ex: set shiftwidth=4 tabstop=8 expandtab:
308 * :indentSize=4:tabSize=8:noTabs=true: