diff --git a/src/resources/icons/note_info_tb.svg b/src/resources/icons/note_info_tb.svg new file mode 100644 index 00000000..e70c4ced --- /dev/null +++ b/src/resources/icons/note_info_tb.svg @@ -0,0 +1,10 @@ + + + + + + + + + diff --git a/src/utils/vutils.cpp b/src/utils/vutils.cpp index 2c714305..adb79ecb 100644 --- a/src/utils/vutils.cpp +++ b/src/utils/vutils.cpp @@ -108,3 +108,16 @@ bool VUtils::isMarkdown(const QString &name) } return false; } + +QString VUtils::fileNameFromPath(const QString &path) +{ + if (path.isEmpty()) { + return path; + } + return QFileInfo(QDir::cleanPath(path)).fileName(); +} + +QString VUtils::basePathFromPath(const QString &path) +{ + return QFileInfo(path).path(); +} diff --git a/src/utils/vutils.h b/src/utils/vutils.h index bde1540c..b8295c3e 100644 --- a/src/utils/vutils.h +++ b/src/utils/vutils.h @@ -20,6 +20,9 @@ public: const QString &format = "png"); static void processStyle(QString &style); static bool isMarkdown(const QString &fileName); + static inline QString directoryNameFromPath(const QString& path); + static QString fileNameFromPath(const QString &path); + static QString basePathFromPath(const QString &path); private: static inline void addQssVarToMap(QVector > &map, const QString &key, const QString &value); @@ -31,4 +34,9 @@ inline void VUtils::addQssVarToMap(QVector > &map, map.append(QPair(key, value)); } +inline QString VUtils::directoryNameFromPath(const QString &path) +{ + return fileNameFromPath(path); +} + #endif // VUTILS_H diff --git a/src/veditwindow.cpp b/src/veditwindow.cpp index 776f4400..627f533a 100644 --- a/src/veditwindow.cpp +++ b/src/veditwindow.cpp @@ -4,6 +4,7 @@ #include "vedittab.h" #include "vnote.h" #include "vconfigmanager.h" +#include "utils/vutils.h" extern VConfigManager vconfig; @@ -91,7 +92,7 @@ void VEditWindow::openWelcomePage() int VEditWindow::insertTabWithData(int index, QWidget *page, const QJsonObject &tabData) { - QString label = getFileName(tabData["relative_path"].toString()); + QString label = VUtils::fileNameFromPath(tabData["relative_path"].toString()); int idx = insertTab(index, page, label); QTabBar *tabs = tabBar(); tabs->setTabData(idx, tabData); @@ -293,7 +294,7 @@ void VEditWindow::handleFileRenamed(const QString ¬ebook, const QString &oldR tabJson["relative_path"] = relativePath; tabs->setTabData(i, tabJson); tabs->setTabToolTip(i, generateTooltip(tabJson)); - tabs->setTabText(i, getFileName(relativePath)); + tabs->setTabText(i, VUtils::fileNameFromPath(relativePath)); QString path = QDir::cleanPath(QDir(vnote->getNotebookPath(notebook)).filePath(relativePath)); getTab(i)->updatePath(path); } diff --git a/src/veditwindow.h b/src/veditwindow.h index 7d8342b2..2c128ea1 100644 --- a/src/veditwindow.h +++ b/src/veditwindow.h @@ -72,7 +72,6 @@ private: int insertTabWithData(int index, QWidget *page, const QJsonObject &tabData); int appendTabWithData(QWidget *page, const QJsonObject &tabData); int openFileInTab(const QString ¬ebook, const QString &relativePath, bool modifiable); - inline QString getFileName(const QString &relativePath) const; inline VEditTab *getTab(int tabIndex) const; void noticeTabStatus(int index); void updateTabListMenu(); @@ -91,11 +90,6 @@ private: QActionGroup *tabListAct; }; -inline QString VEditWindow::getFileName(const QString &path) const -{ - return QFileInfo(QDir::cleanPath(path)).fileName(); -} - inline VEditTab* VEditWindow::getTab(int tabIndex) const { return dynamic_cast(widget(tabIndex)); diff --git a/src/vfilelist.cpp b/src/vfilelist.cpp index ddf64d8e..093ba847 100644 --- a/src/vfilelist.cpp +++ b/src/vfilelist.cpp @@ -50,7 +50,7 @@ void VFileList::initActions() tr("&Info"), this); fileInfoAct->setStatusTip(tr("View and edit current note's information")); connect(fileInfoAct, &QAction::triggered, - this, &VFileList::fileInfo); + this, &VFileList::curFileInfo); } void VFileList::setDirectory(QJsonObject dirJson) @@ -117,21 +117,26 @@ void VFileList::updateFileList() } } -void VFileList::fileInfo() +void VFileList::curFileInfo() { QListWidgetItem *curItem = fileList->currentItem(); QJsonObject curItemJson = curItem->data(Qt::UserRole).toJsonObject(); Q_ASSERT(!curItemJson.isEmpty()); QString curItemName = curItemJson["name"].toString(); + fileInfo(notebook, QDir(relativePath).filePath(curItemName)); +} +void VFileList::fileInfo(const QString &p_notebook, const QString &p_relativePath) +{ + qDebug() << "fileInfo" << p_notebook << p_relativePath; QString info; - QString defaultName = curItemName; - + QString defaultName = VUtils::directoryNameFromPath(p_relativePath); + QString curName = defaultName; do { VFileInfoDialog dialog(tr("Note Information"), info, defaultName, this); if (dialog.exec() == QDialog::Accepted) { QString name = dialog.getNameInput(); - if (name == curItemName) { + if (name == curName) { return; } if (isConflictNameWithExisting(name)) { @@ -139,7 +144,7 @@ void VFileList::fileInfo() defaultName = name; continue; } - renameFile(curItem, name); + renameFile(p_notebook, p_relativePath, name); } break; } while (true); @@ -176,8 +181,8 @@ void VFileList::newFile() QString text("&Note name:"); QString defaultText("new_note"); do { - VNewFileDialog dialog(QString("Create a new note under %1").arg(getDirectoryName()), text, - defaultText, this); + VNewFileDialog dialog(QString("Create a new note under %1").arg(VUtils::directoryNameFromPath(relativePath)), + text, defaultText, this); if (dialog.exec() == QDialog::Accepted) { QString name = dialog.getNameInput(); if (isConflictNameWithExisting(name)) { @@ -259,6 +264,24 @@ bool VFileList::isConflictNameWithExisting(const QString &name) return false; } +QListWidgetItem* VFileList::findItem(const QString &p_notebook, const QString &p_relativePath) +{ + if (p_notebook != notebook || VUtils::basePathFromPath(p_relativePath) != QDir::cleanPath(relativePath)) { + return NULL; + } + QString name = VUtils::fileNameFromPath(p_relativePath); + int nrChild = fileList->count(); + for (int i = 0; i < nrChild; ++i) { + QListWidgetItem *item = fileList->item(i); + QJsonObject itemJson = item->data(Qt::UserRole).toJsonObject(); + Q_ASSERT(!itemJson.isEmpty()); + if (itemJson["name"].toString() == name) { + return item; + } + } + return NULL; +} + QListWidgetItem* VFileList::createFileAndUpdateList(const QString &name) { QString path = QDir(rootPath).filePath(relativePath); @@ -411,29 +434,25 @@ void VFileList::handleDirectoryRenamed(const QString ¬ebook, } } -void VFileList::renameFile(QListWidgetItem *item, const QString &newName) +// @p_relativePath contains the flie name +void VFileList::renameFile(const QString &p_notebook, + const QString &p_relativePath, const QString &p_newName) { - if (!item) { - return; - } - QJsonObject itemJson = item->data(Qt::UserRole).toJsonObject(); - Q_ASSERT(!itemJson.isEmpty()); - QString name = itemJson["name"].toString(); + QString name = VUtils::fileNameFromPath(p_relativePath); // If change the file type, we need to convert it DocType docType = VUtils::isMarkdown(name) ? DocType::Markdown : DocType::Html; - DocType newDocType = VUtils::isMarkdown(newName) ? DocType::Markdown : DocType::Html; + DocType newDocType = VUtils::isMarkdown(p_newName) ? DocType::Markdown : DocType::Html; if (docType != newDocType) { - QString fileRelativePath = QDir::cleanPath(QDir(relativePath).filePath(name)); - if (editArea->isFileOpened(notebook, fileRelativePath)) { + if (editArea->isFileOpened(p_notebook, p_relativePath)) { QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), QString("Rename will change the note type"), QMessageBox::Ok | QMessageBox::Cancel, this); msgBox.setDefaultButton(QMessageBox::Ok); msgBox.setInformativeText(QString("You should close the note %1 before continue").arg(name)); if (QMessageBox::Ok == msgBox.exec()) { QJsonObject curItemJson; - curItemJson["notebook"] = notebook; - curItemJson["relative_path"] = fileRelativePath; + curItemJson["notebook"] = p_notebook; + curItemJson["relative_path"] = p_relativePath; curItemJson["is_forced"] = false; if (!editArea->closeFile(curItemJson)) { return; @@ -442,18 +461,18 @@ void VFileList::renameFile(QListWidgetItem *item, const QString &newName) return; } } - convertFileType(notebook, fileRelativePath, docType, newDocType); + convertFileType(p_notebook, p_relativePath, docType, newDocType); } - QString path = QDir(rootPath).filePath(relativePath); + QString path = QDir(vnote->getNotebookPath(p_notebook)).filePath(VUtils::basePathFromPath(p_relativePath)); QFile file(QDir(path).filePath(name)); - QString newFilePath(QDir(path).filePath(newName)); + QString newFilePath(QDir(path).filePath(p_newName)); Q_ASSERT(file.exists()); if (!file.rename(newFilePath)) { qWarning() << "error: fail to rename file" << name << "under" << path; QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), QString("Could not rename note \"%1\" under \"%2\".") .arg(name).arg(path), QMessageBox::Ok, this); - msgBox.setInformativeText(QString("Please check if there already exists a file named \"%1\".").arg(newName)); + msgBox.setInformativeText(QString("Please check if there already exists a file named \"%1\".").arg(p_newName)); msgBox.exec(); return; } @@ -466,7 +485,7 @@ void VFileList::renameFile(QListWidgetItem *item, const QString &newName) for (index = 0; index < fileArray.size(); ++index) { QJsonObject tmp = fileArray[index].toObject(); if (tmp["name"].toString() == name) { - tmp["name"] = newName; + tmp["name"] = p_newName; fileArray[index] = tmp; break; } @@ -475,20 +494,24 @@ void VFileList::renameFile(QListWidgetItem *item, const QString &newName) dirJson["files"] = fileArray; if (!VConfigManager::writeDirectoryConfig(path, dirJson)) { qWarning() << "error: fail to rename file" - << name << "to" << newName; + << name << "to" << p_newName; file.rename(name); return; } // Update item - itemJson["name"] = newName; - item->setData(Qt::UserRole, itemJson); - item->setText(newName); + QListWidgetItem *item = findItem(p_notebook, p_relativePath); + if (item) { + QJsonObject itemJson = item->data(Qt::UserRole).toJsonObject(); + itemJson["name"] = p_newName; + item->setData(Qt::UserRole, itemJson); + item->setText(p_newName); + } - QString oldPath = QDir::cleanPath(QDir(relativePath).filePath(name)); - QString newPath = QDir::cleanPath(QDir(relativePath).filePath(newName)); + QString oldPath = QDir::cleanPath(p_relativePath); + QString newPath = QDir::cleanPath(QDir(VUtils::basePathFromPath(p_relativePath)).filePath(p_newName)); qDebug() << "file renamed" << oldPath << "to" << newPath; - emit fileRenamed(notebook, oldPath, newPath); + emit fileRenamed(p_notebook, oldPath, newPath); } void VFileList::convertFileType(const QString ¬ebook, const QString &fileRelativePath, diff --git a/src/vfilelist.h b/src/vfilelist.h index 234fdfba..0c6102f2 100644 --- a/src/vfilelist.h +++ b/src/vfilelist.h @@ -22,6 +22,7 @@ public: explicit VFileList(VNote *vnote, QWidget *parent = 0); bool importFile(const QString &name); inline void setEditArea(VEditArea *editArea); + void fileInfo(const QString &p_notebook, const QString &p_relativePath); signals: void fileClicked(QJsonObject fileJson); @@ -34,7 +35,7 @@ signals: private slots: void contextMenuRequested(QPoint pos); void handleItemClicked(QListWidgetItem *currentItem); - void fileInfo(); + void curFileInfo(); public slots: void setDirectory(QJsonObject dirJson); @@ -55,10 +56,11 @@ private: QListWidgetItem *createFileAndUpdateList(const QString &name); void deleteFileAndUpdateList(QListWidgetItem *item); void clearDirectoryInfo(); - inline QString getDirectoryName(); - void renameFile(QListWidgetItem *item, const QString &newName); + void renameFile(const QString &p_notebook, + const QString &p_relativePath, const QString &p_newName); void convertFileType(const QString ¬ebook, const QString &fileRelativePath, DocType oldType, DocType newType); + QListWidgetItem *findItem(const QString &p_notebook, const QString &p_relativePath); VNote *vnote; QString notebook; @@ -77,14 +79,6 @@ private: QAction *fileInfoAct; }; -inline QString VFileList::getDirectoryName() -{ - if (relativePath.isEmpty()) { - return ""; - } - return QFileInfo(QDir::cleanPath(relativePath)).fileName(); -} - inline void VFileList::setEditArea(VEditArea *editArea) { this->editArea = editArea; diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index c28676d5..3193bd4b 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -148,12 +148,19 @@ void VMainWindow::initActions() newRootDirAct->setStatusTip(tr("Create a new root directory")); connect(newRootDirAct, &QAction::triggered, directoryTree, &VDirectoryTree::newRootDirectory); + newNoteAct = new QAction(QIcon(":/resources/icons/create_note_tb.svg"), tr("&New note"), this); newNoteAct->setStatusTip(tr("Create a new note")); connect(newNoteAct, &QAction::triggered, fileList, &VFileList::newFile); + noteInfoAct = new QAction(QIcon(":/resources/icons/note_info_tb.svg"), + tr("&Note info"), this); + noteInfoAct->setStatusTip(tr("Current note information")); + connect(noteInfoAct, &QAction::triggered, + this, &VMainWindow::curEditFileInfo); + editNoteAct = new QAction(QIcon(":/resources/icons/edit_note.svg"), tr("&Edit"), this); editNoteAct->setStatusTip(tr("Edit current note")); @@ -262,6 +269,7 @@ void VMainWindow::initToolBar() QToolBar *fileToolBar = addToolBar(tr("Note")); fileToolBar->addAction(newRootDirAct); fileToolBar->addAction(newNoteAct); + fileToolBar->addAction(noteInfoAct); fileToolBar->addAction(editNoteAct); fileToolBar->addAction(saveExitAct); fileToolBar->addAction(discardExitAct); @@ -269,6 +277,7 @@ void VMainWindow::initToolBar() newRootDirAct->setEnabled(false); newNoteAct->setEnabled(false); + noteInfoAct->setEnabled(false); editNoteAct->setEnabled(false); saveExitAct->setVisible(false); discardExitAct->setVisible(false); @@ -669,7 +678,6 @@ void VMainWindow::updateToolbarFromTabChage(bool empty, bool editMode, bool modi saveExitAct->setVisible(false); discardExitAct->setVisible(false); saveNoteAct->setVisible(false); - return; } else if (editMode) { editNoteAct->setEnabled(false); saveExitAct->setVisible(true); @@ -681,6 +689,12 @@ void VMainWindow::updateToolbarFromTabChage(bool empty, bool editMode, bool modi discardExitAct->setVisible(false); saveNoteAct->setVisible(false); } + + if (empty) { + noteInfoAct->setEnabled(false); + } else { + noteInfoAct->setEnabled(true); + } } void VMainWindow::handleCurTabStatusChanged(const QString ¬ebook, const QString &relativePath, @@ -696,6 +710,9 @@ void VMainWindow::handleCurTabStatusChanged(const QString ¬ebook, const QStri } } updateWindowTitle(title); + + curEditNotebook = notebook; + curEditRelativePath = relativePath; } void VMainWindow::changePanelView(QAction *action) @@ -751,3 +768,8 @@ void VMainWindow::updateWindowTitle(const QString &str) } setWindowTitle(title); } + +void VMainWindow::curEditFileInfo() +{ + fileList->fileInfo(curEditNotebook, curEditRelativePath); +} diff --git a/src/vmainwindow.h b/src/vmainwindow.h index 47fd06c3..ac49b6a0 100644 --- a/src/vmainwindow.h +++ b/src/vmainwindow.h @@ -50,6 +50,7 @@ private slots: bool editMode, bool modifiable, bool modified); void changePanelView(QAction *action); void handleFileListDirectoryChanged(const QString ¬ebook, const QString &relativePath); + void curEditFileInfo(); signals: void curNotebookChanged(const QString ¬ebookName); @@ -72,6 +73,9 @@ private: bool notebookComboMuted; VNote *vnote; + QString curEditNotebook; + QString curEditRelativePath; + QLabel *notebookLabel; QLabel *directoryLabel; QComboBox *notebookComboBox; @@ -88,6 +92,7 @@ private: // Actions QAction *newRootDirAct; QAction *newNoteAct; + QAction *noteInfoAct; QAction *editNoteAct; QAction *saveNoteAct; QAction *saveExitAct; diff --git a/src/vnote.qrc b/src/vnote.qrc index c7c01cfe..fbb1713b 100644 --- a/src/vnote.qrc +++ b/src/vnote.qrc @@ -62,5 +62,6 @@ resources/icons/vnote.svg resources/icons/vnote.ico resources/vnote.qss + resources/icons/note_info_tb.svg