Add QtConcurrence pour threaded processes.

Parallel sort of time zones (it can take 2s to process).
Print logs if not connected to SDK.
Fix combobox wth empty models.
This commit is contained in:
Julien Wadel 2024-11-05 16:37:11 +01:00
parent 23b961d681
commit c5777e2dd1
7 changed files with 29 additions and 15 deletions

View file

@ -22,7 +22,7 @@ set(APP_TARGETS ${LinphoneCxx_TARGET}
${LibLinphone_TARGET})#MediastreamerUtils
set(QT_DEFAULT_MAJOR_VERSION 6)
set(QT_PACKAGES Core Quick Qml Widgets Svg Multimedia Test NetworkAuth)# Search Core at first for initialize Qt scripts for next find_packages.
set(QT_PACKAGES Core Quick Qml Widgets Svg Multimedia Test NetworkAuth Concurrent)# Search Core at first for initialize Qt scripts for next find_packages.
if (UNIX AND NOT APPLE)
list(APPEND QT_PACKAGES DBus)
endif()

View file

@ -88,7 +88,7 @@ QString QtLogger::formatLog(QString contextFile, int contextLine, QString msg) {
void QtLogger::onQtLog(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
QString out;
QString message = QtLogger::formatLog(context.file, context.line, msg);
if (gLogger.mVerboseEnabled) {
if (gLogger.mVerboseEnabled || !gLogger.isSignalConnected(QMetaMethod::fromSignal(&QtLogger::qtLogReceived))) {
gLogger.printLog(&out, Constants::AppDomain, LinphoneEnums::toLinphone(type),
Utils::appStringToCoreString(message));
}

View file

@ -21,7 +21,10 @@
#include "TimeZoneList.hpp"
#include "core/App.hpp"
#include <QFuture>
#include <QTimeZone>
#include <QtConcurrent>
#include <set>
// =============================================================================
@ -37,7 +40,8 @@ QSharedPointer<TimeZoneList> TimeZoneList::create() {
TimeZoneList::TimeZoneList(QObject *parent) : ListProxy(parent) {
App::getInstance()->mEngine->setObjectOwnership(this, QQmlEngine::CppOwnership);
initTimeZones();
// There is too much items (more than 400) to be sort in main thread. Dispatch the process.
QFuture<void> future = QtConcurrent::run([this]() { initTimeZones(); });
}
TimeZoneList::~TimeZoneList() {
@ -47,6 +51,8 @@ TimeZoneList::~TimeZoneList() {
void TimeZoneList::initTimeZones() {
QList<QSharedPointer<QObject>> models;
QElapsedTimer benchmark;
benchmark.start();
for (auto id : QTimeZone::availableTimeZoneIds()) {
auto model = QSharedPointer<TimeZoneModel>::create(QTimeZone(id));
if (std::find_if(mList.begin(), mList.end(), [id](const QSharedPointer<QObject> &a) {
@ -57,7 +63,16 @@ void TimeZoneList::initTimeZones() {
}
}
}
resetData(models);
std::sort(models.begin(), models.end(), [](QSharedPointer<QObject> a, QSharedPointer<QObject> b) {
auto tA = a.objectCast<TimeZoneModel>();
auto tB = b.objectCast<TimeZoneModel>();
auto timeA = tA->getStandardTimeOffset() / 3600;
auto timeB = tB->getStandardTimeOffset() / 3600;
return timeA < timeB || (timeA == timeB && tA->getCountryName() < tB->getCountryName());
});
lDebug() << log().arg("Sorting %1 TZ : %2ms").arg(models.size()).arg(benchmark.elapsed());
// Reset models inside main thread.
QMetaObject::invokeMethod(this, [this, models]() { resetData(models); });
}
QHash<int, QByteArray> TimeZoneList::roleNames() const {
@ -73,11 +88,11 @@ QVariant TimeZoneList::data(const QModelIndex &index, int role) const {
if (!index.isValid() || row < 0 || row >= mList.count()) return QVariant();
auto timeZoneModel = getAt<TimeZoneModel>(row);
if (!timeZoneModel) return QVariant();
if (role == Qt::DisplayRole + 1) {
return QVariant::fromValue(timeZoneModel.get());
} else {
int offset = timeZoneModel->getStandardTimeOffset() / 3600;
int absOffset = std::abs(offset);
if (role == Qt::DisplayRole + 1) {
return QVariant::fromValue(new TimeZoneModel(timeZoneModel->getTimeZone()));
} else {
return QStringLiteral("(GMT%1%2%3:00) %4 %5")
.arg(offset >= 0 ? "+" : "-")
.arg(absOffset < 10 ? "0" : "")

View file

@ -26,7 +26,8 @@
TimeZoneProxy::TimeZoneProxy(QObject *parent) : LimitProxy(parent) {
mList = TimeZoneList::create();
setSourceModels(new SortFilterList(mList.get(), Qt::AscendingOrder));
auto a = new SortFilterList(mList.get()); // Avoid using sort because it is too slow
setSourceModels(a);
}
// -----------------------------------------------------------------------------

View file

@ -37,12 +37,8 @@ void cleanStream() {
}
#endif
}
void fallbackLog(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
QString message = QtLogger::formatLog(context.file, context.line, msg);
std::cout << message.toStdString() << std::endl;
}
int main(int argc, char *argv[]) {
qInstallMessageHandler(fallbackLog); // Use for messages prior QCoreApplication generation.
/*
#if defined _WIN32
// log in console only if launched from console

View file

@ -23,6 +23,7 @@ Control.ComboBox {
onCurrentIndexChanged: {
var item = model[currentIndex]
if (!item) item = model.getAt(currentIndex)
if (!item) return
selectedItemText.text = item.text
? item.text
: item
@ -142,7 +143,7 @@ Control.ComboBox {
clip: true
implicitHeight: contentHeight
height: contentHeight
model: mainItem.model
model: visible? mainItem.model : []
currentIndex: mainItem.highlightedIndex >= 0 ? mainItem.highlightedIndex : 0
highlightFollowsCurrentItem: true
highlightMoveDuration: -1

View file

@ -204,7 +204,7 @@ FocusScope {
constantImageSource: AppIcons.globe
weight: 700 * DefaultStyle.dp
leftMargin: 0
currentIndex: mainItem.conferenceInfoGui ? model.getIndex(mainItem.conferenceInfoGui.core.timeZoneModel) : -1
currentIndex: mainItem.conferenceInfoGui && model.count > 0 ? model.getIndex(mainItem.conferenceInfoGui.core.timeZoneModel) : -1
background: Rectangle {
visible: parent.hovered || parent.down
anchors.fill: parent
@ -212,6 +212,7 @@ FocusScope {
}
model: TimeZoneProxy{
}
visible: model.count > 0
onCurrentIndexChanged: {
var modelIndex = timeZoneCbox.model.index(currentIndex, 0)
mainItem.conferenceInfoGui.core.timeZoneModel = timeZoneCbox.model.data(modelIndex, Qt.DisplayRole + 1)