Blob Blame History Raw
From f6659597570e3281cbd23449b39782b25c77963d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cristian=20One=C8=9B?= <onet.cristian@gmail.com>
Date: Thu, 15 Nov 2012 21:51:02 +0200
Subject: [PATCH 02/22] Fix the investment accounts handling in the
 institutions view. Make the investment accounts the only accounts that are
 added to their parent in the institutions view. The rest of the accounts are
 added at the first level because they don't inherit the institution from
 their parent. Thanks to Allen for working on this.

BUG: 309105
(cherry picked from commit a5371f1e0ad7b42c7cfd1a2790108abbac96a732)
---
 kmymoney/models/accountsmodel.cpp | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/kmymoney/models/accountsmodel.cpp b/kmymoney/models/accountsmodel.cpp
index c21c39d..37816f5 100644
--- a/kmymoney/models/accountsmodel.cpp
+++ b/kmymoney/models/accountsmodel.cpp
@@ -759,7 +759,10 @@ public:
     }
     QStandardItem *institutionItem = institutionItemFromId(model, institutionId);
     QStandardItem *item = itemFromAccountId(institutionItem, account.id());
-    QStandardItem *parentAccounItem = itemFromAccountId(institutionItem, account.parentAccountId());
+    // only investment accounts are added to their parent in the institutions view
+    // this makes hierarchy maintenance a lot easier since the investment accounts
+    // are the only ones that always have the same institution as their parent
+    QStandardItem *parentAccounItem = account.isInvest() ? itemFromAccountId(institutionItem, account.parentAccountId()) : 0;
     if (!item) {
       item = new QStandardItem(account.name());
       if (parentAccounItem) {
@@ -876,7 +879,18 @@ void InstitutionsModel::slotObjectAdded(MyMoneyFile::notificationObjectT objType
   if (!account || account->parentAccountId().isEmpty() || account->isIncomeExpense())
     return;
 
+  // load the account into the institution
   static_cast<InstitutionsPrivate *>(d)->loadInstitution(this, *account);
+
+  // load the investment sub-accounts if there are any - there could be sub-accounts if this is an add operation
+  // that was triggered in slotObjectModified on an already existing account which went trough a hierarchy change
+  QList<MyMoneyAccount> subAccounts;
+  d->m_file->accountList(subAccounts, account->accountList(), true);
+  for (QList<MyMoneyAccount>::ConstIterator it_a = subAccounts.constBegin(); it_a != subAccounts.constEnd(); ++it_a) {
+    if ((*it_a).isInvest()) {
+      static_cast<InstitutionsPrivate *>(d)->loadInstitution(this, *it_a);
+    }
+  }
 }
 
 /**
-- 
1.8.1.4