* @brief Adds a new group with a unique title and the given widget type. */
| 4579 | * @brief Adds a new group with a unique title and the given widget type. |
| 4580 | */ |
| 4581 | void DataModel::ProjectModel::addGroup(const QString& title, |
| 4582 | const SerialStudio::GroupWidget widget, |
| 4583 | int sourceId, |
| 4584 | int parentFolderId) |
| 4585 | { |
| 4586 | int count = 1; |
| 4587 | QString newTitle = title; |
| 4588 | for (const auto& group : std::as_const(m_groups)) { |
| 4589 | if (group.title == newTitle) { |
| 4590 | count++; |
| 4591 | newTitle = QString("%1 (%2)").arg(title, QString::number(count)); |
| 4592 | } |
| 4593 | } |
| 4594 | |
| 4595 | while (count > 1) { |
| 4596 | bool titleExists = false; |
| 4597 | for (const auto& group : std::as_const(m_groups)) { |
| 4598 | if (group.title != newTitle) |
| 4599 | continue; |
| 4600 | |
| 4601 | count++; |
| 4602 | newTitle = QString("%1 (%2)").arg(title, QString::number(count)); |
| 4603 | titleExists = true; |
| 4604 | break; |
| 4605 | } |
| 4606 | |
| 4607 | if (!titleExists) |
| 4608 | break; |
| 4609 | } |
| 4610 | |
| 4611 | DataModel::Group group; |
| 4612 | group.title = newTitle; |
| 4613 | group.groupId = m_groups.size(); |
| 4614 | group.uniqueId = allocateUniqueId(); |
| 4615 | group.parentFolderId = |
| 4616 | (parentFolderId != -1 && folderExists(m_groupFolders, parentFolderId)) ? parentFolderId : -1; |
| 4617 | |
| 4618 | if (sourceId >= 0) |
| 4619 | group.sourceId = sourceId; |
| 4620 | |
| 4621 | m_groups.push_back(group); |
| 4622 | setGroupWidget(static_cast<int>(m_groups.size()) - 1, widget); |
| 4623 | m_selectedGroup = m_groups.back(); |
| 4624 | |
| 4625 | Q_EMIT groupAdded(static_cast<int>(m_groups.size()) - 1); |
| 4626 | setModified(true); |
| 4627 | } |
| 4628 | |
| 4629 | /** |
| 4630 | * @brief Assigns a widget type to the group, replacing fixed-layout datasets. |
no test coverage detected