summaryrefslogtreecommitdiff
path: root/src/study/CardSideView.cpp
blob: 8956210688d72e92b2f985ab6567e212011026b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "CardSideView.h"
#include "../field-styles/FieldStyleFactory.h"
#include "../dictionary/CardPack.h"
#include "../dictionary/IDictionary.h"

#include <QtCore>

CardSideView::CardSideView( bool aMode ):
    m_showMode(aMode), cardPack(NULL)
{
    setFrameStyle( QFrame::Plain | QFrame::Box );
    setAlignment( Qt::AlignCenter );
    setTextFormat( Qt::RichText );
    setWordWrap( true );
    setAutoFillBackground( true );
    setPalette( FieldStyleFactory::inst()->cardBgColor );
    newIcon = new QLabel(this);
    newIcon->setPixmap(QPixmap(":/images/new-topright.png"));
    updateNewIconPos();
    newIcon->hide();
}

void CardSideView::setEnabled( bool aEnabled )
    {
    QLabel::setEnabled( aEnabled );
    setBackgroundVisible( aEnabled );
    }

/** Cleans background and text */
void CardSideView::setBackgroundVisible( bool aVisible )
    {
    if( aVisible )
        QLabel::setEnabled( true );
    setAutoFillBackground( aVisible );
    setLineWidth( aVisible? 1 : 0 );
    if( !aVisible )
        setText("");
    }

void CardSideView::setPack(const CardPack* pack)
    {
    cardPack = pack;
    }

void CardSideView::setQstAnsr( const QString aQuestion, const QStringList aAnswers )
    {
    m_question = aQuestion;
    m_answers = aAnswers;
    updateText();
    }

void CardSideView::setQuestion( const QString aQuestion )
    {
    m_question = aQuestion;
    updateText();
    }

void CardSideView::setShowMode( bool aMode )
    {
    m_showMode = aMode;
    updateText();
    }

void CardSideView::updateText()
{
    setText(getFormattedText());
}

QString CardSideView::getFormattedText() const
{
    if(!cardPack)
        return QString();
    if(m_showMode == QstMode)
        return getFormattedQuestion();
    else
        return getFormattedAnswer();
}

QString CardSideView::getFormattedQuestion() const
{
    const Field* qstField = cardPack->getQuestionField();
    if(!qstField)
        return QString();
    return formattedField(m_question, qstField->style());
}

QString CardSideView::getFormattedAnswer() const
{
    QStringList formattedAnswers;
    int i = 0;
    foreach(const Field* field, cardPack->getAnswerFields())
    {
        QString text = getFormattedAnswerField(field, i);
        if(i == 0)
            text += "<br/>";
        if(!text.isEmpty())
            formattedAnswers << text;
        i++;
    }
    return formattedAnswers.join("<br/>");
}

QString CardSideView::getFormattedAnswerField(const Field* field, int index) const
{
    if(!field)
        return QString();
    if(index >= m_answers.size())
        return QString();
    if(m_answers[index].isEmpty())
        return QString();
    return formattedField(m_answers[index], field->style() );
}

QString CardSideView::formattedField( const QString aField, const QString aStyle ) const
    {
    Q_ASSERT( cardPack );
    QString text = static_cast<IDictionary*>(cardPack->dictionary())->
        extendImagePaths( aField );
    
    FieldStyle fieldStyle =  FieldStyleFactory::inst()->getStyle( aStyle );
    QString beginning("<span style=\"");
    beginning += QString("font-family:'%1'").arg( fieldStyle.font.family() );
    beginning += QString("; font-size:%1pt").arg( fieldStyle.font.pointSize() );
    beginning += getHighlighting(fieldStyle);
    beginning += "\">" + fieldStyle.prefix;
    QString ending( fieldStyle.suffix + "</span>");

    // Highlight keywords inside plain text. Ignore tags.
    if(m_showMode == AnsMode && fieldStyle.hasKeyword)
        {
        QString highlightedText;
        int curPos = 0;
        while( curPos < text.length() )
            {
            QString curText;
            int beginTagPos = text.indexOf( "<", curPos );
            if( beginTagPos > -1 )
                curText = text.mid( curPos, beginTagPos - curPos ); // copy plain text
            else
                curText = text.mid( curPos, -1 ); // copy until end of string
            curText = highlightKeyword(curText, fieldStyle);
            highlightedText += curText;
            if( beginTagPos == -1 )
                break;
            int endTagPos = text.indexOf( ">", beginTagPos );
            if( endTagPos > -1 )
                highlightedText += text.mid( beginTagPos, endTagPos - beginTagPos + 1 );    // copy tag
            else
                {
                highlightedText += text.mid( curPos, -1 );  // copy until end of string
                break;
                }
            curPos = endTagPos + 1;
            }
        text = highlightedText;
        }

    return beginning + text + ending;
    }

QString CardSideView::highlightKeyword( const QString aText, const FieldStyle& fieldStyle ) const
    {
    QString resText = aText;
    if( !m_question.isEmpty() )
        resText.replace(QRegExp( "\\b(\\w*" + QRegExp::escape( m_question ) + "\\w*)\\b",
            Qt::CaseInsensitive ), "[\\1]");
    QString spanBegin("<span style=\"");
    spanBegin += getHighlighting(fieldStyle.getKeywordStyle()) + "\">";
    resText.replace('[', spanBegin);
    resText.replace( ']', "</span>" );
    return resText;
    }

QString CardSideView::getHighlighting(const FieldStyle& fieldStyle) const
{
    QString res;
    if(fieldStyle.color != Qt::black)
        res += QString("; color:%1").arg(fieldStyle.color.name());
    if(fieldStyle.font.bold())
        res += "; font-weight:bold";
    if(fieldStyle.font.italic())
        res += "; font-style:italic";
    return res;
}

QSize CardSideView::sizeHint() const
{
    return QSize(300, 200);
}

void CardSideView::updateNewIconPos()
{
    newIcon->move(rect().topRight() - QPoint(newIcon->width(), 0));
}

void CardSideView::showNewIcon(bool visible)
{
    newIcon->setVisible(visible);
    updateNewIconPos();
}

void CardSideView::resizeEvent(QResizeEvent* /*event*/)
{
    updateNewIconPos();
}