File: main.cpp

package info (click to toggle)
lager 0.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,904 kB
  • sloc: cpp: 10,868; javascript: 10,433; makefile: 214; python: 100; sh: 98
file content (132 lines) | stat: -rw-r--r-- 3,513 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// lager - library for functional interactive c++ programs
// Copyright (C) 2017 Juan Pedro Bolivar Puente
//
// This file is part of lager.
//
// lager is free software: you can redistribute it and/or modify
// it under the terms of the MIT License, as detailed in the LICENSE
// file located at the root of this source code distribution,
// or here: <https://github.com/arximboldi/lager/blob/master/LICENSE>
//

#include "../model.hpp"

#include <lager/cursor.hpp>
#include <lager/extra/qt.hpp>
#include <lager/state.hpp>

#include <QApplication>
#include <QFileInfo>
#include <QMessageBox>
#include <QObject>
#include <QQmlApplicationEngine>
#include <QQuickStyle>

#include <iostream>

class Item : public QObject
{
    Q_OBJECT

public:
    Item(lager::cursor<todo::item> data)
        : LAGER_QT(done){data[&todo::item::done]}
        , LAGER_QT(text){data[&todo::item::text].xform(
              zug::map([](auto&& x) { return QString::fromStdString(x); }),
              zug::map([](auto&& x) { return x.toStdString(); }))}
    {}

    LAGER_QT_CURSOR(bool, done);
    LAGER_QT_CURSOR(QString, text);
};

class Model : public QObject
{
    Q_OBJECT

    lager::state<todo::model, lager::automatic_tag> state_;
    lager::state<QString, lager::automatic_tag> file_name_;

public:
    LAGER_QT_READER(QString, fileName);
    LAGER_QT_READER(QString, name);
    LAGER_QT_READER(int, count);

    Model()
        : LAGER_QT(fileName){file_name_}
        , LAGER_QT(name){file_name_.map(
              [](auto f) { return QFileInfo{f}.baseName(); })}
        , LAGER_QT(count){state_.xform(zug::map(
              [](auto&& x) { return static_cast<int>(x.todos.size()); }))}
    {}

    Q_INVOKABLE Item* todo(int index)
    {
        return new Item{
            state_[&todo::model::todos][index][lager::lenses::or_default]};
    }

    Q_INVOKABLE void add(QString text)
    {
        if (!text.isEmpty()) {
            state_.update([&](auto x) {
                x.todos = x.todos.push_front({false, text.toStdString()});
                return x;
            });
        }
    }

    Q_INVOKABLE void remove(int index)
    {
        state_.update([&](auto x) {
            x.todos = x.todos.erase(index);
            return x;
        });
    }

    Q_INVOKABLE bool save(QString fname)
    {
        try {
            auto fpath = QUrl{fname}.toLocalFile();
            if (QFileInfo{fname}.suffix() != "todo")
                fpath += ".todo";
            todo::save(fpath.toStdString(), *state_);
            file_name_.set(fname);
            return true;
        } catch (std::exception const& err) {
            std::cerr << "Exception thrown: " << err.what() << std::endl;
            return false;
        }
    }

    Q_INVOKABLE bool load(QString fname)
    {
        try {
            auto model = todo::load(QUrl{fname}.toLocalFile().toStdString());
            state_.set(model);
            file_name_.set(fname);
            return true;
        } catch (std::exception const& err) {
            std::cerr << "Exception thrown: " << err.what() << std::endl;
            return false;
        }
    }
};

#include "main.moc"

int main(int argc, char** argv)
{
    QApplication app{argc, argv};
    QQmlApplicationEngine engine;

    qmlRegisterType<Model>("Lager.Example.Todo", 1, 0, "Model");
    qmlRegisterUncreatableType<Item>("Lager.Example.Todo", 1, 0, "Item", "");

    QQuickStyle::setStyle("Material");

    engine.load(LAGER_TODO_QML_DIR "/main.qml");

    return app.exec();
}