mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
handle note deletion and creation friendly
Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
parent
2dd84bdc93
commit
6b59886847
@ -96,6 +96,7 @@ void VEditor::showFileEditMode()
|
||||
isEditMode = true;
|
||||
textEditor->beginEdit();
|
||||
setCurrentWidget(textEditor);
|
||||
textEditor->setFocus();
|
||||
}
|
||||
|
||||
bool VEditor::requestClose()
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,8 @@ public:
|
||||
|
||||
signals:
|
||||
void fileClicked(QJsonObject fileJson);
|
||||
void fileDeleted(QJsonObject fileJson);
|
||||
void fileCreated(QJsonObject fileJson);
|
||||
|
||||
private slots:
|
||||
void newFile();
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user