+ settings.endGroup();
+
+ qDebug() << "Locations indexes before restoring positions";
+ qDebug() << this->m_indexStorage;
+ qDebug() << "Restoring these locations positions.";
+ qDebug() << tempIndex;
+
+ // Swap locations to correct indexes.
+ QMap<QString, int>::iterator it, ite;
+ for (it = tempIndex.begin(), ite = tempIndex.end(); it != ite; ++it)
+ {
+ int oldIndex = this->m_indexStorage.indexOf(it.key());
+ // Only operate on this item if current index is not the same as specified
+ if (it.value() != oldIndex + 1)
+ {
+ // Move to last if requested index is greater than the number of items.
+ if (it.value() >= this->m_indexStorage.size()) {
+ this->m_indexStorage.swap(oldIndex, this->m_indexStorage.size() - 1);
+ }
+ else {
+ this->m_indexStorage.swap(oldIndex, it.value() - 1);
+ }
+ }
+ }
+
+ qDebug() << "Locations indexes after positions are restored.";
+ qDebug() << this->m_indexStorage;
+}
+
+void Locations::saveLocation(Location *location)
+{
+ if (!location) {
+ qDebug() << "Null location given to saveLocation. Aborting";
+ return;
+ }
+ qDebug() << "Saving location " << location->label();
+ QSettings settings;
+ settings.beginGroup("Locations");
+ settings.beginGroup(location->label() );
+ if (location->isValid()) {
+ settings.setValue( "address", location->address() );
+ settings.setValue( "x", location->x() );
+ settings.setValue( "y", location->y() );
+ }
+ else {
+ if (settings.contains("address")) settings.remove("address");
+ if (settings.contains("x")) settings.remove("x");
+ if (settings.contains("y")) settings.remove("y");
+ }
+ settings.setValue("index", this->m_indexStorage.indexOf(location->label()) + 1);
+ settings.endGroup();
+ settings.endGroup();
+}