From: Modestas Vainius <modax@debian.org>
Subject: Make "Edit talker" functional again
Last-Update: 2010-12-04
Bug-Debian: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=599825
Origin: vendor
Forwarded: no

This makes "Edit talker" button in KTTS talker configuration dialog work again.
"Edit" support is based on "Add talker" support and reuses its dialog and
majority of support code.

--- a/kttsd/kcmkttsmgr/addtalker.cpp
+++ b/kttsd/kcmkttsmgr/addtalker.cpp
@@ -37,16 +37,21 @@
 #include <kmessagebox.h>
 
 #include "kspeechinterface.h"
+#include "talkercode.h"
 
 #include "ui_addtalkerwidget.h"
 
 const int kLanguageColumn = 0;
 const int kSynthesizerColumn = 1;
 
-AddTalker::AddTalker(QWidget* parent)
+AddTalker::AddTalker(QWidget* parent, TalkerCode const * initTc)
     : KDialog(parent)
 {
-    this->setCaption(i18n("Add Talker"));
+    if (initTc)
+        this->setCaption(i18n("Edit Talker"));
+    else
+        this->setCaption(i18n("Add Talker"));
+
     this->setButtons(KDialog::Help|KDialog::Ok|KDialog::Cancel);
     this->setDefaultButton(KDialog::Cancel);
     this->enableButtonOk(false);
@@ -67,7 +72,10 @@ AddTalker::AddTalker(QWidget* parent)
     QString fullLanguageCode = KGlobal::locale()->defaultLanguage();
     QString languageCode;
     QString countryCode;
-    TalkerCode::splitFullLanguageCode(fullLanguageCode, languageCode, countryCode);
+    if (initTc)
+        languageCode = initTc->language();
+    else
+        TalkerCode::splitFullLanguageCode(fullLanguageCode, languageCode, countryCode);
 
     QTableWidgetItem * defaultItem = 0;
 
@@ -85,7 +93,8 @@ AddTalker::AddTalker(QWidget* parent)
             mUi->AvailableTalkersTable->setItem(rowcount, kSynthesizerColumn, item);
 
             QString langName = TalkerCode::languageCodeToLanguage(language);
-            if (language == languageCode)
+            if (!defaultItem && language == languageCode &&
+                (!initTc || initTc->outputModule() == module))
             {
                 defaultItem = item;
             }
@@ -107,6 +116,16 @@ AddTalker::AddTalker(QWidget* parent)
     {
         mUi->AvailableTalkersTable->setCurrentItem(defaultItem);
     }
+
+    // Set name, voice, volume, speed, pitch, from initTc if available
+    if (initTc)
+    {
+        mUi->nameEdit->setText(initTc->name());
+        mUi->voiceComboBox->setCurrentIndex(initTc->voiceType() - 1);
+        mUi->volumeSlider->setValue(initTc->volume());
+        mUi->speedSlider->setValue(initTc->rate());
+        mUi->pitchSlider->setValue(initTc->pitch());
+    }
 }
 
 AddTalker::~AddTalker()
--- a/kttsd/kcmkttsmgr/addtalker.h
+++ b/kttsd/kcmkttsmgr/addtalker.h
@@ -33,6 +33,8 @@
 
 #include "../libkttsd/talkercode.h"
 
+class TalkerCode;
+
 namespace Ui
 {
     class AddTalkerWidget;
@@ -48,7 +50,7 @@ public:
     * @param parent             Inherited KDialog parameter.
     * @param name               Inherited KDialog parameter.
     */
-    explicit AddTalker(QWidget* parent = 0);
+    explicit AddTalker(QWidget* parent = 0, TalkerCode const * initTc = 0);
 
     /**
     * Destructor.
--- a/kttsd/kcmkttsmgr/kcmkttsmgr.cpp
+++ b/kttsd/kcmkttsmgr/kcmkttsmgr.cpp
@@ -766,7 +766,7 @@ void KCMKttsMgr::slotAddTalkerButton_cli
         m_talkerListModel.appendRow(code);
 
         // Make sure visible.
-        const QModelIndex modelIndex = m_talkerListModel.index(m_talkerListModel.rowCount(),
+        const QModelIndex modelIndex = m_talkerListModel.index(m_talkerListModel.rowCount()-1,
             0, QModelIndex());
         talkersView->scrollTo(modelIndex);
 
@@ -1225,10 +1225,31 @@ void KCMKttsMgr::kttsdExiting()
 */
 void KCMKttsMgr::slotConfigureTalkerButton_clicked()
 {
-    //// Get highlighted plugin from Talker ListView and load into memory.
-    //QModelIndex modelIndex = talkersView->currentIndex();
-    //if (!modelIndex.isValid()) return;
-    //TalkerCode tc = m_talkerListModel.getRow(modelIndex.row());
+    // Get highlighted plugin from Talker ListView and load into memory.
+    QModelIndex modelIndex = talkersView->currentIndex();
+    if (!modelIndex.isValid()) return;
+    TalkerCode tc = m_talkerListModel.getRow(modelIndex.row());
+
+    QPointer<AddTalker> dlg = new AddTalker(this, &tc);
+    if (dlg->exec() == QDialog::Accepted) {
+        int row = modelIndex.row();
+        TalkerCode code = dlg->getTalkerCode();
+        m_talkerListModel.updateRow(row, code);
+
+        // Make sure visible.
+        const QModelIndex newIndex = m_talkerListModel.index(row, 0);
+        talkersView->scrollTo(newIndex);
+
+        // Select the new item, update buttons.
+        talkersView->setCurrentIndex(newIndex);
+        updateTalkerButtons();
+
+        // Inform Control Center that change has been made.
+        configChanged();
+    }
+    delete dlg;
+
+    kDebug() << "KCMKttsMgr::configureTalker: done.";
     //QString talkerID = tc.id();
     //QString synthName = tc.plugInName();
     //QString desktopEntryName = tc.desktopEntryName();
