today nick learns about the QDataWidgetMapper, which solves all his problems

This commit is contained in:
Nick Foster 2012-07-11 09:15:05 -07:00
parent 855535644f
commit 96db9a9608

View File

@ -66,9 +66,10 @@ class mainwindow(QtGui.QMainWindow):
self.ui.list_aircraft.setModelColumn(0)
self.icaodelegate = ICAOViewDelegate()
self.ui.list_aircraft.setItemDelegate(self.icaodelegate)
#self.ui.list_aircraft.show() #TODO remove
self.dashboard_mapper = QtGui.QDataWidgetMapper()
self.dashboard_mapper.setModel(self.datamodel)
self.dashboard_mapper.addMapping(self.ui.line_ident, 2)
#todo add a custom delegate for the QListView
#goes and gets valid antenna, sample rate options from the device and grays out appropriate things
def populate_source_options(self):
@ -207,23 +208,18 @@ class mainwindow(QtGui.QMainWindow):
#refresh dashboard display with info from clicked aircraft.
#this can either be on-click (hence the name for auto slot) or
#due to a database update.
#this is all due to be replaced by model accesses. do not update.
def on_list_aircraft_clicked(self, index):
icao = long(str(index.data().toString()), 16)
#icao = index.data().toInt()[0]
self.db = sqlite3.connect(self.dbname)
#get ident
q = "select ident from ident where icao=%i" % icao
cursor = self.db.cursor()
cursor.execute(q)
ident = cursor.fetchall()
self.ui.line_ident.clear()
if len(ident) > 0:
self.ui.line_ident.insert(ident[-1][0])
#self.ui.line_ident.clear()
#self.ui.line_ident.insert(index.model().data(index.model().index(index.row(), 2)))
self.dashboard_mapper.setCurrentIndex(index.row())
#get vector info
q = "select seen, speed, heading, vertical from vectors where icao=%i order by seen desc limit 1" % icao
cursor = self.db.cursor()
cursor.execute(q)
r = cursor.fetchall()
speed = "-"
@ -248,17 +244,12 @@ class ICAOViewDelegate(QtGui.QStyledItemDelegate):
last_report = index.model().data(index.model().index(index.row(), 1))
age = (datetime.datetime.now() - datetime.datetime.strptime(str(last_report), "%Y-%m-%d %H:%M:%S")).total_seconds()
#minimum alpha is 0x40 (oldest), max is 0xFF (newest)
alpha = min(abs(0xFF - (0xBF / 600.) * age), 0x40)
alpha = 0xFF - (0xBF / 600.) * min(age, 600)
painter.setPen(QtGui.QColor(0, 0, 0, alpha))
#QtGui.QStyledItemDelegate.paint(self, painter, option, index)
painter.drawText(option.rect.left()+3, option.rect.top(), option.rect.width(), option.rect.height(), option.displayAlignment, paintstr)
#TODO: draw highlight for selection
#eventually: set up a QTimer or whatever and have it self-update and emit dataChanged()
#better yet just have the SQL interface yell and say hey that line changed.
#on selected aircraft or on update for visible aircraft, emit signal to update current dashboard display.
#maybe the datamodel should be responsible for inserting data into the SQL db as well, making this the
#main interface. this could subclass modes_sql for insert functionality. then you get the ability to
#invalidate on update.
class modes_datamodel(QtCore.QAbstractItemModel):
def __init__(self, db):
QtCore.QAbstractItemModel.__init__(self)
@ -274,33 +265,44 @@ class modes_datamodel(QtCore.QAbstractItemModel):
def columnCount(self, parent=QtCore.QModelIndex()):
return 2
#for future use
def flags(self, index):
return QtCore.QAbstractItemModel.flags(self, index)
def data(self, index, role=QtCore.Qt.DisplayRole):
if not index.isValid():
return QtCore.QVariant()
if index.row() >= self.rowCount():
return QtCore.QVariant()
if role != QtCore.Qt.DisplayRole:
if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
#get ICAO of current index
query = QtSql.QSqlQuery()
#TODO: limit this to aircraft seen in last 600 seconds? or make a spinbox for this limit.
icaoquery = "select distinct icao from positions order by icao limit %i,1" % index.row()
query.exec_(icaoquery)
query.next()
icao = query.value(0).toInt()[0]
if index.column() == 0: #ICAO
return "%06x" % icao
elif index.column() == 1: #last seen
seenquery = "select seen from positions where icao = %i order by seen desc limit 1" % icao
query.exec_(seenquery)
query.next()
return query.value(0).toString()
elif index.column() == 2: #ident
identquery = "select ident from ident where icao = %i" % icao
query.exec_(identquery)
if query.next() is False:
return ""
return query.value(0).toString()
elif index.column() == 3: #last position
return QtCore.QVariant()
elif index.column() == 4: #last vector
return QtCore.QVariant()
else:
return QtCore.QVariant()
#TODO eventually find a way to populate the ICAOs with fading according to age
icaoquery = "select distinct icao from positions order by icao limit %i,1" % index.row()
query = QtSql.QSqlQuery()
query.exec_(icaoquery)
query.next()
icao = query.value(0).toInt()[0]
icaostr = "%06x" % icao
seenquery = "select seen from positions where icao = %i order by seen desc limit 1" % icao
query = QtSql.QSqlQuery()
query.exec_(seenquery)
query.next()
seen = query.value(0).toString()
if index.column() == 0:
return icaostr
if index.column() == 1:
return seen
return QtCore.QVariant()
def index(self, row, column, parent=QtCore.QModelIndex()):
return self.createIndex(row, column)