Port to qt4
[kdeaccessibility.git] / kmouth / kmouth.cpp
blob8f6ef3052f0a6b99a210d86e2f09c96a2fd36758
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>
21 //Added by qt3to4:
22 #include <Q3PopupMenu>
24 // include files for KDE
25 #include <kiconloader.h>
26 #include <kmenubar.h>
27 #include <kstatusbar.h>
28 #include <klocale.h>
29 #include <kconfig.h>
30 #include <kstdaction.h>
31 #include <kprinter.h>
32 #include <kpopupmenu.h>
33 #include <kstandarddirs.h>
35 // application specific includes
36 #include "kmouth.h"
37 #include "phraselist.h"
38 #include "phrasebook/phrasebook.h"
39 #include "phrasebook/phrasebookdialog.h"
40 #include "optionsdialog.h"
41 #include "configwizard.h"
43 #define ID_STATUS_MSG 1
45 KMouthApp::KMouthApp(QWidget* , const char* name):KMainWindow(0, name)
47 isConfigured = false;
48 config=kapp->config();
50 ///////////////////////////////////////////////////////////////////
51 // call inits to invoke all other construction parts
52 initStatusBar();
53 initPhraseList();
54 initActions();
55 optionsDialog = new OptionsDialog(this);
56 connect (optionsDialog, SIGNAL(configurationChanged ()),
57 this, SLOT(slotConfigurationChanged ()));
58 connect (optionsDialog, SIGNAL(configurationChanged ()),
59 phraseList, SLOT(configureCompletion ()));
61 phrases = new KActionCollection (this);
63 readOptions();
64 ConfigWizard *wizard = new ConfigWizard (this, "ConfigWizard", config);
65 if (wizard->configurationNeeded ()) {
66 if (wizard->requestConfiguration ()) {
67 isConfigured = true;
68 saveOptions();
69 wizard->saveConfig (config);
70 readOptions();
72 else
73 isConfigured = false;
75 else
76 isConfigured = true;
77 delete wizard;
79 if (isConfigured) {
80 phraseList->configureCompletion();
83 ///////////////////////////////////////////////////////////////////
84 // disable actions at startup
85 fileSaveAs->setEnabled(false);
86 filePrint->setEnabled(false);
89 KMouthApp::~KMouthApp()
94 bool KMouthApp::configured() {
95 return isConfigured;
98 void KMouthApp::initActions() {
99 // The "File" menu
100 fileOpen = new KAction(i18n("&Open as History..."), "phrasehistory_open", KStdAccel::open(), this, SLOT(slotFileOpen()), actionCollection(),"file_open");
101 fileOpen->setStatusText(i18n("Opens an existing file as history"));
102 fileOpen->setWhatsThis (i18n("Opens an existing file as history"));
104 fileSaveAs = new KAction(i18n("Save &History As..."), "phrasehistory_save", KStdAccel::save(), this, SLOT(slotFileSaveAs()), actionCollection(),"file_save_as");
105 fileSaveAs->setStatusText(i18n("Saves the actual history as..."));
106 fileSaveAs->setWhatsThis (i18n("Saves the actual history as..."));
108 filePrint = new KAction(i18n("&Print History..."), "phrasehistory_print", KStdAccel::print(), this, SLOT(slotFilePrint()), actionCollection(),"file_print");
109 filePrint->setStatusText(i18n("Prints out the actual history"));
110 filePrint->setWhatsThis (i18n("Prints out the actual history"));
112 fileQuit = KStdAction::quit(this, SLOT(slotFileQuit()), actionCollection());
113 fileQuit->setStatusText(i18n("Quits the application"));
114 fileQuit->setWhatsThis (i18n("Quits the application"));
116 // The "Edit" menu
117 editCut = KStdAction::cut(phraseList, SLOT(cut()), actionCollection());
118 editCut->setStatusText(i18n("Cuts the selected section and puts it to the clipboard"));
119 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."));
121 editCopy = KStdAction::copy(phraseList, SLOT(copy()), actionCollection());
122 editCopy->setStatusText(i18n("Copies the selected section to the clipboard"));
123 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."));
125 editPaste = KStdAction::paste(phraseList, SLOT(paste()), actionCollection());
126 editPaste->setStatusText(i18n("Pastes the clipboard contents to actual position"));
127 editPaste->setWhatsThis (i18n("Pastes the clipboard contents at the current cursor position into the edit field."));
129 editSpeak = new KAction (i18n("&Speak"), "speak", 0, phraseList, SLOT(speak()), actionCollection(),"edit_speak");
130 editSpeak->setStatusText(i18n("Speaks the currently active sentence(s)"));
131 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."));
133 // The "Phrase book" menu
134 phrasebookEdit = new KAction(i18n("&Edit..."), 0, 0, this, SLOT(slotEditPhrasebook()), actionCollection(),"phrasebook_edit");
136 // The "Options" menu
137 viewMenuBar = KStdAction::showMenubar(this, SLOT(slotViewMenuBar()), actionCollection());
138 viewToolBar = KStdAction::showToolbar(this, SLOT(slotViewToolBar()), actionCollection());
139 viewToolBar->setStatusText(i18n("Enables/disables the toolbar"));
140 viewToolBar->setWhatsThis (i18n("Enables/disables the toolbar"));
142 viewPhrasebookBar = new KToggleAction (i18n("Show P&hrasebook Bar"), 0, 0, this, SLOT(slotViewPhrasebookBar()), actionCollection(), "showPhrasebookBar");
143 viewPhrasebookBar->setStatusText(i18n("Enables/disables the phrasebook bar"));
144 viewPhrasebookBar->setWhatsThis (i18n("Enables/disables the phrasebook bar"));
146 viewStatusBar = KStdAction::showStatusbar(this, SLOT(slotViewStatusBar()), actionCollection());
147 viewStatusBar->setStatusText(i18n("Enables/disables the statusbar"));
148 viewStatusBar->setWhatsThis (i18n("Enables/disables the statusbar"));
150 configureTTS = new KAction (i18n("&Configure KMouth..."), "configure", 0, this, SLOT(slotConfigureTTS()), actionCollection(), "configureTTS");
151 configureTTS->setStatusText(i18n("Opens the configuration dialog"));
152 configureTTS->setWhatsThis (i18n("Opens the configuration dialog"));
154 // The "Help" menu
155 // The "Help" menu will automatically get created.
157 // The popup menu of the list of spoken sentences
158 phraseListSpeak = new KAction (i18n("&Speak"), "speak", 0, phraseList, SLOT(speakListSelection()), actionCollection(), "phraselist_speak");
159 phraseListSpeak->setStatusText(i18n("Speaks the currently selected phrases in the history"));
160 phraseListSpeak->setWhatsThis (i18n("Speaks the currently selected phrases in the history"));
162 phraseListRemove = new KAction (i18n("&Delete"), "editdelete", 0, phraseList, SLOT(removeListSelection()), actionCollection(), "phraselist_remove");
163 phraseListRemove->setStatusText(i18n("Deletes the currently selected phrases from the history"));
164 phraseListRemove->setWhatsThis (i18n("Deletes the currently selected phrases from the history"));
166 phraseListCut = new KAction (i18n("Cu&t"), "editcut", 0, phraseList, SLOT(cutListSelection()), actionCollection(), "phraselist_cut");
167 phraseListCut->setStatusText(i18n("Cuts the currently selected phrases from the history and puts them to the clipboard"));
168 phraseListCut->setWhatsThis (i18n("Cuts the currently selected phrases from the history and puts them to the clipboard"));
170 phraseListCopy = new KAction (i18n("&Copy"), "editcopy", 0, phraseList, SLOT(copyListSelection()), actionCollection(), "phraselist_copy");
171 phraseListCut->setStatusText(i18n("Copies the currently selected phrases from the history to the clipboard"));
172 phraseListCut->setWhatsThis (i18n("Copies the currently selected phrases from the history to the clipboard"));
174 phraselistSelectAll = new KAction (i18n("Select &All Entries"), 0, 0, phraseList, SLOT(selectAllEntries()), actionCollection(),"phraselist_select_all");
175 phraselistSelectAll->setStatusText(i18n("Selects all phrases in the history"));
176 phraselistSelectAll->setWhatsThis (i18n("Selects all phrases in the history"));
178 phraselistDeselectAll = new KAction (i18n("D&eselect All Entries"), 0, 0, phraseList, SLOT(deselectAllEntries()), actionCollection(),"phraselist_deselect_all");
179 phraselistDeselectAll->setStatusText(i18n("Deselects all phrases in the history"));
180 phraselistDeselectAll->setWhatsThis (i18n("Deselects all phrases in the history"));
182 // The popup menu of the edit field
183 // The popup menu of the edit field will automatically get created.
185 // use the absolute path to your kmouthui.rc file for testing purpose in createGUI();
186 createGUI();
189 void KMouthApp::initStatusBar()
191 ///////////////////////////////////////////////////////////////////
192 // STATUSBAR
193 // TODO: add your own items you need for displaying current application status.
194 statusBar()->insertItem(i18n("Ready."), ID_STATUS_MSG);
197 void KMouthApp::initPhraseList()
199 ////////////////////////////////////////////////////////////////////
200 // create the main widget here that is managed by KTMainWindow's view-region and
201 // connect the widget to your document to display document contents.
203 phraseList = new PhraseList(this);
204 setCentralWidget(phraseList);
207 void KMouthApp::openDocumentFile(const KURL& url)
209 slotStatusMsg(i18n("Opening file..."));
211 phraseList->open (url);
212 slotStatusMsg(i18n("Ready."));
215 void KMouthApp::saveOptions() {
216 if (isConfigured) {
217 config->setGroup("General Options");
218 config->writeEntry("Geometry", size());
219 config->writeEntry("Show Menubar", viewMenuBar->isChecked());
220 config->writeEntry("Show Toolbar", viewToolBar->isChecked());
221 config->writeEntry("Show Phrasebook Bar", viewPhrasebookBar->isChecked());
222 config->writeEntry("Show Statusbar",viewStatusBar->isChecked());
223 config->writeEntry("ToolBarPos", (int) toolBar("mainToolBar")->barPos());
225 if (phraseList != 0)
226 phraseList->saveCompletionOptions(config);
227 optionsDialog->saveOptions(config);
228 toolBar("mainToolBar")->saveSettings (config, "mainToolBar");
229 toolBar("phrasebookBar")->saveSettings (config, "phrasebookBar");
234 void KMouthApp::readOptions()
236 config->setGroup("General Options");
238 // bar status settings
239 bool bViewMenubar = config->readBoolEntry("Show Menubar", true);
240 viewMenuBar->setChecked(bViewMenubar);
241 slotViewMenuBar();
243 bool bViewToolbar = config->readBoolEntry("Show Toolbar", true);
244 viewToolBar->setChecked(bViewToolbar);
245 slotViewToolBar();
247 bool bViewPhrasebookbar = config->readBoolEntry("Show Phrasebook Bar", true);
248 viewPhrasebookBar->setChecked(bViewPhrasebookbar);
250 bool bViewStatusbar = config->readBoolEntry("Show Statusbar", true);
251 viewStatusBar->setChecked(bViewStatusbar);
252 slotViewStatusBar();
255 // bar position settings
256 KToolBar::BarPosition toolBarPos;
257 toolBarPos=(KToolBar::BarPosition) config->readNumEntry("ToolBarPos", KToolBar::Top);
258 toolBar("mainToolBar")->setBarPos(toolBarPos);
260 QSize size=config->readSizeEntry("Geometry");
261 if(!size.isEmpty())
263 resize(size);
266 optionsDialog->readOptions(config);
268 toolBar("mainToolBar")->applySettings (config, "mainToolBar");
269 toolBar("phrasebookBar")->applySettings (config, "phrasebookBar");
271 QString standardBook = KApplication::kApplication()->dirs()->findResource("appdata", "standard.phrasebook");
272 if (!standardBook.isEmpty()) {
273 PhraseBook book;
274 book.open(KURL::fromPathOrURL( standardBook ));
275 slotPhrasebookConfirmed(book);
277 if (phraseList != 0)
278 phraseList->readCompletionOptions(config);
281 bool KMouthApp::queryClose()
283 return true;
286 bool KMouthApp::queryExit()
288 saveOptions();
289 return true;
292 void KMouthApp::enableMenuEntries(bool existSelectedEntries, bool existDeselectedEntries) {
293 bool existEntries = existSelectedEntries | existDeselectedEntries;
294 fileSaveAs->setEnabled (existEntries);
295 filePrint->setEnabled (existEntries);
297 phraselistSelectAll->setEnabled (existDeselectedEntries);
299 phraselistDeselectAll->setEnabled (existSelectedEntries);
300 phraseListSpeak->setEnabled (existSelectedEntries);
301 phraseListRemove->setEnabled (existSelectedEntries);
302 phraseListCut->setEnabled (existSelectedEntries);
303 phraseListCopy->setEnabled (existSelectedEntries);
306 /////////////////////////////////////////////////////////////////////
307 // SLOT IMPLEMENTATION
308 /////////////////////////////////////////////////////////////////////
310 void KMouthApp::slotFileOpen() {
311 slotStatusMsg(i18n("Opening file..."));
313 phraseList->open();
315 slotStatusMsg(i18n("Ready."));
318 void KMouthApp::slotFileSaveAs() {
319 slotStatusMsg(i18n("Saving history with a new filename..."));
321 phraseList->save();
323 slotStatusMsg(i18n("Ready."));
326 void KMouthApp::slotFilePrint()
328 slotStatusMsg(i18n("Printing..."));
330 KPrinter printer;
331 if (printer.setup(this))
333 phraseList->print(&printer);
336 slotStatusMsg(i18n("Ready."));
339 void KMouthApp::slotFileQuit()
341 slotStatusMsg(i18n("Exiting..."));
342 saveOptions();
343 // close the first window, the list makes the next one the first again.
344 // This ensures that queryClose() is called on each window to ask for closing
345 KMainWindow* w;
346 if (memberList)
348 for(w=memberList->first(); w!=0; w=memberList->first())
350 // only close the window if the closeEvent is accepted. If the user presses Cancel on the saveModified() dialog,
351 // the window and the application stay open.
352 if(!w->close())
353 break;
354 memberList->removeRef(w);
359 void KMouthApp::slotEditPhrasebook () {
360 PhraseBookDialog *phraseBookDialog = PhraseBookDialog::get();
361 // As we do not know whether the we are already connected to the slot,
362 // we first disconnect and then connect again.
363 disconnect (phraseBookDialog, SIGNAL(phrasebookConfirmed (PhraseBook &)),
364 this, SLOT(slotPhrasebookConfirmed (PhraseBook &)));
365 connect (phraseBookDialog, SIGNAL(phrasebookConfirmed (PhraseBook &)),
366 this, SLOT(slotPhrasebookConfirmed (PhraseBook &)));
368 // As we do not know whether the phrase book edit window is already open,
369 // we first open and then raise it, so that it is surely the top window.
370 phraseBookDialog->show();
371 phraseBookDialog->raise();
374 void KMouthApp::slotViewMenuBar() {
375 slotStatusMsg(i18n("Toggling menubar..."));
377 if(!viewMenuBar->isChecked())
378 menuBar()->hide();
379 else
380 menuBar()->show();
382 slotStatusMsg(i18n("Ready."));
385 void KMouthApp::slotViewToolBar()
387 slotStatusMsg(i18n("Toggling toolbar..."));
388 ///////////////////////////////////////////////////////////////////
389 // turn Toolbar on or off
390 if(!viewToolBar->isChecked())
392 toolBar("mainToolBar")->hide();
394 else
396 toolBar("mainToolBar")->show();
399 slotStatusMsg(i18n("Ready."));
402 void KMouthApp::slotViewPhrasebookBar()
404 slotStatusMsg(i18n("Toggling phrasebook bar..."));
405 ///////////////////////////////////////////////////////////////////
406 // turn Toolbar on or off
407 if(!viewPhrasebookBar->isChecked())
409 toolBar("phrasebookBar")->hide();
411 else
413 toolBar("phrasebookBar")->show();
416 slotStatusMsg(i18n("Ready."));
419 void KMouthApp::slotViewStatusBar()
421 slotStatusMsg(i18n("Toggle the statusbar..."));
422 ///////////////////////////////////////////////////////////////////
423 //turn Statusbar on or off
424 if(!viewStatusBar->isChecked())
426 statusBar()->hide();
428 else
430 statusBar()->show();
433 slotStatusMsg(i18n("Ready."));
436 void KMouthApp::slotConfigureTTS() {
437 phraseList->saveWordCompletion();
438 optionsDialog->show();
442 void KMouthApp::slotStatusMsg(const QString &text)
444 ///////////////////////////////////////////////////////////////////
445 // change status message permanently
446 statusBar()->clear();
447 statusBar()->changeItem(text, ID_STATUS_MSG);
450 void KMouthApp::slotPhrasebookConfirmed (PhraseBook &book) {
451 QString name = "phrasebooks";
452 Q3PopupMenu *popup = (Q3PopupMenu *)factory()->container(name, this);
453 KToolBar *toolbar = toolBar ("phrasebookBar");
455 KActionPtrList actions = phrases->actions ();
456 KActionPtrList::iterator iter;
457 for (iter = actions.begin(); iter != actions.end(); ++iter) {
458 (*iter)->unplugAll();
460 delete phrases;
462 phrases = new KActionCollection (this, actionCollection());
463 book.addToGUI (popup, toolbar, phrases, this, SLOT(slotPhraseSelected (const QString &)));
465 QString bookLocation = KApplication::kApplication()->dirs()->saveLocation ("appdata", "/");
466 if (!bookLocation.isNull() && !bookLocation.isEmpty()) {
467 book.save (KURL::fromPathOrURL( bookLocation + "standard.phrasebook" ));
471 void KMouthApp::slotConfigurationChanged()
473 optionsDialog->saveOptions (config);
476 void KMouthApp::slotPhraseSelected (const QString &phrase) {
477 phraseList->insert (phrase);
478 if (optionsDialog->isSpeakImmediately())
479 phraseList->speak ();
482 TextToSpeechSystem *KMouthApp::getTTSSystem() const {
483 return optionsDialog->getTTSSystem();
486 #include "kmouth.moc"