standardAnswer.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "standardAnswer.h"
  2. #include "ui_standardAnswer.h"
  3. #include "CAppInfo.h"
  4. #include "logproc.h"
  5. #include "CCommonTools.h"
  6. standardAnswer::standardAnswer(CSubQuestionStruct &sqs, QWidget *parent) :
  7. CBaseWidget(parent),
  8. ui(new Ui::standardAnswer), m_sqs(sqs)
  9. {
  10. ui->setupUi(this);
  11. setStyleSheet(g_appInfoPtr->m_sQssStr);
  12. m_bShowAnswer = false;
  13. }
  14. standardAnswer::~standardAnswer()
  15. {
  16. delete ui;
  17. }
  18. void standardAnswer::initAnswer()
  19. {
  20. ui->txtb_sa_answer->clear();
  21. QTextCursor cursor = ui->txtb_sa_answer->textCursor();
  22. QTextCharFormat fmt;
  23. QFont font;
  24. font.setFamily("Microsoft YaHei");
  25. font.setPixelSize(14);
  26. font.setWeight(QFont::Medium);
  27. int nRA = m_sqs.vRightAnswer.size();
  28. QString sRightAnswer = "";
  29. for (int ira = 0; ira < nRA; ++ira)
  30. {
  31. if (!m_sqs.vRightAnswer[ira].isEmpty())
  32. {
  33. Json::Reader reader;
  34. Json::Value m_jBody = Json::Value::null;
  35. if (!reader.parse(m_sqs.vRightAnswer[ira].toStdString(), m_jBody))
  36. {
  37. // ShowMsg(QString::fromLocal8Bit("解析题干出错!"), g_appInfoPtr->m_answerWidget);
  38. myDebug() << QString::fromLocal8Bit("解析题干出错!");
  39. }
  40. int nSize = m_jBody["sections"].size();
  41. for (int i = 0; i < nSize; i++)
  42. {
  43. Json::Value blocks = m_jBody["sections"][i];
  44. for (int j = 0; j < (int)blocks["blocks"].size(); j++)
  45. {
  46. if (blocks["blocks"][j]["type"].asString() == "text")
  47. {
  48. QTextCharFormat fmt;
  49. QFont font;
  50. font.setFamily("Microsoft YaHei");
  51. font.setPixelSize(g_appInfoPtr->m_fRate * 16);
  52. font.setWeight(QFont::Medium);
  53. if (blocks["blocks"][j].isMember("param"))
  54. {
  55. if (blocks["blocks"][j]["param"].isMember("bold"))
  56. {
  57. font.setBold(blocks["blocks"][j]["param"]["bold"].asBool());
  58. }
  59. if (blocks["blocks"][j]["param"].isMember("italic"))
  60. {
  61. bool bstyle = blocks["blocks"][j]["param"]["italic"].asBool();
  62. font.setItalic(bstyle);
  63. }
  64. if (blocks["blocks"][j]["param"].isMember("underline"))
  65. {
  66. font.setUnderline(blocks["blocks"][j]["param"]["underline"].asBool());
  67. }
  68. if (blocks["blocks"][j]["param"].isMember("strikeOut"))
  69. {
  70. font.setStrikeOut(blocks["blocks"][j]["param"]["strikeOut"].asBool());
  71. }
  72. if (blocks["blocks"][j]["param"].isMember("size"))
  73. {
  74. font.setPixelSize(blocks["blocks"][j]["param"]["size"].asInt());
  75. }
  76. if (blocks["blocks"][j]["param"].isMember("name"))
  77. {
  78. font.setFamily(blocks["blocks"][j]["param"]["name"].asString().c_str());
  79. }
  80. }
  81. fmt.setFont(font);
  82. QString sTextStr = blocks["blocks"][j]["value"].asString().c_str();
  83. fmt.setFont(font);
  84. if (m_sqs.sQuestionType == QUESTION_TYPE::SingleChoice ||
  85. m_sqs.sQuestionType == QUESTION_TYPE::MultipleChoice)
  86. {
  87. std::vector<int>::iterator result = std::find(m_sqs.voptionPermutation.begin(), m_sqs.voptionPermutation.end(), sTextStr.toInt());
  88. sRightAnswer = QChar('A' + *result);
  89. cursor.insertText(sRightAnswer);
  90. }
  91. else if (m_sqs.sQuestionType == QUESTION_TYPE::TrueOrFalse)
  92. {
  93. if (sTextStr == "true")
  94. {
  95. cursor.insertText(QString::fromLocal8Bit("正确"));
  96. }
  97. else
  98. {
  99. cursor.insertText(QString::fromLocal8Bit("错误"));
  100. }
  101. }
  102. else if (m_sqs.sQuestionType == QUESTION_TYPE::FillUp || m_sqs.sQuestionType == QUESTION_TYPE::Essay)
  103. {
  104. cursor.insertText(sTextStr, fmt);
  105. }
  106. }
  107. else if (blocks["blocks"][j]["type"].asString() == "image")
  108. {
  109. QString sImage = blocks["blocks"][j]["value"].asString().c_str();
  110. if(!(sImage.startsWith("http") || sImage.startsWith("data:image")))
  111. {
  112. sImage = QString("temp/paper/attachment/") + sImage;
  113. }
  114. if(sImage.startsWith("data:image"))
  115. {
  116. QImage img;
  117. img = QImage::fromData(QByteArray::fromBase64(CCommonTools::getImageRawBase64Str(sImage).toLatin1()));
  118. if(blocks["blocks"][j]["param"].isMember("width") && !QString(blocks["blocks"][j]["param"]["width"].asString().c_str()).isEmpty() &&
  119. !QString(blocks["blocks"][j]["param"]["height"].asString().c_str()).isEmpty())
  120. {
  121. img = img.scaled(QString(blocks["blocks"][j]["param"]["width"].asString().c_str()).toInt(),
  122. QString(blocks["blocks"][j]["param"]["height"].asString().c_str()).toInt(),
  123. Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  124. }
  125. cursor.insertImage(img);
  126. }
  127. else
  128. {
  129. QImage img(sImage);
  130. if(blocks["blocks"][j]["param"].isMember("width") && !QString(blocks["blocks"][j]["param"]["width"].asString().c_str()).isEmpty() &&
  131. !QString(blocks["blocks"][j]["param"]["height"].asString().c_str()).isEmpty())
  132. {
  133. img = img.scaled(QString(blocks["blocks"][j]["param"]["width"].asString().c_str()).toInt(),
  134. QString(blocks["blocks"][j]["param"]["height"].asString().c_str()).toInt(),
  135. Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  136. }
  137. cursor.insertImage(img);
  138. }
  139. // QString strHtml = "";
  140. // if (blocks["blocks"][j]["param"].isMember("width"))
  141. // {
  142. // strHtml = QString("<img src=\"%1\" height=\"%2\" width=\"%3\" align=\"bottom\" />")
  143. // .arg(blocks["blocks"][j]["value"].asString().c_str())
  144. // .arg(blocks["blocks"][j]["param"]["height"].asString().c_str())
  145. // .arg(blocks["blocks"][j]["param"]["width"].asString().c_str());
  146. // }
  147. // else
  148. // {
  149. // strHtml = "<img src=\"" + QString::fromLocal8Bit(blocks["blocks"][j]["value"].asString().c_str()) + "\" align=\"bottom\" />";
  150. // }
  151. // cursor.insertHtml(strHtml);
  152. }
  153. }
  154. if (i < nSize - 1)
  155. {
  156. cursor.insertText("\n");
  157. }
  158. }
  159. }
  160. }
  161. ui->txtb_sa_answer->adjustSize();
  162. ui->txtb_sa_answer->selectAll();
  163. ui->txtb_sa_answer->setAlignment(Qt::AlignJustify);
  164. cursor.movePosition(QTextCursor::End);
  165. ui->txtb_sa_answer->setTextCursor(cursor);
  166. }
  167. int standardAnswer::setUI(const int nWidth)
  168. {
  169. ui->btn_sa_showAnswer->setGeometry(g_appInfoPtr->m_fRate*30, g_appInfoPtr->m_fRate*20, g_appInfoPtr->m_fRate*72, g_appInfoPtr->m_fRate*24);
  170. ui->label_sa_arrow->setGeometry(ui->btn_sa_showAnswer->x() + ui->btn_sa_showAnswer->width() - g_appInfoPtr->m_fRate*4,
  171. ui->btn_sa_showAnswer->y() + (ui->btn_sa_showAnswer->height())/2,
  172. g_appInfoPtr->m_fRate*8, g_appInfoPtr->m_fRate*4);
  173. ui->label_sa_answer->adjustSize();
  174. ui->label_sa_answer->setGeometry(0, 0,
  175. ui->label_sa_answer->width(), ui->label_sa_answer->height());
  176. ui->txtb_sa_answer->setGeometry(ui->label_sa_answer->x() + ui->label_sa_answer->width(), ui->label_sa_answer->y() - g_appInfoPtr->m_fRate *3,
  177. nWidth - ui->label_sa_answer->width(), 300);
  178. ui->txtb_sa_answer->setFixedWidth(nWidth - ui->label_sa_answer->width());
  179. initAnswer();
  180. int nHeight = ui->txtb_sa_answer->document()->size().height() + g_appInfoPtr->m_fRate*5;
  181. ui->txtb_sa_answer->setFixedHeight(nHeight);
  182. ui->widget_sa_answer->setGeometry(ui->btn_sa_showAnswer->x() + g_appInfoPtr->m_fRate*5, ui->btn_sa_showAnswer->y() + ui->btn_sa_showAnswer->height() + g_appInfoPtr->m_fRate * g_appInfoPtr->m_fRate*10,
  183. nWidth, ui->txtb_sa_answer->height());
  184. ui->widget_sa_answer->setVisible(false);
  185. setGeometry(0, 0, nWidth, ui->widget_sa_answer->y() + ui->widget_sa_answer->height() + g_appInfoPtr->m_fRate*20);
  186. return height();
  187. }
  188. void standardAnswer::on_btn_sa_showAnswer_clicked()
  189. {
  190. if(!m_bShowAnswer)
  191. {
  192. setFixedHeight(ui->widget_sa_answer->y() + ui->widget_sa_answer->height() + g_appInfoPtr->m_fRate * 20);
  193. m_bShowAnswer = true;
  194. }
  195. else
  196. {
  197. setFixedHeight(ui->btn_sa_showAnswer->y() + ui->btn_sa_showAnswer->height() + g_appInfoPtr->m_fRate * 20);
  198. m_bShowAnswer = false;
  199. }
  200. ui->widget_sa_answer->setVisible(m_bShowAnswer);
  201. emit heightChange(height());
  202. }
  203. int standardAnswer::widgetType()
  204. {
  205. return wt_standardAnswer;
  206. }