mirror of
https://github.com/status-im/dotherside.git
synced 2025-02-14 13:46:39 +00:00
Added a sample contact application
Basic contact app the shows how to expose a list of elements to Qml
This commit is contained in:
parent
e8075796bc
commit
42ded08c7f
@ -1,4 +1,5 @@
|
|||||||
add_subdirectory(HelloWorld)
|
add_subdirectory(HelloWorld)
|
||||||
add_subdirectory(SimpleData)
|
add_subdirectory(SimpleData)
|
||||||
add_subdirectory(SlotsAndProperties)
|
add_subdirectory(SlotsAndProperties)
|
||||||
add_subdirectory(QtObjectMacro)
|
add_subdirectory(QtObjectMacro)
|
||||||
|
add_subdirectory(ContactApp)
|
34
Nim/Examples/ContactApp/ApplicationLogic.nim
Normal file
34
Nim/Examples/ContactApp/ApplicationLogic.nim
Normal file
@ -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 = ""
|
3
Nim/Examples/ContactApp/CMakeLists.txt
Normal file
3
Nim/Examples/ContactApp/CMakeLists.txt
Normal file
@ -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)
|
51
Nim/Examples/ContactApp/Contact.nim
Normal file
51
Nim/Examples/ContactApp/Contact.nim
Normal file
@ -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
|
43
Nim/Examples/ContactApp/ContactList.nim
Normal file
43
Nim/Examples/ContactApp/ContactList.nim
Normal file
@ -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
|
15
Nim/Examples/ContactApp/main.nim
Normal file
15
Nim/Examples/ContactApp/main.nim
Normal file
@ -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()
|
52
Nim/Examples/ContactApp/main.qml
Normal file
52
Nim/Examples/ContactApp/main.qml
Normal file
@ -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) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user