2018-04-28 13:48:49 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QStandardItemModel>
|
2018-04-29 23:25:49 +02:00
|
|
|
#include <QTimer>
|
|
|
|
#include <boost/noncopyable.hpp>
|
2018-04-28 13:48:49 +02:00
|
|
|
#include <pajlada/signals/signal.hpp>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "util/assertinguithread.hpp"
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace util {
|
|
|
|
|
|
|
|
template <typename TVectorItem>
|
2018-04-29 23:25:49 +02:00
|
|
|
class ReadOnlySignalVector : boost::noncopyable
|
2018-04-28 13:48:49 +02:00
|
|
|
{
|
|
|
|
public:
|
2018-04-29 23:25:49 +02:00
|
|
|
ReadOnlySignalVector()
|
|
|
|
{
|
|
|
|
QObject::connect(&this->itemsChangedTimer, &QTimer::timeout,
|
|
|
|
[this] { this->delayedItemsChanged.invoke(); });
|
|
|
|
}
|
2018-04-28 13:48:49 +02:00
|
|
|
virtual ~ReadOnlySignalVector() = default;
|
|
|
|
|
2018-04-29 23:25:49 +02:00
|
|
|
struct ItemArgs {
|
2018-04-28 13:48:49 +02:00
|
|
|
const TVectorItem &item;
|
|
|
|
int index;
|
2018-04-29 23:25:49 +02:00
|
|
|
void *caller;
|
2018-04-28 13:48:49 +02:00
|
|
|
};
|
|
|
|
|
2018-04-29 23:25:49 +02:00
|
|
|
pajlada::Signals::Signal<ItemArgs> itemInserted;
|
|
|
|
pajlada::Signals::Signal<ItemArgs> itemRemoved;
|
2018-04-28 13:48:49 +02:00
|
|
|
pajlada::Signals::NoArgSignal delayedItemsChanged;
|
|
|
|
|
|
|
|
const std::vector<TVectorItem> &getVector() const
|
|
|
|
{
|
|
|
|
util::assertInGuiThread();
|
|
|
|
|
|
|
|
return this->vector;
|
|
|
|
}
|
|
|
|
|
2018-04-29 23:25:49 +02:00
|
|
|
void invokeDelayedItemsChanged()
|
|
|
|
{
|
|
|
|
util::assertInGuiThread();
|
|
|
|
|
|
|
|
if (!this->itemsChangedTimer.isActive()) {
|
|
|
|
itemsChangedTimer.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-28 13:48:49 +02:00
|
|
|
protected:
|
|
|
|
std::vector<TVectorItem> vector;
|
2018-04-29 23:25:49 +02:00
|
|
|
QTimer itemsChangedTimer;
|
2018-04-28 13:48:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename TVectorItem>
|
|
|
|
class BaseSignalVector : public ReadOnlySignalVector<TVectorItem>
|
|
|
|
{
|
|
|
|
public:
|
2018-04-29 23:25:49 +02:00
|
|
|
virtual void appendItem(const TVectorItem &item, void *caller = 0) = 0;
|
|
|
|
|
|
|
|
void removeItem(int index, void *caller = 0)
|
2018-04-28 13:48:49 +02:00
|
|
|
{
|
|
|
|
util::assertInGuiThread();
|
|
|
|
assert(index >= 0 && index < this->vector.size());
|
|
|
|
|
2018-04-29 23:25:49 +02:00
|
|
|
TVectorItem item = this->vector[index];
|
2018-04-28 13:48:49 +02:00
|
|
|
this->vector.erase(this->vector.begin() + index);
|
2018-04-30 00:41:58 +02:00
|
|
|
ItemArgs args{item, index, caller};
|
2018-04-29 23:25:49 +02:00
|
|
|
this->itemRemoved.invoke(args);
|
2018-04-28 13:48:49 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename TVectorItem>
|
2018-04-29 23:25:49 +02:00
|
|
|
class UnsortedSignalVector : public BaseSignalVector<TVectorItem>
|
2018-04-28 13:48:49 +02:00
|
|
|
{
|
|
|
|
public:
|
2018-04-29 23:25:49 +02:00
|
|
|
void insertItem(const TVectorItem &item, int index, void *caller = 0)
|
2018-04-28 13:48:49 +02:00
|
|
|
{
|
|
|
|
util::assertInGuiThread();
|
|
|
|
assert(index >= 0 && index <= this->vector.size());
|
|
|
|
|
|
|
|
this->vector.insert(this->vector.begin() + index, item);
|
|
|
|
|
2018-04-29 23:25:49 +02:00
|
|
|
ItemArgs args{item, index, caller};
|
2018-04-28 13:48:49 +02:00
|
|
|
this->itemInserted.invoke(args);
|
|
|
|
}
|
|
|
|
|
2018-04-29 23:25:49 +02:00
|
|
|
virtual void appendItem(const TVectorItem &item, void *caller = 0) override
|
2018-04-28 13:48:49 +02:00
|
|
|
{
|
2018-04-29 23:25:49 +02:00
|
|
|
this->insertItem(item, this->vector.size(), caller);
|
2018-04-28 13:48:49 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename TVectorItem>
|
|
|
|
class SortedSignalVector : public BaseSignalVector<TVectorItem>
|
|
|
|
{
|
|
|
|
public:
|
2018-04-29 23:25:49 +02:00
|
|
|
virtual void appendItem(const TVectorItem &item, void *caller = 0) override
|
2018-04-28 13:48:49 +02:00
|
|
|
{
|
|
|
|
util::assertInGuiThread();
|
|
|
|
|
|
|
|
int index = this->vector.insert(
|
|
|
|
std::lower_bound(this->vector.begin(), this->vector.end(), item), item) -
|
|
|
|
this->vector.begin();
|
2018-04-29 23:25:49 +02:00
|
|
|
ItemArgs args{item, index, caller};
|
2018-04-28 13:48:49 +02:00
|
|
|
this->itemInserted.invoke(args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace util
|
|
|
|
} // namespace chatterino
|