read/write the info about notebooks

Use QSettings to store notebooks info in INI format.

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-10-01 20:21:01 +08:00
parent 811172ef8a
commit d8c5114c1b
10 changed files with 245 additions and 8 deletions

View File

@ -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

View File

@ -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();
}

View File

@ -4,3 +4,9 @@
VDirectoryTree::VDirectoryTree(QWidget *parent) : QTreeWidget(parent)
{
}
void VDirectoryTree::setTreePath(const QString& path)
{
treePath = path;
qDebug() << "set directory tree path:" << path;
}

View File

@ -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

View File

@ -1,28 +1,33 @@
#include <QtGui>
#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<VNotebook> &notebooks = vnote->getNotebooks();
notebookComboBox->clear();
for (int i = 0; i <notebooks.size(); ++i) {
notebookComboBox->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());
}

View File

@ -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

91
vnote.cpp Normal file
View File

@ -0,0 +1,91 @@
#include <QSettings>
#include <QDebug>
#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<VNotebook>& 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;
}

31
vnote.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef VNOTE_H
#define VNOTE_H
#include <QString>
#include <QVector>
#include <QSettings>
#include "vnotebook.h"
class VNote
{
public:
VNote();
void readGlobalConfig();
void writeGlobalConfig();
const QVector<VNotebook>& 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<VNotebook> notebooks;
int curNotebookIndex;
static const QString orgName;
static const QString appName;
};
#endif // VNOTE_H

31
vnotebook.cpp Normal file
View File

@ -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;
}

22
vnotebook.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef VNOTEBOOK_H
#define VNOTEBOOK_H
#include <QString>
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