~popey/notes-app/fix-non-starting

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
/*
 * Copyright 2013 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import QtQuick 2.0
import Ubuntu.Components 0.1
import Ubuntu.Components.ListItems 0.1 as ListItem
import Ubuntu.Unity.Action 1.0 as UnityActions
import NotesApp.Plugins 0.1
import "Components"
import "Models"

MainView {
    id: notesMainWindow
    applicationName: "com.ubuntu.notes"
    width: units.gu(40)
    height: units.gu(71)
    automaticOrientation: true

    actions: [
        UnityActions.Action {
            text: i18n.tr("Add")
            keywords: i18n.tr("Create New Note")
            onTriggered: notesMainWindow.createNewNote()
        },
        UnityActions.Action {
            text: i18n.tr("Delete")
            keywords: i18n.tr("Trash;Erase Note")
            enabled: noteList.currentIndex !== -1
            onTriggered: dataModel.deleteNote(noteList.currentIndex)
        }
    ]

    NotesApplication {
        id: application

        /* We need to set this because we are being run through qmlscene, but
           our custom CachingProvider image provider needs to reserve a directory
           to save its data, and Qt will reserve it based on application name */
        applicationName: "notes-app"

        onAboutToQuit: {
            if (noteList.currentIndex !== -1)
                noteList.commitNote(noteList.currentIndex, true);
        }
    }

    Page {
        id: page

        anchors.fill: parent
        title: i18n.tr("Notes")

        tools: ToolbarItems {
            ToolbarButton {
                action: Action {
                    text: i18n.tr("Add")
                    iconSource: Qt.resolvedUrl("Components/graphics/add.png")
                    onTriggered: {
                        page.tools.opened = false
                        notesMainWindow.createNewNote()
                    }
                }
            }
        }

        // model to access the database of Notes
        DataModel {
            id: dataModel
        }

        // background
        Rectangle {
            anchors.fill: parent
            color: "#fff6e5"
        }

        NoteList {
            id: noteList
            anchors.fill: parent
            anchors.topMargin: units.gu(1.5)
            focus: true
            model: dataModel
        }

        Connections {
            target: Qt.inputMethod
            onVisibleChanged: {
                if (!Qt.inputMethod.visible) {
                    noteList.focus = true;
                }
            }
        }

    }

    function createNewNote() {
        // Don't create a new note if there's only one and it's empty already,
        // but ensure it's activated so that the user can start typing
        if (dataModel.count === 1 &&
            noteList.commitNote(0, false).length === 0) {
        } else {
            dataModel.newNote();
        }
        noteList.setActiveIndex(0);
    }

    KeyboardRectangle {
    }
}