update index.theme
[kdeaccessibility.git] / kmouth / kmouth.cpp
blob1ba7fd08c35c256f68b8fdebd50cee91b2f59dca
1 /***************************************************************************
2 kmouth.cpp - description
3 -------------------
4 begin : Mon Aug 26 15:41:23 CEST 2002
5 copyright : (C) 2002 by Gunnar Schmi Dt
6 email : kmouth@schmi-dt.de
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 // application specific includes
19 #include "kmouth.h"
20 #include "phraselist.h"
21 #include "phrasebook/phrasebook.h"
22 #include "phrasebook/phrasebookdialog.h"
23 #include "optionsdialog.h"
24 #include "configwizard.h"
26 // include files for Qt
27 #include <QtCore/QDir>
28 #include <QtGui/QPainter>
29 #include <QtGui/QPrinter>
30 #include <QtGui/QPrintDialog>
31 #include <QtGui/QMenu>
33 // include files for KDE
34 #include <kxmlguifactory.h>
35 #include <kiconloader.h>
36 #include <kmenubar.h>
37 #include <kstatusbar.h>
38 #include <klocale.h>
39 #include <kconfig.h>
40 #include <kstandardaction.h>
41 #include <kmenu.h>
42 #include <kstandarddirs.h>
43 #include <kglobal.h>
44 #include <ktoolbar.h>
45 #include <kactioncollection.h>
46 #include <kactionmenu.h>
47 #include <ktoggleaction.h>
48 #include <kstandardshortcut.h>
49 #include <kapplication.h>
51 #define ID_STATUS_MSG 1
53 KMouthApp::KMouthApp(QWidget* , const char* name):KXmlGuiWindow(0)
55 setObjectName(name);
56 isConfigured = false;
57 config=KGlobal::config();
59 ///////////////////////////////////////////////////////////////////
60 // call inits to invoke all other construction parts
61 initStatusBar();
62 initPhraseList();
63 initActions();
64 optionsDialog = new OptionsDialog(this);
65 connect (optionsDialog, SIGNAL(configurationChanged ()),
66 this, SLOT(slotConfigurationChanged ()));
67 connect (optionsDialog, SIGNAL(configurationChanged ()),
68 phraseList, SLOT(configureCompletion ()));
70 phrases = new KActionCollection (static_cast<QWidget*>(this));
72 readOptions();
73 ConfigWizard *wizard = new ConfigWizard (this, "ConfigWizard", config.data());
74 if (wizard->configurationNeeded ()) {
75 if (wizard->requestConfiguration ()) {
76 isConfigured = true;
77 saveOptions();
78 wizard->saveConfig (config.data());
79 readOptions();
81 else
82 isConfigured = false;
84 else
85 isConfigured = true;
86 delete wizard;
88 if (isConfigured) {
89 phraseList->configureCompletion();
92 ///////////////////////////////////////////////////////////////////
93 // disable actions at startup
94 fileSaveAs->setEnabled(false);
95 filePrint->setEnabled(false);
98 KMouthApp::~KMouthApp()
103 bool KMouthApp::configured() {
104 return isConfigured;
107 void KMouthApp::initActions() {
108 // The "File" menu
109 fileOpen = actionCollection()->addAction("file_open");
110 fileOpen->setIcon(KIcon("phrasehistory_open"));
111 fileOpen->setText(i18n("&Open as History..."));
112 fileOpen->setShortcuts(KStandardShortcut::open());
113 connect(fileOpen, SIGNAL(triggered(bool)), this, SLOT(slotFileOpen()));
114 fileOpen->setToolTip(i18n("Opens an existing file as history"));
115 fileOpen->setWhatsThis (i18n("Opens an existing file as history"));
117 fileSaveAs = actionCollection()->addAction("file_save_as");
118 fileSaveAs->setIcon(KIcon("phrasehistory_save"));
119 fileSaveAs->setText(i18n("Save &History As..."));
120 fileSaveAs->setShortcuts(KStandardShortcut::save());
121 connect(fileSaveAs, SIGNAL(triggered(bool)), this, SLOT(slotFileSaveAs()));
122 fileSaveAs->setToolTip(i18n("Saves the actual history as..."));
123 fileSaveAs->setWhatsThis (i18n("Saves the actual history as..."));
125 filePrint = actionCollection()->addAction("file_print");
126 filePrint->setIcon(KIcon("phrasehistory_print"));
127 filePrint->setText(i18n("&Print History..."));
128 filePrint->setShortcuts(KStandardShortcut::print());
129 connect(filePrint, SIGNAL(triggered(bool)), this, SLOT(slotFilePrint()));
130 filePrint->setToolTip(i18n("Prints out the actual history"));
131 filePrint->setWhatsThis (i18n("Prints out the actual history"));
133 fileQuit = KStandardAction::quit(this, SLOT(slotFileQuit()), actionCollection());
134 fileQuit->setToolTip(i18n("Quits the application"));
135 fileQuit->setWhatsThis (i18n("Quits the application"));
137 // The "Edit" menu
138 editCut = KStandardAction::cut(phraseList, SLOT(cut()), actionCollection());
139 editCut->setToolTip(i18n("Cuts the selected section and puts it to the clipboard"));
140 editCut->setWhatsThis (i18n("Cuts the selected section and puts it to the clipboard. If there is some text selected in the edit field it is placed it on the clipboard. Otherwise the selected sentences in the history (if any) are placed on the clipboard."));
142 editCopy = KStandardAction::copy(phraseList, SLOT(copy()), actionCollection());
143 editCopy->setToolTip(i18n("Copies the selected section to the clipboard"));
144 editCopy->setWhatsThis (i18n("Copies the selected section to the clipboard. If there is some text selected in the edit field it is copied to the clipboard. Otherwise the selected sentences in the history (if any) are copied to the clipboard."));
146 editPaste = KStandardAction::paste(phraseList, SLOT(paste()), actionCollection());
147 editPaste->setToolTip(i18n("Pastes the clipboard contents to actual position"));
148 editPaste->setWhatsThis (i18n("Pastes the clipboard contents at the current cursor position into the edit field."));
150 editSpeak = actionCollection()->addAction("edit_speak");
151 editSpeak->setIcon(KIcon("speak"));
152 editSpeak->setText(i18n("&Speak"));
153 connect(editSpeak, SIGNAL(triggered(bool)), phraseList, SLOT(speak()));
154 editSpeak->setToolTip(i18n("Speaks the currently active sentence(s)"));
155 editSpeak->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."));
157 // The "Phrase book" menu
158 phrasebookEdit = actionCollection()->addAction("phrasebook_edit");
159 phrasebookEdit->setText(i18n("&Edit..."));
160 connect(phrasebookEdit, SIGNAL(triggered(bool)), this, SLOT(slotEditPhrasebook()));
162 // The "Options" menu
163 viewMenuBar = KStandardAction::showMenubar(this, SLOT(slotViewMenuBar()), actionCollection());
164 // FIXME: Disable so it will compile.
165 // viewToolBar = KStandardAction::showToolbar(this, SLOT(slotViewToolBar()), actionCollection());
166 // viewToolBar->setToolTip(i18n("Enables/disables the toolbar"));
167 // viewToolBar->setWhatsThis (i18n("Enables/disables the toolbar"));
169 viewPhrasebookBar = actionCollection()->add<KToggleAction>("showPhrasebookBar");
170 viewPhrasebookBar->setText(i18n("Show P&hrasebook Bar"));
171 connect(viewPhrasebookBar, SIGNAL(triggered(bool)), this, SLOT(slotViewPhrasebookBar()));
172 viewPhrasebookBar->setToolTip(i18n("Enables/disables the phrasebook bar"));
173 viewPhrasebookBar->setWhatsThis (i18n("Enables/disables the phrasebook bar"));
175 viewStatusBar = KStandardAction::showStatusbar(this, SLOT(slotViewStatusBar()), actionCollection());
176 viewStatusBar->setToolTip(i18n("Enables/disables the statusbar"));
177 viewStatusBar->setWhatsThis (i18n("Enables/disables the statusbar"));
179 configureTTS = actionCollection()->addAction("configureTTS");
180 configureTTS->setIcon(KIcon("configure"));
181 configureTTS->setText(i18n("&Configure KMouth..."));
182 connect(configureTTS, SIGNAL(triggered(bool)), this, SLOT(slotConfigureTTS()));
183 configureTTS->setToolTip(i18n("Opens the configuration dialog"));
184 configureTTS->setWhatsThis (i18n("Opens the configuration dialog"));
186 // The "Help" menu
187 // The "Help" menu will automatically get created.
189 // The popup menu of the list of spoken sentences
190 phraseListSpeak = actionCollection()->addAction("phraselist_speak");
191 phraseListSpeak->setIcon(KIcon("speak"));
192 phraseListSpeak->setText(i18n("&Speak"));
193 phraseListSpeak->setToolTip(i18n("Speaks the currently selected phrases in the history"));
194 connect(phraseListSpeak, SIGNAL(triggered(bool)), phraseList, SLOT(speakListSelection()));
195 phraseListSpeak->setWhatsThis (i18n("Speaks the currently selected phrases in the history"));
197 phraseListRemove = actionCollection()->addAction("phraselist_remove");
198 phraseListRemove->setIcon(KIcon("edit-delete"));
199 phraseListRemove->setText(i18n("&Delete"));
200 connect(phraseListRemove, SIGNAL(triggered(bool)), phraseList, SLOT(removeListSelection()));
201 phraseListRemove->setToolTip(i18n("Deletes the currently selected phrases from the history"));
202 phraseListRemove->setWhatsThis (i18n("Deletes the currently selected phrases from the history"));
204 phraseListCut = actionCollection()->addAction("phraselist_cut");
205 phraseListCut->setIcon(KIcon("edit-cut"));
206 phraseListCut->setText(i18n("Cu&t"));
207 connect(phraseListCut, SIGNAL(triggered(bool)), phraseList, SLOT(cutListSelection()));
208 phraseListCut->setToolTip(i18n("Cuts the currently selected phrases from the history and puts them to the clipboard"));
209 phraseListCut->setWhatsThis (i18n("Cuts the currently selected phrases from the history and puts them to the clipboard"));
211 phraseListCopy = actionCollection()->addAction("phraselist_copy");
212 phraseListCopy->setIcon(KIcon("edit-copy"));
213 phraseListCopy->setText(i18n("&Copy"));
214 connect(phraseListCopy, SIGNAL(triggered(bool)), phraseList, SLOT(copyListSelection()));
215 phraseListCut->setToolTip(i18n("Copies the currently selected phrases from the history to the clipboard"));
216 phraseListCut->setWhatsThis (i18n("Copies the currently selected phrases from the history to the clipboard"));
218 phraselistSelectAll = actionCollection()->addAction("phraselist_select_all");
219 phraselistSelectAll->setText(i18n("Select &All Entries"));
220 connect(phraselistSelectAll, SIGNAL(triggered(bool)), phraseList, SLOT(selectAllEntries()));
221 phraselistSelectAll->setToolTip(i18n("Selects all phrases in the history"));
222 phraselistSelectAll->setWhatsThis (i18n("Selects all phrases in the history"));
224 phraselistDeselectAll = actionCollection()->addAction("phraselist_deselect_all");
225 phraselistDeselectAll->setText(i18n("D&eselect All Entries"));
226 connect(phraselistDeselectAll, SIGNAL(triggered(bool)), phraseList, SLOT(deselectAllEntries()));
227 phraselistDeselectAll->setToolTip(i18n("Deselects all phrases in the history"));
228 phraselistDeselectAll->setWhatsThis (i18n("Deselects all phrases in the history"));
230 // The popup menu of the edit field
231 // The popup menu of the edit field will automatically get created.
233 // use the absolute path to your kmouthui.rc file for testing purpose in createGUI();
234 createGUI();
237 void KMouthApp::initStatusBar()
239 ///////////////////////////////////////////////////////////////////
240 // STATUSBAR
241 // TODO: add your own items you need for displaying current application status.
242 statusBar()->insertItem(i18n("Ready."), ID_STATUS_MSG);
245 void KMouthApp::initPhraseList()
247 ////////////////////////////////////////////////////////////////////
248 // create the main widget here that is managed by KTMainWindow's view-region and
249 // connect the widget to your document to display document contents.
251 phraseList = new PhraseList(this);
252 setCentralWidget(phraseList);
255 void KMouthApp::openDocumentFile(const KUrl& url)
257 slotStatusMsg(i18n("Opening file..."));
259 phraseList->open (url);
260 slotStatusMsg(i18n("Ready."));
263 void KMouthApp::saveOptions() {
264 if (isConfigured) {
265 KConfigGroup cg( config, "General Options");
266 cg.writeEntry("Geometry", size());
267 cg.writeEntry("Show Menubar", viewMenuBar->isChecked());
268 // FIXME: Toolbar disabled so it will compile.
269 // cg.writeEntry("Show Toolbar", viewToolBar->isChecked());
270 cg.writeEntry("Show Phrasebook Bar", viewPhrasebookBar->isChecked());
271 cg.writeEntry("Show Statusbar",viewStatusBar->isChecked());
272 // FIXME: KToolBar no longer has barPos() method.
273 // cg.writeEntry("ToolBarPos", (int) toolBar("mainToolBar")->barPos());
275 if (phraseList != 0)
276 phraseList->saveCompletionOptions(config.data());
277 optionsDialog->saveOptions(config.data());
278 cg.changeGroup( "mainToolBar" );
279 toolBar("mainToolBar")->saveSettings( cg );
280 cg.changeGroup( "phrasebookBar");
281 toolBar("phrasebookBar")->saveSettings( cg );
286 void KMouthApp::readOptions()
288 KConfigGroup cg( config, "General Options");
290 // bar status settings
291 bool bViewMenubar = cg.readEntry("Show Menubar", true);
292 viewMenuBar->setChecked(bViewMenubar);
293 slotViewMenuBar();
295 // FIXME: Toolbar disabled so it will compile.
296 // bool bViewToolbar = cg.readEntry("Show Toolbar", QVariant(true)).toBool();
297 // viewToolBar->setChecked(bViewToolbar);
298 // slotViewToolBar();
300 bool bViewPhrasebookbar = cg.readEntry("Show Phrasebook Bar", true);
301 viewPhrasebookBar->setChecked(bViewPhrasebookbar);
303 bool bViewStatusbar = cg.readEntry("Show Statusbar", true);
304 viewStatusBar->setChecked(bViewStatusbar);
305 slotViewStatusBar();
308 // bar position settings
309 // FIXME:
310 // KToolBar::BarPosition toolBarPos;
311 // toolBarPos=(KToolBar::BarPosition) cg.readEntry("ToolBarPos", int(KToolBar::Top));
312 // toolBar("mainToolBar")->setBarPos(toolBarPos);
314 QSize size=cg.readEntry("Geometry",QSize());
315 if(!size.isEmpty())
317 resize(size);
320 optionsDialog->readOptions(config.data());
322 toolBar("mainToolBar")->applySettings (config->group( "mainToolBar" ) );
323 toolBar("phrasebookBar")->applySettings (config->group( "phrasebookBar") );
325 QString standardBook = KGlobal::dirs()->findResource("appdata", "standard.phrasebook");
326 if (!standardBook.isEmpty()) {
327 PhraseBook book;
328 book.open(KUrl::fromPathOrUrl( standardBook ));
329 slotPhrasebookConfirmed(book);
331 if (phraseList != 0)
332 phraseList->readCompletionOptions(config.data());
335 bool KMouthApp::queryClose()
337 return true;
340 bool KMouthApp::queryExit()
342 saveOptions();
343 return true;
346 void KMouthApp::enableMenuEntries(bool existSelectedEntries, bool existDeselectedEntries) {
347 bool existEntries = existSelectedEntries | existDeselectedEntries;
348 fileSaveAs->setEnabled (existEntries);
349 filePrint->setEnabled (existEntries);
351 phraselistSelectAll->setEnabled (existDeselectedEntries);
353 phraselistDeselectAll->setEnabled (existSelectedEntries);
354 phraseListSpeak->setEnabled (existSelectedEntries);
355 phraseListRemove->setEnabled (existSelectedEntries);
356 phraseListCut->setEnabled (existSelectedEntries);
357 phraseListCopy->setEnabled (existSelectedEntries);
360 /////////////////////////////////////////////////////////////////////
361 // SLOT IMPLEMENTATION
362 /////////////////////////////////////////////////////////////////////
364 void KMouthApp::slotFileOpen() {
365 slotStatusMsg(i18n("Opening file..."));
367 phraseList->open();
369 slotStatusMsg(i18n("Ready."));
372 void KMouthApp::slotFileSaveAs() {
373 slotStatusMsg(i18n("Saving history with a new filename..."));
375 phraseList->save();
377 slotStatusMsg(i18n("Ready."));
380 void KMouthApp::slotFilePrint()
382 slotStatusMsg(i18n("Printing..."));
384 QPrinter printer;
385 QPrintDialog printDialog(&printer, this);
386 if (printDialog.exec())
388 phraseList->print(&printer);
391 slotStatusMsg(i18n("Ready."));
394 void KMouthApp::slotFileQuit()
396 slotStatusMsg(i18n("Exiting..."));
397 saveOptions();
398 // close the first window, the list makes the next one the first again.
399 // This ensures that queryClose() is called on each window to ask for closing
400 KMainWindow* w;
401 if (!memberList().isEmpty())
403 for (int i = 0; i < memberList().size(); ++i)
405 // only close the window if the closeEvent is accepted. If the user presses Cancel on the saveModified() dialog,
406 // the window and the application stay open.
407 w = memberList().at(i);
408 if(!w->close())
409 break;
410 #ifdef __GNUC__
411 #warning "kde4: how remove it ?.???"
412 #endif
413 //memberList()->removeRef(w);
418 void KMouthApp::slotEditPhrasebook () {
419 PhraseBookDialog *phraseBookDialog = PhraseBookDialog::get();
420 // As we do not know whether the we are already connected to the slot,
421 // we first disconnect and then connect again.
422 disconnect (phraseBookDialog, SIGNAL(phrasebookConfirmed (PhraseBook &)),
423 this, SLOT(slotPhrasebookConfirmed (PhraseBook &)));
424 connect (phraseBookDialog, SIGNAL(phrasebookConfirmed (PhraseBook &)),
425 this, SLOT(slotPhrasebookConfirmed (PhraseBook &)));
427 // As we do not know whether the phrase book edit window is already open,
428 // we first open and then raise it, so that it is surely the top window.
429 phraseBookDialog->show();
430 phraseBookDialog->raise();
433 void KMouthApp::slotViewMenuBar() {
434 slotStatusMsg(i18n("Toggling menubar..."));
436 if(!viewMenuBar->isChecked())
437 menuBar()->hide();
438 else
439 menuBar()->show();
441 slotStatusMsg(i18n("Ready."));
444 void KMouthApp::slotViewToolBar()
446 slotStatusMsg(i18n("Toggling toolbar..."));
447 ///////////////////////////////////////////////////////////////////
448 // turn Toolbar on or off
449 if(!viewToolBar->isChecked())
451 toolBar("mainToolBar")->hide();
453 else
455 toolBar("mainToolBar")->show();
458 slotStatusMsg(i18n("Ready."));
461 void KMouthApp::slotViewPhrasebookBar()
463 slotStatusMsg(i18n("Toggling phrasebook bar..."));
464 ///////////////////////////////////////////////////////////////////
465 // turn Toolbar on or off
466 if(!viewPhrasebookBar->isChecked())
468 toolBar("phrasebookBar")->hide();
470 else
472 toolBar("phrasebookBar")->show();
475 slotStatusMsg(i18n("Ready."));
478 void KMouthApp::slotViewStatusBar()
480 slotStatusMsg(i18n("Toggle the statusbar..."));
481 ///////////////////////////////////////////////////////////////////
482 //turn Statusbar on or off
483 if(!viewStatusBar->isChecked())
485 statusBar()->hide();
487 else
489 statusBar()->show();
492 slotStatusMsg(i18n("Ready."));
495 void KMouthApp::slotConfigureTTS() {
496 phraseList->saveWordCompletion();
497 optionsDialog->show();
501 void KMouthApp::slotStatusMsg(const QString &text)
503 ///////////////////////////////////////////////////////////////////
504 // change status message permanently
505 statusBar()->clearMessage();
506 statusBar()->changeItem(text, ID_STATUS_MSG);
509 void KMouthApp::slotPhrasebookConfirmed (PhraseBook &book) {
510 QString name = "phrasebooks";
511 QMenu *popup = (QMenu *)factory()->container(name, this);
512 KToolBar *toolbar = toolBar ("phrasebookBar");
514 delete phrases;
515 phrases = new KActionCollection (actionCollection());
516 book.addToGUI (popup, toolbar, phrases, this, SLOT(slotPhraseSelected (const QString &)));
518 QString bookLocation = KGlobal::dirs()->saveLocation ("appdata", "/");
519 if (!bookLocation.isNull() && !bookLocation.isEmpty()) {
520 book.save (KUrl::fromPathOrUrl( bookLocation + "standard.phrasebook" ));
524 void KMouthApp::slotConfigurationChanged()
526 optionsDialog->saveOptions (config.data());
529 void KMouthApp::slotPhraseSelected (const QString &phrase) {
530 phraseList->insert (phrase);
531 if (optionsDialog->isSpeakImmediately())
532 phraseList->speak ();
535 TextToSpeechSystem *KMouthApp::getTTSSystem() const {
536 return optionsDialog->getTTSSystem();
539 #include "kmouth.moc"