51 lines
1.4 KiB
C++
51 lines
1.4 KiB
C++
#include "MeasurementController.h"
|
|
|
|
#include "backend/ChunkMeasurer.h"
|
|
|
|
#include <QtConcurrent>
|
|
|
|
MeasurementController::MeasurementController(QObject *parent)
|
|
: QObject(parent)
|
|
{
|
|
}
|
|
|
|
MeasurementController::~MeasurementController()
|
|
{
|
|
if (m_future.isRunning()) {
|
|
m_future.cancel();
|
|
m_future.waitForFinished();
|
|
}
|
|
}
|
|
|
|
void MeasurementController::setImageLoader(std::unique_ptr<IImageLoader> loader)
|
|
{
|
|
m_imageLoader = std::move(loader);
|
|
}
|
|
|
|
bool MeasurementController::measuring() const
|
|
{
|
|
return m_measuring;
|
|
}
|
|
|
|
void MeasurementController::start(const QStringList &htmls, int contentWidth,
|
|
int fontSize, qreal lineSpacing, bool isNew)
|
|
{
|
|
if (m_future.isRunning())
|
|
m_future.cancel();
|
|
|
|
m_measuring = true;
|
|
emit measuringChanged();
|
|
|
|
std::unique_ptr<IImageLoader> loaderClone = m_imageLoader ? m_imageLoader->clone() : nullptr;
|
|
auto measurer = std::make_shared<ChunkMeasurer>(std::move(loaderClone), fontSize, lineSpacing);
|
|
|
|
m_future = QtConcurrent::run([this, htmls, measurer, contentWidth, isNew]() {
|
|
QVector<int> heights = measurer->measure(htmls, contentWidth);
|
|
QMetaObject::invokeMethod(this, [this, heights, isNew] {
|
|
m_measuring = false;
|
|
emit measuringChanged();
|
|
emit heightsReady(heights, isNew);
|
|
}, Qt::QueuedConnection);
|
|
});
|
|
}
|