SailfishOS Logic: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
'''English''' | [[SailfishOS Polish|Polski]]
[[Category:SailfishOS::Developer_guide]]


=Logic=
[toc align_right="yes" depth="2"]


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.
'''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++'''
* *C+''*<br />* '''Javascript'''
* '''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 &quot;jolla harbour&amp;quot;:https://harbour.jolla.com/ yet, but you can still install them on devices manually. More info &quot;here&amp;quot;:https://wiki.merproject.org/wiki/Sailfish/Python_Development.


Except C++ and Javascript there is also partial support for '''Python''' scripts. However it is not available to put apps based on python into [https://harbour.jolla.com/ jolla harbour] ''[harbour.jolla.com]'' yet, but you can still install them on devices manually. More info [https://wiki.merproject.org/wiki/Sailfish/Python_Development here] ''[wiki.merproject.org]''.
== How can I put logic into my apps? ==


==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.


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++ ==


==C++==


== Javascript ==
 
Presented sinippets come from tutorial project 'calqlator' which is appeneded to SailfishOS IDE.
 
==== Importing external logic - .qml ====
 
<code>[]<br />import &quot;content/calculator.js&amp;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({ &quot;operator&amp;quot;: operator, &quot;operand&amp;quot;: &quot;&quot; })<br /> enteringDigits = true<br /> }<br /> […]<br /> function appendDigit(digit)<br /> {<br /> if (!enteringDigits)<br /> listView.model.append({ &quot;operator&amp;quot;: &quot;&quot;, &quot;operand&amp;quot;: &quot;&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  &amp;quot;.&amp;quot; &amp;amp;&amp;amp; digits.toString().search(/\./) != -1) &amp;#123;
        return true
    &amp;#125; else if (op  window.squareRoot &amp;&amp; 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 &gt;= 14)<br /> return<br /> if (lastOp.toString().length  1 &amp;amp;&amp;amp; ((lastOp &amp;gt;= &amp;quot;0&amp;quot; &amp;amp;&amp;amp; lastOp &amp;lt;= &amp;quot;9&amp;quot;) || lastOp  &quot;.&quot;) ) {<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. &quot;Here&amp;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 &quot;this article&amp;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


==Javascript==
<code>import os<br />import struct<br />import pyotherside


Presented sinippets come from tutorial project ‘calqlator’ which is appeneded to SailfishOS <span class="caps">IDE</span>.
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]


====Importing external logic – \calqlatr\qml\calqlatr.qml====
def get_other_item_list():<br /> items = [<br /> { 'name': 'it works' },<br /> { 'name': 'like' },<br /> { 'name': 'charm' },<br /> { 'name': 'really!' },<br /> ]<br /> return items[:]


====Implicit behaviour code – \calqlatr\qml\content\Display.qml.====
pyotherside.atexit(get_item_list)<br />pyotherside.atexit(get_other_item_list)</code>


====Calling external functions – \calqlatr\qml\content\Button.qml.====
==== Calling external functions - .qml. ====


====External module with logic – \calqlatr\qml\content\calculator.js.====
<code>import QtQuick 2.0<br />import Sailfish.Silica 1.0<br />import io.thp.pyotherside 1.0


Page {<br /> id: page


==Python==
Python {<br /> id:py<br /> Component.onCompleted: {


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. [http://pyotherside.readthedocs.org/en/latest/ Here] ''[pyotherside.readthedocs.org]'' you can find documentation of PyOtherSide. It contains some examples and describes general idea.
addImportPath(Qt.resolvedUrl('.').substr('file://'.length));


Before deploying apps with PyOtherSide on targets with SailfishOS you need to follow guides from [https://lists.sailfishos.org/pipermail/devel/2013-September/000782.html this article] ''[lists.sailfishos.org]''. 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.
importModule('duration', function() {<br /> call('duration.get_item_list', [5], function(result){<br /> for (var i=0; i&amp;lt;result.length; i+'') {<br /> listModel.append(result[i]);<br /> }<br /> });<br /> });<br /> }<br /> onError: console.log('Python error: ''' traceback)<br /> }


====External module with logic – ProjectName\qml\pages\duration.py====
SilicaListView {<br /> PullDownMenu {<br /> MenuItem {<br /> text: &quot;Option 1&amp;quot;<br /> onClicked: py.call('duration.get_other_item_list',[], function(result){<br /> for (var i=0; i&amp;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'' &quot; item&amp;quot;<br /> anchors.verticalCenter: parent.verticalCenter<br /> color: delegate.highlighted ? Theme.highlightColor : Theme.primaryColor<br /> }<br /> onClicked: console.log(&quot;Clicked &quot; + name)<br /> }<br /> VerticalScrollDecorator {}


====Calling external functions – \calqlatr\qml\content\Page.qml.====
}<br />}</code>


====Entry point for qml pages – \calqlatr\qml\ProjectName.qml.====
==== Entry point for qml pages - .qml. ====


<code>import QtQuick 2.0<br />import Sailfish.Silica 1.0<br />import &quot;pages&amp;quot;


===Categories:===
ApplicationWindow<br />{<br /> initialPage: Component { Page { } }<br />}</code>


* [[:Category:SailfishOS|SailfishOS]]
** [[:Category:SailfishOS::Developer guide|Developer_guide]]

Revision as of 10:40, 24 February 2015


[toc align_right="yes&quot; depth="2&quot;]

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&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

[]<br />import &quot;content/calculator.js&amp;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({ &quot;operator&amp;quot;: operator, &quot;operand&amp;quot;: &quot;&quot; })<br /> enteringDigits = true<br /> }<br /> []<br /> function appendDigit(digit)<br /> {<br /> if (!enteringDigits)<br /> listView.model.append({ &quot;operator&amp;quot;: &quot;&quot;, &quot;operand&amp;quot;: &quot;&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  &amp;quot;.&amp;quot; &amp;amp;&amp;amp; digits.toString().search(/\./) != -1) &amp;#123;
        return true
    &amp;#125; else if (op  window.squareRoot &amp;&amp; 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 &gt;= 14)<br /> return<br /> if (lastOp.toString().length  1 &amp;amp;&amp;amp; ((lastOp &amp;gt;= &amp;quot;0&amp;quot; &amp;amp;&amp;amp; lastOp &amp;lt;= &amp;quot;9&amp;quot;) || lastOp  &quot;.&quot;) ) {<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&quot;: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&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.
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&amp;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: &quot;Option 1&amp;quot;<br /> onClicked: py.call('duration.get_other_item_list',[], function(result){<br /> for (var i=0; i&amp;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'' &quot; item&amp;quot;<br /> anchors.verticalCenter: parent.verticalCenter<br /> color: delegate.highlighted ? Theme.highlightColor : Theme.primaryColor<br /> }<br /> onClicked: console.log(&quot;Clicked &quot; + name)<br /> }<br /> VerticalScrollDecorator {}

}<br />}

Entry point for qml pages - .qml.

import QtQuick 2.0<br />import Sailfish.Silica 1.0<br />import &quot;pages&amp;quot;

ApplicationWindow<br />{<br /> initialPage: Component { Page { } }<br />}