Skip to content

Commit 2724762

Browse files
committed
QtCommon: introduce QtCoreUtils::runJobOnMainThread()
1 parent 83e3b34 commit 2724762

File tree

6 files changed

+84
-52
lines changed

6 files changed

+84
-52
lines changed

src/app/commands_file.cpp

+15-14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "../base/task_manager.h"
1111
#include "../gui/gui_application.h"
1212
#include "../qtcommon/filepath_conv.h"
13+
#include "../qtcommon/qtcore_utils.h"
1314
#include "../qtcommon/qstring_conv.h"
1415
#include "app_module.h"
1516
#include "recent_files.h"
@@ -20,7 +21,6 @@
2021
#include <QtCore/QtDebug>
2122
#include <QtCore/QElapsedTimer>
2223
#include <QtCore/QMimeData>
23-
#include <QtCore/QTimer>
2424
#include <QtGui/QDragEnterEvent>
2525
#include <QtGui/QDropEvent>
2626
#include <QtWidgets/QApplication>
@@ -203,7 +203,7 @@ void FileCommandTools::openDocumentsFromList(IAppContext* context, Span<const Fi
203203
.execute();
204204
if (okImport) {
205205
appModule->emitInfo(fmt::format(Command::textIdTr("Import time: {}ms"), chrono.elapsed()));
206-
QTimer::singleShot(0, context, [=]{
206+
QtCoreUtils::runJobOnMainThread([=]{
207207
appModule->prependRecentFile(fp, context->guiApp()->findGuiDocument(newDocId));
208208
});
209209
}
@@ -234,18 +234,19 @@ void FileCommandTools::importInDocument(
234234
chrono.start();
235235

236236
auto doc = appModule->application()->findDocumentByIdentifier(targetDocId);
237-
const bool okImport = appModule->ioSystem()->importInDocument()
238-
.targetDocument(doc)
239-
.withFilepaths(listFilePaths)
240-
.withParametersProvider(appModule)
241-
.withEntityPostProcess([=](TDF_Label labelEntity, TaskProgress* progress) {
242-
appModule->computeBRepMesh(labelEntity, progress);
243-
})
244-
.withEntityPostProcessRequiredIf(&IO::formatProvidesBRep)
245-
.withEntityPostProcessInfoProgress(20, Command::textIdTr("Mesh BRep shapes"))
246-
.withMessenger(appModule)
247-
.withTaskProgress(progress)
248-
.execute();
237+
const bool okImport =
238+
appModule->ioSystem()->importInDocument()
239+
.targetDocument(doc)
240+
.withFilepaths(listFilePaths)
241+
.withParametersProvider(appModule)
242+
.withEntityPostProcess([=](TDF_Label labelEntity, TaskProgress* progress) {
243+
appModule->computeBRepMesh(labelEntity, progress);
244+
})
245+
.withEntityPostProcessRequiredIf(&IO::formatProvidesBRep)
246+
.withEntityPostProcessInfoProgress(20, Command::textIdTr("Mesh BRep shapes"))
247+
.withMessenger(appModule)
248+
.withTaskProgress(progress)
249+
.execute();
249250
if (okImport)
250251
appModule->emitInfo(fmt::format(Command::textIdTr("Import time: {}ms"), chrono.elapsed()));
251252
});

src/app/main.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "../qtcommon/filepath_conv.h"
2929
#include "../qtcommon/log_message_handler.h"
3030
#include "../qtcommon/qstring_conv.h"
31+
#include "../qtcommon/qtcore_utils.h"
3132
#include "app_module.h"
3233
#include "commands_help.h"
3334
#include "document_tree_node_properties_providers.h"
@@ -46,7 +47,6 @@
4647
#include <QtCore/QCommandLineParser>
4748
#include <QtCore/QDir>
4849
#include <QtCore/QSettings>
49-
#include <QtCore/QTimer>
5050
#include <QtCore/QTranslator>
5151
#include <QtCore/QVersionNumber>
5252
#include <QtGui/QOffscreenSurface>
@@ -440,7 +440,9 @@ static int runApp(QCoreApplication* qtApp)
440440
mainWindow.setWindowTitle(QCoreApplication::applicationName());
441441
mainWindow.show();
442442
if (!args.listFilepathToOpen.empty()) {
443-
QTimer::singleShot(0, qtApp, [&]{ mainWindow.openDocumentsFromList(args.listFilepathToOpen); });
443+
QtCoreUtils::runJobOnMainThread([&]{
444+
mainWindow.openDocumentsFromList(args.listFilepathToOpen);
445+
});
444446
}
445447

446448
appModule->settings()->resetAll();

src/app/widget_main_control.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "../gui/gui_application.h"
1313
#include "../qtcommon/filepath_conv.h"
1414
#include "../qtcommon/qstring_conv.h"
15+
#include "../qtcommon/qtcore_utils.h"
1516

1617
#include "app_module.h"
1718
#include "commands_api.h"
@@ -30,7 +31,6 @@
3031
#include "widget_properties_editor.h"
3132

3233
#include <QtCore/QDir>
33-
#include <QtCore/QTimer>
3434
#include <QtWidgets/QMenu>
3535
#include <QtWidgets/QMessageBox>
3636
#include <cassert>
@@ -423,7 +423,7 @@ void WidgetMainControl::onGuiDocumentAdded(GuiDocument* guiDoc)
423423

424424
m_ui->stack_GuiDocuments->addWidget(widget);
425425
const int newDocIndex = m_guiApp->application()->documentCount() - 1;
426-
QTimer::singleShot(0, this, [=]{ this->setCurrentDocumentIndex(newDocIndex); });
426+
QtCoreUtils::runJobOnMainThread([=]{ this->setCurrentDocumentIndex(newDocIndex); });
427427
}
428428

429429
int WidgetMainControl::currentDocumentIndex() const

src/cli/main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "../qtcommon/filepath_conv.h"
3030
#include "../qtcommon/log_message_handler.h"
3131
#include "../qtcommon/qstring_conv.h"
32+
#include "../qtcommon/qtcore_utils.h"
3233
#include "cli_export.h"
3334
#include "console.h"
3435
#include <common/mayo_version.h>
@@ -38,7 +39,6 @@
3839
#include <QtCore/QDir>
3940
#include <QtCore/QLibraryInfo>
4041
#include <QtCore/QSettings>
41-
#include <QtCore/QTimer>
4242
#include <QtCore/QTranslator>
4343

4444
#include <OpenGl_GraphicDriver.hxx>
@@ -470,7 +470,7 @@ static int runApp(QCoreApplication* qtApp)
470470
}
471471
}
472472
else {
473-
QTimer::singleShot(0, qtApp, [=]{
473+
QtCoreUtils::runJobOnMainThread([=]{
474474
CliExportArgs cliArgs;
475475
cliArgs.progressReport = args.progressReport;
476476
cliArgs.filesToOpen = args.listFilepathToOpen;

src/qtcommon/qtcore_utils.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/****************************************************************************
2+
** Copyright (c) 2024, Fougue Ltd. <https://www.fougue.pro>
3+
** All rights reserved.
4+
** See license at https://github.com/fougue/mayo/blob/master/LICENSE.txt
5+
****************************************************************************/
6+
7+
#include "qtcore_utils.h"
8+
9+
#include <QtCore/QCoreApplication>
10+
#include <QtCore/QTimer>
11+
12+
namespace Mayo::QtCoreUtils {
13+
14+
QByteArray QByteArray_frowRawData(const QByteArray& bytes)
15+
{
16+
return QByteArray::fromRawData(bytes.data(), bytes.size());
17+
}
18+
19+
QByteArray QByteArray_frowRawData(std::string_view str)
20+
{
21+
return QByteArray::fromRawData(str.data(), int(str.size()));
22+
}
23+
24+
Qt::CheckState toQtCheckState(Mayo::CheckState state)
25+
{
26+
switch (state) {
27+
case CheckState::Off: return Qt::Unchecked;
28+
case CheckState::Partially: return Qt::PartiallyChecked;
29+
case CheckState::On: return Qt::Checked;
30+
}
31+
32+
return Qt::Unchecked;
33+
}
34+
35+
// Converts Qt::CheckState -> Mayo::CheckState
36+
Mayo::CheckState toCheckState(Qt::CheckState state)
37+
{
38+
switch (state) {
39+
case Qt::Unchecked: return CheckState::Off;
40+
case Qt::PartiallyChecked: return CheckState::Partially;
41+
case Qt::Checked: return CheckState::On;
42+
}
43+
44+
return CheckState::Off;
45+
}
46+
47+
void runJobOnMainThread(const std::function<void()>& fn)
48+
{
49+
QTimer::singleShot(0, QCoreApplication::instance(), fn);
50+
}
51+
52+
} // namespace Mayo::QtCoreUtils

src/qtcommon/qtcore_utils.h

+9-32
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,17 @@
88

99
#include "../base/global.h"
1010
#include <QtCore/QByteArray>
11+
#include <functional>
1112
#include <string_view>
1213

13-
namespace Mayo {
14-
1514
// Provides a collection of tools for the QtCore module
16-
namespace QtCoreUtils {
15+
namespace Mayo::QtCoreUtils {
1716

1817
// Convenience function over QByteArray::fromRawData() taking a QByteArray object
19-
inline QByteArray QByteArray_frowRawData(const QByteArray& bytes)
20-
{
21-
return QByteArray::fromRawData(bytes.data(), bytes.size());
22-
}
18+
QByteArray QByteArray_frowRawData(const QByteArray& bytes);
2319

2420
// Convenience function over QByteArray::fromRawData() taking a std::string_view object
25-
inline QByteArray QByteArray_frowRawData(std::string_view str)
26-
{
27-
return QByteArray::fromRawData(str.data(), int(str.size()));
28-
}
21+
QByteArray QByteArray_frowRawData(std::string_view str);
2922

3023
// Convenience function over QByteArray::fromRawData() taking a C array of characters
3124
template<size_t N>
@@ -35,28 +28,12 @@ QByteArray QByteArray_frowRawData(const char (&str)[N])
3528
}
3629

3730
// Converts Mayo::CheckState -> Qt::CheckState
38-
inline Qt::CheckState toQtCheckState(Mayo::CheckState state)
39-
{
40-
switch (state) {
41-
case CheckState::Off: return Qt::Unchecked;
42-
case CheckState::Partially: return Qt::PartiallyChecked;
43-
case CheckState::On: return Qt::Checked;
44-
}
45-
46-
return Qt::Unchecked;
47-
}
31+
Qt::CheckState toQtCheckState(Mayo::CheckState state);
4832

4933
// Converts Qt::CheckState -> Mayo::CheckState
50-
inline Mayo::CheckState toCheckState(Qt::CheckState state)
51-
{
52-
switch (state) {
53-
case Qt::Unchecked: return CheckState::Off;
54-
case Qt::PartiallyChecked: return CheckState::Partially;
55-
case Qt::Checked: return CheckState::On;
56-
}
34+
Mayo::CheckState toCheckState(Qt::CheckState state);
5735

58-
return CheckState::Off;
59-
}
36+
// Enqueues function 'fn' to be executed on main thread
37+
void runJobOnMainThread(const std::function<void()>& fn);
6038

61-
} // namespace QtCoreUtils
62-
} // namespace Mayo
39+
} // namespace Mayo::QtCoreUtils

0 commit comments

Comments
 (0)