text: #include "coincontroldialog.h"
  #include "ui_coincontroldialog.h"
  
  #include "init.h"
  #include "bitcoinunits.h"
  #include "walletmodel.h"
  #include "addresstablemodel.h"
  #include "optionsmodel.h"
  #include "guiutil.h"
  #include "coincontrol.h"
  
  #include <QApplication>
  #include <QCheckBox>
  #include <QClipboard>
  #include <QColor>
  #include <QCursor>
  #include <QDateTime>
  #include <QDialogButtonBox>
  #include <QFlags>
  #include <QIcon>
  #include <QString>
  #include <QTreeWidget>
  #include <QTreeWidgetItem>
  
  using namespace std;
  QList<qint64> CoinControlDialog::payAmounts;
  CCoinControl* CoinControlDialog::coinControl = new CCoinControl();
  
  CoinControlDialog::CoinControlDialog(QWidget *parent) :
      QDialog(parent),
      ui(new Ui::CoinControlDialog),
      model(0)
  {
      ui->setupUi(this);
      
  
      QAction *copyAddressAction = new QAction(tr("Copy address"), this);
      QAction *copyLabelAction = new QAction(tr("Copy label"), this);
      QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
  
  
  
  
  
      contextMenu = new QMenu();
      contextMenu->addAction(copyAddressAction);
      contextMenu->addAction(copyLabelAction);
      contextMenu->addAction(copyAmountAction);
      contextMenu->addAction(copyTransactionHashAction);
      contextMenu->addSeparator();
      contextMenu->addAction(lockAction);
      contextMenu->addAction(unlockAction);
      
  
      connect(ui->treeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu(QPoint)));
      connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
      connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
      connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
      connect(copyTransactionHashAction, SIGNAL(triggered()), this, SLOT(copyTransactionHash()));
      connect(lockAction, SIGNAL(triggered()), this, SLOT(lockCoin()));
      connect(unlockAction, SIGNAL(triggered()), this, SLOT(unlockCoin()));
      
  
      QAction *clipboardQuantityAction = new QAction(tr("Copy quantity"), this);
      QAction *clipboardAmountAction = new QAction(tr("Copy amount"), this);
      QAction *clipboardFeeAction = new QAction(tr("Copy fee"), this);
      QAction *clipboardAfterFeeAction = new QAction(tr("Copy after fee"), this);
      QAction *clipboardBytesAction = new QAction(tr("Copy bytes"), this);
      QAction *clipboardPriorityAction = new QAction(tr("Copy priority"), this);
      QAction *clipboardLowOutputAction = new QAction(tr("Copy low output"), this);
      QAction *clipboardChangeAction = new QAction(tr("Copy change"), this);
      
      connect(clipboardQuantityAction, SIGNAL(triggered()), this, SLOT(clipboardQuantity()));
      connect(clipboardAmountAction, SIGNAL(triggered()), this, SLOT(clipboardAmount()));
      connect(clipboardFeeAction, SIGNAL(triggered()), this, SLOT(clipboardFee()));
      connect(clipboardAfterFeeAction, SIGNAL(triggered()), this, SLOT(clipboardAfterFee()));
      connect(clipboardBytesAction, SIGNAL(triggered()), this, SLOT(clipboardBytes()));
      connect(clipboardPriorityAction, SIGNAL(triggered()), this, SLOT(clipboardPriority()));
      connect(clipboardLowOutputAction, SIGNAL(triggered()), this, SLOT(clipboardLowOutput()));
      connect(clipboardChangeAction, SIGNAL(triggered()), this, SLOT(clipboardChange()));
      
      ui->labelCoinControlQuantity->addAction(clipboardQuantityAction);
      ui->labelCoinControlAmount->addAction(clipboardAmountAction);
      ui->labelCoinControlFee->addAction(clipboardFeeAction);
      ui->labelCoinControlAfterFee->addAction(clipboardAfterFeeAction);
      ui->labelCoinControlBytes->addAction(clipboardBytesAction);
      ui->labelCoinControlPriority->addAction(clipboardPriorityAction);
      ui->labelCoinControlLowOutput->addAction(clipboardLowOutputAction);
      ui->labelCoinControlChange->addAction(clipboardChangeAction);
      
  
      connect(ui->radioTreeMode, SIGNAL(toggled(bool)), this, SLOT(radioTreeMode(bool)));
      connect(ui->radioListMode, SIGNAL(toggled(bool)), this, SLOT(radioListMode(bool)));
      
  
      connect(ui->treeWidget, SIGNAL(itemChanged( QTreeWidgetItem*, int)), this, SLOT(viewItemChanged( QTreeWidgetItem*, int)));
      
  
      #if QT_VERSION < 0x050000
          ui->treeWidget->header()->setClickable(true);
      #else
          ui->treeWidget->header()->setSectionsClickable(true);
      #endif
      connect(ui->treeWidget->header(), SIGNAL(sectionClicked(int)), this, SLOT(headerSectionClicked(int)));
      
  
      connect(ui->buttonBox, SIGNAL(clicked( QAbstractButton*)), this, SLOT(buttonBoxClicked(QAbstractButton*)));
      
  
      connect(ui->pushButtonSelectAll, SIGNAL(clicked()), this, SLOT(buttonSelectAllClicked()));
      
      ui->treeWidget->setColumnWidth(COLUMN_CHECKBOX, 84);
      ui->treeWidget->setColumnWidth(COLUMN_AMOUNT, 100);
      ui->treeWidget->setColumnWidth(COLUMN_LABEL, 170);
      ui->treeWidget->setColumnWidth(COLUMN_ADDRESS, 290);
      ui->treeWidget->setColumnWidth(COLUMN_DATE, 110);
      ui->treeWidget->setColumnWidth(COLUMN_CONFIRMATIONS, 100);
      ui->treeWidget->setColumnWidth(COLUMN_PRIORITY, 100);
  
  
  
  
     
  
      sortView(COLUMN_AMOUNT_INT64, Qt::DescendingOrder);
  }
  
  CoinControlDialog::~CoinControlDialog()
  {    
      delete ui;
  }
  
  void CoinControlDialog::setModel(WalletModel *model)
  {
      this->model = model;
      
      if(model && model->getOptionsModel() && model->getAddressTableModel())
      {
          updateView();
          updateLabelLocked();
          CoinControlDialog::updateLabels(model, this);
      }
  }
  
  
  QString CoinControlDialog::strPad(QString s, int nPadLength, QString sPadding)
  {
      while (s.length() < nPadLength)
          s = sPadding + s;
          
      return s;
  }
  
  
  void CoinControlDialog::buttonBoxClicked(QAbstractButton* button)
  {
      if (ui->buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
  
  }
  
  
  void CoinControlDialog::buttonSelectAllClicked()
  {
      Qt::CheckState state = Qt::Checked;
      for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
      {
          if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != Qt::Unchecked)
          {
              state = Qt::Unchecked;
              break;
          }
      }
      ui->treeWidget->setEnabled(false);
      for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
              if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != state)
                  ui->treeWidget->topLevelItem(i)->setCheckState(COLUMN_CHECKBOX, state);
      ui->treeWidget->setEnabled(true);
      if (state == Qt::Unchecked)
  
      CoinControlDialog::updateLabels(model, this);
  }
  
  
  void CoinControlDialog::showMenu(const QPoint &point)
  {
      QTreeWidgetItem *item = ui->treeWidget->itemAt(point);
      if(item)
      {
          contextMenuItem = item;
          
  
  
          {
              copyTransactionHashAction->setEnabled(true);
              if (model->isLockedCoin(uint256(item->text(COLUMN_TXHASH).toStdString()), item->text(COLUMN_VOUT_INDEX).toUInt()))
              {
                  lockAction->setEnabled(false);
                  unlockAction->setEnabled(true);
              }
              else
              {
                  lockAction->setEnabled(true);
                  unlockAction->setEnabled(false);
              }
          }
  
          {
              copyTransactionHashAction->setEnabled(false);
              lockAction->setEnabled(false);
              unlockAction->setEnabled(false);
          }
  
  
          contextMenu->exec(QCursor::pos());
      }
  }
      
  
  void CoinControlDialog::copyAmount()
  {
      GUIUtil::setClipboard(contextMenuItem->text(COLUMN_AMOUNT));
  }
  
  
  void CoinControlDialog::copyLabel()
  {
      if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_LABEL).length() == 0 && contextMenuItem->parent())
          GUIUtil::setClipboard(contextMenuItem->parent()->text(COLUMN_LABEL));
      else
          GUIUtil::setClipboard(contextMenuItem->text(COLUMN_LABEL));
  }
  
  
  void CoinControlDialog::copyAddress()
  {
      if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_ADDRESS).length() == 0 && contextMenuItem->parent())
          GUIUtil::setClipboard(contextMenuItem->parent()->text(COLUMN_ADDRESS));
      else
          GUIUtil::setClipboard(contextMenuItem->text(COLUMN_ADDRESS));
  }
  
  
  void CoinControlDialog::copyTransactionHash()
  {
      GUIUtil::setClipboard(contextMenuItem->text(COLUMN_TXHASH));
  }
  
  
  void CoinControlDialog::lockCoin()
  {
      if (contextMenuItem->checkState(COLUMN_CHECKBOX) == Qt::Checked)
          contextMenuItem->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
          
      COutPoint outpt(uint256(contextMenuItem->text(COLUMN_TXHASH).toStdString()), contextMenuItem->text(COLUMN_VOUT_INDEX).toUInt());
      model->lockCoin(outpt);
      contextMenuItem->setDisabled(true);
      contextMenuItem->setIcon(COLUMN_CHECKBOX, QIcon(":/icons/lock_closed"));
      updateLabelLocked();
  }
  
  
  void CoinControlDialog::unlockCoin()
  {
      COutPoint outpt(uint256(contextMenuItem->text(COLUMN_TXHASH).toStdString()), contextMenuItem->text(COLUMN_VOUT_INDEX).toUInt());
      model->unlockCoin(outpt);
      contextMenuItem->setDisabled(false);
      contextMenuItem->setIcon(COLUMN_CHECKBOX, QIcon());
      updateLabelLocked();
  }
  
  
  void CoinControlDialog::clipboardQuantity()
  {
      GUIUtil::setClipboard(ui->labelCoinControlQuantity->text());
  }
  
  
  void CoinControlDialog::clipboardAmount()
  {
      GUIUtil::setClipboard(ui->labelCoinControlAmount->text().left(ui->labelCoinControlAmount->text().indexOf(" ")));
  }
  
  
  void CoinControlDialog::clipboardFee()
  {
      GUIUtil::setClipboard(ui->labelCoinControlFee->text().left(ui->labelCoinControlFee->text().indexOf(" ")));
  }
  
  
  void CoinControlDialog::clipboardAfterFee()
  {
      GUIUtil::setClipboard(ui->labelCoinControlAfterFee->text().left(ui->labelCoinControlAfterFee->text().indexOf(" ")));
  }
  
  
  void CoinControlDialog::clipboardBytes()
  {
      GUIUtil::setClipboard(ui->labelCoinControlBytes->text());
  }
  
  
  void CoinControlDialog::clipboardPriority()
  {
      GUIUtil::setClipboard(ui->labelCoinControlPriority->text());
  }
  
  
  void CoinControlDialog::clipboardLowOutput()
  {
      GUIUtil::setClipboard(ui->labelCoinControlLowOutput->text());
  }
  
  
  void CoinControlDialog::clipboardChange()
  {
      GUIUtil::setClipboard(ui->labelCoinControlChange->text().left(ui->labelCoinControlChange->text().indexOf(" ")));
  }
  
  
  void CoinControlDialog::sortView(int column, Qt::SortOrder order)
  {
      sortColumn = column;
      sortOrder = order;
      ui->treeWidget->sortItems(column, order);
      ui->treeWidget->header()->setSortIndicator((sortColumn == COLUMN_AMOUNT_INT64 ? COLUMN_AMOUNT : (sortColumn == COLUMN_PRIORITY_INT64 ? COLUMN_PRIORITY : sortColumn)), sortOrder);
  }
  
  
  void CoinControlDialog::headerSectionClicked(int logicalIndex)
  {
  
      {      
          ui->treeWidget->header()->setSortIndicator((sortColumn == COLUMN_AMOUNT_INT64 ? COLUMN_AMOUNT : (sortColumn == COLUMN_PRIORITY_INT64 ? COLUMN_PRIORITY : sortColumn)), sortOrder);
      }
      else
      {
  
              logicalIndex = COLUMN_AMOUNT_INT64;
              
  
              logicalIndex = COLUMN_PRIORITY_INT64;
              
          if (sortColumn == logicalIndex)
              sortOrder = ((sortOrder == Qt::AscendingOrder) ? Qt::DescendingOrder : Qt::AscendingOrder);
          else
          {
              sortColumn = logicalIndex;
  
          }
  
          sortView(sortColumn, sortOrder);
      }
  }
  
  
  void CoinControlDialog::radioTreeMode(bool checked)
  {
      if (checked && model)
          updateView();
  }
  
  
  void CoinControlDialog::radioListMode(bool checked)
  {
      if (checked && model)
          updateView();
  }
  
  
  void CoinControlDialog::viewItemChanged(QTreeWidgetItem* item, int column)
  {
  
      { 
          COutPoint outpt(uint256(item->text(COLUMN_TXHASH).toStdString()), item->text(COLUMN_VOUT_INDEX).toUInt());
          
          if (item->checkState(COLUMN_CHECKBOX) == Qt::Unchecked)              
              coinControl->UnSelect(outpt);
  
              item->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
          else
              coinControl->Select(outpt);
   
  
  
              CoinControlDialog::updateLabels(model, this);
      }
  }
  
  
  QString CoinControlDialog::getPriorityLabel(double dPriority)
  {
  
      {
          if      (CTransaction::AllowFree(dPriority / 10000))  return tr("highest");
          else if (CTransaction::AllowFree(dPriority / 1000))   return tr("high");
          else if (CTransaction::AllowFree(dPriority / 100))    return tr("medium-high");
          else                                    return tr("medium");
      }
      else
      {
          if      (CTransaction::AllowFree(dPriority * 100))    return tr("low-medium");
          else if (CTransaction::AllowFree(dPriority * 10000))  return tr("low");
          else                                    return tr("lowest");
      }
  }
  
  
  void CoinControlDialog::updateLabelLocked()
  {
      vector<COutPoint> vOutpts;
      model->listLockedCoins(vOutpts);
      if (vOutpts.size() > 0)
      {
         ui->labelLocked->setText(tr("(%1 locked)").arg(vOutpts.size()));
         ui->labelLocked->setVisible(true); 
      }
      else ui->labelLocked->setVisible(false);
  }
  
  void CoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog)
  {
      if (!model) return;
  
  
      qint64 nPayAmount = 0;
      bool fLowOutput = false;
      bool fDust = false;
      unsigned int nQuantityDust = 0;
      CTransaction txDummy;
      foreach(const qint64 &amount, CoinControlDialog::payAmounts)
      {
          nPayAmount += amount;
          
          if (amount > 0)
          {
              if (amount < CENT) {
                  fLowOutput = true;
                  nQuantityDust++;
              }
  
              CTxOut txout(amount, (CScript)vector<unsigned char>(24, 0));
              txDummy.vout.push_back(txout);
              if (txout.IsDust())
                 fDust = true; 
          }
      }
  
      QString sPriorityLabel      = "";
      int64 nAmount               = 0;
      int64 nPayFee               = 0;
      int64 nAfterFee             = 0;
      int64 nChange               = 0;
      unsigned int nBytes         = 0;
      unsigned int nBytesInputs   = 0;
      double dPriority            = 0;
      double dPriorityInputs      = 0;
      unsigned int nQuantity      = 0;
      
      vector<COutPoint> vCoinControl;
      vector<COutput>   vOutputs;
      coinControl->ListSelected(vCoinControl);
      model->getOutputs(vCoinControl, vOutputs);
  
      BOOST_FOREACH(const COutput& out, vOutputs)
      {
  
          if (out.tx->IsSpent(out.i))
          {
              uint256 txhash = out.tx->GetHash();
              COutPoint outpt(txhash, out.i);
              coinControl->UnSelect(outpt);
              continue;
          }
  
  
          nQuantity++;
              
  
          nAmount += out.tx->vout[out.i].nValue;
          
  
          dPriorityInputs += (double)out.tx->vout[out.i].nValue * (out.nDepth+1);
          
  
          CTxDestination address;
          if(ExtractDestination(out.tx->vout[out.i].scriptPubKey, address))
          {
              CPubKey pubkey;
              CKeyID *keyid = boost::get<CKeyID>(&address);
              if (keyid && model->getPubKey(*keyid, pubkey))
                  nBytesInputs += (pubkey.IsCompressed() ? 148 : 180);
              else
  
          }
          else nBytesInputs += 148;
      }
      
  
      if (nQuantity > 0)
      {
  
  
          
  
          dPriority = dPriorityInputs / nBytes;
          sPriorityLabel = CoinControlDialog::getPriorityLabel(dPriority);
          
  
          int64 nFee = nTransactionFee * (1 + (int64)nBytes / 1000);
          
  
          int64 nMinFee = CTransaction::nMinTxFee * (1 + (int64)nBytes / 1000) + CTransaction::nMinTxFee * nQuantityDust;
          if (CTransaction::AllowFree(dPriority) && nBytes < 5000)
              nMinFee = 0;
          
          nPayFee = max(nFee, nMinFee);
          
          if (nPayAmount > 0)
          {
              nChange = nAmount - nPayFee - nPayAmount;
              
  
              if (nPayFee < CTransaction::nMinTxFee && fLowOutput)
              {
                  nChange = nChange + nPayFee - CTransaction::nMinTxFee;
                  nPayFee = CTransaction::nMinTxFee * nQuantityDust;
              }
              
  
              if (nPayFee < CTransaction::nMinTxFee && nChange > 0 && nChange < CENT)
              {
  
                  {
                      nPayFee += nChange;
                      nChange = 0;
                  }
                  else
                  {
                      nChange = nChange + nPayFee - CTransaction::nMinTxFee;
                      nPayFee = CTransaction::nMinTxFee;
                  }  
              }
              
  
              if (nChange > 0 && nChange < CENT)
              {
                  CTxOut txout(nChange, (CScript)vector<unsigned char>(24, 0));
                  if (txout.IsDust())
                  {
                      nPayFee += nChange;
                      nChange = 0;
                  }
              }
              
              if (nChange == 0)
                  nBytes -= 34;
          }
          
  
          nAfterFee = nAmount - nPayFee;
          if (nAfterFee < 0)
              nAfterFee = 0;
      }
      
  
      int nDisplayUnit = BitcoinUnits::BTC;
      if (model && model->getOptionsModel())
          nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
              
      QLabel *l1 = dialog->findChild<QLabel *>("labelCoinControlQuantity");
      QLabel *l2 = dialog->findChild<QLabel *>("labelCoinControlAmount");
      QLabel *l3 = dialog->findChild<QLabel *>("labelCoinControlFee");
      QLabel *l4 = dialog->findChild<QLabel *>("labelCoinControlAfterFee");
      QLabel *l5 = dialog->findChild<QLabel *>("labelCoinControlBytes");
      QLabel *l6 = dialog->findChild<QLabel *>("labelCoinControlPriority");
      QLabel *l7 = dialog->findChild<QLabel *>("labelCoinControlLowOutput");
      QLabel *l8 = dialog->findChild<QLabel *>("labelCoinControlChange");
      
  
      dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setEnabled(nPayAmount > 0);
      dialog->findChild<QLabel *>("labelCoinControlLowOutput")    ->setEnabled(nPayAmount > 0);
      dialog->findChild<QLabel *>("labelCoinControlChangeText")   ->setEnabled(nPayAmount > 0);
      dialog->findChild<QLabel *>("labelCoinControlChange")       ->setEnabled(nPayAmount > 0);
      
  
  
  
  
  
  
  
  
  
      
  
  
  
  
  
          
  
      l5->setToolTip(tr("This label turns red, if the transaction size is bigger than 5000 bytes.\n\n This means a fee of at least %1 per kb is required.\n\n Can vary +/- 1 Byte per input.").arg(BitcoinUnits::formatWithUnit(nDisplayUnit, CTransaction::nMinTxFee)));
      l6->setToolTip(tr("Transactions with higher priority get more likely into a block.\n\nThis label turns red, if the priority is smaller than \"medium\".\n\n This means a fee of at least %1 per kb is required.").arg(BitcoinUnits::formatWithUnit(nDisplayUnit, CTransaction::nMinTxFee)));
      l7->setToolTip(tr("This label turns red, if any recipient receives an amount smaller than %1.\n\n This means a fee of at least %2 is required. \n\n Amounts below 0.546 times the minimum relay fee are shown as DUST.").arg(BitcoinUnits::formatWithUnit(nDisplayUnit, CENT)).arg(BitcoinUnits::formatWithUnit(nDisplayUnit, CTransaction::nMinTxFee)));
      l8->setToolTip(tr("This label turns red, if the change is smaller than %1.\n\n This means a fee of at least %2 is required.").arg(BitcoinUnits::formatWithUnit(nDisplayUnit, CENT)).arg(BitcoinUnits::formatWithUnit(nDisplayUnit, CTransaction::nMinTxFee)));
      dialog->findChild<QLabel *>("labelCoinControlBytesText")    ->setToolTip(l5->toolTip());
      dialog->findChild<QLabel *>("labelCoinControlPriorityText") ->setToolTip(l6->toolTip());
      dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setToolTip(l7->toolTip());
      dialog->findChild<QLabel *>("labelCoinControlChangeText")   ->setToolTip(l8->toolTip());
     
  
      QLabel *label = dialog->findChild<QLabel *>("labelCoinControlInsuffFunds");
      if (label)
          label->setVisible(nChange < 0);
  }
  
  void CoinControlDialog::updateView()
  {
      bool treeMode = ui->radioTreeMode->isChecked();
  
      ui->treeWidget->clear();
  
      ui->treeWidget->setAlternatingRowColors(!treeMode);
      QFlags<Qt::ItemFlag> flgCheckbox=Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
      QFlags<Qt::ItemFlag> flgTristate=Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsTristate;    
      
      int nDisplayUnit = BitcoinUnits::BTC;
      if (model && model->getOptionsModel())
          nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
          
      map<QString, vector<COutput> > mapCoins;
      model->listCoins(mapCoins);
  
      BOOST_FOREACH(PAIRTYPE(QString, vector<COutput>) coins, mapCoins)
      {
          QTreeWidgetItem *itemWalletAddress = new QTreeWidgetItem();
          QString sWalletAddress = coins.first;
          QString sWalletLabel = "";
          if (model->getAddressTableModel())
              sWalletLabel = model->getAddressTableModel()->labelForAddress(sWalletAddress);
          if (sWalletLabel.length() == 0)
              sWalletLabel = tr("(no label)");
          
          if (treeMode)
          {
  
              ui->treeWidget->addTopLevelItem(itemWalletAddress);
  
              itemWalletAddress->setFlags(flgTristate);
              itemWalletAddress->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
              
              for (int i = 0; i < ui->treeWidget->columnCount(); i++)
                  itemWalletAddress->setBackground(i, QColor(248, 247, 246));
              
  
              itemWalletAddress->setText(COLUMN_LABEL, sWalletLabel);
  
  
              itemWalletAddress->setText(COLUMN_ADDRESS, sWalletAddress);
          }
  
          int64 nSum = 0;
          double dPrioritySum = 0;
          int nChildren = 0;
          int nInputSum = 0;
          BOOST_FOREACH(const COutput& out, coins.second)
          {
  
              nSum += out.tx->vout[out.i].nValue;
              nChildren++;
              
              QTreeWidgetItem *itemOutput;
              if (treeMode)    itemOutput = new QTreeWidgetItem(itemWalletAddress);
              else             itemOutput = new QTreeWidgetItem(ui->treeWidget);
              itemOutput->setFlags(flgCheckbox);
              itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
                  
  
              CTxDestination outputAddress;
              QString sAddress = "";
              if(ExtractDestination(out.tx->vout[out.i].scriptPubKey, outputAddress))
              {
                  sAddress = CBitcoinAddress(outputAddress).ToString().c_str();
                  
  
                  if (!treeMode || (!(sAddress == sWalletAddress)))
                      itemOutput->setText(COLUMN_ADDRESS, sAddress);
                      
                  CPubKey pubkey;
                  CKeyID *keyid = boost::get<CKeyID>(&outputAddress);
                  if (keyid && model->getPubKey(*keyid, pubkey) && !pubkey.IsCompressed())
                      nInputSize = 180;
              }
  
  
  
              {
  
                  itemOutput->setToolTip(COLUMN_LABEL, tr("change from %1 (%2)").arg(sWalletLabel).arg(sWalletAddress));
                  itemOutput->setText(COLUMN_LABEL, tr("(change)"));
              }
              else if (!treeMode)
              {
                  QString sLabel = "";
                  if (model->getAddressTableModel())
                      sLabel = model->getAddressTableModel()->labelForAddress(sAddress);
                  if (sLabel.length() == 0)
                      sLabel = tr("(no label)");
                  itemOutput->setText(COLUMN_LABEL, sLabel); 
              }
  
  
              itemOutput->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, out.tx->vout[out.i].nValue));
  
  
  
              itemOutput->setText(COLUMN_DATE, QDateTime::fromTime_t(out.tx->GetTxTime()).toString("yy-MM-dd hh:mm"));
              
  
              itemOutput->setText(COLUMN_CONFIRMATIONS, strPad(QString::number(out.nDepth), 8, " "));
              
  
  
              itemOutput->setText(COLUMN_PRIORITY, CoinControlDialog::getPriorityLabel(dPriority));
              itemOutput->setText(COLUMN_PRIORITY_INT64, strPad(QString::number((int64)dPriority), 20, " "));
              dPrioritySum += (double)out.tx->vout[out.i].nValue  * (out.nDepth+1);
              nInputSum    += nInputSize;
              
  
              uint256 txhash = out.tx->GetHash();
              itemOutput->setText(COLUMN_TXHASH, txhash.GetHex().c_str());
      
  
              itemOutput->setText(COLUMN_VOUT_INDEX, QString::number(out.i));
              
  
              if (model->isLockedCoin(txhash, out.i))
              {
                  COutPoint outpt(txhash, out.i);
  
                  itemOutput->setDisabled(true);
                  itemOutput->setIcon(COLUMN_CHECKBOX, QIcon(":/icons/lock_closed"));
              }
                
  
              if (coinControl->IsSelected(txhash, out.i))
                  itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Checked);
          }
  
  
          if (treeMode)
          {
              dPrioritySum = dPrioritySum / (nInputSum + 78);
              itemWalletAddress->setText(COLUMN_CHECKBOX, "(" + QString::number(nChildren) + ")");
              itemWalletAddress->setText(COLUMN_AMOUNT, BitcoinUnits::format(nDisplayUnit, nSum));
              itemWalletAddress->setText(COLUMN_AMOUNT_INT64, strPad(QString::number(nSum), 15, " "));
              itemWalletAddress->setText(COLUMN_PRIORITY, CoinControlDialog::getPriorityLabel(dPrioritySum));
              itemWalletAddress->setText(COLUMN_PRIORITY_INT64, strPad(QString::number((int64)dPrioritySum), 20, " "));
          }
      }
      
  
      if (treeMode)
      {
          for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
              if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
                  ui->treeWidget->topLevelItem(i)->setExpanded(true);
      }
      
  
      sortView(sortColumn, sortOrder);
      ui->treeWidget->setEnabled(true);
  }
  