1 /*
2     SPDX-FileCopyrightText: 2010 Daniel Laidig <laidig@kde.org>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #include "statustoggle.h"
7 #include "statustogglebutton.h"
8 #include "themedbackgroundrenderer.h"
9 
10 #include <KLocalizedString>
11 
12 #include <QAction>
13 
14 using namespace Practice;
15 
StatusToggle(QWidget * parent)16 StatusToggle::StatusToggle(QWidget *parent)
17     : ImageWidget(parent)
18 {
19     setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
20     setScalingEnabled(false);
21     setMinimumSize(minimumSizeHint());
22     setMaximumSize(minimumSizeHint());
23 
24     QSize size = minimumSizeHint();
25     size *= 0.4;
26     m_toggle = new StatusToggleButton(this);
27     m_toggle->setMinimumSize(size);
28     m_toggle->setMaximumSize(size);
29     m_toggle->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
30     m_toggle->setScalingEnabled(false);
31     m_toggle->move(width() - m_toggle->width(), height() - m_toggle->height());
32 
33     connect(m_toggle, &StatusToggleButton::clicked, this, &StatusToggle::toggle);
34 }
35 
setRenderer(ThemedBackgroundRenderer * renderer)36 void StatusToggle::setRenderer(ThemedBackgroundRenderer *renderer)
37 {
38     m_renderer = renderer;
39 
40     QSize size = minimumSizeHint();
41 
42     m_defaultStatus = m_renderer->getPixmapForId(QStringLiteral("status-default"), size);
43     m_correctStatus = m_renderer->getPixmapForId(QStringLiteral("status-correct"), size);
44     m_wrongStatus = m_renderer->getPixmapForId(QStringLiteral("status-wrong"), size);
45 
46     size = size * 0.4;
47 
48     m_toggleCorrect = m_renderer->getPixmapForId(QStringLiteral("toggle-correct"), size);
49     m_toggleCorrectHover = m_renderer->getPixmapForId(QStringLiteral("toggle-correct-hover"), size);
50     m_toggleCorrectPressed = m_renderer->getPixmapForId(QStringLiteral("toggle-correct-pressed"), size);
51 
52     m_toggleWrong = m_renderer->getPixmapForId(QStringLiteral("toggle-wrong"), size);
53     m_toggleWrongHover = m_renderer->getPixmapForId(QStringLiteral("toggle-wrong-hover"), size);
54     m_toggleWrongPressed = m_renderer->getPixmapForId(QStringLiteral("toggle-wrong-pressed"), size);
55 
56     updatePixmap();
57     updateToggle();
58 }
59 
minimumSizeHint() const60 QSize StatusToggle::minimumSizeHint() const
61 {
62     return QSize(128, 128);
63 }
64 
setResultState(AbstractFrontend::ResultState state)65 void StatusToggle::setResultState(AbstractFrontend::ResultState state)
66 {
67     if (m_resultState == state)
68         return;
69     m_resultState = state;
70     switch (m_resultState) {
71     case AbstractFrontend::QuestionState:
72     case AbstractFrontend::AnswerSynonym:
73         setToolTip(QString());
74         m_toggle->setToolTip(QString());
75         break;
76     case AbstractFrontend::AnswerCorrect:
77         setToolTip(i18n("This answer will be counted as correct.\nWords will only be counted as correct if they are answered correctly on the first attempt."));
78         m_toggle->setToolTip(i18n("Count this answer as wrong"));
79         break;
80     case AbstractFrontend::AnswerWrong:
81         setToolTip(i18n("This answer will be counted as wrong.\nWords will only be counted as correct if they are answered correctly on the first attempt."));
82         m_toggle->setToolTip(i18n("Count this answer as correct"));
83         break;
84     default:
85         setPixmap(QPixmap());
86     }
87     updatePixmap();
88     updateToggle();
89 }
90 
updatePixmap()91 void StatusToggle::updatePixmap()
92 {
93     switch (m_resultState) {
94     case AbstractFrontend::QuestionState:
95     case AbstractFrontend::AnswerSynonym:
96         setPixmap(m_defaultStatus);
97         break;
98     case AbstractFrontend::AnswerCorrect:
99         setPixmap(m_correctStatus);
100         break;
101     case AbstractFrontend::AnswerWrong:
102         setPixmap(m_wrongStatus);
103         break;
104     default:
105         setPixmap(QPixmap());
106     }
107 }
108 
updateToggle()109 void StatusToggle::updateToggle()
110 {
111     switch (m_resultState) {
112     case AbstractFrontend::AnswerCorrect:
113         m_toggle->setPixmaps(m_toggleWrong, m_toggleWrongHover, m_toggleWrongPressed);
114         break;
115     case AbstractFrontend::AnswerWrong:
116         m_toggle->setPixmaps(m_toggleCorrect, m_toggleCorrectHover, m_toggleCorrectPressed);
117         break;
118     default:
119         m_toggle->setPixmaps(QPixmap(), QPixmap(), QPixmap());
120     }
121 }
122