When selecting objects for a group and after hitting enter a window opens with the list of possible groups. The sorting is by creation order (at least it seems to me).
Would it be possible to have the same order as in the groupmanager window?
When selecting objects for a group and after hitting enter a window opens with the list of possible groups. The sorting is by creation order (at least it seems to me).
Would it be possible to have the same order as in the groupmanager window?
Correct. The list of groups in the group tool dialog is based on parsing the < groups > elements in order they appear in the xml. Technically in terms of the combobox they are not sorted. We could turn on the sorting to sort acending or decending by item->text.
Not easily. The Group manager items can be dynamically sorted by visibilty, locked, has objects. color, or name. Again the tool dialog is parsing the xml by creation order to get the list, and not by the order in the Group manager list. The Group tool dialog has no pointer argument to the Group Manager class, and the Group Manager class has no getter for the QTable.
Sorted by name acending:
Sorted by name decending:
Sorted by Visiblilty:
Understood, but maybe a sorting by nam would be better than nothing
Fair enough.
Actually I’m kinda of surprised it’s not already sorting by item text as that’s the default for comboboxes (or list and table widgets for that matter). You normally have to disable sorting if you don’t want the order changing.
Ok… I took a look at the AddToGroupDialog and it now makes sense why it doesn’t sort.
Here’s the current routine to fill the combobox:
void AddToGroupDialog::fillNameBox() { QMap<quint32, GroupAttributes> groups = m_doc->getGroups(); QStringList groupNames; auto i = groups.constBegin(); while (i != groups.constEnd()) { const GroupAttributes data = i.value(); groupNames.append(data.name); ++i; } ui->groupName_ComboBox->addItems(groupNames); }
Since the groupnames are appended to a string list, which is not sorted, the whole list is added as is to the combobox - unsorted.
There’s 3 ways we could have it sort:
void AddToGroupDialog::fillNameBox() { QMap<quint32, GroupAttributes> groups = m_doc->getGroups(); QStringList groupNames; auto i = groups.constBegin(); while (i != groups.constEnd()) { const GroupAttributes data = i.value(); groupNames.append(data.name); ++i; } groupNames.sort(); ui->groupName_ComboBox->addItems(groupNames); }
2: Sort the combobox afer items added:
void AddToGroupDialog::fillNameBox() { QMap<quint32, GroupAttributes> groups = m_doc->getGroups(); QStringList groupNames; auto i = groups.constBegin(); while (i != groups.constEnd()) { const GroupAttributes data = i.value(); groupNames.append(data.name); ++i; } ui->groupName_ComboBox->addItems(groupNames); ui->groupName_ComboBox->model()->sort(0, Qt::AscendingOrder); }
3: Which would be my choice as it’s cleaner, even though maybe takes a few more cpu cycles… get rid of the QStringList and just add each item seperately, which automatically triggers the combobox to sort as each item is added:
void AddToGroupDialog::fillNameBox() { QMap<quint32, GroupAttributes> groups = m_doc->getGroups(); auto i = groups.constBegin(); while (i != groups.constEnd()) { ui->groupName_ComboBox->addItem(i.value().name); ++i; } }
And that concludes our lesson for the day.
Oh… and another issue in sorting the AddToGroup dialog by how the GroupWidget table in the Group Manager dock is sorted… We would have to add a getter for a pointer to groupsWidget in MainWindow and AbstractMainWindow… and because AddToGroupsDialog is in the LIBS folder we can’t directly access a pointer to mainWindow in the APPS folder, so in the AddToGroupsDialog we have to create an instance of AbstractMainWindow from which we can then get the handle of GroupsWidget to then access the ui->groups_TableWidget. Not impossible… just not straight forward.
Update: Challenge accepted and done. The combobox in the Add to Group dialog will match the sort state of the Groups in the Group Manager.
And the fillNameBox() routine got even shorter.
void AddToGroupDialog::fillNameBox() { VAbstractMainWindow *window = qobject_cast<VAbstractMainWindow *>(qApp->getMainWindow()); SCASSERT(window != nullptr) QStringList groupNames = window->getGroupsByTable(); ui->groupName_ComboBox->addItems(groupNames); }
Update: Challenge accepted and done. The combobox in the Add to Group dialog will match the sort state of the Groups in the Group Manager.
And the fillNameBox() routine got even shorter.
void AddToGroupDialog::fillNameBox() { VAbstractMainWindow *window = qobject_cast<VAbstractMainWindow *>(qApp->getMainWindow()); SCASSERT(window != nullptr) QStringList groupNames = window->getGroupsByTable(); ui->groupName_ComboBox->addItems(groupNames); }
For what it’s worth I added a group color icon to combobox as well to further help identify the groups.
and fillNameBox is smaller yet.
void AddToGroupDialog::fillNameBox() { VAbstractMainWindow *window = qobject_cast<VAbstractMainWindow *>(qApp->getMainWindow()); SCASSERT(window != nullptr) window->getGroupsByTable(ui->groupName_ComboBox); }