settings_page.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "settings_page.h"
  2. #include "encoder/video_encoder.h"
  3. #include <QFileDialog>
  4. SettingsPage::SettingsPage(Param* param, QWidget* parent)
  5. : QDialog(parent)
  6. , _param(param)
  7. {
  8. setFont(QFont("Microsoft Yahei"));
  9. _InitUi();
  10. _InitConnect();
  11. }
  12. void SettingsPage::_InitConnect()
  13. {
  14. connect(_applyBtn, &QPushButton::released, [this] {
  15. _WriteSettings();
  16. });
  17. connect(_cancelBtn, &QPushButton::released, [this] {
  18. this->close();
  19. });
  20. connect(_yesBtn, &QPushButton::released, [this] {
  21. _WriteSettings();
  22. this->close();
  23. });
  24. connect(_selDirBtn, &QPushButton::released, [this] {
  25. QString selectedDir = QFileDialog::getExistingDirectory(this, "选择输出目录", "./", QFileDialog::ShowDirsOnly);
  26. // 若目录路径不为空
  27. if (!selectedDir.isEmpty()) {
  28. // 显示选择的目录路径
  29. _fileDirEdit->setText(selectedDir);
  30. }
  31. });
  32. }
  33. void SettingsPage::_WriteSettings()
  34. {
  35. _param->videoParam.bitRate = _videoBitRateBox->value() * 1000;
  36. _param->videoParam.fps = _videoFpsBox->value();
  37. _param->videoParam.name = _videoEncoderBox->currentText().toStdString();
  38. _param->audioParam.bitRate = _audioBitRateBox->value() * 1000;
  39. _param->outputDir = _fileDirEdit->text().toStdString();
  40. _param->liveUrl = _liveUrlEdit->text().toStdString();
  41. _param->liveName = _liveNameEdit->text().toStdString();
  42. }
  43. void SettingsPage::_InitUi()
  44. {
  45. setWindowTitle("Settings");
  46. auto layout = new QVBoxLayout;
  47. layout->addWidget(_InitVideoUi());
  48. layout->addWidget(_InitAudioUi());
  49. layout->addWidget(_InitOutputUi());
  50. layout->addWidget(_InitLiveUi());
  51. auto hLayout = new QHBoxLayout;
  52. _applyBtn = new QPushButton("应用");
  53. _cancelBtn = new QPushButton("取消");
  54. _yesBtn = new QPushButton("确定");
  55. hLayout->setAlignment(Qt::AlignRight);
  56. hLayout->addWidget(_applyBtn);
  57. hLayout->addWidget(_cancelBtn);
  58. hLayout->addWidget(_yesBtn);
  59. layout->addLayout(hLayout);
  60. setLayout(layout);
  61. }
  62. QGroupBox* SettingsPage::_InitVideoUi()
  63. {
  64. auto groupBox = new QGroupBox("视频");
  65. auto layout = new QVBoxLayout;
  66. _videoBitRateBox = new QSpinBox;
  67. _videoBitRateBox->setMinimum(0);
  68. _videoBitRateBox->setMaximum(INT_MAX);
  69. _videoBitRateBox->setValue(_param->videoParam.bitRate / 1000);
  70. _videoFpsBox = new QSpinBox;
  71. _videoFpsBox->setMinimum(0);
  72. _videoFpsBox->setMaximum(60);
  73. _videoFpsBox->setValue(_param->videoParam.fps);
  74. _videoEncoderBox = new QComboBox;
  75. auto&& encoders = Encoder<MediaType::VIDEO>::GetUsableEncoders();
  76. for (auto&& encoder : encoders) {
  77. _videoEncoderBox->addItem(encoder.c_str());
  78. }
  79. _videoEncoderBox->setCurrentText(_param->videoParam.name.c_str());
  80. layout->addLayout(_CreateDescription("比特率(kB):", "越高的比特率越清晰, 但越占用硬件资源", _videoBitRateBox));
  81. layout->addLayout(_CreateDescription("帧率:", "越高的帧率越流畅, 但越占用硬件资源", _videoFpsBox));
  82. layout->addLayout(_CreateDescription("编码器:", "libx264 为软件编码, CPU占用高但兼容性强, 其他为硬件编码, 效果与软件编码相反", _videoEncoderBox));
  83. groupBox->setLayout(layout);
  84. return groupBox;
  85. }
  86. QGroupBox* SettingsPage::_InitAudioUi()
  87. {
  88. auto groupBox = new QGroupBox("音频");
  89. auto layout = new QVBoxLayout;
  90. _audioBitRateBox = new QSpinBox;
  91. _audioBitRateBox->setMinimum(0);
  92. _audioBitRateBox->setMaximum(INT_MAX);
  93. _audioBitRateBox->setValue(_param->audioParam.bitRate / 1000);
  94. layout->addLayout(_CreateDescription("比特率(kB):", "越高的比特率越清晰, 但越占用硬件资源", _audioBitRateBox));
  95. groupBox->setLayout(layout);
  96. return groupBox;
  97. }
  98. QGroupBox* SettingsPage::_InitOutputUi()
  99. {
  100. auto groupBox = new QGroupBox("输出");
  101. auto layout = new QHBoxLayout;
  102. _fileDirEdit = new QLineEdit(_param->outputDir.c_str());
  103. _selDirBtn = new QPushButton("选择");
  104. layout->addWidget(_fileDirEdit);
  105. layout->addWidget(_selDirBtn);
  106. groupBox->setLayout(layout);
  107. return groupBox;
  108. }
  109. QGroupBox* SettingsPage::_InitLiveUi()
  110. {
  111. auto groupBox = new QGroupBox("直播");
  112. auto layout = new QVBoxLayout;
  113. _liveUrlEdit = new QLineEdit(_param->liveUrl.c_str());
  114. _liveNameEdit = new QLineEdit(_param->liveName.c_str());
  115. auto liveUrlLayout = new QHBoxLayout();
  116. liveUrlLayout->addWidget(new QLabel("地址:"));
  117. liveUrlLayout->addWidget(_liveUrlEdit);
  118. auto liveNameLayout = new QHBoxLayout();
  119. liveNameLayout->addWidget(new QLabel("名称(密钥):"));
  120. liveNameLayout->addWidget(_liveNameEdit);
  121. layout->addLayout(liveUrlLayout);
  122. layout->addLayout(liveNameLayout);
  123. groupBox->setLayout(layout);
  124. return groupBox;
  125. }
  126. QHBoxLayout* SettingsPage::_CreateDescription(std::string_view text, std::string_view textEx, QWidget* widget)
  127. {
  128. auto layout = new QHBoxLayout;
  129. auto label = new QLabel(text.data());
  130. label->setToolTip(textEx.data());
  131. layout->addWidget(label);
  132. layout->addWidget(widget);
  133. return layout;
  134. }