2019-01-12 19:25:43 +00:00
|
|
|
|
|
|
|
#ifndef REFRESHDEFERRER_H
|
|
|
|
#define REFRESHDEFERRER_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
|
|
|
|
class CutterDockWidget;
|
|
|
|
class RefreshDeferrer;
|
|
|
|
|
|
|
|
using RefreshDeferrerParams = void *;
|
|
|
|
using RefreshDeferrerParamsResult = void *;
|
|
|
|
|
2019-01-13 14:26:55 +00:00
|
|
|
/*!
|
|
|
|
* \brief Abstract class for accumulating params in RefreshDeferrer
|
|
|
|
*/
|
2019-01-12 19:25:43 +00:00
|
|
|
class RefreshDeferrerAccumulator
|
|
|
|
{
|
|
|
|
friend class RefreshDeferrer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~RefreshDeferrerAccumulator() = default;
|
|
|
|
|
|
|
|
protected:
|
2019-01-13 14:26:55 +00:00
|
|
|
/*!
|
|
|
|
* \brief Add a new param to the accumulator
|
|
|
|
*/
|
2019-01-12 19:25:43 +00:00
|
|
|
virtual void accumulate(RefreshDeferrerParams params) =0;
|
2019-01-13 14:26:55 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Ignore the incoming params. Useful for freeing if necessary.
|
|
|
|
*/
|
2019-01-12 19:25:43 +00:00
|
|
|
virtual void ignoreParams(RefreshDeferrerParams params) =0;
|
2019-01-13 14:26:55 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Clear the current accumulator
|
|
|
|
*/
|
2019-01-12 19:25:43 +00:00
|
|
|
virtual void clear() =0;
|
2019-01-13 14:26:55 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Return the final result of the accumulation
|
|
|
|
*/
|
2019-01-12 19:25:43 +00:00
|
|
|
virtual RefreshDeferrerParamsResult result() =0;
|
|
|
|
};
|
|
|
|
|
2019-01-13 14:26:55 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Accumulator which simply replaces the current value by an incoming new one
|
|
|
|
* \tparam T The type of the param to store
|
|
|
|
*
|
|
|
|
* This accumulator takes the ownership of all params passed to it and deletes them automatically if not needed anymore!
|
|
|
|
*/
|
2019-01-12 19:25:43 +00:00
|
|
|
template<class T>
|
|
|
|
class ReplacingRefreshDeferrerAccumulator: public RefreshDeferrerAccumulator
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
T *value = nullptr;
|
2019-01-13 13:59:08 +00:00
|
|
|
bool replaceIfNull;
|
2019-01-12 19:25:43 +00:00
|
|
|
|
|
|
|
public:
|
2019-01-13 14:26:55 +00:00
|
|
|
/*!
|
|
|
|
* \param Determines whether, if nullptr is passed, the current value should be replaced or kept.
|
|
|
|
*/
|
2019-01-13 13:59:08 +00:00
|
|
|
explicit ReplacingRefreshDeferrerAccumulator(bool replaceIfNull = true)
|
|
|
|
: replaceIfNull(replaceIfNull) {}
|
|
|
|
|
2019-01-12 19:25:43 +00:00
|
|
|
~ReplacingRefreshDeferrerAccumulator() override
|
|
|
|
{
|
|
|
|
delete value;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void accumulate(RefreshDeferrerParams params) override
|
|
|
|
{
|
2019-01-13 13:59:08 +00:00
|
|
|
if (!replaceIfNull && !params) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-12 19:25:43 +00:00
|
|
|
delete value;
|
|
|
|
value = static_cast<T *>(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ignoreParams(RefreshDeferrerParams params) override
|
|
|
|
{
|
|
|
|
delete static_cast<T *>(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() override
|
|
|
|
{
|
|
|
|
delete value;
|
|
|
|
value = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual RefreshDeferrerParamsResult result() override
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-13 14:26:55 +00:00
|
|
|
/*!
|
|
|
|
* \brief Helper class for deferred refreshing in Widgets
|
|
|
|
*
|
|
|
|
* This class can handle the logic necessary to defer the refreshing of widgets when they are not visible.
|
|
|
|
* It contains an optional RefreshDeferrerAccumulator, which can be used to accumulate incoming events while
|
|
|
|
* refreshing is deferred.
|
|
|
|
*
|
|
|
|
* Example (don't write it like this in practice, use the convenience methods in CutterDockWidget):
|
|
|
|
* ```
|
|
|
|
* // in the constructor of a widget
|
|
|
|
* this->refreshDeferrer = new RefreshDeferrer(new ReplacingRefreshDeferrerAccumulator(false), this);
|
|
|
|
* this->refreshDeferrer->registerFor(this);
|
|
|
|
* connect(this->refreshDeferrer, &RefreshDeferrer::refreshNow, this, [this](MyParam *param) {
|
|
|
|
* // We attempted a refresh some time before, but it got deferred.
|
|
|
|
* // Now the RefreshDeferrer tells us to do the refresh and gives us the accumulated param.
|
|
|
|
* this->doRefresh(*param);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // ...
|
|
|
|
*
|
|
|
|
* void MyWidget::doRefresh(MyParam param)
|
|
|
|
* {
|
|
|
|
* if (!this->refreshDeferrer->attemptRefresh(new MyParam(param))) {
|
|
|
|
* // We shouldn't refresh right now.
|
|
|
|
* // The RefreshDeferrer takes over the param we passed it in attemptRefresh()
|
|
|
|
* // and gives it to the ReplacingRefreshDeferrerAccumulator.
|
|
|
|
* return;
|
|
|
|
* }
|
|
|
|
* // do the actual refresh depending on param
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
*/
|
2019-01-12 19:25:43 +00:00
|
|
|
class RefreshDeferrer : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private:
|
|
|
|
CutterDockWidget *dockWidget = nullptr;
|
|
|
|
RefreshDeferrerAccumulator *acc;
|
|
|
|
bool dirty = false;
|
|
|
|
|
|
|
|
public:
|
2019-01-13 14:26:55 +00:00
|
|
|
/*!
|
|
|
|
* \param acc The accumulator (can be nullptr). The RefreshDeferrer takes the ownership!
|
|
|
|
*/
|
|
|
|
explicit RefreshDeferrer(RefreshDeferrerAccumulator *acc, QObject *parent = nullptr);
|
|
|
|
~RefreshDeferrer() override;
|
2019-01-12 19:25:43 +00:00
|
|
|
|
|
|
|
bool attemptRefresh(RefreshDeferrerParams params);
|
|
|
|
void registerFor(CutterDockWidget *dockWidget);
|
|
|
|
|
|
|
|
signals:
|
2019-01-13 11:40:54 +00:00
|
|
|
void refreshNow(const RefreshDeferrerParamsResult paramsResult);
|
2019-01-12 19:25:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif //REFRESHDEFERRER_H
|