Skip to content

Commit

Permalink
Allow a build with Qt 6
Browse files Browse the repository at this point in the history
  • Loading branch information
tsimonq2 committed Aug 13, 2023
1 parent 9eb4287 commit d9f699d
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 39 deletions.
114 changes: 88 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(QTERMINAL_VERSION "1.3.0")

option(UPDATE_TRANSLATIONS "Update source translation translations/*.ts files" OFF)
option(BUILD_TESTS "Builds tests" ON)
option(USE_QT6 "Build with Qt 6 instead of Qt 5" OFF)

if(APPLE)
option(APPLEBUNDLE "Build as qterminal.app bundle" ON)
Expand All @@ -21,24 +22,38 @@ endif()
# we need qpa/qplatformnativeinterface.h for global shortcut

# Minimum Versions
set(LXQTBT_MINIMUM_VERSION "0.13.0")
set(QTERMWIDGET_MINIMUM_VERSION "1.3.0")
set(QT_MINIMUM_VERSION "5.15.0")
if(NOT USE_QT6)
set(LXQTBT_MINIMUM_VERSION "0.13.0")
set(QT_MINIMUM_VERSION "5.15.0")
set(QT_MAJOR_VERSION "5")
else()
set(LXQTBT_MINIMUM_VERSION "2.0.0")
set(QT_MINIMUM_VERSION "6.3.0")
set(QT_MAJOR_VERSION "6")
endif()

find_package(Qt5Gui ${QT_MINIMUM_VERSION} REQUIRED)
find_package(Qt5LinguistTools ${QT_MINIMUM_VERSION} REQUIRED)
find_package(Qt5Widgets ${QT_MINIMUM_VERSION} REQUIRED)
find_package(Qt${QT_MAJOR_VERSION}Core ${QT_MINIMUM_VERSION} REQUIRED)
find_package(Qt${QT_MAJOR_VERSION}Gui ${QT_MINIMUM_VERSION} REQUIRED)
find_package(Qt${QT_MAJOR_VERSION}LinguistTools ${QT_MINIMUM_VERSION} REQUIRED)
find_package(Qt${QT_MAJOR_VERSION}Widgets ${QT_MINIMUM_VERSION} REQUIRED)
if(UNIX)
find_package(Qt5DBus ${QT_MINIMUM_VERSION} REQUIRED)
if (NOT APPLE)
find_package(Qt${QT_MAJOR_VERSION}DBus ${QT_MINIMUM_VERSION} REQUIRED)
if(NOT APPLE AND QT_MAJOR_VERSION EQUAL 5)
find_package(Qt5X11Extras ${QT_MINIMUM_VERSION} REQUIRED)
endif (NOT APPLE)
elseif(QT_MAJOR_VERSION EQUAL 6)
find_package(Qt6 COMPONENTS Core Core5Compat REQUIRED)
endif()
endif()
find_package(QTermWidget${QT_MAJOR_VERSION} ${QTERMWIDGET_MINIMUM_VERSION} REQUIRED)
if(QT_MAJOR_VERSION EQUAL 5)
find_package(lxqt-build-tools ${LXQTBT_MINIMUM_VERSION} REQUIRED)
else()
find_package(lxqt2-build-tools ${LXQTBT_MINIMUM_VERSION} REQUIRED)
endif()
find_package(QTermWidget5 ${QTERMWIDGET_MINIMUM_VERSION} REQUIRED)
find_package(lxqt-build-tools ${LXQTBT_MINIMUM_VERSION} REQUIRED)

if (BUILD_TESTS)
find_package(Qt5 ${QT_MINIMUM_VERSION} CONFIG REQUIRED Test)
find_package(Qt${QT_MAJOR_VERSION} ${QT_MINIMUM_VERSION} CONFIG REQUIRED Test)
endif()

include(LXQtPreventInSourceBuilds)
Expand All @@ -47,13 +62,23 @@ pkg_check_modules(LIBCANBERRA libcanberra)
include(LXQtTranslateTs)
include(LXQtTranslateDesktop)
include(LXQtCompilerSettings NO_POLICY_SCOPE)
message(STATUS "Qt version: ${Qt5Core_VERSION}")
message(STATUS "Qt version: ${Qt${QT_MAJOR_VERSION}Core_VERSION}")

# TODO remove Qxt
message(STATUS "Using bundled Qxt...")
set(QXT_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/src/third-party")


if(QT_MAJOR_VERSION EQUAL 5)
find_path(QTERMWIDGET5_INCLUDE_DIR NAMES qtermwidget5/qtermwidget.h)
add_definitions(-DUSE_QTERMWIDGET5)
include_directories(${QTERMWIDGET5_INCLUDE_DIR})
elseif(QT_MAJOR_VERSION EQUAL 6)
find_path(QTERMWIDGET6_INCLUDE_DIR NAMES qtermwidget6/qtermwidget.h)
add_definitions(-DUSE_QTERMWIDGET6)
include_directories(${QTERMWIDGET6_INCLUDE_DIR})
endif()

if(APPLE)
find_library(CARBON_LIBRARY Carbon REQUIRED)
message(STATUS "CARBON_LIBRARY: ${CARBON_LIBRARY}")
Expand Down Expand Up @@ -98,14 +123,24 @@ set(QTERM_MOC_SRC
src/tab-switcher.h
)

if (Qt5DBus_FOUND)
if (Qt${QT_MAJOR_VERSION}DBus_FOUND)
add_definitions(-DHAVE_QDBUS)
QT5_ADD_DBUS_ADAPTOR(QTERM_SRC src/org.lxqt.QTerminal.Window.xml mainwindow.h MainWindow)
QT5_ADD_DBUS_ADAPTOR(QTERM_SRC src/org.lxqt.QTerminal.Tab.xml termwidgetholder.h TermWidgetHolder)
QT5_ADD_DBUS_ADAPTOR(QTERM_SRC src/org.lxqt.QTerminal.Terminal.xml termwidget.h TermWidget)
QT5_ADD_DBUS_ADAPTOR(QTERM_SRC src/org.lxqt.QTerminal.Process.xml qterminalapp.h QTerminalApp)

macro(ADD_DBUS_ADAPTOR_FOR_VERSION)
if("${QT_MAJOR_VERSION}" STREQUAL "5")
QT5_ADD_DBUS_ADAPTOR(${ARGV})
elseif("${QT_MAJOR_VERSION}" STREQUAL "6")
QT6_ADD_DBUS_ADAPTOR(${ARGV})
endif()
endmacro()

ADD_DBUS_ADAPTOR_FOR_VERSION(QTERM_SRC src/org.lxqt.QTerminal.Window.xml mainwindow.h MainWindow)
ADD_DBUS_ADAPTOR_FOR_VERSION(QTERM_SRC src/org.lxqt.QTerminal.Tab.xml termwidgetholder.h TermWidgetHolder)
ADD_DBUS_ADAPTOR_FOR_VERSION(QTERM_SRC src/org.lxqt.QTerminal.Terminal.xml termwidget.h TermWidget)
ADD_DBUS_ADAPTOR_FOR_VERSION(QTERM_SRC src/org.lxqt.QTerminal.Process.xml qterminalapp.h QTerminalApp)

set(QTERM_MOC_SRC ${QTERM_MOC_SRC} src/dbusaddressable.h)
message(STATUS "Building with Qt5DBus support")
message(STATUS "Building with Qt${QT_MAJOR_VERSION}DBus support")
endif()

if(NOT QXT_FOUND)
Expand All @@ -132,9 +167,34 @@ set(QTERM_RCC_SRC
src/icons.qrc
)

qt5_wrap_ui( QTERM_UI ${QTERM_UI_SRC} )
qt5_wrap_cpp( QTERM_MOC ${QTERM_MOC_SRC} )
qt5_add_resources( QTERM_RCC ${QTERM_RCC_SRC} )
macro(WRAP_UI_FOR_VERSION)
if("${QT_MAJOR_VERSION}" STREQUAL "5")
qt5_wrap_ui(${ARGV})
elseif("${QT_MAJOR_VERSION}" STREQUAL "6")
qt6_wrap_ui(${ARGV})
endif()
endmacro()

macro(WRAP_CPP_FOR_VERSION)
if("${QT_MAJOR_VERSION}" STREQUAL "5")
qt5_wrap_cpp(${ARGV})
elseif("${QT_MAJOR_VERSION}" STREQUAL "6")
qt6_wrap_cpp(${ARGV})
endif()
endmacro()

macro(ADD_RESOURCES_FOR_VERSION)
if("${QT_MAJOR_VERSION}" STREQUAL "5")
qt5_add_resources(${ARGV})
elseif("${QT_MAJOR_VERSION}" STREQUAL "6")
qt6_add_resources(${ARGV})
endif()
endmacro()

WRAP_UI_FOR_VERSION(QTERM_UI ${QTERM_UI_SRC})
WRAP_CPP_FOR_VERSION(QTERM_MOC ${QTERM_MOC_SRC})
ADD_RESOURCES_FOR_VERSION(QTERM_RCC ${QTERM_RCC_SRC})

lxqt_translate_ts(QTERM_QM
UPDATE_TRANSLATIONS
${UPDATE_TRANSLATIONS}
Expand Down Expand Up @@ -202,21 +262,23 @@ add_executable(${EXE_NAME} ${GUI_TYPE}
)

target_link_libraries(${EXE_NAME}
Qt5::Gui
qtermwidget5
Qt${QT_MAJOR_VERSION}::Core
Qt${QT_MAJOR_VERSION}::Gui
Qt${QT_MAJOR_VERSION}::Widgets
qtermwidget${QT_MAJOR_VERSION}
util
)
if(QXT_FOUND)
target_link_libraries(${EXE_NAME} ${QXT_CORE_LIB} ${QXT_GUI_LIB})
endif()

if (Qt5DBus_FOUND)
target_link_libraries(${EXE_NAME} ${Qt5DBus_LIBRARIES})
if (Qt${QT_MAJOR_VERSION}DBus_FOUND)
target_link_libraries(${EXE_NAME} ${Qt${QT_MAJOR_VERSION}DBus_LIBRARIES})
endif()

if(APPLE)
target_link_libraries(${EXE_NAME} ${CARBON_LIBRARY})
elseif(UNIX)
elseif(UNIX AND QT_MAJOR_VERSION EQUAL 5)
target_link_libraries(${EXE_NAME} Qt5::X11Extras)
endif()

Expand Down
2 changes: 1 addition & 1 deletion src/dbusaddressable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ DBusAddressable::DBusAddressable(const QString& prefix)
{
#ifdef HAVE_QDBUS
QString uuidString = QUuid::createUuid().toString();
m_path = prefix + QLatin1Char('/') + uuidString.replace(QRegExp(QStringLiteral("[\\{\\}\\-]")), QString());
m_path = prefix + QLatin1Char('/') + uuidString.replace(QRegularExpression(QStringLiteral("[\\{\\}\\-]")), QString());
#endif
}
3 changes: 2 additions & 1 deletion src/dbusaddressable.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef DBUSADDRESSABLE_H
#define DBUSADDRESSABLE_H

#include <QRegularExpression>
#include <QString>
#ifdef HAVE_QDBUS
#include <QtDBus/QtDBus>
Expand Down Expand Up @@ -31,4 +32,4 @@ template <class AClass, class WClass> void registerAdapter(WClass *obj)
#endif


#endif
#endif
3 changes: 1 addition & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,14 @@ int main(int argc, char *argv[])

// icons
/* setup our custom icon theme if there is no system theme (OS X, Windows) */
QCoreApplication::instance()->setAttribute(Qt::AA_UseHighDpiPixmaps); //Fix for High-DPI systems
if (QIcon::themeName().isEmpty())
QIcon::setThemeName(QStringLiteral("QTerminal"));

// translations

// install the translations built-into Qt itself
QTranslator qtTranslator;
qtTranslator.load(QStringLiteral("qt_") + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
qtTranslator.load(QStringLiteral("qt_") + QLocale::system().name(), QLibraryInfo::path(QLibraryInfo::TranslationsPath));
app->installTranslator(&qtTranslator);

QTranslator translator;
Expand Down
1 change: 1 addition & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <QTimer>
#include <functional>
#include <QGuiApplication>
#include <QActionGroup>

#ifdef HAVE_QDBUS
#include <QtDBus/QtDBus>
Expand Down
1 change: 1 addition & 0 deletions src/tabwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <QColorDialog>
#include <QMouseEvent>
#include <QMenu>
#include <QActionGroup>

#include "mainwindow.h"
#include "termwidgetholder.h"
Expand Down
4 changes: 2 additions & 2 deletions src/terminalconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ TerminalConfig TerminalConfig::fromDbus(const QHash<QString,QVariant> &termArgsC

static QString variantToString(const QVariant& variant, QString &defaultVal)
{
if (variant.type() == QVariant::String)
if (variant.typeId() == QMetaType::type("QString"))
return qvariant_cast<QString>(variant);
return defaultVal;
}

static QStringList variantToStringList(const QVariant& variant, QStringList &defaultVal)
{
if (variant.type() == QVariant::StringList)
if (variant.typeId() == QMetaType::type("QStringList"))
return qvariant_cast<QStringList>(variant);
return defaultVal;
}
Expand Down
7 changes: 6 additions & 1 deletion src/termwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
#ifndef TERMWIDGET_H
#define TERMWIDGET_H

#include <qtermwidget.h>
#ifdef USE_QTERMWIDGET5
#include <qtermwidget5/qtermwidget.h>
#elif defined(USE_QTERMWIDGET6)
#include <qtermwidget6/qtermwidget.h>
#endif

#include "terminalconfig.h"

#include <QAction>
Expand Down
2 changes: 1 addition & 1 deletion src/third-party/qxtglobalshortcut_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class QxtGlobalShortcutPrivate : public QxtPrivate<QxtGlobalShortcut>
static bool error;
#ifndef Q_OS_MAC
static int ref;
bool nativeEventFilter(const QByteArray & eventType, void * message, long * result) override;
bool nativeEventFilter(const QByteArray &eventType, void *message, qintptr *result);
#endif

static void activateShortcut(quint32 nativeKey, quint32 nativeMods);
Expand Down
6 changes: 3 additions & 3 deletions src/third-party/qxtglobalshortcut_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

#include <QGuiApplication>
#include <QVector>
#include <QtX11Extras/QX11Info>

#include <xcb/xcb.h>
#include <X11/Xlib.h>
Expand Down Expand Up @@ -95,7 +94,8 @@ class QxtX11Data {
public:
QxtX11Data()
{
m_display = QX11Info::display();
auto *x11Application = qGuiApp->nativeInterface<QNativeInterface::QX11Application>();
m_display = x11Application ? x11Application->display() : nullptr;
}

bool isValid()
Expand Down Expand Up @@ -149,7 +149,7 @@ class QxtX11Data {
} // namespace

bool QxtGlobalShortcutPrivate::nativeEventFilter(const QByteArray & eventType,
void *message, long *result)
void *message, qintptr *result)
{
Q_UNUSED(result);

Expand Down
21 changes: 19 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
qt5_wrap_cpp(QTERM_TEST_MOC qterminal_test.h)
# Check if QT_MAJOR_VERSION was not defined
if(NOT DEFINED QT_MAJOR_VERSION)
message(FATAL_ERROR "QT_MAJOR_VERSION not set.")
endif()

# Use appropriate wrap_cpp command based on QT_MAJOR_VERSION
if("${QT_MAJOR_VERSION}" STREQUAL "5")
qt5_wrap_cpp(QTERM_TEST_MOC qterminal_test.h)
set(QT_TEST_LIB Qt5::Test)
elseif("${QT_MAJOR_VERSION}" STREQUAL "6")
qt6_wrap_cpp(QTERM_TEST_MOC qterminal_test.h)
set(QT_TEST_LIB Qt6::Test)
else()
message(FATAL_ERROR "Unsupported QT_MAJOR_VERSION: ${QT_MAJOR_VERSION}")
endif()

add_executable(qterminal_test
qterminal_test.cpp
${CMAKE_SOURCE_DIR}/src/qterminalutils.cpp
${QTERM_TEST_MOC})
target_link_libraries(qterminal_test Qt5::Test)

target_link_libraries(qterminal_test ${QT_TEST_LIB})

add_test(NAME qterminal_test COMMAND qterminal_test)

0 comments on commit d9f699d

Please sign in to comment.