Notices


Reply
Thread Tools
Markkyboy's Avatar
Posts: 320 | Thanked: 444 times | Joined on Oct 2012 @ Costa Blanca, España
#1
Hi,
as the title suggests, I'm trying to associate colours to weather temperature, mostly it seems to work until I get to single figures and minus temperatures.

Here is my clunky code, this code is used in `/usr/lib/qt5/qml/Sailfish/Weather/WeatherDetailsHeader.qml`

Code:
slotted in @ line: 124
           
    Rectangle {
        id: colorTemperature
        width: 100; height: 140
        radius: 12
        anchors.centerIn: temperatureHighLabel
        color: {
            ((temperatureHighLabel.text  >= '38'  && '#b20012' || // dark red
              temperatureHighLabel.text  >= '32'  && '#bd0014' || // red
              temperatureHighLabel.text  >= '27'  && '#ec0019' || // scarlet
              temperatureHighLabel.text  >= '21'  && '#ec9649' || // orange
              temperatureHighLabel.text  >= '16'  && '#f7c200' || // yellow
              temperatureHighLabel.text  >= '10'  && '#9ad346' || // rich green
              temperatureHighLabel.text  >= '4'   && '#47b04b' || // green
              temperatureHighLabel.text  >= '-1'  && '#4293ff' || // sky blue
              temperatureHighLabel.text  >= '-7'  && '#3c00ff' || // blue
              temperatureHighLabel.text  >= '-12' && '#6a28a3' || // purple
              temperatureHighLabel.text  >= '-18' && '#8d009a' || // pinky purple
              temperatureHighLabel.text  >= '-23' && '#ff00ff' )) // magenta
         }
    }
    Label {
        id: temperatureHighLabel
The colour shows correctly until I get down to single and minus figures, for example, a day that is '8º' which should be shown as 'green' but is shown as 'dark red', in fact 'dark red' is the colour of all single figures, yet 'dark red' is associated with temps greater than or equal to 38º. I have also tried using plus+ and minus- symbols with temps but to no avail.

I've played with this extensively, originally it was in if/else format but the same problem occurred. I've tried different java operators, I've tried adding plus/minus symbols to temp figures, but still I get dark red for single and minus figures. Any ideas appreciated.

The format used for laying out my code, was taken directly from harbour-nationalrail used for associating rail operator colours. Originally, the code was laid out in the same way as my windDirection patch, that being inthe style of 'if/else' statements, but that yielded the same problem as does the above.
__________________
| Openrepos | T.J.C. |

Don't just sit there standing around, pick up a shovel and sweep up!

Last edited by Markkyboy; 2018-09-04 at 09:57.
 
coderus's Avatar
Posts: 6,158 | Thanked: 11,661 times | Joined on Nov 2011 @ Open Mobile Platform, Innopolis, Russia
#2
you can try to learn qml basics.

if () else if () ...
__________________
Twitter | Openrepos | GitHub | PayPal.Me
 
Markkyboy's Avatar
Posts: 320 | Thanked: 444 times | Joined on Oct 2012 @ Costa Blanca, España
#3
Originally Posted by coderus View Post
you can try to learn qml basics.

if () else if () ...
I have already mentioned in the last paragraph of my original question, that I have already tried using if/else and it does work....until I hit minus or single figures.

I am always learning, it is fine and right for you to say, 'learn qml basics', what do you think I am trying to do?, I am constantly reading doc.qt QML documents online, I have nearly all saved as browser favourites. I also try as much as I can to solve the problems I create before asking questions here. What is clearly obvious to you in many cases, isn't so obvious to me.

Most of the examples set out by Qt, is basic stuff I have already learned, basic Text, Image, Rectangle,Color and more, many of the examples do not go into any detail and what may work on a QtCreator SDK, doesn't always work in Sailfish, as we know, org.nemomobile.configuration 1.0 replaces Settings 1.0. ...when I say "doesn't always work" actually means "I don't fully understand what I'm doing" <------from that, I take cues from stuff I know that works, like reusing the contents of my WindDirection.patch, which used 'if/else' and works correctly.

Code:
            
    Rectangle {
        id: colorTemperature
        width: 100; height: 140
        radius: 12
        anchors.centerIn: temperatureHighLabel
        color: {
            if      (temperatureHighLabel.text >= "38" )  "#b20012" // dark red
            else if (temperatureHighLabel.text >= "32" )  "#bd0014" // red
            else if (temperatureHighLabel.text >= "27" )  "#ec0019" // scarlet
            else if (temperatureHighLabel.text >= "21" )  "#ec9649" // orange
            else if (temperatureHighLabel.text >= "16" )  "#f7c200" // yellow
            else if (temperatureHighLabel.text >= "10" )  "#9ad346" // rich green
            else if (temperatureHighLabel.text >= "4"  )  "#47b04b" // green
            else if (temperatureHighLabel.text <= "-1" )  "#4293ff" // sky blue
            else if (temperatureHighLabel.text <= "-7" )  "#3c00ff" // blue
            else if (temperatureHighLabel.text <= "-12")  "#6a28a3" // purple
            else if (temperatureHighLabel.text <= "-18")  "#8d009a" // pinky purple
            else if (temperatureHighLabel.text <= "-23")  "#bd009a" // magenta
       }
}
the use of if/else above is the same as I used in windDirection, I wrongly assumed it would work, but it does work until minus or single figures occur. I don't know how to solve it, so I ask for help.

Did you never ask for help while learning your trade?
__________________
| Openrepos | T.J.C. |

Don't just sit there standing around, pick up a shovel and sweep up!
 
pichlo's Avatar
Posts: 5,563 | Thanked: 17,346 times | Joined on Sep 2012 @ UK
#4
^^^ That and trying not to confuse comparing strings with numbers.

"8" > "32" or even "32000", just like "z" > "aaaaaaaaa".
__________________
In particle accelerators atoms are indeed not only touching each others. But banging together in a massive explosive orgasm.
-- nieldk in a TMO post
 

The Following User Says Thank You to pichlo For This Useful Post:
Ancelad's Avatar
Posts: 1,471 | Thanked: 2,770 times | Joined on Jun 2012 @ Russia, 96
#5
Originally Posted by pichlo View Post
^^^ That and trying not to confuse comparing strings with numbers.

"8" > "32" or even "32000", just like "z" > "aaaaaaaaa".
It doesn't matter if text returns numbers
__________________
Twitter: @iAncelad
Try my SailfishOS theme for Nokia N9
 
pichlo's Avatar
Posts: 5,563 | Thanked: 17,346 times | Joined on Sep 2012 @ UK
#6
Originally Posted by Ancelad View Post
It doesn't matter if text returns numbers
Really? I stand corrected. But it means I dislike QML even more than before.

Being Old Skool, I would convert temperatureHighLabel.text to a number and work with that. It would be faster anyway if you do the conversion only once and not at the background for each comparison.

It would also give you a lovely opportunity to change the colour continuously rather than in discreet steps like in your example
__________________
In particle accelerators atoms are indeed not only touching each others. But banging together in a massive explosive orgasm.
-- nieldk in a TMO post

Last edited by pichlo; 2018-09-04 at 11:00.
 

The Following User Says Thank You to pichlo For This Useful Post:
Markkyboy's Avatar
Posts: 320 | Thanked: 444 times | Joined on Oct 2012 @ Costa Blanca, España
#7
Originally Posted by pichlo View Post
Really? I stand corrected. But it means I dislike QML even more than before.

Being Old Skool, I would convert temperatureHighLabel.text to a number and work with that. It would be faster anyway if you do the conversion only once and not at the background for each comparison.

It would also give you a lovely opportunity to change the colour continuously rather than in discreet steps like in your example
temperatureHighLabel.text is converted to a number, that being the same number you see in the app but as temperature. I wholly agree about the way I'm using the code, I know its ugly, but for now, it's worked in previous applications/patches.
I don't yet know how else to actually tackle it. My WindDirection patch uses the same method, it gets windDirection as a number and converts to a given heading as text, so I know it works.....at least in that instance.

thanks,
__________________
| Openrepos | T.J.C. |

Don't just sit there standing around, pick up a shovel and sweep up!
 
Posts: 31 | Thanked: 25 times | Joined on Aug 2008 @ Netherlands
#8
Originally Posted by Ancelad View Post
It doesn't matter if text returns numbers
Really? So (-2 > -1) returns the same as ("-2" > "-1")?
 

The Following 2 Users Say Thank You to wdehoog For This Useful Post:
velox's Avatar
Posts: 340 | Thanked: 1,155 times | Joined on Dec 2009
#9
Try something like:
Code:
          
Rectangle {
    id: colorTemperature
    width: 100; height: 140
    radius: 12
    property int temperatureInt: {
        return parseInt(temperatureHighLabel.text)
    }
    anchors.centerIn: temperatureHighLabel
    color: {
        if (temperatureInt >= 38 ) {
            return "#b20012" // dark red
        }
        else if (temperatureInt >= 32 ) {
            return "#bd0014" // red
        }
        else if (temperatureInt >= 27 ) {
            return "#ec0019" // scarlet
        }
        else if (temperatureInt >= 21 ) {
            return "#ec9649" // orange
        }
        else if (temperatureInt >= 16 ) {
            return "#f7c200" // yellow
        }
        else if (temperatureInt >= 10 ) {
            return "#9ad346" // rich green
        }
        else if (temperatureInt >= 4  ) {
            return "#47b04b" // green
        }
        else if (temperatureInt >= -1 ) {
            return "#4293ff" // sky blue
        }
        else if (temperatureInt >= -7 ) {
            return "#3c00ff" // blue
        }
        else if (temperatureInt >= -12) {
            return "#6a28a3" // purple
        }
        else if (temperatureInt >= -18) {
            return "#8d009a" // pinky purple
        }
        else return "#bd009a" // magenta
    }
}
I have not tested it in any way, just quickly written it down, so I may have missed something or it may not be correct syntax (I may or may not have even added errors on purpose to help you learn – just kidding). More or less interesting points are:
  • there is a new property with an integer type, this should fix the negative comparisons
  • You had a gap (no colours between -1 and 4) between where you switched to "<=" – it's easier to just stick with ">=" and make an else for everything that does not match.

Keep in mind:
Long lists of if/else almost always are a sign that something could be better thought out.

Edit: The number conversion, of course, is exactly what pichlo pointed out earlier. I just wrote it down.
__________________
slumber: sensors enabled sleep timer for SFOS (translations/input/… appreciated if you've got some spare time)
talefish: directory based audiobook player for SFOS
nofono: ofono restart for SFOS
___
list of i486/noarch packages on openrepos (jolla tablet)

Last edited by velox; 2018-09-05 at 06:54. Reason: credit where credit is due
 

The Following 4 Users Say Thank You to velox For This Useful Post:
Ancelad's Avatar
Posts: 1,471 | Thanked: 2,770 times | Joined on Jun 2012 @ Russia, 96
#10
Originally Posted by wdehoog View Post
Really? So (-2 > -1) returns the same as ("-2" > "-1")?
I mean "clear" numbers without " obviously
__________________
Twitter: @iAncelad
Try my SailfishOS theme for Nokia N9
 

The Following 2 Users Say Thank You to Ancelad For This Useful Post:
Reply

Thread Tools

 
Forum Jump


All times are GMT. The time now is 12:38.