File: AlphabetTable.cpp

package info (click to toggle)
qabcs 1.0.2-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 44,724 kB
  • sloc: cpp: 2,226; sh: 2,001; xml: 20; makefile: 5
file content (224 lines) | stat: -rw-r--r-- 7,863 bytes parent folder | download | duplicates (2)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "AlphabetTable.h"
#include "ui_AlphabetTable.h"

#include <QDir>
#include <QIcon>
#include <QtDebug>

#include "config_qabcs.h"
#include "SoundEngine.h"

AlphabetTable::AlphabetTable(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AlphabetTable)
{
    ui->setupUi(this);    
    this->setWindowTitle(tr("Alphabet table"));
    this->setWindowIcon(QIcon(QString(GLOBAL_PATH_USERDATA)+"/images/icons/abc.png"));

    isPlayLetter=false;

    QDir dirConfig(QDir::homePath()+"/.qabcs/");
    confSettings = new QSettings(dirConfig.absoluteFilePath("settings.ini"), QSettings::IniFormat);
    confSettings->setPath(QSettings::IniFormat, QSettings::UserScope, QDir::currentPath());

    QString pathAbc = QString(GLOBAL_PATH_USERDATA)+"/abcs/"+confSettings->value("abc/language","en").toString()+"/"+confSettings->value("abc/filename","abc1.json").toString();

    config_current = LoaderAbcFormats::LoadFilename(pathAbc,true);

    int x=10,y=10;
    int sizeFormWidth=500;

    QFont font;
    font.setBold(true);
    font.setPixelSize(30);

    for (auto &obj:config_current.letters){
        QLabel *cube = new QLabel(this);
        cube->setText(LoaderAbcFormats::upperString(obj.letter)+obj.letter.toLower());
        cube->setFont(font);
        cube->setAutoFillBackground(true);
        cube->setAlignment(Qt::AlignCenter);
        cube->setGeometry(x,y,100,80);
        cube->setFrameShape(QFrame::Box);
        cube->setProperty("letter",obj.letter);
        cube->installEventFilter(this);

        setPalette(cube,false);

        listLabelLetters.push_back(cube);

        x+=110;
        if (x>600){
            sizeFormWidth=x;
            x=10;
            y+=90;
        }
    }

    this->setFixedSize(sizeFormWidth,y+90);

    readLetterVariants();
}

AlphabetTable::~AlphabetTable(){
    delete confSettings;
    delete ui;
}


bool AlphabetTable::eventFilter(QObject *obj, QEvent *event){
    if (event->type() == QEvent::MouseButtonRelease and !isPlayLetter){
        auto it = std::find_if(listLabelLetters.begin(),listLabelLetters.end(),[&obj](QLabel *label){
            return obj==label;
        });
        if (it!=listLabelLetters.end()){
            QLabel *label = dynamic_cast<QLabel*>(*it);
            if (label==nullptr) return QDialog::eventFilter(obj, event);

            playLetter(label);
        }
    }

    return QDialog::eventFilter(obj, event);
}

void AlphabetTable::keyPressEvent(QKeyEvent *event){
    if (isPlayLetter) return;
    int key=event->key();

    auto it = std::find_if(listLabelLetters.begin(),listLabelLetters.end(),[&](QLabel *label){
        return isKeypressLetter(label->property("letter").toString(),QString(QChar(key)));
    });
    if (it!=listLabelLetters.end()){
        QLabel *label = dynamic_cast<QLabel*>(*it);
        if (label==nullptr) return;
        playLetter(label);
    }

}

void AlphabetTable::playLetter(QLabel *label){
    QString letter = label->property("letter").toString().toLower();

    auto it2 = std::find_if(config_current.letters.begin(),config_current.letters.end(),[&letter](ABC_CONFIG_ALPHA configAlpha){
        return configAlpha.letter.toLower()==letter.toLower();
    });
    if (it2==config_current.letters.end()) return;

    ABC_CONFIG_ALPHA configAlpha = static_cast<ABC_CONFIG_ALPHA>(*it2);

    setPalette(label,true);

    QString currentLanguageAbc=confSettings->value("abc/language","en").toString();
    QString currentFilenameAbc=confSettings->value("abc/filename","abc1.json").toString();


    QString speak_method = (configAlpha.speak_method.isEmpty()) ? config_current.speak_method : configAlpha.speak_method;
    QString espeak_params = (configAlpha.espeak_params.isEmpty()) ? config_current.espeak_params : configAlpha.espeak_params;

    if (config_current.format=="properties"){
        if (speak_method!="properties_espeak"){
            QString letterSoundLetterFilename =  SoundEngine::findSoundFile(QString(GLOBAL_PATH_USERDATA)+"/abcs/"+currentLanguageAbc+"/"+currentFilenameAbc,letter,"letter");
            if (!letterSoundLetterFilename.isEmpty()){
                SoundEngine::playSoundFromFile(letterSoundLetterFilename,false);
                setPalette(label,false);
                return;
            }
        }
        if (!configAlpha.espeak_words.isEmpty()){
            SoundEngine::playSoundFromSpeechSynthesizer(global_path_to_espeak+" "+espeak_params+" \""+configAlpha.espeak_words+"\"",false);
        }

    }else{
        if (speak_method=="espeak"){
            if (!configAlpha.espeak_words.isEmpty()){
                SoundEngine::playSoundFromSpeechSynthesizer(global_path_to_espeak+" "+espeak_params+" \""+configAlpha.espeak_words+"\"",false);
            }
        }else{
            QString soundFile = configAlpha.sound_letter;
            QString pathSoundLetterFilename = SoundEngine::findSoundFile(QString(GLOBAL_PATH_USERDATA)+"/abcs/"+currentLanguageAbc+"/"+currentFilenameAbc,soundFile,"letter");
            if (!pathSoundLetterFilename.isEmpty() and QFile::exists(pathSoundLetterFilename)){
                SoundEngine::playSoundFromFile(pathSoundLetterFilename,false);
            }else{
                SoundEngine::playSoundFromSpeechSynthesizer(global_path_to_espeak+" "+espeak_params+" \""+letter+"\"",false);
            }
        }
    }

    setPalette(label,false);
}

void AlphabetTable::setPalette(QLabel *label,bool fill){

    QPalette palette;
    QBrush brush(QColor(0, 0, 255, 255));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::WindowText, brush);
    palette.setBrush(QPalette::Inactive, QPalette::WindowText, brush);
    palette.setBrush(QPalette::Disabled, QPalette::WindowText, brush);

    if (fill){
        QBrush brush3(QColor(255, 255, 255, 255));
        brush3.setStyle(Qt::SolidPattern);
        palette.setBrush(QPalette::Active, QPalette::Window, brush3);
        palette.setBrush(QPalette::Inactive, QPalette::Window, brush3);
        palette.setBrush(QPalette::Disabled, QPalette::Window, brush3);
        isPlayLetter=true;
    }else{
        QBrush brush2(QColor(0, 255, 255, 255));
        brush2.setStyle(Qt::SolidPattern);
        palette.setBrush(QPalette::Active, QPalette::Window, brush2);
        palette.setBrush(QPalette::Inactive, QPalette::Window, brush2);
        palette.setBrush(QPalette::Disabled, QPalette::Window, brush2);
        isPlayLetter=false;
    }

    label->setPalette(palette);
    QCoreApplication::instance()->processEvents();
}


void AlphabetTable::readLetterVariants(){
    listLetterVariants.clear();

    QString currentLanguageAbc=confSettings->value("abc/language","en").toString();

    QStringList filepaths {
        QString(GLOBAL_PATH_USERDATA)+"/langs/variants_"+currentLanguageAbc+".txt",
        QString(GLOBAL_PATH_USERDATA)+"/langs/variants.txt"
    };

    for (auto filename:filepaths){
        QFile file(filename);
        if (!file.exists()) continue;

        if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
            while (!file.atEnd()) {
                QString line = file.readLine();
                QStringList pairs = line.split("=");
                if (pairs.size()!=2) continue;

                QString l = pairs.at(1);
                l.replace(QRegExp("\\t|\\s|\\n|\\r"),"");
                listLetterVariants[pairs.at(0)].push_back(l);
            }
            file.close();
        }
    }

}

bool AlphabetTable::isKeypressLetter(QString letter,QString key){
    if (letter.toLower().at(0)==key.toLower().at(0)) return true;

    if (std::find_if(listLetterVariants[letter.toLower()].begin(),listLetterVariants[letter.toLower()].end(),
        [&key](QString var){
            return (key.toLower()==var.toLower());
        }
    )!=listLetterVariants[letter.toLower()].end()){
        return true;
    }

    return  false;
}