Skip to content

Commit

Permalink
fix: [editor] The modified file cannot be saved when the tab page is …
Browse files Browse the repository at this point in the history
…closed

as title

Log: fix bug
Bug: https://pms.uniontech.com/bug-view-255827.html
  • Loading branch information
Kakueeen authored and deepin-mozart committed Jun 11, 2024
1 parent e06aef9 commit 988d051
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/plugins/codeeditor/gui/tabbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include "tabbar.h"
#include "private/tabbar_p.h"

#include "common/common.h"
#include "services/editor/editorservice.h"
#include "common/util/eventdefinitions.h"

#include <DMenu>
#include <DDialog>
#include <DDesktopServices>

#include <QSignalBlocker>
Expand Down Expand Up @@ -201,15 +201,19 @@ void TabBar::removeTab(const QString &fileName)
QString text = d->tabBar->tabText(index);
QFileInfo info(fileName);
if (info.exists() && text.length() > 0 && text.at(0) == "*") {
int ret = QMessageBox::question(this, QMessageBox::tr("Save Changes"),
QMessageBox::tr("The file has unsaved changes, will save?"),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
QMessageBox::Cancel);
if (QMessageBox::Yes != ret && QMessageBox::No != ret) {
return;
} else if (QMessageBox::Yes == ret) {
DDialog dialog(this);
dialog.setWindowTitle(tr("Save Changes"));
dialog.setIcon(QIcon::fromTheme("dialog-warning"));
dialog.setMessage(tr("The file has unsaved changes, will save?"));
dialog.addButton(tr("Save", "button"), true, DDialog::ButtonRecommend);
dialog.addButton(tr("Do Not Save", "button"));
dialog.addButton(tr("Cancel"), "button");

int ret = dialog.exec();
if (ret == 0) // save
emit saveFileRequested(fileName);
}
else if (ret == 2 || ret == -1) // cancel or close
return;
}

emit tabClosed(fileName);
Expand Down
7 changes: 7 additions & 0 deletions src/plugins/codeeditor/gui/tabwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ void TabWidgetPrivate::initConnection()
connect(tabBar, &TabBar::tabClosed, this, &TabWidgetPrivate::onTabClosed);
connect(tabBar, &TabBar::spliterClicked, this, &TabWidgetPrivate::onSpliterClicked);
connect(tabBar, &TabBar::closeRequested, q, &TabWidget::closeRequested);
connect(tabBar, &TabBar::saveFileRequested, q, &TabWidget::saveFile);

connect(EditorCallProxy::instance(), &EditorCallProxy::reqAddAnnotation, this, &TabWidgetPrivate::handleAddAnnotation);
connect(EditorCallProxy::instance(), &EditorCallProxy::reqRemoveAnnotation, this, &TabWidgetPrivate::handleRemoveAnnotation);
Expand Down Expand Up @@ -919,6 +920,12 @@ void TabWidget::gotoPosition(int line, int column)
editor->gotoPosition(editor->positionFromLineIndex(line, column));
}

void TabWidget::saveFile(const QString &fileName)
{
if (auto editor = d->findEditor(fileName))
editor->save();
}

void TabWidget::dragEnterEvent(QDragEnterEvent *event)
{
if (!event->mimeData()->hasUrls())
Expand Down
1 change: 1 addition & 0 deletions src/plugins/codeeditor/gui/tabwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public slots:
void removeDebugLine();
void gotoLine(int line);
void gotoPosition(int line, int column);
void saveFile(const QString &fileName);

signals:
void closeRequested();
Expand Down

0 comments on commit 988d051

Please sign in to comment.