refactor ProjectDialogController

This commit is contained in:
Johannes Pohl
2017-02-14 11:24:31 +01:00
parent 3e6d92de18
commit deae8a1289

View File

@@ -23,7 +23,7 @@ from urh.util.ProjectManager import ProjectManager
class ProjectDialogController(QDialog):
class ProtocolParticipantModel(QAbstractTableModel):
parti_rssi_edited = pyqtSignal()
participant_rssi_edited = pyqtSignal()
def __init__(self, participants):
super().__init__()
@@ -34,10 +34,10 @@ class ProjectDialogController(QDialog):
self.beginResetModel()
self.endResetModel()
def columnCount(self, QModelIndex_parent=None, *args, **kwargs):
def columnCount(self, parent: QModelIndex=None, *args, **kwargs):
return len(self.header_labels)
def rowCount(self, QModelIndex_parent=None, *args, **kwargs):
def rowCount(self, parent: QModelIndex=None, *args, **kwargs):
return len(self.participants)
def headerData(self, section, orientation, role=Qt.DisplayRole):
@@ -67,23 +67,23 @@ class ProjectDialogController(QDialog):
if i >= len(self.participants):
return False
parti = self.participants[i]
participant = self.participants[i]
if j == 0:
parti.name = value
participant.name = value
elif j == 1:
parti.shortname = value
participant.shortname = value
elif j == 2:
parti.color_index = int(value)
participant.color_index = int(value)
elif j == 3:
for other in self.participants:
if other.relative_rssi == int(value):
other.relative_rssi = parti.relative_rssi
other.relative_rssi = participant.relative_rssi
break
parti.relative_rssi = int(value)
self.parti_rssi_edited.emit()
participant.relative_rssi = int(value)
self.participant_rssi_edited.emit()
elif j == 4:
parti.address_hex = value
participant.address_hex = value
return True
@@ -102,9 +102,9 @@ class ProjectDialogController(QDialog):
self.ui.setupUi(self)
if new_project:
self.parti_table_model = self.ProtocolParticipantModel([])
self.participant_table_model = self.ProtocolParticipantModel([])
else:
self.parti_table_model = self.ProtocolParticipantModel(project_manager.participants)
self.participant_table_model = self.ProtocolParticipantModel(project_manager.participants)
self.ui.spinBoxSampleRate.setValue(project_manager.sample_rate)
self.ui.spinBoxFreq.setValue(project_manager.frequency)
@@ -120,7 +120,7 @@ class ProjectDialogController(QDialog):
self.ui.lNewProject.setText("Edit project")
self.ui.btnOK.setText("Accept")
self.ui.tblParticipants.setModel(self.parti_table_model)
self.ui.tblParticipants.setModel(self.participant_table_model)
self.ui.tblParticipants.setItemDelegateForColumn(2, ComboBoxDelegate([""] * len(constants.PARTICIPANT_COLORS),
colors=constants.PARTICIPANT_COLORS,
parent=self))
@@ -152,7 +152,7 @@ class ProjectDialogController(QDialog):
if new_project:
self.ui.lineEdit_Path.setText(os.path.realpath(os.path.join(os.curdir, "new")))
self.on_path_edited()
self.on_line_edit_path_text_edited()
self.open_editors()
@@ -168,7 +168,7 @@ class ProjectDialogController(QDialog):
items[-1] += " (high)"
for row in range(len(self.participants)):
self.ui.tblParticipants.closePersistentEditor(self.parti_table_model.index(row, 3))
self.ui.tblParticipants.closePersistentEditor(self.participant_table_model.index(row, 3))
self.ui.tblParticipants.setItemDelegateForColumn(3, ComboBoxDelegate(items, parent=self))
@@ -182,44 +182,64 @@ class ProjectDialogController(QDialog):
:rtype: list of Participant
"""
return self.parti_table_model.participants
return self.participant_table_model.participants
def create_connects(self):
self.ui.spinBoxFreq.valueChanged.connect(self.on_frequency_changed)
self.ui.spinBoxSampleRate.valueChanged.connect(self.on_sample_rate_changed)
self.ui.spinBoxBandwidth.valueChanged.connect(self.on_bandwidth_changed)
self.ui.spinBoxGain.valueChanged.connect(self.on_gain_changed)
self.ui.txtEdDescription.textChanged.connect(self.on_description_changed)
self.ui.lineEditBroadcastAddress.textEdited.connect(self.on_broadcast_address_edited)
self.ui.spinBoxFreq.valueChanged.connect(self.on_spin_box_frequency_value_changed)
self.ui.spinBoxSampleRate.valueChanged.connect(self.on_spin_box_sample_rate_value_changed)
self.ui.spinBoxBandwidth.valueChanged.connect(self.on_spin_box_bandwidth_value_changed)
self.ui.spinBoxGain.valueChanged.connect(self.on_spin_box_gain_value_changed)
self.ui.txtEdDescription.textChanged.connect(self.on_txt_edit_description_text_changed)
self.ui.lineEditBroadcastAddress.textEdited.connect(self.on_line_edit_broadcast_address_text_edited)
self.ui.btnAddParticipant.clicked.connect(self.on_btn_add_participant_clicked)
self.ui.btnRemoveParticipant.clicked.connect(self.on_btn_remove_participant_clicked)
self.ui.lineEdit_Path.textEdited.connect(self.on_path_edited)
self.ui.lineEdit_Path.textEdited.connect(self.on_line_edit_path_text_edited)
self.ui.btnOK.clicked.connect(self.on_button_ok_clicked)
self.ui.btnSelectPath.clicked.connect(self.on_btn_select_path_clicked)
self.ui.lOpenSpectrumAnalyzer.linkActivated.connect(self.on_spectrum_analyzer_link_activated)
self.parti_table_model.parti_rssi_edited.connect(self.__on_relative_rssi_edited)
self.participant_table_model.participant_rssi_edited.connect(self.__on_relative_rssi_edited)
def on_sample_rate_changed(self):
self.sample_rate = self.ui.spinBoxSampleRate.value()
def set_path(self, path):
self.path = path
self.ui.lineEdit_Path.setText(self.path)
name = os.path.basename(os.path.normpath(self.path))
self.ui.lblName.setText(name)
def on_frequency_changed(self):
self.freq = self.ui.spinBoxFreq.value()
self.ui.lblNewPath.setVisible(not os.path.isdir(self.path))
def on_bandwidth_changed(self):
self.bandwidth = self.ui.spinBoxBandwidth.value()
def open_editors(self):
for row in range(len(self.participants)):
self.ui.tblParticipants.openPersistentEditor(self.participant_table_model.index(row, 2))
self.ui.tblParticipants.openPersistentEditor(self.participant_table_model.index(row, 3))
def on_gain_changed(self):
self.gain = self.ui.spinBoxGain.value()
@pyqtSlot(float)
def on_spin_box_sample_rate_value_changed(self, value: float):
self.sample_rate = value
def on_path_edited(self):
@pyqtSlot(float)
def on_spin_box_frequency_value_changed(self, value: float):
self.freq = value
@pyqtSlot(float)
def on_spin_box_bandwidth_value_changed(self, value: float):
self.bandwidth = value
@pyqtSlot(int)
def on_spin_box_gain_value_changed(self, value: int):
self.gain = value
@pyqtSlot()
def on_line_edit_path_text_edited(self):
self.set_path(self.ui.lineEdit_Path.text())
def on_description_changed(self):
@pyqtSlot()
def on_txt_edit_description_text_changed(self):
self.description = self.ui.txtEdDescription.toPlainText()
@pyqtSlot()
def on_button_ok_clicked(self):
self.path = os.path.realpath(self.path)
if not os.path.exists(self.path):
@@ -236,21 +256,9 @@ class ProjectDialogController(QDialog):
self.committed = True
self.close()
def on_broadcast_address_edited(self):
self.broadcast_address_hex = self.ui.lineEditBroadcastAddress.text()
def set_path(self, path):
self.path = path
self.ui.lineEdit_Path.setText(self.path)
name = os.path.basename(os.path.normpath(self.path))
self.ui.lblName.setText(name)
self.ui.lblNewPath.setVisible(not os.path.isdir(self.path))
def open_editors(self):
for row in range(len(self.participants)):
self.ui.tblParticipants.openPersistentEditor(self.parti_table_model.index(row, 2))
self.ui.tblParticipants.openPersistentEditor(self.parti_table_model.index(row, 3))
@pyqtSlot(str)
def on_line_edit_broadcast_address_text_edited(self, value: str):
self.broadcast_address_hex = value
@pyqtSlot()
def on_btn_select_path_clicked(self):
@@ -289,12 +297,12 @@ class ProjectDialogController(QDialog):
else:
color_index = random.choice(range(len(constants.PARTICIPANT_COLORS)))
nchars = 0
num_chars = 0
participant = None
while participant is None:
nchars += 1
num_chars += 1
for c in string.ascii_uppercase:
shortname = nchars * str(c)
shortname = num_chars * str(c)
if shortname not in used_shortnames:
participant = Participant("Device " + shortname, shortname=shortname, color_index=color_index)
break
@@ -302,7 +310,7 @@ class ProjectDialogController(QDialog):
self.participants.append(participant)
participant.relative_rssi = len(self.participants) - 1
self.__set_relative_rssi_delegate()
self.parti_table_model.update()
self.participant_table_model.update()
self.ui.btnRemoveParticipant.setEnabled(True)
self.open_editors()
@@ -322,11 +330,11 @@ class ProjectDialogController(QDialog):
start += 1
del self.participants[start:end + 1]
nremoved = (end + 1) - start
for parti in self.participants:
if parti.relative_rssi > len(self.participants) - 1:
parti.relative_rssi -= nremoved
num_removed = (end + 1) - start
for participant in self.participants:
if participant.relative_rssi > len(self.participants) - 1:
participant.relative_rssi -= num_removed
self.__set_relative_rssi_delegate()
self.parti_table_model.update()
self.participant_table_model.update()
self.ui.btnRemoveParticipant.setDisabled(len(self.participants) <= 1)
self.open_editors()
self.open_editors()