@@ -181,17 +181,20 @@ bool displayDeleteWarning(QWidget* parent, QString windowTitle, const QList<What
181
181
* Repopulates the given combo box with the given table's entries, filtered and sorted according to
182
182
* the given parameters, and writes the IDs of the entries to the referenced list.
183
183
*
184
- * @param table The table to get the entries from.
185
- * @param displayAndSortColumn The column of the table to use for displaying and sorting the entries.
186
- * @param sortAsString Whether to sort the entries as strings. Otherwise, QVariant::compare() is used.
187
- * @param combo The combo box to populate.
188
- * @param idList The list in which to store the IDs of the entries.
189
- * @param overrideFirstLine If not empty, this string will be used as the first line of the combo box instead of the table's none string.
190
- * @param filterColumn If not null, only entries whose foreign key ID in this column matches the given ID will be added to the combo box.
191
- * @param filterID The ID to use for filtering entries, or an invalid ID to filter for entries with no reference.
184
+ * @param table The table to get the entries from.
185
+ * @param displayAndSortColumn The column of the table to use for displaying and sorting the entries.
186
+ * @param sortAsString Whether to sort the entries as strings. Otherwise, QVariant::compare() is used.
187
+ * @param combo The combo box to populate.
188
+ * @param idList The list in which to store the IDs of the entries.
189
+ * @param overrideFirstLine If not empty, this string will be used as the first line of the combo box instead of the table's none string.
190
+ * @param distinctionKeyColumn If not null, this column will be used to point to a cell in distinctionContentColumn.
191
+ * @param distinctionContentColumn If not null, this column will be used to add a distinguishing name in brackets to entries with duplicate names.
192
+ * @param filterColumn If not null, only entries whose foreign key ID in this column matches the given ID will be added to the combo box.
193
+ * @param filterID The ID to use for filtering entries, or an invalid ID to filter for entries with no reference.
192
194
*/
193
- void populateItemCombo (NormalTable& table, const ValueColumn& displayAndSortColumn, bool sortAsString, QComboBox* combo, QList<ValidItemID>& idList, QString overrideFirstLine, const ForeignKeyColumn* filterColumn, ItemID filterID)
195
+ void populateItemCombo (NormalTable& table, const ValueColumn& displayAndSortColumn, bool sortAsString, QComboBox* combo, QList<ValidItemID>& idList, const QString& overrideFirstLine, const ForeignKeyColumn* distinctionKeyColumn, const ValueColumn* distinctionContentColumn , const ForeignKeyColumn* filterColumn, ItemID filterID)
194
196
{
197
+ assert (displayAndSortColumn.table == &table);
195
198
assert (!(!filterColumn && filterID.isValid ()));
196
199
197
200
combo->clear ();
@@ -202,6 +205,7 @@ void populateItemCombo(NormalTable& table, const ValueColumn& displayAndSortColu
202
205
203
206
// Get pairs of ID and display/sort field
204
207
QList<QPair<ValidItemID, QVariant>> selectableItems = table.pairIDWith (displayAndSortColumn);
208
+ if (selectableItems.isEmpty ()) return ;
205
209
206
210
if (filterColumn) {
207
211
// Filter entries: if an item's foreign key ID doesn't match the given one, discard it
@@ -222,6 +226,36 @@ void populateItemCombo(NormalTable& table, const ValueColumn& displayAndSortColu
222
226
};
223
227
std::sort (selectableItems.begin (), selectableItems.end (), comparator);
224
228
229
+ // Add distinction field in brackets in case of duplicates
230
+ if (distinctionKeyColumn || distinctionContentColumn) {
231
+ assert (distinctionKeyColumn && distinctionContentColumn);
232
+ assert (distinctionKeyColumn->table == &table);
233
+ assert (distinctionKeyColumn->foreignColumn ->table == distinctionContentColumn->table );
234
+ // Find duplicates
235
+ QSet<int > duplicateNameIndices = QSet<int >();
236
+ QString previousName;
237
+ for (int i = 0 ; i < selectableItems.size (); i++) {
238
+ const QString currentName = selectableItems.at (i).second .toString ();
239
+ const bool sameName = currentName == previousName && i != 0 ;
240
+ if (sameName) {
241
+ duplicateNameIndices.insert (i - 1 );
242
+ duplicateNameIndices.insert (i);
243
+ }
244
+ previousName = currentName;
245
+ }
246
+ // Append distinction column content to duplicate names
247
+ for (const int duplicateNameIndex : duplicateNameIndices) {
248
+ const ValidItemID& itemID = selectableItems.at (duplicateNameIndex).first ;
249
+ const ItemID& distinctionKey = distinctionKeyColumn->getValueFor (itemID);
250
+ if (distinctionKey.isInvalid ()) continue ;
251
+ const QString& distinctionContent = distinctionContentColumn->getValueFor (FORCE_VALID (distinctionKey)).toString ();
252
+ if (distinctionContent.isEmpty ()) continue ;
253
+ const QString& currentName = selectableItems.at (duplicateNameIndex).second .toString ();
254
+ const QString newName = currentName + " (" + distinctionContent + " )" ;
255
+ selectableItems[duplicateNameIndex].second = newName;
256
+ }
257
+ }
258
+
225
259
// Save IDs and populate combo box
226
260
for (const auto & [itemID, name] : selectableItems) {
227
261
idList.append (itemID);
0 commit comments