Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/source/common/uistruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define TEMPPATH DStandardPaths::writableLocation(QStandardPaths::TempLocation) // 临时路径(打开等操作)
#define MAINWINDOW_WIDTH_NAME "MainWindowWidthName" // 主界宽
#define MAINWINDOW_HEIGHT_NAME "MainWindowHeightName" // 主界面高
#define MAINWINDOW_STATE_NAME "MainWindowStateName" // 主界面窗口状态
#define ORDER_JSON "OrderJson" // 编辑json
#define ORDER_EDIT "edit" // 编辑权限
#define ORDER_RENAME "rename" // 重命名权限
Expand Down
61 changes: 50 additions & 11 deletions src/source/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,24 @@
#include <DWindowCloseButton>
#include <DWindowOptionButton>
#include <DArrowLineDrawer>
#include <DFontSizeManager>

Check warning on line 40 in src/source/mainwindow.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DFontSizeManager> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <denhancedwidget.h>

Check warning on line 41 in src/source/mainwindow.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <denhancedwidget.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <DSysInfo>

Check warning on line 42 in src/source/mainwindow.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DSysInfo> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#ifdef DTKCORE_CLASS_DConfigFile
#include <DConfig>

Check warning on line 44 in src/source/mainwindow.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DConfig> not found. Please note: Cppcheck does not need standard library headers to get proper results.
DCORE_USE_NAMESPACE
#endif

#include <QStackedWidget>

Check warning on line 48 in src/source/mainwindow.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QStackedWidget> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QKeyEvent>

Check warning on line 49 in src/source/mainwindow.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QKeyEvent> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QSettings>
#include <QDebug>
#include <QThreadPool>
#include <QtConcurrent/QtConcurrent>
#include <QScreen>
#include <QFormLayout>

Check warning on line 55 in src/source/mainwindow.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFormLayout> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QShortcut>

Check warning on line 56 in src/source/mainwindow.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QShortcut> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QJsonObject>

Check warning on line 57 in src/source/mainwindow.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QJsonObject> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#ifdef DTKCORE_CLASS_DConfigFile
#include <DConfig>
DCORE_USE_NAMESPACE
#endif

static QMutex mutex; // 静态全局变量只在定义该变量的源文件内有效
#define FILE_TRUNCATION_LENGTH 70
Expand Down Expand Up @@ -114,7 +114,7 @@
qDebug() << "MainWindow destructor started";
// 保存窗口大小状态
qDebug() << "Saving window size configuration";
saveConfigWinSize(width(), height());
saveConfigWinState();

qDebug() << "Destroying ArchiveManager instance";
ArchiveManager::get_instance()->destory_instance();
Expand Down Expand Up @@ -241,7 +241,7 @@
}

qDebug() << "Setting window size and minimum size";
resize(getConfigWinSize()); // 设置窗口尺寸
restoreConfigWinState(); // 恢复窗口状态(大小和最大化状态)
setMinimumSize(620, 300); // task 16309调整最小大小
qDebug() << "Application data initialized";
}
Expand Down Expand Up @@ -2459,15 +2459,54 @@
return QSize(winWidth, winHeight);
}

void MainWindow::saveConfigWinSize(int w, int h)
void MainWindow::saveConfigWinState()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider replacing custom window size and state management with Qt's built-in save/restore routines for geometry and state.

Consider dropping the custom size/state logic entirely and switch to Qt’s built-in save/restore routines. You can keep all of your functionality—size, position, maximized/minimized state—while removing ~100 lines of custom code and the QTimer hack. For example:

  1. In your initialization (after you create m_pSettings), replace

    restoreConfigWinState();

    with:

    if (m_pSettings->contains("geometry"))
        restoreGeometry(m_pSettings->value("geometry").toByteArray());
    if (m_pSettings->contains("windowState"))
        restoreState(m_pSettings->value("windowState").toByteArray());
  2. In your close or destructor (override closeEvent):

    void MainWindow::closeEvent(QCloseEvent *event) {
        m_pSettings->setValue("geometry", saveGeometry());
        m_pSettings->setValue("windowState", saveState());
        m_pSettings->sync();
        DMainWindow::closeEvent(event);
    }
  3. Remove these methods entirely:

    • getConfigWinSize()
    • saveConfigWinSize(...)
    • saveConfigWinState()
    • restoreConfigWinState()
    • All related constants and QTimer hacks

This preserves window placement, size constraints, and maximized state without manual branching.

{
int winWidth = w > MAINWINDOW_DEFAULTW ? w : MAINWINDOW_DEFAULTW;
int winHeight = h > MAINWINDOW_DEFAULTH ? h : MAINWINDOW_DEFAULTH;
m_pSettings->setValue(MAINWINDOW_HEIGHT_NAME, winHeight);
m_pSettings->setValue(MAINWINDOW_WIDTH_NAME, winWidth);
// 如果窗口是最大化状态,保存最大化标志和正常尺寸
if (isMaximized()) {
m_pSettings->setValue(MAINWINDOW_STATE_NAME, Qt::WindowMaximized);
// 对于最大化窗口,我们需要保存正常尺寸而不是最大化尺寸
// 使用默认尺寸或之前保存的正常尺寸
QVariant tempWidth = m_pSettings->value(MAINWINDOW_WIDTH_NAME);
QVariant tempHeight = m_pSettings->value(MAINWINDOW_HEIGHT_NAME);

if (!tempWidth.isValid() || !tempHeight.isValid()) {
// 如果没有之前保存的尺寸,使用默认尺寸
m_pSettings->setValue(MAINWINDOW_WIDTH_NAME, MAINWINDOW_DEFAULTW);
m_pSettings->setValue(MAINWINDOW_HEIGHT_NAME, MAINWINDOW_DEFAULTH);
}
// 如果有之前保存的尺寸,保持不变(不更新为最大化尺寸)
} else {
// 正常状态,保存当前尺寸和状态
m_pSettings->setValue(MAINWINDOW_STATE_NAME, Qt::WindowNoState);
// 保存窗口尺寸
int w = width();
int h = height();
int winWidth = w > MAINWINDOW_DEFAULTW ? w : MAINWINDOW_DEFAULTW;
int winHeight = h > MAINWINDOW_DEFAULTH ? h : MAINWINDOW_DEFAULTH;
m_pSettings->setValue(MAINWINDOW_HEIGHT_NAME, winHeight);
m_pSettings->setValue(MAINWINDOW_WIDTH_NAME, winWidth);
}
m_pSettings->sync();
}

void MainWindow::restoreConfigWinState()
{
// 获取保存的窗口状态
Qt::WindowState savedState = static_cast<Qt::WindowState>(
m_pSettings->value(MAINWINDOW_STATE_NAME, Qt::WindowNoState).toInt());

// 先设置正常尺寸
resize(getConfigWinSize());

// 然后应用状态
if (savedState == Qt::WindowMaximized) {
// 使用QTimer延迟执行,确保窗口完全初始化后再最大化
QTimer::singleShot(0, this, [this]() {
showMaximized();
});
}
}

void MainWindow::convertArchive(const QString &convertType)
{
qInfo() << "对压缩包进行格式转换" << convertType;
Expand Down
17 changes: 10 additions & 7 deletions src/source/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ public :
*/
void handleQuit();

/**
* @brief saveConfigWinState 保存窗口状态(大小和最大化状态)
*/
void saveConfigWinState();

/**
* @brief restoreConfigWinState 恢复窗口状态(大小和最大化状态)
*/
void restoreConfigWinState();

private:
/**
* @brief initUI 初始化界面
Expand Down Expand Up @@ -224,13 +234,6 @@ public :
*/
QSize getConfigWinSize();

/**
* @brief saveConfigWinSize 保存窗口尺寸
* @param w 宽度
* @param h 高度
*/
void saveConfigWinSize(int w, int h);

/**
* @brief convertArchive 格式转换
* @param convertType 转换后的文件类型
Expand Down
7 changes: 0 additions & 7 deletions tests/UnitTest/src/source/ut_mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1555,13 +1555,6 @@ TEST_F(UT_MainWindow, test_getConfigWinSize)
EXPECT_NE(m_tester->getConfigWinSize().width(), 0);
}

TEST_F(UT_MainWindow, testsaveConfigWinSize)
{
m_tester->saveConfigWinSize(800, 600);
EXPECT_EQ(m_tester->m_pSettings->value(MAINWINDOW_WIDTH_NAME).toInt(), 800);
EXPECT_EQ(m_tester->m_pSettings->value(MAINWINDOW_HEIGHT_NAME).toInt(), 600);
}

TEST_F(UT_MainWindow, test_convertArchive)
{
Stub stub;
Expand Down