Sort children is recursive and will mess up services' children (e.g. search

result item not the first item anymore) so we should call it only once at
the beginning.
And instead of sorting all items everytime, we just insert them at the correct
position.
This commit is contained in:
Arnaud Bienner 2014-12-27 00:33:23 +01:00
parent bb554a4685
commit 9cb2a17f37
3 changed files with 20 additions and 3 deletions

View File

@ -118,6 +118,7 @@ InternetModel::InternetModel(Application* app, QObject* parent)
AddService(new VkService(app, this));
#endif
invisibleRootItem()->sortChildren(0, Qt::AscendingOrder);
UpdateServices();
}
@ -361,13 +362,27 @@ void InternetModel::UpdateServices() {
}
s.endGroup();
}
invisibleRootItem()->sortChildren(0, Qt::AscendingOrder);
int InternetModel::FindItemPosition(const QString& text) {
int a = 0;
int b = invisibleRootItem()->rowCount() - 1;
while (a <= b) {
int mid = a + (b - a)/2;
if (invisibleRootItem()->child(mid, 0)->text() < text) {
a = mid + 1;
} else {
b = mid - 1;
}
}
return a;
}
void InternetModel::ShowService(InternetService* service) {
if (shown_services_[service].shown != true) {
invisibleRootItem()->appendRow(shown_services_[service].item);
QStandardItem* item = shown_services_[service].item;
int pos = FindItemPosition(item->text());
invisibleRootItem()->insertRow(pos, item);
shown_services_[service].shown = true;
}
}

View File

@ -142,6 +142,9 @@ class InternetModel : public QStandardItemModel {
void ShowService(InternetService* service);
// Add or remove the services according to the setting file
void UpdateServices();
// Find the position where to insert this item. The list of services is
// supposed to be alphabetically sorted.
int FindItemPosition(const QString& text);
// Returns the service that is a parent of this item. Works by walking up
// the tree until it finds an item with Role_Service set.

View File

@ -75,7 +75,6 @@ void MoodbarController::AsyncLoadComplete(MoodbarPipeline* pipeline,
case Engine::Empty:
case Engine::Idle:
return;
break;
default:
break;