1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Access view from c+

This commit is contained in:
Laurent Cozic 2016-12-11 16:09:39 +00:00
parent e357dfb072
commit dc4b1580b0
23 changed files with 332 additions and 212 deletions

3
.gitignore vendored
View File

@ -17,4 +17,5 @@
*.sublime-workspace *.sublime-workspace
database.sqlite database.sqlite
QtClient/build-*-Debug/ QtClient/build-*-Debug/
*.pro.user *.pro.user
notes.sqlite

View File

@ -5,7 +5,7 @@ Item {
id: root id: root
property alias model: listView.model property alias model: listView.model
property alias currentIndex: listView.currentIndex property alias currentIndex: listView.currentIndex
signal currentItemChanged() property alias currentItem: listView.currentItem
Rectangle { Rectangle {
color: "#eeeeff" color: "#eeeeff"
@ -37,8 +37,5 @@ Item {
ScrollBar.vertical: ScrollBar { } ScrollBar.vertical: ScrollBar { }
highlight: Rectangle { color: "lightsteelblue"; radius: 5 } highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
focus: true focus: true
onCurrentItemChanged: {
root.currentItemChanged()
}
} }
} }

View File

@ -9,7 +9,11 @@ SOURCES += \
database.cpp \ database.cpp \
uuid.cpp \ uuid.cpp \
services/folderservice.cpp \ services/folderservice.cpp \
models/foldermodel.cpp models/foldermodel.cpp \
models/notemodel.cpp \
models/note.cpp \
services/noteservice.cpp \
application.cpp
RESOURCES += qml.qrc RESOURCES += qml.qrc
@ -28,7 +32,11 @@ HEADERS += \
database.h \ database.h \
uuid.h \ uuid.h \
services/folderservice.h \ services/folderservice.h \
models/foldermodel.h models/foldermodel.h \
models/notemodel.h \
models/note.h \
services/noteservice.h \
application.h
DISTFILES += DISTFILES +=

View File

@ -0,0 +1,44 @@
import QtQuick 2.0
import QtQuick.Controls 2.0
Item {
id: root
property alias model: listView.model
property alias currentIndex: listView.currentIndex
signal currentItemChanged()
Rectangle {
color: "#ffeeee"
border.color: "#00ff00"
anchors.fill: parent
}
Component {
id: noteDelegate
Item {
width: parent.width
height: 25
Text {
text: display
}
MouseArea {
anchors.fill: parent
onClicked: {
listView.currentIndex = index
}
}
}
}
ListView {
id: listView
anchors.fill: parent
delegate: noteDelegate
ScrollBar.vertical: ScrollBar { }
highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
focus: true
onCurrentItemChanged: {
root.currentItemChanged()
}
}
}

View File

@ -0,0 +1,38 @@
#include "application.h"
#include "models/folder.h"
#include "database.h"
#include "models/foldermodel.h"
#include "services/folderservice.h"
using namespace jop;
Application::Application(int &argc, char **argv) : QGuiApplication(argc, argv) {
db_ = Database("D:/Web/www/joplin/notes.sqlite");
folderService_ = FolderService(db_);
folderModel_.setService(folderService_);
view_.setResizeMode(QQuickView::SizeRootObjectToView);
QQmlContext *ctxt = view_.rootContext();
ctxt->setContextProperty("folderListModel", &folderModel_);
view_.setSource(QUrl("qrc:/main.qml"));
QObject* rootObject = (QObject*)view_.rootObject();
connect(rootObject, SIGNAL(currentFolderChanged()), this, SLOT(view_currentFolderChanged()));
view_.show();
}
QString Application::selectedFolderId() const {
QObject* rootObject = (QObject*)view_.rootObject();
int index = rootObject->property("currentFolderIndex").toInt();
QModelIndex modelIndex = folderModel_.index(index);
return folderModel_.data(modelIndex, FolderModel::IdRole).toString();
}
void Application::view_currentFolderChanged() {
qDebug() << selectedFolderId();
}

View File

@ -0,0 +1,36 @@
#ifndef APPLICATION_H
#define APPLICATION_H
#include <stable.h>
#include "database.h"
#include "services/folderservice.h"
#include "models/foldermodel.h"
namespace jop {
class Application : public QGuiApplication {
Q_OBJECT
public:
Application(int &argc, char **argv);
private:
QQuickView view_;
Database db_;
FolderService folderService_;
FolderModel folderModel_;
QString selectedFolderId() const;
public slots:
void view_currentFolderChanged();
};
}
#endif // APPLICATION_H

View File

@ -5,7 +5,7 @@ using namespace jop;
Database::Database(const QString &path) { Database::Database(const QString &path) {
version_ = -1; version_ = -1;
QFile::remove(path); //QFile::remove(path);
db_ = QSqlDatabase::addDatabase("QSQLITE"); db_ = QSqlDatabase::addDatabase("QSQLITE");
db_.setDatabaseName(path); db_.setDatabaseName(path);
@ -16,7 +16,7 @@ Database::Database(const QString &path) {
qDebug() << "Database: connection ok"; qDebug() << "Database: connection ok";
} }
upgrade(); //upgrade();
} }
Database::Database() {} Database::Database() {}
@ -80,11 +80,11 @@ void Database::upgrade() {
db_.exec("CREATE TABLE folders (id TEXT PRIMARY KEY, title TEXT, created_time INT)"); db_.exec("CREATE TABLE folders (id TEXT PRIMARY KEY, title TEXT, created_time INT)");
for (int i = 1; i < 100; i++) { // for (int i = 1; i < 100; i++) {
QUuid uuid = QUuid::createUuid(); // QUuid uuid = QUuid::createUuid();
QString title = QString::number(i); // QString title = QString::number(i);
db_.exec(QString("INSERT INTO folders (id, title, created_time) VALUES (\"%1\", \"%2\", 1481235571)").arg(uuid.toString(), title.repeated(10))); // db_.exec(QString("INSERT INTO folders (id, title, created_time) VALUES (\"%1\", \"%2\", 1481235571)").arg(uuid.toString(), title.repeated(10)));
} // }
//db_.exec("INSERT INTO folders (id, title, created_time) VALUES (\"ed735d55415bee976b771989be8f7005\", \"bbbb\", 1481235571)"); //db_.exec("INSERT INTO folders (id, title, created_time) VALUES (\"ed735d55415bee976b771989be8f7005\", \"bbbb\", 1481235571)");
//db_.exec("INSERT INTO folders (id, title, created_time) VALUES (\"5d41402abc4b2a76b9719d911017c592\", \"cccc\", 1481235571)"); //db_.exec("INSERT INTO folders (id, title, created_time) VALUES (\"5d41402abc4b2a76b9719d911017c592\", \"cccc\", 1481235571)");

View File

@ -1,5 +1,6 @@
#include <stable.h> #include <stable.h>
#include "application.h"
#include "models/folder.h" #include "models/folder.h"
#include "database.h" #include "database.h"
#include "models/foldermodel.h" #include "models/foldermodel.h"
@ -9,73 +10,6 @@ using namespace jop;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv); Application app(argc, argv);
Database db("D:/Web/www/joplin/database.sqlite");
// FolderService s(db);
// qDebug() << s.count();
// Folder f = s.byId("35dbdd6e633566c4160e699a86601ab8");
// qDebug() << f.id() << f.title() << f.createdTime();
// QSqlQuery q = db.query("SELECT * FROM folders WHERE id = :id");
// q.bindValue(":id", "35dbdd6e633566c4160e699a86601ab8");
// q.exec();
// q.next();
//qDebug() << q.isValid();
// Folder f;
// f.fromSqlQuery(q);
// qDebug() << f.title() << f.id() << f.createdTime();
//QSqlQuery q = query("SELECT * FROM folders WHERE id = :id");
//q.bindValue(":id", "a");
//q.exec();
FolderService folderService(db);
FolderModel model(folderService);
//Folder* f = new Folder(); f->setTitle("oneXXX"); model.addFolder(f);
//f = new Folder(); f->setTitle("two"); model.addFolder(f);
//f = new Folder(); f->setTitle("three"); model.addFolder(f);
// QQuickView view;
// view.setResizeMode(QQuickView::SizeRootObjectToView);
// QQmlContext *ctxt = view.rootContext();
// ctxt->setContextProperty("myModel", &model);
// QSqlDatabase m_db = QSqlDatabase::addDatabase("QSQLITE");
// m_db.setDatabaseName("D:/Web/www/joplin/QtClient/JoplinQtClient/test.sqlite3");
// if (!m_db.open())
// {
// qDebug() << "Error: connection with database fail";
// }
// else
// {
// qDebug() << "Database: connection ok";
// }
//QQmlApplicationEngine engine;
//engine.load(QUrl(QLatin1String("qrc:/main.qml")));
QQuickView view;
view.setResizeMode(QQuickView::SizeRootObjectToView);
QQmlContext *ctxt = view.rootContext();
ctxt->setContextProperty("folderTreeViewModel", &model);
view.setSource(QUrl("qrc:/main.qml"));
view.show();
return app.exec(); return app.exec();
} }

View File

@ -4,17 +4,20 @@ import QtQuick.Controls 1.4
import QtQuick.Layouts 1.0 import QtQuick.Layouts 1.0
Item { Item {
id: root
width: 800 width: 800
height: 600 height: 600
signal currentFolderChanged()
property alias currentFolderIndex: folderList.currentIndex
RowLayout { RowLayout {
id: layout id: layout
anchors.fill: parent anchors.fill: parent
spacing: 0 spacing: 0
FolderListView { FolderList {
id: folderTreeView id: folderList
model: folderTreeViewModel model: folderListModel
Layout.fillWidth: true Layout.fillWidth: true
Layout.fillHeight: true Layout.fillHeight: true
Layout.minimumWidth: 50 Layout.minimumWidth: 50
@ -23,105 +26,19 @@ Item {
Layout.minimumHeight: 150 Layout.minimumHeight: 150
onCurrentItemChanged: { onCurrentItemChanged: {
console.info(folderTreeView.currentIndex) root.currentFolderChanged()
} }
} }
// Rectangle { NoteList {
// color: 'teal' id: noteList
// Layout.fillWidth: true
// Layout.minimumWidth: 50
// Layout.preferredWidth: 100
// Layout.maximumWidth: 300
// Layout.minimumHeight: 150
// Text {
// anchors.centerIn: parent
// text: parent.width + 'x' + parent.height
// }
// }
Rectangle {
color: 'plum'
Layout.fillWidth: true Layout.fillWidth: true
Layout.fillHeight: true
Layout.minimumWidth: 100 Layout.minimumWidth: 100
Layout.preferredWidth: 200 Layout.preferredWidth: 200
Layout.preferredHeight: 100 Layout.preferredHeight: 100
Text {
anchors.centerIn: parent
text: parent.width + 'x' + parent.height
}
} }
} }
// visible: true
// width: 640
// height: 480
// //title: qsTr("Hello World")
// RowLayout {
// anchors.fill: parent
// FolderTreeView {
// id: folderTreeView
// model: folderTreeViewModel
// width: 200
// //height: 500
// //currentIndex: folderTreeViewCurrentIndex
// anchors.fill: parent
// onCurrentItemChanged: {
// console.info(folderTreeView.currentIndex)
// //folderTreeViewCurrentIndex = folderTreeView.currentIndex
// }
// }
// Rectangle {
// color: 'plum'
// Text {
// anchors.centerIn: parent
// text: parent.width + 'x' + parent.height
// }
// }
// }
} }
//import QtQuick 2.7
//import QtQuick.Controls 2.0
//import QtQuick.Controls 1.4
//import QtQuick.Layouts 1.0
//ApplicationWindow {
// visible: true
// width: 640
// height: 480
// title: qsTr("Hello World")
// SwipeView {
// id: swipeView
// anchors.fill: parent
// currentIndex: tabBar.currentIndex
// Page1 {
// }
// Page {
// Label {
// text: qsTr("Second page")
// anchors.centerIn: parent
// }
// }
// }
// footer: TabBar {
// id: tabBar
// currentIndex: swipeView.currentIndex
// TabButton {
// text: qsTr("First")
// }
// TabButton {
// text: qsTr("Second")
// }
// }
//}

View File

@ -2,9 +2,9 @@
using namespace jop; using namespace jop;
//FolderModel::FolderModel() : QAbstractListModel() {} FolderModel::FolderModel() : QAbstractListModel() {}
FolderModel::FolderModel(FolderService &folderService) : QAbstractListModel() { void FolderModel::setService(FolderService &folderService) {
folderService_ = folderService; folderService_ = folderService;
} }
@ -33,13 +33,18 @@ QVariant FolderModel::data(const QModelIndex & index, int role) const {
return QVariant(folder.title()); return QVariant(folder.title());
} }
if (role == IdRole) {
return QVariant(folder.id());
}
return QVariant(); return QVariant();
} }
QHash<int, QByteArray> FolderModel::roleNames() const { QHash<int, QByteArray> FolderModel::roleNames() const {
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames(); QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
roles[TitleRole] = "title"; roles[TitleRole] = "title";
roles[UuidRole] = "uuid"; roles[IdRole] = "uuid";
roles[RawRole] = "raw";
return roles; return roles;
} }

View File

@ -14,12 +14,14 @@ class FolderModel : public QAbstractListModel {
public: public:
enum FolderRoles { enum FolderRoles {
UuidRole = Qt::UserRole + 1, IdRole = Qt::UserRole + 1,
TitleRole TitleRole,
RawRole
}; };
//FolderModel(); FolderModel();
FolderModel(FolderService& folderService);
void setService(FolderService& folderService);
void addFolder(Folder* folder); void addFolder(Folder* folder);

View File

@ -12,12 +12,12 @@ void Item::fromSqlQuery(const QSqlQuery &q) {
int i_title = q.record().indexOf("title"); int i_title = q.record().indexOf("title");
int i_created_time = q.record().indexOf("created_time"); int i_created_time = q.record().indexOf("created_time");
id_ = jop::uuid::fromString(q.value(i_id).toString()); id_ = q.value(i_id).toString();
title_ = q.value(i_title).toString(); title_ = q.value(i_title).toString();
createdTime_ = q.value(i_created_time).toInt(); createdTime_ = q.value(i_created_time).toInt();
} }
QUuid Item::id() const { QString Item::id() const {
return id_; return id_;
} }
@ -29,13 +29,8 @@ int Item::createdTime() const {
return createdTime_; return createdTime_;
} }
void Item::setId(const QUuid &v) {
id_ = v;
}
void Item::setId(const QString& v) { void Item::setId(const QString& v) {
QUuid u = uuid::fromString(v); id_ = v;
setId(u);
} }
void Item::setTitle(const QString &v) { void Item::setTitle(const QString &v) {

View File

@ -11,12 +11,11 @@ public:
Item(); Item();
QUuid id() const; QString id() const;
QString title() const; QString title() const;
int createdTime() const; int createdTime() const;
bool isPartial() const; bool isPartial() const;
void setId(const QUuid& v);
void setId(const QString& v); void setId(const QString& v);
void setTitle(const QString& v); void setTitle(const QString& v);
void setCreatedTime(int v); void setCreatedTime(int v);
@ -26,7 +25,7 @@ public:
private: private:
QUuid id_; QString id_;
QString title_; QString title_;
int createdTime_; int createdTime_;
bool isPartial_; bool isPartial_;

View File

@ -0,0 +1,8 @@
#include "note.h"
using namespace jop;
Note::Note()
{
}

View File

@ -0,0 +1,23 @@
#ifndef NOTE_H
#define NOTE_H
#include <stable.h>
#include "models/item.h"
namespace jop {
class Note : public Item {
public:
Note();
private:
};
}
#endif // NOTE_H

View File

@ -0,0 +1,25 @@
#include "notemodel.h"
jop::NoteModel::NoteModel(NoteService &noteService)
{
noteService_ = noteService;
}
int jop::NoteModel::rowCount(const QModelIndex &parent) const
{
return 0;
}
QVariant jop::NoteModel::data(const QModelIndex &index, int role) const
{
return QVariant();
}
QHash<int, QByteArray> jop::NoteModel::roleNames() const
{
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
// roles[TitleRole] = "title";
// roles[UuidRole] = "uuid";
return roles;
}

View File

@ -0,0 +1,33 @@
#ifndef NOTEMODEL_H
#define NOTEMODEL_H
#include <stable.h>
#include "services/noteservice.h"
namespace jop {
class NoteModel : public QAbstractListModel {
Q_OBJECT
public:
NoteModel(NoteService &noteService);
int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
protected:
QHash<int, QByteArray> roleNames() const;
private:
QList<Note> notes_;
NoteService noteService_;
};
}
#endif // NOTEMODEL_H

View File

@ -1,9 +1,7 @@
<RCC> <RCC>
<qresource prefix="/"> <qresource prefix="/">
<file>main.qml</file> <file>main.qml</file>
<file>Page1.qml</file> <file>FolderList.qml</file>
<file>Page1Form.ui.qml</file> <file>NoteList.qml</file>
<file>FolderListView.qml</file>
<file>TestUnQuatre.qml</file>
</qresource> </qresource>
</RCC> </RCC>

View File

@ -0,0 +1,26 @@
#include "noteservice.h"
using namespace jop;
NoteService::NoteService() {}
NoteService::NoteService(jop::Database &database) {
database_ = database;
}
int NoteService::count(int parentFolderId) const {
QSqlQuery q = database_.query("SELECT count(*) as row_count FROM notes WHERE parent_id = :parent_id");
q.bindValue(":parent_id", parentFolderId);
q.exec();
q.next();
return q.value(0).toInt();
}
Note NoteService::byId(const QString &id) const {
Note n;
return n;
}
const QList<Note> NoteService::overviewList(const QString &orderBy, int from, int to) const {
return QList<Note>();
}

View File

@ -0,0 +1,29 @@
#ifndef NOTESERVICE_H
#define NOTESERVICE_H
#include <stable.h>
#include "database.h"
#include "models/note.h"
namespace jop {
class NoteService {
public:
NoteService();
NoteService(Database& database);
int count(int parentFolderId) const;
Note byId(const QString& id) const;
const QList<Note> overviewList(const QString& orderBy, int from, int to) const;
private:
Database database_;
mutable QList<Note> cache_;
};
}
#endif // NOTESERVICE_H

View File

@ -4,19 +4,21 @@
#if defined __cplusplus #if defined __cplusplus
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QGuiApplication>
#include <QDebug> #include <QDebug>
#include <QFileInfo> #include <QFileInfo>
#include <QSqlDatabase> #include <QSqlDatabase>
#include <QSqlQuery> #include <QSqlQuery>
#include <QSqlRecord> #include <QSqlRecord>
#include <QUuid> //#include <QUuid>
#include <vector> //#include <vector>
#include <QList> #include <QList>
#include <QGuiApplication> #include <QGuiApplication>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QSqlDatabase> #include <QSqlDatabase>
#include <QQuickView> #include <QQuickView>
#include <QQmlContext> #include <QQmlContext>
#include <QQmlProperty>
#endif // __cplusplus #endif // __cplusplus

View File

@ -4,19 +4,19 @@
namespace jop { namespace jop {
namespace uuid { namespace uuid {
QUuid fromString(const QString& s) { //QUuid fromString(const QString& s) {
// {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} // // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
QString mod = s; // QString mod = s;
mod.insert(8, '-'); // mod.insert(8, '-');
mod.insert(13, '-'); // mod.insert(13, '-');
mod.insert(18, '-'); // mod.insert(18, '-');
mod.insert(23, '-'); // mod.insert(23, '-');
mod = "{" + mod + "}"; // mod = "{" + mod + "}";
qDebug() << mod; // //qDebug() << mod;
return QUuid(mod); // return QUuid(mod);
} //}
} }
} }

View File

@ -6,7 +6,7 @@
namespace jop { namespace jop {
namespace uuid { namespace uuid {
QUuid fromString(const QString& s); //QUuid fromString(const QString& s);
} }
} }