Преглед изворни кода

handle note deletion and creation friendly

Signed-off-by: Le Tan <[email protected]>
Le Tan пре 9 година
родитељ
комит
6b59886847
6 измењених фајлова са 55 додато и 3 уклоњено
  1. 1 0
      veditor.cpp
  2. 12 1
      vfilelist.cpp
  3. 2 0
      vfilelist.h
  4. 4 0
      vmainwindow.cpp
  5. 34 2
      vtabwidget.cpp
  6. 2 0
      vtabwidget.h

+ 1 - 0
veditor.cpp

@@ -96,6 +96,7 @@ void VEditor::showFileEditMode()
     isEditMode = true;
     textEditor->beginEdit();
     setCurrentWidget(textEditor);
+    textEditor->setFocus();
 }
 
 bool VEditor::requestClose()

+ 12 - 1
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);
 }
 

+ 2 - 0
vfilelist.h

@@ -15,6 +15,8 @@ public:
 
 signals:
     void fileClicked(QJsonObject fileJson);
+    void fileDeleted(QJsonObject fileJson);
+    void fileCreated(QJsonObject fileJson);
 
 private slots:
     void newFile();

+ 4 - 0
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,

+ 34 - 2
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<VEditor *>(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)

+ 2 - 0
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();