Skip to content

Commit 60c12ef

Browse files
authored
Merge pull request #32 from scratchcpp/menu_bar
Implement menu bar
2 parents b7262bb + 45fbc2c commit 60c12ef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1511
-82
lines changed

.github/workflows/utests.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Unit tests
2+
3+
on:
4+
push:
5+
branches: '*'
6+
pull_request:
7+
branches: [ "master" ]
8+
9+
env:
10+
BUILD_TYPE: Debug
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v3
18+
with:
19+
submodules: 'recursive'
20+
21+
- name: Install dependencies
22+
run: |
23+
sudo apt-get update
24+
sudo apt-get install -y nlohmann-json3-dev libutfcpp-dev xvfb libxcb-cursor0
25+
shell: bash
26+
- name: Install Qt
27+
uses: jurplel/install-qt-action@v3
28+
with:
29+
version: '6.6.*'
30+
- name: Configure CMake
31+
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DSCRATCHCPP_PLAYER_BUILD_UNIT_TESTS=ON
32+
33+
- name: Build
34+
run: xvfb-run cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -j$(nproc --all)
35+
36+
- name: Run unit tests
37+
run: xvfb-run ctest --test-dir build -V

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "scratchcpp-render"]
22
path = scratchcpp-render
33
url = https://github.com/scratchcpp/scratchcpp-render
4+
[submodule "thirdparty/googletest"]
5+
path = thirdparty/googletest
6+
url = https://github.com/google/googletest/

CMakeLists.txt

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,19 @@ project(scratchcpp-player VERSION 0.1 LANGUAGES CXX)
55
set(CMAKE_AUTOMOC ON)
66
set(CMAKE_CXX_STANDARD_REQUIRED ON)
77

8-
find_package(Qt6 6.6 COMPONENTS Quick QuickControls2 REQUIRED)
9-
10-
add_subdirectory(src)
11-
add_subdirectory(res)
8+
option(SCRATCHCPP_PLAYER_BUILD_UNIT_TESTS "Build unit tests" ON)
129

13-
set_target_properties(appscratchcpp-player PROPERTIES
14-
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
15-
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
16-
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
17-
MACOSX_BUNDLE TRUE
18-
WIN32_EXECUTABLE TRUE
19-
)
10+
find_package(Qt6 6.6 COMPONENTS Quick QuickControls2 REQUIRED)
2011

21-
target_compile_definitions(appscratchcpp-player
22-
PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
23-
target_link_libraries(appscratchcpp-player
24-
PRIVATE Qt6::Quick Qt6::QuickControls2)
12+
if (SCRATCHCPP_PLAYER_BUILD_UNIT_TESTS)
13+
set(GTEST_DIR thirdparty/googletest)
14+
add_subdirectory(${PROJECT_SOURCE_DIR}/${GTEST_DIR} ${CMAKE_CURRENT_BINARY_DIR}/${GTEST_DIR})
15+
enable_testing()
16+
find_package(Qt6 6.6 COMPONENTS Test REQUIRED)
17+
endif()
2518

2619
set(SCRATCHCPPRENDER_BUILD_UNIT_TESTS OFF)
2720
add_subdirectory(scratchcpp-render)
28-
target_link_libraries(appscratchcpp-player PRIVATE scratchcpp-render scratchcpp-renderplugin)
21+
22+
add_subdirectory(src)
23+
add_subdirectory(res)

build/module.cmake

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
qt_add_library(${MODULE} STATIC)
2+
3+
set_target_properties(${MODULE} PROPERTIES AUTOMOC ON)
4+
5+
qt_add_qml_module(${MODULE}
6+
URI ScratchCPP.${MODULE_URI}
7+
VERSION 1.0
8+
OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ScratchCPP/${MODULE_URI}
9+
QML_FILES
10+
${MODULE_QML_FILES}
11+
RESOURCES
12+
${MODULE_RESOURCES}
13+
SOURCES
14+
${MODULE_SRC}
15+
)
16+
17+
set(QML_IMPORT_PATH "${QML_IMPORT_PATH};${CMAKE_CURRENT_LIST_DIR}"
18+
CACHE STRING "Qt Creator extra QML import paths"
19+
FORCE
20+
)
21+
22+
list(APPEND QML_IMPORT_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
23+
list(REMOVE_DUPLICATES QML_IMPORT_PATH)
24+
set(QML_IMPORT_PATH ${QML_IMPORT_PATH} CACHE STRING "" FORCE)
25+
26+
target_link_libraries(${APP_TARGET} PRIVATE ${MODULE} ${MODULE}plugin)
27+
set(MODULE_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})

build/module_test.cmake

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
if (SCRATCHCPP_PLAYER_BUILD_UNIT_TESTS)
2+
set(TARGET ${MODULE}_test)
3+
set(TEST_MAIN_SRC ${PROJECT_SOURCE_DIR}/test/main.cpp)
4+
5+
include(GoogleTest)
6+
7+
add_executable(
8+
${TARGET}
9+
${TEST_MAIN_SRC}
10+
${MODULE_TEST_SRC}
11+
)
12+
13+
target_link_libraries(
14+
${TARGET}
15+
${MODULE}
16+
GTest::gtest_main
17+
GTest::gmock_main
18+
Qt6::Gui
19+
Qt6::Qml
20+
Qt6::Test
21+
)
22+
23+
target_include_directories(${TARGET} PRIVATE ${MODULE_SRC_DIR})
24+
gtest_discover_tests(${TARGET})
25+
endif()

res/icons/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
qt_add_resources(
2-
appscratchcpp-player "icon_theme"
2+
${APP_TARGET} "icon_theme"
33
PREFIX "/icons/scratchcpp"
44
FILES
55
index.theme
66
)
77

88
qt_add_resources(
9-
appscratchcpp-player "icons"
9+
${APP_TARGET} "icons"
1010
PREFIX "/icons/scratchcpp/32x32"
1111
FILES
1212
green_flag.svg

src/CMakeLists.txt

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
qt_add_executable(appscratchcpp-player
2-
main.cpp
3-
)
4-
5-
qt_add_qml_module(appscratchcpp-player
6-
URI ScratchCPP
7-
VERSION 1.0
8-
QML_FILES main.qml
9-
)
1+
add_subdirectory(app)
102

3+
add_subdirectory(global)
114
add_subdirectory(uicomponents)
12-
13-
target_link_libraries(appscratchcpp-player PRIVATE scratchcpp-uicomponents)

src/app/CMakeLists.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
set(APP_TARGET scratchcpp-player CACHE INTERNAL "")
2+
3+
qt_add_executable(${APP_TARGET}
4+
main.cpp
5+
app.cpp
6+
app.h
7+
appmenubar.cpp
8+
appmenubar.h
9+
)
10+
11+
qt_add_qml_module(${APP_TARGET}
12+
URI ScratchCPP
13+
VERSION 1.0
14+
QML_FILES main.qml
15+
)
16+
17+
set(QML_IMPORT_PATH "${QML_IMPORT_PATH};${CMAKE_CURRENT_LIST_DIR}"
18+
CACHE STRING "Qt Creator extra QML import paths"
19+
FORCE
20+
)
21+
22+
list(APPEND QML_IMPORT_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
23+
list(REMOVE_DUPLICATES QML_IMPORT_PATH)
24+
set(QML_IMPORT_PATH ${QML_IMPORT_PATH} CACHE STRING "" FORCE)
25+
26+
set_target_properties(${APP_TARGET} PROPERTIES
27+
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
28+
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
29+
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
30+
MACOSX_BUNDLE TRUE
31+
WIN32_EXECUTABLE TRUE
32+
)
33+
34+
target_compile_definitions(${APP_TARGET}
35+
PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
36+
target_compile_definitions(${APP_TARGET} PRIVATE BUILD_VERSION="${CMAKE_PROJECT_VERSION}")
37+
target_link_libraries(${APP_TARGET}
38+
PRIVATE Qt6::Quick Qt6::QuickControls2)
39+
target_include_directories(${APP_TARGET} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/..)
40+
target_include_directories(${APP_TARGET} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../global)
41+
42+
target_link_libraries(${APP_TARGET} PRIVATE scratchcpp-render scratchcpp-renderplugin)

src/app/app.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
#include <QGuiApplication>
4+
#include <QQmlApplicationEngine>
5+
#include <QQuickStyle>
6+
#include <QIcon>
7+
#include <cassert>
8+
9+
#include "app.h"
10+
#include "globalmodule.h"
11+
#include "modularity/ioc.h"
12+
13+
using namespace scratchcpp;
14+
using namespace scratchcpp::modularity;
15+
16+
App::App()
17+
{
18+
addModule(new GlobalModule);
19+
}
20+
21+
int App::run(int argc, char **argv)
22+
{
23+
qputenv("QSG_RENDER_LOOP", "basic");
24+
25+
// Set up application object
26+
QGuiApplication app(argc, argv);
27+
QCoreApplication::setOrganizationDomain("scratchcpp.github.io");
28+
QCoreApplication::setOrganizationName("ScratchCPP");
29+
QCoreApplication::setApplicationName("ScratchCPP");
30+
QCoreApplication::setApplicationVersion(BUILD_VERSION);
31+
32+
// Set style and icon theme name
33+
QQuickStyle::setStyle("Material");
34+
QIcon::setThemeName("scratchcpp");
35+
36+
// Register exports
37+
for (IModuleSetup *module : m_modules)
38+
module->registerExports();
39+
40+
// Init settings
41+
for (IModuleSetup *module : m_modules)
42+
module->initSettings();
43+
44+
// Setup modules: onPreInit
45+
for (IModuleSetup *module : m_modules)
46+
module->onPreInit();
47+
48+
/* Splash screen should show now, if any. */
49+
50+
// Setup modules: onInit
51+
for (IModuleSetup *module : m_modules)
52+
module->onInit();
53+
54+
// Setup modules: onStartApp (on next event loop)
55+
QMetaObject::invokeMethod(
56+
qApp,
57+
[this]() {
58+
for (IModuleSetup *m : m_modules) {
59+
m->onStartApp();
60+
}
61+
},
62+
Qt::QueuedConnection);
63+
64+
// Load main.qml
65+
QQmlApplicationEngine engine;
66+
engine.addImportPath(":/");
67+
68+
const QUrl url(u"qrc:/ScratchCPP/main.qml"_qs);
69+
QObject::connect(
70+
&engine,
71+
&QQmlApplicationEngine::objectCreated,
72+
&app,
73+
[url](QObject *obj, const QUrl &objUrl) {
74+
if (!obj && url == objUrl)
75+
QCoreApplication::exit(-1);
76+
},
77+
Qt::QueuedConnection);
78+
engine.load(url);
79+
80+
// Run the event loop
81+
int exitCode = app.exec();
82+
83+
// Deinit modules
84+
for (IModuleSetup *module : m_modules)
85+
module->onDeinit();
86+
87+
for (IModuleSetup *module : m_modules)
88+
module->onDestroy();
89+
90+
// Remove all modules
91+
removeModules();
92+
93+
return exitCode;
94+
}
95+
96+
void App::addModule(IModuleSetup *module)
97+
{
98+
assert(module);
99+
assert(std::find_if(m_modules.begin(), m_modules.end(), [module](IModuleSetup *m) { return m->moduleName() == module->moduleName(); }) == m_modules.end());
100+
m_modules.push_back(module);
101+
}
102+
103+
void App::removeModules()
104+
{
105+
for (IModuleSetup *module : m_modules)
106+
delete module;
107+
108+
m_modules.clear();
109+
ioc()->reset();
110+
}

src/app/app.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#include <vector>
6+
7+
namespace scratchcpp
8+
{
9+
10+
namespace modularity
11+
{
12+
13+
class IModuleSetup;
14+
15+
}
16+
17+
class App
18+
{
19+
public:
20+
App();
21+
22+
int run(int argc, char **argv);
23+
void addModule(modularity::IModuleSetup *module);
24+
25+
private:
26+
void removeModules();
27+
28+
std::vector<modularity::IModuleSetup *> m_modules;
29+
};
30+
31+
} // namespace scratchcpp

0 commit comments

Comments
 (0)