refactor: VNote and VNotebook

regression: notebook renaming logics does not work for now

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-11-25 10:09:32 +08:00
parent 013b6cecc2
commit 8e8d69c85f
16 changed files with 127 additions and 214 deletions

View File

@ -65,9 +65,6 @@ void VConfigManager::initialize()
m_mainWindowGeometry = getConfigFromSettings("session", "main_window_geometry").toByteArray(); m_mainWindowGeometry = getConfigFromSettings("session", "main_window_geometry").toByteArray();
m_mainWindowState = getConfigFromSettings("session", "main_window_state").toByteArray(); m_mainWindowState = getConfigFromSettings("session", "main_window_state").toByteArray();
// Update notebooks
readNotebookFromSettings();
updateMarkdownEditStyle(); updateMarkdownEditStyle();
} }
@ -87,25 +84,23 @@ void VConfigManager::readPredefinedColorsFromSettings()
<< "pre-defined colors from [predefined_colors] section"; << "pre-defined colors from [predefined_colors] section";
} }
void VConfigManager::readNotebookFromSettings() void VConfigManager::readNotebookFromSettings(QVector<VNotebook *> &p_notebooks, QObject *parent)
{ {
notebooks.clear(); Q_ASSERT(p_notebooks.isEmpty());
int size = userSettings->beginReadArray("notebooks"); int size = userSettings->beginReadArray("notebooks");
for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) {
userSettings->setArrayIndex(i); userSettings->setArrayIndex(i);
VNotebook notebook;
QString name = userSettings->value("name").toString(); QString name = userSettings->value("name").toString();
QString path = userSettings->value("path").toString(); QString path = userSettings->value("path").toString();
notebook.setName(name); VNotebook *notebook = new VNotebook(name, path, parent);
notebook.setPath(path); p_notebooks.append(notebook);
notebooks.append(notebook);
} }
userSettings->endArray(); userSettings->endArray();
qDebug() << "read" << notebooks.size() qDebug() << "read" << p_notebooks.size()
<< "notebook items from [notebooks] section"; << "notebook items from [notebooks] section";
} }
void VConfigManager::writeNotebookToSettings() void VConfigManager::writeNotebookToSettings(const QVector<VNotebook *> &p_notebooks)
{ {
// Clear it first // Clear it first
userSettings->beginGroup("notebooks"); userSettings->beginGroup("notebooks");
@ -113,13 +108,14 @@ void VConfigManager::writeNotebookToSettings()
userSettings->endGroup(); userSettings->endGroup();
userSettings->beginWriteArray("notebooks"); userSettings->beginWriteArray("notebooks");
for (int i = 0; i < notebooks.size(); ++i) { for (int i = 0; i < p_notebooks.size(); ++i) {
userSettings->setArrayIndex(i); userSettings->setArrayIndex(i);
userSettings->setValue("name", notebooks[i].getName()); const VNotebook &notebook = *p_notebooks[i];
userSettings->setValue("path", notebooks[i].getPath()); userSettings->setValue("name", notebook.getName());
userSettings->setValue("path", notebook.getPath());
} }
userSettings->endArray(); userSettings->endArray();
qDebug() << "write" << notebooks.size() qDebug() << "write" << p_notebooks.size()
<< "notebook items in [notebooks] section"; << "notebook items in [notebooks] section";
} }

View File

@ -60,8 +60,8 @@ public:
inline int getCurNotebookIndex() const; inline int getCurNotebookIndex() const;
inline void setCurNotebookIndex(int index); inline void setCurNotebookIndex(int index);
inline const QVector<VNotebook>& getNotebooks() const; inline void getNotebooks(QVector<VNotebook *> &p_notebooks, QObject *parent);
inline void setNotebooks(const QVector<VNotebook> &notebooks); inline void setNotebooks(const QVector<VNotebook *> &p_notebooks);
inline hoedown_extensions getMarkdownExtensions() const; inline hoedown_extensions getMarkdownExtensions() const;
inline MarkdownConverterType getMdConverterType() const; inline MarkdownConverterType getMdConverterType() const;
@ -96,8 +96,8 @@ private:
void updateMarkdownEditStyle(); void updateMarkdownEditStyle();
QVariant getConfigFromSettings(const QString &section, const QString &key); QVariant getConfigFromSettings(const QString &section, const QString &key);
void setConfigToSettings(const QString &section, const QString &key, const QVariant &value); void setConfigToSettings(const QString &section, const QString &key, const QVariant &value);
void readNotebookFromSettings(); void readNotebookFromSettings(QVector<VNotebook *> &p_notebooks, QObject *parent);
void writeNotebookToSettings(); void writeNotebookToSettings(const QVector<VNotebook *> &p_notebooks);
void readPredefinedColorsFromSettings(); void readPredefinedColorsFromSettings();
// Update baseEditPalette according to curBackgroundColor // Update baseEditPalette according to curBackgroundColor
void updatePaletteColor(); void updatePaletteColor();
@ -113,7 +113,6 @@ private:
QString postTemplatePath; QString postTemplatePath;
QString templateCssUrl; QString templateCssUrl;
int curNotebookIndex; int curNotebookIndex;
QVector<VNotebook> notebooks;
// Markdown Converter // Markdown Converter
hoedown_extensions markdownExtensions; hoedown_extensions markdownExtensions;
@ -199,15 +198,14 @@ inline void VConfigManager::setCurNotebookIndex(int index)
setConfigToSettings("global", "current_notebook", index); setConfigToSettings("global", "current_notebook", index);
} }
inline const QVector<VNotebook>& VConfigManager::getNotebooks() const inline void VConfigManager::getNotebooks(QVector<VNotebook *> &p_notebooks, QObject *parent)
{ {
return notebooks; readNotebookFromSettings(p_notebooks, parent);
} }
inline void VConfigManager::setNotebooks(const QVector<VNotebook> &notebooks) inline void VConfigManager::setNotebooks(const QVector<VNotebook *> &p_notebooks)
{ {
this->notebooks = notebooks; writeNotebookToSettings(p_notebooks);
writeNotebookToSettings();
} }
inline hoedown_extensions VConfigManager::getMarkdownExtensions() const inline hoedown_extensions VConfigManager::getMarkdownExtensions() const

View File

@ -64,10 +64,10 @@ void VDirectoryTree::setNotebook(const QString& notebookName)
clear(); clear();
return; return;
} }
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() == notebook) { if (notebooks[i]->getName() == notebook) {
treePath = notebooks[i].getPath(); treePath = notebooks[i]->getPath();
break; break;
} }
} }
@ -585,12 +585,3 @@ void VDirectoryTree::renameDirectory(QTreeWidgetItem *item, const QString &newNa
qDebug() << "directory renamed" << oldPath << "to" << newPath; qDebug() << "directory renamed" << oldPath << "to" << newPath;
emit directoryRenamed(notebook, oldPath, newPath); emit directoryRenamed(notebook, oldPath, newPath);
} }
void VDirectoryTree::handleNotebookRenamed(const QVector<VNotebook> &notebooks,
const QString &oldName, const QString &newName)
{
if (oldName == notebook) {
notebook = newName;
qDebug() << "directoryTree update notebook" << oldName << "to" << newName;
}
}

View File

@ -23,8 +23,6 @@ public slots:
void newRootDirectory(); void newRootDirectory();
void deleteDirectory(); void deleteDirectory();
void editDirectoryInfo(); void editDirectoryInfo();
void handleNotebookRenamed(const QVector<VNotebook> &notebooks, const QString &oldName,
const QString &newName);
private slots: private slots:
// Read config file and pdate the subtree of @item in the directory tree. // Read config file and pdate the subtree of @item in the directory tree.

View File

@ -233,17 +233,6 @@ void VEditArea::saveAndReadFile()
win->saveAndReadFile(); win->saveAndReadFile();
} }
void VEditArea::handleNotebookRenamed(const QVector<VNotebook> &notebooks,
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 &notebook, const QString &oldRelativePath, void VEditArea::handleDirectoryRenamed(const QString &notebook, const QString &oldRelativePath,
const QString &newRelativePath) const QString &newRelativePath)
{ {

View File

@ -40,8 +40,6 @@ public slots:
void saveFile(); void saveFile();
void readFile(); void readFile();
void saveAndReadFile(); void saveAndReadFile();
void handleNotebookRenamed(const QVector<VNotebook> &notebooks, const QString &oldName,
const QString &newName);
void handleOutlineItemActivated(const VAnchor &anchor); void handleOutlineItemActivated(const VAnchor &anchor);
void handleDirectoryRenamed(const QString &notebook, void handleDirectoryRenamed(const QString &notebook,
const QString &oldRelativePath, const QString &newRelativePath); const QString &oldRelativePath, const QString &newRelativePath);

View File

@ -260,22 +260,6 @@ void VEditWindow::saveFile()
editor->saveFile(); editor->saveFile();
} }
void VEditWindow::handleNotebookRenamed(const QVector<VNotebook> &notebooks,
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 &notebook, const QString &oldRelativePath, void VEditWindow::handleDirectoryRenamed(const QString &notebook, const QString &oldRelativePath,
const QString &newRelativePath) const QString &newRelativePath)
{ {

View File

@ -27,8 +27,6 @@ public:
void saveFile(); void saveFile();
void readFile(); void readFile();
void saveAndReadFile(); void saveAndReadFile();
void handleNotebookRenamed(const QVector<VNotebook> &notebooks, const QString &oldName,
const QString &newName);
bool closeAllFiles(bool p_forced); bool closeAllFiles(bool p_forced);
void setRemoveSplitEnable(bool enabled); void setRemoveSplitEnable(bool enabled);
void requestUpdateTabStatus(); void requestUpdateTabStatus();

View File

@ -83,10 +83,10 @@ void VFileList::setDirectory(QJsonObject dirJson)
notebook = dirJson["notebook"].toString(); notebook = dirJson["notebook"].toString();
relativePath = dirJson["relative_path"].toString(); relativePath = dirJson["relative_path"].toString();
rootPath = ""; rootPath = "";
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() == notebook) { if (notebooks[i]->getName() == notebook) {
rootPath = notebooks[i].getPath(); rootPath = notebooks[i]->getPath();
break; break;
} }
} }
@ -430,14 +430,6 @@ bool VFileList::importFile(const QString &name)
return insertFileListItem(fileJson, true); return insertFileListItem(fileJson, true);
} }
void VFileList::handleNotebookRenamed(const QVector<VNotebook> &notebooks,
const QString &oldName, const QString &newName)
{
if (oldName == notebook) {
notebook = newName;
}
}
void VFileList::handleDirectoryRenamed(const QString &notebook, void VFileList::handleDirectoryRenamed(const QString &notebook,
const QString &oldRelativePath, const QString &newRelativePath) const QString &oldRelativePath, const QString &newRelativePath)
{ {

View File

@ -44,8 +44,6 @@ private slots:
public slots: public slots:
void setDirectory(QJsonObject dirJson); void setDirectory(QJsonObject dirJson);
void handleNotebookRenamed(const QVector<VNotebook> &notebooks, const QString &oldName,
const QString &newName);
void handleDirectoryRenamed(const QString &notebook, const QString &oldRelativePath, void handleDirectoryRenamed(const QString &notebook, const QString &oldRelativePath,
const QString &newRelativePath); const QString &newRelativePath);
void newFile(); void newFile();

View File

@ -18,7 +18,7 @@ VMainWindow::VMainWindow(QWidget *parent)
{ {
setWindowIcon(QIcon(":/resources/icons/vnote.ico")); setWindowIcon(QIcon(":/resources/icons/vnote.ico"));
// Must be called before those who uses VConfigManager // Must be called before those who uses VConfigManager
vnote = new VNote(); vnote = new VNote(this);
vnote->initPalette(palette()); vnote->initPalette(palette());
initPredefinedColorPixmaps(); initPredefinedColorPixmaps();
setupUI(); setupUI();
@ -31,11 +31,6 @@ VMainWindow::VMainWindow(QWidget *parent)
updateNotebookComboBox(vnote->getNotebooks()); updateNotebookComboBox(vnote->getNotebooks());
} }
VMainWindow::~VMainWindow()
{
delete vnote;
}
void VMainWindow::setupUI() void VMainWindow::setupUI()
{ {
QWidget *directoryPanel = setupDirectoryPanel(); QWidget *directoryPanel = setupDirectoryPanel();
@ -60,8 +55,6 @@ void VMainWindow::setupUI()
connect(notebookComboBox, SIGNAL(currentIndexChanged(int)), this, connect(notebookComboBox, SIGNAL(currentIndexChanged(int)), this,
SLOT(setCurNotebookIndex(int))); SLOT(setCurNotebookIndex(int)));
connect(vnote, &VNote::notebooksRenamed,
fileList, &VFileList::handleNotebookRenamed);
connect(directoryTree, &VDirectoryTree::currentDirectoryChanged, connect(directoryTree, &VDirectoryTree::currentDirectoryChanged,
fileList, &VFileList::setDirectory); fileList, &VFileList::setDirectory);
connect(directoryTree, &VDirectoryTree::directoryRenamed, connect(directoryTree, &VDirectoryTree::directoryRenamed,
@ -75,8 +68,6 @@ void VMainWindow::setupUI()
editArea, &VEditArea::closeFile); editArea, &VEditArea::closeFile);
connect(fileList, &VFileList::fileCreated, connect(fileList, &VFileList::fileCreated,
editArea, &VEditArea::openFile); editArea, &VEditArea::openFile);
connect(vnote, &VNote::notebooksRenamed,
editArea, &VEditArea::handleNotebookRenamed);
connect(editArea, &VEditArea::curTabStatusChanged, connect(editArea, &VEditArea::curTabStatusChanged,
this, &VMainWindow::handleCurTabStatusChanged); this, &VMainWindow::handleCurTabStatusChanged);
connect(directoryTree, &VDirectoryTree::directoryRenamed, connect(directoryTree, &VDirectoryTree::directoryRenamed,
@ -91,19 +82,15 @@ void VMainWindow::setupUI()
connect(notebookInfoBtn, &QPushButton::clicked, connect(notebookInfoBtn, &QPushButton::clicked,
this, &VMainWindow::onNotebookInfoBtnClicked); this, &VMainWindow::onNotebookInfoBtnClicked);
connect(vnote, &VNote::notebooksChanged, connect(vnote, &VNote::notebookDeleted,
this, &VMainWindow::updateNotebookComboBox);
connect(vnote, &VNote::notebooksDeleted,
this, &VMainWindow::notebookComboBoxDeleted); this, &VMainWindow::notebookComboBoxDeleted);
connect(vnote, &VNote::notebooksRenamed, connect(vnote, &VNote::notebookRenamed,
this, &VMainWindow::notebookComboBoxRenamed); this, &VMainWindow::notebookComboBoxRenamed);
connect(vnote, &VNote::notebooksAdded, connect(vnote, &VNote::notebookAdded,
this, &VMainWindow::notebookComboBoxAdded); this, &VMainWindow::notebookComboBoxAdded);
connect(this, &VMainWindow::curNotebookChanged, connect(this, &VMainWindow::curNotebookChanged,
directoryTree, &VDirectoryTree::setNotebook); directoryTree, &VDirectoryTree::setNotebook);
connect(vnote, &VNote::notebooksRenamed,
directoryTree, &VDirectoryTree::handleNotebookRenamed);
setCentralWidget(mainSplitter); setCentralWidget(mainSplitter);
// Create and show the status bar // Create and show the status bar
@ -383,18 +370,18 @@ void VMainWindow::initDockWindows()
viewMenu->addAction(toolDock->toggleViewAction()); viewMenu->addAction(toolDock->toggleViewAction());
} }
void VMainWindow::updateNotebookComboBox(const QVector<VNotebook> &notebooks) void VMainWindow::updateNotebookComboBox(const QVector<VNotebook *> &p_notebooks)
{ {
notebookComboMuted = true; notebookComboMuted = true;
notebookComboBox->clear(); 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"), notebookComboBox->addItem(QIcon(":/resources/icons/notebook_item.svg"),
notebooks[i].getName()); p_notebooks[i]->getName());
} }
notebookComboMuted = false; notebookComboMuted = false;
int index = vconfig.getCurNotebookIndex(); int index = vconfig.getCurNotebookIndex();
if (notebooks.isEmpty()) { if (p_notebooks.isEmpty()) {
index = -1; index = -1;
} }
if (notebookComboBox->currentIndex() == index) { if (notebookComboBox->currentIndex() == index) {
@ -404,11 +391,11 @@ void VMainWindow::updateNotebookComboBox(const QVector<VNotebook> &notebooks)
} }
} }
void VMainWindow::notebookComboBoxAdded(const QVector<VNotebook> &notebooks, int idx) void VMainWindow::notebookComboBoxAdded(const VNotebook *p_notebook, int p_idx)
{ {
notebookComboMuted = true; notebookComboMuted = true;
notebookComboBox->insertItem(idx, QIcon(":/resources/icons/notebook_item.svg"), notebookComboBox->insertItem(p_idx, QIcon(":/resources/icons/notebook_item.svg"),
notebooks[idx].getName()); p_notebook->getName());
notebookComboMuted = false; notebookComboMuted = false;
if (notebookComboBox->currentIndex() == vconfig.getCurNotebookIndex()) { if (notebookComboBox->currentIndex() == vconfig.getCurNotebookIndex()) {
setCurNotebookIndex(vconfig.getCurNotebookIndex()); setCurNotebookIndex(vconfig.getCurNotebookIndex());
@ -417,16 +404,11 @@ void VMainWindow::notebookComboBoxAdded(const QVector<VNotebook> &notebooks, int
} }
} }
void VMainWindow::notebookComboBoxDeleted(const QVector<VNotebook> &notebooks, const QString &deletedName) void VMainWindow::notebookComboBoxDeleted(int p_oriIdx)
{ {
Q_ASSERT(p_oriIdx >= 0 && p_oriIdx < notebookComboBox->count());
notebookComboMuted = true; notebookComboMuted = true;
int nrItem = notebookComboBox->count(); notebookComboBox->removeItem(p_oriIdx);
for (int i = 0; i < nrItem; ++i) {
if (notebookComboBox->itemText(i) == deletedName) {
notebookComboBox->removeItem(i);
break;
}
}
notebookComboMuted = false; notebookComboMuted = false;
if (notebookComboBox->currentIndex() == vconfig.getCurNotebookIndex()) { if (notebookComboBox->currentIndex() == vconfig.getCurNotebookIndex()) {
@ -434,19 +416,12 @@ void VMainWindow::notebookComboBoxDeleted(const QVector<VNotebook> &notebooks, c
} else { } else {
notebookComboBox->setCurrentIndex(vconfig.getCurNotebookIndex()); notebookComboBox->setCurrentIndex(vconfig.getCurNotebookIndex());
} }
Q_ASSERT(notebooks.size() == notebookComboBox->count());
} }
void VMainWindow::notebookComboBoxRenamed(const QVector<VNotebook> &notebooks, void VMainWindow::notebookComboBoxRenamed(const VNotebook *p_notebook, int p_idx)
const QString &oldName, const QString &newName)
{ {
int nrItem = notebookComboBox->count(); Q_ASSERT(p_idx >= 0 && p_idx < notebookComboBox->count());
for (int i = 0; i < nrItem; ++i) { notebookComboBox->setItemText(p_idx, p_notebook->getName());
if (notebookComboBox->itemText(i) == oldName) {
notebookComboBox->setItemText(i, newName);
break;
}
}
// Renaming a notebook won't change current index // Renaming a notebook won't change current index
} }
@ -461,7 +436,7 @@ void VMainWindow::setCurNotebookIndex(int index)
QString notebook; QString notebook;
if (index > -1) { if (index > -1) {
vconfig.setCurNotebookIndex(index); vconfig.setCurNotebookIndex(index);
notebook = vnote->getNotebooks()[index].getName(); notebook = vnote->getNotebooks()[index]->getName();
newRootDirAct->setEnabled(true); newRootDirAct->setEnabled(true);
} else { } else {
newRootDirAct->setEnabled(false); newRootDirAct->setEnabled(false);
@ -494,9 +469,9 @@ void VMainWindow::onNewNotebookBtnClicked()
bool VMainWindow::isConflictWithExistingNotebooks(const QString &name) 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) { if (notebooks[i]->getName() == name) {
return true; return true;
} }
} }
@ -506,15 +481,15 @@ bool VMainWindow::isConflictWithExistingNotebooks(const QString &name)
void VMainWindow::onDeleteNotebookBtnClicked() void VMainWindow::onDeleteNotebookBtnClicked()
{ {
int curIndex = notebookComboBox->currentIndex(); int curIndex = notebookComboBox->currentIndex();
QString curName = vnote->getNotebooks()[curIndex].getName(); QString curName = vnote->getNotebooks()[curIndex]->getName();
QString curPath = vnote->getNotebooks()[curIndex].getPath(); QString curPath = vnote->getNotebooks()[curIndex]->getPath();
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), QString("Are you sure you want to delete notebook \"%1\"?") QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), QString("Are you sure you want to delete notebook \"%1\"?")
.arg(curName), QMessageBox::Ok | QMessageBox::Cancel, this); .arg(curName), QMessageBox::Ok | QMessageBox::Cancel, this);
msgBox.setInformativeText(QString("This will delete any files in this notebook (\"%1\").").arg(curPath)); msgBox.setInformativeText(QString("This will delete any files in this notebook (\"%1\").").arg(curPath));
msgBox.setDefaultButton(QMessageBox::Cancel); msgBox.setDefaultButton(QMessageBox::Cancel);
if (msgBox.exec() == QMessageBox::Ok) { if (msgBox.exec() == QMessageBox::Ok) {
vnote->removeNotebook(curName); vnote->removeNotebook(curIndex);
} }
} }
@ -525,8 +500,8 @@ void VMainWindow::onNotebookInfoBtnClicked()
return; return;
} }
QString info; QString info;
QString curName = vnote->getNotebooks()[curIndex].getName(); QString curName = vnote->getNotebooks()[curIndex]->getName();
QString defaultPath = vnote->getNotebooks()[curIndex].getPath(); QString defaultPath = vnote->getNotebooks()[curIndex]->getPath();
QString defaultName(curName); QString defaultName(curName);
do { do {
VNotebookInfoDialog dialog(tr("Notebook information"), info, defaultName, VNotebookInfoDialog dialog(tr("Notebook information"), info, defaultName,
@ -541,7 +516,7 @@ void VMainWindow::onNotebookInfoBtnClicked()
defaultName = name; defaultName = name;
continue; continue;
} }
vnote->renameNotebook(curName, name); vnote->renameNotebook(curIndex, name);
} }
break; break;
} while (true); } while (true);

View File

@ -29,7 +29,6 @@ class VMainWindow : public QMainWindow
public: public:
VMainWindow(QWidget *parent = 0); VMainWindow(QWidget *parent = 0);
~VMainWindow();
const QVector<QPair<QString, QString> > &getPalette() const; const QVector<QPair<QString, QString> > &getPalette() const;
private slots: private slots:
@ -38,11 +37,10 @@ private slots:
void onNewNotebookBtnClicked(); void onNewNotebookBtnClicked();
void onDeleteNotebookBtnClicked(); void onDeleteNotebookBtnClicked();
void onNotebookInfoBtnClicked(); void onNotebookInfoBtnClicked();
void updateNotebookComboBox(const QVector<VNotebook> &notebooks); void updateNotebookComboBox(const QVector<VNotebook *> &p_notebooks);
void notebookComboBoxAdded(const QVector<VNotebook> &notebooks, int idx); void notebookComboBoxAdded(const VNotebook *p_notebook, int p_idx);
void notebookComboBoxDeleted(const QVector<VNotebook> &notebooks, const QString &deletedName); void notebookComboBoxDeleted(int p_oriIdx);
void notebookComboBoxRenamed(const QVector<VNotebook> &notebooks, void notebookComboBoxRenamed(const VNotebook *p_notebook, int p_idx);
const QString &oldName, const QString &newName);
void importNoteFromFile(); void importNoteFromFile();
void changeMarkdownConverter(QAction *action); void changeMarkdownConverter(QAction *action);
void aboutMessage(); void aboutMessage();

View File

@ -13,12 +13,12 @@ QString VNote::templateHtml;
QString VNote::preTemplateHtml; QString VNote::preTemplateHtml;
QString VNote::postTemplateHtml; QString VNote::postTemplateHtml;
VNote::VNote() : QObject() VNote::VNote(QObject *parent)
: QObject(parent)
{ {
vconfig.initialize(); vconfig.initialize();
initTemplate(); initTemplate();
notebooks = vconfig.getNotebooks(); vconfig.getNotebooks(m_notebooks, this);
emit notebooksChanged(notebooks);
} }
void VNote::initPalette(QPalette palette) void VNote::initPalette(QPalette palette)
@ -75,9 +75,9 @@ void VNote::updateTemplate()
postTemplateHtml = VUtils::readFileFromDisk(vconfig.getPostTemplatePath()); postTemplateHtml = VUtils::readFileFromDisk(vconfig.getPostTemplatePath());
} }
const QVector<VNotebook>& VNote::getNotebooks() const QVector<VNotebook *> &VNote::getNotebooks()
{ {
return notebooks; return m_notebooks;
} }
void VNote::createNotebook(const QString &name, const QString &path) 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 // Update notebooks settings
notebooks.prepend(VNotebook(name, path)); VNotebook *nb = new VNotebook(name, path, this);
vconfig.setNotebooks(notebooks); m_notebooks.prepend(nb);
vconfig.setNotebooks(m_notebooks);
// Set current index to the new notebook // Set current index to the new notebook
vconfig.setCurNotebookIndex(0); 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 Q_ASSERT(idx >= 0 && idx < m_notebooks.size());
QString path; VNotebook *nb = m_notebooks[idx];
QString name = nb->getName();
QString path = nb->getPath();
// Update notebook settings
int curIndex = vconfig.getCurNotebookIndex(); int curIndex = vconfig.getCurNotebookIndex();
int index; m_notebooks.remove(idx);
for (index = 0; index < notebooks.size(); ++index) { vconfig.setNotebooks(m_notebooks);
if (notebooks[index].getName() == name) { if (m_notebooks.isEmpty()) {
path = notebooks[index].getPath();
break;
}
}
if (index == notebooks.size()) {
return;
}
notebooks.remove(index);
vconfig.setNotebooks(notebooks);
if (notebooks.isEmpty()) {
vconfig.setCurNotebookIndex(-1); vconfig.setCurNotebookIndex(-1);
} else if (index == curIndex) { } else if (idx == curIndex) {
vconfig.setCurNotebookIndex(0); vconfig.setCurNotebookIndex(0);
} }
// Close all the directory and files
notebookPathHash.remove(name);
nb->close(true);
delete nb;
qDebug() << "notebook" << name << "deleted";
// Delete the directory // Delete the directory
QDir dir(path); QDir dir(path);
if (!dir.removeRecursively()) { if (!dir.removeRecursively()) {
qWarning() << "error: fail to delete" << path << "recursively"; qWarning() << "failed to delete" << path << "recursively";
} else {
qDebug() << "delete" << path << "recursively";
} }
emit notebookDeleted(idx);
notebookPathHash.remove(name);
emit notebooksDeleted(notebooks, name);
} }
void VNote::renameNotebook(const QString &name, const QString &newName) void VNote::renameNotebook(int idx, const QString &newName)
{ {
QString path; Q_ASSERT(idx >= 0 && idx < m_notebooks.size());
int index; VNotebook *nb = m_notebooks[idx];
for (index = 0; index < notebooks.size(); ++index) { QString name = nb->getName();
if (notebooks[index].getName() == name) {
path = notebooks[index].getPath();
break;
}
}
if (index == notebooks.size()) {
return;
}
notebooks[index].setName(newName);
vconfig.setNotebooks(notebooks);
notebookPathHash.remove(name); 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 VNote::getNotebookPath(const QString &name)
{ {
QString path = notebookPathHash.value(name); QString path = notebookPathHash.value(name);
if (path.isEmpty()) { if (path.isEmpty()) {
for (int i = 0; i < notebooks.size(); ++i) { for (int i = 0; i < m_notebooks.size(); ++i) {
if (notebooks[i].getName() == name) { if (m_notebooks[i]->getName() == name) {
path = notebooks[i].getPath(); path = m_notebooks[i]->getPath();
break; break;
} }
} }

View File

@ -17,9 +17,9 @@ class VNote : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
VNote(); VNote(QObject *parent = 0);
const QVector<VNotebook>& getNotebooks(); const QVector<VNotebook *>& getNotebooks();
void initTemplate(); void initTemplate();
@ -31,8 +31,8 @@ public:
static QString postTemplateHtml; static QString postTemplateHtml;
void createNotebook(const QString &name, const QString &path); void createNotebook(const QString &name, const QString &path);
void removeNotebook(const QString &name); void removeNotebook(int idx);
void renameNotebook(const QString &name, const QString &newName); void renameNotebook(int idx, const QString &newName);
QString getNotebookPath(const QString &name); QString getNotebookPath(const QString &name);
@ -43,15 +43,13 @@ public slots:
void updateTemplate(); void updateTemplate();
signals: signals:
// Force to do a fully update void notebookAdded(const VNotebook *p_notebook, int p_idx);
void notebooksChanged(const QVector<VNotebook> &notebooks); void notebookDeleted(int p_oriIdx);
void notebooksAdded(const QVector<VNotebook> &notebooks, int idx); void notebookRenamed(const VNotebook *p_notebook, int p_idx);
void notebooksDeleted(const QVector<VNotebook> &notebooks, const QString &deletedName);
void notebooksRenamed(const QVector<VNotebook> &notebooks,
const QString &oldName, const QString &newName);
private: private:
QVector<VNotebook> notebooks; // Maintain all the notebooks. Other holder should use QPointer.
QVector<VNotebook *> m_notebooks;
QHash<QString, QString> notebookPathHash; QHash<QString, QString> notebookPathHash;
QVector<QPair<QString, QString> > m_palette; QVector<QPair<QString, QString> > m_palette;
}; };

View File

@ -1,31 +1,37 @@
#include "vnotebook.h" #include "vnotebook.h"
VNotebook::VNotebook() VNotebook::VNotebook(QObject *parent)
: QObject(parent)
{ {
} }
VNotebook::VNotebook(const QString &name, const QString &path) VNotebook::VNotebook(const QString &name, const QString &path, QObject *parent)
: name(name), path(path) : QObject(parent), m_name(name), m_path(path)
{ {
} }
QString VNotebook::getName() const QString VNotebook::getName() const
{ {
return this->name; return m_name;
} }
QString VNotebook::getPath() const QString VNotebook::getPath() const
{ {
return this->path; return m_path;
} }
void VNotebook::setName(const QString &name) void VNotebook::setName(const QString &name)
{ {
this->name = name; m_name = name;
} }
void VNotebook::setPath(const QString &path) void VNotebook::setPath(const QString &path)
{ {
this->path = path; m_path = path;
}
void VNotebook::close(bool p_forced)
{
//TODO
} }

View File

@ -1,13 +1,19 @@
#ifndef VNOTEBOOK_H #ifndef VNOTEBOOK_H
#define VNOTEBOOK_H #define VNOTEBOOK_H
#include <QObject>
#include <QString> #include <QString>
class VNotebook class VNotebook : public QObject
{ {
Q_OBJECT
public: public:
VNotebook(); VNotebook(QObject *parent = 0);
VNotebook(const QString &name, const QString &path); 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 getName() const;
QString getPath() const; QString getPath() const;
@ -15,8 +21,8 @@ public:
void setPath(const QString &path); void setPath(const QString &path);
private: private:
QString name; QString m_name;
QString path; QString m_path;
}; };
#endif // VNOTEBOOK_H #endif // VNOTEBOOK_H