widget.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include "configure.h"
  4. #include "miner.h"
  5. #include <iomanip>
  6. #include <thread>
  7. #include <sstream>
  8. #include <iostream>
  9. #include <QString>
  10. #include <QAction>
  11. #include <QDir>
  12. #include <QTimer>
  13. #include <QClipboard>
  14. Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget), m_tpool(new QThreadPool)
  15. {
  16. ui->setupUi(this);
  17. const unsigned int processor_count = std::thread::hardware_concurrency();
  18. ui->threads->setMaximum(processor_count);
  19. ui->threads->setValue(processor_count);
  20. auto actionShotcuts = new QAction(this);
  21. actionShotcuts->setShortcuts({ Qt::Key_Return, Qt::Key_Enter });
  22. this->addAction(actionShotcuts);
  23. connect(actionShotcuts, &QAction::triggered, this, [&](){ ui->action->animateClick(); });
  24. QObject::connect(ui->height, SIGNAL(valueChanged(int)), this, SLOT(secondByteEdit(int)));
  25. QObject::connect(ui->action, SIGNAL(clicked()), this, SLOT(action()));
  26. QObject::connect(ui->ipv6_pat_mode, SIGNAL(clicked()), this, SLOT(ipv6_pat_mode()));
  27. QObject::connect(ui->high_mode, SIGNAL(clicked()), this, SLOT(high_mode()));
  28. QObject::connect(ui->ipv6_pat_high_mode, SIGNAL(clicked()), this, SLOT(ipv6_pat_high_mode()));
  29. QObject::connect(ui->ipv6_reg_mode, SIGNAL(clicked()), this, SLOT(ipv6_reg_mode()));
  30. QObject::connect(ui->ipv6_reg_high_mode, SIGNAL(clicked()), this, SLOT(ipv6_reg_high_mode()));
  31. QObject::connect(ui->mesh_pat_mode, SIGNAL(clicked()), this, SLOT(mesh_pat_mode()));
  32. QObject::connect(ui->mesh_reg_mode, SIGNAL(clicked()), this, SLOT(mesh_reg_mode()));
  33. connect (ui->xmrCopyAddressButton, &QPushButton::clicked, this, [&](){
  34. QApplication::clipboard()->setText(
  35. "84RsgSFSrJ2iKhd3XW6cYHdH6hVfLKYwBfUj6BNWqbVzhtCJz7GzaFe8jnToG1NGmPh7pwABdvBiCivTZRE2KfphPFXiyNN"
  36. );
  37. ui->xmrCopyAddressButton->setText("Copied!");
  38. QTimer* timer = new QTimer;
  39. timer->setSingleShot(true);
  40. timer->setInterval(1500);
  41. connect (timer, &QTimer::timeout, this, [&, timer](){
  42. ui->xmrCopyAddressButton->setText("Copy");
  43. timer->deleteLater();
  44. });
  45. timer->start();
  46. });
  47. ui->path->setText(QDir::currentPath());
  48. this->setFixedSize(this->size());
  49. }
  50. void Widget::setLog(const QString tm, const quint64 tt, const quint64 f, const quint64 k)
  51. {
  52. if (k > m_speedRecord)
  53. {
  54. m_speedRecord = k;
  55. QString hs = "Maximum speed: " + numToReadableString(m_speedRecord) + " kH/s";
  56. ui->hs->setText(hs);
  57. }
  58. ui->time->setText(tm); // время
  59. ui->found->setText(numToReadableString(f)); // колесо фартуны
  60. ui->khs->setText(numToReadableString(k)); // скорость
  61. ui->total->setText(numToReadableString(tt)); // общий счетчик
  62. }
  63. void Widget::setAddr(const QString address)
  64. {
  65. ui->last->setText(address);
  66. }
  67. Widget::~Widget()
  68. {
  69. if (not conf.stop)
  70. {
  71. conf.stop = true;
  72. m_tpool->waitForDone(100);
  73. }
  74. delete ui;
  75. if (m_tpool)
  76. {
  77. m_tpool->clear();
  78. m_tpool->deleteLater();
  79. }
  80. }
  81. QString Widget::numToReadableString(const quint64 num)
  82. {
  83. QString string = QString::number(num);
  84. for (int i = string.size()-1, counter = 0; i >= 0; --i)
  85. {
  86. if (++counter % 3 == 0 && i != 0) string.insert(i, ' ');
  87. }
  88. return string;
  89. }
  90. void Widget::secondByteEdit(int i)
  91. {
  92. std::stringstream ss;
  93. ss << std::setw(2) << std::setfill('0') << std::hex << i ;
  94. ui->secondByte->setText("2<b>" + QString::fromStdString(ss.str()) + "</b>:");
  95. }
  96. void Widget::ipv6_pat_mode()
  97. {
  98. m_mode = 0;
  99. string_status(true);
  100. altitude_status(false);
  101. }
  102. void Widget::high_mode()
  103. {
  104. m_mode = 1;
  105. altitude_status(true);
  106. string_status(false);
  107. }
  108. void Widget::ipv6_pat_high_mode()
  109. {
  110. m_mode = 2;
  111. altitude_status(true);
  112. string_status(true);
  113. }
  114. void Widget::ipv6_reg_mode()
  115. {
  116. m_mode = 3;
  117. string_status(true);
  118. altitude_status(false);
  119. }
  120. void Widget::ipv6_reg_high_mode()
  121. {
  122. m_mode = 4;
  123. altitude_status(true);
  124. string_status(true);
  125. }
  126. void Widget::mesh_pat_mode()
  127. {
  128. m_mode = 5;
  129. string_status(true);
  130. altitude_status(false);
  131. }
  132. void Widget::mesh_reg_mode()
  133. {
  134. m_mode = 6;
  135. string_status(true);
  136. altitude_status(false);
  137. }
  138. void Widget::altitude_status(bool b)
  139. {
  140. ui->x_startaltitude->setEnabled(b);
  141. ui->secondByte->setEnabled(b);
  142. ui->height->setEnabled(b);
  143. ui->disableIncrease->setEnabled(b);
  144. }
  145. void Widget::string_status(bool b)
  146. {
  147. ui->stringSet->setEnabled(b);
  148. }
  149. void Widget::action()
  150. {
  151. if (m_isStarted)
  152. {
  153. conf.stop = true;
  154. m_isStarted = false;
  155. ui->khs->setNum(0);
  156. ui->stackedWidget->setCurrentIndex(0);
  157. ui->action->setText("START");
  158. return;
  159. }
  160. if (ui->stringSet->text() == "" and m_mode != 1)
  161. {
  162. ui->stringSet->setPlaceholderText("PATTERN/REGEXP");
  163. ui->stringSet->setStyleSheet("background-color: gray");
  164. QTimer* timer = new QTimer;
  165. timer->setSingleShot(true);
  166. timer->setInterval(1000);
  167. connect (timer, &QTimer::timeout, this, [&, timer](){
  168. ui->stringSet->setStyleSheet("");
  169. timer->deleteLater();
  170. });
  171. timer->start();
  172. return;
  173. }
  174. ui->stackedWidget->setCurrentIndex(1);
  175. ui->last->setText("<last address will be here>");
  176. ui->hs->setText("Maximum speed: 0 kH/s");
  177. setLog("00:00:00:00", 0, 0, 0);
  178. m_speedRecord = 0;
  179. conf.mode = m_mode;
  180. conf.proc = ui->threads->value();
  181. conf.high = ui->height->value();
  182. conf.str = ui->stringSet->text();
  183. conf.letsup = !ui->disableIncrease->isChecked();
  184. conf.stop = false;
  185. m_isStarted = true;
  186. ui->action->setText("STOP");
  187. Miner::dropCounters();
  188. for (unsigned i = 0; i < conf.proc; i++)
  189. {
  190. auto miner = new Miner(this);
  191. miner->setAutoDelete(true);
  192. m_tpool->start(miner);
  193. }
  194. }