AsyncProgressDialog
Classes | Public Slots | Public Member Functions | List of all members
APD::AsyncProgressDialog Class Reference

The AsyncProgressDialog dialog is capable of running task threads and displaying a progress widget for each thread. More...

Inheritance diagram for APD::AsyncProgressDialog:

Classes

class  Impl
 

Public Slots

void reject () override
 

Public Member Functions

 AsyncProgressDialog (QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::WindowFlags())
 
void addTask (TaskThread *thread, ProgressWidget *widget)
 
template<typename F >
auto addTask (F func, ProgressWidget *widget=ProgressWidgetFactory::createProgressBar()) -> FunctionThread< decltype(func(static_cast< TaskThread * >(nullptr)))> *
 
void setAutoClose (bool close)
 
bool autoClose () const
 
void setOverallProgress (bool enabled)
 
bool hasOverallProgress () const
 
void setLabelText (const QString &labelText)
 
QString labelText () const
 
int threadCount () const
 
TaskThreadthreadAt (int index) const
 
int widgetCount () const
 
ProgressWidgetwidgetAt (int index) const
 
void setAutoHideWidget (int index, bool autoHide)
 
bool autoHideWidget (int index) const
 

Detailed Description

The AsyncProgressDialog dialog is capable of running task threads and displaying a progress widget for each thread.

Task threads and progress widgets are added to the dialog using addTask(). Newly added threads are started when added to the dialog and paired with the progress widget. The widget then shows progress as reported by the thread. If more than one tasks are added, a progress bar indicating overall progress can be shown (see setOverallProgress())

There are two addTask() methods, which accept a function instead of a task thread object. They can be conveniently combined with lambda expressions, which avoids construction of a thread object on the caller side. The return value of the lambda expression can be safely accessed after the thread finishes.

auto thread = adlg.addTask([](auto thread) {
thread->setRange(0, 10);
// The thread is going to calculate a summary and then return it
int sum = 0;
for (int i = 0; i < 10; i++)
{
thread->setValue(i);
if (thread->isCanceled())
break;
sum += i; // Calculate sum
QThread::msleep(100);
}
thread->setValue(10);
sum += 10; // Calculate sum
return sum; // Return sum variable, it can be later accesed using thread->result() method
});
// Task result is not ready yet.
// Calling FunctionalThread::result() is thread safe.
assert(thread->result() == std::nullopt);
adlg.exec();
// Now the task has finished, we can safely access its result
QMessageBox::information(this, QString("Resut"), QString("Result is %1").arg(*thread->result()));

After a thread finishes, the associated widget can be kept visible or hidden depending on autoHideWidget. The thread object is deleted in destructor if the parent of the thread object is this dialog. If the thread is still running when the destructor is called, the thread object is scheduled to be deleted as soon as the thread finishes. Life time of thread object with different parent is not managed by this dialog.

See also
TaskThread, ProgressWidget

Constructor & Destructor Documentation

◆ AsyncProgressDialog()

APD::AsyncProgressDialog::AsyncProgressDialog ( QWidget *  parent = nullptr,
Qt::WindowFlags  flags = Qt::WindowFlags() 
)
explicit

Constructs a progress dialog.

The parent and flags arguments are passed to the QDialog constructor.

Member Function Documentation

◆ addTask() [1/2]

void APD::AsyncProgressDialog::addTask ( TaskThread thread,
ProgressWidget widget 
)

Add a task thread object and the associated progress widget. The widget displays the progress as reported by the thread.

The ownership of widget is transferred to the dialog.

The ownership ofthread is not transferred. If the parent of the thread object is this dialog, the thread object is either deleted in destructor if finished, or scheduled to be deleted as soon as the thread finishes. Life time of thread object with different parent is not managed by this dialog.

◆ addTask() [2/2]

template<typename F >
auto APD::AsyncProgressDialog::addTask ( func,
ProgressWidget widget = ProgressWidgetFactory::createProgressBar() 
) -> FunctionThread<decltype(func(static_cast<TaskThread*>(nullptr)))>*
inline

Add task defined by a function func and the associated progress widget. The method creates a FunctionThread object internally. The widget displays the progress as reported by the function thread. The thread object is returned by this method.

The ownership of widget is transferred to the dialog.

The ownership of the function thread object is set to this dialog and is deleted in the destructor of the dialog.

◆ autoClose()

bool APD::AsyncProgressDialog::autoClose ( ) const

Return a flag whether the dialog gets hidden after all threads has finished.

The default is true.

See also
setAutoClose()

◆ autoHideWidget()

bool APD::AsyncProgressDialog::autoHideWidget ( int  index) const

Return the flag indicating whether the progress widget at given index should be automatically hidden after the associated thread finishes. The index must be in the range [0, widgetCount())

See also
widgetAt(), widgetCount()

◆ hasOverallProgress()

bool APD::AsyncProgressDialog::hasOverallProgress ( ) const

Return a flag whether the dialog shows total progress of all tasks.

The overal progress can be only shown if all running tasks report progress, i.e. their range (minimum and maximum progress values) is not empty.

See also
setOverallProgress()

◆ labelText()

QString APD::AsyncProgressDialog::labelText ( ) const

Returns the label's text. Label is displayed as a top widget in the dialog. The label is hidden when empty.

The default text is an empty string.

See also
setLabelText()

◆ reject

void APD::AsyncProgressDialog::reject ( )
overrideslot

Reimplemented from QDialog::reject()

◆ setAutoClose()

void APD::AsyncProgressDialog::setAutoClose ( bool  close)

Set a flag whether the dialog gets hidden after all threads has finished.

See also
autoClose()

◆ setAutoHideWidget()

void APD::AsyncProgressDialog::setAutoHideWidget ( int  index,
bool  autoHide 
)

Sets the flag autoHide indicating whether the progress widget at given index should be automatically hidden after the associated thread finishes.

The index must be in the range [0, widgetCount())

See also
widgetAt(), widgetCount()

◆ setLabelText()

void APD::AsyncProgressDialog::setLabelText ( const QString &  labelText)

Set the label's text.

See also
labelText()

◆ setOverallProgress()

void APD::AsyncProgressDialog::setOverallProgress ( bool  enabled)

Set a flag whether the dialog shows total progress of all tasks.

See also
hasOverallProgress()

◆ threadAt()

TaskThread * APD::AsyncProgressDialog::threadAt ( int  index) const

Returns the thread object at given index. The index must be in the range [0, threadCount())

See also
threadCount()

◆ threadCount()

int APD::AsyncProgressDialog::threadCount ( ) const

Returns number of threads in this dialog.

Note
Number of threads is always equal to number of widgets
See also
threadAt(), widgetCount()

◆ widgetAt()

ProgressWidget * APD::AsyncProgressDialog::widgetAt ( int  index) const

Returns the progress widget at given index. The index must be in the range [0, widgetCount())

See also
widgetCount()

◆ widgetCount()

int APD::AsyncProgressDialog::widgetCount ( ) const

Returns number of progress widgets in this dialog.

Note
Number of threads is always equal to number of widgets
See also
widgetAt(), threadCount()

The documentation for this class was generated from the following files: