bar widget, are regions possible?

More
10 Feb 2014 09:20 #43689 by gmouer
I did my first attempt at a pyvcp panel today with pretty good success. I did a spindle load meter and it works nicely. I used the bar widget for the display but would really like to have green yellow red regions/ zones on the bar. I seen that the meter widget allows setting of regions like this but could not find any info on doing similar with a bar.

I would prefer to use the bar display rather than the meter. I think glade allows a bar with regions but would rather use pyvcp if possible.

Is there any way of doing regions like this on a pyvcp bar?

Please Log in or Create an account to join the conversation.

More
10 Feb 2014 20:48 #43699 by BigJohnT
Both the dial and the bar are subclassed from a TkInter Canvas so I don't see why you can't add that. In a RIP the file is lib/python/pyvcp_widgets.py.

JT

Please Log in or Create an account to join the conversation.

More
10 Feb 2014 21:33 #43701 by gmouer
Thanks John. I was hoping that it was already available out there somewhere. I will explore what it takes to add the feature but python is like greek to me. I am still new to linux and linuxcnc and the learning curve can be overwhelming at times.

Glade already has that feature in its bar display, I am surprised that pyvcp does not also. Maybe it could be put on a feature request for the future? Others would surely find it handy I am sure.

Meanwhile, my spindle load meter pyvcp panel is working nicely.

Thanks again, I will explore the link you posted.

Please Log in or Create an account to join the conversation.

More
10 Feb 2014 21:46 - 10 Feb 2014 21:48 #43702 by ArcEye

I did my first attempt at a pyvcp panel today with pretty good success. I did a spindle load meter and it works nicely. I used the bar widget for the display but would really like to have green yellow red regions/ zones on the bar. I seen that the meter widget allows setting of regions like this but could not find any info on doing similar with a bar.

Is there any way of doing regions like this on a pyvcp bar?


There is now.
Because there is no dial and needle, it does not make much sense to have a coloured scale, but the colour of the bar itself can be changed to reflect its state.

Comment out the original class pyvcp_bar in pyvcp_widgets.py
Then insert this code
class pyvcp_bar(Canvas):

## reworked by ArcEye 10022014
##	   choice to have value ranges in different colours
    """ (indicator) a bar-indicator for a float
    <bar>
        <halpin>"my-bar"</halpin>
        <min_>0</min_>
        <max_>150</max_>
        <bgcolor>"grey"</bgcolor>
 	<range1>(0,80,"green")</range1>
        <range2>(80,100,"orange")</range2>
        <range3>(100,123,"red")</range3>
        <fillcolor>"green"</fillcolor>
    </bar>
    """
    n=0

    def __init__(self,master,pycomp,fillcolor="green",bgcolor="grey",
               halpin=None,min_=0.0,max_=100.0,range1=None,range2=None,range3=None,**kw):
    
        self.cw=200    # canvas width
        self.ch=50     # canvas height
        self.bh=30     # bar height
        self.bw=150    # bar width
        self.pad=((self.cw-self.bw)/2)
		
        Canvas.__init__(self,master,width=self.cw,height=self.ch)

        if halpin == None:
            halpin = "bar."+str(pyvcp_bar.n)
            pyvcp_bar.n += 1
        self.halpin=halpin
        self.endval=max_
        self.startval=min_


        pycomp.newpin(halpin, HAL_FLOAT, HAL_IN)
        
        self.value=0.0 # some dummy value to start with  
             
        pycomp[self.halpin] = self.value
        
        # the border
        border=self.create_rectangle(self.pad,1,self.pad+self.bw,self.bh)
        self.itemconfig(border,fill=bgcolor)
        
        # the bar
        tmp=self.bar_coords()
        start=tmp[0]
        end=tmp[1]
        self.bar=self.create_rectangle(start,2,end,self.bh-1)
	# default fill unless overriden
        self.itemconfig(self.bar,fill=fillcolor)

        # start text
        start_text=self.create_text(self.pad,self.bh+10,text=str(self.startval) )
        #end text
        end_text=self.create_text(self.pad+self.bw,self.bh+10,text=str(self.endval) )
        # value text
        self.val_text=self.create_text(self.pad+self.bw/2,
                                   self.bh/2,text=str(self.value) )
	if range1!=None: 
	    self.range1 = range1
        if range2!=None: 
	    self.range2 = range2
        if range3!=None: 
	    self.range3 = range3

    def set_fill(self, (start1, end1, color1),(start2, end2, color2), (start3, end3, color3)):
        if self.value:
    	    if (self.value > start1) and (self.value <= end1):
    		self.itemconfig(self.bar,fill=color1)	
    	    else:
    		if (self.value > start2) and (self.value <= end2):
    		    self.itemconfig(self.bar,fill=color2)	
		else:
		    if (self.value > start3) and (self.value <= end3):
    			self.itemconfig(self.bar,fill=color3)	
	
	
    def bar_coords(self):
        """ calculates the coordinates in pixels for the bar """
        # the bar should start at value = zero 
        # and extend to value = self.value
        # it should not extend beyond the initial box reserved for the bar
        min_pixels=self.pad
        max_pixels=self.pad+self.bw
        bar_end = min_pixels + ((float)(max_pixels-min_pixels)/(float)(self.endval-self.startval)) * (self.value-self.startval)
        if bar_end>max_pixels:
            bar_end = max_pixels
        elif bar_end < min_pixels:
            bar_end = min_pixels
        bar_start = min_pixels + ((float)(max_pixels-min_pixels)/(float)(self.endval-self.startval)) * (0-self.startval)
        if bar_start < min_pixels:  # don't know if this is really needed
            bar_start = min_pixels

        return [bar_start, bar_end]
    
			
    def update(self,pycomp):
        # update value
        newvalue=pycomp[self.halpin]
        if newvalue != self.value:
            self.value = newvalue
            valtext = str( "%(b)3.1f" % {'b':self.value} )
            self.itemconfig(self.val_text,text=valtext)
            # set bar colour
            if self.range1 and self.range2 and self.range3:
        	self.set_fill(self.range1, self.range2, self.range3)
            # set bar size
            tmp=self.bar_coords()
            start=tmp[0]
            end=tmp[1]
            self.coords(self.bar, start, 2, 
                        end, self.bh-1)


# -------------------------------------------

The revised widget takes 3 range definitions (you must use all 3 or none will be used, if you just want 2, set 2 of them to the same colour)
If these are set the bar colour will change depending upon the value of the pin

xml file snippet
<pyvcp>
  	<hbox>
	<bar>
        <halpin>"my-bar"</halpin>
        <min_>0</min_>
        <max_>150</max_>
        <bgcolor>"grey"</bgcolor>
 	<range1>(0,100,"green")</range1>
        <range2>(100,130,"orange")</range2>
        <range3>(130,150,"red")</range3>
	</bar>
	</hbox>
	<hbox>
	<scale>
	    <font>("Helvetica",16)</font>
    	<width>"25"</width>
    	<halpin>"my-hscale"</halpin>
    	<resolution>1</resolution>
    	<orient>HORIZONTAL</orient>
    	<initval>0</initval>
    	<min_>0</min_>
    	<max_>150</max_>
	</scale>
	</hbox>
</pyvcp>

postgui hal file snippet
net bar1 pyvcp.my-bar <= pyvcp.my-hscale-f

and results










bar changes colour with increase in value within the ranges set

regards
Attachments:
Last edit: 10 Feb 2014 21:48 by ArcEye.
The following user(s) said Thank You: BigJohnT

Please Log in or Create an account to join the conversation.

More
10 Feb 2014 22:35 - 11 Feb 2014 07:51 #43705 by gmouer
WOW ! I cannot thank you enough! Coming to linuxcnc as a former mach3 user, I just love linuxcnc and am constantly telling people about it, ESPECIALLY the unbelievable support!!! Support like this is beyond what anyone could reasonably expect.


Thanks so much!

I am off to add the code!


George

Follow up Works just as advertised !! Thanks once again for the effort and great support.
Last edit: 11 Feb 2014 07:51 by gmouer. Reason: follow up

Please Log in or Create an account to join the conversation.

Time to create page: 0.770 seconds
Powered by Kunena Forum