From 42ded08c7f5c023d5f3915cb6793860e3969a17c Mon Sep 17 00:00:00 2001 From: Filippo Cucchetto Date: Sun, 11 Jan 2015 21:01:00 +0100 Subject: [PATCH] Added a sample contact application Basic contact app the shows how to expose a list of elements to Qml --- Nim/Examples/CMakeLists.txt | 3 +- Nim/Examples/ContactApp/ApplicationLogic.nim | 34 +++++++++++++ Nim/Examples/ContactApp/CMakeLists.txt | 3 ++ Nim/Examples/ContactApp/Contact.nim | 51 +++++++++++++++++++ Nim/Examples/ContactApp/ContactList.nim | 43 ++++++++++++++++ Nim/Examples/ContactApp/main.nim | 15 ++++++ Nim/Examples/ContactApp/main.qml | 52 ++++++++++++++++++++ 7 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 Nim/Examples/ContactApp/ApplicationLogic.nim create mode 100644 Nim/Examples/ContactApp/CMakeLists.txt create mode 100644 Nim/Examples/ContactApp/Contact.nim create mode 100644 Nim/Examples/ContactApp/ContactList.nim create mode 100644 Nim/Examples/ContactApp/main.nim create mode 100644 Nim/Examples/ContactApp/main.qml diff --git a/Nim/Examples/CMakeLists.txt b/Nim/Examples/CMakeLists.txt index d9f665a..15abd67 100644 --- a/Nim/Examples/CMakeLists.txt +++ b/Nim/Examples/CMakeLists.txt @@ -1,4 +1,5 @@ add_subdirectory(HelloWorld) add_subdirectory(SimpleData) add_subdirectory(SlotsAndProperties) -add_subdirectory(QtObjectMacro) \ No newline at end of file +add_subdirectory(QtObjectMacro) +add_subdirectory(ContactApp) \ No newline at end of file diff --git a/Nim/Examples/ContactApp/ApplicationLogic.nim b/Nim/Examples/ContactApp/ApplicationLogic.nim new file mode 100644 index 0000000..6af7c61 --- /dev/null +++ b/Nim/Examples/ContactApp/ApplicationLogic.nim @@ -0,0 +1,34 @@ +import NimQml, NimQmlMacros, Contact, ContactList + +QtObject: + type ApplicationLogic* = ref object of QObject + contactList: ContactList + app: QApplication + + proc delete*(self: ApplicationLogic) = + let qobject = self.QObject + qobject.delete() + + proc newApplicationLogic*(app: QApplication): ApplicationLogic = + new(result, delete) + result.contactList = newContactList() + result.app = app + result.create() + + method getContactList(self: ApplicationLogic): QVariant {.slot.} = + return newQVariant(self.contactList) + + method onLoadTriggered(self: ApplicationLogic) {.slot.} = + echo "Load Triggered" + self.contactList.add("John", "Doo") + + method onSaveTriggered(self: ApplicationLogic) {.slot.} = + echo "Save Triggered" + + method onExitTriggered(self: ApplicationLogic) {.slot.} = + self.app.quit() + + QtProperty[QVariant] contactList: + read = getContactList + write = "" + notify = "" diff --git a/Nim/Examples/ContactApp/CMakeLists.txt b/Nim/Examples/ContactApp/CMakeLists.txt new file mode 100644 index 0000000..28c0862 --- /dev/null +++ b/Nim/Examples/ContactApp/CMakeLists.txt @@ -0,0 +1,3 @@ +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/main.qml DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/ContactForm.qml DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) +add_nim_executable(TARGET ContactApp SOURCES main.nim PATHS ../../NimQml) \ No newline at end of file diff --git a/Nim/Examples/ContactApp/Contact.nim b/Nim/Examples/ContactApp/Contact.nim new file mode 100644 index 0000000..b9c343e --- /dev/null +++ b/Nim/Examples/ContactApp/Contact.nim @@ -0,0 +1,51 @@ +## Please note we are using templates where ordinarily we would like to use procedures +## due to bug: https://github.com/Araq/Nim/issues/1821 +import NimQml, NimQmlMacros + +QtObject: + type Contact* = ref object of QObject + name: string + surname: string + + proc delete(self: Contact) = + let qobject = self.QObject + qobject.delete() + + proc newContact*(): Contact = + new(result, delete) + result.name = "" + result.create + + method name*(self: Contact): string {.slot.} = + result = self.name + + method nameChanged*(self: Contact) {.signal.} + + method setName(self: Contact, name: string) {.slot.} = + if self.name != name: + self.name = name + self.nameChanged() + + proc `name=`*(self: Contact, name: string) = self.setName(name) + + QtProperty[string] name: + read = name + write = setName + notify = nameChanged + + method surname*(self: Contact): string {.slot.} = + result = self.surname + + method surnameChanged*(self: Contact) {.signal.} + + method setSurname(self: Contact, surname: string) {.slot.} = + if self.surname != surname: + self.surname = surname + self.surnameChanged() + + proc `surname=`*(self: Contact, surname: string) = self.setSurname(surname) + + QtProperty[string] surname: + read = surname + write = setSurname + notify = surnameChanged diff --git a/Nim/Examples/ContactApp/ContactList.nim b/Nim/Examples/ContactApp/ContactList.nim new file mode 100644 index 0000000..49742a6 --- /dev/null +++ b/Nim/Examples/ContactApp/ContactList.nim @@ -0,0 +1,43 @@ +## Please note we are using templates where ordinarily we would like to use procedures +## due to bug: https://github.com/Araq/Nim/issues/1821 +import NimQml, NimQmlMacros, Contact + +QtObject: + type ContactList* = ref object of QObject + contacts*: seq[Contact] + + proc delete*(self: ContactList) = + let qobject = self.QObject + qobject.delete() + + proc newContactList*(): ContactList = + new(result, delete) + result.contacts = @[] + result.create() + + method getCount(self: ContactList): int {.slot.} = + return self.contacts.len + + method countChanged(self: ContactList) {.signal.} + + method add*(self: ContactList, name: string, surname: string) {.slot.} = + let contact = newContact() + contact.name = name + contact.surname = surname + self.contacts.add(contact) + self.countChanged() + + method get*(self: ContactList, index: int): QVariant {.slot.} = + if index < 0 or index >= self.contacts.len: + return newQVariant() + let contact = self.contacts[index] + result = newQVariant(newQVariant(contact)) + + method del*(self: ContactList, index: int) {.slot.} = + self.contacts.del(index) + self.countChanged() + + QtProperty[int] count: + read = getCount + write = "" + notify = countChanged diff --git a/Nim/Examples/ContactApp/main.nim b/Nim/Examples/ContactApp/main.nim new file mode 100644 index 0000000..87f92bc --- /dev/null +++ b/Nim/Examples/ContactApp/main.nim @@ -0,0 +1,15 @@ +import NimQml, ApplicationLogic + +proc mainProc() = + let app = newQApplication() + defer: app.delete + let logic = newApplicationLogic(app) + defer: logic.delete + let engine = newQQmlApplicationEngine() + defer: engine.delete + engine.rootContext.setContextProperty("logic", newQVariant(logic)) + engine.load("main.qml") + app.exec() + +when isMainModule: + mainProc() diff --git a/Nim/Examples/ContactApp/main.qml b/Nim/Examples/ContactApp/main.qml new file mode 100644 index 0000000..8c35cf8 --- /dev/null +++ b/Nim/Examples/ContactApp/main.qml @@ -0,0 +1,52 @@ +import QtQuick 2.2 +import QtQuick.Controls 1.2 +import QtQuick.Layouts 1.1 +import QtQuick.Window 2.1 + +ApplicationWindow +{ + width: 400 + height: 300 + title: "ContactApp" + visible: true + + menuBar: MenuBar { + Menu + { + title: "&File" + MenuItem { text: "Load"; onTriggered: logic.onLoadTriggered() } + MenuItem { text: "Save"; onTriggered: logic.onSaveTriggered() } + MenuItem { text: "Exit"; onTriggered: logic.onExitTriggered() } + } + } + + ColumnLayout + { + anchors.fill: parent + + ListView + { + Layout.fillWidth: true + Layout.fillHeight: true + model: logic.contactList.count + spacing: 5 + delegate: RowLayout { + width: ListView.view.width + property QtObject contact: logic.contactList.get(index) + TextField { Layout.fillWidth: true; text: contact.name } + TextField { Layout.fillWidth: true; text: contact.surname } + Button { text: "Save" } + Button { text: "Delete"; onClicked: logic.contactList.del(index) } + } + } + + RowLayout + { + Label { text: "Name" } + TextField { id: nameTextField; Layout.fillWidth: true; text: "" } + Label { text: "Surname" } + TextField { id: surnameTextField; Layout.fillWidth: true; text: "" } + Button { text: "Add"; onClicked: logic.contactList.add(nameTextField.text, surnameTextField.text) } + } + } +}