Browse Source

add note info button in tool bar

Signed-off-by: Le Tan <[email protected]>
Le Tan 9 years ago
parent
commit
a6c2416cc8

+ 10 - 0
src/resources/icons/note_info_tb.svg

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Generator: Adobe Illustrator 16.2.1, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
+	 width="512px" height="512px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
+<g>
+	<polygon points="288,448 288,192 192,192 192,208 224,208 224,448 192,448 192,464 320,464 320,448 	"/>
+	<path d="M255.8,144.5c26.6,0,48.2-21.6,48.2-48.2s-21.6-48.2-48.2-48.2c-26.6,0-48.2,21.6-48.2,48.2S229.2,144.5,255.8,144.5z"/>
+</g>
+</svg>

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

+ 8 - 0
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<QPair<QString, QString> > &map,
                                       const QString &key, const QString &value);
@@ -31,4 +34,9 @@ inline void VUtils::addQssVarToMap(QVector<QPair<QString, QString> > &map,
     map.append(QPair<QString, QString>(key, value));
 }
 
+inline QString VUtils::directoryNameFromPath(const QString &path)
+{
+    return fileNameFromPath(path);
+}
+
 #endif // VUTILS_H

+ 3 - 2
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 &notebook, 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);
             }

+ 0 - 6
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 &notebook, 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<VEditTab *>(widget(tabIndex));

+ 55 - 32
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 &notebook,
     }
 }
 
-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 &notebook, const QString &fileRelativePath,

+ 5 - 11
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 &notebook, 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;

+ 23 - 1
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 &notebook, const QString &relativePath,
@@ -696,6 +710,9 @@ void VMainWindow::handleCurTabStatusChanged(const QString &notebook, 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);
+}

+ 5 - 0
src/vmainwindow.h

@@ -50,6 +50,7 @@ private slots:
                                    bool editMode, bool modifiable, bool modified);
     void changePanelView(QAction *action);
     void handleFileListDirectoryChanged(const QString &notebook, const QString &relativePath);
+    void curEditFileInfo();
 
 signals:
     void curNotebookChanged(const QString &notebookName);
@@ -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;

+ 1 - 0
src/vnote.qrc

@@ -62,5 +62,6 @@
         <file>resources/icons/vnote.svg</file>
         <file>resources/icons/vnote.ico</file>
         <file>resources/vnote.qss</file>
+        <file>resources/icons/note_info_tb.svg</file>
     </qresource>
 </RCC>