From 6b59886847d028a1fe59bb2265b308198a936955 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Fri, 21 Oct 2016 14:41:35 +0800 Subject: [PATCH] handle note deletion and creation friendly Signed-off-by: Le Tan --- veditor.cpp | 1 + vfilelist.cpp | 13 ++++++++++++- vfilelist.h | 2 ++ vmainwindow.cpp | 4 ++++ vtabwidget.cpp | 36 ++++++++++++++++++++++++++++++++++-- vtabwidget.h | 2 ++ 6 files changed, 55 insertions(+), 3 deletions(-) diff --git a/veditor.cpp b/veditor.cpp index e8e29e17..c3e7f8ff 100644 --- a/veditor.cpp +++ b/veditor.cpp @@ -96,6 +96,7 @@ void VEditor::showFileEditMode() isEditMode = true; textEditor->beginEdit(); setCurrentWidget(textEditor); + textEditor->setFocus(); } bool VEditor::requestClose() diff --git a/vfilelist.cpp b/vfilelist.cpp index 7e36b0d1..9385aae6 100644 --- a/vfilelist.cpp +++ b/vfilelist.cpp @@ -126,6 +126,13 @@ void VFileList::newFile() QListWidgetItem *newItem = createFileAndUpdateList(name, description); if (newItem) { this->setCurrentItem(newItem); + + // Open this file in edit mode + QJsonObject itemJson = newItem->data(Qt::UserRole).toJsonObject(); + Q_ASSERT(!itemJson.isEmpty()); + itemJson["path"] = QDir::cleanPath(QDir(rootPath).filePath(relativePath)); + itemJson["mode"] = 1; + emit fileCreated(itemJson); } } break; @@ -135,6 +142,7 @@ void VFileList::newFile() void VFileList::deleteFile() { QListWidgetItem *curItem = currentItem(); + Q_ASSERT(curItem); QJsonObject curItemJson = curItem->data(Qt::UserRole).toJsonObject(); QString curItemName = curItemJson["name"].toString(); @@ -145,6 +153,10 @@ void VFileList::deleteFile() msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); msgBox.setDefaultButton(QMessageBox::Ok); if (msgBox.exec() == QMessageBox::Ok) { + // First close this file forcely + curItemJson["path"] = QDir::cleanPath(QDir(rootPath).filePath(relativePath)); + emit fileDeleted(curItemJson); + deleteFileAndUpdateList(curItem); } } @@ -268,7 +280,6 @@ void VFileList::handleItemClicked(QListWidgetItem *currentItem) QJsonObject itemJson = currentItem->data(Qt::UserRole).toJsonObject(); Q_ASSERT(!itemJson.isEmpty()); itemJson["path"] = QDir::cleanPath(QDir(rootPath).filePath(relativePath)); - qDebug() << "click file:" << itemJson; emit fileClicked(itemJson); } diff --git a/vfilelist.h b/vfilelist.h index eac91bd6..8d33d076 100644 --- a/vfilelist.h +++ b/vfilelist.h @@ -15,6 +15,8 @@ public: signals: void fileClicked(QJsonObject fileJson); + void fileDeleted(QJsonObject fileJson); + void fileCreated(QJsonObject fileJson); private slots: void newFile(); diff --git a/vmainwindow.cpp b/vmainwindow.cpp index 002790de..8beeee73 100644 --- a/vmainwindow.cpp +++ b/vmainwindow.cpp @@ -84,6 +84,10 @@ void VMainWindow::setupUI() fileList, &VFileList::setDirectory); connect(fileList, &VFileList::fileClicked, tabs, &VTabWidget::openFile); + connect(fileList, &VFileList::fileDeleted, + tabs, &VTabWidget::closeFile); + connect(fileList, &VFileList::fileCreated, + tabs, &VTabWidget::openFile); connect(newNotebookBtn, &QPushButton::clicked, this, &VMainWindow::onNewNotebookBtnClicked); connect(deleteNotebookBtn, &QPushButton::clicked, diff --git a/vtabwidget.cpp b/vtabwidget.cpp index 21b043c8..bca4dbb7 100644 --- a/vtabwidget.cpp +++ b/vtabwidget.cpp @@ -11,6 +11,7 @@ VTabWidget::VTabWidget(QWidget *parent) : QTabWidget(parent) { setTabsClosable(true); + setMovable(true); connect(tabBar(), &QTabBar::tabCloseRequested, this, &VTabWidget::handleTabCloseRequest); @@ -48,17 +49,48 @@ void VTabWidget::openFile(QJsonObject fileJson) QString path = fileJson["path"].toString(); QString name = fileJson["name"].toString(); + int mode = 0; + if (fileJson.contains("mode")) { + mode = fileJson["mode"].toInt(); + } // Find if it has been opened already int idx = findTabByFile(path, name); if (idx > -1) { - setCurrentIndex(idx); - return; + goto out; } idx = openFileInTab(path, name, true); +out: setCurrentIndex(idx); + if (mode == 1) { + VEditor *editor = dynamic_cast(currentWidget()); + Q_ASSERT(editor); + editor->editFile(); + } +} + +void VTabWidget::closeFile(QJsonObject fileJson) +{ + if (fileJson.isEmpty()) { + return; + } + qDebug() << "close file:" << fileJson; + + QString path = fileJson["path"].toString(); + QString name = fileJson["name"].toString(); + + // Find if it has been opened already + int idx = findTabByFile(path, name); + if (idx == -1) { + return; + } + + QWidget* page = widget(idx); + Q_ASSERT(page); + removeTab(idx); + delete page; } int VTabWidget::openFileInTab(const QString &path, const QString &name, bool modifiable) diff --git a/vtabwidget.h b/vtabwidget.h index 3c5a05a6..60523763 100644 --- a/vtabwidget.h +++ b/vtabwidget.h @@ -15,6 +15,8 @@ signals: public slots: void openFile(QJsonObject fileJson); + // Close the file forcely + void closeFile(QJsonObject fileJson); void editFile(); void saveFile(); void readFile();