* @brief Rebuilds DeviceManagers for all sources when the project source list changes. */
| 1245 | * @brief Rebuilds DeviceManagers for all sources when the project source list changes. |
| 1246 | */ |
| 1247 | void IO::ConnectionManager::rebuildDevices() |
| 1248 | { |
| 1249 | const auto opMode = AppState::instance().operationMode(); |
| 1250 | const bool wasConnected = isConnected(); |
| 1251 | |
| 1252 | bool willRebuildDevice0 = (opMode != SerialStudio::ProjectFile); |
| 1253 | bool didChangeBusType = false; |
| 1254 | if (opMode == SerialStudio::ProjectFile) { |
| 1255 | const auto& srcs = DataModel::ProjectModel::instance().sources(); |
| 1256 | for (const auto& src : srcs) { |
| 1257 | if (src.sourceId != 0) |
| 1258 | continue; |
| 1259 | |
| 1260 | const bool missingPrimary = (m_devices.find(0) == m_devices.end()); |
| 1261 | const bool busTypeChanged = m_busType != static_cast<SerialStudio::BusType>(src.busType); |
| 1262 | willRebuildDevice0 = missingPrimary || busTypeChanged || !src.connectionSettings.isEmpty(); |
| 1263 | |
| 1264 | if (busTypeChanged && willRebuildDevice0) { |
| 1265 | m_busType = static_cast<SerialStudio::BusType>(src.busType); |
| 1266 | m_settings.setValue("IOManager/busType", static_cast<int>(m_busType)); |
| 1267 | didChangeBusType = true; |
| 1268 | } |
| 1269 | |
| 1270 | break; |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | for (auto it = m_devices.begin(); it != m_devices.end();) { |
| 1275 | const bool skipPrimary = (it->first == 0 && !willRebuildDevice0); |
| 1276 | if (skipPrimary) { |
| 1277 | ++it; |
| 1278 | continue; |
| 1279 | } |
| 1280 | |
| 1281 | if (it->second) { |
| 1282 | it->second->close(); |
| 1283 | disconnect(it->second.get(), nullptr, this, nullptr); |
| 1284 | } |
| 1285 | |
| 1286 | it = m_devices.erase(it); |
| 1287 | } |
| 1288 | |
| 1289 | if (opMode == SerialStudio::ProjectFile) { |
| 1290 | const auto& sources = DataModel::ProjectModel::instance().sources(); |
| 1291 | for (const auto& src : sources) |
| 1292 | buildDeviceForSource(src, willRebuildDevice0); |
| 1293 | } |
| 1294 | |
| 1295 | Q_EMIT configurationChanged(); |
| 1296 | Q_EMIT driverChanged(); |
| 1297 | Q_EMIT connectedChanged(); |
| 1298 | Q_EMIT contextsRebuilt(); |
| 1299 | |
| 1300 | if (didChangeBusType) |
| 1301 | Q_EMIT busTypeChanged(); |
| 1302 | |
| 1303 | if (opMode == SerialStudio::ProjectFile) { |
| 1304 | QMetaObject::invokeMethod( |