Byebye Q3CString
[kdeaccessibility.git] / kmouth / kmouth.cpp
blobb35d973b7b9e106710b4af02c5a874a53a066e7d
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 // FIXME: Disable so it will compile.
139 // viewToolBar = KStdAction::showToolbar(this, SLOT(slotViewToolBar()), actionCollection());
140 // viewToolBar->setStatusText(i18n("Enables/disables the toolbar"));
141 // viewToolBar->setWhatsThis (i18n("Enables/disables the toolbar"));
143 viewPhrasebookBar = new KToggleAction (i18n("Show P&hrasebook Bar"), 0, 0, this, SLOT(slotViewPhrasebookBar()), actionCollection(), "showPhrasebookBar");
144 viewPhrasebookBar->setStatusText(i18n("Enables/disables the phrasebook bar"));
145 viewPhrasebookBar->setWhatsThis (i18n("Enables/disables the phrasebook bar"));
147 viewStatusBar = KStdAction::showStatusbar(this, SLOT(slotViewStatusBar()), actionCollection());
148 viewStatusBar->setStatusText(i18n("Enables/disables the statusbar"));
149 viewStatusBar->setWhatsThis (i18n("Enables/disables the statusbar"));
151 configureTTS = new KAction (i18n("&Configure KMouth..."), "configure", 0, this, SLOT(slotConfigureTTS()), actionCollection(), "configureTTS");
152 configureTTS->setStatusText(i18n("Opens the configuration dialog"));
153 configureTTS->setWhatsThis (i18n("Opens the configuration dialog"));
155 // The "Help" menu
156 // The "Help" menu will automatically get created.
158 // The popup menu of the list of spoken sentences
159 phraseListSpeak = new KAction (i18n("&Speak"), "speak", 0, phraseList, SLOT(speakListSelection()), actionCollection(), "phraselist_speak");
160 phraseListSpeak->setStatusText(i18n("Speaks the currently selected phrases in the history"));
161 phraseListSpeak->setWhatsThis (i18n("Speaks the currently selected phrases in the history"));
163 phraseListRemove = new KAction (i18n("&Delete"), "editdelete", 0, phraseList, SLOT(removeListSelection()), actionCollection(), "phraselist_remove");
164 phraseListRemove->setStatusText(i18n("Deletes the currently selected phrases from the history"));
165 phraseListRemove->setWhatsThis (i18n("Deletes the currently selected phrases from the history"));
167 phraseListCut = new KAction (i18n("Cu&t"), "editcut", 0, phraseList, SLOT(cutListSelection()), actionCollection(), "phraselist_cut");
168 phraseListCut->setStatusText(i18n("Cuts the currently selected phrases from the history and puts them to the clipboard"));
169 phraseListCut->setWhatsThis (i18n("Cuts the currently selected phrases from the history and puts them to the clipboard"));
171 phraseListCopy = new KAction (i18n("&Copy"), "editcopy", 0, phraseList, SLOT(copyListSelection()), actionCollection(), "phraselist_copy");
172 phraseListCut->setStatusText(i18n("Copies the currently selected phrases from the history to the clipboard"));
173 phraseListCut->setWhatsThis (i18n("Copies the currently selected phrases from the history to the clipboard"));
175 phraselistSelectAll = new KAction (i18n("Select &All Entries"), 0, 0, phraseList, SLOT(selectAllEntries()), actionCollection(),"phraselist_select_all");
176 phraselistSelectAll->setStatusText(i18n("Selects all phrases in the history"));
177 phraselistSelectAll->setWhatsThis (i18n("Selects all phrases in the history"));
179 phraselistDeselectAll = new KAction (i18n("D&eselect All Entries"), 0, 0, phraseList, SLOT(deselectAllEntries()), actionCollection(),"phraselist_deselect_all");
180 phraselistDeselectAll->setStatusText(i18n("Deselects all phrases in the history"));
181 phraselistDeselectAll->setWhatsThis (i18n("Deselects all phrases in the history"));
183 // The popup menu of the edit field
184 // The popup menu of the edit field will automatically get created.
186 // use the absolute path to your kmouthui.rc file for testing purpose in createGUI();
187 createGUI();
190 void KMouthApp::initStatusBar()
192 ///////////////////////////////////////////////////////////////////
193 // STATUSBAR
194 // TODO: add your own items you need for displaying current application status.
195 statusBar()->insertItem(i18n("Ready."), ID_STATUS_MSG);
198 void KMouthApp::initPhraseList()
200 ////////////////////////////////////////////////////////////////////
201 // create the main widget here that is managed by KTMainWindow's view-region and
202 // connect the widget to your document to display document contents.
204 phraseList = new PhraseList(this);
205 setCentralWidget(phraseList);
208 void KMouthApp::openDocumentFile(const KURL& url)
210 slotStatusMsg(i18n("Opening file..."));
212 phraseList->open (url);
213 slotStatusMsg(i18n("Ready."));
216 void KMouthApp::saveOptions() {
217 if (isConfigured) {
218 config->setGroup("General Options");
219 config->writeEntry("Geometry", size());
220 config->writeEntry("Show Menubar", viewMenuBar->isChecked());
221 // FIXME: Toolbar disabled so it will compile.
222 // config->writeEntry("Show Toolbar", viewToolBar->isChecked());
223 config->writeEntry("Show Phrasebook Bar", viewPhrasebookBar->isChecked());
224 config->writeEntry("Show Statusbar",viewStatusBar->isChecked());
225 config->writeEntry("ToolBarPos", (int) toolBar("mainToolBar")->barPos());
227 if (phraseList != 0)
228 phraseList->saveCompletionOptions(config);
229 optionsDialog->saveOptions(config);
230 toolBar("mainToolBar")->saveSettings (config, "mainToolBar");
231 toolBar("phrasebookBar")->saveSettings (config, "phrasebookBar");
236 void KMouthApp::readOptions()
238 config->setGroup("General Options");
240 // bar status settings
241 bool bViewMenubar = config->readBoolEntry("Show Menubar", true);
242 viewMenuBar->setChecked(bViewMenubar);
243 slotViewMenuBar();
245 // FIXME: Toolbar disabled so it will compile.
246 // bool bViewToolbar = config->readBoolEntry("Show Toolbar", true);
247 // viewToolBar->setChecked(bViewToolbar);
248 // slotViewToolBar();
250 bool bViewPhrasebookbar = config->readBoolEntry("Show Phrasebook Bar", true);
251 viewPhrasebookBar->setChecked(bViewPhrasebookbar);
253 bool bViewStatusbar = config->readBoolEntry("Show Statusbar", true);
254 viewStatusBar->setChecked(bViewStatusbar);
255 slotViewStatusBar();
258 // bar position settings
259 KToolBar::BarPosition toolBarPos;
260 toolBarPos=(KToolBar::BarPosition) config->readNumEntry("ToolBarPos", KToolBar::Top);
261 toolBar("mainToolBar")->setBarPos(toolBarPos);
263 QSize size=config->readSizeEntry("Geometry");
264 if(!size.isEmpty())
266 resize(size);
269 optionsDialog->readOptions(config);
271 toolBar("mainToolBar")->applySettings (config, "mainToolBar");
272 toolBar("phrasebookBar")->applySettings (config, "phrasebookBar");
274 QString standardBook = KApplication::kApplication()->dirs()->findResource("appdata", "standard.phrasebook");
275 if (!standardBook.isEmpty()) {
276 PhraseBook book;
277 book.open(KURL::fromPathOrURL( standardBook ));
278 slotPhrasebookConfirmed(book);
280 if (phraseList != 0)
281 phraseList->readCompletionOptions(config);
284 bool KMouthApp::queryClose()
286 return true;
289 bool KMouthApp::queryExit()
291 saveOptions();
292 return true;
295 void KMouthApp::enableMenuEntries(bool existSelectedEntries, bool existDeselectedEntries) {
296 bool existEntries = existSelectedEntries | existDeselectedEntries;
297 fileSaveAs->setEnabled (existEntries);
298 filePrint->setEnabled (existEntries);
300 phraselistSelectAll->setEnabled (existDeselectedEntries);
302 phraselistDeselectAll->setEnabled (existSelectedEntries);
303 phraseListSpeak->setEnabled (existSelectedEntries);
304 phraseListRemove->setEnabled (existSelectedEntries);
305 phraseListCut->setEnabled (existSelectedEntries);
306 phraseListCopy->setEnabled (existSelectedEntries);
309 /////////////////////////////////////////////////////////////////////
310 // SLOT IMPLEMENTATION
311 /////////////////////////////////////////////////////////////////////
313 void KMouthApp::slotFileOpen() {
314 slotStatusMsg(i18n("Opening file..."));
316 phraseList->open();
318 slotStatusMsg(i18n("Ready."));
321 void KMouthApp::slotFileSaveAs() {
322 slotStatusMsg(i18n("Saving history with a new filename..."));
324 phraseList->save();
326 slotStatusMsg(i18n("Ready."));
329 void KMouthApp::slotFilePrint()
331 slotStatusMsg(i18n("Printing..."));
333 KPrinter printer;
334 if (printer.setup(this))
336 phraseList->print(&printer);
339 slotStatusMsg(i18n("Ready."));
342 void KMouthApp::slotFileQuit()
344 slotStatusMsg(i18n("Exiting..."));
345 saveOptions();
346 // close the first window, the list makes the next one the first again.
347 // This ensures that queryClose() is called on each window to ask for closing
348 KMainWindow* w;
349 if (memberList())
351 for(w=memberList()->first(); w!=0; w=memberList()->first())
353 // only close the window if the closeEvent is accepted. If the user presses Cancel on the saveModified() dialog,
354 // the window and the application stay open.
355 if(!w->close())
356 break;
357 memberList()->removeRef(w);
362 void KMouthApp::slotEditPhrasebook () {
363 PhraseBookDialog *phraseBookDialog = PhraseBookDialog::get();
364 // As we do not know whether the we are already connected to the slot,
365 // we first disconnect and then connect again.
366 disconnect (phraseBookDialog, SIGNAL(phrasebookConfirmed (PhraseBook &)),
367 this, SLOT(slotPhrasebookConfirmed (PhraseBook &)));
368 connect (phraseBookDialog, SIGNAL(phrasebookConfirmed (PhraseBook &)),
369 this, SLOT(slotPhrasebookConfirmed (PhraseBook &)));
371 // As we do not know whether the phrase book edit window is already open,
372 // we first open and then raise it, so that it is surely the top window.
373 phraseBookDialog->show();
374 phraseBookDialog->raise();
377 void KMouthApp::slotViewMenuBar() {
378 slotStatusMsg(i18n("Toggling menubar..."));
380 if(!viewMenuBar->isChecked())
381 menuBar()->hide();
382 else
383 menuBar()->show();
385 slotStatusMsg(i18n("Ready."));
388 void KMouthApp::slotViewToolBar()
390 slotStatusMsg(i18n("Toggling toolbar..."));
391 ///////////////////////////////////////////////////////////////////
392 // turn Toolbar on or off
393 if(!viewToolBar->isChecked())
395 toolBar("mainToolBar")->hide();
397 else
399 toolBar("mainToolBar")->show();
402 slotStatusMsg(i18n("Ready."));
405 void KMouthApp::slotViewPhrasebookBar()
407 slotStatusMsg(i18n("Toggling phrasebook bar..."));
408 ///////////////////////////////////////////////////////////////////
409 // turn Toolbar on or off
410 if(!viewPhrasebookBar->isChecked())
412 toolBar("phrasebookBar")->hide();
414 else
416 toolBar("phrasebookBar")->show();
419 slotStatusMsg(i18n("Ready."));
422 void KMouthApp::slotViewStatusBar()
424 slotStatusMsg(i18n("Toggle the statusbar..."));
425 ///////////////////////////////////////////////////////////////////
426 //turn Statusbar on or off
427 if(!viewStatusBar->isChecked())
429 statusBar()->hide();
431 else
433 statusBar()->show();
436 slotStatusMsg(i18n("Ready."));
439 void KMouthApp::slotConfigureTTS() {
440 phraseList->saveWordCompletion();
441 optionsDialog->show();
445 void KMouthApp::slotStatusMsg(const QString &text)
447 ///////////////////////////////////////////////////////////////////
448 // change status message permanently
449 statusBar()->clear();
450 statusBar()->changeItem(text, ID_STATUS_MSG);
453 void KMouthApp::slotPhrasebookConfirmed (PhraseBook &book) {
454 QString name = "phrasebooks";
455 Q3PopupMenu *popup = (Q3PopupMenu *)factory()->container(name, this);
456 KToolBar *toolbar = toolBar ("phrasebookBar");
458 KActionPtrList actions = phrases->actions ();
459 KActionPtrList::iterator iter;
460 for (iter = actions.begin(); iter != actions.end(); ++iter) {
461 (*iter)->unplugAll();
463 delete phrases;
465 phrases = new KActionCollection (this, actionCollection());
466 book.addToGUI (popup, toolbar, phrases, this, SLOT(slotPhraseSelected (const QString &)));
468 QString bookLocation = KApplication::kApplication()->dirs()->saveLocation ("appdata", "/");
469 if (!bookLocation.isNull() && !bookLocation.isEmpty()) {
470 book.save (KURL::fromPathOrURL( bookLocation + "standard.phrasebook" ));
474 void KMouthApp::slotConfigurationChanged()
476 optionsDialog->saveOptions (config);
479 void KMouthApp::slotPhraseSelected (const QString &phrase) {
480 phraseList->insert (phrase);
481 if (optionsDialog->isSpeakImmediately())
482 phraseList->speak ();
485 TextToSpeechSystem *KMouthApp::getTTSSystem() const {
486 return optionsDialog->getTTSSystem();
489 #include "kmouth.moc"