A few new icons...
[kdeaccessibility.git] / kmouth / kmouth.cpp
blobc78a0107978f7d0cf9068275cca26c61b146e6c1
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 // include files for QT
19 #include <qdir.h>
20 #include <qpainter.h>
22 // include files for KDE
23 #include <kiconloader.h>
24 #include <kmenubar.h>
25 #include <kstatusbar.h>
26 #include <klocale.h>
27 #include <kconfig.h>
28 #include <kstdaction.h>
29 #include <kprinter.h>
30 #include <kpopupmenu.h>
31 #include <kstandarddirs.h>
33 // application specific includes
34 #include "kmouth.h"
35 #include "phraselist.h"
36 #include "phrasebook/phrasebook.h"
37 #include "phrasebook/phrasebookdialog.h"
38 #include "optionsdialog.h"
39 #include "configwizard.h"
41 #define ID_STATUS_MSG 1
43 KMouthApp::KMouthApp(QWidget* , const char* name):KMainWindow(0, name)
45 isConfigured = false;
46 config=kapp->config();
48 ///////////////////////////////////////////////////////////////////
49 // call inits to invoke all other construction parts
50 initStatusBar();
51 initPhraseList();
52 initActions();
53 optionsDialog = new OptionsDialog(this);
54 connect (optionsDialog, SIGNAL(configurationChanged ()),
55 this, SLOT(slotConfigurationChanged ()));
56 connect (optionsDialog, SIGNAL(configurationChanged ()),
57 phraseList, SLOT(configureCompletion ()));
59 phrases = new KActionCollection (this);
61 readOptions();
62 ConfigWizard *wizard = new ConfigWizard (this, "ConfigWizard", config);
63 if (wizard->configurationNeeded ()) {
64 if (wizard->requestConfiguration ()) {
65 isConfigured = true;
66 saveOptions();
67 wizard->saveConfig (config);
68 readOptions();
70 else
71 isConfigured = false;
73 else
74 isConfigured = true;
75 delete wizard;
77 if (isConfigured) {
78 phraseList->configureCompletion();
81 ///////////////////////////////////////////////////////////////////
82 // disable actions at startup
83 fileSaveAs->setEnabled(false);
84 filePrint->setEnabled(false);
87 KMouthApp::~KMouthApp()
92 bool KMouthApp::configured() {
93 return isConfigured;
96 void KMouthApp::initActions() {
97 // The "File" menu
98 fileOpen = new KAction(i18n("&Open as History..."), "phrasehistory_open", KStdAccel::open(), this, SLOT(slotFileOpen()), actionCollection(),"file_open");
99 fileOpen->setStatusText(i18n("Opens an existing file as history"));
100 fileOpen->setWhatsThis (i18n("Opens an existing file as history"));
102 fileSaveAs = new KAction(i18n("Save &History As..."), "phrasehistory_save", KStdAccel::save(), this, SLOT(slotFileSaveAs()), actionCollection(),"file_save_as");
103 fileSaveAs->setStatusText(i18n("Saves the actual history as..."));
104 fileSaveAs->setWhatsThis (i18n("Saves the actual history as..."));
106 filePrint = new KAction(i18n("&Print History..."), "phrasehistory_print", KStdAccel::print(), this, SLOT(slotFilePrint()), actionCollection(),"file_print");
107 filePrint->setStatusText(i18n("Prints out the actual history"));
108 filePrint->setWhatsThis (i18n("Prints out the actual history"));
110 fileQuit = KStdAction::quit(this, SLOT(slotFileQuit()), actionCollection());
111 fileQuit->setStatusText(i18n("Quits the application"));
112 fileQuit->setWhatsThis (i18n("Quits the application"));
114 // The "Edit" menu
115 editCut = KStdAction::cut(phraseList, SLOT(cut()), actionCollection());
116 editCut->setStatusText(i18n("Cuts the selected section and puts it to the clipboard"));
117 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."));
119 editCopy = KStdAction::copy(phraseList, SLOT(copy()), actionCollection());
120 editCopy->setStatusText(i18n("Copies the selected section to the clipboard"));
121 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."));
123 editPaste = KStdAction::paste(phraseList, SLOT(paste()), actionCollection());
124 editPaste->setStatusText(i18n("Pastes the clipboard contents to actual position"));
125 editPaste->setWhatsThis (i18n("Pastes the clipboard contents at the current cursor position into the edit field."));
127 editSpeak = new KAction (i18n("&Speak"), "speak", 0, phraseList, SLOT(speak()), actionCollection(),"edit_speak");
128 editSpeak->setStatusText(i18n("Speaks the currently active sentence(s)"));
129 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."));
131 // The "Phrase book" menu
132 phrasebookEdit = new KAction(i18n("&Edit..."), 0, 0, this, SLOT(slotEditPhrasebook()), actionCollection(),"phrasebook_edit");
134 // The "Options" menu
135 viewMenuBar = KStdAction::showMenubar(this, SLOT(slotViewMenuBar()), actionCollection());
136 viewToolBar = KStdAction::showToolbar(this, SLOT(slotViewToolBar()), actionCollection());
137 viewToolBar->setStatusText(i18n("Enables/disables the toolbar"));
138 viewToolBar->setWhatsThis (i18n("Enables/disables the toolbar"));
140 viewPhrasebookBar = new KToggleAction (i18n("Show P&hrasebook Bar"), 0, 0, this, SLOT(slotViewPhrasebookBar()), actionCollection(), "showPhrasebookBar");
141 viewPhrasebookBar->setStatusText(i18n("Enables/disables the phrasebook bar"));
142 viewPhrasebookBar->setWhatsThis (i18n("Enables/disables the phrasebook bar"));
144 viewStatusBar = KStdAction::showStatusbar(this, SLOT(slotViewStatusBar()), actionCollection());
145 viewStatusBar->setStatusText(i18n("Enables/disables the statusbar"));
146 viewStatusBar->setWhatsThis (i18n("Enables/disables the statusbar"));
148 configureTTS = new KAction (i18n("&Configure KMouth..."), "configure", 0, this, SLOT(slotConfigureTTS()), actionCollection(), "configureTTS");
149 configureTTS->setStatusText(i18n("Opens the configuration dialog"));
150 configureTTS->setWhatsThis (i18n("Opens the configuration dialog"));
152 // The "Help" menu
153 // The "Help" menu will automatically get created.
155 // The popup menu of the list of spoken sentences
156 phraseListSpeak = new KAction (i18n("&Speak"), "speak", 0, phraseList, SLOT(speakListSelection()), actionCollection(), "phraselist_speak");
157 phraseListSpeak->setStatusText(i18n("Speaks the currently selected phrases in the history"));
158 phraseListSpeak->setWhatsThis (i18n("Speaks the currently selected phrases in the history"));
160 phraseListRemove = new KAction (i18n("&Delete"), "editdelete", 0, phraseList, SLOT(removeListSelection()), actionCollection(), "phraselist_remove");
161 phraseListRemove->setStatusText(i18n("Deletes the currently selected phrases from the history"));
162 phraseListRemove->setWhatsThis (i18n("Deletes the currently selected phrases from the history"));
164 phraseListCut = new KAction (i18n("Cu&t"), "editcut", 0, phraseList, SLOT(cutListSelection()), actionCollection(), "phraselist_cut");
165 phraseListCut->setStatusText(i18n("Cuts the currently selected phrases from the history and puts them to the clipboard"));
166 phraseListCut->setWhatsThis (i18n("Cuts the currently selected phrases from the history and puts them to the clipboard"));
168 phraseListCopy = new KAction (i18n("&Copy"), "editcopy", 0, phraseList, SLOT(copyListSelection()), actionCollection(), "phraselist_copy");
169 phraseListCut->setStatusText(i18n("Copies the currently selected phrases from the history to the clipboard"));
170 phraseListCut->setWhatsThis (i18n("Copies the currently selected phrases from the history to the clipboard"));
172 phraselistSelectAll = new KAction (i18n("Select &All Entries"), 0, 0, phraseList, SLOT(selectAllEntries()), actionCollection(),"phraselist_select_all");
173 phraselistSelectAll->setStatusText(i18n("Selects all phrases in the history"));
174 phraselistSelectAll->setWhatsThis (i18n("Selects all phrases in the history"));
176 phraselistDeselectAll = new KAction (i18n("D&eselect All Entries"), 0, 0, phraseList, SLOT(deselectAllEntries()), actionCollection(),"phraselist_deselect_all");
177 phraselistDeselectAll->setStatusText(i18n("Deselects all phrases in the history"));
178 phraselistDeselectAll->setWhatsThis (i18n("Deselects all phrases in the history"));
180 // The popup menu of the edit field
181 // The popup menu of the edit field will automatically get created.
183 // use the absolute path to your kmouthui.rc file for testing purpose in createGUI();
184 createGUI();
187 void KMouthApp::initStatusBar()
189 ///////////////////////////////////////////////////////////////////
190 // STATUSBAR
191 // TODO: add your own items you need for displaying current application status.
192 statusBar()->insertItem(i18n("Ready."), ID_STATUS_MSG);
195 void KMouthApp::initPhraseList()
197 ////////////////////////////////////////////////////////////////////
198 // create the main widget here that is managed by KTMainWindow's view-region and
199 // connect the widget to your document to display document contents.
201 phraseList = new PhraseList(this);
202 setCentralWidget(phraseList);
205 void KMouthApp::openDocumentFile(const KURL& url)
207 slotStatusMsg(i18n("Opening file..."));
209 phraseList->open (url);
210 slotStatusMsg(i18n("Ready."));
213 void KMouthApp::saveOptions() {
214 if (isConfigured) {
215 config->setGroup("General Options");
216 config->writeEntry("Geometry", size());
217 config->writeEntry("Show Menubar", viewMenuBar->isChecked());
218 config->writeEntry("Show Toolbar", viewToolBar->isChecked());
219 config->writeEntry("Show Phrasebook Bar", viewPhrasebookBar->isChecked());
220 config->writeEntry("Show Statusbar",viewStatusBar->isChecked());
221 config->writeEntry("ToolBarPos", (int) toolBar("mainToolBar")->barPos());
223 if (phraseList != 0)
224 phraseList->saveCompletionOptions(config);
225 optionsDialog->saveOptions(config);
226 toolBar("mainToolBar")->saveSettings (config, "mainToolBar");
227 toolBar("phrasebookBar")->saveSettings (config, "phrasebookBar");
232 void KMouthApp::readOptions()
234 config->setGroup("General Options");
236 // bar status settings
237 bool bViewMenubar = config->readBoolEntry("Show Menubar", true);
238 viewMenuBar->setChecked(bViewMenubar);
239 slotViewMenuBar();
241 bool bViewToolbar = config->readBoolEntry("Show Toolbar", true);
242 viewToolBar->setChecked(bViewToolbar);
243 slotViewToolBar();
245 bool bViewPhrasebookbar = config->readBoolEntry("Show Phrasebook Bar", true);
246 viewPhrasebookBar->setChecked(bViewPhrasebookbar);
248 bool bViewStatusbar = config->readBoolEntry("Show Statusbar", true);
249 viewStatusBar->setChecked(bViewStatusbar);
250 slotViewStatusBar();
253 // bar position settings
254 KToolBar::BarPosition toolBarPos;
255 toolBarPos=(KToolBar::BarPosition) config->readNumEntry("ToolBarPos", KToolBar::Top);
256 toolBar("mainToolBar")->setBarPos(toolBarPos);
258 QSize size=config->readSizeEntry("Geometry");
259 if(!size.isEmpty())
261 resize(size);
264 optionsDialog->readOptions(config);
266 toolBar("mainToolBar")->applySettings (config, "mainToolBar");
267 toolBar("phrasebookBar")->applySettings (config, "phrasebookBar");
269 QString standardBook = KApplication::kApplication()->dirs()->findResource("appdata", "standard.phrasebook");
270 if (!standardBook.isEmpty()) {
271 PhraseBook book;
272 book.open(KURL::fromPathOrURL( standardBook ));
273 slotPhrasebookConfirmed(book);
275 if (phraseList != 0)
276 phraseList->readCompletionOptions(config);
279 bool KMouthApp::queryClose()
281 return true;
284 bool KMouthApp::queryExit()
286 saveOptions();
287 return true;
290 void KMouthApp::enableMenuEntries(bool existSelectedEntries, bool existDeselectedEntries) {
291 bool existEntries = existSelectedEntries | existDeselectedEntries;
292 fileSaveAs->setEnabled (existEntries);
293 filePrint->setEnabled (existEntries);
295 phraselistSelectAll->setEnabled (existDeselectedEntries);
297 phraselistDeselectAll->setEnabled (existSelectedEntries);
298 phraseListSpeak->setEnabled (existSelectedEntries);
299 phraseListRemove->setEnabled (existSelectedEntries);
300 phraseListCut->setEnabled (existSelectedEntries);
301 phraseListCopy->setEnabled (existSelectedEntries);
304 /////////////////////////////////////////////////////////////////////
305 // SLOT IMPLEMENTATION
306 /////////////////////////////////////////////////////////////////////
308 void KMouthApp::slotFileOpen() {
309 slotStatusMsg(i18n("Opening file..."));
311 phraseList->open();
313 slotStatusMsg(i18n("Ready."));
316 void KMouthApp::slotFileSaveAs() {
317 slotStatusMsg(i18n("Saving history with a new filename..."));
319 phraseList->save();
321 slotStatusMsg(i18n("Ready."));
324 void KMouthApp::slotFilePrint()
326 slotStatusMsg(i18n("Printing..."));
328 KPrinter printer;
329 if (printer.setup(this))
331 phraseList->print(&printer);
334 slotStatusMsg(i18n("Ready."));
337 void KMouthApp::slotFileQuit()
339 slotStatusMsg(i18n("Exiting..."));
340 saveOptions();
341 // close the first window, the list makes the next one the first again.
342 // This ensures that queryClose() is called on each window to ask for closing
343 KMainWindow* w;
344 if (memberList)
346 for(w=memberList->first(); w!=0; w=memberList->first())
348 // only close the window if the closeEvent is accepted. If the user presses Cancel on the saveModified() dialog,
349 // the window and the application stay open.
350 if(!w->close())
351 break;
352 memberList->removeRef(w);
357 void KMouthApp::slotEditPhrasebook () {
358 PhraseBookDialog *phraseBookDialog = PhraseBookDialog::get();
359 // As we do not know whether the we are already connected to the slot,
360 // we first disconnect and then connect again.
361 disconnect (phraseBookDialog, SIGNAL(phrasebookConfirmed (PhraseBook &)),
362 this, SLOT(slotPhrasebookConfirmed (PhraseBook &)));
363 connect (phraseBookDialog, SIGNAL(phrasebookConfirmed (PhraseBook &)),
364 this, SLOT(slotPhrasebookConfirmed (PhraseBook &)));
366 // As we do not know whether the phrase book edit window is already open,
367 // we first open and then raise it, so that it is surely the top window.
368 phraseBookDialog->show();
369 phraseBookDialog->raise();
372 void KMouthApp::slotViewMenuBar() {
373 slotStatusMsg(i18n("Toggling menubar..."));
375 if(!viewMenuBar->isChecked())
376 menuBar()->hide();
377 else
378 menuBar()->show();
380 slotStatusMsg(i18n("Ready."));
383 void KMouthApp::slotViewToolBar()
385 slotStatusMsg(i18n("Toggling toolbar..."));
386 ///////////////////////////////////////////////////////////////////
387 // turn Toolbar on or off
388 if(!viewToolBar->isChecked())
390 toolBar("mainToolBar")->hide();
392 else
394 toolBar("mainToolBar")->show();
397 slotStatusMsg(i18n("Ready."));
400 void KMouthApp::slotViewPhrasebookBar()
402 slotStatusMsg(i18n("Toggling phrasebook bar..."));
403 ///////////////////////////////////////////////////////////////////
404 // turn Toolbar on or off
405 if(!viewPhrasebookBar->isChecked())
407 toolBar("phrasebookBar")->hide();
409 else
411 toolBar("phrasebookBar")->show();
414 slotStatusMsg(i18n("Ready."));
417 void KMouthApp::slotViewStatusBar()
419 slotStatusMsg(i18n("Toggle the statusbar..."));
420 ///////////////////////////////////////////////////////////////////
421 //turn Statusbar on or off
422 if(!viewStatusBar->isChecked())
424 statusBar()->hide();
426 else
428 statusBar()->show();
431 slotStatusMsg(i18n("Ready."));
434 void KMouthApp::slotConfigureTTS() {
435 phraseList->saveWordCompletion();
436 optionsDialog->show();
440 void KMouthApp::slotStatusMsg(const QString &text)
442 ///////////////////////////////////////////////////////////////////
443 // change status message permanently
444 statusBar()->clear();
445 statusBar()->changeItem(text, ID_STATUS_MSG);
448 void KMouthApp::slotPhrasebookConfirmed (PhraseBook &book) {
449 QString name = "phrasebooks";
450 QPopupMenu *popup = (QPopupMenu *)factory()->container(name, this);
451 KToolBar *toolbar = toolBar ("phrasebookBar");
453 KActionPtrList actions = phrases->actions ();
454 KActionPtrList::iterator iter;
455 for (iter = actions.begin(); iter != actions.end(); ++iter) {
456 (*iter)->unplugAll();
458 delete phrases;
460 phrases = new KActionCollection (this, actionCollection());
461 book.addToGUI (popup, toolbar, phrases, this, SLOT(slotPhraseSelected (const QString &)));
463 QString bookLocation = KApplication::kApplication()->dirs()->saveLocation ("appdata", "/");
464 if (!bookLocation.isNull() && !bookLocation.isEmpty()) {
465 book.save (KURL::fromPathOrURL( bookLocation + "standard.phrasebook" ));
469 void KMouthApp::slotConfigurationChanged()
471 optionsDialog->saveOptions (config);
474 void KMouthApp::slotPhraseSelected (const QString &phrase) {
475 phraseList->insert (phrase);
476 if (optionsDialog->isSpeakImmediately())
477 phraseList->speak ();
480 TextToSpeechSystem *KMouthApp::getTTSSystem() const {
481 return optionsDialog->getTTSSystem();
484 #include "kmouth.moc"