~neon/kmouth/master

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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/***************************************************************************
 *   Copyright (C) 2002 by Gunnar Schmi Dt <kmouth@schmi-dt.de             *
 *             (C) 2015 by Jeremy Whiting <jpwhiting@kde.org>              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/

// application specific includes
#include "phraselist.h"

// include files for Qt
#include <QApplication>
#include <QClipboard>
#include <QFileDialog>
#include <QHBoxLayout>
#include <QKeyEvent>
#include <QListView>
#include <QPushButton>
#include <QStandardItem>
#include <QVBoxLayout>

// include files for KDE
#include <KComboBox>
#include <KConfigGroup>
#include <KLineEdit>
#include <KLocalizedString>
#include <KMessageBox>
#include <KXMLGUIFactory>
#include <QIcon>

#include "kmouth.h"
#include "phrasebook/phrasebook.h"
#include "texttospeechsystem.h"
#include "wordcompletion/wordcompletion.h"

PhraseList::PhraseList(QWidget *parent, const QString &name)
    : QWidget(parent)
{
    Q_UNUSED(name);
    isInSlot = false;
    // FIXME: Remove or change PaletteBase to Qt::OpaqueMode?
    // setBackgroundMode(PaletteBase);
    QVBoxLayout *layout = new QVBoxLayout(this);

    m_listView = new QListView(this);
    m_model = new QStandardItemModel(this);
    m_listView->setModel(m_model);
    m_listView->setFocusPolicy(Qt::NoFocus);
    m_listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
    m_listView->setWhatsThis(i18n("This list contains the history of spoken sentences. You can select sentences and press the speak button for re-speaking."));
    layout->addWidget(m_listView);

    QHBoxLayout *rowLayout = new QHBoxLayout();
    layout->addLayout(rowLayout);

    completion = new WordCompletion();

    dictionaryCombo = new KComboBox(this);
    configureCompletionCombo(completion->wordLists());
    rowLayout->addWidget(dictionaryCombo);

    lineEdit = new KLineEdit(this);
    lineEdit->setFocusPolicy(Qt::StrongFocus);
    lineEdit->setFrame(true);
    lineEdit->setEchoMode(QLineEdit::Normal);
    lineEdit->setCompletionObject(completion);
    lineEdit->setAutoDeleteCompletionObject(true);
    lineEdit->setWhatsThis(i18n("Into this edit field you can type a phrase. Click on the speak button in order to speak the entered phrase."));
    rowLayout->addWidget(lineEdit);
    lineEdit->setFocus();

    QIcon icon = QIcon::fromTheme(QStringLiteral("text-speak"));
    speakButton = new QPushButton(icon, i18n("&Speak"), this);
    speakButton->setFocusPolicy(Qt::NoFocus);
    speakButton->setAutoDefault(false);
    speakButton->setWhatsThis(
        i18n("Speaks the currently active sentence(s). If there is some text in the edit field it is spoken. Otherwise the selected sentences in the history "
             "(if any) are spoken."));
    rowLayout->addWidget(speakButton);

    connect(dictionaryCombo, QOverload<const QString &>::of(&KComboBox::textActivated), completion, &WordCompletion::setWordList);
    connect(completion, &WordCompletion::wordListsChanged, this, &PhraseList::configureCompletionCombo);

    connect(m_listView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &PhraseList::selectionChanged);
    connect(m_listView, &QWidget::customContextMenuRequested, this, &PhraseList::contextMenuRequested);
    connect(lineEdit, &KLineEdit::returnKeyPressed, this, &PhraseList::lineEntered);
    connect(lineEdit, &QLineEdit::textChanged, this, &PhraseList::textChanged);
    connect(speakButton, &QAbstractButton::clicked, this, &PhraseList::speak);
}

PhraseList::~PhraseList()
{
    delete speakButton;
    delete m_model;
    delete lineEdit;
}

void PhraseList::print(QPrinter *pPrinter)
{
    PhraseBook book;
    QStandardItem *rootItem = m_model->invisibleRootItem();
    int count = rootItem->rowCount();
    for (int i = 0; i < count; ++i) {
        QStandardItem *item = rootItem->child(i);
        book += PhraseBookEntry(Phrase(item->text()));
    }

    book.print(pPrinter);
}

QStringList PhraseList::getListSelection()
{
    QStringList res = QStringList();

    QStandardItem *rootItem = m_model->invisibleRootItem();
    int count = rootItem->rowCount();
    QItemSelectionModel *selection = m_listView->selectionModel();
    for (int i = 0; i < count; ++i) {
        QStandardItem *item = rootItem->child(i);
        if (selection->isSelected(m_model->indexFromItem(item)))
            res += item->text();
    }

    return res;
}

bool PhraseList::existListSelection()
{
    return m_listView->selectionModel()->hasSelection();
}

bool PhraseList::existEditSelection()
{
    return lineEdit->hasSelectedText();
}

void PhraseList::enableMenuEntries()
{
    bool deselected = false;
    bool selected = existListSelection();
    QStandardItem *rootItem = m_model->invisibleRootItem();
    int count = rootItem->rowCount();
    QItemSelectionModel *selection = m_listView->selectionModel();
    for (int i = 0; i < count; ++i) {
        QStandardItem *item = rootItem->child(i);
        if (!selection->isSelected(m_model->indexFromItem(item))) {
            deselected = true;
            break;
        }
    }
    KMouthApp *theApp = qobject_cast<KMouthApp *>(parentWidget());
    theApp->enableMenuEntries(selected, deselected);
}

void PhraseList::configureCompletion()
{
    completion->configure();
}

void PhraseList::configureCompletionCombo(const QStringList &list)
{
    QString current = completion->currentWordList();
    dictionaryCombo->clear();
    if (list.isEmpty())
        dictionaryCombo->hide();
    else if (list.count() == 1) {
        dictionaryCombo->addItems(list);
        dictionaryCombo->setCurrentIndex(0);
        dictionaryCombo->hide();
    } else {
        dictionaryCombo->addItems(list);
        dictionaryCombo->show();

        QStringList::ConstIterator it;
        int i = 0;
        for (it = list.begin(), i = 0; it != list.end(); ++it, ++i) {
            if (current == *it) {
                dictionaryCombo->setCurrentIndex(i);
                return;
            }
        }
    }
}

void PhraseList::saveCompletionOptions()
{
    KConfigGroup cg(KSharedConfig::openConfig(), QStringLiteral("General Options"));
    cg.writeEntry("Show speak button", speakButton->isVisible() || !lineEdit->isVisible());

    KConfigGroup cg2(KSharedConfig::openConfig(), QStringLiteral("Completion"));
    cg2.writeEntry("Mode", static_cast<int>(lineEdit->completionMode()));
    cg2.writeEntry("List", completion->currentWordList());
}

void PhraseList::readCompletionOptions()
{
    KConfigGroup cg(KSharedConfig::openConfig(), QStringLiteral("General Options"));
    if (!cg.readEntry("Show speak button", true))
        speakButton->hide();

    if (KSharedConfig::openConfig()->hasGroup(QLatin1String("Completion"))) {
        KConfigGroup cg2(KSharedConfig::openConfig(), QStringLiteral("Completion"));
        // int mode = cg2.readEntry("Mode", int(KGlobalSettings::completionMode()));
        // lineEdit->setCompletionMode(static_cast<KGlobalSettings::Completion>(mode));

        QString current = cg2.readEntry("List", QString());
        const QStringList list = completion->wordLists();
        QStringList::ConstIterator it;
        int i = 0;
        for (it = list.constBegin(), i = 0; it != list.constEnd(); ++it, ++i) {
            if (current == *it) {
                dictionaryCombo->setCurrentIndex(i);
                return;
            }
        }
    }
}

void PhraseList::saveWordCompletion()
{
    completion->save();
}

void PhraseList::selectAllEntries()
{
    m_listView->selectAll();
}

void PhraseList::deselectAllEntries()
{
    m_listView->clearSelection();
}

void PhraseList::speak()
{
    QString phrase = lineEdit->text();
    if (phrase.isNull() || phrase.isEmpty())
        speakListSelection();
    else {
        insertIntoPhraseList(phrase, true);
        speakPhrase(phrase);
    }
}

void PhraseList::cut()
{
    if (lineEdit->hasSelectedText())
        lineEdit->cut();
    else
        cutListSelection();
}

void PhraseList::copy()
{
    if (lineEdit->hasSelectedText())
        lineEdit->copy();
    else
        copyListSelection();
}

void PhraseList::paste()
{
    lineEdit->paste();
}

void PhraseList::insert(const QString &s)
{
    setEditLineText(s);
}

void PhraseList::speakListSelection()
{
    speakPhrase(getListSelection().join(QLatin1String("\n")));
}

void PhraseList::removeListSelection()
{
    if (m_listView->selectionModel()->hasSelection()) {
        QList<QModelIndex> selected = m_listView->selectionModel()->selectedRows();
        std::sort(selected.begin(), selected.end());
        // Iterate over the rows backwards so we don't modify the .row of any indexes in selected.
        for (int i = selected.size() - 1; i >= 0; --i) {
            QModelIndex index = selected.at(i);
            m_model->removeRows(index.row(), 1);
        }
    }
    enableMenuEntries();
}

void PhraseList::cutListSelection()
{
    copyListSelection();
    removeListSelection();
}

void PhraseList::copyListSelection()
{
    QApplication::clipboard()->setText(getListSelection().join(QLatin1String("\n")));
}

void PhraseList::lineEntered(const QString &phrase)
{
    if (phrase.isNull() || phrase.isEmpty())
        speakListSelection();
    else {
        insertIntoPhraseList(phrase, true);
        speakPhrase(phrase);
    }
}

void PhraseList::speakPhrase(const QString &phrase)
{
    QApplication::setOverrideCursor(Qt::WaitCursor);
    KMouthApp *theApp = qobject_cast<KMouthApp *>(parentWidget());
    QString language = completion->languageOfWordList(completion->currentWordList());
    theApp->getTTSSystem()->speak(phrase, language);
    QApplication::restoreOverrideCursor();
}

void PhraseList::insertIntoPhraseList(const QString &phrase, bool clearEditLine)
{
    int lastLine = m_model->rowCount() - 1;
    if ((lastLine == -1) || (phrase != m_model->data(m_model->index(lastLine, 0)).toString())) {
        QStandardItem *item = new QStandardItem(phrase);
        m_model->appendRow(item);
        if (clearEditLine)
            completion->addSentence(phrase);
    }

    if (clearEditLine) {
        lineEdit->selectAll();
        line.clear();
    }
    enableMenuEntries();
}

void PhraseList::contextMenuRequested(const QPoint &pos)
{
    QString name;
    if (existListSelection())
        name = QStringLiteral("phraselist_selection_popup");
    else
        name = QStringLiteral("phraselist_popup");

    KMouthApp *theApp = qobject_cast<KMouthApp *>(parentWidget());
    KXMLGUIFactory *factory = theApp->factory();
    QMenu *popup = (QMenu *)factory->container(name, theApp);
    if (popup != nullptr) {
        popup->exec(pos, nullptr);
    }
}

void PhraseList::textChanged(const QString &s)
{
    if (!isInSlot) {
        isInSlot = true;
        line = s;
        m_listView->setCurrentIndex(m_model->index(m_model->rowCount() - 1, 0));
        m_listView->clearSelection();
        isInSlot = false;
    }
}

void PhraseList::selectionChanged()
{
    if (!isInSlot) {
        isInSlot = true;

        QStringList sel = getListSelection();

        if (sel.empty())
            setEditLineText(line);
        else if (sel.count() == 1)
            setEditLineText(sel.first());
        else {
            setEditLineText(QLatin1String(""));
        }
        isInSlot = false;
    }
    enableMenuEntries();
}

void PhraseList::setEditLineText(const QString &s)
{
    lineEdit->end(false);
    while (!(lineEdit->text().isNull() || lineEdit->text().isEmpty()))
        lineEdit->backspace();
    lineEdit->insert(s);
}

void PhraseList::keyPressEvent(QKeyEvent *e)
{
    if (e->key() == Qt::Key_Up) {
        bool selected = m_listView->selectionModel()->hasSelection();

        if (!selected) {
            m_listView->setCurrentIndex(m_model->index(m_model->rowCount() - 1, 0));
            // listBox->ensureCurrentVisible ();
        } else {
            int curr = m_listView->currentIndex().row();

            if (curr == -1) {
                isInSlot = true;
                m_listView->clearSelection();
                isInSlot = false;
                curr = m_model->rowCount() - 1;
                m_listView->setCurrentIndex(m_model->index(curr, 0));
                // listBox->ensureCurrentVisible ();
            } else if (curr != 0) {
                isInSlot = true;
                m_listView->clearSelection();
                isInSlot = false;
                m_listView->setCurrentIndex(m_model->index(curr - 1, 0));
                // listBox->ensureCurrentVisible ();
            }
        }

        e->accept();
    } else if (e->key() == Qt::Key_Down) {
        bool selected = m_listView->selectionModel()->hasSelection();

        if (selected) {
            int curr = m_listView->currentIndex().row();

            if (curr == (int)m_model->rowCount() - 1) {
                m_listView->clearSelection();
            } else if (curr != -1) {
                isInSlot = true;
                m_listView->clearSelection();
                isInSlot = false;
                m_listView->setCurrentIndex(m_model->index(curr + 1, 0));
                // listBox->ensureCurrentVisible ();
            }
        }
        e->accept();
    } else if (e->modifiers() & Qt::ControlModifier) {
        if (e->key() == Qt::Key_C) {
            copy();
            e->accept();
        } else if (e->key() == Qt::Key_X) {
            cut();
            e->accept();
        }
    } else
        e->ignore();
}

void PhraseList::save()
{
    // We want to save a history of spoken sentences here. However, as
    // the class PhraseBook does already provide a method for saving
    // phrase books in both the phrase book format and plain text file
    // format we use that method here.

    PhraseBook book;
    QStandardItem *rootItem = m_model->invisibleRootItem();
    int count = m_model->rowCount();
    for (int i = 0; i < count; ++i) {
        QStandardItem *item = rootItem->child(i);
        book += PhraseBookEntry(Phrase(item->text()));
    }

    QUrl url;
    if (book.save(this, i18n("Save As"), url, false) == -1)
        KMessageBox::error(this, i18n("There was an error saving file\n%1", url.url()));
}

void PhraseList::open()
{
    QUrl url =
        QFileDialog::getOpenFileUrl(this, i18n("Open File as History"), QUrl(), i18n("All Files (*);;Phrase Books (*.phrasebook);;Plain Text Files (*.txt)"));

    if (!url.isEmpty())
        open(url);
}

void PhraseList::open(const QUrl &url)
{
    // We want to open a history of spoken sentences here. However, as
    // the class PhraseBook does already provide a method for opening
    // both phrase books and plain text files we use that method here.

    PhraseBook book;
    if (book.open(url)) {
        // convert PhraseBookEntryList -> QStringList
        QStringList list = book.toStringList();
        m_model->clear();
        QStringList::iterator it;
        for (it = list.begin(); it != list.end(); ++it)
            insertIntoPhraseList(*it, false);
    } else
        KMessageBox::error(this, i18n("There was an error loading file\n%1", url.toDisplayString()));
}

#include "moc_phraselist.cpp"