Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Replace ui with DTk when closing a window with unsaved changes #304

Merged
merged 1 commit into from
Jan 3, 2024
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
51 changes: 36 additions & 15 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,24 +184,43 @@ void TextEditTabBar::doFileSaved(const QString &file)
void TextEditTabBar::removeTab(const QString &file)
{
int index = fileIndex(file);
if (index != -1){
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) {

if (index == -1)
return;

bool cancelFlag = false;

QString text = d->tab->tabText(index);
QFileInfo info(file);
if (info.exists() && text.length() > 0 && text.at(0) == "*") {
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();
cancelFlag = true;
} else if(index == 1) {
d->removeDialog->accept();
} else if(index == 2) {
d->removeDialog->accept();
emit saveFile(file);
}
}
emit fileClosed(file);
editor.closedFile(file);
d->tab->removeTab(index);
});
d->removeDialog->exec();
}

if (cancelFlag)
return;

emit fileClosed(file);
editor.closedFile(file);
d->tab->removeTab(index);
}

int TextEditTabBar::count() const
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