SailfishOS Logic: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:SailfishOS::Developer_guide]] | |||
= | [toc align_right="yes" depth="2"] | ||
'''English''' | [[:SailfishOS_Polish|Polski]] | |||
==Available technology== | = Logic = | ||
Except showing nice pages containig widgets full of data usually application has to do some tasks to manipulate the data(in fact it is very common to treat providing data as a task itself). Obviously complexity of tasks vary between diffrent applications and use cases, but even the simplest ones maybe very important. For example the 'add two given numbers' opration in a calculator app is a very simple logic but without it the calculator app wouldn't be very useful. | |||
== Available technology == | |||
Currently there are three languages supported for implementing business logic. However SailfishOS offically support two of them(which are btw. standard solution for Qt/QML apps): | Currently there are three languages supported for implementing business logic. However SailfishOS offically support two of them(which are btw. standard solution for Qt/QML apps): | ||
* ''' | * *C+''*<br />* '''Javascript''' | ||
<br />Except C''+ and Javascript there is also partial support for '''Python''' scripts. However it is not available to put apps based on python into "jolla harbour&quot;:https://harbour.jolla.com/ yet, but you can still install them on devices manually. More info "here&quot;:https://wiki.merproject.org/wiki/Sailfish/Python_Development. | |||
== How can I put logic into my apps? == | |||
You can do it either implicitly - write logic as a functions in qml file or explicitly - call logic from external file. The implicit approach is only available with Javascript and it's recommended only with simple routines. The external solution can be done with all of the supported technology and it's considered as better approach. It lets you decouple code responsible for logic and code responsible for behaviour or appearence which is significat advantage while working on bigger projects. | |||
== C++ == | |||
… | |||
… | == Javascript == | ||
Presented sinippets come from tutorial project 'calqlator' which is appeneded to SailfishOS IDE. | |||
==== Importing external logic - .qml ==== | |||
<code>[…]<br />import "content/calculator.js&quot; as CalcEngine<br />[…]<br />Rectangle {<br /> […]<br /> function operatorPressed(operator) { CalcEngine.operatorPressed(operator) }<br /> function digitPressed(digit) { CalcEngine.digitPressed(digit) }<br />[…]</code> | |||
==== Implicit behaviour code - .qml. ==== | |||
<code>[…]<br />Item {<br /> […]<br /> function displayOperator(operator)<br /> {<br /> listView.model.append({ "operator&quot;: operator, "operand&quot;: "" })<br /> enteringDigits = true<br /> }<br /> […]<br /> function appendDigit(digit)<br /> {<br /> if (!enteringDigits)<br /> listView.model.append({ "operator&quot;: "", "operand&quot;: "" })<br /> var i = listView.model.count - 1;<br /> listView.model.get(i).operand = listView.model.get(i).operand + digit;<br /> enteringDigits = true<br /> }<br />[…]</code> | |||
==== Calling external functions - .qml. ==== | |||
<code>[…]<br />MouseArea {<br /> […]<br /> onClicked: {<br /> //parent.clicked()<br /> if (operator)<br /> window.operatorPressed(parent.text)<br /> else<br /> window.digitPressed(parent.text)<br /> }<br /> }<br />[…]</code> | |||
==== External module with logic - .js. ==== | |||
<code>[…]<br />function disabled(op) {<br /> if (op &quot;.&quot; &amp;&amp; digits.toString().search(/\./) != -1) &#123; | |||
return true | |||
&#125; else if (op window.squareRoot && digits.toString().search(/-/) != <s>1) {<br /> return true<br /> } else {<br /> return false<br /> }<br />} | |||
<br />function digitPressed(op)<br />{<br /> if (disabled(op))<br /> return<br /> if (digits.toString().length >= 14)<br /> return<br /> if (lastOp.toString().length 1 &amp;&amp; ((lastOp &gt;= &quot;0&quot; &amp;&amp; lastOp &lt;= &quot;9&quot;) || lastOp ".") ) {<br /> digits = digits + op.toString()<br /> display.appendDigit(op.toString())<br /> } else {<br /> digits = op<br /> display.appendDigit(op.toString())<br /> }<br /> lastOp = op<br />}<br />[…]</code> | |||
<br />… | |||
<br />h2. Python | |||
<br />Currently the best approach to develop with python on SailfishOS is to use PyOtherSide qml plugin, as it was mentioned before it should be supported in jolla harbour soon. "Here&quot;:http://pyotherside.readthedocs.org/en/latest/ you can find documentation of PyOtherSide. It contains some examples and describes general idea. | |||
<br />Before deploying apps with PyOtherSide on targets with SailfishOS you need to follow guides from "this article&quot;:https://lists.sailfishos.org/pipermail/devel/2013-September/000782.html. Generally you need to instal python3 and PyOtherSide plugin on target machine(either device or emulator). Please read it before trying to build example code presented below. | |||
<br />h4. External module with logic</s> ProjectName\qml\pages\duration.py | |||
<code>import os<br />import struct<br />import pyotherside | |||
def get_item_list(count):<br /> items = [<br /> { 'name': 'first' },<br /> { 'name': 'second' },<br /> { 'name': 'third' },<br /> { 'name': 'fourth' },<br /> { 'name': 'fifth' },<br /> { 'name': 'sixth' },<br /> { 'name': 'seventh' },<br /> { 'name': 'eight' },<br /> { 'name': 'ninth' },<br /> { 'name': 'tenth' },<br /> ]<br /> return items[:count] | |||
= | def get_other_item_list():<br /> items = [<br /> { 'name': 'it works' },<br /> { 'name': 'like' },<br /> { 'name': 'charm' },<br /> { 'name': 'really!' },<br /> ]<br /> return items[:] | ||
pyotherside.atexit(get_item_list)<br />pyotherside.atexit(get_other_item_list)</code> | |||
====Calling external functions | ==== Calling external functions - .qml. ==== | ||
<code>import QtQuick 2.0<br />import Sailfish.Silica 1.0<br />import io.thp.pyotherside 1.0 | |||
Page {<br /> id: page | |||
Python {<br /> id:py<br /> Component.onCompleted: { | |||
addImportPath(Qt.resolvedUrl('.').substr('file://'.length)); | |||
importModule('duration', function() {<br /> call('duration.get_item_list', [5], function(result){<br /> for (var i=0; i&lt;result.length; i+'') {<br /> listModel.append(result[i]);<br /> }<br /> });<br /> });<br /> }<br /> onError: console.log('Python error: ''' traceback)<br /> } | |||
SilicaListView {<br /> PullDownMenu {<br /> MenuItem {<br /> text: "Option 1&quot;<br /> onClicked: py.call('duration.get_other_item_list',[], function(result){<br /> for (var i=0; i&lt;result.length; i+'') {<br /> listModel.append(result[i]);<br /> }<br /> });<br /> }<br /> }<br /> id: listView<br /> model: ListModel{<br /> id: listModel<br /> } | |||
<br /> anchors.fill: parent | |||
<br /> delegate: BackgroundItem {<br /> id: delegate | |||
<br /> Label {<br /> x: Theme.paddingLarge<br /> text: name'' " item&quot;<br /> anchors.verticalCenter: parent.verticalCenter<br /> color: delegate.highlighted ? Theme.highlightColor : Theme.primaryColor<br /> }<br /> onClicked: console.log("Clicked " + name)<br /> }<br /> VerticalScrollDecorator {} | |||
}<br />}</code> | |||
====Entry point for qml pages | ==== Entry point for qml pages - .qml. ==== | ||
<code>import QtQuick 2.0<br />import Sailfish.Silica 1.0<br />import "pages&quot; | |||
ApplicationWindow<br />{<br /> initialPage: Component { Page { } }<br />}</code> | |||
… | |||
Revision as of 10:40, 24 February 2015
[toc align_right="yes" depth="2"]
English | Polski
Logic
Except showing nice pages containig widgets full of data usually application has to do some tasks to manipulate the data(in fact it is very common to treat providing data as a task itself). Obviously complexity of tasks vary between diffrent applications and use cases, but even the simplest ones maybe very important. For example the 'add two given numbers' opration in a calculator app is a very simple logic but without it the calculator app wouldn't be very useful.
Available technology
Currently there are three languages supported for implementing business logic. However SailfishOS offically support two of them(which are btw. standard solution for Qt/QML apps):
- *C+*
* Javascript
Except C+ and Javascript there is also partial support for Python scripts. However it is not available to put apps based on python into "jolla harbour":https://harbour.jolla.com/ yet, but you can still install them on devices manually. More info "here":https://wiki.merproject.org/wiki/Sailfish/Python_Development.
How can I put logic into my apps?
You can do it either implicitly - write logic as a functions in qml file or explicitly - call logic from external file. The implicit approach is only available with Javascript and it's recommended only with simple routines. The external solution can be done with all of the supported technology and it's considered as better approach. It lets you decouple code responsible for logic and code responsible for behaviour or appearence which is significat advantage while working on bigger projects.
C++
…
Javascript
Presented sinippets come from tutorial project 'calqlator' which is appeneded to SailfishOS IDE.
Importing external logic - .qml
[…]<br />import "content/calculator.js&quot; as CalcEngine<br />[…]<br />Rectangle {<br /> […]<br /> function operatorPressed(operator) { CalcEngine.operatorPressed(operator) }<br /> function digitPressed(digit) { CalcEngine.digitPressed(digit) }<br />[…]
Implicit behaviour code - .qml.
[…]<br />Item {<br /> […]<br /> function displayOperator(operator)<br /> {<br /> listView.model.append({ "operator&quot;: operator, "operand&quot;: "" })<br /> enteringDigits = true<br /> }<br /> […]<br /> function appendDigit(digit)<br /> {<br /> if (!enteringDigits)<br /> listView.model.append({ "operator&quot;: "", "operand&quot;: "" })<br /> var i = listView.model.count - 1;<br /> listView.model.get(i).operand = listView.model.get(i).operand + digit;<br /> enteringDigits = true<br /> }<br />[…]
Calling external functions - .qml.
[…]<br />MouseArea {<br /> […]<br /> onClicked: {<br /> //parent.clicked()<br /> if (operator)<br /> window.operatorPressed(parent.text)<br /> else<br /> window.digitPressed(parent.text)<br /> }<br /> }<br />[…]
External module with logic - .js.
[…]<br />function disabled(op) {<br /> if (op &quot;.&quot; &amp;&amp; digits.toString().search(/\./) != -1) &#123;
return true
&#125; else if (op window.squareRoot && digits.toString().search(/-/) != <s>1) {<br /> return true<br /> } else {<br /> return false<br /> }<br />}
<br />function digitPressed(op)<br />{<br /> if (disabled(op))<br /> return<br /> if (digits.toString().length >= 14)<br /> return<br /> if (lastOp.toString().length 1 &amp;&amp; ((lastOp &gt;= &quot;0&quot; &amp;&amp; lastOp &lt;= &quot;9&quot;) || lastOp ".") ) {<br /> digits = digits + op.toString()<br /> display.appendDigit(op.toString())<br /> } else {<br /> digits = op<br /> display.appendDigit(op.toString())<br /> }<br /> lastOp = op<br />}<br />[…]
…
h2. Python
Currently the best approach to develop with python on SailfishOS is to use PyOtherSide qml plugin, as it was mentioned before it should be supported in jolla harbour soon. "Here":http://pyotherside.readthedocs.org/en/latest/ you can find documentation of PyOtherSide. It contains some examples and describes general idea.
Before deploying apps with PyOtherSide on targets with SailfishOS you need to follow guides from "this article":https://lists.sailfishos.org/pipermail/devel/2013-September/000782.html. Generally you need to instal python3 and PyOtherSide plugin on target machine(either device or emulator). Please read it before trying to build example code presented below.
h4. External module with logic ProjectName\qml\pages\duration.py
import os<br />import struct<br />import pyotherside
def get_item_list(count):<br /> items = [<br /> { 'name': 'first' },<br /> { 'name': 'second' },<br /> { 'name': 'third' },<br /> { 'name': 'fourth' },<br /> { 'name': 'fifth' },<br /> { 'name': 'sixth' },<br /> { 'name': 'seventh' },<br /> { 'name': 'eight' },<br /> { 'name': 'ninth' },<br /> { 'name': 'tenth' },<br /> ]<br /> return items[:count]
def get_other_item_list():<br /> items = [<br /> { 'name': 'it works' },<br /> { 'name': 'like' },<br /> { 'name': 'charm' },<br /> { 'name': 'really!' },<br /> ]<br /> return items[:]
pyotherside.atexit(get_item_list)<br />pyotherside.atexit(get_other_item_list)
Calling external functions - .qml.
import QtQuick 2.0<br />import Sailfish.Silica 1.0<br />import io.thp.pyotherside 1.0
Page {<br /> id: page
Python {<br /> id:py<br /> Component.onCompleted: {
addImportPath(Qt.resolvedUrl('.').substr('file://'.length));
importModule('duration', function() {<br /> call('duration.get_item_list', [5], function(result){<br /> for (var i=0; i&lt;result.length; i+'') {<br /> listModel.append(result[i]);<br /> }<br /> });<br /> });<br /> }<br /> onError: console.log('Python error: ''' traceback)<br /> }
SilicaListView {<br /> PullDownMenu {<br /> MenuItem {<br /> text: "Option 1&quot;<br /> onClicked: py.call('duration.get_other_item_list',[], function(result){<br /> for (var i=0; i&lt;result.length; i+'') {<br /> listModel.append(result[i]);<br /> }<br /> });<br /> }<br /> }<br /> id: listView<br /> model: ListModel{<br /> id: listModel<br /> }
<br /> anchors.fill: parent
<br /> delegate: BackgroundItem {<br /> id: delegate
<br /> Label {<br /> x: Theme.paddingLarge<br /> text: name'' " item&quot;<br /> anchors.verticalCenter: parent.verticalCenter<br /> color: delegate.highlighted ? Theme.highlightColor : Theme.primaryColor<br /> }<br /> onClicked: console.log("Clicked " + name)<br /> }<br /> VerticalScrollDecorator {}
}<br />}
Entry point for qml pages - .qml.
import QtQuick 2.0<br />import Sailfish.Silica 1.0<br />import "pages&quot;
ApplicationWindow<br />{<br /> initialPage: Component { Page { } }<br />}
…