| 1 | #include "SymbolManager.h" |
| 2 | #include "SymbolTable.h" |
| 3 | #include "Settings.h" |
| 4 | #include "Convert.h" |
| 5 | #include <QComboBox> |
| 6 | #include <QFileDialog> |
| 7 | #include <QMessageBox> |
| 8 | #include <QHeaderView> |
| 9 | |
| 10 | SymbolManager::SymbolManager(SymbolTable& symtable, QWidget* parent) |
| 11 | : QDialog(parent), symTable(symtable) |
| 12 | { |
| 13 | setupUi(this); |
| 14 | |
| 15 | // restore layout |
| 16 | Settings& s = Settings::get(); |
| 17 | restoreGeometry(s.value("SymbolManager/WindowGeometry", saveGeometry()).toByteArray()); |
| 18 | treeFiles->header()->restoreState(s.value("SymbolManager/HeaderFiles", |
| 19 | treeFiles->header()->saveState()).toByteArray()); |
| 20 | treeLabels->header()->restoreState(s.value("SymbolManager/HeaderSymbols", |
| 21 | treeLabels->header()->saveState()).toByteArray()); |
| 22 | |
| 23 | treeLabelsUpdateCount = 0; |
| 24 | editColumn = -1; |
| 25 | |
| 26 | // put slot checkboxes in a convenience array |
| 27 | chkSlots[ 0] = chk00; chkSlots[ 1] = chk01; chkSlots[ 2] = chk02; chkSlots[ 3] = chk03; |
| 28 | chkSlots[ 4] = chk10; chkSlots[ 5] = chk11; chkSlots[ 6] = chk12; chkSlots[ 7] = chk13; |
| 29 | chkSlots[ 8] = chk20; chkSlots[ 9] = chk21; chkSlots[10] = chk22; chkSlots[11] = chk23; |
| 30 | chkSlots[12] = chk30; chkSlots[13] = chk31; chkSlots[14] = chk32; chkSlots[15] = chk33; |
| 31 | chkRegs[ 0] = chkRegA; chkRegs[ 1] = chkRegB; chkRegs[ 2] = chkRegC; chkRegs[ 3] = chkRegD; |
| 32 | chkRegs[ 4] = chkRegE; chkRegs[ 5] = chkRegH; chkRegs[ 6] = chkRegL; chkRegs[ 7] = chkRegBC; |
| 33 | chkRegs[ 8] = chkRegDE; chkRegs[ 9] = chkRegHL; chkRegs[10] = chkRegIX; chkRegs[11] = chkRegIY; |
| 34 | chkRegs[12] = chkRegIXL;chkRegs[13] = chkRegIXH;chkRegs[14] = chkRegIYL;chkRegs[15] = chkRegIYH; |
| 35 | chkRegs[16] = chkRegOffset; chkRegs[17] = chkRegI; |
| 36 | |
| 37 | connect(treeFiles, &QTreeWidget::itemSelectionChanged, this, &SymbolManager::fileSelectionChange); |
| 38 | connect(btnAddFile, &QPushButton::clicked, this, &SymbolManager::addFile); |
| 39 | connect(btnRemoveFile, &QPushButton::clicked, this, &SymbolManager::removeFile); |
| 40 | connect(btnReloadFiles, &QPushButton::clicked, this, &SymbolManager::reloadFiles); |
| 41 | connect(treeLabels, &QTreeWidget::itemSelectionChanged, this, &SymbolManager::labelSelectionChanged); |
| 42 | connect(treeLabels, &QTreeWidget::itemDoubleClicked, this, &SymbolManager::labelEdit); |
| 43 | connect(treeLabels, &QTreeWidget::itemChanged, this, &SymbolManager::labelChanged); |
| 44 | connect(btnAddSymbol, &QPushButton::clicked, this, &SymbolManager::addLabel); |
| 45 | connect(btnRemoveSymbol, &QPushButton::clicked, this, &SymbolManager::removeLabel); |
| 46 | connect(radJump, &QRadioButton::toggled, this, &SymbolManager::changeType); |
| 47 | connect(radVar, &QRadioButton::toggled, this, &SymbolManager::changeType); |
| 48 | connect(radValue, &QRadioButton::toggled, this, &SymbolManager::changeType); |
| 49 | connect(chk00, &QCheckBox::stateChanged, this, &SymbolManager::changeSlot00); |
| 50 | connect(chk01, &QCheckBox::stateChanged, this, &SymbolManager::changeSlot01); |
| 51 | connect(chk02, &QCheckBox::stateChanged, this, &SymbolManager::changeSlot02); |
| 52 | connect(chk03, &QCheckBox::stateChanged, this, &SymbolManager::changeSlot03); |
| 53 | connect(chk10, &QCheckBox::stateChanged, this, &SymbolManager::changeSlot10); |
| 54 | connect(chk11, &QCheckBox::stateChanged, this, &SymbolManager::changeSlot11); |
| 55 | connect(chk12, &QCheckBox::stateChanged, this, &SymbolManager::changeSlot12); |
| 56 | connect(chk13, &QCheckBox::stateChanged, this, &SymbolManager::changeSlot13); |
| 57 | connect(chk20, &QCheckBox::stateChanged, this, &SymbolManager::changeSlot20); |
| 58 | connect(chk21, &QCheckBox::stateChanged, this, &SymbolManager::changeSlot21); |
| 59 | connect(chk22, &QCheckBox::stateChanged, this, &SymbolManager::changeSlot22); |
| 60 | connect(chk23, &QCheckBox::stateChanged, this, &SymbolManager::changeSlot23); |
| 61 | connect(chk30, &QCheckBox::stateChanged, this, &SymbolManager::changeSlot30); |
| 62 | connect(chk31, &QCheckBox::stateChanged, this, &SymbolManager::changeSlot31); |
| 63 | connect(chk32, &QCheckBox::stateChanged, this, &SymbolManager::changeSlot32); |
| 64 | connect(chk33, &QCheckBox::stateChanged, this, &SymbolManager::changeSlot33); |
| 65 | connect(chkRegA, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterA); |
| 66 | connect(chkRegB, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterB); |
| 67 | connect(chkRegC, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterC); |
| 68 | connect(chkRegD, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterD); |
| 69 | connect(chkRegE, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterE); |
| 70 | connect(chkRegH, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterH); |
| 71 | connect(chkRegL, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterL); |
| 72 | connect(chkRegBC, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterBC); |
| 73 | connect(chkRegDE, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterDE); |
| 74 | connect(chkRegHL, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterHL); |
| 75 | connect(chkRegIX, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterIX); |
| 76 | connect(chkRegIY, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterIY); |
| 77 | connect(chkRegIXL, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterIXL); |
| 78 | connect(chkRegIXH, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterIXH); |
| 79 | connect(chkRegIYL, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterIYL); |
| 80 | connect(chkRegIYH, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterIYH); |
| 81 | connect(chkRegOffset, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterOffset); |
| 82 | connect(chkRegI, &QCheckBox::stateChanged, this, &SymbolManager::changeRegisterI); |
| 83 | |
| 84 | groupSlots->setEnabled(false); |
| 85 | groupSegments->setEnabled(false); |
| 86 | btnRemoveFile->setEnabled(false); |
| 87 | btnRemoveSymbol->setEnabled(false); |
| 88 | |
| 89 | initFileList(); |
| 90 | initSymbolList(); |
| 91 | } |
| 92 | |
| 93 | void SymbolManager::closeEvent(QCloseEvent* e) |
| 94 | { |
| 95 | // store layout |
| 96 | Settings& s = Settings::get(); |
| 97 | s.setValue("SymbolManager/WindowGeometry", saveGeometry()); |
| 98 | s.setValue("SymbolManager/HeaderFiles", treeFiles->header()->saveState()); |
| 99 | s.setValue("SymbolManager/HeaderSymbols", treeLabels->header()->saveState()); |
| 100 | QDialog::closeEvent(e); |
| 101 | } |
| 102 | |
| 103 | void SymbolManager::refresh() |
| 104 | { |
| 105 | initSymbolList(); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * File list support functions |
| 110 | */ |
| 111 | |
| 112 | enum TableColumns { |
| 113 | FILENAME = 0, |
| 114 | DESTINATION_SLOT, |
| 115 | LAST_REFRESH, |
| 116 | }; |
| 117 | |
| 118 | void SymbolManager::initFileList() |
| 119 | { |
| 120 | treeFiles->clear(); |
| 121 | for (int i = 0; i < symTable.symbolFilesSize(); ++i) { |
| 122 | auto* item = new QTreeWidgetItem(treeFiles); |
| 123 | QFileInfo info(symTable.symbolFile(i)); |
| 124 | item->setText(FILENAME, info.fileName()); |
| 125 | item->setToolTip(FILENAME, symTable.symbolFile(i)); |
| 126 | auto fmt = QLocale().dateTimeFormat(QLocale::ShortFormat); |
| 127 | createComboBox(i); |
| 128 | item->setText(LAST_REFRESH, symTable.symbolFileRefresh(i).toString(fmt)); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | static const char* destinationLabels[] = { |
| 133 | "All", |
| 134 | "0-0", "0-1", "0-2", "0-3", |
| 135 | "1-0", "1-1", "1-2", "1-3", |
| 136 | "2-0", "2-1", "2-2", "2-3", |
| 137 | "3-0", "3-1", "3-2", "3-3", |
| 138 | }; |
| 139 | |
| 140 | void SymbolManager::createComboBox(int row) |
| 141 | { |
| 142 | auto* combo = new QComboBox(); |
| 143 | combo->setEditable(false); |
| 144 | combo->insertItems(0, QStringList(std::begin(destinationLabels), std::end(destinationLabels))); |
| 145 | combo->setCurrentIndex(0); |
| 146 | |
| 147 | auto* model = treeFiles->model(); |
| 148 | auto index = model->index(row, DESTINATION_SLOT); |
| 149 | |
| 150 | treeFiles->setIndexWidget(index, combo); |
| 151 | |
| 152 | connect(combo, qOverload<int>(&QComboBox::currentIndexChanged), |
| 153 | [this, row](int index){ addSymbolFileDestination(row, index - 1); }); |
| 154 | } |
| 155 | |
| 156 | void SymbolManager::addFile() |
| 157 | { |
| 158 | // create dialog |
| 159 | auto* d = new QFileDialog(this); |
| 160 | QStringList types; |
| 161 | types << "All supported files (*.omds *.sym *.map *.noi *.symbol *.publics *.sys)" |
| 162 | << "OpenMSX Debugger session files (*.omds)" |
| 163 | << "tniASM 0.x symbol files (*.sym)" |
| 164 | << "tniASM 1.x symbol files (*.sym)" |
| 165 | << "asMSX 0.x symbol files (*.sym)" |
| 166 | << "HiTech C symbol files (*.sym)" |
| 167 | << "HiTech C link map files (*.map)" |
| 168 | << "NoICE command files (*.noi)" |
| 169 | << "pasmo symbol files (*.symbol *.publics *.sys)" |
| 170 | << "vasm symbol files (*.sym)"; |
| 171 | d->setNameFilters(types); |
| 172 | d->setAcceptMode(QFileDialog::AcceptOpen); |
| 173 | d->setFileMode(QFileDialog::ExistingFile); |
| 174 | // set default directory |
| 175 | d->setDirectory(Settings::get().value("SymbolManager/OpenDir", QDir::currentPath()).toString()); |
| 176 | // run |
| 177 | if (d->exec()) { |
| 178 | QString f = d->selectedNameFilter(); |
| 179 | QString n = d->selectedFiles().at(0); |
| 180 | // load file from the correct type |
| 181 | bool read = false; |
| 182 | if (f.startsWith("OpenMSX Debugger session")) { |
| 183 | read = symTable.readFile(n, SymbolTable::OMDS_FILE); |
| 184 | } else if (f.startsWith("tniASM 0")) { |
| 185 | read = symTable.readFile(n, SymbolTable::TNIASM0_FILE); |
| 186 | } else if (f.startsWith("tniASM 1")) { |
| 187 | read = symTable.readFile(n, SymbolTable::TNIASM1_FILE); |
| 188 | } else if (f.startsWith("asMSX")) { |
| 189 | read = symTable.readFile(n, SymbolTable::ASMSX_FILE); |
| 190 | } else if (f.startsWith("HiTech C symbol")) { |
| 191 | read = symTable.readFile(n, SymbolTable::HTC_FILE); |
| 192 | } else if (f.startsWith("HiTech C link")) { |
| 193 | read = symTable.readFile(n, SymbolTable::LINKMAP_FILE); |
| 194 | } else if (f.startsWith("NoICE")) { |
| 195 | read = symTable.readFile(n, SymbolTable::NOICE_FILE); |
| 196 | } else if (f.startsWith("pasmo")) { |
| 197 | read = symTable.readFile(n, SymbolTable::PASMO_FILE); |
| 198 | } else if (f.startsWith("vasm")) { |
| 199 | read = symTable.readFile(n, SymbolTable::VASM_FILE); |
| 200 | } else { |
| 201 | read = symTable.readFile(n); |
| 202 | } |
| 203 | // if read succesful, add it to the list |
| 204 | if (read) { |
| 205 | initFileList(); |
| 206 | initSymbolList(); |
| 207 | emit symbolTableChanged(); |
| 208 | } |
| 209 | } |
| 210 | // store last used path |
| 211 | Settings::get().setValue("SymbolManager/OpenDir", d->directory().absolutePath()); |
| 212 | } |
| 213 | |
| 214 | void SymbolManager::removeFile() |
| 215 | { |
| 216 | int r = QMessageBox::question( |
| 217 | this, |
| 218 | tr("Remove symbol file(s)"), |
| 219 | tr("When removing the symbol file(s), do you want keep or " |
| 220 | "delete the attached symbols?"), |
| 221 | "Keep symbols", "Delete symbols", "Cancel", 1, 2); |
| 222 | if (r == 2) return; |
| 223 | |
| 224 | for (int i = 0; i < treeFiles->selectedItems().size(); ++i) { |
| 225 | symTable.unloadFile(treeFiles->selectedItems().at(i)->toolTip(0), r == 0); |
| 226 | } |
| 227 | |
| 228 | initFileList(); |
| 229 | initSymbolList(); |
| 230 | emit symbolTableChanged(); |
| 231 | } |
| 232 | |
| 233 | void SymbolManager::reloadFiles() |
| 234 | { |
| 235 | symTable.reloadFiles(); |
| 236 | initFileList(); |
| 237 | initSymbolList(); |
| 238 | emit symbolTableChanged(); |
| 239 | } |
| 240 | |
| 241 | void SymbolManager::addSymbolFileDestination(int fileIndex, int slotIndex) |
| 242 | { |
| 243 | for (Symbol* sym = symTable.findFirstAddressSymbol(0); |
| 244 | sym != nullptr; |
| 245 | sym = symTable.findNextAddressSymbol()) { |
| 246 | if (sym->source() == &symTable.symbolFile(fileIndex)) { |
| 247 | sym->setValidSlots(slotIndex == -1 ? 0xFFFF : 0xFFFF & (1 << slotIndex)); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | refresh(); |
| 252 | emit symbolTableChanged(); |
| 253 | wipeSelectedItem(); |
| 254 | } |
| 255 | |
| 256 | void SymbolManager::fileSelectionChange() |
| 257 | { |
| 258 | btnRemoveFile->setEnabled(!treeFiles->selectedItems().empty()); |
| 259 | } |
| 260 | |
| 261 | void SymbolManager::labelEdit(QTreeWidgetItem* item, int column) |
| 262 | { |
| 263 | // only symbol name and value are editable |
| 264 | if (column == 0 || column == 2) { |
| 265 | // open editor if manually added symbol |
| 266 | auto* sym = reinterpret_cast<Symbol*>(item->data(0, Qt::UserRole).value<quintptr>()); |
| 267 | if (!sym->source()) { |
| 268 | // first close possible existing editor |
| 269 | closeEditor(); |
| 270 | // open new editor |
| 271 | treeLabels->openPersistentEditor(item, column); |
| 272 | editItem = item; |
| 273 | editColumn = column; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Symbol support functions |
| 280 | */ |
| 281 | |
| 282 | void SymbolManager::initSymbolList() |
| 283 | { |
| 284 | treeLabels->clear(); |
| 285 | beginTreeLabelsUpdate(); |
| 286 | for (Symbol* sym = symTable.findFirstAddressSymbol(0); |
| 287 | sym != nullptr; |
| 288 | sym = symTable.findNextAddressSymbol()) { |
| 289 | auto* item = new QTreeWidgetItem(treeLabels); |
| 290 | // attach a pointer to the symbol object to the tree item |
| 291 | item->setData(0L, Qt::UserRole, quintptr(sym)); |
| 292 | // update columns |
| 293 | updateItemName(item); |
| 294 | updateItemType(item); |
| 295 | updateItemValue(item); |
| 296 | updateItemSlots(item); |
| 297 | updateItemSegments(item); |
| 298 | updateItemRegisters(item); |
| 299 | } |
| 300 | endTreeLabelsUpdate(); |
| 301 | } |
| 302 | |
| 303 | void SymbolManager::beginTreeLabelsUpdate() |
| 304 | { |
| 305 | ++treeLabelsUpdateCount; |
| 306 | } |
| 307 | |
| 308 | void SymbolManager::endTreeLabelsUpdate() |
| 309 | { |
| 310 | if (treeLabelsUpdateCount > 0) { |
| 311 | --treeLabelsUpdateCount; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | void SymbolManager::closeEditor() |
| 316 | { |
| 317 | if (editColumn >= 0) { |
| 318 | treeLabels->closePersistentEditor(editItem, editColumn); |
| 319 | editColumn = -1; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | void SymbolManager::addLabel() |
| 324 | { |
| 325 | // create an empty symbol |
| 326 | auto* sym = symTable.add(std::make_unique<Symbol>(tr("New symbol"), 0)); |
| 327 | |
| 328 | beginTreeLabelsUpdate(); |
| 329 | auto* item = new QTreeWidgetItem(treeLabels); |
| 330 | item->setData(0, Qt::UserRole, quintptr(sym)); |
| 331 | updateItemName(item); |
| 332 | updateItemType(item); |
| 333 | updateItemValue(item); |
| 334 | updateItemSlots(item); |
| 335 | updateItemSegments(item); |
| 336 | updateItemRegisters(item); |
| 337 | endTreeLabelsUpdate(); |
| 338 | closeEditor(); |
| 339 | treeLabels->setFocus(); |
| 340 | treeLabels->setCurrentItem(item, 0); |
| 341 | treeLabels->scrollToItem(item); |
| 342 | treeLabels->openPersistentEditor(item, 0); |
| 343 | editItem = item; |
| 344 | editColumn = 0; |
| 345 | |
| 346 | // emit notification that something has changed |
| 347 | emit symbolTableChanged(); |
| 348 | } |
| 349 | |
| 350 | void SymbolManager::removeLabel() |
| 351 | { |
| 352 | QList<QTreeWidgetItem*> selection = treeLabels->selectedItems(); |
| 353 | // check for selection |
| 354 | if (selection.empty()) return; |
| 355 | // remove selected items |
| 356 | bool deleted = false; |
| 357 | for (auto* sel : selection) { |
| 358 | // get symbol |
| 359 | Symbol* sym = reinterpret_cast<Symbol*>(sel->data(0, Qt::UserRole).value<quintptr>()); |
| 360 | // check if symbol is from symbol file |
| 361 | if (!sym->source()) { |
| 362 | // remove from table |
| 363 | symTable.remove(sym); |
| 364 | deleted = true; |
| 365 | } |
| 366 | } |
| 367 | if (deleted) { |
| 368 | // refresh tree |
| 369 | initSymbolList(); |
| 370 | emit symbolTableChanged(); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | void SymbolManager::labelChanged(QTreeWidgetItem* item, int column) |
| 375 | { |
| 376 | if (!treeLabelsUpdateCount) { |
| 377 | auto* sym = reinterpret_cast<Symbol*>(item->data(0, Qt::UserRole).value<quintptr>()); |
| 378 | // Set symbol text from tree item if any |
| 379 | QString symText = item->text(0).trimmed(); |
| 380 | if (symText.isEmpty()) symText = "[unnamed]"; |
| 381 | sym->setText(symText); |
| 382 | // set value TODO: proper decoding of value |
| 383 | auto value = stringToValue<uint16_t>(item->text(2)); |
| 384 | if (value) sym->setValue(*value); |
| 385 | treeLabels->closePersistentEditor(item, column); |
| 386 | editColumn = -1; |
| 387 | // update item name and value |
| 388 | beginTreeLabelsUpdate(); |
| 389 | updateItemName(item); |
| 390 | updateItemValue(item); |
| 391 | endTreeLabelsUpdate(); |
| 392 | // notify change |
| 393 | emit symbolTableChanged(); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | void SymbolManager::wipeSelectedItem() |
| 398 | { |
| 399 | for (int index = 0; index < vbox0->count(); ++index) { |
| 400 | static_cast<QCheckBox*>(vbox0->itemAt(index)->widget())->setChecked(false); |
| 401 | } |
| 402 | for (int index = 0; index < vbox1->count(); ++index) { |
| 403 | static_cast<QCheckBox*>(vbox1->itemAt(index)->widget())->setChecked(false); |
| 404 | } |
| 405 | for (int index = 0; index < vbox2->count(); ++index) { |
| 406 | static_cast<QCheckBox*>(vbox2->itemAt(index)->widget())->setChecked(false); |
| 407 | } |
| 408 | for (int index = 0; index < vbox3->count(); ++index) { |
| 409 | static_cast<QCheckBox*>(vbox3->itemAt(index)->widget())->setChecked(false); |
| 410 | } |
| 411 | for (int index = 0; index < gridRegs8->count(); ++index) { |
| 412 | static_cast<QCheckBox*>(gridRegs8->itemAt(index)->widget())->setChecked(false); |
| 413 | } |
| 414 | for (int index = 0; index < gridRegs16->count(); ++index) { |
| 415 | static_cast<QCheckBox*>(gridRegs16->itemAt(index)->widget())->setChecked(false); |
| 416 | } |
| 417 | for (int index = 0; index < vboxSymbolType->count(); ++index) { |
| 418 | auto radio = static_cast<QRadioButton*>(vboxSymbolType->itemAt(index)->widget()); |
| 419 | radio->setAutoExclusive(false); |
| 420 | radio->setChecked(false); |
| 421 | radio->setAutoExclusive(true); |
| 422 | } |
| 423 | |
| 424 | txtSegments->setText(""); |
| 425 | treeLabels->setCurrentItem(nullptr); |
| 426 | } |
| 427 | |
| 428 | void SymbolManager::labelSelectionChanged() |
| 429 | { |
| 430 | // remove possible editor |
| 431 | closeEditor(); |
| 432 | |
| 433 | QList<QTreeWidgetItem*> selection = treeLabels->selectedItems(); |
| 434 | // check if is available at all |
| 435 | if (selection.empty()) { |
| 436 | wipeSelectedItem(); |
| 437 | // disable everything |
| 438 | btnRemoveSymbol->setEnabled(false); |
| 439 | groupSlots->setEnabled(false); |
| 440 | groupSegments->setEnabled(false); |
| 441 | groupType->setEnabled(false); |
| 442 | groupRegs8->setEnabled(false); |
| 443 | groupRegs16->setEnabled(false); |
| 444 | return; |
| 445 | } |
| 446 | // check selection for "manual insertion", identical slot mask, |
| 447 | // identical register mask, identical types and value size |
| 448 | bool removeButActive = true; |
| 449 | bool anyEight = false; |
| 450 | bool sameType = true; |
| 451 | Symbol::SymbolType type; |
| 452 | int slotMask, slotMaskMultiple = 0; |
| 453 | int regMask, regMaskMultiple = 0; |
| 454 | for (auto selit = selection.begin(); selit != selection.end(); ++selit) { |
| 455 | // get symbol |
| 456 | auto* sym = reinterpret_cast<Symbol*>((*selit)->data(0, Qt::UserRole).value<quintptr>()); |
| 457 | // check if symbol is from symbol file |
| 458 | if (sym->source()) removeButActive = false; |
| 459 | |
| 460 | if (selit == selection.begin()) { |
| 461 | // first item, reference for slotMask and regMask |
| 462 | slotMask = sym->validSlots(); |
| 463 | regMask = sym->validRegisters(); |
| 464 | type = sym->type(); |
| 465 | } else { |
| 466 | // other, set all different bits |
| 467 | slotMaskMultiple |= slotMask ^ sym->validSlots(); |
| 468 | regMaskMultiple |= regMask ^ sym->validRegisters(); |
| 469 | // check for different type |
| 470 | if (type != sym->type()) sameType = false; |
| 471 | } |
| 472 | |
| 473 | // check for 8 bit values |
| 474 | if ((sym->value() & 0xFF00) == 0) { |
| 475 | anyEight = true; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | btnRemoveSymbol->setEnabled(removeButActive); |
| 480 | groupSlots->setEnabled(true); |
| 481 | groupType->setEnabled(true); |
| 482 | groupRegs8->setEnabled(anyEight); |
| 483 | groupRegs16->setEnabled(true); |
| 484 | beginTreeLabelsUpdate(); |
| 485 | |
| 486 | // set slot selection |
| 487 | for (auto* chkSlot : chkSlots) { |
| 488 | chkSlot->setTristate(false); |
| 489 | if (slotMaskMultiple & 1) { |
| 490 | chkSlot->setCheckState(Qt::PartiallyChecked); |
| 491 | } else if (slotMask & 1) { |
| 492 | chkSlot->setCheckState(Qt::Checked); |
| 493 | } else { |
| 494 | chkSlot->setCheckState(Qt::Unchecked); |
| 495 | } |
| 496 | slotMask >>= 1; |
| 497 | slotMaskMultiple >>= 1; |
| 498 | } |
| 499 | // set register selection |
| 500 | for (auto* chkReg : chkRegs) { |
| 501 | chkReg->setTristate(false); |
| 502 | if (regMaskMultiple & 1) { |
| 503 | chkReg->setCheckState(Qt::PartiallyChecked); |
| 504 | } else if (regMask & 1) { |
| 505 | chkReg->setCheckState(Qt::Checked); |
| 506 | } else { |
| 507 | chkReg->setCheckState(Qt::Unchecked); |
| 508 | } |
| 509 | regMask >>= 1; |
| 510 | regMaskMultiple >>= 1; |
| 511 | } |
| 512 | |
| 513 | // temporarily disable exclusive radiobuttons to be able to |
| 514 | // deselect them all. |
| 515 | radJump->setAutoExclusive (false); |
| 516 | radVar->setAutoExclusive (false); |
| 517 | radValue->setAutoExclusive(false); |
| 518 | // set type radio buttons |
| 519 | radJump->setChecked (sameType && type == Symbol::JUMPLABEL); |
| 520 | radVar->setChecked (sameType && type == Symbol::VARIABLELABEL); |
| 521 | radValue->setChecked(sameType && type == Symbol::VALUE); |
| 522 | // enable exclusive radiobuttons (this won't immediately activate |
| 523 | // one if none are active). |
| 524 | radJump->setAutoExclusive (true); |
| 525 | radVar->setAutoExclusive (true); |
| 526 | radValue->setAutoExclusive(true); |
| 527 | endTreeLabelsUpdate(); |
| 528 | } |
| 529 | |
| 530 | void SymbolManager::changeSlot(int id, int state) |
| 531 | { |
| 532 | if (treeLabelsUpdateCount) return; |
| 533 | |
| 534 | // disallow another tristate selection |
| 535 | chkSlots[id]->setTristate(false); |
| 536 | // get selected items |
| 537 | QList<QTreeWidgetItem*> selection = treeLabels->selectedItems(); |
| 538 | |
| 539 | // update items |
| 540 | beginTreeLabelsUpdate(); |
| 541 | int bit = 1 << id; |
| 542 | for (auto* sel : selection) { |
| 543 | auto* sym = reinterpret_cast<Symbol*>(sel->data(0, Qt::UserRole).value<quintptr>()); |
| 544 | // set or clear bit |
| 545 | if (state == Qt::Checked) { |
| 546 | sym->setValidSlots(sym->validSlots() | bit); |
| 547 | } else { |
| 548 | sym->setValidSlots(sym->validSlots() & ~bit); |
| 549 | } |
| 550 | // update item in treewidget |
| 551 | updateItemSlots(sel); |
| 552 | } |
| 553 | endTreeLabelsUpdate(); |
| 554 | // notify change |
| 555 | emit symbolTableChanged(); |
| 556 | } |
| 557 | |
| 558 | void SymbolManager::changeRegister(int id, int state) |
| 559 | { |
| 560 | if (treeLabelsUpdateCount) return; |
| 561 | |
| 562 | // disallow another tristate selection |
| 563 | chkRegs[id]->setTristate(false); |
| 564 | // get selected items |
| 565 | QList<QTreeWidgetItem*> selection = treeLabels->selectedItems(); |
| 566 | |
| 567 | // update items |
| 568 | beginTreeLabelsUpdate(); |
| 569 | int bit = 1 << id; |
| 570 | for (auto* sel : selection) { |
| 571 | auto* sym = reinterpret_cast<Symbol*>(sel->data(0, Qt::UserRole).value<quintptr>()); |
| 572 | // set or clear bit |
| 573 | if (state == Qt::Checked) { |
| 574 | sym->setValidRegisters(sym->validRegisters() | bit); |
| 575 | } else { |
| 576 | sym->setValidRegisters(sym->validRegisters() & ~bit); |
| 577 | } |
| 578 | // update item in treewidget |
| 579 | updateItemRegisters(sel); |
| 580 | } |
| 581 | endTreeLabelsUpdate(); |
| 582 | // notify change |
| 583 | emit symbolTableChanged(); |
| 584 | } |
| 585 | |
| 586 | void SymbolManager::changeType(bool /*checked*/) |
| 587 | { |
| 588 | if (treeLabelsUpdateCount) return; |
| 589 | |
| 590 | // determine selected type |
| 591 | Symbol::SymbolType newType = Symbol::JUMPLABEL; |
| 592 | if (radVar->isChecked()) { |
| 593 | newType = Symbol::VARIABLELABEL; |
| 594 | } else if (radValue->isChecked()) { |
| 595 | newType = Symbol::VALUE; |
| 596 | } |
| 597 | |
| 598 | // get selected items |
| 599 | QList<QTreeWidgetItem*> selection = treeLabels->selectedItems(); |
| 600 | |
| 601 | // update items |
| 602 | beginTreeLabelsUpdate(); |
| 603 | for (auto* sel : selection) { |
| 604 | auto* sym = reinterpret_cast<Symbol*>(sel->data(0, Qt::UserRole).value<quintptr>()); |
| 605 | sym->setType(newType); |
| 606 | updateItemType(sel); |
| 607 | } |
| 608 | endTreeLabelsUpdate(); |
| 609 | // notify change |
| 610 | emit symbolTableChanged(); |
| 611 | } |
| 612 | |
| 613 | |
| 614 | /* |
| 615 | * Symbol tree layout functions |
| 616 | */ |
| 617 | |
| 618 | void SymbolManager::updateItemName(QTreeWidgetItem* item) |
| 619 | { |
| 620 | auto* sym = reinterpret_cast<Symbol*>(item->data(0, Qt::UserRole).value<quintptr>()); |
| 621 | // set text and icon |
| 622 | item->setText(0, sym->text()); |
| 623 | if (sym->source()) { |
| 624 | item->setIcon(0, QIcon(":/icons/symfil.png")); |
| 625 | item->setText(6, *sym->source()); |
| 626 | } else { |
| 627 | item->setIcon(0, QIcon(":/icons/symman.png")); |
| 628 | } |
| 629 | // set color based on status as well as status in 2nd column |
| 630 | switch (sym->status()) { |
| 631 | case Symbol::HIDDEN: |
| 632 | item->setForeground(0, QColor(128, 128, 128)); |
| 633 | break; |
| 634 | case Symbol::LOST: |
| 635 | item->setForeground(0, QColor(128, 0, 0)); |
| 636 | break; |
| 637 | default: |
| 638 | break; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | void SymbolManager::updateItemType(QTreeWidgetItem* item) |
| 643 | { |
| 644 | auto* sym = reinterpret_cast<Symbol*>(item->data(0, Qt::UserRole).value<quintptr>()); |
| 645 | // set symbol type in 2nd column |
| 646 | switch (sym->type()) { |
| 647 | case Symbol::JUMPLABEL: |
| 648 | item->setText(1, tr("Jump label")); |
| 649 | break; |
| 650 | case Symbol::VARIABLELABEL: |
| 651 | item->setText(1, tr("Variable label")); |
| 652 | break; |
| 653 | case Symbol::VALUE: |
| 654 | item->setText(1, tr("Value")); |
| 655 | break; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | void SymbolManager::updateItemValue(QTreeWidgetItem* item) |
| 660 | { |
| 661 | auto* sym = reinterpret_cast<Symbol*>(item->data(0, Qt::UserRole).value<quintptr>()); |
| 662 | |
| 663 | // symbol value in 3rd column |
| 664 | item->setText(2, hexValue(sym->value(), 4)); |
| 665 | } |
| 666 | |
| 667 | void SymbolManager::updateItemSlots(QTreeWidgetItem* item) |
| 668 | { |
| 669 | auto* sym = reinterpret_cast<Symbol*>(item->data(0, Qt::UserRole).value<quintptr>()); |
| 670 | |
| 671 | QString slotText; |
| 672 | int slotmask = sym->validSlots(); |
| 673 | // value represents 16 bits for 4 subslots in 4 slots |
| 674 | if (slotmask == 0xFFFF) { |
| 675 | slotText = tr("All"); |
| 676 | } else if (slotmask == 0) { |
| 677 | slotText = tr("None"); |
| 678 | } else { |
| 679 | // create a list of valid slots |
| 680 | // loop over all primary slots |
| 681 | for (int ps = 0; ps < 4; ++ps) { |
| 682 | QString subText; |
| 683 | int subslots = (slotmask >> (4 * ps)) & 15; |
| 684 | if (subslots == 15) { |
| 685 | // all subslots are ok |
| 686 | subText = QString("%1-*").arg(ps); |
| 687 | } else if (subslots) { |
| 688 | // some subslots are ok |
| 689 | if (subslots & 1) subText += "/0"; |
| 690 | if (subslots & 2) subText += "/1"; |
| 691 | if (subslots & 4) subText += "/2"; |
| 692 | if (subslots & 8) subText += "/3"; |
| 693 | subText = QString("%1-").arg(ps) + subText.mid(1); |
| 694 | } |
| 695 | // add to string if any subslots were ok |
| 696 | if (!subText.isEmpty()) { |
| 697 | if (!slotText.isEmpty()) slotText += ", "; |
| 698 | slotText += subText; |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | // valid slots in 4th column |
| 704 | item->setText(3, slotText); |
| 705 | } |
| 706 | |
| 707 | void SymbolManager::updateItemSegments(QTreeWidgetItem* item) |
| 708 | { |
| 709 | item->data(0, Qt::UserRole).value<quintptr>(); |
| 710 | } |
| 711 | |
| 712 | void SymbolManager::updateItemRegisters(QTreeWidgetItem* item) |
| 713 | { |
| 714 | auto* sym = reinterpret_cast<Symbol*>(item->data(0, Qt::UserRole).value<quintptr>()); |
| 715 | |
| 716 | QString regText; |
| 717 | int regmask = sym->validRegisters(); |
| 718 | // value represents 16 bits for 4 subslots in 4 slots |
| 719 | if (regmask == 0x3FFFF) { |
| 720 | regText = tr("All"); |
| 721 | } else if (regmask == 0) { |
| 722 | regText = tr("None"); |
| 723 | } else { |
| 724 | if ((regmask & Symbol::REG_ALL8) == Symbol::REG_ALL8) { |
| 725 | // all 8 bit registers selected |
| 726 | regText = "All 8 bit, "; |
| 727 | regmask ^= Symbol::REG_ALL8; |
| 728 | } else if ((regmask & Symbol::REG_ALL16) == Symbol::REG_ALL16) { |
| 729 | // all 16 bit registers selected |
| 730 | regText = "All 16 bit, "; |
| 731 | regmask ^= Symbol::REG_ALL16; |
| 732 | } |
| 733 | // register list for remaining registers |
| 734 | static const char* const registers[] = { |
| 735 | "A", "B", "C", "D", "E", "H", "L", "BC", "DE", "HL", |
| 736 | "IX", "IY", "IXL", "IXH", "IYL", "IYH", "Offset", "I" |
| 737 | }; |
| 738 | for (const char* reg : registers) { |
| 739 | if (regmask & 1) { |
| 740 | regText += QString("%1, ").arg(reg); |
| 741 | } |
| 742 | regmask >>= 1; |
| 743 | } |
| 744 | regText.chop(2); |
| 745 | } |
| 746 | |
| 747 | // valid slots in 4th column |
| 748 | item->setText(5, regText); |
| 749 | } |
| 750 | |
| 751 | |
| 752 | // load of functions that shouldn't really be necessary |
| 753 | void SymbolManager::changeSlot00(int state) |
| 754 | { |
| 755 | changeSlot(0, state); |
| 756 | } |
| 757 | void SymbolManager::changeSlot01(int state) |
| 758 | { |
| 759 | changeSlot(1, state); |
| 760 | } |
| 761 | void SymbolManager::changeSlot02(int state) |
| 762 | { |
| 763 | changeSlot(2, state); |
| 764 | } |
| 765 | void SymbolManager::changeSlot03(int state) |
| 766 | { |
| 767 | changeSlot(3, state); |
| 768 | } |
| 769 | void SymbolManager::changeSlot10(int state) |
| 770 | { |
| 771 | changeSlot(4, state); |
| 772 | } |
| 773 | void SymbolManager::changeSlot11(int state) |
| 774 | { |
| 775 | changeSlot(5, state); |
| 776 | } |
| 777 | void SymbolManager::changeSlot12(int state) |
| 778 | { |
| 779 | changeSlot(6, state); |
| 780 | } |
| 781 | void SymbolManager::changeSlot13(int state) |
| 782 | { |
| 783 | changeSlot(7, state); |
| 784 | } |
| 785 | void SymbolManager::changeSlot20(int state) |
| 786 | { |
| 787 | changeSlot(8, state); |
| 788 | } |
| 789 | void SymbolManager::changeSlot21(int state) |
| 790 | { |
| 791 | changeSlot(9, state); |
| 792 | } |
| 793 | void SymbolManager::changeSlot22(int state) |
| 794 | { |
| 795 | changeSlot(10, state); |
| 796 | } |
| 797 | void SymbolManager::changeSlot23(int state) |
| 798 | { |
| 799 | changeSlot(11, state); |
| 800 | } |
| 801 | void SymbolManager::changeSlot30(int state) |
| 802 | { |
| 803 | changeSlot(12, state); |
| 804 | } |
| 805 | void SymbolManager::changeSlot31(int state) |
| 806 | { |
| 807 | changeSlot(13, state); |
| 808 | } |
| 809 | void SymbolManager::changeSlot32(int state) |
| 810 | { |
| 811 | changeSlot(14, state); |
| 812 | } |
| 813 | void SymbolManager::changeSlot33(int state) |
| 814 | { |
| 815 | changeSlot(15, state); |
| 816 | } |
| 817 | void SymbolManager::changeRegisterA(int state) |
| 818 | { |
| 819 | changeRegister(0, state); |
| 820 | } |
| 821 | void SymbolManager::changeRegisterB(int state) |
| 822 | { |
| 823 | changeRegister(1, state); |
| 824 | } |
| 825 | void SymbolManager::changeRegisterC(int state) |
| 826 | { |
| 827 | changeRegister(2, state); |
| 828 | } |
| 829 | void SymbolManager::changeRegisterD(int state) |
| 830 | { |
| 831 | changeRegister(3, state); |
| 832 | } |
| 833 | void SymbolManager::changeRegisterE(int state) |
| 834 | { |
| 835 | changeRegister(4, state); |
| 836 | } |
| 837 | void SymbolManager::changeRegisterH(int state) |
| 838 | { |
| 839 | changeRegister(5, state); |
| 840 | } |
| 841 | void SymbolManager::changeRegisterL(int state) |
| 842 | { |
| 843 | changeRegister(6, state); |
| 844 | } |
| 845 | void SymbolManager::changeRegisterBC(int state) |
| 846 | { |
| 847 | changeRegister(7, state); |
| 848 | } |
| 849 | void SymbolManager::changeRegisterDE(int state) |
| 850 | { |
| 851 | changeRegister(8, state); |
| 852 | } |
| 853 | void SymbolManager::changeRegisterHL(int state) |
| 854 | { |
| 855 | changeRegister(9, state); |
| 856 | } |
| 857 | void SymbolManager::changeRegisterIX(int state) |
| 858 | { |
| 859 | changeRegister(10, state); |
| 860 | } |
| 861 | void SymbolManager::changeRegisterIY(int state) |
| 862 | { |
| 863 | changeRegister(11, state); |
| 864 | } |
| 865 | void SymbolManager::changeRegisterIXL(int state) |
| 866 | { |
| 867 | changeRegister(12, state); |
| 868 | } |
| 869 | void SymbolManager::changeRegisterIXH(int state) |
| 870 | { |
| 871 | changeRegister(13, state); |
| 872 | } |
| 873 | void SymbolManager::changeRegisterIYL(int state) |
| 874 | { |
| 875 | changeRegister(14, state); |
| 876 | } |
| 877 | void SymbolManager::changeRegisterIYH(int state) |
| 878 | { |
| 879 | changeRegister(15, state); |
| 880 | } |
| 881 | void SymbolManager::changeRegisterOffset(int state) |
| 882 | { |
| 883 | changeRegister(16, state); |
| 884 | } |
| 885 | void SymbolManager::changeRegisterI(int state) |
| 886 | { |
| 887 | changeRegister(17, state); |
| 888 | } |