/demos/licenseDemo/licensedialog.cpp

https://github.com/canpool/qtcanpool · C++ · 118 lines · 108 code · 10 blank · 0 comment · 9 complexity · 926906d3b9c01b08d5b775dedae2a525 MD5 · raw file

  1. #include "licensedialog.h"
  2. #include "license/nrlicense.h"
  3. #include <QApplication>
  4. #include <QHBoxLayout>
  5. #include <QVBoxLayout>
  6. #include <QGridLayout>
  7. #include <QDesktopServices>
  8. #include <QUrl>
  9. #include <QMessageBox>
  10. LicenseDialog::LicenseDialog(QWidget *parent)
  11. : QDialog(parent)
  12. {
  13. initUi();
  14. QMetaObject::connectSlotsByName(this);
  15. setWindowTitle(tr("Serial number register"));
  16. setMinimumWidth(300);
  17. setStyleSheet("color:black;");
  18. }
  19. LicenseDialog::~LicenseDialog()
  20. {
  21. }
  22. void LicenseDialog::initUi()
  23. {
  24. hostLabel = new QLabel(this);
  25. hostLabel->setText(tr("Host id: "));
  26. hostLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  27. licenseLabel = new QLabel(this);
  28. licenseLabel->setText(tr("Serial number: "));
  29. licenseLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  30. hostEdit = new QLineEdit(this);
  31. hostEdit->setReadOnly(true);
  32. licenseEdit = new QLineEdit(this);
  33. licenseEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  34. emailLabel = new QLabel(this);
  35. emailLabel->setText(tr("Contact way: "));
  36. emailLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  37. emailTo = new QLabel(this);
  38. emailTo->setText("<a href = 1074867723@qq.com>1074867723@qq.com</a>");
  39. connect(emailTo, SIGNAL(linkActivated(const QString &)), this, SLOT(on_open_email_url(const QString &)));
  40. phoneLabel = new QLabel(this);
  41. phoneLabel->setText(tr("Telephone: ") + QString("888-888888888"));
  42. phoneLabel->setAlignment(Qt::AlignRight);
  43. registerButton = new QPushButton(this);
  44. registerButton->setText(tr("Register"));
  45. registerButton->setObjectName("registerButton");
  46. cancelButton = new QPushButton(this);
  47. cancelButton->setText(tr("Cancel"));
  48. cancelButton->setObjectName("cancelButton");
  49. QGridLayout *gridlayout = new QGridLayout;
  50. gridlayout->addWidget(hostLabel, 0, 0, 1, 2);
  51. gridlayout->addWidget(hostEdit, 0, 1, 1, 2);
  52. gridlayout->addWidget(licenseLabel, 1, 0, 1, 2);
  53. gridlayout->addWidget(licenseEdit, 1, 1, 1, 2);
  54. gridlayout->addWidget(emailLabel, 2, 0, 1, 1);
  55. gridlayout->addWidget(emailTo, 2, 1, 1, 1);
  56. gridlayout->addWidget(phoneLabel, 2, 2, 1, 1);
  57. QHBoxLayout *hlayout = new QHBoxLayout;
  58. hlayout->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Fixed));
  59. hlayout->addWidget(registerButton);
  60. hlayout->addWidget(cancelButton);
  61. hlayout->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Fixed));
  62. QVBoxLayout *vlayout = new QVBoxLayout(this);
  63. vlayout->addLayout(gridlayout);
  64. vlayout->addLayout(hlayout);
  65. NR_CHAR hostid[NR_HOSTID_BUFSIZE] = {0};
  66. NR_INT nret = CreateHostIDNew(hostid, 0);
  67. if (nret == NR_OK) {
  68. hostEdit->setText(QString::fromLocal8Bit(hostid));
  69. } else {
  70. hostEdit->setText(tr("Please check configuration!"));
  71. }
  72. }
  73. void LicenseDialog::on_registerButton_clicked()
  74. {
  75. QString softname = QApplication::applicationName();
  76. QString shostid = hostEdit->text();
  77. QString slicense = licenseEdit->text();
  78. #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
  79. if (RegisterLicense(softname.toAscii().data(), shostid.data(), slicense.toAscii().data()) == NR_OK)
  80. #else
  81. if (RegisterLicense(softname.toLocal8Bit().data(), shostid.toLocal8Bit().data(),
  82. slicense.toLocal8Bit().data()) == NR_OK)
  83. #endif
  84. {
  85. #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
  86. int nret = CheckLicense(softname.toAscii().data());
  87. #else
  88. int nret = CheckLicense(softname.toLocal8Bit().data());
  89. #endif
  90. if (nret > 0) {
  91. accept();
  92. } else {
  93. QMessageBox::information(this, tr("Tips"), tr("Serial number registration failure!"));
  94. }
  95. }
  96. }
  97. void LicenseDialog::on_cancelButton_clicked()
  98. {
  99. reject();
  100. }
  101. void LicenseDialog::on_open_email_url(const QString &url)
  102. {
  103. Q_UNUSED(url);
  104. QString softname = QApplication::applicationName();
  105. QString hostid = hostEdit->text();
  106. QDesktopServices::openUrl(QUrl(tr("mailto:1074867723@qq.com?subject=%1 &body=soft name: %2; host id: %3")
  107. .arg(softname).arg(softname).arg(hostid)));
  108. }