source: SMSSender/gateways/Schoolnet/gui/ui/widgets/questionbox.cpp @ 385:5f0e735f46e1

separation-frontend-backend
Last change on this file since 385:5f0e735f46e1 was 385:5f0e735f46e1, checked in by Sämy Zehnder <saemy.zehnder@…>, 7 years ago
  • Fixes crash when switching to another question in the Schoolnet send-integration and then providing the answer for it.
File size: 3.5 KB
Line 
1/*
2 Schoolnet gateway plugin - The smssender plugin for the Schoolnet sms platform.
3 Copyright (C) 2010-2014, gorrión. See http://smssender.gorrion.ch
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18#include "questionbox.h"
19
20#include <QPushButton>
21#include <QRadioButton>
22
23namespace Schoolnet {
24namespace DeepThought {
25namespace UI {
26
27QuestionBox::QuestionBox(const Question *question, QWidget *parent)
28    : QWidget(parent)
29    , m_hintLabel(NULL)
30{
31    this->moveToThread(parent->thread());
32
33        ui.setupUi(this);
34
35    ui.btnsNavigation->addButton(tr("Other Question"), QDialogButtonBox::RejectRole);
36    ui.btnsNavigation->button(QDialogButtonBox::Ok)->setEnabled(false);
37
38        loadQuestion(question);
39}
40
41void QuestionBox::loadQuestion(const Question *question) {
42    m_question = question;
43
44    if (m_hintLabel) delete m_hintLabel;
45    while (!m_radios.isEmpty()) delete m_radios.takeFirst();
46
47    ui.lblQuestion->setText(question->text());
48
49    if (!question->hint().isEmpty()) {
50        m_hintLabel = new QLabel(this);
51        m_hintLabel->setText("<b>" + tr("Hint") + ": </b>" + question->hint());
52        m_hintLabel->setWordWrap(true);
53        m_hintLabel->setTextInteractionFlags(Qt::TextSelectableByMouse|Qt::TextSelectableByKeyboard);
54        qobject_cast<QBoxLayout*>(this->layout())->insertWidget(2, m_hintLabel);
55    }
56
57    QBoxLayout *answersLayout = qobject_cast<QBoxLayout*>(ui.grpAnswers->layout());
58    foreach (const Answer *answer, question->answers()) {
59        QRadioButton *radio = new QRadioButton(ui.grpAnswers);
60
61        radio->setText(answer->text());
62        radio->setEnabled(!answer->isAnswered());
63
64        QSizePolicy sp = radio->sizePolicy();
65        sp.setVerticalPolicy(QSizePolicy::Preferred);
66        radio->setSizePolicy(sp);
67
68        connect(radio, SIGNAL(toggled(bool)),
69                this, SLOT(on_answerRadioToggled(bool)));
70
71        answersLayout->addWidget(radio);
72        m_radios.append(radio);
73    }
74    this->adjustSize();
75    this->setMinimumSize(this->size());
76}
77
78QuestionBox::~QuestionBox() {
79
80}
81
82void QuestionBox::on_answerRadioToggled(bool checked) {
83    Q_UNUSED(checked);
84    ui.btnsNavigation->button(QDialogButtonBox::Ok)->setEnabled(true);
85}
86
87void QuestionBox::on_btnsNavigation_clicked(QAbstractButton *button){
88    if (ui.btnsNavigation->standardButton(button) == QDialogButtonBox::Ok){
89
90        Answer *theAnswer = NULL;
91        foreach (QRadioButton *radio, m_radios) {
92            if (!radio->isChecked()) {
93                continue;
94            }
95
96            foreach (Answer *answer, m_question->answers()) {
97                if (answer->text() == radio->text()) {
98                    theAnswer = answer;
99                    break;
100                }
101            }
102        }
103
104        Q_ASSERT(theAnswer);
105        emit answerProvided(theAnswer);
106    } else if (ui.btnsNavigation->standardButton(button) == QDialogButtonBox::Cancel) {
107        emit rejected();
108    } else {
109        emit nextQuestionRequested();
110    }
111}
112
113} // namespace UI
114} // namespace DeepThought
115} // namespace Schoolnet
Note: See TracBrowser for help on using the repository browser.