close all files before closing app

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-11-21 22:07:01 +08:00
parent 1ed324fd20
commit 3722e6cb71
8 changed files with 47 additions and 20 deletions

View File

@ -196,6 +196,19 @@ bool VEditArea::closeFile(QJsonObject fileJson)
return ret; return ret;
} }
bool VEditArea::closeAllFiles(bool p_forced)
{
int nrWin = splitter->count();
for (int i = 0; i < nrWin; ++i) {
VEditWindow *win = getWindow(i);
setCurrentWindow(i, false);
if (!win->closeAllFiles(p_forced)) {
return false;
}
}
return true;
}
void VEditArea::editFile() void VEditArea::editFile()
{ {
VEditWindow *win = getWindow(curWindowIndex); VEditWindow *win = getWindow(curWindowIndex);
@ -267,7 +280,7 @@ void VEditArea::handleSplitWindowRequest(VEditWindow *curWindow)
void VEditArea::handleRemoveSplitRequest(VEditWindow *curWindow) void VEditArea::handleRemoveSplitRequest(VEditWindow *curWindow)
{ {
if (!curWindow) { if (!curWindow || splitter->count() <= 1) {
return; return;
} }
int idx = splitter->indexOf(curWindow); int idx = splitter->indexOf(curWindow);

View File

@ -22,6 +22,7 @@ class VEditArea : public QWidget
public: public:
explicit VEditArea(VNote *vnote, QWidget *parent = 0); explicit VEditArea(VNote *vnote, QWidget *parent = 0);
bool isFileOpened(const QString &notebook, const QString &relativePath); bool isFileOpened(const QString &notebook, const QString &relativePath);
bool closeAllFiles(bool p_forced);
signals: signals:
void curTabStatusChanged(const QString &notebook, const QString &relativePath, void curTabStatusChanged(const QString &notebook, const QString &relativePath,

View File

@ -359,6 +359,7 @@ void VEditTab::scrollToAnchor(const VAnchor &anchor)
document.scrollToAnchor(anchor.anchor.mid(1)); document.scrollToAnchor(anchor.anchor.mid(1));
} }
} }
curHeader = anchor;
} }
void VEditTab::updateCurHeader(const QString &anchor) void VEditTab::updateCurHeader(const QString &anchor)

View File

@ -71,7 +71,7 @@ void VEditWindow::removeSplit()
{ {
// Close all the files one by one // Close all the files one by one
// If user do not want to close a file, just stop removing // If user do not want to close a file, just stop removing
if (closeAllFiles()) { if (closeAllFiles(false)) {
Q_ASSERT(count() == 0); Q_ASSERT(count() == 0);
emit requestRemoveSplit(this); emit requestRemoveSplit(this);
} }
@ -124,18 +124,21 @@ out:
return idx; return idx;
} }
// Return true if we closed the file
bool VEditWindow::closeFile(const QString &notebook, const QString &relativePath, bool isForced) bool VEditWindow::closeFile(const QString &notebook, const QString &relativePath, bool isForced)
{ {
// Find if it has been opened already // Find if it has been opened already
int idx = findTabByFile(notebook, relativePath); int idx = findTabByFile(notebook, relativePath);
if (idx == -1) { if (idx == -1) {
return true; return false;
} }
VEditTab *editor = getTab(idx); VEditTab *editor = getTab(idx);
Q_ASSERT(editor); Q_ASSERT(editor);
bool ok = true; bool ok = true;
if (!isForced) { if (!isForced) {
setCurrentIndex(idx);
noticeStatus(idx);
ok = editor->requestClose(); ok = editor->requestClose();
} }
if (ok) { if (ok) {
@ -146,16 +149,31 @@ bool VEditWindow::closeFile(const QString &notebook, const QString &relativePath
return ok; return ok;
} }
bool VEditWindow::closeAllFiles() bool VEditWindow::closeAllFiles(bool p_forced)
{ {
int nrTab = count(); int nrTab = count();
bool ret = true;
for (int i = 0; i < nrTab; ++i) { for (int i = 0; i < nrTab; ++i) {
// Always close the first tab VEditTab *editor = getTab(0);
if (!handleTabCloseRequest(0)) { bool ok = true;
return false; if (!p_forced) {
setCurrentIndex(0);
noticeStatus(0);
ok = editor->requestClose();
}
if (ok) {
removeTab(0);
delete editor;
} else {
ret = false;
break;
} }
} }
return true; updateTabListMenu();
if (ret) {
emit requestRemoveSplit(this);
}
return ret;
} }
int VEditWindow::openFileInTab(const QString &notebook, const QString &relativePath, int VEditWindow::openFileInTab(const QString &notebook, const QString &relativePath,

View File

@ -29,7 +29,7 @@ public:
void saveAndReadFile(); void saveAndReadFile();
void handleNotebookRenamed(const QVector<VNotebook> &notebooks, const QString &oldName, void handleNotebookRenamed(const QVector<VNotebook> &notebooks, const QString &oldName,
const QString &newName); const QString &newName);
bool closeAllFiles(); bool closeAllFiles(bool p_forced);
void setRemoveSplitEnable(bool enabled); void setRemoveSplitEnable(bool enabled);
void requestUpdateTabStatus(); void requestUpdateTabStatus();
void requestUpdateOutline(); void requestUpdateOutline();

View File

@ -800,6 +800,11 @@ void VMainWindow::deleteCurNote()
void VMainWindow::closeEvent(QCloseEvent *event) void VMainWindow::closeEvent(QCloseEvent *event)
{ {
if (!editArea->closeAllFiles(false)) {
// Fail to close all the opened files, cancel closing app
event->ignore();
return;
}
saveStateAndGeometry(); saveStateAndGeometry();
QMainWindow::closeEvent(event); QMainWindow::closeEvent(event);
} }

View File

@ -89,9 +89,6 @@ void VOutline::handleItemClicked(QTreeWidgetItem *item, int column)
void VOutline::updateCurHeader(const VAnchor &anchor) void VOutline::updateCurHeader(const VAnchor &anchor)
{ {
if (anchor == curHeader) {
return;
}
curHeader = anchor; curHeader = anchor;
if (outline.type == VHeaderType::Anchor) { if (outline.type == VHeaderType::Anchor) {
selectAnchor(anchor.anchor); selectAnchor(anchor.anchor);

View File

@ -26,19 +26,11 @@ struct VAnchor
VAnchor() : lineNumber(-1) {} VAnchor() : lineNumber(-1) {}
VAnchor(const QString filePath, const QString &anchor, int lineNumber) VAnchor(const QString filePath, const QString &anchor, int lineNumber)
: filePath(filePath), anchor(anchor), lineNumber(lineNumber) {} : filePath(filePath), anchor(anchor), lineNumber(lineNumber) {}
inline bool operator ==(const VAnchor &p_anchor) const;
QString filePath; QString filePath;
QString anchor; QString anchor;
int lineNumber; int lineNumber;
}; };
inline bool VAnchor::operator ==(const VAnchor &p_anchor) const
{
return p_anchor.filePath == filePath &&
p_anchor.anchor == anchor &&
p_anchor.lineNumber == lineNumber;
}
class VToc class VToc
{ {
public: public: