From 8e8d69c85f247e79f53b5da81c4d236ed32d73fd Mon Sep 17 00:00:00 2001 From: Le Tan Date: Fri, 25 Nov 2016 10:09:32 +0800 Subject: [PATCH] refactor: VNote and VNotebook regression: notebook renaming logics does not work for now Signed-off-by: Le Tan --- src/vconfigmanager.cpp | 26 ++++++------ src/vconfigmanager.h | 18 ++++----- src/vdirectorytree.cpp | 15 ++----- src/vdirectorytree.h | 2 - src/veditarea.cpp | 11 ------ src/veditarea.h | 2 - src/veditwindow.cpp | 16 -------- src/veditwindow.h | 2 - src/vfilelist.cpp | 14 ++----- src/vfilelist.h | 2 - src/vmainwindow.cpp | 77 ++++++++++++------------------------ src/vmainwindow.h | 10 ++--- src/vnote.cpp | 90 ++++++++++++++++++------------------------ src/vnote.h | 20 +++++----- src/vnotebook.cpp | 20 ++++++---- src/vnotebook.h | 16 +++++--- 16 files changed, 127 insertions(+), 214 deletions(-) diff --git a/src/vconfigmanager.cpp b/src/vconfigmanager.cpp index 7a7d52da..60b7983a 100644 --- a/src/vconfigmanager.cpp +++ b/src/vconfigmanager.cpp @@ -65,9 +65,6 @@ void VConfigManager::initialize() m_mainWindowGeometry = getConfigFromSettings("session", "main_window_geometry").toByteArray(); m_mainWindowState = getConfigFromSettings("session", "main_window_state").toByteArray(); - // Update notebooks - readNotebookFromSettings(); - updateMarkdownEditStyle(); } @@ -87,25 +84,23 @@ void VConfigManager::readPredefinedColorsFromSettings() << "pre-defined colors from [predefined_colors] section"; } -void VConfigManager::readNotebookFromSettings() +void VConfigManager::readNotebookFromSettings(QVector &p_notebooks, QObject *parent) { - notebooks.clear(); + Q_ASSERT(p_notebooks.isEmpty()); int size = userSettings->beginReadArray("notebooks"); for (int i = 0; i < size; ++i) { userSettings->setArrayIndex(i); - VNotebook notebook; QString name = userSettings->value("name").toString(); QString path = userSettings->value("path").toString(); - notebook.setName(name); - notebook.setPath(path); - notebooks.append(notebook); + VNotebook *notebook = new VNotebook(name, path, parent); + p_notebooks.append(notebook); } userSettings->endArray(); - qDebug() << "read" << notebooks.size() + qDebug() << "read" << p_notebooks.size() << "notebook items from [notebooks] section"; } -void VConfigManager::writeNotebookToSettings() +void VConfigManager::writeNotebookToSettings(const QVector &p_notebooks) { // Clear it first userSettings->beginGroup("notebooks"); @@ -113,13 +108,14 @@ void VConfigManager::writeNotebookToSettings() userSettings->endGroup(); userSettings->beginWriteArray("notebooks"); - for (int i = 0; i < notebooks.size(); ++i) { + for (int i = 0; i < p_notebooks.size(); ++i) { userSettings->setArrayIndex(i); - userSettings->setValue("name", notebooks[i].getName()); - userSettings->setValue("path", notebooks[i].getPath()); + const VNotebook ¬ebook = *p_notebooks[i]; + userSettings->setValue("name", notebook.getName()); + userSettings->setValue("path", notebook.getPath()); } userSettings->endArray(); - qDebug() << "write" << notebooks.size() + qDebug() << "write" << p_notebooks.size() << "notebook items in [notebooks] section"; } diff --git a/src/vconfigmanager.h b/src/vconfigmanager.h index 4246897f..2ad322e1 100644 --- a/src/vconfigmanager.h +++ b/src/vconfigmanager.h @@ -60,8 +60,8 @@ public: inline int getCurNotebookIndex() const; inline void setCurNotebookIndex(int index); - inline const QVector& getNotebooks() const; - inline void setNotebooks(const QVector ¬ebooks); + inline void getNotebooks(QVector &p_notebooks, QObject *parent); + inline void setNotebooks(const QVector &p_notebooks); inline hoedown_extensions getMarkdownExtensions() const; inline MarkdownConverterType getMdConverterType() const; @@ -96,8 +96,8 @@ private: void updateMarkdownEditStyle(); QVariant getConfigFromSettings(const QString §ion, const QString &key); void setConfigToSettings(const QString §ion, const QString &key, const QVariant &value); - void readNotebookFromSettings(); - void writeNotebookToSettings(); + void readNotebookFromSettings(QVector &p_notebooks, QObject *parent); + void writeNotebookToSettings(const QVector &p_notebooks); void readPredefinedColorsFromSettings(); // Update baseEditPalette according to curBackgroundColor void updatePaletteColor(); @@ -113,7 +113,6 @@ private: QString postTemplatePath; QString templateCssUrl; int curNotebookIndex; - QVector notebooks; // Markdown Converter hoedown_extensions markdownExtensions; @@ -199,15 +198,14 @@ inline void VConfigManager::setCurNotebookIndex(int index) setConfigToSettings("global", "current_notebook", index); } -inline const QVector& VConfigManager::getNotebooks() const +inline void VConfigManager::getNotebooks(QVector &p_notebooks, QObject *parent) { - return notebooks; + readNotebookFromSettings(p_notebooks, parent); } -inline void VConfigManager::setNotebooks(const QVector ¬ebooks) +inline void VConfigManager::setNotebooks(const QVector &p_notebooks) { - this->notebooks = notebooks; - writeNotebookToSettings(); + writeNotebookToSettings(p_notebooks); } inline hoedown_extensions VConfigManager::getMarkdownExtensions() const diff --git a/src/vdirectorytree.cpp b/src/vdirectorytree.cpp index 9de6b06b..4b23453f 100644 --- a/src/vdirectorytree.cpp +++ b/src/vdirectorytree.cpp @@ -64,10 +64,10 @@ void VDirectoryTree::setNotebook(const QString& notebookName) clear(); return; } - const QVector ¬ebooks = vnote->getNotebooks(); + const QVector ¬ebooks = vnote->getNotebooks(); for (int i = 0; i < notebooks.size(); ++i) { - if (notebooks[i].getName() == notebook) { - treePath = notebooks[i].getPath(); + if (notebooks[i]->getName() == notebook) { + treePath = notebooks[i]->getPath(); break; } } @@ -585,12 +585,3 @@ void VDirectoryTree::renameDirectory(QTreeWidgetItem *item, const QString &newNa qDebug() << "directory renamed" << oldPath << "to" << newPath; emit directoryRenamed(notebook, oldPath, newPath); } - -void VDirectoryTree::handleNotebookRenamed(const QVector ¬ebooks, - const QString &oldName, const QString &newName) -{ - if (oldName == notebook) { - notebook = newName; - qDebug() << "directoryTree update notebook" << oldName << "to" << newName; - } -} diff --git a/src/vdirectorytree.h b/src/vdirectorytree.h index 286a25c8..47acc5c1 100644 --- a/src/vdirectorytree.h +++ b/src/vdirectorytree.h @@ -23,8 +23,6 @@ public slots: void newRootDirectory(); void deleteDirectory(); void editDirectoryInfo(); - void handleNotebookRenamed(const QVector ¬ebooks, const QString &oldName, - const QString &newName); private slots: // Read config file and pdate the subtree of @item in the directory tree. diff --git a/src/veditarea.cpp b/src/veditarea.cpp index ae6bede8..6a1eb9a9 100644 --- a/src/veditarea.cpp +++ b/src/veditarea.cpp @@ -233,17 +233,6 @@ void VEditArea::saveAndReadFile() win->saveAndReadFile(); } -void VEditArea::handleNotebookRenamed(const QVector ¬ebooks, - const QString &oldName, const QString &newName) -{ - int nrWin = splitter->count(); - for (int i = 0; i < nrWin; ++i) { - VEditWindow *win = getWindow(i); - win->handleNotebookRenamed(notebooks, oldName, newName); - } - updateWindowStatus(); -} - void VEditArea::handleDirectoryRenamed(const QString ¬ebook, const QString &oldRelativePath, const QString &newRelativePath) { diff --git a/src/veditarea.h b/src/veditarea.h index d0b25e4b..206165ba 100644 --- a/src/veditarea.h +++ b/src/veditarea.h @@ -40,8 +40,6 @@ public slots: void saveFile(); void readFile(); void saveAndReadFile(); - void handleNotebookRenamed(const QVector ¬ebooks, const QString &oldName, - const QString &newName); void handleOutlineItemActivated(const VAnchor &anchor); void handleDirectoryRenamed(const QString ¬ebook, const QString &oldRelativePath, const QString &newRelativePath); diff --git a/src/veditwindow.cpp b/src/veditwindow.cpp index 124170f9..00eeaabf 100644 --- a/src/veditwindow.cpp +++ b/src/veditwindow.cpp @@ -260,22 +260,6 @@ void VEditWindow::saveFile() editor->saveFile(); } -void VEditWindow::handleNotebookRenamed(const QVector ¬ebooks, - const QString &oldName, const QString &newName) -{ - QTabBar *tabs = tabBar(); - int nrTabs = tabs->count(); - for (int i = 0; i < nrTabs; ++i) { - QJsonObject tabJson = tabs->tabData(i).toJsonObject(); - if (tabJson["notebook"] == oldName) { - tabJson["notebook"] = newName; - tabs->setTabData(i, tabJson); - tabs->setTabToolTip(i, generateTooltip(tabJson)); - } - } - updateTabListMenu(); -} - void VEditWindow::handleDirectoryRenamed(const QString ¬ebook, const QString &oldRelativePath, const QString &newRelativePath) { diff --git a/src/veditwindow.h b/src/veditwindow.h index bfcce0dd..c3a6494b 100644 --- a/src/veditwindow.h +++ b/src/veditwindow.h @@ -27,8 +27,6 @@ public: void saveFile(); void readFile(); void saveAndReadFile(); - void handleNotebookRenamed(const QVector ¬ebooks, const QString &oldName, - const QString &newName); bool closeAllFiles(bool p_forced); void setRemoveSplitEnable(bool enabled); void requestUpdateTabStatus(); diff --git a/src/vfilelist.cpp b/src/vfilelist.cpp index 1c261e64..a93761c1 100644 --- a/src/vfilelist.cpp +++ b/src/vfilelist.cpp @@ -83,10 +83,10 @@ void VFileList::setDirectory(QJsonObject dirJson) notebook = dirJson["notebook"].toString(); relativePath = dirJson["relative_path"].toString(); rootPath = ""; - const QVector ¬ebooks = vnote->getNotebooks(); + const QVector ¬ebooks = vnote->getNotebooks(); for (int i = 0; i < notebooks.size(); ++i) { - if (notebooks[i].getName() == notebook) { - rootPath = notebooks[i].getPath(); + if (notebooks[i]->getName() == notebook) { + rootPath = notebooks[i]->getPath(); break; } } @@ -430,14 +430,6 @@ bool VFileList::importFile(const QString &name) return insertFileListItem(fileJson, true); } -void VFileList::handleNotebookRenamed(const QVector ¬ebooks, - const QString &oldName, const QString &newName) -{ - if (oldName == notebook) { - notebook = newName; - } -} - void VFileList::handleDirectoryRenamed(const QString ¬ebook, const QString &oldRelativePath, const QString &newRelativePath) { diff --git a/src/vfilelist.h b/src/vfilelist.h index efb5a604..2050bba1 100644 --- a/src/vfilelist.h +++ b/src/vfilelist.h @@ -44,8 +44,6 @@ private slots: public slots: void setDirectory(QJsonObject dirJson); - void handleNotebookRenamed(const QVector ¬ebooks, const QString &oldName, - const QString &newName); void handleDirectoryRenamed(const QString ¬ebook, const QString &oldRelativePath, const QString &newRelativePath); void newFile(); diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index e8748306..948ea2d3 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -18,7 +18,7 @@ VMainWindow::VMainWindow(QWidget *parent) { setWindowIcon(QIcon(":/resources/icons/vnote.ico")); // Must be called before those who uses VConfigManager - vnote = new VNote(); + vnote = new VNote(this); vnote->initPalette(palette()); initPredefinedColorPixmaps(); setupUI(); @@ -31,11 +31,6 @@ VMainWindow::VMainWindow(QWidget *parent) updateNotebookComboBox(vnote->getNotebooks()); } -VMainWindow::~VMainWindow() -{ - delete vnote; -} - void VMainWindow::setupUI() { QWidget *directoryPanel = setupDirectoryPanel(); @@ -60,8 +55,6 @@ void VMainWindow::setupUI() connect(notebookComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setCurNotebookIndex(int))); - connect(vnote, &VNote::notebooksRenamed, - fileList, &VFileList::handleNotebookRenamed); connect(directoryTree, &VDirectoryTree::currentDirectoryChanged, fileList, &VFileList::setDirectory); connect(directoryTree, &VDirectoryTree::directoryRenamed, @@ -75,8 +68,6 @@ void VMainWindow::setupUI() editArea, &VEditArea::closeFile); connect(fileList, &VFileList::fileCreated, editArea, &VEditArea::openFile); - connect(vnote, &VNote::notebooksRenamed, - editArea, &VEditArea::handleNotebookRenamed); connect(editArea, &VEditArea::curTabStatusChanged, this, &VMainWindow::handleCurTabStatusChanged); connect(directoryTree, &VDirectoryTree::directoryRenamed, @@ -91,19 +82,15 @@ void VMainWindow::setupUI() connect(notebookInfoBtn, &QPushButton::clicked, this, &VMainWindow::onNotebookInfoBtnClicked); - connect(vnote, &VNote::notebooksChanged, - this, &VMainWindow::updateNotebookComboBox); - connect(vnote, &VNote::notebooksDeleted, + connect(vnote, &VNote::notebookDeleted, this, &VMainWindow::notebookComboBoxDeleted); - connect(vnote, &VNote::notebooksRenamed, + connect(vnote, &VNote::notebookRenamed, this, &VMainWindow::notebookComboBoxRenamed); - connect(vnote, &VNote::notebooksAdded, + connect(vnote, &VNote::notebookAdded, this, &VMainWindow::notebookComboBoxAdded); connect(this, &VMainWindow::curNotebookChanged, directoryTree, &VDirectoryTree::setNotebook); - connect(vnote, &VNote::notebooksRenamed, - directoryTree, &VDirectoryTree::handleNotebookRenamed); setCentralWidget(mainSplitter); // Create and show the status bar @@ -383,18 +370,18 @@ void VMainWindow::initDockWindows() viewMenu->addAction(toolDock->toggleViewAction()); } -void VMainWindow::updateNotebookComboBox(const QVector ¬ebooks) +void VMainWindow::updateNotebookComboBox(const QVector &p_notebooks) { notebookComboMuted = true; notebookComboBox->clear(); - for (int i = 0; i < notebooks.size(); ++i) { + for (int i = 0; i < p_notebooks.size(); ++i) { notebookComboBox->addItem(QIcon(":/resources/icons/notebook_item.svg"), - notebooks[i].getName()); + p_notebooks[i]->getName()); } notebookComboMuted = false; int index = vconfig.getCurNotebookIndex(); - if (notebooks.isEmpty()) { + if (p_notebooks.isEmpty()) { index = -1; } if (notebookComboBox->currentIndex() == index) { @@ -404,11 +391,11 @@ void VMainWindow::updateNotebookComboBox(const QVector ¬ebooks) } } -void VMainWindow::notebookComboBoxAdded(const QVector ¬ebooks, int idx) +void VMainWindow::notebookComboBoxAdded(const VNotebook *p_notebook, int p_idx) { notebookComboMuted = true; - notebookComboBox->insertItem(idx, QIcon(":/resources/icons/notebook_item.svg"), - notebooks[idx].getName()); + notebookComboBox->insertItem(p_idx, QIcon(":/resources/icons/notebook_item.svg"), + p_notebook->getName()); notebookComboMuted = false; if (notebookComboBox->currentIndex() == vconfig.getCurNotebookIndex()) { setCurNotebookIndex(vconfig.getCurNotebookIndex()); @@ -417,16 +404,11 @@ void VMainWindow::notebookComboBoxAdded(const QVector ¬ebooks, int } } -void VMainWindow::notebookComboBoxDeleted(const QVector ¬ebooks, const QString &deletedName) +void VMainWindow::notebookComboBoxDeleted(int p_oriIdx) { + Q_ASSERT(p_oriIdx >= 0 && p_oriIdx < notebookComboBox->count()); notebookComboMuted = true; - int nrItem = notebookComboBox->count(); - for (int i = 0; i < nrItem; ++i) { - if (notebookComboBox->itemText(i) == deletedName) { - notebookComboBox->removeItem(i); - break; - } - } + notebookComboBox->removeItem(p_oriIdx); notebookComboMuted = false; if (notebookComboBox->currentIndex() == vconfig.getCurNotebookIndex()) { @@ -434,19 +416,12 @@ void VMainWindow::notebookComboBoxDeleted(const QVector ¬ebooks, c } else { notebookComboBox->setCurrentIndex(vconfig.getCurNotebookIndex()); } - Q_ASSERT(notebooks.size() == notebookComboBox->count()); } -void VMainWindow::notebookComboBoxRenamed(const QVector ¬ebooks, - const QString &oldName, const QString &newName) +void VMainWindow::notebookComboBoxRenamed(const VNotebook *p_notebook, int p_idx) { - int nrItem = notebookComboBox->count(); - for (int i = 0; i < nrItem; ++i) { - if (notebookComboBox->itemText(i) == oldName) { - notebookComboBox->setItemText(i, newName); - break; - } - } + Q_ASSERT(p_idx >= 0 && p_idx < notebookComboBox->count()); + notebookComboBox->setItemText(p_idx, p_notebook->getName()); // Renaming a notebook won't change current index } @@ -461,7 +436,7 @@ void VMainWindow::setCurNotebookIndex(int index) QString notebook; if (index > -1) { vconfig.setCurNotebookIndex(index); - notebook = vnote->getNotebooks()[index].getName(); + notebook = vnote->getNotebooks()[index]->getName(); newRootDirAct->setEnabled(true); } else { newRootDirAct->setEnabled(false); @@ -494,9 +469,9 @@ void VMainWindow::onNewNotebookBtnClicked() bool VMainWindow::isConflictWithExistingNotebooks(const QString &name) { - const QVector ¬ebooks = vnote->getNotebooks(); + const QVector ¬ebooks = vnote->getNotebooks(); for (int i = 0; i < notebooks.size(); ++i) { - if (notebooks[i].getName() == name) { + if (notebooks[i]->getName() == name) { return true; } } @@ -506,15 +481,15 @@ bool VMainWindow::isConflictWithExistingNotebooks(const QString &name) void VMainWindow::onDeleteNotebookBtnClicked() { int curIndex = notebookComboBox->currentIndex(); - QString curName = vnote->getNotebooks()[curIndex].getName(); - QString curPath = vnote->getNotebooks()[curIndex].getPath(); + QString curName = vnote->getNotebooks()[curIndex]->getName(); + QString curPath = vnote->getNotebooks()[curIndex]->getPath(); QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), QString("Are you sure you want to delete notebook \"%1\"?") .arg(curName), QMessageBox::Ok | QMessageBox::Cancel, this); msgBox.setInformativeText(QString("This will delete any files in this notebook (\"%1\").").arg(curPath)); msgBox.setDefaultButton(QMessageBox::Cancel); if (msgBox.exec() == QMessageBox::Ok) { - vnote->removeNotebook(curName); + vnote->removeNotebook(curIndex); } } @@ -525,8 +500,8 @@ void VMainWindow::onNotebookInfoBtnClicked() return; } QString info; - QString curName = vnote->getNotebooks()[curIndex].getName(); - QString defaultPath = vnote->getNotebooks()[curIndex].getPath(); + QString curName = vnote->getNotebooks()[curIndex]->getName(); + QString defaultPath = vnote->getNotebooks()[curIndex]->getPath(); QString defaultName(curName); do { VNotebookInfoDialog dialog(tr("Notebook information"), info, defaultName, @@ -541,7 +516,7 @@ void VMainWindow::onNotebookInfoBtnClicked() defaultName = name; continue; } - vnote->renameNotebook(curName, name); + vnote->renameNotebook(curIndex, name); } break; } while (true); diff --git a/src/vmainwindow.h b/src/vmainwindow.h index 68c43843..5eec4ceb 100644 --- a/src/vmainwindow.h +++ b/src/vmainwindow.h @@ -29,7 +29,6 @@ class VMainWindow : public QMainWindow public: VMainWindow(QWidget *parent = 0); - ~VMainWindow(); const QVector > &getPalette() const; private slots: @@ -38,11 +37,10 @@ private slots: void onNewNotebookBtnClicked(); void onDeleteNotebookBtnClicked(); void onNotebookInfoBtnClicked(); - void updateNotebookComboBox(const QVector ¬ebooks); - void notebookComboBoxAdded(const QVector ¬ebooks, int idx); - void notebookComboBoxDeleted(const QVector ¬ebooks, const QString &deletedName); - void notebookComboBoxRenamed(const QVector ¬ebooks, - const QString &oldName, const QString &newName); + void updateNotebookComboBox(const QVector &p_notebooks); + void notebookComboBoxAdded(const VNotebook *p_notebook, int p_idx); + void notebookComboBoxDeleted(int p_oriIdx); + void notebookComboBoxRenamed(const VNotebook *p_notebook, int p_idx); void importNoteFromFile(); void changeMarkdownConverter(QAction *action); void aboutMessage(); diff --git a/src/vnote.cpp b/src/vnote.cpp index 3cf635c3..b2883688 100644 --- a/src/vnote.cpp +++ b/src/vnote.cpp @@ -13,12 +13,12 @@ QString VNote::templateHtml; QString VNote::preTemplateHtml; QString VNote::postTemplateHtml; -VNote::VNote() : QObject() +VNote::VNote(QObject *parent) + : QObject(parent) { vconfig.initialize(); initTemplate(); - notebooks = vconfig.getNotebooks(); - emit notebooksChanged(notebooks); + vconfig.getNotebooks(m_notebooks, this); } void VNote::initPalette(QPalette palette) @@ -75,9 +75,9 @@ void VNote::updateTemplate() postTemplateHtml = VUtils::readFileFromDisk(vconfig.getPostTemplatePath()); } -const QVector& VNote::getNotebooks() +const QVector &VNote::getNotebooks() { - return notebooks; + return m_notebooks; } void VNote::createNotebook(const QString &name, const QString &path) @@ -92,78 +92,66 @@ void VNote::createNotebook(const QString &name, const QString &path) } // Update notebooks settings - notebooks.prepend(VNotebook(name, path)); - vconfig.setNotebooks(notebooks); + VNotebook *nb = new VNotebook(name, path, this); + m_notebooks.prepend(nb); + vconfig.setNotebooks(m_notebooks); // Set current index to the new notebook vconfig.setCurNotebookIndex(0); - emit notebooksAdded(notebooks, 0); + emit notebookAdded(nb, 0); } -void VNote::removeNotebook(const QString &name) +void VNote::removeNotebook(int idx) { - // Update notebooks settings - QString path; + Q_ASSERT(idx >= 0 && idx < m_notebooks.size()); + VNotebook *nb = m_notebooks[idx]; + QString name = nb->getName(); + QString path = nb->getPath(); + + // Update notebook settings int curIndex = vconfig.getCurNotebookIndex(); - 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.remove(index); - vconfig.setNotebooks(notebooks); - if (notebooks.isEmpty()) { + m_notebooks.remove(idx); + vconfig.setNotebooks(m_notebooks); + if (m_notebooks.isEmpty()) { vconfig.setCurNotebookIndex(-1); - } else if (index == curIndex) { + } else if (idx == curIndex) { vconfig.setCurNotebookIndex(0); } + // Close all the directory and files + notebookPathHash.remove(name); + nb->close(true); + delete nb; + qDebug() << "notebook" << name << "deleted"; + // Delete the directory QDir dir(path); if (!dir.removeRecursively()) { - qWarning() << "error: fail to delete" << path << "recursively"; - } else { - qDebug() << "delete" << path << "recursively"; + qWarning() << "failed to delete" << path << "recursively"; } - - notebookPathHash.remove(name); - emit notebooksDeleted(notebooks, name); + emit notebookDeleted(idx); } -void VNote::renameNotebook(const QString &name, const QString &newName) +void VNote::renameNotebook(int idx, 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); - + Q_ASSERT(idx >= 0 && idx < m_notebooks.size()); + VNotebook *nb = m_notebooks[idx]; + QString name = nb->getName(); notebookPathHash.remove(name); - emit notebooksRenamed(notebooks, name, newName); + nb->setName(newName); + vconfig.setNotebooks(m_notebooks); + + emit notebookRenamed(nb, idx); } QString VNote::getNotebookPath(const QString &name) { QString path = notebookPathHash.value(name); if (path.isEmpty()) { - for (int i = 0; i < notebooks.size(); ++i) { - if (notebooks[i].getName() == name) { - path = notebooks[i].getPath(); + for (int i = 0; i < m_notebooks.size(); ++i) { + if (m_notebooks[i]->getName() == name) { + path = m_notebooks[i]->getPath(); break; } } diff --git a/src/vnote.h b/src/vnote.h index 450b56d7..d4c4c674 100644 --- a/src/vnote.h +++ b/src/vnote.h @@ -17,9 +17,9 @@ class VNote : public QObject { Q_OBJECT public: - VNote(); + VNote(QObject *parent = 0); - const QVector& getNotebooks(); + const QVector& getNotebooks(); void initTemplate(); @@ -31,8 +31,8 @@ public: static QString postTemplateHtml; void createNotebook(const QString &name, const QString &path); - void removeNotebook(const QString &name); - void renameNotebook(const QString &name, const QString &newName); + void removeNotebook(int idx); + void renameNotebook(int idx, const QString &newName); QString getNotebookPath(const QString &name); @@ -43,15 +43,13 @@ public slots: void updateTemplate(); signals: - // Force to do a fully update - void notebooksChanged(const QVector ¬ebooks); - void notebooksAdded(const QVector ¬ebooks, int idx); - void notebooksDeleted(const QVector ¬ebooks, const QString &deletedName); - void notebooksRenamed(const QVector ¬ebooks, - const QString &oldName, const QString &newName); + void notebookAdded(const VNotebook *p_notebook, int p_idx); + void notebookDeleted(int p_oriIdx); + void notebookRenamed(const VNotebook *p_notebook, int p_idx); private: - QVector notebooks; + // Maintain all the notebooks. Other holder should use QPointer. + QVector m_notebooks; QHash notebookPathHash; QVector > m_palette; }; diff --git a/src/vnotebook.cpp b/src/vnotebook.cpp index f24fc0ab..0f6223c9 100644 --- a/src/vnotebook.cpp +++ b/src/vnotebook.cpp @@ -1,31 +1,37 @@ #include "vnotebook.h" -VNotebook::VNotebook() +VNotebook::VNotebook(QObject *parent) + : QObject(parent) { } -VNotebook::VNotebook(const QString &name, const QString &path) - : name(name), path(path) +VNotebook::VNotebook(const QString &name, const QString &path, QObject *parent) + : QObject(parent), m_name(name), m_path(path) { } QString VNotebook::getName() const { - return this->name; + return m_name; } QString VNotebook::getPath() const { - return this->path; + return m_path; } void VNotebook::setName(const QString &name) { - this->name = name; + m_name = name; } void VNotebook::setPath(const QString &path) { - this->path = path; + m_path = path; +} + +void VNotebook::close(bool p_forced) +{ + //TODO } diff --git a/src/vnotebook.h b/src/vnotebook.h index 77ce4ed3..03716575 100644 --- a/src/vnotebook.h +++ b/src/vnotebook.h @@ -1,13 +1,19 @@ #ifndef VNOTEBOOK_H #define VNOTEBOOK_H +#include #include -class VNotebook +class VNotebook : public QObject { + Q_OBJECT public: - VNotebook(); - VNotebook(const QString &name, const QString &path); + VNotebook(QObject *parent = 0); + VNotebook(const QString &name, const QString &path, QObject *parent = 0); + + // Close all the directory and files of this notebook. + // If @p_forced, unsaved files will also be closed without a confirm. + void close(bool p_forced); QString getName() const; QString getPath() const; @@ -15,8 +21,8 @@ public: void setPath(const QString &path); private: - QString name; - QString path; + QString m_name; + QString m_path; }; #endif // VNOTEBOOK_H