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
10 changes: 9 additions & 1 deletion reader/MainWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd.
// Copyright (C) 2019 - 2026 Uniontech Software Technology Co.,Ltd.
// SPDX-FileCopyrightText: 2023 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
Expand Down Expand Up @@ -479,6 +479,14 @@ void MainWindow::resizeFullTitleWidget()
qCDebug(appLog) << "MainWindow::resizeFullTitleWidget() - Resize completed";
}

void MainWindow::setInitialActiveFile(const QString &filePath)
{
qCDebug(appLog) << "MainWindow::setInitialActiveFile - file:" << filePath;
if (m_central && !filePath.isEmpty()) {
m_central->docPage()->setActiveTabByFilePath(filePath);
}
}

MainWindow *MainWindow::windowContainSheet(DocSheet *sheet)
{
qCDebug(appLog) << "Finding window containing sheet:" << (sheet ? sheet->filePath() : "null");
Expand Down
9 changes: 8 additions & 1 deletion reader/MainWindow.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2023 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -70,6 +70,13 @@ class MainWindow : public Dtk::Widget::DMainWindow
*/
void resizeFullTitleWidget();

/**
* @brief setInitialActiveFile
* 设置初始打开后需要激活的文件,用于恢复上次激活的标签页
* @param filePath 文件路径
*/
void setInitialActiveFile(const QString &filePath);

/**
* @brief 更新TAB控件顺序
* @param orderlst
Expand Down
17 changes: 14 additions & 3 deletions reader/browser/SheetBrowser.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019 ~ 2026 Uniontech Software Technology Co.,Ltd.
// Copyright (C) 2019 - 2026 Uniontech Software Technology Co.,Ltd.
// SPDX-FileCopyrightText: 2023 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
Expand Down Expand Up @@ -222,6 +222,7 @@ void SheetBrowser::init(SheetOperation &operation, const QSet<int> &bookmarks)
m_hasLoaded = true;

qCInfo(appLog) << "SheetBrowser initialized successfully, initial page:" << m_initPage;
qCDebug(appLog) << "init done m_initPage=" << m_initPage << "currentPage=" << operation.currentPage << "hasLoaded=" << m_hasLoaded << "scaleMode=" << operation.scaleMode;
}

void SheetBrowser::setMouseShape(const Dr::MouseShape &shape)
Expand Down Expand Up @@ -693,6 +694,7 @@ void SheetBrowser::onRemoveIconAnnotSelect()

void SheetBrowser::onInit()
{
qCDebug(appLog) << "onInit enter m_initPage=" << m_initPage << "hasLoaded=" << m_hasLoaded;
qCDebug(appLog) << "SheetBrowser::onInit() - Starting init";
if (1 != m_initPage) {
setCurrentPage(m_initPage);
Expand Down Expand Up @@ -1086,7 +1088,7 @@ void SheetBrowser::deform(SheetOperation &operation)
setSceneRect(0, 0, maxWidth, maxHeight);

if (page > 0 && page <= m_items.count()) {
qCDebug(appLog) << "SheetBrowser::deform() - Setting scroll bar value for page:" << page;
qCDebug(appLog) << "deform set vbar page=" << page << "y=" << m_items[page - 1]->getTopLeftPos().y();
verticalScrollBar()->setValue(static_cast<int>(m_items[page - 1]->getTopLeftPos().y()));
horizontalScrollBar()->setValue(static_cast<int>(m_items[page - 1]->getTopLeftPos().x()));
}
Expand All @@ -1113,6 +1115,7 @@ bool SheetBrowser::hasLoaded()

void SheetBrowser::resizeEvent(QResizeEvent *event)
{
qCDebug(appLog) << "resizeEvent hasLoaded=" << m_hasLoaded << "scaleMode=" << (m_sheet ? m_sheet->operation().scaleMode : -1) << "newsize=" << event->size();
// qCDebug(appLog) << "SheetBrowser::resizeEvent() - Starting resize event";
if (hasLoaded() && m_sheet->operation().scaleMode != Dr::ScaleFactorMode) {
// qCDebug(appLog) << "SheetBrowser::resizeEvent() - Has loaded, deforming";
Expand Down Expand Up @@ -1725,6 +1728,7 @@ int SheetBrowser::currentScrollValueForPage()

void SheetBrowser::setCurrentPage(int page)
{
qCDebug(appLog) << "setCurrentPage(" << page << ") curPage=" << m_currentPage << "vbar[" << (verticalScrollBar() ? verticalScrollBar()->minimum() : -1) << (verticalScrollBar() ? verticalScrollBar()->maximum() : -1) << (verticalScrollBar() ? verticalScrollBar()->value() : -1) << "]";
// qCDebug(appLog) << "SheetBrowser::setCurrentPage() - Setting current page to:" << page;
if (page < 1 || page > allPages()) {
qCWarning(appLog) << "Invalid page number:" << page << "max pages:" << allPages();
Expand Down Expand Up @@ -1897,6 +1901,7 @@ void SheetBrowser::dragEnterEvent(QDragEnterEvent *event)

void SheetBrowser::showEvent(QShowEvent *event)
{
qCDebug(appLog) << "showEvent scheduling onInit +100ms";
// qCDebug(appLog) << "SheetBrowser::showEvent() - Showing event";
QTimer::singleShot(100, this, SLOT(onInit()));

Expand Down Expand Up @@ -2103,6 +2108,7 @@ void SheetBrowser::handleFindFinished(int searchcnt)

void SheetBrowser::curpageChanged(int curpage)
{
qCDebug(appLog) << "curpageChanged(" << curpage << ") prev=" << m_currentPage;
qCDebug(appLog) << "SheetBrowser::curpageChanged() - Current page changed";
if (m_currentPage != curpage) {
m_currentPage = curpage;
Expand Down Expand Up @@ -2441,11 +2447,16 @@ float SheetBrowser::getScrollPosition()

void SheetBrowser::restoreScrollPosition(float position)
{
qCDebug(appLog) << "Restoring scroll position:" << position;
QScrollBar *vBar = verticalScrollBar();
int vmin = vBar ? vBar->minimum() : -1;
int vmax = vBar ? vBar->maximum() : -1;
int vcur = vBar ? vBar->value() : -1;
qCDebug(appLog) << "restoreScrollPosition(" << position << ") vbar[" << vmin << vmax << vcur << "]";
if (!vBar || vBar->maximum() <= vBar->minimum()) {
qCDebug(appLog) << "restoreScrollPosition SKIP (range not ready)";
return;
}
int value = vBar->minimum() + static_cast<int>(position * (vBar->maximum() - vBar->minimum()));
vBar->setValue(value);
qCDebug(appLog) << "restoreScrollPosition set value=" << value << "-> after val=" << vBar->value();
}
50 changes: 40 additions & 10 deletions reader/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
#include <QCommandLineParser>
#include <QScreen>
#include <QAccessible>
#include <QDebug>

Check warning on line 24 in reader/main.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

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

Check warning on line 25 in reader/main.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

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

Check warning on line 26 in reader/main.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

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

Check warning on line 27 in reader/main.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

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

DGUI_USE_NAMESPACE
DWIDGET_USE_NAMESPACE
Expand Down Expand Up @@ -110,6 +111,7 @@
// 这是第一个实例(没有其他 deepin-reader 在运行),恢复上次的标签页组
// 不管命令行是否指定了文件,都恢复之前的标签页
// 注意:当前仅恢复 windowIndex=0 的标签页组(多窗口场景的完整恢复待后续优化)
QString initialActiveFile;
{
int savedActiveIndex = 0;
QStringList restoredFiles = Database::instance()->readTabGroup(0, savedActiveIndex);
Expand All @@ -132,21 +134,43 @@
localArguments.append(arg);
}

// 合并文件列表:先恢复历史标签页,再把用户指定的文件放到末尾(成为活动标签页)
// 如果用户指定的文件已在恢复列表中,先移除再追加到末尾
// 合并文件列表:保留历史标签页及其拖拽后的顺序,仅追加新打开的文件
// 这样重新打开相同文件时能保持上次的标签顺序;打开新文件时历史不会丢失
QStringList allFiles = validRestored;
int existingCount = 0;
QSet<QString> restoredSet(validRestored.begin(), validRestored.end());
int newFileCount = 0;
for (const QString &fp : localArguments) {
if (allFiles.removeAll(fp) > 0)
existingCount++;
if (QFile::exists(fp))
if (QFile::exists(fp) && !restoredSet.contains(fp)) {
allFiles.append(fp);
newFileCount++;
}
}

arguments = allFiles;
int newFileCount = localArguments.size() - existingCount;
qCInfo(appLog) << "Restoring" << validRestored.size() << "tabs from last session"
<< "+" << newFileCount << "new files";

// 计算需要激活的标签页:
// - 用户指定了文件时,激活用户指定的第一个有效文件(优先响应用户意图)
// - 用户未指定文件时,恢复上次激活的标签页(按保存时的路径定位)
if (!localArguments.isEmpty()) {
for (const QString &fp : localArguments) {
if (QFile::exists(fp) && restoredSet.contains(fp)) {
initialActiveFile = fp;
break;
}
}
// 指定的文件不在历史中(新追加的),激活最后一个新文件
if (initialActiveFile.isEmpty() && newFileCount > 0) {
initialActiveFile = allFiles.last();
}
} else if (savedActiveIndex >= 0 && savedActiveIndex < restoredFiles.size()) {
QString savedActivePath = restoredFiles.at(savedActiveIndex);
if (restoredSet.contains(savedActivePath))
initialActiveFile = savedActivePath;
}

int existingCount = allFiles.size() - newFileCount;
qCInfo(appLog) << "Restoring" << existingCount << "tabs from last session"
<< "+" << newFileCount << "new files"
<< "active:" << initialActiveFile;
}
}

Expand All @@ -169,6 +193,12 @@
MainWindow *w = MainWindow::createWindow(arguments);
w->winId();
qApp->setAttribute(Qt::AA_ForceRasterWidgets, false);

// 恢复上次激活的标签页(需在事件循环启动前设置,以覆盖 insertSheet 的延时 setCurrentIndex)
if (!initialActiveFile.isEmpty()) {
w->setInitialActiveFile(initialActiveFile);
}

w->show();

qCDebug(appLog) << __FUNCTION__ << "主窗口已创建并显示";
Expand Down
36 changes: 35 additions & 1 deletion reader/uiframe/CentralDocPage.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd.
// Copyright (C) 2019 - 2026 Uniontech Software Technology Co.,Ltd.
// SPDX-FileCopyrightText: 2023 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
Expand Down Expand Up @@ -533,6 +533,40 @@ QList<DocSheet *> CentralDocPage::getSheets()
return m_tabBar->getSheets();
}

void CentralDocPage::setActiveTabByFilePath(const QString &filePath)
{
qCInfo(appLog) << "setActiveTabByFilePath - file:" << filePath;
if (!m_tabBar || filePath.isEmpty())
return;

// 设置 m_delayIndex,使后续 onSetCurrentIndex 延时定时器激活正确的标签
m_tabBar->setPendingActiveFile(filePath);

int idx = m_tabBar->indexOfFilePath(filePath);
if (idx < 0)
return;

if (m_tabBar->currentIndex() != idx) {
// 标签栏索引不同,切换会触发 currentChanged → onTabChanged → 内容自动切换
m_tabBar->setCurrentIndex(idx);
} else {
// 标签栏索引已正确,但内容区可能不一致
// (addFileAsync 每次都调用 setCurrentWidget,导致内容区停留在最后一个文档)
// 此时 setCurrentIndex(idx) 不会触发 currentChanged,需要同步触发当前标签页选中文档的渲染
DocSheet *sheet = getSheet(filePath);
if (sheet && m_stackedLayout->currentWidget() != sheet) {
QPointer<DocSheet> prevSheet = qobject_cast<DocSheet *>(m_stackedLayout->currentWidget());
if (prevSheet && prevSheet != sheet && prevSheet->opened()) {
prevSheet->saveCurrentViewState();
}
m_stackedLayout->setCurrentWidget(sheet);
sheet->restoreSavedViewState();
sheet->defaultFocus();
emit sigCurSheetChanged(sheet);
}
}
}

bool CentralDocPage::saveCurrent()
{
qCDebug(appLog) << "Attempting to save current document";
Expand Down
9 changes: 8 additions & 1 deletion reader/uiframe/CentralDocPage.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd.
// Copyright (C) 2019 - 2026 Uniontech Software Technology Co.,Ltd.
// SPDX-FileCopyrightText: 2023 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
Expand Down Expand Up @@ -126,6 +126,13 @@ class CentralDocPage : public BaseWidget
*/
QList<DocSheet *> getSheets();

/**
* @brief setActiveTabByFilePath
* 设置需要激活的标签页(按文件路径),用于批量打开后恢复上次的激活位置
* @param filePath 文件路径
*/
void setActiveTabByFilePath(const QString &filePath);

/**
* @brief getCurSheet
* 返回当前显示的sheet,不存在返回nullptr
Expand Down
Loading
Loading