alarmserver.gui

Package alarmserver :: Module gui |
[hide private]
[frames] | no frames]

# Source Code for Module alarmserver.gui

      1  #-*- coding: utf-8 -*-
      2  """
      3  Created on 28.10.2013
      4  @author: lehmann
      5
      6  """
      7
      8  from PyQt4.QtGui import QColor
      9  from PyQt4.QtCore import QAbstractTableModel, Qt, QVariant, QModelIndex, SIGNAL
     10
     11  from alarmserver import AlarmServer, AlarmNotDefinedError
     12
     13  ALARM_NR = 0
     14  ALARM_TEXT = 1
     15  ALARM_COUNTER = 2
     16  ALARM_TIME_COMING = 3
     17  ALARM_TIME_GOING = 4
     18
     19  COLUMN_COUNT = 5
     20  TIME_PATTERN = "%H:%M:%S"
     21
     22 -class AlarmServerModel(QAbstractTableModel, AlarmServer):
     23      """
     24      This class allows the user access to the alarm server via
     25      the model/view mechanism of the Qt framework.
     26
     27      """
     28
     29 -    def __init__(self):
     30          AlarmServer.__init__(self)
     31          super(AlarmServerModel, self).__init__()
     32
     33 -    def _dataChanged_signal(self, alarm_nr):
     34          alarm = self.defined_alarms.get(alarm_nr)
     35
     36          if alarm is None:
     37              raise AlarmNotDefinedError(alarm_nr)
     38
     39          if alarm in self.current_alarms:
     40              row = self.current_alarms.index(alarm)
     41              indexTopLeft = self.index(row, 0, QModelIndex())
     42              indexBottomRight = self.index(row, COLUMN_COUNT - 1, QModelIndex())
     43              self.emit(SIGNAL("dataChanged(QModelIndex, QModelIndex)"), indexTopLeft, indexBottomRight)
     44
     45 -    def acknowledge(self, alarm_nr):
     46          AlarmServer.acknowledge(self, alarm_nr)
     47          self._dataChanged_signal(alarm_nr)
     48
     49 -    def acknowledge_all(self):
     50          self.beginResetModel()
     51          AlarmServer.acknowledge_all(self)
     52          self.endResetModel()
     53
     54 -    def alarm_coming(self, alarm_nr):
     55          alarm = self.defined_alarms.get(alarm_nr)
     56          if alarm is None:
     57              raise AlarmNotDefinedError(alarm_nr)
     58
     59          if alarm in self.current_alarms:
     60              AlarmServer.alarm_coming(self, alarm_nr)
     61              row = self.current_alarms.index(alarm)
     62              indexTopLeft = self.index(row, 0, QModelIndex())
     63              indexBottomRight = self.index(row, COLUMN_COUNT - 1, QModelIndex())
     64
     65              self.emit(SIGNAL("dataChanged(QModelIndex, QModelIndex)"), indexTopLeft, indexBottomRight)
     66          else:
     67              alarm_count = len(self.current_alarms)
     68              self.beginInsertRows(QModelIndex(), alarm_count - 1, alarm_count - 1)
     69              AlarmServer.alarm_coming(self, alarm_nr)
     70              self.endInsertRows()
     71
     72 -    def alarm_going(self, alarm_nr):
     73          AlarmServer.alarm_going(self, alarm_nr)
     74          self._dataChanged_signal(alarm_nr)
     75
     76 -    def clear(self, alarm_nr):
     77          alarm = self.defined_alarms.get(alarm_nr)
     78          if alarm is None:
     79              raise AlarmNotDefinedError(alarm_nr)
     80
     81          if alarm in self.current_alarms:
     82              row = self.current_alarms.index(alarm)
     83              self.beginRemoveRows(QModelIndex(), row, row)
     84              AlarmServer.clear(self, alarm_nr)
     85              self.endRemoveRows()
     86
     87 -    def clear_all(self):
     88          self.beginResetModel()
     89          AlarmServer.clear_all(self)
     90          self.endResetModel()
     91
     92 -    def columnCount(self, index=QModelIndex()):
     93          return COLUMN_COUNT
     94
     95 -    def data(self, index, role=Qt.DisplayRole):
     96          """
     97          @type index: Gui.QtCore.QModelIndex
     98          @param role: int
     99
    100          """
    101
    102          if not index.isValid() or \
    103                  not (0 <= index.row() < len(self.current_alarms)):
    104              return QVariant
    105
    106          alarm = self.current_alarms[index.row()]
    107          column = index.column()
    108          if role == Qt.DisplayRole:
    109              if column == ALARM_NR:
    110                  return QVariant(alarm.alarm_nr)
    111              elif column == ALARM_TEXT:
    112                  return QVariant(alarm.text)
    113              elif column == ALARM_COUNTER:
    114                  return QVariant(alarm.counter)
    115              elif column == ALARM_TIME_COMING:
    116                  return QVariant(alarm.time_coming.strftime(TIME_PATTERN))
    117              elif column == ALARM_TIME_GOING:
    118                  time_going = alarm.time_going
    119                  if time_going is None:
    120                      return QVariant()
    121                  return QVariant(alarm.time_going.strftime(TIME_PATTERN))
    122          elif role == Qt.TextAlignmentRole:
    123              return QVariant(int(Qt.AlignLeft | Qt.AlignVCenter))
    124          elif role == Qt.TextColorRole:
    125              if alarm.is_acknowledged:
    126                  return QVariant(QColor(Qt.darkGreen))
    127              elif alarm.is_active:
    128                  return QVariant(QColor(Qt.red))
    129
    130 -    def headerData(self, section, orientation, role=Qt.DisplayRole):
    131          if role == Qt.TextAlignmentRole:
    132              return QVariant(int(Qt.AlignLeft | Qt.AlignVCenter))
    133          if role == Qt.DisplayRole:
    134              if orientation == Qt.Horizontal:
    135                  if section == ALARM_NR:
    136                      return "id"
    137                  elif section == ALARM_TEXT:
    138                      return "message"
    139                  elif section == ALARM_COUNTER:
    140                      return "counter"
    141                  elif section == ALARM_TIME_COMING:
    142                      return "time coming"
    143                  elif section == ALARM_TIME_GOING:
    144                      return "time going"
    145                  else:
    146                      return QVariant()
    147              else:
    148                  return QVariant(section + 1)
    149          return QVariant()
    150
    151 -    def rowCount(self, index=QModelIndex()):
    152          return len(self.current_alarms)
    153