Browse Source

implement notebookInfoBtn logics

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

+ 68 - 0
src/dialog/vnotebookinfodialog.cpp

@@ -0,0 +1,68 @@
+#include <QtWidgets>
+#include "vnotebookinfodialog.h"
+
+VNotebookInfoDialog::VNotebookInfoDialog(const QString &title, const QString &info,
+                                         const QString &defaultName, const QString &defaultPath,
+                                         QWidget *parent)
+    : QDialog(parent), infoLabel(NULL), title(title), info(info), defaultName(defaultName),
+      defaultPath(defaultPath)
+{
+    setupUI();
+
+    connect(nameEdit, &QLineEdit::textChanged, this, &VNotebookInfoDialog::enableOkButton);
+    connect(okBtn, &QPushButton::clicked, this, &VNotebookInfoDialog::accept);
+    connect(cancelBtn, &QPushButton::clicked, this, &VNotebookInfoDialog::reject);
+
+    enableOkButton();
+}
+
+void VNotebookInfoDialog::setupUI()
+{
+    if (!info.isEmpty()) {
+        infoLabel = new QLabel(info);
+    }
+    nameLabel = new QLabel(tr("&Name"));
+    nameEdit = new QLineEdit(defaultName);
+    nameEdit->selectAll();
+    nameLabel->setBuddy(nameEdit);
+
+    QLabel *pathLabel = new QLabel(tr("&Path"));
+    pathEdit = new QLineEdit(defaultPath);
+    pathLabel->setBuddy(pathEdit);
+    pathEdit->setEnabled(false);
+
+    okBtn = new QPushButton(tr("&OK"));
+    okBtn->setDefault(true);
+    cancelBtn = new QPushButton(tr("&Cancel"));
+
+    QVBoxLayout *topLayout = new QVBoxLayout();
+    if (infoLabel) {
+        topLayout->addWidget(infoLabel);
+    }
+    topLayout->addWidget(nameLabel);
+    topLayout->addWidget(nameEdit);
+    topLayout->addWidget(pathLabel);
+    topLayout->addWidget(pathEdit);
+
+    QHBoxLayout *btmLayout = new QHBoxLayout();
+    btmLayout->addStretch();
+    btmLayout->addWidget(okBtn);
+    btmLayout->addWidget(cancelBtn);
+
+    QVBoxLayout *mainLayout = new QVBoxLayout();
+    mainLayout->addLayout(topLayout);
+    mainLayout->addLayout(btmLayout);
+    setLayout(mainLayout);
+
+    setWindowTitle(title);
+}
+
+void VNotebookInfoDialog::enableOkButton()
+{
+    okBtn->setEnabled(!nameEdit->text().isEmpty());
+}
+
+QString VNotebookInfoDialog::getNameInput() const
+{
+    return nameEdit->text();
+}

+ 38 - 0
src/dialog/vnotebookinfodialog.h

@@ -0,0 +1,38 @@
+#ifndef VNOTEBOOKINFODIALOG_H
+#define VNOTEBOOKINFODIALOG_H
+
+#include <QDialog>
+
+class QLabel;
+class QLineEdit;
+class QPushButton;
+class QString;
+
+class VNotebookInfoDialog : public QDialog
+{
+    Q_OBJECT
+public:
+    VNotebookInfoDialog(const QString &title, const QString &info, const QString &defaultName,
+                        const QString &defaultPath, QWidget *parent = 0);
+    QString getNameInput() const;
+
+private slots:
+    void enableOkButton();
+
+private:
+    void setupUI();
+
+    QLabel *infoLabel;
+    QLabel *nameLabel;
+    QLineEdit *nameEdit;
+    QLineEdit *pathEdit;
+    QPushButton *okBtn;
+    QPushButton *cancelBtn;
+
+    QString title;
+    QString info;
+    QString defaultName;
+    QString defaultPath;
+};
+
+#endif // VNOTEBOOKINFODIALOG_H

BIN
src/resources/icons/create_rootdir.png


BIN
src/resources/icons/delete_dir.png


BIN
src/resources/icons/dir_info.png


+ 4 - 2
src/src.pro

@@ -33,7 +33,8 @@ SOURCES += main.cpp\
     vstyleparser.cpp \
     vstyleparser.cpp \
     utils/peg-highlight/pmh_styleparser.c \
     utils/peg-highlight/pmh_styleparser.c \
     dialog/vnewnotebookdialog.cpp \
     dialog/vnewnotebookdialog.cpp \
-    vmarkdownconverter.cpp
+    vmarkdownconverter.cpp \
+    dialog/vnotebookinfodialog.cpp
 
 
 HEADERS  += vmainwindow.h \
 HEADERS  += vmainwindow.h \
     vdirectorytree.h \
     vdirectorytree.h \
@@ -57,7 +58,8 @@ HEADERS  += vmainwindow.h \
     vstyleparser.h \
     vstyleparser.h \
     utils/peg-highlight/pmh_styleparser.h \
     utils/peg-highlight/pmh_styleparser.h \
     dialog/vnewnotebookdialog.h \
     dialog/vnewnotebookdialog.h \
-    vmarkdownconverter.h
+    vmarkdownconverter.h \
+    dialog/vnotebookinfodialog.h
 
 
 RESOURCES += \
 RESOURCES += \
     vnote.qrc
     vnote.qrc

+ 2 - 1
src/vdirectorytree.cpp

@@ -48,11 +48,12 @@ void VDirectoryTree::setTreePath(const QString& path)
         return;
         return;
     }
     }
 
 
+    treePath = path;
     if (path.isEmpty()) {
     if (path.isEmpty()) {
         clear();
         clear();
         return;
         return;
     }
     }
-    treePath = path;
+
     qDebug() << "set directory tree path:" << path;
     qDebug() << "set directory tree path:" << path;
 
 
     updateDirectoryTree();
     updateDirectoryTree();

+ 52 - 5
src/vmainwindow.cpp

@@ -6,6 +6,7 @@
 #include "vtabwidget.h"
 #include "vtabwidget.h"
 #include "vconfigmanager.h"
 #include "vconfigmanager.h"
 #include "dialog/vnewnotebookdialog.h"
 #include "dialog/vnewnotebookdialog.h"
+#include "dialog/vnotebookinfodialog.h"
 
 
 extern VConfigManager vconfig;
 extern VConfigManager vconfig;
 
 
@@ -32,12 +33,21 @@ void VMainWindow::setupUI()
     // Notebook directory browser tree
     // Notebook directory browser tree
     notebookLabel = new QLabel(tr("Notebook"));
     notebookLabel = new QLabel(tr("Notebook"));
     directoryLabel = new QLabel(tr("Directory"));
     directoryLabel = new QLabel(tr("Directory"));
+
     newNotebookBtn = new QPushButton(QIcon(":/resources/icons/create_notebook.png"), "");
     newNotebookBtn = new QPushButton(QIcon(":/resources/icons/create_notebook.png"), "");
     newNotebookBtn->setToolTip(tr("Create a new notebook"));
     newNotebookBtn->setToolTip(tr("Create a new notebook"));
     deleteNotebookBtn = new QPushButton(QIcon(":/resources/icons/delete_notebook.png"), "");
     deleteNotebookBtn = new QPushButton(QIcon(":/resources/icons/delete_notebook.png"), "");
     deleteNotebookBtn->setToolTip(tr("Delete current notebook"));
     deleteNotebookBtn->setToolTip(tr("Delete current notebook"));
     notebookInfoBtn = new QPushButton(QIcon(":/resources/icons/notebook_info.png"), "");
     notebookInfoBtn = new QPushButton(QIcon(":/resources/icons/notebook_info.png"), "");
     notebookInfoBtn->setToolTip(tr("View and edit current notebook's information"));
     notebookInfoBtn->setToolTip(tr("View and edit current notebook's information"));
+
+    newRootDirBtn = new QPushButton(QIcon(":/resources/icons/create_rootdir.png"), "");
+    newRootDirBtn->setToolTip(tr("Create a new root directory"));
+    deleteDirBtn = new QPushButton(QIcon(":/resources/icons/delete_dir.png"), "");
+    deleteDirBtn->setToolTip(tr("Delete current directory"));
+    dirInfoBtn = new QPushButton(QIcon(":/resources/icons/dir_info.png"), "");
+    dirInfoBtn->setToolTip(tr("View and edit current directory's information"));
+
     notebookComboBox = new QComboBox();
     notebookComboBox = new QComboBox();
     notebookComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
     notebookComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
     directoryTree = new VDirectoryTree();
     directoryTree = new VDirectoryTree();
@@ -48,10 +58,16 @@ void VMainWindow::setupUI()
     nbBtnLayout->addWidget(newNotebookBtn);
     nbBtnLayout->addWidget(newNotebookBtn);
     nbBtnLayout->addWidget(deleteNotebookBtn);
     nbBtnLayout->addWidget(deleteNotebookBtn);
     nbBtnLayout->addWidget(notebookInfoBtn);
     nbBtnLayout->addWidget(notebookInfoBtn);
+    QHBoxLayout *dirBtnLayout = new QHBoxLayout;
+    dirBtnLayout->addWidget(directoryLabel);
+    dirBtnLayout->addStretch();
+    dirBtnLayout->addWidget(newRootDirBtn);
+    dirBtnLayout->addWidget(deleteDirBtn);
+    dirBtnLayout->addWidget(dirInfoBtn);
     QVBoxLayout *nbLayout = new QVBoxLayout;
     QVBoxLayout *nbLayout = new QVBoxLayout;
     nbLayout->addLayout(nbBtnLayout);
     nbLayout->addLayout(nbBtnLayout);
     nbLayout->addWidget(notebookComboBox);
     nbLayout->addWidget(notebookComboBox);
-    nbLayout->addWidget(directoryLabel);
+    nbLayout->addLayout(dirBtnLayout);
     nbLayout->addWidget(directoryTree);
     nbLayout->addWidget(directoryTree);
     QWidget *nbContainer = new QWidget();
     QWidget *nbContainer = new QWidget();
     nbContainer->setLayout(nbLayout);
     nbContainer->setLayout(nbLayout);
@@ -92,6 +108,8 @@ void VMainWindow::setupUI()
             this, &VMainWindow::onNewNotebookBtnClicked);
             this, &VMainWindow::onNewNotebookBtnClicked);
     connect(deleteNotebookBtn, &QPushButton::clicked,
     connect(deleteNotebookBtn, &QPushButton::clicked,
             this, &VMainWindow::onDeleteNotebookBtnClicked);
             this, &VMainWindow::onDeleteNotebookBtnClicked);
+    connect(notebookInfoBtn, &QPushButton::clicked,
+            this, &VMainWindow::onNotebookInfoBtnClicked);
     connect(vnote, &VNote::notebooksChanged,
     connect(vnote, &VNote::notebooksChanged,
             this, &VMainWindow::updateNotebookComboBox);
             this, &VMainWindow::updateNotebookComboBox);
 
 
@@ -224,8 +242,8 @@ void VMainWindow::onNewNotebookBtnClicked()
         if (dialog.exec() == QDialog::Accepted) {
         if (dialog.exec() == QDialog::Accepted) {
             QString name = dialog.getNameInput();
             QString name = dialog.getNameInput();
             QString path = dialog.getPathInput();
             QString path = dialog.getPathInput();
-            if (isConflictWithExistingNotebooks(name, path)) {
-                info = "Name already exists or the path already has a notebook.";
+            if (isConflictWithExistingNotebooks(name)) {
+                info = "Name already exists.";
                 defaultName = name;
                 defaultName = name;
                 defaultPath = path;
                 defaultPath = path;
                 continue;
                 continue;
@@ -236,11 +254,11 @@ void VMainWindow::onNewNotebookBtnClicked()
     } while (true);
     } while (true);
 }
 }
 
 
-bool VMainWindow::isConflictWithExistingNotebooks(const QString &name, const QString &path)
+bool VMainWindow::isConflictWithExistingNotebooks(const QString &name)
 {
 {
     const QVector<VNotebook> &notebooks = vnote->getNotebooks();
     const QVector<VNotebook> &notebooks = vnote->getNotebooks();
     for (int i = 0; i < notebooks.size(); ++i) {
     for (int i = 0; i < notebooks.size(); ++i) {
-        if (notebooks[i].getName() == name || notebooks[i].getPath() == path) {
+        if (notebooks[i].getName() == name) {
             return true;
             return true;
         }
         }
     }
     }
@@ -262,6 +280,35 @@ void VMainWindow::onDeleteNotebookBtnClicked()
     }
     }
 }
 }
 
 
+void VMainWindow::onNotebookInfoBtnClicked()
+{
+    int curIndex = notebookComboBox->currentIndex();
+    if (curIndex < 0) {
+        return;
+    }
+    QString info;
+    QString curName = vnote->getNotebooks()[curIndex].getName();
+    QString defaultPath = vnote->getNotebooks()[curIndex].getPath();
+    QString defaultName(curName);
+    do {
+        VNotebookInfoDialog dialog(tr("Notebook information"), info, defaultName,
+                                  defaultPath, this);
+        if (dialog.exec() == QDialog::Accepted) {
+            QString name = dialog.getNameInput();
+            if (name == curName) {
+                return;
+            }
+            if (isConflictWithExistingNotebooks(name)) {
+                info = "Name already exists.";
+                defaultName = name;
+                continue;
+            }
+            vnote->renameNotebook(curName, name);
+        }
+        break;
+    } while (true);
+}
+
 void VMainWindow::importNoteFromFile()
 void VMainWindow::importNoteFromFile()
 {
 {
     QStringList files = QFileDialog::getOpenFileNames(this,tr("Select files(HTML or Markdown) to be imported as notes"),
     QStringList files = QFileDialog::getOpenFileNames(this,tr("Select files(HTML or Markdown) to be imported as notes"),

+ 5 - 1
src/vmainwindow.h

@@ -32,6 +32,7 @@ private slots:
     // Create a notebook
     // Create a notebook
     void onNewNotebookBtnClicked();
     void onNewNotebookBtnClicked();
     void onDeleteNotebookBtnClicked();
     void onDeleteNotebookBtnClicked();
+    void onNotebookInfoBtnClicked();
     void updateNotebookComboBox(const QVector<VNotebook> &notebooks);
     void updateNotebookComboBox(const QVector<VNotebook> &notebooks);
     void importNoteFromFile();
     void importNoteFromFile();
     void changeMarkdownConverter(QAction *action);
     void changeMarkdownConverter(QAction *action);
@@ -45,7 +46,7 @@ private:
     void initActions();
     void initActions();
     void initToolBar();
     void initToolBar();
     void initMenuBar();
     void initMenuBar();
-    bool isConflictWithExistingNotebooks(const QString &name, const QString &path);
+    bool isConflictWithExistingNotebooks(const QString &name);
 
 
     QLabel *notebookLabel;
     QLabel *notebookLabel;
     QLabel *directoryLabel;
     QLabel *directoryLabel;
@@ -53,6 +54,9 @@ private:
     QPushButton *newNotebookBtn;
     QPushButton *newNotebookBtn;
     QPushButton *deleteNotebookBtn;
     QPushButton *deleteNotebookBtn;
     QPushButton *notebookInfoBtn;
     QPushButton *notebookInfoBtn;
+    QPushButton *newRootDirBtn;
+    QPushButton *deleteDirBtn;
+    QPushButton *dirInfoBtn;
     VDirectoryTree *directoryTree;
     VDirectoryTree *directoryTree;
     VFileList *fileList;
     VFileList *fileList;
     VTabWidget *tabs;
     VTabWidget *tabs;

+ 20 - 0
src/vnote.cpp

@@ -90,3 +90,23 @@ void VNote::removeNotebook(const QString &name)
     }
     }
     emit notebooksChanged(notebooks);
     emit notebooksChanged(notebooks);
 }
 }
+
+void VNote::renameNotebook(const QString &name, const QString &newName)
+{
+    QString path;
+    int index;
+    for (index = 0; index < notebooks.size(); ++index) {
+        if (notebooks[index].getName() == name) {
+            path = notebooks[index].getPath();
+            break;
+        }
+    }
+    if (index == notebooks.size()) {
+        return;
+    }
+
+    notebooks[index].setName(newName);
+    vconfig.setNotebooks(notebooks);
+
+    emit notebooksChanged(notebooks);
+}

+ 1 - 0
src/vnote.h

@@ -29,6 +29,7 @@ public:
 
 
     void createNotebook(const QString &name, const QString &path);
     void createNotebook(const QString &name, const QString &path);
     void removeNotebook(const QString &name);
     void removeNotebook(const QString &name);
+    void renameNotebook(const QString &name, const QString &newName);
 
 
 signals:
 signals:
     void notebooksChanged(const QVector<VNotebook> &notebooks);
     void notebooksChanged(const QVector<VNotebook> &notebooks);

+ 3 - 0
src/vnote.qrc

@@ -39,5 +39,8 @@
         <file>resources/icons/create_notebook.png</file>
         <file>resources/icons/create_notebook.png</file>
         <file>resources/icons/delete_notebook.png</file>
         <file>resources/icons/delete_notebook.png</file>
         <file>resources/icons/notebook_info.png</file>
         <file>resources/icons/notebook_info.png</file>
+        <file>resources/icons/create_rootdir.png</file>
+        <file>resources/icons/delete_dir.png</file>
+        <file>resources/icons/dir_info.png</file>
     </qresource>
     </qresource>
 </RCC>
 </RCC>