alarmserver.qt_alarmserver

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

# Source Code for Module alarmserver.qt_alarmserver

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