1 /***************************************************************************
2 * Copyright (C) 2004 by Albert Astals Cid *
3 * aacid@kde.org *
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 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
10
11 #include "answer.h"
12
13 #include <QImage>
14 #include <QLabel>
15 #include <QLayout>
16 #include <QPixmap>
17
userAnswer()18 userAnswer::userAnswer()
19 {
20 }
21
userAnswer(const userAnswer & r)22 userAnswer::userAnswer(const userAnswer &r)
23 {
24 p_question = r.p_question;
25 p_answer = r.p_answer;
26 p_correctAnswer = r.p_correctAnswer;
27 p_correct = r.p_correct;
28 }
29
operator =(const userAnswer & r)30 userAnswer &userAnswer::operator=(const userAnswer &r)
31 {
32 p_question = r.p_question;
33 p_answer = r.p_answer;
34 p_correctAnswer = r.p_correctAnswer;
35 p_correct = r.p_correct;
36 return *this;
37 }
38
setQuestion(const QVariant & question)39 void userAnswer::setQuestion(const QVariant &question)
40 {
41 p_question = question;
42 }
43
setAnswer(const QVariant & answer)44 void userAnswer::setAnswer(const QVariant &answer)
45 {
46 p_answer = answer;
47 }
48
setAnswerCorrect(bool correct)49 void userAnswer::setAnswerCorrect(bool correct)
50 {
51 p_correct = correct;
52 }
53
setCorrectAnswer(const QVariant & correctAnswer)54 void userAnswer::setCorrectAnswer(const QVariant &correctAnswer)
55 {
56 p_correctAnswer = correctAnswer;
57 }
58
putWidgets(QWidget * w,QGridLayout * lay,int row,int margin) const59 void userAnswer::putWidgets(QWidget *w, QGridLayout *lay, int row, int margin) const
60 {
61 QWidget *widgets[3];
62 const QVariant *v;
63
64 for (int i = 0; i < 3; i++)
65 {
66 if (i == 0) v = &p_question;
67 else if (i == 1) v = &p_answer;
68 else v = &p_correctAnswer;
69 if (v -> type() == QVariant::String)
70 {
71 QLabel *l;
72 l = new QLabel(w);
73 l -> setText(v -> toString());
74 l -> setContentsMargins(margin, margin, margin, margin);
75 widgets[i] = l;
76 }
77 else if (v -> type() == QVariant::Color)
78 {
79 QWidget *aux = new QWidget(w);
80 QHBoxLayout *lay = new QHBoxLayout(aux);
81
82 QFrame *inner = new QFrame(aux);
83 lay -> addWidget(inner);
84 inner -> setPalette(QPalette(v -> value<QColor>()));
85 inner -> setAutoFillBackground(true);
86 lay -> setContentsMargins(2, 2, 2, 2);
87 lay -> setSpacing(2);
88 widgets[i] = aux;
89 }
90 else if (v -> type() == QVariant::List)
91 {
92 QVariantList vl = v->value<QVariantList>();
93 QVariant vi;
94 QWidget *aux = new QWidget(w);
95 QHBoxLayout *lay = new QHBoxLayout(aux);
96
97 QLabel *coloredLabel = new QLabel(aux);
98 lay -> addWidget(coloredLabel);
99 coloredLabel-> setAutoFillBackground(true);
100 coloredLabel-> setContentsMargins(margin, margin, margin, margin);
101 lay -> setContentsMargins(2, 2, 2, 2);
102 lay -> setSpacing(2);
103
104 for (int i=0; i<vl.count(); i++)
105 {
106 vi = vl[i];
107 if(vi.type() == QVariant::Color)
108 {
109 coloredLabel -> setPalette(QPalette(vi.value<QColor>()));
110 }
111 if(vi.type() == QVariant::String)
112 {
113 coloredLabel -> setText(vi.value<QString>());
114 }
115 }
116 widgets[i] = aux;
117 }
118 else if (v -> type() == QVariant::Pixmap)
119 {
120 QLabel *l;
121 l = new QLabel(w);
122 l -> setPixmap(v -> value<QPixmap>());
123 l -> setAlignment(Qt::AlignHCenter);
124 l -> setContentsMargins(margin, margin, margin, margin);
125 widgets[i] = l;
126 }
127
128 lay -> addWidget(widgets[i], row, i + 1);
129 }
130
131 if (!p_correct)
132 {
133 // Calc the background color
134 // using widgets[i] -> setBackgroundRole(QPalette::Highlight);
135 // does not work because "overwrites" the color when v -> type() == QVariant::Color
136 widgets[0]->show();
137 widgets[0]->setBackgroundRole(QPalette::Highlight);
138 QColor c = widgets[0]->palette().highlight().color();
139 widgets[0]->setBackgroundRole(QPalette::NoRole);
140
141 for (int i = 0; i < 3; i++)
142 {
143 widgets[i] -> setForegroundRole(QPalette::HighlightedText);
144 widgets[i] -> setPalette( c );
145 widgets[i] -> setAutoFillBackground(true);
146 }
147 }
148 }
149
150