From 53a8bbba8f28a19b58203b7206120baedb658d13 Mon Sep 17 00:00:00 2001 From: Ghislain MARY Date: Mon, 29 Apr 2024 18:49:19 +0200 Subject: [PATCH] Add 00_HelloWorld qt tutorial. --- qt/00_HelloWorld/.gitignore | 1 + qt/00_HelloWorld/CMakeLists.txt | 44 ++++++++++++++++++++++++++++ qt/00_HelloWorld/README.md | 15 ++++++++++ qt/00_HelloWorld/resources.qrc | 5 ++++ qt/00_HelloWorld/src/CoreManager.cpp | 32 ++++++++++++++++++++ qt/00_HelloWorld/src/CoreManager.hpp | 25 ++++++++++++++++ qt/00_HelloWorld/src/main.cpp | 28 ++++++++++++++++++ qt/00_HelloWorld/ui/MainPage.qml | 18 ++++++++++++ 8 files changed, 168 insertions(+) create mode 100644 qt/00_HelloWorld/.gitignore create mode 100644 qt/00_HelloWorld/CMakeLists.txt create mode 100644 qt/00_HelloWorld/README.md create mode 100644 qt/00_HelloWorld/resources.qrc create mode 100644 qt/00_HelloWorld/src/CoreManager.cpp create mode 100644 qt/00_HelloWorld/src/CoreManager.hpp create mode 100644 qt/00_HelloWorld/src/main.cpp create mode 100644 qt/00_HelloWorld/ui/MainPage.qml diff --git a/qt/00_HelloWorld/.gitignore b/qt/00_HelloWorld/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/qt/00_HelloWorld/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/qt/00_HelloWorld/CMakeLists.txt b/qt/00_HelloWorld/CMakeLists.txt new file mode 100644 index 0000000..7bf4f7b --- /dev/null +++ b/qt/00_HelloWorld/CMakeLists.txt @@ -0,0 +1,44 @@ +cmake_minimum_required(VERSION 3.22) + +project(00_HelloWorld LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(Qt5 REQUIRED COMPONENTS Core Gui Qml Quick) +find_package(LinphoneCxx REQUIRED) + +set(CMAKE_AUTOMOC ON) +SET(CMAKE_AUTOUIC ON) + +set(SOURCES + "src/main.cpp" + "src/CoreManager.cpp" +) +set(QRC_RESOURCES resources.qrc) +set(QML_SOURCES) +file(STRINGS ${QRC_RESOURCES} QRC_RESOURCES_CONTENT) +foreach(line ${QRC_RESOURCES_CONTENT}) + set(result) + string(REGEX REPLACE + "^[ \t]*<[ \t]*file[ \t]*>[ \t]*(.+\\.[a-z]+)[ \t]*<[ \t]*/[ \t]*file[ \t]*>[ \t]*$" + "\\1" + result + "${line}" + ) + string(REGEX MATCH "\\.[a-z]+$" is_ui ${result}) + if(NOT ${is_ui} STREQUAL "") + list(APPEND QML_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/${result}") + endif() +endforeach() + +add_executable(00_HelloWorld ${SOURCES} ${QML_SOURCES} ${QRC_RESOURCES}) + +target_include_directories(00_HelloWorld PRIVATE ${LINPHONECXX_INCLUDE_DIRS}) +target_link_libraries(00_HelloWorld PRIVATE Qt5::Core Qt5::Gui Qt5::Qml Qt5::Quick ${LINPHONECXX_LIBRARIES}) + +set_target_properties(00_HelloWorld PROPERTIES AUTORCC ON) +set_target_properties(00_HelloWorld PROPERTIES + WIN32_EXECUTABLE ON + MACOSX_BUNDLE ON +) \ No newline at end of file diff --git a/qt/00_HelloWorld/README.md b/qt/00_HelloWorld/README.md new file mode 100644 index 0000000..e0f5253 --- /dev/null +++ b/qt/00_HelloWorld/README.md @@ -0,0 +1,15 @@ +# Hello World tutorial + +The purpose of this tutorial is to explain how to build a Qt app depending on the Linphone SDK and to create the `Core` object that all our APIs depends on. + +The user interface will only display the `Core`'s version, but in the next tutorial you will learn how to use it to login your SIP account. + + +## How to build + +In the following instructions, replace **** by the real path where your SDK is located, e.g. *~/projects/linphone-sdk/build-default/linphone-sdk/desktop/* + + mkdir build + cd build + cmake .. -DCMAKE_PREFIX_PATH= + cmake --build . diff --git a/qt/00_HelloWorld/resources.qrc b/qt/00_HelloWorld/resources.qrc new file mode 100644 index 0000000..739694b --- /dev/null +++ b/qt/00_HelloWorld/resources.qrc @@ -0,0 +1,5 @@ + + + ui/MainPage.qml + + diff --git a/qt/00_HelloWorld/src/CoreManager.cpp b/qt/00_HelloWorld/src/CoreManager.cpp new file mode 100644 index 0000000..39545d4 --- /dev/null +++ b/qt/00_HelloWorld/src/CoreManager.cpp @@ -0,0 +1,32 @@ +#include "CoreManager.hpp" + +CoreManager *CoreManager::mInstance = nullptr; + +CoreManager::CoreManager() +{ + // Create a core from the factory. + mCore = linphone::Factory::get()->createCore("", "", nullptr); +} + +CoreManager::~CoreManager() +{ + mCore = nullptr; +} + +void CoreManager::init() +{ + if (mInstance) + return; + mInstance = new CoreManager(); +} + +CoreManager *CoreManager::getInstance() +{ + return mInstance; +} + +QString CoreManager::getVersion() const +{ + // Get the version from the core. + return QString::fromStdString(mCore->getVersion()); +} diff --git a/qt/00_HelloWorld/src/CoreManager.hpp b/qt/00_HelloWorld/src/CoreManager.hpp new file mode 100644 index 0000000..8347bd3 --- /dev/null +++ b/qt/00_HelloWorld/src/CoreManager.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include + +#include + +class CoreManager : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString version READ getVersion CONSTANT) + +public: + static void init(); + static CoreManager *getInstance(); + + QString getVersion() const; + +private: + CoreManager(); + ~CoreManager(); + + std::shared_ptr mCore = nullptr; + + static CoreManager *mInstance; +}; \ No newline at end of file diff --git a/qt/00_HelloWorld/src/main.cpp b/qt/00_HelloWorld/src/main.cpp new file mode 100644 index 0000000..9ca6189 --- /dev/null +++ b/qt/00_HelloWorld/src/main.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#include "CoreManager.hpp" + +int main(int argc, char *argv[]) +{ + QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QGuiApplication app(argc, argv); + app.setOrganizationName("Belledonne Communications"); + app.setOrganizationDomain("belledonne-communications.com"); + app.setApplicationName(QFileInfo(app.applicationFilePath()).baseName()); + + QQmlApplicationEngine engine; + engine.load(QUrl("qrc:/ui/MainPage.qml")); + if (engine.rootObjects().isEmpty()) + qFatal("Unable to open main window."); + + // Initialize the CoreManager singleton and add it to the Qml context. + CoreManager::init(); + auto coreManager = CoreManager::getInstance(); + QQmlContext *ctx = engine.rootContext(); + ctx->setContextProperty("coreManager", coreManager); + + return app.exec(); +} diff --git a/qt/00_HelloWorld/ui/MainPage.qml b/qt/00_HelloWorld/ui/MainPage.qml new file mode 100644 index 0000000..05f5064 --- /dev/null +++ b/qt/00_HelloWorld/ui/MainPage.qml @@ -0,0 +1,18 @@ +import QtQuick 2.9 +import QtQuick.Controls 2.2 + +ApplicationWindow { + id: window + visible: true + + title: "Hello World" + width: 640 + height: 480 + + Text { + id: versionText + text: "Hello world, Linphone core version is " + coreManager.version // Get the version from the core manager. + anchors.horizontalCenter: parent.horizontalCenter + anchors.verticalCenter: parent.verticalCenter + } +}