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.ui.list_aircraft.setModelColumn(0)
self.icaodelegate = ICAOViewDelegate() self.icaodelegate = ICAOViewDelegate()
self.ui.list_aircraft.setItemDelegate(self.icaodelegate) 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 #goes and gets valid antenna, sample rate options from the device and grays out appropriate things
def populate_source_options(self): def populate_source_options(self):
@ -207,23 +208,18 @@ class mainwindow(QtGui.QMainWindow):
#refresh dashboard display with info from clicked aircraft. #refresh dashboard display with info from clicked aircraft.
#this can either be on-click (hence the name for auto slot) or #this can either be on-click (hence the name for auto slot) or
#due to a database update. #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): def on_list_aircraft_clicked(self, index):
icao = long(str(index.data().toString()), 16) icao = long(str(index.data().toString()), 16)
#icao = index.data().toInt()[0] #icao = index.data().toInt()[0]
self.db = sqlite3.connect(self.dbname) self.db = sqlite3.connect(self.dbname)
#get ident #self.ui.line_ident.clear()
q = "select ident from ident where icao=%i" % icao #self.ui.line_ident.insert(index.model().data(index.model().index(index.row(), 2)))
cursor = self.db.cursor() self.dashboard_mapper.setCurrentIndex(index.row())
cursor.execute(q)
ident = cursor.fetchall()
self.ui.line_ident.clear()
if len(ident) > 0:
self.ui.line_ident.insert(ident[-1][0])
#get vector info #get vector info
q = "select seen, speed, heading, vertical from vectors where icao=%i order by seen desc limit 1" % icao 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) cursor.execute(q)
r = cursor.fetchall() r = cursor.fetchall()
speed = "-" speed = "-"
@ -248,17 +244,12 @@ class ICAOViewDelegate(QtGui.QStyledItemDelegate):
last_report = index.model().data(index.model().index(index.row(), 1)) 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() 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) #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)) painter.setPen(QtGui.QColor(0, 0, 0, alpha))
#QtGui.QStyledItemDelegate.paint(self, painter, option, index) #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) 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): class modes_datamodel(QtCore.QAbstractItemModel):
def __init__(self, db): def __init__(self, db):
QtCore.QAbstractItemModel.__init__(self) QtCore.QAbstractItemModel.__init__(self)
@ -274,31 +265,42 @@ class modes_datamodel(QtCore.QAbstractItemModel):
def columnCount(self, parent=QtCore.QModelIndex()): def columnCount(self, parent=QtCore.QModelIndex()):
return 2 return 2
#for future use
def flags(self, index):
return QtCore.QAbstractItemModel.flags(self, index)
def data(self, index, role=QtCore.Qt.DisplayRole): def data(self, index, role=QtCore.Qt.DisplayRole):
if not index.isValid(): if not index.isValid():
return QtCore.QVariant() return QtCore.QVariant()
if index.row() >= self.rowCount(): if index.row() >= self.rowCount():
return QtCore.QVariant() return QtCore.QVariant()
if role != QtCore.Qt.DisplayRole: if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
return QtCore.QVariant() #get ICAO of current index
#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 = 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.exec_(icaoquery)
query.next() query.next()
icao = query.value(0).toInt()[0] icao = query.value(0).toInt()[0]
icaostr = "%06x" % icao 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 seenquery = "select seen from positions where icao = %i order by seen desc limit 1" % icao
query = QtSql.QSqlQuery()
query.exec_(seenquery) query.exec_(seenquery)
query.next() query.next()
seen = query.value(0).toString() return query.value(0).toString()
if index.column() == 0: elif index.column() == 2: #ident
return icaostr identquery = "select ident from ident where icao = %i" % icao
if index.column() == 1: query.exec_(identquery)
return seen 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() return QtCore.QVariant()
def index(self, row, column, parent=QtCore.QModelIndex()): def index(self, row, column, parent=QtCore.QModelIndex()):