Skip to content

Commit

Permalink
Migrate to Qt 6 (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonMagon committed May 5, 2024
1 parent e6d9ea1 commit ffe2bba
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 39 deletions.
18 changes: 9 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ set(CPACK_NSIS_DISPLAY_NAME ${APPLICATION_NAME})
set(QAPPLICATION_CLASS QApplication)
add_subdirectory(src/singleapplication)

find_package(Qt5 COMPONENTS Widgets LinguistTools DBus REQUIRED)
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 +50,17 @@ include(ECMInstallIcons)
include(ECMConfiguredInstall)
include(KDEInstallDirs)

find_package(PolkitQt5-1 REQUIRED)
find_package(PolkitQt6-1 REQUIRED)

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

qt5_generate_dbus_interface(
qt6_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)
qt6_add_dbus_interface(HelperInterface_SRCS ${CMAKE_CURRENT_BINARY_DIR}/${helper_interface_xml} helper_interface)

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

qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
qt6_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})

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

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

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

target_link_libraries(${PROJECT_NAME}_helper
Qt5::DBus
PolkitQt5-1::Core
Qt6::DBus
PolkitQt6-1::Core
)

install(TARGETS ${PROJECT_NAME}_helper DESTINATION ${KDE_INSTALL_LIBEXECDIR})
Expand Down
23 changes: 19 additions & 4 deletions src/appsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ AppSettings::AppSettings(QObject *parent)
void AppSettings::setupLocalization()
{
applyLocale(locale());
QCoreApplication::installTranslator(&s_appTranslator);
QCoreApplication::installTranslator(&s_qtTranslator);
}

QLocale AppSettings::locale() const
Expand All @@ -42,8 +40,25 @@ void AppSettings::applyLocale(const QLocale &locale)
{
const QLocale newLocale = locale == defaultLocale() ? QLocale::system() : locale;
QLocale::setDefault(newLocale);
s_appTranslator.load(newLocale, QStringLiteral(PROJECT_NAME), QStringLiteral("_"), QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("translations"), QStandardPaths::LocateDirectory));
s_qtTranslator.load(newLocale, QStringLiteral("qt"), QStringLiteral("_"), QLibraryInfo::location(QLibraryInfo::TranslationsPath));

QCoreApplication::removeTranslator(&s_appTranslator);
QCoreApplication::removeTranslator(&s_qtTranslator);

if (newLocale.language() != QLocale::English) {
bool appTranslatorLoaded = s_appTranslator.load(newLocale, QStringLiteral(PROJECT_NAME), QStringLiteral("_"), QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("translations"), QStandardPaths::LocateDirectory));
if (appTranslatorLoaded) {
QCoreApplication::installTranslator(&s_appTranslator);
} else {
qWarning() << "Failed to load application translations for locale" << newLocale;
}
}

bool qtTranslatorLoaded = s_qtTranslator.load(newLocale, QStringLiteral("qt"), QStringLiteral("_"), QLibraryInfo::path(QLibraryInfo::TranslationsPath));
if (qtTranslatorLoaded) {
QCoreApplication::installTranslator(&s_qtTranslator);
} else {
qWarning() << "Failed to load Qt translations for locale" << newLocale;
}
}

QLocale AppSettings::defaultLocale()
Expand Down
5 changes: 0 additions & 5 deletions src/helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,7 @@ 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
2 changes: 0 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ int main(int argc, char *argv[])
.arg(PROJECT_VERSION_MINOR).arg(PROJECT_VERSION_PATCH));
QCoreApplication::setOrganizationName(QStringLiteral(PROJECT_NAME));

QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

SingleApplication a(argc, argv);

AppSettings().setupLocalization();
Expand Down
8 changes: 3 additions & 5 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <QAbstractItemView>
#include <QStyleFactory>
#include <QTimer>
#include <QActionGroup>

#include "math.h"
#include "about.h"
Expand Down Expand Up @@ -47,7 +48,7 @@ MainWindow::MainWindow(QWidget *parent)
if (QLocale().name() == locale.name()) lang->setChecked(true);
}

connect(localesGroup, SIGNAL(triggered(QAction*)), this, SLOT(localeSelected(QAction*)));
connect(localesGroup, &QActionGroup::triggered, this, &MainWindow::localeSelected);

ui->extraIcon->setPixmap(style()->standardIcon(QStyle::SP_MessageBoxWarning).pixmap(QSize(16, 16)));
ui->extraIcon->setToolTip(tr("The device is encrypted. Performance may drop."));
Expand Down Expand Up @@ -371,11 +372,8 @@ void MainWindow::resizeComboBoxItemsPopup(QComboBox *combobox)
QFontMetrics fontMetrics(combobox->font());
for (int i = 0; i < combobox->count(); i++)
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
int width = fontMetrics.horizontalAdvance(combobox->itemText(i));
#else
int width = fontMetrics.width(combobox->itemText(i));
#endif

if (width > maxWidth)
maxWidth = width;
}
Expand Down
27 changes: 13 additions & 14 deletions src/storageitemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ void StorageItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &o
QVariant variant = index.data(Qt::UserRole);

if (variant.canConvert<Global::Storage>()) {
QStyledItemDelegate::paint(painter, option, QModelIndex());

Global::Storage storage = variant.value<Global::Storage>();

QStyle *style = option.widget ? option.widget->style() : QApplication::style();

QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
opt.text = QString();
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, option.widget);

QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
? QPalette::Normal : QPalette::Disabled;
if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
Expand Down Expand Up @@ -54,11 +57,8 @@ 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;

progressBarOption.rect = progressBarRect.adjusted(0, textHeight / 2 + textVMargin, -percentTextWidth, -textHeight / 2 - textVMargin);
Expand All @@ -76,11 +76,15 @@ void StorageItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &o

painter->drawText(progressBarRect, option.displayAlignment | Qt::AlignRight, QString::number(percent) + "%");
}
else if (index.data(Qt::DecorationRole).type() == QVariant::Icon) {
QStyledItemDelegate::paint(painter, option, QModelIndex());

else if (index.data(Qt::DecorationRole).typeId() == QVariant::Icon) {
QStyle *style = option.widget ? option.widget->style() : QApplication::style();

QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
opt.icon = QIcon();
opt.text = QString();
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, option.widget);

QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
? QPalette::Normal : QPalette::Disabled;
if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
Expand All @@ -95,16 +99,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;

auto opt = option;
if (!(opt.state & QStyle::State_HasFocus))
opt.state &= ~(QStyle::State_MouseOver);
opt.rect = opt.rect.adjusted(textMarginCenter, 0, -textMarginCenter, 0);
Expand Down

0 comments on commit ffe2bba

Please sign in to comment.