diff --git a/VNote.pro b/VNote.pro index 0bd30c5e..0d30f0d0 100644 --- a/VNote.pro +++ b/VNote.pro @@ -14,10 +14,14 @@ TEMPLATE = app SOURCES += main.cpp\ vmainwindow.cpp \ - vdirectorytree.cpp + vdirectorytree.cpp \ + vnote.cpp \ + vnotebook.cpp HEADERS += vmainwindow.h \ - vdirectorytree.h + vdirectorytree.h \ + vnote.h \ + vnotebook.h RESOURCES += \ vnote.qrc diff --git a/main.cpp b/main.cpp index 60bc48e8..05879475 100644 --- a/main.cpp +++ b/main.cpp @@ -3,9 +3,10 @@ int main(int argc, char *argv[]) { - QApplication a(argc, argv); + QApplication app(argc, argv); + VMainWindow w; w.show(); - return a.exec(); + return app.exec(); } diff --git a/vdirectorytree.cpp b/vdirectorytree.cpp index 7c9996d1..1b5e3be7 100644 --- a/vdirectorytree.cpp +++ b/vdirectorytree.cpp @@ -4,3 +4,9 @@ VDirectoryTree::VDirectoryTree(QWidget *parent) : QTreeWidget(parent) { } + +void VDirectoryTree::setTreePath(const QString& path) +{ + treePath = path; + qDebug() << "set directory tree path:" << path; +} diff --git a/vdirectorytree.h b/vdirectorytree.h index 3c2d62d3..ae421374 100644 --- a/vdirectorytree.h +++ b/vdirectorytree.h @@ -12,8 +12,11 @@ public: signals: public slots: + void setTreePath(const QString& path); private: + // The path of the directory tree root + QString treePath; }; #endif // VDIRECTORYTREE_H diff --git a/vmainwindow.cpp b/vmainwindow.cpp index 83044d41..d2c4a9cf 100644 --- a/vmainwindow.cpp +++ b/vmainwindow.cpp @@ -1,28 +1,33 @@ #include #include "vmainwindow.h" #include "vdirectorytree.h" +#include "vnote.h" VMainWindow::VMainWindow(QWidget *parent) : QMainWindow(parent) { setupUI(); + + vnote = new VNote(); + vnote->readGlobalConfig(); + updateNotebookComboBox(); } VMainWindow::~VMainWindow() { - + delete vnote; } void VMainWindow::setupUI() { // Notebook directory browser tree - notebookLabel = new QLabel(tr("&Notebook")); + notebookLabel = new QLabel(tr("Notebook")); notebookComboBox = new QComboBox(); - notebookLabel->setBuddy(notebookComboBox); directoryTree = new VDirectoryTree(); QHBoxLayout *nbTopLayout = new QHBoxLayout; - notebookComboBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); + notebookComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); + notebookComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents); nbTopLayout->setAlignment(Qt::AlignLeft); nbTopLayout->addWidget(notebookLabel); nbTopLayout->addWidget(notebookComboBox); @@ -60,5 +65,37 @@ void VMainWindow::setupUI() mainSplitter->setStretchFactor(1, 1); mainSplitter->setStretchFactor(2, 10); + // Signals + connect(notebookComboBox, SIGNAL(currentIndexChanged(int)), this, + SLOT(setCurNotebookIndex(int))); + connect(this, SIGNAL(curNotebookIndexChanged(const QString&)), directoryTree, + SLOT(setTreePath(const QString&))); + setCentralWidget(mainSplitter); } + +void VMainWindow::updateNotebookComboBox() +{ + const QVector ¬ebooks = vnote->getNotebooks(); + + notebookComboBox->clear(); + for (int i = 0; i addItem(notebooks[i].getName()); + } + + notebookComboBox->setCurrentIndex(vnote->getCurNotebookIndex()); + + qDebug() << "update notebook combobox with" << notebookComboBox->count() + << "items"; +} + +void VMainWindow::setCurNotebookIndex(int index) +{ + Q_ASSERT(index < vnote->getNotebooks().size()); + qDebug() << "set current notebook index:" << index; + vnote->setCurNotebookIndex(index); + notebookComboBox->setCurrentIndex(index); + + // Update directoryTree + emit curNotebookIndexChanged(vnote->getNotebooks()[index].getPath()); +} diff --git a/vmainwindow.h b/vmainwindow.h index 758c16d4..7babb2ba 100644 --- a/vmainwindow.h +++ b/vmainwindow.h @@ -9,6 +9,7 @@ class VDirectoryTree; class QSplitter; class QListWidget; class QTabWidget; +class VNote; class VMainWindow : public QMainWindow { @@ -18,8 +19,17 @@ public: VMainWindow(QWidget *parent = 0); ~VMainWindow(); +private slots: + // Change current notebook index and update the directory tree + void setCurNotebookIndex(int index); + +signals: + void curNotebookIndexChanged(const QString &path); + private: void setupUI(); + // Update notebookComboBox according to vnote + void updateNotebookComboBox(); QLabel *notebookLabel; QComboBox *notebookComboBox; @@ -27,6 +37,7 @@ private: QListWidget *fileListWidget; QTabWidget *editorTabWidget; QSplitter *mainSplitter; + VNote *vnote; }; #endif // VMAINWINDOW_H diff --git a/vnote.cpp b/vnote.cpp new file mode 100644 index 00000000..2b486524 --- /dev/null +++ b/vnote.cpp @@ -0,0 +1,91 @@ +#include +#include +#include "vnote.h" + +const QString VNote::orgName = QString("tamlok"); +const QString VNote::appName = QString("VNote"); + +VNote::VNote() + : curNotebookIndex(0) +{ +} + +void VNote::readGlobalConfig() +{ + QSettings settings(QSettings::IniFormat, QSettings::UserScope, + orgName, appName); + + // [global] section + settings.beginGroup("global"); + curNotebookIndex = settings.value("current_notebook", 0).toInt(); + qDebug() << "read current_notebook=" << curNotebookIndex; + settings.endGroup(); + + readGlobalConfigNotebooks(settings); +} + +void VNote::writeGlobalConfig() +{ + QSettings settings(QSettings::IniFormat, QSettings::UserScope, + orgName, appName); + + // [global] section + settings.beginGroup("global"); + settings.setValue("current_notebook", curNotebookIndex); + qDebug() << "write current_notebook=" << curNotebookIndex; + settings.endGroup(); + + writeGlobalConfigNotebooks(settings); +} + +void VNote::readGlobalConfigNotebooks(QSettings &settings) +{ + notebooks.clear(); + int size = settings.beginReadArray("notebooks"); + for (int i = 0; i < size; ++i) { + settings.setArrayIndex(i); + VNotebook notebook; + QString name = settings.value("name").toString(); + QString path = settings.value("path").toString(); + notebook.setName(name); + notebook.setPath(path); + notebooks.append(notebook); + } + settings.endArray(); + qDebug() << "read" << notebooks.size() + << "notebook items from [notebooks] section"; +} + +void VNote::writeGlobalConfigNotebooks(QSettings &settings) +{ + settings.beginWriteArray("notebooks"); + for (int i = 0; i < notebooks.size(); ++i) { + settings.setArrayIndex(i); + settings.setValue("name", notebooks[i].getName()); + settings.setValue("path", notebooks[i].getPath()); + } + settings.endArray(); + qDebug() << "write" << notebooks.size() + << "notebook items in [notebooks] section"; +} + +const QVector& VNote::getNotebooks() +{ + return notebooks; +} + +int VNote::getCurNotebookIndex() const +{ + return curNotebookIndex; +} + +void VNote::setCurNotebookIndex(int index) +{ + curNotebookIndex = index; + + // Update settings + QSettings settings(QSettings::IniFormat, QSettings::UserScope, + orgName, appName); + settings.setValue("global/current_notebook", curNotebookIndex); + qDebug() << "write current_notebook=" << curNotebookIndex; +} diff --git a/vnote.h b/vnote.h new file mode 100644 index 00000000..4b03d856 --- /dev/null +++ b/vnote.h @@ -0,0 +1,31 @@ +#ifndef VNOTE_H +#define VNOTE_H + +#include +#include +#include +#include "vnotebook.h" + +class VNote +{ +public: + VNote(); + void readGlobalConfig(); + void writeGlobalConfig(); + + const QVector& getNotebooks(); + int getCurNotebookIndex() const; + void setCurNotebookIndex(int index); +private: + // Write notebooks section of global config + void writeGlobalConfigNotebooks(QSettings &settings); + // Read notebooks section of global config + void readGlobalConfigNotebooks(QSettings &settings); + + QVector notebooks; + int curNotebookIndex; + static const QString orgName; + static const QString appName; +}; + +#endif // VNOTE_H diff --git a/vnotebook.cpp b/vnotebook.cpp new file mode 100644 index 00000000..f24fc0ab --- /dev/null +++ b/vnotebook.cpp @@ -0,0 +1,31 @@ +#include "vnotebook.h" + +VNotebook::VNotebook() +{ + +} + +VNotebook::VNotebook(const QString &name, const QString &path) + : name(name), path(path) +{ +} + +QString VNotebook::getName() const +{ + return this->name; +} + +QString VNotebook::getPath() const +{ + return this->path; +} + +void VNotebook::setName(const QString &name) +{ + this->name = name; +} + +void VNotebook::setPath(const QString &path) +{ + this->path = path; +} diff --git a/vnotebook.h b/vnotebook.h new file mode 100644 index 00000000..77ce4ed3 --- /dev/null +++ b/vnotebook.h @@ -0,0 +1,22 @@ +#ifndef VNOTEBOOK_H +#define VNOTEBOOK_H + +#include + +class VNotebook +{ +public: + VNotebook(); + VNotebook(const QString &name, const QString &path); + + QString getName() const; + QString getPath() const; + void setName(const QString &name); + void setPath(const QString &path); + +private: + QString name; + QString path; +}; + +#endif // VNOTEBOOK_H