class CTRL(wx.Panel): """ Delai d'actualisation en secondes """ def __init__(self, parent, delai=60, listeActivites=[], fps=20, ppf=2, couleurFond=None): wx.Panel.__init__(self, parent, id=-1) self.parent = parent self.delai = delai self.listeActivites = listeActivites if couleurFond != None : self.SetBackgroundColour(couleurFond) self.timer = wx.Timer(self, -1) self.ticker = Ticker(self) self.ticker.SetBackgroundColour(self.GetBackgroundColour()) self.ticker.SetFPS(fps) self.ticker.SetPPF(ppf) # Layout sizer_base = wx.BoxSizer(wx.VERTICAL) sizer_base.Add(self.ticker, 1, wx.EXPAND|wx.ALL, 5) self.SetSizer(sizer_base) sizer_base.Fit(self) # Binds self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer) def SetTexte(self, texte=u""): self.ticker.SetText(texte) def Stop(self): """Stop moving the text""" self.timer.Stop() def Start(self): """Starts the text moving""" if not self.timer.IsRunning(): self.timer.Start(self.delai * 1000) def OnTimer(self, event): self.MAJ() def SetActivites(self, listeActivites=[]): self.listeActivites = listeActivites def MAJ(self): try : texte = self.GetTexte(self.listeActivites) self.SetTexte(texte) except : texte = u"" if len(texte) > 0 : etat = True if self.ticker.IsTicking() == False : self.ticker.Start() else : etat = False if self.ticker.IsTicking() : self.ticker.Stop() try : self.parent.AffichePresents(etat) except : pass def JoinListe(self, listeTemp=[]): if len(listeTemp) > 2 : return _(u"%s et %s") % (u", ".join(listeTemp[:-1], listeTemp[-1])) else : return _(u" et ").join(listeTemp) def GetTexte(self, listeActivites=[]): """ Récupération des données dans la base de données """ if len(listeActivites) == 0 : conditionActivites = "()" elif len(listeActivites) == 1 : conditionActivites = "(%d)" % listeActivites[0] else : conditionActivites = str(tuple(listeActivites)) now = datetime.datetime.now() date = datetime.date(now.year, now.month, now.day) heure = "%02d:%02d" % (now.hour, now.minute) DB = GestionDB.DB() req = """SELECT activites.IDactivite, activites.nom, groupes.IDgroupe, groupes.nom, groupes.ordre, COUNT(IDconso), SUM(quantite) FROM consommations LEFT JOIN activites ON activites.IDactivite = consommations.IDactivite LEFT JOIN groupes ON groupes.IDgroupe = consommations.IDgroupe WHERE consommations.IDactivite IN %s AND date = '%s' AND heure_debut <= '%s' AND heure_fin >= '%s' AND etat IN ("reservation", "present") GROUP BY consommations.IDactivite, consommations.IDindividu ORDER BY activites.nom, groupes.ordre;""" % (conditionActivites, date, heure, heure) DB.ExecuterReq(req) listeConso = DB.ResultatReq() DB.Close() if len(listeConso) == 0 : return u"" dictTemp = {} listeActivites = [] for IDactivite, nomActivite, IDgroupe, nomGroupe, ordreGroupe, nbreConso, quantite in listeConso : if dictTemp.has_key(IDactivite) == False : dictTemp[IDactivite] = {"nom" : nomActivite, "nbre" : 0, "groupes" : {} } listeActivites.append(IDactivite) if dictTemp[IDactivite]["groupes"].has_key(IDgroupe) == False : dictTemp[IDactivite]["groupes"][IDgroupe] = {"nom" : nomGroupe, "ordre" : ordreGroupe, "nbre" : 0} dictTemp[IDactivite]["groupes"][IDgroupe]["nbre"] += 1 dictTemp[IDactivite]["nbre"] += 1 listeTextes = [] for IDactivite in listeActivites : nomActivite = dictTemp[IDactivite]["nom"] listeGroupes = [] for IDgroupe, dictGroupe in dictTemp[IDactivite]["groupes"].iteritems() : label = u"%d %s" % (dictGroupe["nbre"], dictGroupe["nom"]) listeGroupes.append((dictGroupe["ordre"], label)) listeGroupes.sort() groupes = [] for ordre, label in listeGroupes : groupes.append(label) nbre = dictTemp[IDactivite]["nbre"] if nbre == 1 : temp = _(u"individu") else : temp = _(u"individus") listeTextes.append(_(u"%d %s sur l'activité %s (%s)") % (nbre, temp, nomActivite, self.JoinListe(groupes))) texte = _(u"Il y a actuellement %s") % self.JoinListe(listeTextes) return texte
class TestPanel(wx.Panel): def __init__(self, parent, log): self.log = log wx.Panel.__init__(self, parent, -1) self.ticker = Ticker(self) # Controls for ...controlling... the ticker. self.txt = wx.TextCtrl(self, value="I am a scrolling ticker!!!!", size=(200,-1)) wx.CallAfter(self.txt.SetInsertionPoint, 0) txtl = wx.StaticText(self, label="Ticker text:") fgb = csel.ColourSelect(self, -1, colour=self.ticker.GetForegroundColour()) fgl = wx.StaticText(self, label="Foreground Color:") bgb = csel.ColourSelect(self, -1, colour=self.ticker.GetBackgroundColour()) bgl = wx.StaticText(self, label="Background Color:") fontb = wx.Button(self, label="Change") self.fontl = wx.StaticText(self) dirb = wx.Button(self, label="Switch") self.dirl = wx.StaticText(self) fpsl = wx.StaticText(self, label="Frames per Second:") fps = wx.Slider(self, value=self.ticker.GetFPS(), minValue=1, maxValue=100, size=(150,-1), style=wx.SL_HORIZONTAL|wx.SL_AUTOTICKS|wx.SL_LABELS) fps.SetTickFreq(5) ppfl = wx.StaticText(self, label="Pixels per frame:") ppf = wx.Slider(self, value=self.ticker.GetPPF(), minValue=1, maxValue=10, size=(150,-1), style=wx.SL_HORIZONTAL|wx.SL_AUTOTICKS|wx.SL_LABELS) # Do layout sz = wx.FlexGridSizer(cols=2, hgap=4, vgap=4) sz.Add(txtl, flag=wx.ALIGN_CENTER_VERTICAL) sz.Add(self.txt, flag=wx.ALIGN_CENTER_VERTICAL) sz.Add(fgl, flag=wx.ALIGN_CENTER_VERTICAL) sz.Add(fgb, flag=wx.ALIGN_CENTER_VERTICAL) sz.Add(bgl, flag=wx.ALIGN_CENTER_VERTICAL) sz.Add(bgb, flag=wx.ALIGN_CENTER_VERTICAL) sz.Add(self.fontl, flag=wx.ALIGN_CENTER_VERTICAL) sz.Add(fontb, flag=wx.ALIGN_CENTER_VERTICAL) sz.Add(self.dirl, flag=wx.ALIGN_CENTER_VERTICAL) sz.Add(dirb, flag=wx.ALIGN_CENTER_VERTICAL) sz.Add(fpsl, flag=wx.ALIGN_CENTER_VERTICAL) sz.Add(fps, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT) sz.Add(ppfl, flag=wx.ALIGN_CENTER_VERTICAL) sz.Add(ppf, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT) sz2 = wx.BoxSizer(wx.VERTICAL) sz2.Add(self.ticker, flag=wx.EXPAND|wx.ALL, border=5) sz2.Add(sz, flag=wx.EXPAND|wx.ALL, proportion=1, border=25) self.SetSizer(sz2) sz2.SetSizeHints(self) # Bind events self.Bind(wx.EVT_BUTTON, self.OnChangeTickDirection, dirb) self.Bind(wx.EVT_BUTTON, self.OnChangeTickFont, fontb) self.Bind(wx.EVT_TEXT, self.OnText, self.txt) self.Bind(csel.EVT_COLOURSELECT, self.ChangeTickFGColor, fgb) self.Bind(csel.EVT_COLOURSELECT, self.ChangeTickBGColor, bgb) self.Bind(wx.EVT_SCROLL, self.ChangeFPS, fps) self.Bind(wx.EVT_SCROLL, self.ChangePPF, ppf) # Set defaults self.SetTickDirection("rtl") self.SetTickFont(self.ticker.GetFont()) self.ticker.SetText(self.txt.GetValue()) def SetTickFont(self, font): """Sets ticker font, updates label""" self.ticker.SetFont(font) self.fontl.SetLabel("Font: %s"%(self.ticker.GetFont().GetFaceName())) self.Layout() def OnChangeTickFont(self, evt): fd = wx.FontData() fd.EnableEffects(False) fd.SetInitialFont(self.ticker.GetFont()) dlg = wx.FontDialog(wx.GetTopLevelParent(self), fd) if dlg.ShowModal() == wx.ID_OK: data = dlg.GetFontData() self.SetTickFont(data.GetChosenFont()) def SetTickDirection(self, dir): """Sets tick direction, updates label""" self.ticker.SetDirection(dir) self.dirl.SetLabel("Direction: %s"%(self.ticker.GetDirection())) def OnChangeTickDirection(self, dir): if self.ticker.GetDirection() == "rtl": self.SetTickDirection("ltr") else: self.SetTickDirection("rtl") def OnText(self, evt): """Live update of the ticker text""" self.ticker.SetText(self.txt.GetValue()) def ChangeTickFGColor(self, evt): self.ticker.SetForegroundColour(evt.GetValue()) def ChangeTickBGColor(self, evt): self.ticker.SetBackgroundColour(evt.GetValue()) def ChangeFPS(self, evt): self.ticker.SetFPS(evt.GetPosition()) def ChangePPF(self, evt): self.ticker.SetPPF(evt.GetPosition()) def ShutdownDemo(self): self.ticker.Stop()