* @brief Returns a unique title for a duplicated item using a numbered " (N)" suffix. */
| 157 | * @brief Returns a unique title for a duplicated item using a numbered " (N)" suffix. |
| 158 | */ |
| 159 | static QString nextDuplicateTitle(const QString& title, const QStringList& taken) |
| 160 | { |
| 161 | static const QRegularExpression kSuffixRe(QStringLiteral("^(.*?)\\s*\\((\\d+)\\)\\s*$")); |
| 162 | |
| 163 | QString base = title; |
| 164 | const auto stripped = kSuffixRe.match(title); |
| 165 | if (stripped.hasMatch()) |
| 166 | base = stripped.captured(1).trimmed(); |
| 167 | |
| 168 | const QString basePattern = QStringLiteral("^") + QRegularExpression::escape(base) |
| 169 | + QStringLiteral("(?:\\s*\\((\\d+)\\))?\\s*$"); |
| 170 | const QRegularExpression baseRe(basePattern); |
| 171 | |
| 172 | int maxN = -1; |
| 173 | for (const auto& t : taken) { |
| 174 | const auto m = baseRe.match(t); |
| 175 | if (!m.hasMatch()) |
| 176 | continue; |
| 177 | |
| 178 | const QString suffix = m.captured(1); |
| 179 | if (suffix.isEmpty()) { |
| 180 | maxN = qMax(maxN, 0); |
| 181 | continue; |
| 182 | } |
| 183 | |
| 184 | bool ok = false; |
| 185 | const int n = suffix.toInt(&ok); |
| 186 | if (ok) |
| 187 | maxN = qMax(maxN, n); |
| 188 | } |
| 189 | |
| 190 | if (maxN < 0) |
| 191 | return QStringLiteral("%1 (1)").arg(base); |
| 192 | |
| 193 | return QStringLiteral("%1 (%2)").arg(base, QString::number(maxN + 1)); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * @brief Increments the per-type counter for every eligible dataset widget. |
no test coverage detected