alarmserver.alarmserver_qt

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

# Source Code for Module alarmserver.alarmserver_qt

     1  """
     2  Created on 28.10.2013
     3  @author: lehmann
     4
     5  """
     6
     7  from PyQt4.QtGui import QColor
     8  from PyQt4.QtCore import QAbstractTableModel, Qt, QVariant, QModelIndex
     9
    10  from alarmserver import AlarmServer, AlarmNotDefinedError
    11
    12  ALARM_NR = 0
    13  ALARM_TEXT = 1
    14  ALARM_COUNTER = 2
    15
    16 -class AlarmServerModel(QAbstractTableModel, AlarmServer):
    17      """
    18      Model for viewing active alarms.
    19
    20      """
    21
    22 -    def __init__(self):
    23          """
    24          @type alarmserver: AlarmServer
    25          """
    26          AlarmServer.__init__(self)
    27          super(AlarmServerModel, self).__init__()
    28
    29 -    def raise_alarm(self, alarm_nr):
    30          alarm = self.defined_alarms.get(alarm_nr)
    31          if alarm is None:
    32              raise AlarmNotDefinedError(alarm_nr)
    33
    34          if alarm.active:
    35              alarm.counter += 1
    36          else:
    37              alarm_count = len(self.active_alarms)
    38              self.beginInsertRows(QModelIndex(), alarm_count - 1, alarm_count - 1)
    39              AlarmServer.raise_alarm(self, alarm_nr)
    40              self.endInsertRows()
    41
    42 -    def rowCount(self, index=QModelIndex()):
    43          return len(self.active_alarms)
    44
    45 -    def columnCount(self, index=QModelIndex()):
    46          return 3
    47
    48 -    def data(self, index, role=Qt.DisplayRole):
    49          """
    50          @type index: PyQt4.QtCore.QModelIndex
    51          @param role: int
    52
    53          """
    54
    55          if not index.isValid() or \
    56                  not (0 <= index.row() < len(self.active_alarms)):
    57              return QVariant
    58
    59          alarm = self.active_alarms[index.row()]
    60          column = index.column()
    61          if role == Qt.DisplayRole:
    62              if column == ALARM_NR:
    63                  return QVariant(alarm.alarm_nr)
    64              elif column == ALARM_TEXT:
    65                  return QVariant(alarm.text)
    66              elif column == ALARM_COUNTER:
    67                  return QVariant(alarm.counter)
    68          elif role == Qt.TextAlignmentRole:
    69              return QVariant(int(Qt.AlignLeft | Qt.AlignVCenter))
    70          elif role == Qt.TextColorRole:
    71              if alarm.acknowledged:
    72                  return QVariant(QColor(Qt.darkGreen))
    73              elif alarm.active:
    74                  return QVariant(QColor(Qt.red))
    75