diff --git a/src/dialog/vnotebookinfodialog.cpp b/src/dialog/vnotebookinfodialog.cpp new file mode 100644 index 00000000..9e74379d --- /dev/null +++ b/src/dialog/vnotebookinfodialog.cpp @@ -0,0 +1,68 @@ +#include +#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(); +} diff --git a/src/dialog/vnotebookinfodialog.h b/src/dialog/vnotebookinfodialog.h new file mode 100644 index 00000000..ecc7958f --- /dev/null +++ b/src/dialog/vnotebookinfodialog.h @@ -0,0 +1,38 @@ +#ifndef VNOTEBOOKINFODIALOG_H +#define VNOTEBOOKINFODIALOG_H + +#include + +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 diff --git a/src/resources/icons/create_rootdir.png b/src/resources/icons/create_rootdir.png new file mode 100755 index 00000000..a76887a0 Binary files /dev/null and b/src/resources/icons/create_rootdir.png differ diff --git a/src/resources/icons/delete_dir.png b/src/resources/icons/delete_dir.png new file mode 100755 index 00000000..67337156 Binary files /dev/null and b/src/resources/icons/delete_dir.png differ diff --git a/src/resources/icons/dir_info.png b/src/resources/icons/dir_info.png new file mode 100755 index 00000000..8e03190a Binary files /dev/null and b/src/resources/icons/dir_info.png differ diff --git a/src/src.pro b/src/src.pro index c40db8fb..486f74b9 100644 --- a/src/src.pro +++ b/src/src.pro @@ -33,7 +33,8 @@ SOURCES += main.cpp\ vstyleparser.cpp \ utils/peg-highlight/pmh_styleparser.c \ dialog/vnewnotebookdialog.cpp \ - vmarkdownconverter.cpp + vmarkdownconverter.cpp \ + dialog/vnotebookinfodialog.cpp HEADERS += vmainwindow.h \ vdirectorytree.h \ @@ -57,7 +58,8 @@ HEADERS += vmainwindow.h \ vstyleparser.h \ utils/peg-highlight/pmh_styleparser.h \ dialog/vnewnotebookdialog.h \ - vmarkdownconverter.h + vmarkdownconverter.h \ + dialog/vnotebookinfodialog.h RESOURCES += \ vnote.qrc diff --git a/src/vdirectorytree.cpp b/src/vdirectorytree.cpp index dc2389b0..294ee516 100644 --- a/src/vdirectorytree.cpp +++ b/src/vdirectorytree.cpp @@ -48,11 +48,12 @@ void VDirectoryTree::setTreePath(const QString& path) return; } + treePath = path; if (path.isEmpty()) { clear(); return; } - treePath = path; + qDebug() << "set directory tree path:" << path; updateDirectoryTree(); diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index cb6278f1..cde3940f 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -6,6 +6,7 @@ #include "vtabwidget.h" #include "vconfigmanager.h" #include "dialog/vnewnotebookdialog.h" +#include "dialog/vnotebookinfodialog.h" extern VConfigManager vconfig; @@ -32,12 +33,21 @@ void VMainWindow::setupUI() // Notebook directory browser tree notebookLabel = new QLabel(tr("Notebook")); directoryLabel = new QLabel(tr("Directory")); + newNotebookBtn = new QPushButton(QIcon(":/resources/icons/create_notebook.png"), ""); newNotebookBtn->setToolTip(tr("Create a new notebook")); deleteNotebookBtn = new QPushButton(QIcon(":/resources/icons/delete_notebook.png"), ""); deleteNotebookBtn->setToolTip(tr("Delete current notebook")); notebookInfoBtn = new QPushButton(QIcon(":/resources/icons/notebook_info.png"), ""); 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->setSizeAdjustPolicy(QComboBox::AdjustToContents); directoryTree = new VDirectoryTree(); @@ -48,10 +58,16 @@ void VMainWindow::setupUI() nbBtnLayout->addWidget(newNotebookBtn); nbBtnLayout->addWidget(deleteNotebookBtn); 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; nbLayout->addLayout(nbBtnLayout); nbLayout->addWidget(notebookComboBox); - nbLayout->addWidget(directoryLabel); + nbLayout->addLayout(dirBtnLayout); nbLayout->addWidget(directoryTree); QWidget *nbContainer = new QWidget(); nbContainer->setLayout(nbLayout); @@ -92,6 +108,8 @@ void VMainWindow::setupUI() this, &VMainWindow::onNewNotebookBtnClicked); connect(deleteNotebookBtn, &QPushButton::clicked, this, &VMainWindow::onDeleteNotebookBtnClicked); + connect(notebookInfoBtn, &QPushButton::clicked, + this, &VMainWindow::onNotebookInfoBtnClicked); connect(vnote, &VNote::notebooksChanged, this, &VMainWindow::updateNotebookComboBox); @@ -224,8 +242,8 @@ void VMainWindow::onNewNotebookBtnClicked() if (dialog.exec() == QDialog::Accepted) { QString name = dialog.getNameInput(); 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; defaultPath = path; continue; @@ -236,11 +254,11 @@ void VMainWindow::onNewNotebookBtnClicked() } while (true); } -bool VMainWindow::isConflictWithExistingNotebooks(const QString &name, const QString &path) +bool VMainWindow::isConflictWithExistingNotebooks(const QString &name) { const QVector ¬ebooks = vnote->getNotebooks(); for (int i = 0; i < notebooks.size(); ++i) { - if (notebooks[i].getName() == name || notebooks[i].getPath() == path) { + if (notebooks[i].getName() == name) { 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() { QStringList files = QFileDialog::getOpenFileNames(this,tr("Select files(HTML or Markdown) to be imported as notes"), diff --git a/src/vmainwindow.h b/src/vmainwindow.h index 59f4566d..3afba7f7 100644 --- a/src/vmainwindow.h +++ b/src/vmainwindow.h @@ -32,6 +32,7 @@ private slots: // Create a notebook void onNewNotebookBtnClicked(); void onDeleteNotebookBtnClicked(); + void onNotebookInfoBtnClicked(); void updateNotebookComboBox(const QVector ¬ebooks); void importNoteFromFile(); void changeMarkdownConverter(QAction *action); @@ -45,7 +46,7 @@ private: void initActions(); void initToolBar(); void initMenuBar(); - bool isConflictWithExistingNotebooks(const QString &name, const QString &path); + bool isConflictWithExistingNotebooks(const QString &name); QLabel *notebookLabel; QLabel *directoryLabel; @@ -53,6 +54,9 @@ private: QPushButton *newNotebookBtn; QPushButton *deleteNotebookBtn; QPushButton *notebookInfoBtn; + QPushButton *newRootDirBtn; + QPushButton *deleteDirBtn; + QPushButton *dirInfoBtn; VDirectoryTree *directoryTree; VFileList *fileList; VTabWidget *tabs; diff --git a/src/vnote.cpp b/src/vnote.cpp index 302b4c49..50b4fd54 100644 --- a/src/vnote.cpp +++ b/src/vnote.cpp @@ -90,3 +90,23 @@ void VNote::removeNotebook(const QString &name) } 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); +} diff --git a/src/vnote.h b/src/vnote.h index fa92a64e..56c826f0 100644 --- a/src/vnote.h +++ b/src/vnote.h @@ -29,6 +29,7 @@ public: void createNotebook(const QString &name, const QString &path); void removeNotebook(const QString &name); + void renameNotebook(const QString &name, const QString &newName); signals: void notebooksChanged(const QVector ¬ebooks); diff --git a/src/vnote.qrc b/src/vnote.qrc index 33c02972..f54a8060 100644 --- a/src/vnote.qrc +++ b/src/vnote.qrc @@ -39,5 +39,8 @@ resources/icons/create_notebook.png resources/icons/delete_notebook.png resources/icons/notebook_info.png + resources/icons/create_rootdir.png + resources/icons/delete_dir.png + resources/icons/dir_info.png