From be625f561c65c7ca5f9ac3bf68b63081f8127fe3 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Sun, 23 Oct 2016 18:07:05 +0800 Subject: [PATCH] implement notebookInfoBtn logics Signed-off-by: Le Tan --- src/dialog/vnotebookinfodialog.cpp | 68 +++++++++++++++++++++++++ src/dialog/vnotebookinfodialog.h | 38 ++++++++++++++ src/resources/icons/create_rootdir.png | Bin 0 -> 223 bytes src/resources/icons/delete_dir.png | Bin 0 -> 160 bytes src/resources/icons/dir_info.png | Bin 0 -> 2236 bytes src/src.pro | 6 ++- src/vdirectorytree.cpp | 3 +- src/vmainwindow.cpp | 57 +++++++++++++++++++-- src/vmainwindow.h | 6 ++- src/vnote.cpp | 20 ++++++++ src/vnote.h | 1 + src/vnote.qrc | 3 ++ 12 files changed, 193 insertions(+), 9 deletions(-) create mode 100644 src/dialog/vnotebookinfodialog.cpp create mode 100644 src/dialog/vnotebookinfodialog.h create mode 100755 src/resources/icons/create_rootdir.png create mode 100755 src/resources/icons/delete_dir.png create mode 100755 src/resources/icons/dir_info.png 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 0000000000000000000000000000000000000000..a76887a0813c78e0bf583167e34d2a319549e932 GIT binary patch literal 223 zcmeAS@N?(olHy`uVBq!ia0y~yU;;9k7#Nv>)VXbLJAo8ifKP}kkOqRK={wH@DaMi@ zzhDN3XE)M-oOVwa$B>F!Z*Le1G8k~M9Q)VXbLJAo8ifKP}kkOqRK={wH@DaMi@ zzhDN3XE)M-91l+y$B>F!Z_gO=GB9v38~mBg#v9{3EzE%(qyr8VQs=WUH99B=a6qUp csL~9I>|Fo8EaN$nFVdQ&MBb@0P=Pq%m4rY literal 0 HcmV?d00001 diff --git a/src/resources/icons/dir_info.png b/src/resources/icons/dir_info.png new file mode 100755 index 0000000000000000000000000000000000000000..8e03190ac92c3c2da03cc24d9977fd61d2ded665 GIT binary patch literal 2236 zcmeAS@N?(olHy`uVBq!ia0y~yU;;9k7&w@L)Zt|+Cx9H50G|+7Aa@ju@DR8cw`Dgl z2v(N_`2{mDGPAI9a`W&D2nmadNlHn}$SW!-tEg*gY3u0f=^Gdso0ytgSXx=z*gH77 zdwO~M`1<(=1O^3%L_|hK$0jDFq^6~3=H%w(7Zw$lR#aBk)YjEEw6wOhcXamjO_(@o z%8Z$_X3v>BZ^5D^%U7&iwR+9kb?Y~5+_Yuuw(UE2?>lt($kF2`PM$t<_QJ(Wm#<#C ze&g1iyZ0VGe)9DBiEaktaqI2fkJVF? zWsZOJNmLRHVF{UZA!Ff69Uo8Wu5CwJS~z(_RHizuT%p1udE~GEtMKXC+luF{f4y)1 z=c>P)^WXn&FD|n^e{)A6u9Wb<`{Q-}xXqcj@9F!^DNJ}i?V$TJe?za;8T-=<;(sll z9Q`{@digiCuWvlkzrXeV>>H9kH-G8>PnRzTzYPhtZNI#N=lteS-nAC<+;VTnR9`)_ z$T`!eZQYj0-k7gtQk(5IUp@Be@=C_td0S$1V}7J%T_}2d^}wCQD--o+udnb9HZF@z zJGklVvS%`%9T#L|2XA)!9Lm#g`og2BRb){DItu>MJ zGM8&{EAY-}BWasNOQGF|~ z-nZYIe65Iwm1Tm}o2afx!SDXoywP3PotZqib)wrO4Zr)#&)UW#-NfW!oEzQ5x!;ZT z!=6I#4LYy>y*&T%n(l$V4Gfo(UO&q(y7Tw4*ym^O8xJtlSzq6GQ<{%Q;cnvnH-9B0 z4^;hr_O9*3@8$ixb9T2oF@JD1sMc>|o3KZD!tT2p8FjW6JlogLn{ePzWp@AG`dc}V z_W1JiJx~^Yo*P%d5I@Tzznq=D;L?%GXdxK~qr!W;STEd~#<^hYUHyh@*HnR6u;%}I z-#OcF9%OuDUU+XA3;T!0&d=AxWDd9qr@za{Vc08n?#{Iv3|6{xb~fH%_>-3M?j7&m z|K)7w?moG|@W;>RAD^$?<9rAl5uic94;6Ctwy1wI`;)U%! z(Jkj|*fVl%9l!to%XC|8;oIjk80MMgD$4b9KHxenmgICodXY3wbdNjpyZT>2yQe+= z{E9gu_i9r~Sl$0-H{Da(q3i|AeY=f5w)i)!U3=^Mk?-@9FJ21yRq#NkdfM~7uio9y zJ*|J!TV$kruxjfd2{clji#%s<>QKOUwwWXRLfuk z#}rsMZ(tB$U~*vKU|>{WU}-?&yb|wVWknW1k)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