questionOption.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include "questionOption.h"
  2. #include "ui_questionOption.h"
  3. #include <QFile>
  4. #include <QDebug>
  5. #include "optionBody.h"
  6. #include "audioPlay.h"
  7. #include "CAppInfo.h"
  8. questionOption::questionOption(int nOrder, Json::Value jOption, int nLimitedPlayTimes, QWidget *parent) :
  9. CBaseWidget(parent),
  10. ui(new Ui::questionOption)
  11. {
  12. ui->setupUi(this);
  13. setAttribute(Qt::WA_DeleteOnClose);
  14. m_jOption = jOption;
  15. m_nOrder = nOrder;
  16. m_nLimitedPlayTimes = nLimitedPlayTimes;
  17. m_bHasAns = false;
  18. m_bIsMultiChoice = false;
  19. setStyleSheet(g_appInfoPtr->m_sQssStr);
  20. }
  21. questionOption::~questionOption()
  22. {
  23. delete ui;
  24. }
  25. int questionOption::setUI(const int nWidth)
  26. {
  27. setCheckStyle();
  28. setGeometry(0, 0, nWidth, g_appInfoPtr->m_fRate*200);
  29. QString sText = QChar('A' + m_nOrder - 1) + QString::fromLocal8Bit(".");
  30. ui->chkb_option->setText(sText);
  31. ui->chkb_option->adjustSize();
  32. ui->chkb_option->setGeometry(g_appInfoPtr->m_fRate*1, g_appInfoPtr->m_fRate*6,
  33. ui->chkb_option->width(), ui->chkb_option->height());
  34. m_nListHeight = 0;
  35. ui->list_options->setFixedWidth(nWidth - ui->chkb_option->x() - ui->chkb_option->width());
  36. initOption();
  37. ui->list_options->setGeometry(ui->chkb_option->x() + ui->chkb_option->width(), g_appInfoPtr->m_fRate*0,
  38. nWidth - ui->chkb_option->x() - ui->chkb_option->width(),
  39. m_nListHeight + g_appInfoPtr->m_fRate*55);
  40. setGeometry(0, 0, nWidth, ui->list_options->y() + m_nListHeight + g_appInfoPtr->m_fRate*5);
  41. return height();
  42. }
  43. void questionOption::initOption()
  44. {
  45. int nSectionSize = m_jOption["sections"].size();
  46. Json::Value jSections = Json::Value::null;
  47. for(int i = 0; i < nSectionSize; i++)
  48. {
  49. Json::Value jblocks = Json::Value::null;
  50. int nBlockSize = m_jOption["sections"][i]["blocks"].size();
  51. for(int j = 0; j < nBlockSize; j++)
  52. {
  53. if(m_jOption["sections"][i]["blocks"][j]["type"].asString() == "audio")
  54. {
  55. if(jblocks != Json::Value::null ||
  56. jSections != Json::Value::null)
  57. {
  58. if(jblocks != Json::Value::null)
  59. {
  60. Json::Value jBlockItem = Json::Value::null;
  61. jBlockItem["blocks"] = jblocks;
  62. jblocks = Json::Value::null;
  63. jSections.append(jBlockItem);
  64. }
  65. Json::Value jQuestionBody = Json::Value::null;
  66. jQuestionBody["sections"] = jSections;
  67. jSections = Json::Value::null;
  68. jblocks = Json::Value::null;
  69. optionBody *wQB = new optionBody(jQuestionBody);
  70. QListWidgetItem *listItem = new QListWidgetItem;
  71. int nQBHeight = wQB->setUI(ui->list_options->width());
  72. m_nListHeight += nQBHeight;
  73. listItem->setSizeHint(QSize(ui->list_options->width(), nQBHeight));
  74. ui->list_options->addItem(listItem);
  75. ui->list_options->setItemWidget(listItem, wQB);
  76. }
  77. QString sAudioName = m_jOption["sections"][i]["blocks"][j]["ext2"].asString().c_str();
  78. QString sAudioUrl = m_jOption["sections"][i]["blocks"][j]["value"].asString().c_str();
  79. audioPlay *wAP = new audioPlay(sAudioName, sAudioUrl, m_nLimitedPlayTimes);
  80. connect(wAP, &audioPlay::playedCountChange, this, [&](){emit audioPlayCountChange();});
  81. QListWidgetItem *listItem = new QListWidgetItem;
  82. int nAPHeight = wAP->setUI(ui->list_options->width());
  83. m_nListHeight += nAPHeight;
  84. listItem->setSizeHint(QSize(ui->list_options->width(), nAPHeight));
  85. ui->list_options->addItem(listItem);
  86. ui->list_options->setItemWidget(listItem, wAP);
  87. }
  88. else
  89. {
  90. jblocks.append(m_jOption["sections"][i]["blocks"][j]);
  91. }
  92. }
  93. if(jblocks != Json::Value::null)
  94. {
  95. Json::Value jBlockItem = Json::Value::null;
  96. jBlockItem["blocks"] = jblocks;
  97. jblocks = Json::Value::null;
  98. jSections.append(jBlockItem);
  99. }
  100. }
  101. if(jSections != Json::Value::null)
  102. {
  103. Json::Value jQuestionBody = Json::Value::null;
  104. jQuestionBody["sections"] = jSections;
  105. jSections = Json::Value::null;
  106. optionBody *wQB = new optionBody(jQuestionBody);
  107. QListWidgetItem *listItem = new QListWidgetItem;
  108. int nQBHeight = wQB->setUI(ui->list_options->width());
  109. m_nListHeight += nQBHeight;
  110. listItem->setSizeHint(QSize(ui->list_options->width(), nQBHeight));
  111. ui->list_options->addItem(listItem);
  112. ui->list_options->setItemWidget(listItem, wQB);
  113. }
  114. }
  115. void questionOption::setCheckStyle()
  116. {
  117. // height:%1px;
  118. QString sCheckStyle = QString(R"(QCheckBox
  119. {
  120. outline: none;
  121. font-size:%1px;
  122. font-family:"Microsoft YaHei";
  123. font-weight:400;
  124. color:rgba(102,102,102,1);
  125. }
  126. QCheckBox::indicator
  127. {
  128. width:%2px;
  129. height:%3px;
  130. }
  131. QCheckBox::indicator:unchecked
  132. {
  133. border: %4px solid rgba(228,229,235,1);
  134. border-radius:%5px;
  135. background:rgba(239,240,245,1);
  136. }
  137. QCheckBox::indicator:checked
  138. {
  139. border-image: url(:/images/icon-checkbox-checked.png);
  140. })")//.arg((int)(g_appInfoPtr->m_fRate*26))
  141. .arg((int)(g_appInfoPtr->m_fRate*14))
  142. .arg((int)(g_appInfoPtr->m_fRate*14))
  143. .arg((int)(g_appInfoPtr->m_fRate*14))
  144. .arg((int)(g_appInfoPtr->m_fRate*1 < 1 ? 1 : g_appInfoPtr->m_fRate*1))
  145. .arg((int)(g_appInfoPtr->m_fRate*3));
  146. // height:%1px;
  147. QString sRadioStyle = QString(R"(QCheckBox
  148. {
  149. outline: none;
  150. border:0px solid rgba(102,102,102,1);
  151. font-size:%1px;
  152. font-family:"Microsoft YaHei";
  153. font-weight:400;
  154. color:rgba(102,102,102,1);
  155. }
  156. QCheckBox::indicator
  157. {
  158. width:%2px;
  159. height:%3px;
  160. }
  161. QCheckBox::indicator:unchecked
  162. {
  163. border-image:url(:/images/radiobtn-normal.png);
  164. }
  165. QCheckBox::indicator:checked
  166. {
  167. border-image: url(:/images/radiobtn-check.png);
  168. })")//.arg((int)(g_appInfoPtr->m_fRate*26))
  169. .arg((int)(g_appInfoPtr->m_fRate*14))
  170. .arg((int)(g_appInfoPtr->m_fRate*16))
  171. .arg((int)(g_appInfoPtr->m_fRate*16));
  172. if(m_bIsMultiChoice)
  173. {
  174. ui->chkb_option->setStyleSheet(sCheckStyle);
  175. }
  176. else
  177. {
  178. ui->chkb_option->setStyleSheet(sRadioStyle);
  179. }
  180. }
  181. void questionOption::setMultiChoice(bool bIsMultiChoice)
  182. {
  183. m_bIsMultiChoice = bIsMultiChoice;
  184. setCheckStyle();
  185. }
  186. void questionOption::setChecked(bool bChecked)
  187. {
  188. ui->chkb_option->setChecked(bChecked);
  189. }
  190. bool questionOption::checked()
  191. {
  192. return ui->chkb_option->isChecked();
  193. }
  194. void questionOption::setAnswered(bool bAnswered)
  195. {
  196. m_bHasAns = bAnswered;
  197. }
  198. int questionOption::widgetType()
  199. {
  200. return wt_optionItem;
  201. }
  202. void questionOption::on_chkb_option_clicked()
  203. {
  204. if(!m_bIsMultiChoice) //单选 判断
  205. {
  206. if(m_bHasAns) //已经有答案
  207. {
  208. if(ui->chkb_option->isChecked())
  209. {
  210. emit answerChanged(m_nOrder);
  211. }
  212. else
  213. {
  214. emit cancelAnswered();
  215. }
  216. }
  217. else
  218. {
  219. if(ui->chkb_option->isChecked())
  220. {
  221. emit answered();
  222. }
  223. }
  224. }
  225. else
  226. {
  227. emit mutiAnswerChanged();
  228. }
  229. }
  230. int questionOption::optionMumber()
  231. {
  232. if(m_jOption.isMember("number"))
  233. {
  234. return m_jOption["number"].asInt();
  235. }
  236. return 0;
  237. }