2013-11-16 17:37:31 +01:00
// Copyright (c) 2011-2013 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2013-08-12 17:03:03 +02:00
# include "coincontroldialog.h"
# include "ui_coincontroldialog.h"
# include "addresstablemodel.h"
2013-11-16 17:37:31 +01:00
# include "bitcoinunits.h"
2013-08-12 17:03:03 +02:00
# include "guiutil.h"
2013-11-16 17:37:31 +01:00
# include "init.h"
# include "optionsmodel.h"
# include "walletmodel.h"
2013-08-12 17:03:03 +02:00
# include "coincontrol.h"
# include "main.h"
# include "wallet.h"
2014-05-27 21:44:57 +02:00
# include <boost/assign/list_of.hpp> // for 'map_list_of()'
2013-08-12 17:03:03 +02:00
# include <QApplication>
# include <QCheckBox>
# include <QCursor>
# include <QDialogButtonBox>
# include <QFlags>
# include <QIcon>
# include <QString>
# include <QTreeWidget>
# include <QTreeWidgetItem>
using namespace std ;
2014-04-23 00:46:19 +02:00
QList < CAmount > CoinControlDialog : : payAmounts ;
2013-08-12 17:03:03 +02:00
CCoinControl * CoinControlDialog : : coinControl = new CCoinControl ( ) ;
CoinControlDialog : : CoinControlDialog ( QWidget * parent ) :
QDialog ( parent ) ,
ui ( new Ui : : CoinControlDialog ) ,
model ( 0 )
{
ui - > setupUi ( this ) ;
// context menu actions
QAction * copyAddressAction = new QAction ( tr ( " Copy address " ) , this ) ;
QAction * copyLabelAction = new QAction ( tr ( " Copy label " ) , this ) ;
QAction * copyAmountAction = new QAction ( tr ( " Copy amount " ) , this ) ;
copyTransactionHashAction = new QAction ( tr ( " Copy transaction ID " ) , this ) ; // we need to enable/disable this
lockAction = new QAction ( tr ( " Lock unspent " ) , this ) ; // we need to enable/disable this
unlockAction = new QAction ( tr ( " Unlock unspent " ) , this ) ; // we need to enable/disable this
// context menu
contextMenu = new QMenu ( ) ;
contextMenu - > addAction ( copyAddressAction ) ;
contextMenu - > addAction ( copyLabelAction ) ;
contextMenu - > addAction ( copyAmountAction ) ;
contextMenu - > addAction ( copyTransactionHashAction ) ;
contextMenu - > addSeparator ( ) ;
contextMenu - > addAction ( lockAction ) ;
contextMenu - > addAction ( unlockAction ) ;
// context menu signals
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 ( ) ) ) ;
// clipboard actions
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 ) ;
2014-06-08 01:05:53 +02:00
QAction * clipboardLowOutputAction = new QAction ( tr ( " Copy dust " ) , this ) ;
2013-08-12 17:03:03 +02:00
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 ) ;
// toggle tree/list mode
connect ( ui - > radioTreeMode , SIGNAL ( toggled ( bool ) ) , this , SLOT ( radioTreeMode ( bool ) ) ) ;
connect ( ui - > radioListMode , SIGNAL ( toggled ( bool ) ) , this , SLOT ( radioListMode ( bool ) ) ) ;
// click on checkbox
2014-04-01 10:08:35 +02:00
connect ( ui - > treeWidget , SIGNAL ( itemChanged ( QTreeWidgetItem * , int ) ) , this , SLOT ( viewItemChanged ( QTreeWidgetItem * , int ) ) ) ;
2013-08-12 17:03:03 +02:00
// click on header
2013-11-16 17:37:31 +01:00
# if QT_VERSION < 0x050000
2013-08-12 17:03:03 +02:00
ui - > treeWidget - > header ( ) - > setClickable ( true ) ;
2013-11-16 17:37:31 +01:00
# else
ui - > treeWidget - > header ( ) - > setSectionsClickable ( true ) ;
# endif
2013-08-12 17:03:03 +02:00
connect ( ui - > treeWidget - > header ( ) , SIGNAL ( sectionClicked ( int ) ) , this , SLOT ( headerSectionClicked ( int ) ) ) ;
// ok button
connect ( ui - > buttonBox , SIGNAL ( clicked ( QAbstractButton * ) ) , this , SLOT ( buttonBoxClicked ( QAbstractButton * ) ) ) ;
// (un)select all
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 ) ;
ui - > treeWidget - > setColumnHidden ( COLUMN_TXHASH , true ) ; // store transacton hash in this column, but dont show it
ui - > treeWidget - > setColumnHidden ( COLUMN_VOUT_INDEX , true ) ; // store vout index in this column, but dont show it
ui - > treeWidget - > setColumnHidden ( COLUMN_AMOUNT_INT64 , true ) ; // store amount int64 in this column, but dont show it
ui - > treeWidget - > setColumnHidden ( COLUMN_PRIORITY_INT64 , true ) ; // store priority int64 in this column, but dont show it
2014-03-03 04:16:42 +01:00
ui - > treeWidget - > setColumnHidden ( COLUMN_DATE_INT64 , true ) ; // store date int64 in this column, but dont show it
2013-08-12 17:03:03 +02:00
// default view is sorted by amount desc
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 ) ;
}
}
// helper function str_pad
QString CoinControlDialog : : strPad ( QString s , int nPadLength , QString sPadding )
{
while ( s . length ( ) < nPadLength )
s = sPadding + s ;
return s ;
}
// ok button
void CoinControlDialog : : buttonBoxClicked ( QAbstractButton * button )
{
if ( ui - > buttonBox - > buttonRole ( button ) = = QDialogButtonBox : : AcceptRole )
done ( QDialog : : Accepted ) ; // closes the dialog
}
// (un)select all
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 )
coinControl - > UnSelectAll ( ) ; // just to be sure
CoinControlDialog : : updateLabels ( model , this ) ;
}
// context menu
void CoinControlDialog : : showMenu ( const QPoint & point )
{
QTreeWidgetItem * item = ui - > treeWidget - > itemAt ( point ) ;
if ( item )
{
contextMenuItem = item ;
// disable some items (like Copy Transaction ID, lock, unlock) for tree roots in context menu
if ( item - > text ( COLUMN_TXHASH ) . length ( ) = = 64 ) // transaction hash is 64 characters (this means its a child node, so its not a parent node in tree mode)
{
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 ) ;
}
}
else // this means click on parent node in tree mode -> disable all
{
copyTransactionHashAction - > setEnabled ( false ) ;
lockAction - > setEnabled ( false ) ;
unlockAction - > setEnabled ( false ) ;
}
// show context menu
contextMenu - > exec ( QCursor : : pos ( ) ) ;
}
}
// context menu action: copy amount
void CoinControlDialog : : copyAmount ( )
{
2014-07-20 04:17:02 +02:00
GUIUtil : : setClipboard ( BitcoinUnits : : removeSpaces ( contextMenuItem - > text ( COLUMN_AMOUNT ) ) ) ;
2013-08-12 17:03:03 +02:00
}
// context menu action: copy label
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 ) ) ;
}
// context menu action: copy address
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 ) ) ;
}
// context menu action: copy transaction id
void CoinControlDialog : : copyTransactionHash ( )
{
GUIUtil : : setClipboard ( contextMenuItem - > text ( COLUMN_TXHASH ) ) ;
}
// context menu action: lock coin
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 ( ) ;
}
// context menu action: unlock coin
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 ( ) ;
}
// copy label "Quantity" to clipboard
void CoinControlDialog : : clipboardQuantity ( )
{
GUIUtil : : setClipboard ( ui - > labelCoinControlQuantity - > text ( ) ) ;
}
// copy label "Amount" to clipboard
void CoinControlDialog : : clipboardAmount ( )
{
GUIUtil : : setClipboard ( ui - > labelCoinControlAmount - > text ( ) . left ( ui - > labelCoinControlAmount - > text ( ) . indexOf ( " " ) ) ) ;
}
// copy label "Fee" to clipboard
void CoinControlDialog : : clipboardFee ( )
{
GUIUtil : : setClipboard ( ui - > labelCoinControlFee - > text ( ) . left ( ui - > labelCoinControlFee - > text ( ) . indexOf ( " " ) ) ) ;
}
// copy label "After fee" to clipboard
void CoinControlDialog : : clipboardAfterFee ( )
{
GUIUtil : : setClipboard ( ui - > labelCoinControlAfterFee - > text ( ) . left ( ui - > labelCoinControlAfterFee - > text ( ) . indexOf ( " " ) ) ) ;
}
// copy label "Bytes" to clipboard
void CoinControlDialog : : clipboardBytes ( )
{
GUIUtil : : setClipboard ( ui - > labelCoinControlBytes - > text ( ) ) ;
}
// copy label "Priority" to clipboard
void CoinControlDialog : : clipboardPriority ( )
{
GUIUtil : : setClipboard ( ui - > labelCoinControlPriority - > text ( ) ) ;
}
2014-06-08 01:05:53 +02:00
// copy label "Dust" to clipboard
2013-08-12 17:03:03 +02:00
void CoinControlDialog : : clipboardLowOutput ( )
{
GUIUtil : : setClipboard ( ui - > labelCoinControlLowOutput - > text ( ) ) ;
}
// copy label "Change" to clipboard
void CoinControlDialog : : clipboardChange ( )
{
GUIUtil : : setClipboard ( ui - > labelCoinControlChange - > text ( ) . left ( ui - > labelCoinControlChange - > text ( ) . indexOf ( " " ) ) ) ;
}
// treeview: sort
void CoinControlDialog : : sortView ( int column , Qt : : SortOrder order )
{
sortColumn = column ;
sortOrder = order ;
ui - > treeWidget - > sortItems ( column , order ) ;
2014-03-03 04:16:42 +01:00
ui - > treeWidget - > header ( ) - > setSortIndicator ( getMappedColumn ( sortColumn ) , sortOrder ) ;
2013-08-12 17:03:03 +02:00
}
// treeview: clicked on header
void CoinControlDialog : : headerSectionClicked ( int logicalIndex )
{
if ( logicalIndex = = COLUMN_CHECKBOX ) // click on most left column -> do nothing
{
2014-03-03 04:16:42 +01:00
ui - > treeWidget - > header ( ) - > setSortIndicator ( getMappedColumn ( sortColumn ) , sortOrder ) ;
2013-08-12 17:03:03 +02:00
}
else
{
2014-03-03 04:16:42 +01:00
logicalIndex = getMappedColumn ( logicalIndex , false ) ;
2013-08-12 17:03:03 +02:00
if ( sortColumn = = logicalIndex )
sortOrder = ( ( sortOrder = = Qt : : AscendingOrder ) ? Qt : : DescendingOrder : Qt : : AscendingOrder ) ;
else
{
sortColumn = logicalIndex ;
2014-03-03 04:16:42 +01:00
sortOrder = ( ( sortColumn = = COLUMN_LABEL | | sortColumn = = COLUMN_ADDRESS ) ? Qt : : AscendingOrder : Qt : : DescendingOrder ) ; // if label or address then default => asc, else default => desc
2013-08-12 17:03:03 +02:00
}
sortView ( sortColumn , sortOrder ) ;
}
}
// toggle tree mode
void CoinControlDialog : : radioTreeMode ( bool checked )
{
if ( checked & & model )
updateView ( ) ;
}
// toggle list mode
void CoinControlDialog : : radioListMode ( bool checked )
{
if ( checked & & model )
updateView ( ) ;
}
// checkbox clicked by user
void CoinControlDialog : : viewItemChanged ( QTreeWidgetItem * item , int column )
{
if ( column = = COLUMN_CHECKBOX & & item - > text ( COLUMN_TXHASH ) . length ( ) = = 64 ) // transaction hash is 64 characters (this means its a child node, so its not a parent node in tree mode)
{
COutPoint outpt ( uint256 ( item - > text ( COLUMN_TXHASH ) . toStdString ( ) ) , item - > text ( COLUMN_VOUT_INDEX ) . toUInt ( ) ) ;
if ( item - > checkState ( COLUMN_CHECKBOX ) = = Qt : : Unchecked )
coinControl - > UnSelect ( outpt ) ;
else if ( item - > isDisabled ( ) ) // locked (this happens if "check all" through parent node)
item - > setCheckState ( COLUMN_CHECKBOX , Qt : : Unchecked ) ;
else
coinControl - > Select ( outpt ) ;
// selection changed -> update labels
if ( ui - > treeWidget - > isEnabled ( ) ) // do not update on every click for (un)select all
CoinControlDialog : : updateLabels ( model , this ) ;
}
2014-03-11 13:42:00 +01:00
// todo: this is a temporary qt5 fix: when clicking a parent node in tree mode, the parent node
// including all childs are partially selected. But the parent node should be fully selected
// as well as the childs. Childs should never be partially selected in the first place.
// Please remove this ugly fix, once the bug is solved upstream.
# if QT_VERSION >= 0x050000
else if ( column = = COLUMN_CHECKBOX & & item - > childCount ( ) > 0 )
{
if ( item - > checkState ( COLUMN_CHECKBOX ) = = Qt : : PartiallyChecked & & item - > child ( 0 ) - > checkState ( COLUMN_CHECKBOX ) = = Qt : : PartiallyChecked )
item - > setCheckState ( COLUMN_CHECKBOX , Qt : : Checked ) ;
}
# endif
2013-08-12 17:03:03 +02:00
}
// return human readable label for priority number
2014-05-27 21:44:57 +02:00
QString CoinControlDialog : : getPriorityLabel ( const CTxMemPool & pool , double dPriority )
2013-08-12 17:03:03 +02:00
{
2014-05-27 21:44:57 +02:00
// confirmations -> textual description
typedef std : : map < unsigned int , QString > PriorityDescription ;
2014-07-03 20:25:32 +02:00
const static PriorityDescription priorityDescriptions = boost : : assign : : map_list_of
2014-05-27 21:44:57 +02:00
( 1 , tr ( " highest " ) ) ( 2 , tr ( " higher " ) ) ( 3 , tr ( " high " ) )
( 5 , tr ( " medium-high " ) ) ( 6 , tr ( " medium " ) )
( 10 , tr ( " low-medium " ) ) ( 15 , tr ( " low " ) )
( 20 , tr ( " lower " ) ) ;
BOOST_FOREACH ( const PriorityDescription : : value_type & i , priorityDescriptions )
2013-08-12 17:03:03 +02:00
{
2014-05-27 21:44:57 +02:00
double p = mempool . estimatePriority ( i . first ) ;
if ( p > 0 & & dPriority > = p ) return i . second ;
2013-08-12 17:03:03 +02:00
}
2014-05-27 21:44:57 +02:00
// Note: if mempool hasn't accumulated enough history (estimatePriority
// returns -1) we're conservative and classify as "lowest"
2014-07-05 07:30:06 +02:00
if ( mempool . estimatePriority ( nTxConfirmTarget ) < = 0 & & AllowFree ( dPriority ) )
return " >= " + tr ( " medium " ) ;
2014-05-27 21:44:57 +02:00
return tr ( " lowest " ) ;
2013-08-12 17:03:03 +02:00
}
// shows count of locked unspent outputs
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 )
{
2014-04-01 10:08:35 +02:00
if ( ! model )
return ;
2013-08-12 17:03:03 +02:00
// nPayAmount
2014-04-23 00:46:19 +02:00
CAmount nPayAmount = 0 ;
2013-08-12 17:03:03 +02:00
bool fDust = false ;
2014-06-07 13:53:27 +02:00
CMutableTransaction txDummy ;
2014-04-23 00:46:19 +02:00
foreach ( const CAmount & amount , CoinControlDialog : : payAmounts )
2013-08-12 17:03:03 +02:00
{
nPayAmount + = amount ;
if ( amount > 0 )
{
CTxOut txout ( amount , ( CScript ) vector < unsigned char > ( 24 , 0 ) ) ;
txDummy . vout . push_back ( txout ) ;
2014-07-03 20:25:32 +02:00
if ( txout . IsDust ( : : minRelayTxFee ) )
2013-08-12 17:03:03 +02:00
fDust = true ;
}
}
2013-12-11 15:12:13 +01:00
QString sPriorityLabel = tr ( " none " ) ;
2014-04-23 00:46:19 +02:00
CAmount nAmount = 0 ;
CAmount nPayFee = 0 ;
CAmount nAfterFee = 0 ;
CAmount nChange = 0 ;
2013-08-12 17:03:03 +02:00
unsigned int nBytes = 0 ;
unsigned int nBytesInputs = 0 ;
double dPriority = 0 ;
double dPriorityInputs = 0 ;
unsigned int nQuantity = 0 ;
int nQuantityUncompressed = 0 ;
vector < COutPoint > vCoinControl ;
vector < COutput > vOutputs ;
coinControl - > ListSelected ( vCoinControl ) ;
model - > getOutputs ( vCoinControl , vOutputs ) ;
BOOST_FOREACH ( const COutput & out , vOutputs )
{
2014-02-15 22:38:28 +01:00
// unselect already spent, very unlikely scenario, this could happen
// when selected are spent elsewhere, like rpc or another computer
uint256 txhash = out . tx - > GetHash ( ) ;
COutPoint outpt ( txhash , out . i ) ;
if ( model - > isSpent ( outpt ) )
2013-08-12 17:03:03 +02:00
{
coinControl - > UnSelect ( outpt ) ;
continue ;
}
// Quantity
nQuantity + + ;
// Amount
nAmount + = out . tx - > vout [ out . i ] . nValue ;
// Priority
dPriorityInputs + = ( double ) out . tx - > vout [ out . i ] . nValue * ( out . nDepth + 1 ) ;
// Bytes
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 ) ;
if ( ! pubkey . IsCompressed ( ) )
nQuantityUncompressed + + ;
}
else
nBytesInputs + = 148 ; // in all error cases, simply assume 148 here
}
else nBytesInputs + = 148 ;
}
// calculation
if ( nQuantity > 0 )
{
// Bytes
nBytes = nBytesInputs + ( ( CoinControlDialog : : payAmounts . size ( ) > 0 ? CoinControlDialog : : payAmounts . size ( ) + 1 : 2 ) * 34 ) + 10 ; // always assume +1 output for change here
// Priority
dPriority = dPriorityInputs / ( nBytes - nBytesInputs + ( nQuantityUncompressed * 29 ) ) ; // 29 = 180 - 151 (uncompressed public keys are over the limit. max 151 bytes of the input are ignored for priority)
2014-05-27 21:44:57 +02:00
sPriorityLabel = CoinControlDialog : : getPriorityLabel ( mempool , dPriority ) ;
2013-08-12 17:03:03 +02:00
2014-07-05 07:30:06 +02:00
// Voluntary Fee
nPayFee = payTxFee . GetFee ( max ( ( unsigned int ) 1000 , nBytes ) ) ;
2013-08-12 17:03:03 +02:00
// Min Fee
2014-07-05 07:30:06 +02:00
if ( nPayFee = = 0 )
{
nPayFee = CWallet : : GetMinimumFee ( nBytes , nTxConfirmTarget , mempool ) ;
2014-05-27 21:44:57 +02:00
2014-07-05 07:30:06 +02:00
double dPriorityNeeded = mempool . estimatePriority ( nTxConfirmTarget ) ;
if ( dPriorityNeeded < = 0 & & ! AllowFree ( dPriority ) ) // not enough mempool history: never send free
dPriorityNeeded = std : : numeric_limits < double > : : max ( ) ;
2013-08-12 17:03:03 +02:00
2014-07-05 07:30:06 +02:00
if ( nBytes < = MAX_FREE_TRANSACTION_CREATE_SIZE & & dPriority > = dPriorityNeeded )
nPayFee = 0 ;
}
2013-08-12 17:03:03 +02:00
if ( nPayAmount > 0 )
{
nChange = nAmount - nPayFee - nPayAmount ;
// Never create dust outputs; if we would, just add the dust to the fee.
if ( nChange > 0 & & nChange < CENT )
{
CTxOut txout ( nChange , ( CScript ) vector < unsigned char > ( 24 , 0 ) ) ;
2014-07-03 20:25:32 +02:00
if ( txout . IsDust ( : : minRelayTxFee ) )
2013-08-12 17:03:03 +02:00
{
nPayFee + = nChange ;
nChange = 0 ;
}
}
if ( nChange = = 0 )
nBytes - = 34 ;
}
// after fee
nAfterFee = nAmount - nPayFee ;
if ( nAfterFee < 0 )
nAfterFee = 0 ;
}
// actually update labels
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 " ) ;
2014-06-08 01:05:53 +02:00
// enable/disable "dust" and "change"
2013-08-12 17:03:03 +02:00
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 ) ;
// stats
l1 - > setText ( QString : : number ( nQuantity ) ) ; // Quantity
l2 - > setText ( BitcoinUnits : : formatWithUnit ( nDisplayUnit , nAmount ) ) ; // Amount
l3 - > setText ( BitcoinUnits : : formatWithUnit ( nDisplayUnit , nPayFee ) ) ; // Fee
l4 - > setText ( BitcoinUnits : : formatWithUnit ( nDisplayUnit , nAfterFee ) ) ; // After Fee
l5 - > setText ( ( ( nBytes > 0 ) ? " ~ " : " " ) + QString : : number ( nBytes ) ) ; // Bytes
l6 - > setText ( sPriorityLabel ) ; // Priority
2014-06-08 01:05:53 +02:00
l7 - > setText ( fDust ? tr ( " yes " ) : tr ( " no " ) ) ; // Dust
2013-08-12 17:03:03 +02:00
l8 - > setText ( BitcoinUnits : : formatWithUnit ( nDisplayUnit , nChange ) ) ; // Change
2014-06-11 13:58:16 +02:00
if ( nPayFee > 0 )
{
l3 - > setText ( " ~ " + l3 - > text ( ) ) ;
l4 - > setText ( " ~ " + l4 - > text ( ) ) ;
if ( nChange > 0 )
l8 - > setText ( " ~ " + l8 - > text ( ) ) ;
}
2013-08-12 17:03:03 +02:00
// turn labels "red"
2014-05-27 21:44:57 +02:00
l5 - > setStyleSheet ( ( nBytes > = MAX_FREE_TRANSACTION_CREATE_SIZE ) ? " color:red; " : " " ) ; // Bytes >= 1000
2013-12-11 15:12:13 +01:00
l6 - > setStyleSheet ( ( dPriority > 0 & & ! AllowFree ( dPriority ) ) ? " color:red; " : " " ) ; // Priority < "medium"
2014-06-08 01:05:53 +02:00
l7 - > setStyleSheet ( ( fDust ) ? " color:red; " : " " ) ; // Dust = "yes"
2013-08-12 17:03:03 +02:00
// tool tips
2013-12-02 13:36:19 +01:00
QString toolTip1 = tr ( " This label turns red, if the transaction size is greater than 1000 bytes. " ) + " <br /><br /> " ;
2014-07-03 20:25:32 +02:00
toolTip1 + = tr ( " This means a fee of at least %1 per kB is required. " ) . arg ( BitcoinUnits : : formatWithUnit ( nDisplayUnit , CWallet : : minTxFee . GetFeePerK ( ) ) ) + " <br /><br /> " ;
2013-12-02 13:36:19 +01:00
toolTip1 + = tr ( " Can vary +/- 1 byte per input. " ) ;
QString toolTip2 = tr ( " Transactions with higher priority are more likely to get included into a block. " ) + " <br /><br /> " ;
2013-12-11 15:12:13 +01:00
toolTip2 + = tr ( " This label turns red, if the priority is smaller than \" medium \" . " ) + " <br /><br /> " ;
2014-07-03 20:25:32 +02:00
toolTip2 + = tr ( " This means a fee of at least %1 per kB is required. " ) . arg ( BitcoinUnits : : formatWithUnit ( nDisplayUnit , CWallet : : minTxFee . GetFeePerK ( ) ) ) ;
2013-12-02 13:36:19 +01:00
2014-07-03 20:25:32 +02:00
QString toolTip3 = tr ( " This label turns red, if any recipient receives an amount smaller than %1. " ) . arg ( BitcoinUnits : : formatWithUnit ( nDisplayUnit , : : minRelayTxFee . GetFee ( 546 ) ) ) ;
2013-12-02 13:36:19 +01:00
2014-06-11 13:58:16 +02:00
// how many satoshis the estimated fee can vary per byte we guess wrong
2014-07-05 07:30:06 +02:00
double dFeeVary = ( double ) std : : max ( CWallet : : minTxFee . GetFeePerK ( ) , std : : max ( payTxFee . GetFeePerK ( ) , mempool . estimateFee ( nTxConfirmTarget ) . GetFeePerK ( ) ) ) / 1000 ;
2014-06-11 13:58:16 +02:00
QString toolTip4 = tr ( " Can vary +/- %1 satoshi(s) per input. " ) . arg ( dFeeVary ) ;
l3 - > setToolTip ( toolTip4 ) ;
l4 - > setToolTip ( toolTip4 ) ;
2013-12-02 13:36:19 +01:00
l5 - > setToolTip ( toolTip1 ) ;
l6 - > setToolTip ( toolTip2 ) ;
l7 - > setToolTip ( toolTip3 ) ;
2014-06-11 13:58:16 +02:00
l8 - > setToolTip ( toolTip4 ) ;
dialog - > findChild < QLabel * > ( " labelCoinControlFeeText " ) - > setToolTip ( l3 - > toolTip ( ) ) ;
dialog - > findChild < QLabel * > ( " labelCoinControlAfterFeeText " ) - > setToolTip ( l4 - > toolTip ( ) ) ;
2013-08-12 17:03:03 +02:00
dialog - > findChild < QLabel * > ( " labelCoinControlBytesText " ) - > setToolTip ( l5 - > toolTip ( ) ) ;
dialog - > findChild < QLabel * > ( " labelCoinControlPriorityText " ) - > setToolTip ( l6 - > toolTip ( ) ) ;
dialog - > findChild < QLabel * > ( " labelCoinControlLowOutputText " ) - > setToolTip ( l7 - > toolTip ( ) ) ;
2014-06-11 13:58:16 +02:00
dialog - > findChild < QLabel * > ( " labelCoinControlChangeText " ) - > setToolTip ( l8 - > toolTip ( ) ) ;
2013-08-12 17:03:03 +02:00
// Insufficient funds
QLabel * label = dialog - > findChild < QLabel * > ( " labelCoinControlInsuffFunds " ) ;
if ( label )
label - > setVisible ( nChange < 0 ) ;
}
void CoinControlDialog : : updateView ( )
{
2014-04-01 10:08:35 +02:00
if ( ! model | | ! model - > getOptionsModel ( ) | | ! model - > getAddressTableModel ( ) )
return ;
2013-08-12 17:03:03 +02:00
bool treeMode = ui - > radioTreeMode - > isChecked ( ) ;
ui - > treeWidget - > clear ( ) ;
ui - > treeWidget - > setEnabled ( false ) ; // performance, otherwise updateLabels would be called for every checked checkbox
ui - > treeWidget - > setAlternatingRowColors ( ! treeMode ) ;
2014-04-01 10:08:35 +02:00
QFlags < Qt : : ItemFlag > flgCheckbox = Qt : : ItemIsSelectable | Qt : : ItemIsEnabled | Qt : : ItemIsUserCheckable ;
QFlags < Qt : : ItemFlag > flgTristate = Qt : : ItemIsSelectable | Qt : : ItemIsEnabled | Qt : : ItemIsUserCheckable | Qt : : ItemIsTristate ;
2013-08-12 17:03:03 +02:00
2014-04-01 10:08:35 +02:00
int nDisplayUnit = model - > getOptionsModel ( ) - > getDisplayUnit ( ) ;
2013-08-12 17:03:03 +02:00
map < QString , vector < COutput > > mapCoins ;
model - > listCoins ( mapCoins ) ;
BOOST_FOREACH ( PAIRTYPE ( QString , vector < COutput > ) coins , mapCoins )
{
QTreeWidgetItem * itemWalletAddress = new QTreeWidgetItem ( ) ;
2014-04-01 10:08:35 +02:00
itemWalletAddress - > setCheckState ( COLUMN_CHECKBOX , Qt : : Unchecked ) ;
2013-08-12 17:03:03 +02:00
QString sWalletAddress = coins . first ;
2014-04-01 10:08:35 +02:00
QString sWalletLabel = model - > getAddressTableModel ( ) - > labelForAddress ( sWalletAddress ) ;
if ( sWalletLabel . isEmpty ( ) )
2013-08-12 17:03:03 +02:00
sWalletLabel = tr ( " (no label) " ) ;
if ( treeMode )
{
// wallet address
ui - > treeWidget - > addTopLevelItem ( itemWalletAddress ) ;
itemWalletAddress - > setFlags ( flgTristate ) ;
2014-04-01 10:08:35 +02:00
itemWalletAddress - > setCheckState ( COLUMN_CHECKBOX , Qt : : Unchecked ) ;
2013-08-12 17:03:03 +02:00
// label
itemWalletAddress - > setText ( COLUMN_LABEL , sWalletLabel ) ;
// address
itemWalletAddress - > setText ( COLUMN_ADDRESS , sWalletAddress ) ;
}
2014-04-23 00:46:19 +02:00
CAmount nSum = 0 ;
2013-08-12 17:03:03 +02:00
double dPrioritySum = 0 ;
int nChildren = 0 ;
int nInputSum = 0 ;
BOOST_FOREACH ( const COutput & out , coins . second )
{
int nInputSize = 0 ;
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 ) ;
// address
CTxDestination outputAddress ;
QString sAddress = " " ;
if ( ExtractDestination ( out . tx - > vout [ out . i ] . scriptPubKey , outputAddress ) )
{
2014-09-08 12:25:52 +02:00
sAddress = QString : : fromStdString ( CBitcoinAddress ( outputAddress ) . ToString ( ) ) ;
2013-08-12 17:03:03 +02:00
// if listMode or change => show bitcoin address. In tree mode, address is not shown again for direct wallet address outputs
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 = 29 ; // 29 = 180 - 151 (public key is 180 bytes, priority free area is 151 bytes)
}
// label
if ( ! ( sAddress = = sWalletAddress ) ) // change
{
// tooltip from where the change comes from
itemOutput - > setToolTip ( COLUMN_LABEL , tr ( " change from %1 (%2) " ) . arg ( sWalletLabel ) . arg ( sWalletAddress ) ) ;
itemOutput - > setText ( COLUMN_LABEL , tr ( " (change) " ) ) ;
}
else if ( ! treeMode )
{
2014-04-01 10:08:35 +02:00
QString sLabel = model - > getAddressTableModel ( ) - > labelForAddress ( sAddress ) ;
if ( sLabel . isEmpty ( ) )
2013-08-12 17:03:03 +02:00
sLabel = tr ( " (no label) " ) ;
itemOutput - > setText ( COLUMN_LABEL , sLabel ) ;
}
// amount
itemOutput - > setText ( COLUMN_AMOUNT , BitcoinUnits : : format ( nDisplayUnit , out . tx - > vout [ out . i ] . nValue ) ) ;
itemOutput - > setText ( COLUMN_AMOUNT_INT64 , strPad ( QString : : number ( out . tx - > vout [ out . i ] . nValue ) , 15 , " " ) ) ; // padding so that sorting works correctly
// date
2013-12-15 15:39:09 +01:00
itemOutput - > setText ( COLUMN_DATE , GUIUtil : : dateTimeStr ( out . tx - > GetTxTime ( ) ) ) ;
2014-03-03 04:16:42 +01:00
itemOutput - > setText ( COLUMN_DATE_INT64 , strPad ( QString : : number ( out . tx - > GetTxTime ( ) ) , 20 , " " ) ) ;
2013-08-12 17:03:03 +02:00
// confirmations
itemOutput - > setText ( COLUMN_CONFIRMATIONS , strPad ( QString : : number ( out . nDepth ) , 8 , " " ) ) ;
// priority
double dPriority = ( ( double ) out . tx - > vout [ out . i ] . nValue / ( nInputSize + 78 ) ) * ( out . nDepth + 1 ) ; // 78 = 2 * 34 + 10
2014-05-27 21:44:57 +02:00
itemOutput - > setText ( COLUMN_PRIORITY , CoinControlDialog : : getPriorityLabel ( mempool , dPriority ) ) ;
2013-08-12 17:03:03 +02:00
itemOutput - > setText ( COLUMN_PRIORITY_INT64 , strPad ( QString : : number ( ( int64_t ) dPriority ) , 20 , " " ) ) ;
dPrioritySum + = ( double ) out . tx - > vout [ out . i ] . nValue * ( out . nDepth + 1 ) ;
nInputSum + = nInputSize ;
// transaction hash
uint256 txhash = out . tx - > GetHash ( ) ;
2014-09-08 12:25:52 +02:00
itemOutput - > setText ( COLUMN_TXHASH , QString : : fromStdString ( txhash . GetHex ( ) ) ) ;
2013-08-12 17:03:03 +02:00
// vout index
itemOutput - > setText ( COLUMN_VOUT_INDEX , QString : : number ( out . i ) ) ;
// disable locked coins
if ( model - > isLockedCoin ( txhash , out . i ) )
{
COutPoint outpt ( txhash , out . i ) ;
coinControl - > UnSelect ( outpt ) ; // just to be sure
itemOutput - > setDisabled ( true ) ;
itemOutput - > setIcon ( COLUMN_CHECKBOX , QIcon ( " :/icons/lock_closed " ) ) ;
}
// set checkbox
if ( coinControl - > IsSelected ( txhash , out . i ) )
2014-04-01 10:08:35 +02:00
itemOutput - > setCheckState ( COLUMN_CHECKBOX , Qt : : Checked ) ;
2013-08-12 17:03:03 +02:00
}
// amount
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 , " " ) ) ;
2014-05-27 21:44:57 +02:00
itemWalletAddress - > setText ( COLUMN_PRIORITY , CoinControlDialog : : getPriorityLabel ( mempool , dPrioritySum ) ) ;
2013-08-12 17:03:03 +02:00
itemWalletAddress - > setText ( COLUMN_PRIORITY_INT64 , strPad ( QString : : number ( ( int64_t ) dPrioritySum ) , 20 , " " ) ) ;
}
}
// expand all partially selected
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 ) ;
}
// sort view
sortView ( sortColumn , sortOrder ) ;
ui - > treeWidget - > setEnabled ( true ) ;
}