controls/LogViewer.py
branch1.1 Korean release
changeset 1280 72a826dfcfbb
parent 1195 8f8d9859e9fc
child 1441 826730e60407
equal deleted inserted replaced
977:c8e008b8cefe 1280:72a826dfcfbb
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
       
     5 #based on the plcopen standard. 
       
     6 #
       
     7 #Copyright (C) 2013: Edouard TISSERANT and Laurent BESSARD
       
     8 #
       
     9 #See COPYING file for copyrights details.
       
    10 #
       
    11 #This library is free software; you can redistribute it and/or
       
    12 #modify it under the terms of the GNU General Public
       
    13 #License as published by the Free Software Foundation; either
       
    14 #version 2.1 of the License, or (at your option) any later version.
       
    15 #
       
    16 #This library is distributed in the hope that it will be useful,
       
    17 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    19 #General Public License for more details.
       
    20 #
       
    21 #You should have received a copy of the GNU General Public
       
    22 #License along with this library; if not, write to the Free Software
       
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    24 
       
    25 from datetime import datetime
       
    26 from time import time as gettime
       
    27 import numpy
       
    28 
       
    29 import wx
       
    30 
       
    31 from controls.CustomToolTip import CustomToolTip, TOOLTIP_WAIT_PERIOD
       
    32 from editors.DebugViewer import DebugViewer, REFRESH_PERIOD
       
    33 from targets.typemapping import LogLevelsCount, LogLevels
       
    34 from util.BitmapLibrary import GetBitmap
       
    35 
       
    36 THUMB_SIZE_RATIO = 1. / 8.
       
    37 
       
    38 def ArrowPoints(direction, width, height, xoffset, yoffset):
       
    39     if direction == wx.TOP:
       
    40         return [wx.Point(xoffset + 1, yoffset + height - 2),
       
    41                 wx.Point(xoffset + width / 2, yoffset + 1),
       
    42                 wx.Point(xoffset + width - 1, yoffset + height - 2)]
       
    43     else:
       
    44         return [wx.Point(xoffset + 1, yoffset - height + 1),
       
    45                 wx.Point(xoffset + width / 2, yoffset - 2),
       
    46                 wx.Point(xoffset + width - 1, yoffset - height + 1)]
       
    47 
       
    48 class LogScrollBar(wx.Panel):
       
    49     
       
    50     def __init__(self, parent, size):
       
    51         wx.Panel.__init__(self, parent, size=size)
       
    52         self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
       
    53         self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
       
    54         self.Bind(wx.EVT_MOTION, self.OnMotion)
       
    55         self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
       
    56         self.Bind(wx.EVT_PAINT, self.OnPaint)
       
    57         self.Bind(wx.EVT_SIZE, self.OnResize)
       
    58         
       
    59         self.ThumbPosition = 0. # -1 <= ThumbPosition <= 1
       
    60         self.ThumbScrollingStartPos = None
       
    61     
       
    62     def GetRangeRect(self):
       
    63         width, height = self.GetClientSize()
       
    64         return wx.Rect(0, width, width, height - 2 * width)
       
    65     
       
    66     def GetThumbRect(self):
       
    67         width, height = self.GetClientSize()
       
    68         range_rect = self.GetRangeRect()
       
    69         thumb_size = range_rect.height * THUMB_SIZE_RATIO
       
    70         thumb_range = range_rect.height - thumb_size
       
    71         thumb_center_position = (thumb_size + (self.ThumbPosition + 1) * thumb_range) / 2.
       
    72         thumb_start = int(thumb_center_position - thumb_size / 2.)
       
    73         thumb_end = int(thumb_center_position + thumb_size / 2.)
       
    74         return wx.Rect(0, range_rect.y + thumb_start, width, thumb_end - thumb_start)
       
    75     
       
    76     def RefreshThumbPosition(self, thumb_position=None):
       
    77         if thumb_position is None:
       
    78             thumb_position = self.ThumbPosition
       
    79         if self.Parent.IsMessagePanelTop():
       
    80             thumb_position = max(0., thumb_position)
       
    81         if self.Parent.IsMessagePanelBottom():
       
    82             thumb_position = min(0., thumb_position)
       
    83         if thumb_position != self.ThumbPosition:
       
    84             self.ThumbPosition = thumb_position
       
    85             self.Parent.SetScrollSpeed(self.ThumbPosition)
       
    86         self.Refresh()
       
    87     
       
    88     def OnLeftDown(self, event):
       
    89         self.CaptureMouse()
       
    90         posx, posy = event.GetPosition()
       
    91         width, height = self.GetClientSize()
       
    92         range_rect = self.GetRangeRect()
       
    93         thumb_rect = self.GetThumbRect()
       
    94         if range_rect.InsideXY(posx, posy):
       
    95             if thumb_rect.InsideXY(posx, posy):
       
    96                 self.ThumbScrollingStartPos = wx.Point(posx, posy)
       
    97             elif posy < thumb_rect.y:
       
    98                 self.Parent.ScrollToLast()
       
    99             elif posy > thumb_rect.y + thumb_rect.height:
       
   100                 self.Parent.ScrollToFirst()
       
   101         elif posy < width:
       
   102             self.Parent.ScrollMessagePanelByPage(1)
       
   103         elif posy > height - width:
       
   104             self.Parent.ScrollMessagePanelByPage(-1)
       
   105         event.Skip()
       
   106         
       
   107     def OnLeftUp(self, event):
       
   108         self.ThumbScrollingStartPos = None
       
   109         self.RefreshThumbPosition(0.)
       
   110         if self.HasCapture():
       
   111             self.ReleaseMouse()
       
   112         event.Skip()
       
   113         
       
   114     def OnMotion(self, event):
       
   115         if event.Dragging() and self.ThumbScrollingStartPos is not None:
       
   116             posx, posy = event.GetPosition()
       
   117             width, height = self.GetClientSize()
       
   118             range_rect = self.GetRangeRect()
       
   119             thumb_size = range_rect.height * THUMB_SIZE_RATIO
       
   120             thumb_range = range_rect.height - thumb_size
       
   121             self.RefreshThumbPosition(
       
   122                 max(-1., min((posy - self.ThumbScrollingStartPos.y) * 2. / thumb_range, 1.)))
       
   123         event.Skip()
       
   124     
       
   125     def OnResize(self, event):
       
   126         self.Refresh()
       
   127         event.Skip()
       
   128     
       
   129     def OnEraseBackground(self, event):
       
   130         pass
       
   131     
       
   132     def OnPaint(self, event):
       
   133         dc = wx.BufferedPaintDC(self)
       
   134         dc.Clear()
       
   135         dc.BeginDrawing()
       
   136         
       
   137         gc = wx.GCDC(dc)
       
   138         
       
   139         width, height = self.GetClientSize()
       
   140         
       
   141         gc.SetPen(wx.Pen(wx.NamedColour("GREY"), 3))
       
   142         gc.SetBrush(wx.GREY_BRUSH)
       
   143         
       
   144         gc.DrawLines(ArrowPoints(wx.TOP, width * 0.75, width * 0.5, 2, (width + height) / 4 - 3))
       
   145         gc.DrawLines(ArrowPoints(wx.TOP, width * 0.75, width * 0.5, 2, (width + height) / 4 + 3))
       
   146         
       
   147         gc.DrawLines(ArrowPoints(wx.BOTTOM, width * 0.75, width * 0.5, 2, (height * 3 - width) / 4 + 3))
       
   148         gc.DrawLines(ArrowPoints(wx.BOTTOM, width * 0.75, width * 0.5, 2, (height * 3 - width) / 4 - 3))
       
   149         
       
   150         thumb_rect = self.GetThumbRect()
       
   151         exclusion_rect = wx.Rect(thumb_rect.x, thumb_rect.y,
       
   152                                  thumb_rect.width, thumb_rect.height)
       
   153         if self.Parent.IsMessagePanelTop():
       
   154             exclusion_rect.y, exclusion_rect.height = width, exclusion_rect.y + exclusion_rect.height - width
       
   155         if self.Parent.IsMessagePanelBottom():
       
   156             exclusion_rect.height = height - width - exclusion_rect.y
       
   157         if exclusion_rect != thumb_rect:
       
   158             colour = wx.NamedColour("LIGHT GREY")
       
   159             gc.SetPen(wx.Pen(colour))
       
   160             gc.SetBrush(wx.Brush(colour))
       
   161         
       
   162             gc.DrawRectangle(exclusion_rect.x, exclusion_rect.y, 
       
   163                              exclusion_rect.width, exclusion_rect.height)
       
   164         
       
   165         gc.SetPen(wx.GREY_PEN)
       
   166         gc.SetBrush(wx.GREY_BRUSH)
       
   167         
       
   168         gc.DrawPolygon(ArrowPoints(wx.TOP, width, width, 0, 0))
       
   169         
       
   170         gc.DrawPolygon(ArrowPoints(wx.BOTTOM, width, width, 0, height))
       
   171             
       
   172         gc.DrawRectangle(thumb_rect.x, thumb_rect.y, 
       
   173                          thumb_rect.width, thumb_rect.height)
       
   174         
       
   175         dc.EndDrawing()
       
   176         event.Skip()
       
   177 
       
   178 BUTTON_SIZE = (30, 15)
       
   179 
       
   180 class LogButton():
       
   181     
       
   182     def __init__(self, label, callback):
       
   183         self.Position = wx.Point(0, 0)
       
   184         self.Size = wx.Size(*BUTTON_SIZE)
       
   185         self.Label = label
       
   186         self.Shown = True
       
   187         self.Callback = callback
       
   188     
       
   189     def __del__(self):
       
   190         self.callback = None
       
   191     
       
   192     def GetSize(self):
       
   193         return self.Size
       
   194     
       
   195     def SetPosition(self, x, y):
       
   196         self.Position = wx.Point(x, y)
       
   197     
       
   198     def HitTest(self, x, y):
       
   199         rect = wx.Rect(self.Position.x, self.Position.y, 
       
   200                        self.Size.width, self.Size.height)
       
   201         if rect.InsideXY(x, y):
       
   202             return True
       
   203         return False
       
   204     
       
   205     def ProcessCallback(self):
       
   206         if self.Callback is not None:
       
   207             wx.CallAfter(self.Callback)
       
   208             
       
   209     def Draw(self, dc):
       
   210         dc.SetPen(wx.TRANSPARENT_PEN)
       
   211         dc.SetBrush(wx.Brush(wx.NamedColour("LIGHT GREY")))
       
   212         
       
   213         dc.DrawRectangle(self.Position.x, self.Position.y, 
       
   214                          self.Size.width, self.Size.height)
       
   215         
       
   216         w, h = dc.GetTextExtent(self.Label)
       
   217         dc.DrawText(self.Label, 
       
   218             self.Position.x + (self.Size.width - w) / 2, 
       
   219             self.Position.y + (self.Size.height - h) / 2)
       
   220 
       
   221 DATE_INFO_SIZE = 10
       
   222 MESSAGE_INFO_SIZE = 18
       
   223 
       
   224 class LogMessage:
       
   225     
       
   226     def __init__(self, tv_sec, tv_nsec, level, level_bitmap, msg):
       
   227         self.Date = datetime.utcfromtimestamp(tv_sec)
       
   228         self.Seconds = self.Date.second + tv_nsec * 1e-9
       
   229         self.Date = self.Date.replace(second=0)
       
   230         self.Timestamp = tv_sec + tv_nsec * 1e-9
       
   231         self.Level = level
       
   232         self.LevelBitmap = level_bitmap
       
   233         self.Message = msg
       
   234         self.DrawDate = True
       
   235     
       
   236     def __cmp__(self, other):
       
   237         if self.Date == other.Date:
       
   238             return cmp(self.Seconds, other.Seconds)
       
   239         return cmp(self.Date, other.Date)
       
   240     
       
   241     def GetFullText(self):
       
   242         date = self.Date.replace(second=int(self.Seconds))
       
   243         nsec = (self.Seconds % 1.) * 1e9
       
   244         return "%s at %s.%9.9d:\n%s" % (
       
   245             LogLevels[self.Level],
       
   246             str(date), nsec,
       
   247             self.Message)
       
   248     
       
   249     def Draw(self, dc, offset, width, draw_date):
       
   250         if draw_date:
       
   251             datetime_text = self.Date.strftime("%d/%m/%y %H:%M")
       
   252             dw, dh = dc.GetTextExtent(datetime_text)
       
   253             dc.DrawText(datetime_text, (width - dw) / 2, offset + (DATE_INFO_SIZE - dh) / 2)
       
   254             offset += DATE_INFO_SIZE
       
   255         
       
   256         seconds_text = "%12.9f" % self.Seconds
       
   257         sw, sh = dc.GetTextExtent(seconds_text)
       
   258         dc.DrawText(seconds_text, 5, offset + (MESSAGE_INFO_SIZE - sh) / 2)
       
   259         
       
   260         bw, bh = self.LevelBitmap.GetWidth(), self.LevelBitmap.GetHeight()
       
   261         dc.DrawBitmap(self.LevelBitmap, 10 + sw, offset + (MESSAGE_INFO_SIZE - bh) / 2)
       
   262         
       
   263         text = self.Message.replace("\n", " ")
       
   264         mw, mh = dc.GetTextExtent(text)
       
   265         dc.DrawText(text, 15 + sw + bw, offset + (MESSAGE_INFO_SIZE - mh) / 2)
       
   266         
       
   267     def GetHeight(self, draw_date):
       
   268         if draw_date:
       
   269             return DATE_INFO_SIZE + MESSAGE_INFO_SIZE
       
   270         return MESSAGE_INFO_SIZE
       
   271 
       
   272 SECOND = 1
       
   273 MINUTE = 60 * SECOND
       
   274 HOUR = 60 * MINUTE
       
   275 DAY = 24 * HOUR
       
   276 
       
   277 CHANGE_TIMESTAMP_BUTTONS = [(_("1d"), DAY),
       
   278                             (_("1h"), HOUR),
       
   279                             (_("1m"), MINUTE),
       
   280                             (_("1s"), SECOND)]
       
   281 
       
   282 class LogViewer(DebugViewer, wx.Panel):
       
   283     
       
   284     def __init__(self, parent, window):
       
   285         wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER)
       
   286         DebugViewer.__init__(self, None, False, False)
       
   287         
       
   288         main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)
       
   289         main_sizer.AddGrowableCol(0)
       
   290         main_sizer.AddGrowableRow(1)
       
   291         
       
   292         filter_sizer = wx.BoxSizer(wx.HORIZONTAL)
       
   293         main_sizer.AddSizer(filter_sizer, border=5, flag=wx.TOP|wx.LEFT|wx.RIGHT|wx.GROW)
       
   294         
       
   295         self.MessageFilter = wx.ComboBox(self, style=wx.CB_READONLY)
       
   296         self.MessageFilter.Append(_("All"))
       
   297         levels = LogLevels[:3]
       
   298         levels.reverse()
       
   299         for level in levels:
       
   300             self.MessageFilter.Append(_(level))
       
   301         self.Bind(wx.EVT_COMBOBOX, self.OnMessageFilterChanged, self.MessageFilter)
       
   302         filter_sizer.AddWindow(self.MessageFilter, 1, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL)
       
   303         
       
   304         self.SearchMessage = wx.SearchCtrl(self, style=wx.TE_PROCESS_ENTER)
       
   305         self.SearchMessage.ShowSearchButton(True)
       
   306         self.SearchMessage.ShowCancelButton(True)
       
   307         self.Bind(wx.EVT_TEXT_ENTER, self.OnSearchMessageChanged, self.SearchMessage)
       
   308         self.Bind(wx.EVT_SEARCHCTRL_SEARCH_BTN, 
       
   309               self.OnSearchMessageSearchButtonClick, self.SearchMessage)
       
   310         self.Bind(wx.EVT_SEARCHCTRL_CANCEL_BTN, 
       
   311               self.OnSearchMessageCancelButtonClick, self.SearchMessage)
       
   312         filter_sizer.AddWindow(self.SearchMessage, 3, border=5, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL)
       
   313         
       
   314         self.CleanButton = wx.lib.buttons.GenBitmapButton(self, bitmap=GetBitmap("Clean"), 
       
   315               size=wx.Size(28, 28), style=wx.NO_BORDER)
       
   316         self.CleanButton.SetToolTipString(_("Clean log messages"))
       
   317         self.Bind(wx.EVT_BUTTON, self.OnCleanButton, self.CleanButton)
       
   318         filter_sizer.AddWindow(self.CleanButton)
       
   319         
       
   320         message_panel_sizer = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=0)
       
   321         message_panel_sizer.AddGrowableCol(0)
       
   322         message_panel_sizer.AddGrowableRow(0)
       
   323         main_sizer.AddSizer(message_panel_sizer, border=5, flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.GROW)
       
   324         
       
   325         self.MessagePanel = wx.Panel(self)
       
   326         if wx.Platform == '__WXMSW__':
       
   327             self.Font = wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, faceName='Courier New')
       
   328         else:
       
   329             self.Font = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL, faceName='Courier')
       
   330         self.MessagePanel.Bind(wx.EVT_LEFT_UP, self.OnMessagePanelLeftUp)
       
   331         self.MessagePanel.Bind(wx.EVT_RIGHT_UP, self.OnMessagePanelRightUp)
       
   332         self.MessagePanel.Bind(wx.EVT_LEFT_DCLICK, self.OnMessagePanelLeftDCLick)
       
   333         self.MessagePanel.Bind(wx.EVT_MOTION, self.OnMessagePanelMotion)
       
   334         self.MessagePanel.Bind(wx.EVT_LEAVE_WINDOW, self.OnMessagePanelLeaveWindow)
       
   335         self.MessagePanel.Bind(wx.EVT_MOUSEWHEEL, self.OnMessagePanelMouseWheel)
       
   336         self.MessagePanel.Bind(wx.EVT_ERASE_BACKGROUND, self.OnMessagePanelEraseBackground)
       
   337         self.MessagePanel.Bind(wx.EVT_PAINT, self.OnMessagePanelPaint)
       
   338         self.MessagePanel.Bind(wx.EVT_SIZE, self.OnMessagePanelResize)
       
   339         message_panel_sizer.AddWindow(self.MessagePanel, flag=wx.GROW)
       
   340         
       
   341         self.MessageScrollBar = LogScrollBar(self, wx.Size(16, -1))
       
   342         message_panel_sizer.AddWindow(self.MessageScrollBar, flag=wx.GROW)
       
   343         
       
   344         self.SetSizer(main_sizer)
       
   345         
       
   346         self.LeftButtons = []
       
   347         for label, callback in [("+" + text, self.GenerateOnDurationButton(duration)) 
       
   348                                 for text, duration in CHANGE_TIMESTAMP_BUTTONS]:
       
   349             self.LeftButtons.append(LogButton(label, callback))
       
   350         
       
   351         self.RightButtons = []
       
   352         for label, callback in [("-" + text, self.GenerateOnDurationButton(-duration)) 
       
   353                                 for text, duration in CHANGE_TIMESTAMP_BUTTONS]:
       
   354             self.RightButtons.append(LogButton(label, callback))
       
   355         
       
   356         self.MessageFilter.SetSelection(0)
       
   357         self.LogSource = None
       
   358         self.ResetLogMessages()
       
   359         self.ParentWindow = window
       
   360     
       
   361         self.LevelIcons = [GetBitmap("LOG_" + level) for level in LogLevels]
       
   362         self.LevelFilters = [range(i) for i in xrange(4, 0, -1)]
       
   363         self.CurrentFilter = self.LevelFilters[0]
       
   364         self.CurrentSearchValue = ""
       
   365         
       
   366         self.ScrollSpeed = 0.
       
   367         self.LastStartTime = None
       
   368         self.ScrollTimer = wx.Timer(self, -1)
       
   369         self.Bind(wx.EVT_TIMER, self.OnScrollTimer, self.ScrollTimer)
       
   370         
       
   371         self.LastMousePos = None
       
   372         self.MessageToolTip = None
       
   373         self.MessageToolTipTimer = wx.Timer(self, -1)
       
   374         self.Bind(wx.EVT_TIMER, self.OnMessageToolTipTimer, self.MessageToolTipTimer)
       
   375     
       
   376     def __del__(self):
       
   377         self.ScrollTimer.Stop()
       
   378     
       
   379     def ResetLogMessages(self):
       
   380         self.previous_log_count = [None]*LogLevelsCount
       
   381         self.OldestMessages = []
       
   382         self.LogMessages = []
       
   383         self.LogMessagesTimestamp = numpy.array([])
       
   384         self.CurrentMessage = None
       
   385         self.HasNewData = False
       
   386     
       
   387     def SetLogSource(self, log_source):
       
   388         self.LogSource = log_source
       
   389         self.CleanButton.Enable(self.LogSource is not None)
       
   390         if log_source is not None:
       
   391             self.ResetLogMessages()
       
   392             self.RefreshView()
       
   393     
       
   394     def GetLogMessageFromSource(self, msgidx, level):
       
   395         if self.LogSource is not None:
       
   396             answer = self.LogSource.GetLogMessage(level, msgidx)
       
   397             if answer is not None:
       
   398                 msg, tick, tv_sec, tv_nsec = answer
       
   399                 return LogMessage(tv_sec, tv_nsec, level, self.LevelIcons[level], msg)
       
   400         return None
       
   401     
       
   402     def SetLogCounters(self, log_count):
       
   403         new_messages = []
       
   404         for level, count, prev in zip(xrange(LogLevelsCount), log_count, self.previous_log_count):
       
   405             if count is not None and prev != count:
       
   406                 if prev is None:
       
   407                     dump_end = max(-1, count - 10)
       
   408                     oldest_message = (-1, None)
       
   409                 else:
       
   410                     dump_end = prev - 1
       
   411                 for msgidx in xrange(count-1, dump_end,-1):
       
   412                     new_message = self.GetLogMessageFromSource(msgidx, level)
       
   413                     if new_message is None:
       
   414                         if prev is None:
       
   415                             oldest_message = (-1, None)
       
   416                         break
       
   417                     if prev is None:
       
   418                         oldest_message = (msgidx, new_message)
       
   419                         if len(new_messages) == 0:
       
   420                             new_messages = [new_message]
       
   421                         else:
       
   422                             new_messages.insert(0, new_message)
       
   423                     else:
       
   424                         new_messages.insert(0, new_message)
       
   425                 if prev is None and len(self.OldestMessages) <= level:
       
   426                     self.OldestMessages.append(oldest_message)
       
   427                 self.previous_log_count[level] = count
       
   428         new_messages.sort()
       
   429         if len(new_messages) > 0:
       
   430             self.HasNewData = True
       
   431             if self.CurrentMessage is not None:
       
   432                 current_is_last = self.GetNextMessage(self.CurrentMessage)[0] is None
       
   433             else:
       
   434                 current_is_last = True
       
   435             for new_message in new_messages:
       
   436                 self.LogMessages.append(new_message)
       
   437                 self.LogMessagesTimestamp = numpy.append(self.LogMessagesTimestamp, [new_message.Timestamp])
       
   438             if current_is_last:
       
   439                 self.ScrollToLast(False)
       
   440                 self.ResetMessageToolTip()
       
   441                 self.MessageToolTipTimer.Stop()
       
   442                 self.ParentWindow.SelectTab(self)
       
   443             self.NewDataAvailable(None)
       
   444     
       
   445     def FilterLogMessage(self, message, timestamp=None):
       
   446         return (message.Level in self.CurrentFilter and 
       
   447                 message.Message.find(self.CurrentSearchValue) != -1 and
       
   448                 (timestamp is None or message.Timestamp < timestamp))
       
   449     
       
   450     def GetMessageByTimestamp(self, timestamp):
       
   451         if self.CurrentMessage is not None:
       
   452             msgidx = numpy.argmin(abs(self.LogMessagesTimestamp - timestamp))
       
   453             message = self.LogMessages[msgidx]
       
   454             if self.FilterLogMessage(message) and message.Timestamp > timestamp:
       
   455                 return self.GetPreviousMessage(msgidx, timestamp)
       
   456             return message, msgidx
       
   457         return None, None
       
   458     
       
   459     def GetNextMessage(self, msgidx):
       
   460         while msgidx < len(self.LogMessages) - 1:
       
   461             message = self.LogMessages[msgidx + 1]
       
   462             if self.FilterLogMessage(message):
       
   463                 return message, msgidx + 1
       
   464             msgidx += 1
       
   465         return None, None
       
   466     
       
   467     def GetPreviousMessage(self, msgidx, timestamp=None):
       
   468         message = None
       
   469         while 0 < msgidx < len(self.LogMessages):
       
   470             message = self.LogMessages[msgidx - 1]
       
   471             if self.FilterLogMessage(message, timestamp):
       
   472                 return message, msgidx - 1
       
   473             msgidx -= 1
       
   474         if len(self.LogMessages) > 0:
       
   475             message = self.LogMessages[0]
       
   476             for idx, msg in self.OldestMessages:
       
   477                 if msg is not None and msg > message:
       
   478                     message = msg
       
   479             while message is not None:
       
   480                 level = message.Level
       
   481                 oldest_msgidx, oldest_message = self.OldestMessages[level]
       
   482                 if oldest_msgidx > 0:
       
   483                     message = self.GetLogMessageFromSource(oldest_msgidx - 1, level)
       
   484                     if message is not None:
       
   485                         self.OldestMessages[level] = (oldest_msgidx - 1, message)
       
   486                     else:
       
   487                         self.OldestMessages[level] = (-1, None)
       
   488                 else:
       
   489                     message = None
       
   490                     self.OldestMessages[level] = (-1, None)
       
   491                 if message is not None:
       
   492                     message_idx = 0
       
   493                     while (message_idx < len(self.LogMessages) and 
       
   494                            self.LogMessages[message_idx] < message):
       
   495                         message_idx += 1
       
   496                     if len(self.LogMessages) > 0:
       
   497                         current_message = self.LogMessages[self.CurrentMessage]
       
   498                     else:
       
   499                         current_message = message
       
   500                     self.LogMessages.insert(message_idx, message)
       
   501                     self.LogMessagesTimestamp = numpy.insert(
       
   502                             self.LogMessagesTimestamp, 
       
   503                             [message_idx], 
       
   504                             [message.Timestamp])
       
   505                     self.CurrentMessage = self.LogMessages.index(current_message)
       
   506                     if message_idx == 0 and self.FilterLogMessage(message, timestamp):
       
   507                         return message, 0
       
   508                 for idx, msg in self.OldestMessages:
       
   509                     if msg is not None and (message is None or msg > message):
       
   510                         message = msg
       
   511         return None, None
       
   512     
       
   513     def RefreshNewData(self, *args, **kwargs):
       
   514         if self.HasNewData:
       
   515             self.HasNewData = False
       
   516             self.RefreshView()
       
   517         DebugViewer.RefreshNewData(self, *args, **kwargs)
       
   518     
       
   519     def RefreshView(self):
       
   520         width, height = self.MessagePanel.GetClientSize()
       
   521         bitmap = wx.EmptyBitmap(width, height)
       
   522         dc = wx.BufferedDC(wx.ClientDC(self.MessagePanel), bitmap)
       
   523         dc.Clear()
       
   524         dc.BeginDrawing()
       
   525         
       
   526         if self.CurrentMessage is not None:
       
   527             
       
   528             dc.SetFont(self.Font)
       
   529             
       
   530             for button in self.LeftButtons + self.RightButtons:
       
   531                 button.Draw(dc)
       
   532             
       
   533             message_idx = self.CurrentMessage
       
   534             message = self.LogMessages[message_idx]
       
   535             draw_date = True
       
   536             offset = 5
       
   537             while offset < height and message is not None:
       
   538                 message.Draw(dc, offset, width, draw_date)
       
   539                 offset += message.GetHeight(draw_date)
       
   540                 
       
   541                 previous_message, message_idx = self.GetPreviousMessage(message_idx)
       
   542                 if previous_message is not None:
       
   543                     draw_date = message.Date != previous_message.Date
       
   544                 message = previous_message
       
   545         
       
   546         dc.EndDrawing()
       
   547         
       
   548         self.MessageScrollBar.RefreshThumbPosition()
       
   549     
       
   550     def IsMessagePanelTop(self, message_idx=None):
       
   551         if message_idx is None:
       
   552             message_idx = self.CurrentMessage
       
   553         if message_idx is not None:
       
   554             return self.GetNextMessage(message_idx)[0] is None
       
   555         return True
       
   556     
       
   557     def IsMessagePanelBottom(self, message_idx=None):
       
   558         if message_idx is None:
       
   559             message_idx = self.CurrentMessage
       
   560         if message_idx is not None:
       
   561             width, height = self.MessagePanel.GetClientSize()
       
   562             offset = 5
       
   563             message = self.LogMessages[message_idx]
       
   564             draw_date = True
       
   565             while message is not None and offset < height:
       
   566                 offset += message.GetHeight(draw_date)
       
   567                 previous_message, message_idx = self.GetPreviousMessage(message_idx)
       
   568                 if previous_message is not None:
       
   569                     draw_date = message.Date != previous_message.Date
       
   570                 message = previous_message
       
   571             return offset < height
       
   572         return True
       
   573     
       
   574     def ScrollMessagePanel(self, scroll):
       
   575         if self.CurrentMessage is not None:
       
   576             message = self.LogMessages[self.CurrentMessage]
       
   577             while scroll > 0 and message is not None:
       
   578                 message, msgidx = self.GetNextMessage(self.CurrentMessage)
       
   579                 if message is not None:
       
   580                     self.CurrentMessage = msgidx
       
   581                     scroll -= 1
       
   582             while scroll < 0 and message is not None and not self.IsMessagePanelBottom():
       
   583                 message, msgidx = self.GetPreviousMessage(self.CurrentMessage)
       
   584                 if message is not None:
       
   585                     self.CurrentMessage = msgidx
       
   586                     scroll += 1
       
   587             self.RefreshView()
       
   588     
       
   589     def ScrollMessagePanelByPage(self, page):
       
   590         if self.CurrentMessage is not None:
       
   591             width, height = self.MessagePanel.GetClientSize()
       
   592             message_per_page = max(1, (height - DATE_INFO_SIZE) / MESSAGE_INFO_SIZE - 1)
       
   593             self.ScrollMessagePanel(page * message_per_page)
       
   594     
       
   595     def ScrollMessagePanelByTimestamp(self, seconds):
       
   596         if self.CurrentMessage is not None:
       
   597             current_message = self.LogMessages[self.CurrentMessage]
       
   598             message, msgidx = self.GetMessageByTimestamp(current_message.Timestamp + seconds)
       
   599             if message is None or self.IsMessagePanelBottom(msgidx):
       
   600                 self.ScrollToFirst()
       
   601             else:
       
   602                 if seconds > 0 and self.CurrentMessage == msgidx and msgidx < len(self.LogMessages) - 1:
       
   603                     msgidx += 1
       
   604                 self.CurrentMessage = msgidx
       
   605                 self.RefreshView()
       
   606             
       
   607     def ResetMessagePanel(self):
       
   608         if len(self.LogMessages) > 0:
       
   609             self.CurrentMessage = len(self.LogMessages) - 1
       
   610             message = self.LogMessages[self.CurrentMessage]
       
   611             while message is not None and not self.FilterLogMessage(message):
       
   612                 message, self.CurrentMessage = self.GetPreviousMessage(self.CurrentMessage)
       
   613             self.RefreshView()
       
   614     
       
   615     def OnMessageFilterChanged(self, event):
       
   616         self.CurrentFilter = self.LevelFilters[self.MessageFilter.GetSelection()]
       
   617         self.ResetMessagePanel()
       
   618         event.Skip()
       
   619     
       
   620     def OnSearchMessageChanged(self, event):
       
   621         self.CurrentSearchValue = self.SearchMessage.GetValue()
       
   622         self.ResetMessagePanel()
       
   623         event.Skip()
       
   624         
       
   625     def OnSearchMessageSearchButtonClick(self, event):
       
   626         self.CurrentSearchValue = self.SearchMessage.GetValue()
       
   627         self.ResetMessagePanel()
       
   628         event.Skip()
       
   629     
       
   630     def OnSearchMessageCancelButtonClick(self, event):
       
   631         self.CurrentSearchValue = ""
       
   632         self.SearchMessage.SetValue("")
       
   633         self.ResetMessagePanel()
       
   634         event.Skip()
       
   635     
       
   636     def OnCleanButton(self, event):
       
   637         if self.LogSource is not None:
       
   638             self.LogSource.ResetLogCount()
       
   639         self.ResetLogMessages()
       
   640         self.RefreshView()
       
   641         event.Skip()
       
   642     
       
   643     def GenerateOnDurationButton(self, duration):
       
   644         def OnDurationButton():
       
   645             self.ScrollMessagePanelByTimestamp(duration)
       
   646         return OnDurationButton
       
   647     
       
   648     def GetCopyMessageToClipboardFunction(self, message):
       
   649         def CopyMessageToClipboardFunction(event):
       
   650             self.ParentWindow.SetCopyBuffer(message.GetFullText())
       
   651         return CopyMessageToClipboardFunction
       
   652     
       
   653     def GetMessageByScreenPos(self, posx, posy):
       
   654         if self.CurrentMessage is not None:
       
   655             width, height = self.MessagePanel.GetClientSize()
       
   656             message_idx = self.CurrentMessage
       
   657             message = self.LogMessages[message_idx]
       
   658             draw_date = True
       
   659             offset = 5
       
   660             
       
   661             while offset < height and message is not None:
       
   662                 if draw_date:
       
   663                     offset += DATE_INFO_SIZE
       
   664     
       
   665                 if offset <= posy < offset + MESSAGE_INFO_SIZE:
       
   666                     return message
       
   667         
       
   668                 offset += MESSAGE_INFO_SIZE
       
   669                 
       
   670                 previous_message, message_idx = self.GetPreviousMessage(message_idx)
       
   671                 if previous_message is not None:
       
   672                     draw_date = message.Date != previous_message.Date
       
   673                 message = previous_message
       
   674         return None
       
   675     
       
   676     def OnMessagePanelLeftUp(self, event):
       
   677         if self.CurrentMessage is not None:
       
   678             posx, posy = event.GetPosition()
       
   679             for button in self.LeftButtons + self.RightButtons:
       
   680                 if button.HitTest(posx, posy):
       
   681                     button.ProcessCallback()
       
   682                     break
       
   683         event.Skip()
       
   684     
       
   685     def OnMessagePanelRightUp(self, event):
       
   686         message = self.GetMessageByScreenPos(*event.GetPosition())
       
   687         if message is not None:
       
   688             menu = wx.Menu(title='')
       
   689             
       
   690             new_id = wx.NewId()
       
   691             menu.Append(help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Copy"))
       
   692             self.Bind(wx.EVT_MENU, self.GetCopyMessageToClipboardFunction(message), id=new_id)
       
   693     
       
   694             self.MessagePanel.PopupMenu(menu)
       
   695             menu.Destroy()
       
   696         event.Skip()
       
   697     
       
   698     def OnMessagePanelLeftDCLick(self, event):
       
   699         message = self.GetMessageByScreenPos(*event.GetPosition())
       
   700         if message is not None:
       
   701             self.SearchMessage.SetFocus()
       
   702             self.SearchMessage.SetValue(message.Message)
       
   703         event.Skip()
       
   704     
       
   705     def ResetMessageToolTip(self):
       
   706         if self.MessageToolTip is not None:
       
   707             self.MessageToolTip.Destroy()
       
   708             self.MessageToolTip = None
       
   709     
       
   710     def OnMessageToolTipTimer(self, event):
       
   711         if self.LastMousePos is not None:
       
   712             message = self.GetMessageByScreenPos(*self.LastMousePos)
       
   713             if message is not None:
       
   714                 tooltip_pos = self.MessagePanel.ClientToScreen(self.LastMousePos)
       
   715                 tooltip_pos.x += 10
       
   716                 tooltip_pos.y += 10
       
   717                 self.MessageToolTip = CustomToolTip(self.MessagePanel, message.GetFullText(), False)
       
   718                 self.MessageToolTip.SetFont(self.Font)
       
   719                 self.MessageToolTip.SetToolTipPosition(tooltip_pos)
       
   720                 self.MessageToolTip.Show()
       
   721         event.Skip()
       
   722     
       
   723     def OnMessagePanelMotion(self, event):
       
   724         if not event.Dragging():
       
   725             self.ResetMessageToolTip()
       
   726             self.LastMousePos = event.GetPosition()
       
   727             self.MessageToolTipTimer.Start(int(TOOLTIP_WAIT_PERIOD * 1000), oneShot=True)
       
   728         event.Skip()
       
   729         
       
   730     def OnMessagePanelLeaveWindow(self, event):
       
   731         self.ResetMessageToolTip()
       
   732         self.LastMousePos = None
       
   733         self.MessageToolTipTimer.Stop()
       
   734         event.Skip()
       
   735     
       
   736     def OnMessagePanelMouseWheel(self, event):
       
   737         self.ScrollMessagePanel(event.GetWheelRotation() / event.GetWheelDelta())
       
   738         event.Skip()
       
   739     
       
   740     def OnMessagePanelEraseBackground(self, event):
       
   741         pass
       
   742     
       
   743     def OnMessagePanelPaint(self, event):
       
   744         self.RefreshView()
       
   745         event.Skip()
       
   746     
       
   747     def OnMessagePanelResize(self, event):
       
   748         width, height = self.MessagePanel.GetClientSize()
       
   749         offset = 2
       
   750         for button in self.LeftButtons:
       
   751             button.SetPosition(offset, 2)
       
   752             w, h = button.GetSize()
       
   753             offset += w + 2
       
   754         offset = width - 2
       
   755         for button in self.RightButtons:
       
   756             w, h = button.GetSize()
       
   757             button.SetPosition(offset - w, 2)
       
   758             offset -= w + 2
       
   759         if self.IsMessagePanelBottom():
       
   760             self.ScrollToFirst()
       
   761         else:
       
   762             self.RefreshView()
       
   763         event.Skip()
       
   764     
       
   765     def OnScrollTimer(self, event):
       
   766         if self.ScrollSpeed != 0.:
       
   767             speed_norm = abs(self.ScrollSpeed)
       
   768             period = REFRESH_PERIOD / speed_norm
       
   769             self.ScrollMessagePanel(-speed_norm / self.ScrollSpeed)
       
   770             self.LastStartTime = gettime()
       
   771             self.ScrollTimer.Start(int(period * 1000), True)
       
   772         event.Skip()
       
   773     
       
   774     def SetScrollSpeed(self, speed):
       
   775         if speed == 0.:
       
   776             self.ScrollTimer.Stop()
       
   777         else:
       
   778             speed_norm = abs(speed)
       
   779             period = REFRESH_PERIOD / speed_norm
       
   780             current_time = gettime()
       
   781             if self.LastStartTime is not None:
       
   782                 elapsed_time = current_time - self.LastStartTime
       
   783                 if elapsed_time > period:
       
   784                     self.ScrollMessagePanel(-speed_norm / speed)
       
   785                     self.LastStartTime = current_time
       
   786                 else:
       
   787                     period -= elapsed_time
       
   788             else:
       
   789                 self.LastStartTime = current_time
       
   790             self.ScrollTimer.Start(int(period * 1000), True)
       
   791         self.ScrollSpeed = speed    
       
   792     
       
   793     def ScrollToLast(self, refresh=True):
       
   794         if len(self.LogMessages) > 0:
       
   795             self.CurrentMessage = len(self.LogMessages) - 1
       
   796             message = self.LogMessages[self.CurrentMessage]
       
   797             if not self.FilterLogMessage(message):
       
   798                 message, self.CurrentMessage = self.GetPreviousMessage(self.CurrentMessage)
       
   799             if refresh:
       
   800                 self.RefreshView()
       
   801 
       
   802     def ScrollToFirst(self):
       
   803         if len(self.LogMessages) > 0:
       
   804             message_idx = 0
       
   805             message = self.LogMessages[message_idx]
       
   806             if not self.FilterLogMessage(message):
       
   807                 next_message, msgidx = self.GetNextMessage(message_idx)
       
   808                 if next_message is not None:
       
   809                     message_idx = msgidx
       
   810                     message = next_message
       
   811             while message is not None:
       
   812                 message, msgidx = self.GetPreviousMessage(message_idx)
       
   813                 if message is not None:
       
   814                     message_idx = msgidx
       
   815             message = self.LogMessages[message_idx]
       
   816             if self.FilterLogMessage(message):
       
   817                 while message is not None:
       
   818                     message, msgidx = self.GetNextMessage(message_idx)
       
   819                     if message is not None:
       
   820                         if not self.IsMessagePanelBottom(msgidx):
       
   821                             break
       
   822                         message_idx = msgidx
       
   823                 self.CurrentMessage = message_idx
       
   824             else:
       
   825                 self.CurrentMessage = None
       
   826             self.RefreshView()