浏览代码

read/write the info about notebooks

Use QSettings to store notebooks info in INI format.

Signed-off-by: Le Tan <[email protected]>
Le Tan 9 年之前
父节点
当前提交
d8c5114c1b
共有 10 个文件被更改,包括 245 次插入8 次删除
  1. 6 2
      VNote.pro
  2. 3 2
      main.cpp
  3. 6 0
      vdirectorytree.cpp
  4. 3 0
      vdirectorytree.h
  5. 41 4
      vmainwindow.cpp
  6. 11 0
      vmainwindow.h
  7. 91 0
      vnote.cpp
  8. 31 0
      vnote.h
  9. 31 0
      vnotebook.cpp
  10. 22 0
      vnotebook.h

+ 6 - 2
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

+ 3 - 2
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();
 }

+ 6 - 0
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;
+}

+ 3 - 0
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

+ 41 - 4
vmainwindow.cpp

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

+ 11 - 0
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

+ 91 - 0
vnote.cpp

@@ -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 - 0
vnote.h

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

+ 22 - 0
vnotebook.h

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