[Qt] rework setNumBlocks to have blockDate as parameter

- reduces some functional overhead and simplifies the code
This commit is contained in:
Philip Kaufmann 2015-02-09 11:19:01 +01:00
parent 513e025239
commit 8517e9709e
7 changed files with 28 additions and 23 deletions

View File

@ -436,8 +436,8 @@ void BitcoinGUI::setClientModel(ClientModel *clientModel)
setNumConnections(clientModel->getNumConnections()); setNumConnections(clientModel->getNumConnections());
connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int))); connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
setNumBlocks(clientModel->getNumBlocks()); setNumBlocks(clientModel->getNumBlocks(), clientModel->getLastBlockDate());
connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int))); connect(clientModel, SIGNAL(numBlocksChanged(int,QDateTime)), this, SLOT(setNumBlocks(int,QDateTime)));
// Receive and report messages from client model // Receive and report messages from client model
connect(clientModel, SIGNAL(message(QString,QString,unsigned int)), this, SLOT(message(QString,QString,unsigned int))); connect(clientModel, SIGNAL(message(QString,QString,unsigned int)), this, SLOT(message(QString,QString,unsigned int)));
@ -653,7 +653,7 @@ void BitcoinGUI::setNumConnections(int count)
labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count)); labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
} }
void BitcoinGUI::setNumBlocks(int count) void BitcoinGUI::setNumBlocks(int count, const QDateTime& blockDate)
{ {
if(!clientModel) if(!clientModel)
return; return;
@ -681,9 +681,8 @@ void BitcoinGUI::setNumBlocks(int count)
QString tooltip; QString tooltip;
QDateTime lastBlockDate = clientModel->getLastBlockDate();
QDateTime currentDate = QDateTime::currentDateTime(); QDateTime currentDate = QDateTime::currentDateTime();
qint64 secs = lastBlockDate.secsTo(currentDate); qint64 secs = blockDate.secsTo(currentDate);
tooltip = tr("Processed %n blocks of transaction history.", "", count); tooltip = tr("Processed %n blocks of transaction history.", "", count);

View File

@ -143,8 +143,8 @@ signals:
public slots: public slots:
/** Set number of connections shown in the UI */ /** Set number of connections shown in the UI */
void setNumConnections(int count); void setNumConnections(int count);
/** Set number of blocks shown in the UI */ /** Set number of blocks and last block date shown in the UI */
void setNumBlocks(int count); void setNumBlocks(int count, const QDateTime& blockDate);
/** Notify the user of an event from the core network or transaction handling code. /** Notify the user of an event from the core network or transaction handling code.
@param[in] title the message box / notification title @param[in] title the message box / notification title

View File

@ -18,7 +18,6 @@
#include <stdint.h> #include <stdint.h>
#include <QDateTime>
#include <QDebug> #include <QDebug>
#include <QTimer> #include <QTimer>
@ -29,6 +28,7 @@ ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
optionsModel(optionsModel), optionsModel(optionsModel),
peerTableModel(0), peerTableModel(0),
cachedNumBlocks(0), cachedNumBlocks(0),
cachedBlockDate(QDateTime()),
cachedReindexing(0), cachedReindexing(0),
cachedImporting(0), cachedImporting(0),
pollTimer(0) pollTimer(0)
@ -79,10 +79,11 @@ quint64 ClientModel::getTotalBytesSent() const
QDateTime ClientModel::getLastBlockDate() const QDateTime ClientModel::getLastBlockDate() const
{ {
LOCK(cs_main); LOCK(cs_main);
if (chainActive.Tip()) if (chainActive.Tip())
return QDateTime::fromTime_t(chainActive.Tip()->GetBlockTime()); return QDateTime::fromTime_t(chainActive.Tip()->GetBlockTime());
else
return QDateTime::fromTime_t(Params().GenesisBlock().GetBlockTime()); // Genesis block's time of current network return QDateTime::fromTime_t(Params().GenesisBlock().GetBlockTime()); // Genesis block's time of current network
} }
double ClientModel::getVerificationProgress() const double ClientModel::getVerificationProgress() const
@ -97,21 +98,26 @@ void ClientModel::updateTimer()
// periodical polls if the core is holding the locks for a longer time - // periodical polls if the core is holding the locks for a longer time -
// for example, during a wallet rescan. // for example, during a wallet rescan.
TRY_LOCK(cs_main, lockMain); TRY_LOCK(cs_main, lockMain);
if(!lockMain) if (!lockMain)
return; return;
// Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change. // Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
// Periodically check and update with a timer. // Periodically check and update with a timer.
int newNumBlocks = getNumBlocks(); int newNumBlocks = getNumBlocks();
QDateTime newBlockDate = getLastBlockDate();
// check for changed number of blocks we have, number of blocks peers claim to have, reindexing state and importing state // check for changed number of blocks we have, number of blocks peers claim to have, reindexing state and importing state
if (cachedNumBlocks != newNumBlocks || if (cachedNumBlocks != newNumBlocks ||
cachedReindexing != fReindex || cachedImporting != fImporting) cachedBlockDate != newBlockDate ||
cachedReindexing != fReindex ||
cachedImporting != fImporting)
{ {
cachedNumBlocks = newNumBlocks; cachedNumBlocks = newNumBlocks;
cachedBlockDate = newBlockDate;
cachedReindexing = fReindex; cachedReindexing = fReindex;
cachedImporting = fImporting; cachedImporting = fImporting;
emit numBlocksChanged(newNumBlocks); emit numBlocksChanged(newNumBlocks, newBlockDate);
} }
emit bytesChanged(getTotalBytesRecv(), getTotalBytesSent()); emit bytesChanged(getTotalBytesRecv(), getTotalBytesSent());

View File

@ -6,6 +6,7 @@
#define BITCOIN_QT_CLIENTMODEL_H #define BITCOIN_QT_CLIENTMODEL_H
#include <QObject> #include <QObject>
#include <QDateTime>
class AddressTableModel; class AddressTableModel;
class OptionsModel; class OptionsModel;
@ -15,7 +16,6 @@ class TransactionTableModel;
class CWallet; class CWallet;
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QDateTime;
class QTimer; class QTimer;
QT_END_NAMESPACE QT_END_NAMESPACE
@ -73,6 +73,7 @@ private:
PeerTableModel *peerTableModel; PeerTableModel *peerTableModel;
int cachedNumBlocks; int cachedNumBlocks;
QDateTime cachedBlockDate;
bool cachedReindexing; bool cachedReindexing;
bool cachedImporting; bool cachedImporting;
@ -83,7 +84,7 @@ private:
signals: signals:
void numConnectionsChanged(int count); void numConnectionsChanged(int count);
void numBlocksChanged(int count); void numBlocksChanged(int count, const QDateTime& blockDate);
void alertsChanged(const QString &warnings); void alertsChanged(const QString &warnings);
void bytesChanged(quint64 totalBytesIn, quint64 totalBytesOut); void bytesChanged(quint64 totalBytesIn, quint64 totalBytesOut);

View File

@ -293,8 +293,8 @@ void RPCConsole::setClientModel(ClientModel *model)
setNumConnections(model->getNumConnections()); setNumConnections(model->getNumConnections());
connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int))); connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
setNumBlocks(model->getNumBlocks()); setNumBlocks(model->getNumBlocks(), model->getLastBlockDate());
connect(model, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int))); connect(model, SIGNAL(numBlocksChanged(int,QDateTime)), this, SLOT(setNumBlocks(int,QDateTime)));
updateTrafficStats(model->getTotalBytesRecv(), model->getTotalBytesSent()); updateTrafficStats(model->getTotalBytesRecv(), model->getTotalBytesSent());
connect(model, SIGNAL(bytesChanged(quint64,quint64)), this, SLOT(updateTrafficStats(quint64, quint64))); connect(model, SIGNAL(bytesChanged(quint64,quint64)), this, SLOT(updateTrafficStats(quint64, quint64)));
@ -404,11 +404,10 @@ void RPCConsole::setNumConnections(int count)
ui->numberOfConnections->setText(connections); ui->numberOfConnections->setText(connections);
} }
void RPCConsole::setNumBlocks(int count) void RPCConsole::setNumBlocks(int count, const QDateTime& blockDate)
{ {
ui->numberOfBlocks->setText(QString::number(count)); ui->numberOfBlocks->setText(QString::number(count));
if(clientModel) ui->lastBlockTime->setText(blockDate.toString());
ui->lastBlockTime->setText(clientModel->getLastBlockDate().toString());
} }
void RPCConsole::on_lineEdit_returnPressed() void RPCConsole::on_lineEdit_returnPressed()

View File

@ -63,8 +63,8 @@ public slots:
void message(int category, const QString &message, bool html = false); void message(int category, const QString &message, bool html = false);
/** Set number of connections shown in the UI */ /** Set number of connections shown in the UI */
void setNumConnections(int count); void setNumConnections(int count);
/** Set number of blocks shown in the UI */ /** Set number of blocks and last block date shown in the UI */
void setNumBlocks(int count); void setNumBlocks(int count, const QDateTime& blockDate);
/** Go forward or back in history */ /** Go forward or back in history */
void browseHistory(int offset); void browseHistory(int offset);
/** Scroll console view to end */ /** Scroll console view to end */

View File

@ -121,7 +121,7 @@ void SendCoinsDialog::setClientModel(ClientModel *clientModel)
this->clientModel = clientModel; this->clientModel = clientModel;
if (clientModel) { if (clientModel) {
connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(updateSmartFeeLabel())); connect(clientModel, SIGNAL(numBlocksChanged(int,QDateTime)), this, SLOT(updateSmartFeeLabel()));
} }
} }