Skip to content

Commit

Permalink
Enable Qt6 build via flag; default to Qt5
Browse files Browse the repository at this point in the history
  • Loading branch information
JonMagon committed Aug 29, 2024
1 parent d0b63f4 commit a1b72a0
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 16 deletions.
54 changes: 38 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ include_directories(${PROJECT_BINARY_BIN})

set(CMAKE_INCLUDE_CURRENT_DIR ON)

option(USE_QT6 "Use Qt6 instead of Qt5" OFF)

if(USE_QT6)
find_package(Qt6 COMPONENTS Widgets LinguistTools DBus REQUIRED)
find_package(PolkitQt6-1 REQUIRED)
set(QT_LIBS Qt6::Widgets Qt6::DBus)
set(POLKIT_LIB PolkitQt6-1::Core)
else()
find_package(Qt5 COMPONENTS Widgets LinguistTools DBus REQUIRED)
find_package(PolkitQt5-1 REQUIRED)
set(QT_LIBS Qt5::Widgets Qt5::DBus)
set(POLKIT_LIB PolkitQt5-1::Core)
endif()

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
Expand All @@ -39,8 +53,6 @@ set(CPACK_NSIS_DISPLAY_NAME ${APPLICATION_NAME})
set(QAPPLICATION_CLASS QApplication)
add_subdirectory(src/singleapplication)

find_package(Qt6 COMPONENTS Widgets LinguistTools DBus REQUIRED)

find_package(ECM REQUIRED NO_MODULE)
list(APPEND CMAKE_MODULE_PATH ${ECM_MODULE_PATH})

Expand All @@ -50,17 +62,24 @@ include(ECMInstallIcons)
include(ECMConfiguredInstall)
include(KDEInstallDirs)

find_package(PolkitQt6-1 REQUIRED)

set(helper_interface_xml dev.jonmagon.kdiskmark.helperinterface.xml)

qt6_generate_dbus_interface(
src/helper.h
${helper_interface_xml}
OPTIONS -a
)

qt6_add_dbus_interface(HelperInterface_SRCS ${CMAKE_CURRENT_BINARY_DIR}/${helper_interface_xml} helper_interface)
# Generate DBus Interface
if(USE_QT6)
qt6_generate_dbus_interface(
src/helper.h
${helper_interface_xml}
OPTIONS -a
)
qt6_add_dbus_interface(HelperInterface_SRCS ${CMAKE_CURRENT_BINARY_DIR}/${helper_interface_xml} helper_interface)
else()
qt5_generate_dbus_interface(
src/helper.h
${helper_interface_xml}
OPTIONS -a
)
qt5_add_dbus_interface(HelperInterface_SRCS ${CMAKE_CURRENT_BINARY_DIR}/${helper_interface_xml} helper_interface)
endif()

set(TS_FILES
data/translations/${PROJECT_NAME}_cs_CZ.ts
Expand All @@ -83,7 +102,11 @@ set(TS_FILES
data/translations/${PROJECT_NAME}_zh_CN.ts
)

qt6_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
if(USE_QT6)
qt6_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
else()
qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
endif()

add_executable(${PROJECT_NAME}
${QM_FILES}
Expand Down Expand Up @@ -113,8 +136,7 @@ add_executable(${PROJECT_NAME}

target_link_libraries(${PROJECT_NAME} PRIVATE
SingleApplication::SingleApplication
Qt6::Widgets
Qt6::DBus
${QT_LIBS}
)

set(APP_ICONS
Expand All @@ -141,8 +163,8 @@ add_executable(${PROJECT_NAME}_helper
)

target_link_libraries(${PROJECT_NAME}_helper
Qt6::DBus
PolkitQt6-1::Core
${QT_LIBS}
${POLKIT_LIB}
)

install(TARGETS ${PROJECT_NAME}_helper DESTINATION ${KDE_INSTALL_LIBEXECDIR})
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ cmake -D CMAKE_BUILD_TYPE=Release ..
cpack -G DEB # Or RPM, ZIP etc.
```

### Building with Qt6
To build **KDiskMark** with Qt6 instead of the default Qt5, use the `USE_QT6` flag during the CMake configuration step:

```bash
mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=Release -D USE_QT6=ON ..
cpack -G DEB # Or RPM, ZIP etc.
```

## Localization [![Crowdin](https://badges.crowdin.net/kdiskmark/localized.svg)](https://crowdin.com/project/kdiskmark)
To help with localization you can use [Crowdin](https://crowdin.com/project/kdiskmark) or translate files in `data/translations` with [Qt Linguist](https://doc.qt.io/Qt-5/linguist-translators.html) directly. To add a new language, copy `data/translations/kdiskmark.ts` to `data/translations/kdiskmark_<ISO 639-1 language code>_<ISO 3166-1 alpha-2 language code>.ts`, translate it, then add the file to the TS_FILES variable in CMakeLists.txt, and create a pull request. It is also possible to add localized Comment and Keywords sections into `data/kdiskmark.desktop` and message for PolicyKit authorization into `data/dev.jonmagon.kdiskmark.helper.policy`.

Expand Down
7 changes: 7 additions & 0 deletions src/appsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <QSettings>
#include <QMetaEnum>

#include <QDebug>

QTranslator AppSettings::s_appTranslator;
QTranslator AppSettings::s_qtTranslator;

Expand Down Expand Up @@ -53,7 +55,12 @@ void AppSettings::applyLocale(const QLocale &locale)
}
}

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
bool qtTranslatorLoaded = s_qtTranslator.load(newLocale, QStringLiteral("qt"), QStringLiteral("_"), QLibraryInfo::path(QLibraryInfo::TranslationsPath));
#else
bool qtTranslatorLoaded = s_qtTranslator.load(newLocale, QStringLiteral("qt"), QStringLiteral("_"), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
#endif

if (qtTranslatorLoaded) {
QCoreApplication::installTranslator(&s_qtTranslator);
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ QVariantMap Helper::endSession()

bool Helper::testFilePath(const QString &benchmarkPath)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
if (QFileInfo(benchmarkPath).isSymbolicLink()) {
#else
// detects *.lnk on Windows, but there's not Windows version, whatever
if (QFileInfo(benchmarkPath).isSymLink()) {
#endif
qWarning("The path should not be symbolic link.");
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ int main(int argc, char *argv[])
.arg(PROJECT_VERSION_MINOR).arg(PROJECT_VERSION_PATCH));
QCoreApplication::setOrganizationName(QStringLiteral(PROJECT_NAME));

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

SingleApplication a(argc, argv);

AppSettings().setupLocalization();
Expand Down
4 changes: 4 additions & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,11 @@ void MainWindow::changeEvent(QEvent *event)
}

ui->comboBox_Storages->setItemText(0, tr("Add a directory"));
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
resizeComboBoxItemsPopup(ui->comboBox_Storages);
#else
int width = fontMetrics.width(combobox->itemText(i));
#endif

break;
}
Expand Down
12 changes: 12 additions & 0 deletions src/storageitemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ void StorageItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &o

const int percent = storage.bytesOccupied * 100 / storage.bytesTotal;

#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
int percentTextWidth = QFontMetrics(painter->font()).horizontalAdvance(QLatin1Char('0')) * 4;
#else
int percentTextWidth = QFontMetrics(painter->font()).width(QLatin1Char('0')) * 4;
#endif

const int textVMargin = style->pixelMetric(QStyle::PM_FocusFrameVMargin, 0, option.widget) + 1;

Expand All @@ -76,7 +80,11 @@ void StorageItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &o

painter->drawText(progressBarRect, option.displayAlignment | Qt::AlignRight, QString::number(percent) + "%");
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
else if (index.data(Qt::DecorationRole).typeId() == QVariant::Icon) {
#else
else if (index.data(Qt::DecorationRole).type() == QVariant::Icon) {
#endif
QStyle *style = option.widget ? option.widget->style() : QApplication::style();

QStyleOptionViewItem opt = option;
Expand All @@ -99,7 +107,11 @@ void StorageItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &o

QString text = index.data(Qt::DisplayRole).toString();

#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
int textWidth = QFontMetrics(painter->font()).horizontalAdvance(text);
#else
int textWidth = QFontMetrics(painter->font()).width(text);
#endif

const int textMargin = (style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, option.widget) + 1);
int textMarginCenter = (option.rect.size().width() - (option.decorationSize.width() + textMargin * 8 + textWidth)) / 2;
Expand Down

0 comments on commit a1b72a0

Please sign in to comment.