Skip to content

Commit

Permalink
fix: Replace ui with DTk when closing a window with unsaved changes
Browse files Browse the repository at this point in the history
Replace ui with DTk when closing a window with unsaved changes
  • Loading branch information
JWWTSL committed Jan 2, 2024
1 parent 8d659b4 commit 7aba19c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
41 changes: 32 additions & 9 deletions src/plugins/codeeditor/textedittabwidget/textedittabbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "common/common.h"
#include "services/editor/editorservice.h"

#include <DDialog>
#include <DToolButton>
#include <DMenu>

Expand All @@ -25,6 +26,7 @@ class TextEditTabBarPrivate
DToolButton *pbtHorizontal = nullptr;
DToolButton *pbtVertical = nullptr;
DToolButton *pbtClose = nullptr;
DDialog *removeDialog = nullptr;
};

TextEditTabBar::TextEditTabBar(QWidget *parent)
Expand Down Expand Up @@ -182,20 +184,41 @@ void TextEditTabBar::doFileSaved(const QString &file)
void TextEditTabBar::removeTab(const QString &file)
{
int index = fileIndex(file);

if (index != -1){
bool removeFlag = false;
auto okCallBack = [&]() {
removeFlag = true;
};

QString text = d->tab->tabText(index);
QFileInfo info(file);
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) {
emit saveFile(file);
}
d->removeDialog = new DDialog(this);
d->removeDialog->setIcon(QIcon::fromTheme("dialog-question"));
d->removeDialog->setAttribute(Qt::WA_DeleteOnClose);
d->removeDialog->setMessage(tr("The file has unsaved changes, will save?"));
d->removeDialog->insertButton(0, tr("Cancel"));
d->removeDialog->insertButton(1, tr("No Changes"));
d->removeDialog->insertButton(2, tr("Save Changes"), true, DDialog::ButtonRecommend);

connect(d->removeDialog, &DDialog::buttonClicked, [=](int index) {
if (index == 0) {
d->removeDialog->reject();
okCallBack();
} else if(index == 1) {
d->removeDialog->accept();
} else if(index == 2) {
d->removeDialog->accept();
emit saveFile(file);
}
});
d->removeDialog->exec();
}

if (removeFlag)
return;

emit fileClosed(file);
editor.closedFile(file);
d->tab->removeTab(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,11 @@ void TextEditTabWidget::removeFileTab(const QString &file)
void TextEditTabWidget::fileModifyed(const QString &file)
{
auto edit = d->textEdits[file];
if (edit && !edit->isHidden() && !edit->isSaveText()) {

if (!edit)
return;

if (!edit->isHidden() && !edit->isSaveText()) {

if (!d->titleBars[file]) {
d->titleBars[file] = TextEditTitleBar::changedReload(file);
Expand All @@ -594,7 +598,7 @@ void TextEditTabWidget::fileModifyed(const QString &file)
}
}

// 100 ms 内多次出发变动将忽略
// 100 ms 内多次触发变动将忽略
QTimer::singleShot(100, [=](){edit->cleanIsSaveText();});
}

Expand Down

0 comments on commit 7aba19c

Please sign in to comment.