mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 22:09: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;
|
isEditMode = true;
|
||||||
textEditor->beginEdit();
|
textEditor->beginEdit();
|
||||||
setCurrentWidget(textEditor);
|
setCurrentWidget(textEditor);
|
||||||
|
textEditor->setFocus();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool VEditor::requestClose()
|
bool VEditor::requestClose()
|
||||||
|
@ -126,6 +126,13 @@ void VFileList::newFile()
|
|||||||
QListWidgetItem *newItem = createFileAndUpdateList(name, description);
|
QListWidgetItem *newItem = createFileAndUpdateList(name, description);
|
||||||
if (newItem) {
|
if (newItem) {
|
||||||
this->setCurrentItem(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;
|
break;
|
||||||
@ -135,6 +142,7 @@ void VFileList::newFile()
|
|||||||
void VFileList::deleteFile()
|
void VFileList::deleteFile()
|
||||||
{
|
{
|
||||||
QListWidgetItem *curItem = currentItem();
|
QListWidgetItem *curItem = currentItem();
|
||||||
|
Q_ASSERT(curItem);
|
||||||
QJsonObject curItemJson = curItem->data(Qt::UserRole).toJsonObject();
|
QJsonObject curItemJson = curItem->data(Qt::UserRole).toJsonObject();
|
||||||
QString curItemName = curItemJson["name"].toString();
|
QString curItemName = curItemJson["name"].toString();
|
||||||
|
|
||||||
@ -145,6 +153,10 @@ void VFileList::deleteFile()
|
|||||||
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
|
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
|
||||||
msgBox.setDefaultButton(QMessageBox::Ok);
|
msgBox.setDefaultButton(QMessageBox::Ok);
|
||||||
if (msgBox.exec() == 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);
|
deleteFileAndUpdateList(curItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -268,7 +280,6 @@ void VFileList::handleItemClicked(QListWidgetItem *currentItem)
|
|||||||
QJsonObject itemJson = currentItem->data(Qt::UserRole).toJsonObject();
|
QJsonObject itemJson = currentItem->data(Qt::UserRole).toJsonObject();
|
||||||
Q_ASSERT(!itemJson.isEmpty());
|
Q_ASSERT(!itemJson.isEmpty());
|
||||||
itemJson["path"] = QDir::cleanPath(QDir(rootPath).filePath(relativePath));
|
itemJson["path"] = QDir::cleanPath(QDir(rootPath).filePath(relativePath));
|
||||||
qDebug() << "click file:" << itemJson;
|
|
||||||
emit fileClicked(itemJson);
|
emit fileClicked(itemJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,6 +15,8 @@ public:
|
|||||||
|
|
||||||
signals:
|
signals:
|
||||||
void fileClicked(QJsonObject fileJson);
|
void fileClicked(QJsonObject fileJson);
|
||||||
|
void fileDeleted(QJsonObject fileJson);
|
||||||
|
void fileCreated(QJsonObject fileJson);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void newFile();
|
void newFile();
|
||||||
|
@ -84,6 +84,10 @@ void VMainWindow::setupUI()
|
|||||||
fileList, &VFileList::setDirectory);
|
fileList, &VFileList::setDirectory);
|
||||||
connect(fileList, &VFileList::fileClicked,
|
connect(fileList, &VFileList::fileClicked,
|
||||||
tabs, &VTabWidget::openFile);
|
tabs, &VTabWidget::openFile);
|
||||||
|
connect(fileList, &VFileList::fileDeleted,
|
||||||
|
tabs, &VTabWidget::closeFile);
|
||||||
|
connect(fileList, &VFileList::fileCreated,
|
||||||
|
tabs, &VTabWidget::openFile);
|
||||||
connect(newNotebookBtn, &QPushButton::clicked,
|
connect(newNotebookBtn, &QPushButton::clicked,
|
||||||
this, &VMainWindow::onNewNotebookBtnClicked);
|
this, &VMainWindow::onNewNotebookBtnClicked);
|
||||||
connect(deleteNotebookBtn, &QPushButton::clicked,
|
connect(deleteNotebookBtn, &QPushButton::clicked,
|
||||||
|
@ -11,6 +11,7 @@ VTabWidget::VTabWidget(QWidget *parent)
|
|||||||
: QTabWidget(parent)
|
: QTabWidget(parent)
|
||||||
{
|
{
|
||||||
setTabsClosable(true);
|
setTabsClosable(true);
|
||||||
|
setMovable(true);
|
||||||
connect(tabBar(), &QTabBar::tabCloseRequested,
|
connect(tabBar(), &QTabBar::tabCloseRequested,
|
||||||
this, &VTabWidget::handleTabCloseRequest);
|
this, &VTabWidget::handleTabCloseRequest);
|
||||||
|
|
||||||
@ -48,17 +49,48 @@ void VTabWidget::openFile(QJsonObject fileJson)
|
|||||||
|
|
||||||
QString path = fileJson["path"].toString();
|
QString path = fileJson["path"].toString();
|
||||||
QString name = fileJson["name"].toString();
|
QString name = fileJson["name"].toString();
|
||||||
|
int mode = 0;
|
||||||
|
if (fileJson.contains("mode")) {
|
||||||
|
mode = fileJson["mode"].toInt();
|
||||||
|
}
|
||||||
|
|
||||||
// Find if it has been opened already
|
// Find if it has been opened already
|
||||||
int idx = findTabByFile(path, name);
|
int idx = findTabByFile(path, name);
|
||||||
if (idx > -1) {
|
if (idx > -1) {
|
||||||
setCurrentIndex(idx);
|
goto out;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
idx = openFileInTab(path, name, true);
|
idx = openFileInTab(path, name, true);
|
||||||
|
|
||||||
|
out:
|
||||||
setCurrentIndex(idx);
|
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)
|
int VTabWidget::openFileInTab(const QString &path, const QString &name, bool modifiable)
|
||||||
|
@ -15,6 +15,8 @@ signals:
|
|||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void openFile(QJsonObject fileJson);
|
void openFile(QJsonObject fileJson);
|
||||||
|
// Close the file forcely
|
||||||
|
void closeFile(QJsonObject fileJson);
|
||||||
void editFile();
|
void editFile();
|
||||||
void saveFile();
|
void saveFile();
|
||||||
void readFile();
|
void readFile();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user