Skip to content

Commit

Permalink
feat: QT6.8 adaptation
Browse files Browse the repository at this point in the history
QT6.8 adaptation

Log: QT6.8 adaptation
  • Loading branch information
pengfeixx committed Jan 9, 2025
1 parent 77bc677 commit 3dc365b
Show file tree
Hide file tree
Showing 63 changed files with 528 additions and 122 deletions.
6 changes: 0 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug")
ADD_SUBDIRECTORY(tests)
endif()

find_package(Qt5LinguistTools REQUIRED)
#lupdate start
#此处其实只有当没有自动翻译需要手动翻译.ts文件才有意义可以创建不同语言名称的ts文件,下同
set(TS_FILES
Expand All @@ -55,11 +54,6 @@ foreach(_ts_file ${DTNG_TS_FILES})
endforeach()
#lrelease end

qt5_create_translation(DTNG_QM_FILES
${DTNG_TS_FILES}
${DTNG_QM_FILES}
)

#根据环境修改/usr 目录
if(DEFINED ENV{PREFIX})
set(CMAKE_INSTALL_PREFIX $ENV{PREFIX})
Expand Down
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Source: com.deepin.gomoku
Section: utils
Priority: optional
Maintainer: Deepin Packages Builder <packages@deepin.com>
Build-Depends: debhelper (>= 11),cmake, pkg-config, qtbase5-dev, qtchooser (>= 55-gc9562a1-1~),qt5-qmake, libdtkwidget-dev, qttools5-dev-tools, libqt5svg5-dev, qttools5-dev,libgtest-dev, libgmock-dev, qtmultimedia5-dev
Build-Depends: debhelper (>= 11),cmake, pkg-config, qt6-base-dev, qtchooser (>= 55-gc9562a1-1~),qmake6, libdtk6widget-dev, qt6-tools-dev-tools, qt6-svg-dev, qt6-tools-dev,libgtest-dev, libgmock-dev, qt6-multimedia-dev
Standards-Version: 4.3.0

Package: com.deepin.gomoku
Expand Down
2 changes: 1 addition & 1 deletion debian/rules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/make -f

export QT_SELECT=5
export QT_SELECT=6
include /usr/share/dpkg/default.mk

DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
Expand Down
35 changes: 20 additions & 15 deletions gomoku/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_FLAGS "-Wl,--as-needed -fPIE")
set(CMAKE_EXE_LINKER_FLAGS "-pie")
set(QT_VERSION_MAJOR 6)

#compile flags
if (CMAKE_BUILD_TYPE MATCHES Debug)
Expand Down Expand Up @@ -59,17 +60,21 @@ foreach(subdir ${all_src})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/${subdir})
endforeach()

if (${QT_VERSION_MAJOR} GREATER_EQUAL 6)
set(DTK_VERSION 6)
endif()

file(GLOB_RECURSE Gomoku_SRC ${CMAKE_CURRENT_LIST_DIR}/*.cpp)

find_package(PkgConfig REQUIRED)
find_package(DtkCore REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Svg REQUIRED)
find_package(Qt5DBus REQUIRED)
find_package(Qt5 COMPONENTS Multimedia REQUIRED)
find_package(DtkWidget REQUIRED)
find_package(DtkGui REQUIRED)
find_package(Qt${QT_VERSION_MAJOR}Gui REQUIRED)
find_package(Qt${QT_VERSION_MAJOR}Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR}Svg REQUIRED)
find_package(Qt${QT_VERSION_MAJOR}DBus REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Multimedia REQUIRED)
find_package(Dtk${DTK_VERSION}Core REQUIRED)
find_package(Dtk${DTK_VERSION}Widget REQUIRED)
find_package(Dtk${DTK_VERSION}Gui REQUIRED)

include_directories(${Qt5Gui_PRIVATE_INCLUDE_DIRS})

Expand All @@ -79,14 +84,14 @@ ADD_EXECUTABLE(${PROJECT_NAME} ${Gomoku_SRC} ${APP_QRC})
target_include_directories(${PROJECT_NAME} PUBLIC ${DtkWidget_INCLUDE_DIRS} ${OBJECT_BINARY_DIR})
#添加链接库
target_link_libraries(${PROJECT_NAME}
${DtkWidget_LIBRARIES}
${DtkCore_LIBRARIES}
${DtkGui_LIBRARIES}
${Qt5Widgets_LIBRARIES}
${Qt5Svg_LIBRARIES}
${Qt5DBus_LIBRARIES}
Dtk${DTK_VERSION}::Core
Dtk${DTK_VERSION}::Gui
Dtk${DTK_VERSION}::Widget
Qt${QT_VERSION_MAJOR}::Widgets
Qt${QT_VERSION_MAJOR}::Svg
Qt${QT_VERSION_MAJOR}::DBus
Qt${QT_VERSION_MAJOR}::Multimedia
${DFrameworkdbus_LIBRARIES}
Qt5::Multimedia
)

#根据环境修改/usr 目录
Expand Down
11 changes: 10 additions & 1 deletion gomoku/src/globaltool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ QString Globaltool::AutoFeed(const QString &text, const int fontSize, const int
labelF.setWeight(QFont::Black);
labelF.setPixelSize(fontSize);
QFontMetrics fm(labelF);
#if QT_VERSION_MAJOR > 5
int titlewidth = fm.boundingRect(strText).width();
#else
int titlewidth = fm.width(strText);
#endif
QStringList strList;
QString str;
strList.clear();
Expand All @@ -87,7 +91,12 @@ QString Globaltool::AutoFeed(const QString &text, const int fontSize, const int
for (int i = 0; i < strText.count(); i++) {
str += strText.at(i);

if (fm.width(str) > textWidth) {
#if QT_VERSION_MAJOR > 5
int strWidth = fm.boundingRect(strText).width();
#else
int strWidth = fm.width(strText);
#endif
if (strWidth > textWidth) {
str.remove(str.count() - 1, 1);
strList.append(str);
resultStr += str + "\n";
Expand Down
4 changes: 4 additions & 0 deletions gomoku/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#include <DMainWindow>
#include <DWidgetUtil>
#include <DGuiApplicationHelper>
#if QT_VERSION_MAJOR <= 5
#include <DApplicationSettings>
#endif
#include <DLog>

#include <QAccessible>
Expand Down Expand Up @@ -61,8 +63,10 @@ int main(int argc, char *argv[])
DLogManager::registerConsoleAppender();
//log输出到日志文件
DLogManager::registerFileAppender();
#if QT_VERSION_MAJOR <= 5
//保存主题
DApplicationSettings applicationset(app);
#endif
//加载系统语言
Globaltool::instacne()->loadSystemLanguage();
GomokuMainWindow ww;
Expand Down
8 changes: 7 additions & 1 deletion gomoku/src/widget/buttonfunction/buttonitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ void ButtonItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)

void ButtonItem::setElidedText(QString &text, QFontMetrics &fontMetrics, const int textWidth)
{
if (fontMetrics.width(text) > (textWidth - 8)) { //判断字符串是否超出长度
int fontWidth;
#if QT_VERSION_MAJOR > 5
fontWidth = fontMetrics.boundingRect(text).width();
#else
fontWidth = fontMetrics.width(text);
#endif
if (fontWidth > (textWidth - 8)) { //判断字符串是否超出长度
setToolTip(text);
text = fontMetrics.elidedText(text, Qt::ElideRight, textWidth - 16); //超出后截断用省略号显示
} else {
Expand Down
12 changes: 12 additions & 0 deletions gomoku/src/widget/checkerboard/checkerboardscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@

CheckerboardScene::CheckerboardScene(qreal x, qreal y, qreal width, qreal height, QObject *parent)
: QGraphicsScene(parent)
#if QT_VERSION_MAJOR > 5
,m_player(new QMediaPlayer(this))
,m_audoiOutput(new QAudioOutput(this))
#elif
, playChessSound(new QSound(":/resources/music/chessone.wav", this))
#endif
, cbitem(new CheckerboardItem)
, buttonStartPause(new BTStartPause)
, buttonReplay(new BTReplay)
Expand All @@ -24,6 +29,10 @@ CheckerboardScene::CheckerboardScene(qreal x, qreal y, qreal width, qreal height
, gameControl(new GameControl(aiChessColor, userChessColor))
, AIChess(-1, -1, 0)
{
#if QT_VERSION_MAJOR > 5
m_player->setAudioOutput(m_audoiOutput);
m_player->setSource(QUrl("qrc:/resources/music/chessone.wav"));
#endif
//设置scene大小
setSceneRect(x, y, width, height);
addItem(cbitem);
Expand Down Expand Up @@ -272,7 +281,10 @@ void CheckerboardScene::slotCPaintItem(ChessItem *cItem)
qInfo() << __FUNCTION__ << "music play statues: " << musicControlStatus;
if (musicControlStatus) {
//播放落子音效
#if QT_VERSION_MAJOR > 5
#else
playChessSound->play();
#endif
}
chessHasPaint[i][j] = true;
qInfo() << __FUNCTION__ << "current chess pos: " << i << j;
Expand Down
9 changes: 9 additions & 0 deletions gomoku/src/widget/checkerboard/checkerboardscene.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#include "gamecontrol/gamecontrol.h"

#include <QGraphicsScene>
#if QT_VERSION_MAJOR > 5
#include <QMediaPlayer>
#include <QAudioOutput>
#endif

class CheckerboardScene : public QGraphicsScene
{
Expand Down Expand Up @@ -72,7 +76,12 @@ private slots:
void slotCPaintItem(ChessItem *cItem);

private:
#if QT_VERSION_MAJOR > 5
QMediaPlayer *m_player = nullptr;
QAudioOutput *m_audoiOutput = nullptr;
#elif
QSound *playChessSound = nullptr;
#endif
QVector<QVector<ChessItem *>> chessItemList{}; //棋子数组
bool chessHasPaint[13][13] = {{false}}; //棋子坐标数组,保存每个位置是否绘制棋子
CheckerboardItem *cbitem = nullptr; //棋盘item
Expand Down
2 changes: 2 additions & 0 deletions gomoku/src/widget/chess/chessitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include <DHiDPIHelper>

#include <QGraphicsItem>
#if QT_VERSION_MAJOR <= 5
#include <QtMultimedia/QSound>
#endif

DWIDGET_USE_NAMESPACE

Expand Down
19 changes: 17 additions & 2 deletions gomoku/src/widget/exitdialog/cancelbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ void CancelButton::mouseReleaseEvent(QMouseEvent *event)
* @param event
* 移入事件
*/
#if QT_VERSION_MAJOR > 5
void CancelButton::enterEvent(QEnterEvent *event)
#else
void CancelButton::enterEvent(QEvent *event)
#endif
{
currentPixmap = buttonHover;
DWidget::enterEvent(event);
Expand Down Expand Up @@ -105,11 +109,22 @@ void CancelButton::paintEvent(QPaintEvent *event)
painter.setFont(font);
QFontMetrics fontMetrics(font);
QString text = tr("Keep Playing");
if (fontMetrics.width(text) > (this->rect().width() - 20)) { //判断字符串是否超出长度
int fontWidth;
#if QT_VERSION_MAJOR > 5
fontWidth = fontMetrics.boundingRect(text).width();
#else
fontWidth = fontMetrics.width(text);
#endif
if (fontWidth > (this->rect().width() - 20)) { //判断字符串是否超出长度
setToolTip(text);
text = fontMetrics.elidedText(text, Qt::ElideRight, this->rect().width() - 20);//超出后截断用省略号显示
}
painter.drawText(QPointF((rect().width() - fontMetrics.width(text)) / 2,
#if QT_VERSION_MAJOR > 5
fontWidth = fontMetrics.boundingRect(text).width();
#else
fontWidth = fontMetrics.width(text);
#endif
painter.drawText(QPointF((rect().width() - fontWidth) / 2,
(rect().height() - fontMetrics.height()) / 2 + fontMetrics.ascent() - fontMetrics.descent() / 4), text);

// painter.drawText(this->rect(), Qt::AlignHCenter | Qt::AlignVCenter, text);
Expand Down
4 changes: 4 additions & 0 deletions gomoku/src/widget/exitdialog/cancelbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ class CancelButton : public QWidget

private:
void mouseReleaseEvent(QMouseEvent *event);
#if QT_VERSION_MAJOR > 5
void enterEvent(QEnterEvent *event);
#else
void enterEvent(QEvent *event);
#endif
void leaveEvent(QEvent *event);
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);
Expand Down
19 changes: 17 additions & 2 deletions gomoku/src/widget/exitdialog/exitbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ void ExitButton::mouseReleaseEvent(QMouseEvent *event)
* @param event
* 移入事件
*/
#if QT_VERSION_MAJOR > 5
void ExitButton::enterEvent(QEnterEvent *event)
#else
void ExitButton::enterEvent(QEvent *event)
#endif
{
currentPixmap = buttonHover;
DWidget::enterEvent(event);
Expand Down Expand Up @@ -103,11 +107,22 @@ void ExitButton::paintEvent(QPaintEvent *event)
painter.setFont(font);
QFontMetrics fontMetrics(font);
QString text = tr("Exit");
if (fontMetrics.width(text) > (this->rect().width() - 20)) {
int fontWidth;
#if QT_VERSION_MAJOR > 5
fontWidth = fontMetrics.boundingRect(text).width();
#else
fontWidth = fontMetrics.width(text);
#endif
if (fontWidth > (this->rect().width() - 20)) {
setToolTip(text);
text = fontMetrics.elidedText(text, Qt::ElideRight, this->rect().width() - 20);
}
painter.drawText(QPointF((rect().width() - fontMetrics.width(text)) / 2,
#if QT_VERSION_MAJOR > 5
fontWidth = fontMetrics.boundingRect(text).width();
#else
fontWidth = fontMetrics.width(text);
#endif
painter.drawText(QPointF((rect().width() - fontWidth) / 2,
(rect().height() - fontMetrics.height()) / 2 + fontMetrics.ascent() - fontMetrics.descent() / 4), text);
// painter.drawText(this->rect(), Qt::AlignHCenter | Qt::AlignVCenter, text);
painter.restore();
Expand Down
4 changes: 4 additions & 0 deletions gomoku/src/widget/exitdialog/exitbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ Q_OBJECT

private:
void mouseReleaseEvent(QMouseEvent *event);
#if QT_VERSION_MAJOR > 5
void enterEvent(QEnterEvent *event);
#else
void enterEvent(QEvent *event);
#endif
void leaveEvent(QEvent *event);
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);
Expand Down
27 changes: 22 additions & 5 deletions gomoku/src/widget/gomokumainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ GomokuMainWindow::GomokuMainWindow(QWidget *parent)

initCompositingStatus();
initUI();
#if QT_VERSION_MAJOR > 5
m_player = new QMediaPlayer(this);
m_audioOutput = new QAudioOutput(this);
m_player->setAudioOutput(m_audioOutput);
#endif

Dtk::Widget::moveToCenter(this);
}
Expand Down Expand Up @@ -91,7 +96,7 @@ void GomokuMainWindow::initUI()

//tooltip背景颜色
QPalette palette;
palette.setColor(QPalette::Background, QColor(Qt::black));
palette.setColor(QPalette::Window, QColor(Qt::black));

//tooltip设置字体大小和颜色
QToolTip::setFont(font);
Expand Down Expand Up @@ -141,17 +146,29 @@ void GomokuMainWindow::paintTitleBar(QWidget *titlebar)
*/
void GomokuMainWindow::playWinMusic()
{
if (checkerboardScene->getMusicPlay())
if (checkerboardScene->getMusicPlay()) {
#if QT_VERSION_MAJOR > 5
m_player->setSource(QUrl("qrc:/resources/music/win.wav"));
m_player->play();
#else
QSound::play(":/resources/music/win.wav");
#endif
}
}

/**
* @brief playWinMusic 播放胜利音乐
*/
void GomokuMainWindow::playFailMusic()
{
if (checkerboardScene->getMusicPlay())
if (checkerboardScene->getMusicPlay()) {
#if QT_VERSION_MAJOR > 5
m_player->setSource(QUrl("qrc:/resources/music/fail.wav"));
m_player->play();
#else
QSound::play(":/resources/music/fail.wav");
#endif
}
}

/**
Expand All @@ -162,9 +179,9 @@ void GomokuMainWindow::viewtransparentFrame()
DPalette tframepa = m_transparentFrame->palette();
QColor tColor = "#000000";
tColor.setAlphaF(0.3);
tframepa.setColor(DPalette::Background, tColor);
tframepa.setColor(DPalette::Window, tColor);
m_transparentFrame->setPalette(tframepa);
m_transparentFrame->setBackgroundRole(DPalette::Background);
m_transparentFrame->setBackgroundRole(DPalette::Window);
m_transparentFrame->resize(this->width(), this->height());
m_transparentFrame->show();
}
Expand Down
Loading

0 comments on commit 3dc365b

Please sign in to comment.