File indexing completed on 2024-04-21 03:41:51

0001 /***************************************************************************
0002  *   Copyright (C) 2004 by Albert Astals Cid                               *
0003  *   aacid@kde.org                                                         *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "answer.h"
0012 
0013 #include <QImage>
0014 #include <QLabel>
0015 #include <QLayout>
0016 #include <QPixmap>
0017 
0018 userAnswer::userAnswer()
0019 {
0020 }
0021 
0022 userAnswer::userAnswer(const userAnswer &r)
0023 {
0024     p_question = r.p_question;
0025     p_answer = r.p_answer;
0026     p_correctAnswer = r.p_correctAnswer;
0027     p_correct = r.p_correct;
0028 }
0029 
0030 userAnswer &userAnswer::operator=(const userAnswer &r)
0031 {
0032     p_question = r.p_question;
0033     p_answer = r.p_answer;
0034     p_correctAnswer = r.p_correctAnswer;
0035     p_correct = r.p_correct;
0036     return *this;
0037 }
0038 
0039 void userAnswer::setQuestion(const QVariant &question)
0040 {
0041     p_question = question;
0042 }
0043 
0044 void userAnswer::setAnswer(const QVariant &answer)
0045 {
0046     p_answer = answer;
0047 }
0048 
0049 void userAnswer::setAnswerCorrect(bool correct)
0050 {
0051     p_correct = correct;
0052 }
0053 
0054 void userAnswer::setCorrectAnswer(const QVariant &correctAnswer)
0055 {
0056     p_correctAnswer = correctAnswer;
0057 }
0058 
0059 void userAnswer::putWidgets(QWidget *w, QGridLayout *lay, int row, int margin) const
0060 {
0061     QWidget *widgets[3];
0062     const QVariant *v;
0063     
0064     for (int i = 0; i < 3; i++)
0065     {
0066         if (i == 0) v = &p_question;
0067         else if (i == 1) v = &p_answer;
0068         else v = &p_correctAnswer;
0069         if (v -> typeId() == QVariant::String)
0070         {
0071             QLabel *l;
0072             l = new QLabel(w);
0073             l -> setText(v -> toString());
0074             l -> setContentsMargins(margin, margin, margin, margin);
0075             widgets[i] = l;
0076         }
0077         else if (v -> typeId() == QVariant::Color)
0078         {
0079             QWidget *aux = new QWidget(w);
0080             QHBoxLayout *lay = new QHBoxLayout(aux);
0081             
0082             QFrame *inner = new QFrame(aux);
0083             lay -> addWidget(inner);
0084             inner -> setPalette(QPalette(v -> value<QColor>()));
0085             inner -> setAutoFillBackground(true);
0086             lay -> setContentsMargins(2, 2, 2, 2);
0087             lay -> setSpacing(2);
0088             widgets[i] = aux;
0089         }
0090         else if (v -> typeId() == QVariant::List)
0091         {
0092             QVariantList vl = v->value<QVariantList>();
0093             QVariant vi;
0094             QWidget *aux = new QWidget(w);
0095             QHBoxLayout *lay = new QHBoxLayout(aux);
0096 
0097             QLabel *coloredLabel = new QLabel(aux);
0098             lay -> addWidget(coloredLabel);
0099             coloredLabel-> setAutoFillBackground(true);
0100             coloredLabel-> setContentsMargins(margin, margin, margin, margin);
0101             lay -> setContentsMargins(2, 2, 2, 2);
0102             lay -> setSpacing(2);
0103 
0104             for (int i=0; i<vl.count(); i++)
0105             {
0106                 vi = vl[i];
0107                 if(vi.typeId() == QVariant::Color)
0108                 {
0109                     coloredLabel -> setPalette(QPalette(vi.value<QColor>()));
0110                 }
0111                 if(vi.typeId() == QVariant::String)
0112                 {
0113                     coloredLabel -> setText(vi.value<QString>());
0114                 }
0115             }
0116             widgets[i] = aux;
0117         }
0118         else if (v -> typeId() == QVariant::Pixmap)
0119         {
0120             QLabel *l;
0121             l = new QLabel(w);
0122             l -> setPixmap(v -> value<QPixmap>());
0123             l -> setAlignment(Qt::AlignHCenter);
0124             l -> setContentsMargins(margin, margin, margin, margin);
0125             widgets[i] = l;
0126         }
0127         
0128         lay -> addWidget(widgets[i], row, i + 1);
0129     }
0130 
0131     if (!p_correct)
0132     {
0133         // Calc the background color
0134         // using widgets[i] -> setBackgroundRole(QPalette::Highlight);
0135         // does not work because "overwrites" the color when v -> type() == QVariant::Color
0136         widgets[0]->show();
0137         widgets[0]->setBackgroundRole(QPalette::Highlight);
0138         QColor c = widgets[0]->palette().highlight().color();
0139         widgets[0]->setBackgroundRole(QPalette::NoRole);
0140     
0141         for (int i = 0; i < 3; i++)
0142         {
0143             widgets[i] -> setForegroundRole(QPalette::HighlightedText);
0144             widgets[i] -> setPalette( c );
0145             widgets[i] -> setAutoFillBackground(true);
0146         }
0147     }
0148 }
0149