source: SMSSender/gateways/Schoolnet/gui/ui/widgets/questionbox.cpp @ 382:c26400d75b91

separation-frontend-backend
Last change on this file since 382:c26400d75b91 was 382:c26400d75b91, checked in by Sämy Zehnder <saemy.zehnder@…>, 5 years ago
  • Non-functional changes only.
  • Applies coding-style throughout the project:
    • "Class* foo" -> "Class *foo"
    • "Class& foo" -> "Class &foo"
    • "Class foo_;" -> "Class m_foo;"
  • Removes imanagers.h since it is obsolete.
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_question(question)
30    , m_hintLabel(NULL)
31{
32    this->moveToThread(parent->thread());
33
34        ui.setupUi(this);
35
36    ui.btnsNavigation->addButton(tr("Other Question"), QDialogButtonBox::RejectRole);
37    ui.btnsNavigation->button(QDialogButtonBox::Ok)->setEnabled(false);
38
39        loadQuestion(question);
40}
41
42void QuestionBox::loadQuestion(const Question &question) {
43    if (m_hintLabel) delete m_hintLabel;
44    while (!m_radios.isEmpty()) delete m_radios.takeFirst();
45
46    ui.lblQuestion->setText(question.text());
47
48    if (!question.hint().isEmpty()) {
49        m_hintLabel = new QLabel(this);
50        m_hintLabel->setText("<b>" + tr("Hint") + ": </b>" + question.hint());
51        m_hintLabel->setWordWrap(true);
52        m_hintLabel->setTextInteractionFlags(Qt::TextSelectableByMouse|Qt::TextSelectableByKeyboard);
53        qobject_cast<QBoxLayout*>(this->layout())->insertWidget(2, m_hintLabel);
54    }
55
56    QBoxLayout *answersLayout = qobject_cast<QBoxLayout*>(ui.grpAnswers->layout());
57    foreach (const Answer *answer, question.answers()) {
58        QRadioButton *radio = new QRadioButton(ui.grpAnswers);
59
60        radio->setText(answer->text());
61        radio->setEnabled(!answer->isAnswered());
62
63        QSizePolicy sp = radio->sizePolicy();
64        sp.setVerticalPolicy(QSizePolicy::Preferred);
65        radio->setSizePolicy(sp);
66
67        connect(radio, SIGNAL(toggled(bool)),
68                this, SLOT(on_answerRadioToggled(bool)));
69
70        answersLayout->addWidget(radio);
71        m_radios.append(radio);
72    }
73    this->adjustSize();
74    this->setMinimumSize(this->size());
75}
76
77QuestionBox::~QuestionBox() {
78
79}
80
81void QuestionBox::on_answerRadioToggled(bool checked) {
82    Q_UNUSED(checked);
83    ui.btnsNavigation->button(QDialogButtonBox::Ok)->setEnabled(true);
84}
85
86void QuestionBox::on_btnsNavigation_clicked(QAbstractButton *button){
87    if (ui.btnsNavigation->standardButton(button) == QDialogButtonBox::Ok){
88
89        Answer *theAnswer = NULL;
90        foreach (QRadioButton *radio, m_radios) {
91            if (!radio->isChecked()) {
92                continue;
93            }
94
95            foreach (Answer *answer, m_question.answers()) {
96                if (answer->text() == radio->text()) {
97                    theAnswer = answer;
98                    break;
99                }
100            }
101        }
102
103        Q_ASSERT(theAnswer);
104        emit answerProvided(theAnswer);
105    } else if (ui.btnsNavigation->standardButton(button) == QDialogButtonBox::Cancel) {
106        emit rejected();
107    } else {
108        emit nextQuestionRequested();
109    }
110}
111
112} // namespace UI
113} // namespace DeepThought
114} // namespace Schoolnet
Note: See TracBrowser for help on using the repository browser.