Skip to content

Commit

Permalink
QML boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrePTJ committed Mar 24, 2024
1 parent f44c2d1 commit 4cf3ae8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 32 deletions.
22 changes: 1 addition & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,7 @@ FetchContent_Declare(range-v3
FetchContent_MakeAvailable(fmt magic_enum spdlog range-v3)

# Setup Qt
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt6 QUIET COMPONENTS Widgets Network LinguistTools)
if (NOT Qt6_FOUND)
message(NOTICE "Qt6 not found. Fallback to Qt5")
find_package(Qt5 COMPONENTS Widgets Network LinguistTools REQUIRED)
add_compile_definitions(UNICODE _UNICODE)
message(STATUS "Using Qt ${Qt5_VERSION}")
else ()
message(STATUS "Using Qt ${Qt6_VERSION}")
endif ()
find_package(Qt6 QUIET COMPONENTS Widgets Network LinguistTools QuickControls2)

# Write version to file to ease packaging
file(WRITE ${CMAKE_BINARY_DIR}/version.txt ${PROJECT_VERSION})
Expand All @@ -58,11 +47,6 @@ option(KEMAI_ENABLE_UPDATE_CHECK "Allow Kemai to check for update from Github re

# OS Dependant options / configurations
if (WIN32)
# Hide console
if (${CMAKE_BUILD_TYPE} STREQUAL "Release")
set(OS_BUNDLE WIN32)
endif ()

# Add Qt libs and installer for windows platform
include(WinDeployQt)

Expand All @@ -78,16 +62,12 @@ if (WIN32)
set(CPACK_CREATE_DESKTOP_LINKS Kemai;${KEMAI_PROJECT_NAME})
set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_SOURCE_DIR}/LICENSE.txt)
elseif (APPLE)
set(OS_BUNDLE MACOSX_BUNDLE)
# set(CMAKE_OSX_DEPLOYMENT_TARGET 10.12)
set(DEPLOY_DIR .)
set(CPACK_GENERATOR DragNDrop)
install(FILES LICENSE.txt DESTINATION ${DEPLOY_DIR})
else ()
include(GNUInstallDirs)
set(OS_BUNDLE)
set(DEPLOY_DIR bin)
# install(FILES share/kemai.desktop DESTINATION ${)
install(FILES share/kemai.desktop
DESTINATION ${CMAKE_INSTALL_DATADIR}/applications
)
Expand Down
23 changes: 18 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
project(Kemai)

add_executable(${PROJECT_NAME} ${OS_BUNDLE})
qt_add_executable(${PROJECT_NAME})

set_target_properties(${PROJECT_NAME} PROPERTIES
WIN32_EXECUTABLE TRUE
MACOSX_BUNDLE TRUE
AUTOMOC ON
AUTOUIC ON
AUTORCC ON
)

configure_file(kemaiConfig.h.in ${PROJECT_BINARY_DIR}/kemaiConfig.h)

Expand Down Expand Up @@ -84,6 +92,9 @@ set(UIS
gui/taskWidget.ui
gui/timeSheetListWidgetItem.ui)

set(QMLS
qml/main.qml)


# Localization
include(LocalizationTools)
Expand Down Expand Up @@ -112,6 +123,8 @@ set(RESX
${KEMAI_L10N_QRC})

qt_add_resources(${PROJECT_NAME} ${RESX})
qt_add_resources(${PROJECT_NAME} "qml" PREFIX "/kemai/" FILES ${QMLS})
qt_add_resources(${PROJECT_NAME} "controls_conf" PREFIX "/" FILES "qtquickcontrols2.conf")

# OS Specifics
if (WIN32)
Expand All @@ -120,7 +133,7 @@ if (WIN32)
monitor/windowsDesktopEventsMonitor.cpp)
list(APPEND HDRS
monitor/windowsDesktopEventsMonitor.h)
target_link_libraries(${PROJECT_NAME} Wtsapi32)
target_link_libraries(${PROJECT_NAME} PRIVATE Wtsapi32)
elseif (APPLE)
list(APPEND SRCS
monitor/macDesktopEventsMonitor.mm)
Expand All @@ -129,16 +142,16 @@ elseif (APPLE)
set(KEMAI_ICNS "${CMAKE_SOURCE_DIR}/bundle/macos/kemai.icns")
set_source_files_properties(${KEMAI_ICNS} PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")
set_target_properties(${PROJECT_NAME} PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_SOURCE_DIR}/bundle/macos/Info.plist)
target_link_libraries(${PROJECT_NAME} "-framework CoreGraphics")
target_link_libraries(${PROJECT_NAME} PRIVATE "-framework CoreGraphics")
elseif (UNIX)
list(APPEND SRCS
monitor/linuxDesktopEventsMonitor.cpp)
list(APPEND HDRS
monitor/linuxDesktopEventsMonitor.h)
target_link_libraries(${PROJECT_NAME} Xss X11)
target_link_libraries(${PROJECT_NAME} PRIVATE Xss X11)
endif ()

# Target configuration
target_sources(${PROJECT_NAME} PRIVATE ${SRCS} ${HDRS} ${UIS} ${RESX} ${KEMAI_ICNS})
target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(${PROJECT_NAME} Qt::Widgets Qt::Network spdlog::spdlog magic_enum::magic_enum range-v3::range-v3)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt::Widgets Qt::Network Qt::QuickControls2 spdlog::spdlog magic_enum::magic_enum range-v3::range-v3)
22 changes: 16 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <QApplication>
#include <QGuiApplication>
#include <QLibraryInfo>
#include <QQmlApplicationEngine>
#include <QTranslator>
#include <QTreeView>

Expand All @@ -19,7 +21,7 @@ using namespace kemai;

static constinit const auto MaxLogFileSize = 1024 * 102 * 5;

void myMessageOutput(QtMsgType type, const QMessageLogContext& /*context*/, const QString& msg)
void kemaiQtMessageOutput(QtMsgType type, const QMessageLogContext& /*context*/, const QString& msg)
{
switch (type)
{
Expand All @@ -41,7 +43,7 @@ void myMessageOutput(QtMsgType type, const QMessageLogContext& /*context*/, cons

int main(int argc, char* argv[])
{
qInstallMessageHandler(myMessageOutput);
qInstallMessageHandler(kemaiQtMessageOutput);

QApplication app(argc, argv);
QApplication::setApplicationName("Kemai");
Expand Down Expand Up @@ -84,10 +86,18 @@ int main(int argc, char* argv[])
KimaiClient::addTrustedCertificates(kemaiSettings.trustedCertificates);

// Startup
MainWindow mainWindow;
mainWindow.restoreGeometry(kemaiSettings.kemai.geometry);
mainWindow.setLoggerTreeModel(loggerTreeModel);
mainWindow.show();
QQmlApplicationEngine engine;
const QUrl url("qrc:/kemai/qml/main.qml");
QObject::connect(
&engine, &QQmlApplicationEngine::objectCreated, &app,
[url](QObject* obj, const QUrl& objUrl) {
if (obj == nullptr && url == objUrl)
{
QCoreApplication::exit(-1);
}
},
Qt::QueuedConnection);
engine.load(url);

return QApplication::exec();
}
12 changes: 12 additions & 0 deletions src/qml/main.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Material
import QtQuick.Layouts
import QtQuick.Window

ApplicationWindow {
id: window
width: 400
height: 500
visible: true
}
5 changes: 5 additions & 0 deletions src/qtquickcontrols2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Controls]
Style=Material
[Material]
Theme=Dark
Accent=Green

0 comments on commit 4cf3ae8

Please sign in to comment.