Remove boost foreach macro
[novacoin.git] / src / qt / mintingtablemodel.cpp
index b77a4c8..5f82578 100644 (file)
@@ -1,4 +1,5 @@
 #include "mintingtablemodel.h"
+#include "mintingfilterproxy.h"
 #include "transactiontablemodel.h"
 #include "guiutil.h"
 #include "kernelrecord.h"
@@ -73,7 +74,7 @@ public:
             for(std::map<uint256, CWalletTx>::iterator it = wallet->mapWallet.begin(); it != wallet->mapWallet.end(); ++it)
             {
                 std::vector<KernelRecord> txList = KernelRecord::decomposeOutput(wallet, it->second);
-                BOOST_FOREACH(KernelRecord& kr, txList) {
+                for (KernelRecord& kr : txList) {
                     if(!kr.spent) {
                         cachedWallet.append(kr);
                     }
@@ -135,16 +136,17 @@ public:
                             KernelRecord::decomposeOutput(wallet, mi->second);
                     if(!toInsert.empty()) /* only if something to insert */
                     {
-                        parent->beginInsertRows(QModelIndex(), lowerIndex, lowerIndex+toInsert.size()-1);
                         int insert_idx = lowerIndex;
-                        BOOST_FOREACH(const KernelRecord &rec, toInsert)
+                        for (const KernelRecord &rec : toInsert)
                         {
-                            if(!rec.spent) {
+                            if(!rec.spent) 
+                            {
+                                parent->beginInsertRows(QModelIndex(), insert_idx, insert_idx);
                                 cachedWallet.insert(insert_idx, rec);
+                                parent->endInsertRows();
                                 insert_idx += 1;
                             }
                         }
-                        parent->endInsertRows();
                     }
                 }
                 else if(!inWallet && inModel)
@@ -156,7 +158,27 @@ public:
                 }
                 else if(inWallet && inModel)
                 {               
-                    // Updated -- nothing to do, status update will take care of this
+                    // Updated -- remove spent coins from table
+                    std::vector<KernelRecord> toCheck = KernelRecord::decomposeOutput(wallet, mi->second);
+                    for (const KernelRecord &rec : toCheck)
+                    {
+                        if(rec.spent)
+                        {
+                            for(int i = 0; i < cachedWallet.size(); i++)
+                            {
+                                KernelRecord cachedRec = cachedWallet.at(i);
+                                if((rec.hash == cachedRec.hash)
+                                    && (rec.nTime == cachedRec.nTime)
+                                    && (rec.nValue == cachedRec.nValue))
+                                {
+                                    parent->beginRemoveRows(QModelIndex(), i, i);
+                                    cachedWallet.removeAt(i);
+                                    parent->endRemoveRows();
+                                    break;
+                                }
+                            }
+                        }
+                    }
                 }
             }
         }
@@ -225,9 +247,19 @@ void MintingTableModel::update()
         TRY_LOCK(wallet->cs_wallet, lockWallet);
         if (lockWallet && !wallet->vMintingWalletUpdated.empty())
         {
-            BOOST_FOREACH(uint256 hash, wallet->vMintingWalletUpdated)
+            for (uint256 hash : wallet->vMintingWalletUpdated)
             {
                 updated.append(hash);
+
+                // Also check the inputs to remove spent outputs from the table if necessary
+                CWalletTx wtx;
+                if(wallet->GetTransaction(hash, wtx))
+                {
+                    for (const CTxIn& txin : wtx.vin)
+                    {
+                        updated.append(txin.prevout.hash);
+                    }
+                }
             }
             wallet->vMintingWalletUpdated.clear();
         }
@@ -236,9 +268,15 @@ void MintingTableModel::update()
     if(!updated.empty())
     {
         priv->updateWallet(updated);
+        mintingProxyModel->invalidate(); // Force deletion of empty rows
     }
 }
 
+void MintingTableModel::setMintingProxyModel(MintingFilterProxy *mintingProxy)
+{
+    mintingProxyModel = mintingProxy;
+}
+
 int MintingTableModel::rowCount(const QModelIndex &parent) const
 {
     Q_UNUSED(parent);
@@ -312,11 +350,11 @@ QVariant MintingTableModel::data(const QModelIndex &index, int role) const
         case TxHash:
             return formatTxHash(rec);
         case Age:
-            return rec->getAge();
+            return static_cast<qlonglong>(rec->getAge());
         case CoinDay:
-            return rec->coinAge;
+            return static_cast<qlonglong>(rec->getCoinDay());
         case Balance:
-            return rec->nValue;
+            return static_cast<qlonglong>(rec->nValue);
         case MintProbability:
             return getDayToMint(rec);
         case MintReward:
@@ -406,7 +444,7 @@ QString MintingTableModel::formatTxCoinDay(const KernelRecord *wtx) const
 
 QString MintingTableModel::formatTxAge(const KernelRecord *wtx) const
 {
-    int64 nAge = wtx->getAge();
+    int64_t nAge = wtx->getAge();
     return QString::number(nAge);
 }