relicense to gplv2+
[kdeaccessibility.git] / kmouth / kmouth.cpp
blob50768d92b576e9e433919dbf33ee374963c3b3c0
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/QPrintDialog>
30 #include <QtGui/QMenu>
32 // include files for KDE
33 #include <kxmlguifactory.h>
34 #include <kiconloader.h>
35 #include <kmenubar.h>
36 #include <kstatusbar.h>
37 #include <klocale.h>
38 #include <kconfig.h>
39 #include <kstandardaction.h>
40 #include <kmenu.h>
41 #include <kstandarddirs.h>
42 #include <kglobal.h>
43 #include <ktoolbar.h>
44 #include <kactioncollection.h>
45 #include <kactionmenu.h>
46 #include <ktoggleaction.h>
47 #include <kstandardshortcut.h>
48 #include <kapplication.h>
49 #include <kdeprintdialog.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);
97 printer = 0;
100 KMouthApp::~KMouthApp()
102 delete printer;
105 bool KMouthApp::configured() {
106 return isConfigured;
109 void KMouthApp::initActions() {
110 // The "File" menu
111 fileOpen = actionCollection()->addAction("file_open");
112 fileOpen->setIcon(KIcon("phrasehistory_open"));
113 fileOpen->setText(i18n("&Open as History..."));
114 fileOpen->setShortcuts(KStandardShortcut::open());
115 connect(fileOpen, SIGNAL(triggered(bool)), this, SLOT(slotFileOpen()));
116 fileOpen->setToolTip(i18n("Opens an existing file as history"));
117 fileOpen->setWhatsThis (i18n("Opens an existing file as history"));
119 fileSaveAs = actionCollection()->addAction("file_save_as");
120 fileSaveAs->setIcon(KIcon("phrasehistory_save"));
121 fileSaveAs->setText(i18n("Save &History As..."));
122 fileSaveAs->setShortcuts(KStandardShortcut::save());
123 connect(fileSaveAs, SIGNAL(triggered(bool)), this, SLOT(slotFileSaveAs()));
124 fileSaveAs->setToolTip(i18n("Saves the actual history as..."));
125 fileSaveAs->setWhatsThis (i18n("Saves the actual history as..."));
127 filePrint = actionCollection()->addAction("file_print");
128 filePrint->setIcon(KIcon("phrasehistory_print"));
129 filePrint->setText(i18n("&Print History..."));
130 filePrint->setShortcuts(KStandardShortcut::print());
131 connect(filePrint, SIGNAL(triggered(bool)), this, SLOT(slotFilePrint()));
132 filePrint->setToolTip(i18n("Prints out the actual history"));
133 filePrint->setWhatsThis (i18n("Prints out the actual history"));
135 fileQuit = KStandardAction::quit(this, SLOT(slotFileQuit()), actionCollection());
136 fileQuit->setToolTip(i18n("Quits the application"));
137 fileQuit->setWhatsThis (i18n("Quits the application"));
139 // The "Edit" menu
140 editCut = KStandardAction::cut(phraseList, SLOT(cut()), actionCollection());
141 editCut->setToolTip(i18n("Cuts the selected section and puts it to the clipboard"));
142 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."));
144 editCopy = KStandardAction::copy(phraseList, SLOT(copy()), actionCollection());
145 editCopy->setToolTip(i18n("Copies the selected section to the clipboard"));
146 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."));
148 editPaste = KStandardAction::paste(phraseList, SLOT(paste()), actionCollection());
149 editPaste->setToolTip(i18n("Pastes the clipboard contents to actual position"));
150 editPaste->setWhatsThis (i18n("Pastes the clipboard contents at the current cursor position into the edit field."));
152 editSpeak = actionCollection()->addAction("edit_speak");
153 editSpeak->setIcon(KIcon("speak"));
154 editSpeak->setText(i18n("&Speak"));
155 connect(editSpeak, SIGNAL(triggered(bool)), phraseList, SLOT(speak()));
156 editSpeak->setToolTip(i18n("Speaks the currently active sentence(s)"));
157 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."));
159 // The "Phrase book" menu
160 phrasebookEdit = actionCollection()->addAction("phrasebook_edit");
161 phrasebookEdit->setText(i18n("&Edit..."));
162 connect(phrasebookEdit, SIGNAL(triggered(bool)), this, SLOT(slotEditPhrasebook()));
164 // The "Options" menu
165 viewMenuBar = KStandardAction::showMenubar(this, SLOT(slotViewMenuBar()), actionCollection());
166 // FIXME: Disable so it will compile.
167 // viewToolBar = KStandardAction::showToolbar(this, SLOT(slotViewToolBar()), actionCollection());
168 // viewToolBar->setToolTip(i18n("Enables/disables the toolbar"));
169 // viewToolBar->setWhatsThis (i18n("Enables/disables the toolbar"));
171 viewPhrasebookBar = actionCollection()->add<KToggleAction>("showPhrasebookBar");
172 viewPhrasebookBar->setText(i18n("Show P&hrasebook Bar"));
173 connect(viewPhrasebookBar, SIGNAL(triggered(bool)), this, SLOT(slotViewPhrasebookBar()));
174 viewPhrasebookBar->setToolTip(i18n("Enables/disables the phrasebook bar"));
175 viewPhrasebookBar->setWhatsThis (i18n("Enables/disables the phrasebook bar"));
177 viewStatusBar = KStandardAction::showStatusbar(this, SLOT(slotViewStatusBar()), actionCollection());
178 viewStatusBar->setToolTip(i18n("Enables/disables the statusbar"));
179 viewStatusBar->setWhatsThis (i18n("Enables/disables the statusbar"));
181 configureTTS = actionCollection()->addAction("configureTTS");
182 configureTTS->setIcon(KIcon("configure"));
183 configureTTS->setText(i18n("&Configure KMouth..."));
184 connect(configureTTS, SIGNAL(triggered(bool)), this, SLOT(slotConfigureTTS()));
185 configureTTS->setToolTip(i18n("Opens the configuration dialog"));
186 configureTTS->setWhatsThis (i18n("Opens the configuration dialog"));
188 // The "Help" menu
189 // The "Help" menu will automatically get created.
191 // The popup menu of the list of spoken sentences
192 phraseListSpeak = actionCollection()->addAction("phraselist_speak");
193 phraseListSpeak->setIcon(KIcon("speak"));
194 phraseListSpeak->setText(i18n("&Speak"));
195 phraseListSpeak->setToolTip(i18n("Speaks the currently selected phrases in the history"));
196 connect(phraseListSpeak, SIGNAL(triggered(bool)), phraseList, SLOT(speakListSelection()));
197 phraseListSpeak->setWhatsThis (i18n("Speaks the currently selected phrases in the history"));
199 phraseListRemove = actionCollection()->addAction("phraselist_remove");
200 phraseListRemove->setIcon(KIcon("edit-delete"));
201 phraseListRemove->setText(i18n("&Delete"));
202 connect(phraseListRemove, SIGNAL(triggered(bool)), phraseList, SLOT(removeListSelection()));
203 phraseListRemove->setToolTip(i18n("Deletes the currently selected phrases from the history"));
204 phraseListRemove->setWhatsThis (i18n("Deletes the currently selected phrases from the history"));
206 phraseListCut = actionCollection()->addAction("phraselist_cut");
207 phraseListCut->setIcon(KIcon("edit-cut"));
208 phraseListCut->setText(i18n("Cu&t"));
209 connect(phraseListCut, SIGNAL(triggered(bool)), phraseList, SLOT(cutListSelection()));
210 phraseListCut->setToolTip(i18n("Cuts the currently selected phrases from the history and puts them to the clipboard"));
211 phraseListCut->setWhatsThis (i18n("Cuts the currently selected phrases from the history and puts them to the clipboard"));
213 phraseListCopy = actionCollection()->addAction("phraselist_copy");
214 phraseListCopy->setIcon(KIcon("edit-copy"));
215 phraseListCopy->setText(i18n("&Copy"));
216 connect(phraseListCopy, SIGNAL(triggered(bool)), phraseList, SLOT(copyListSelection()));
217 phraseListCut->setToolTip(i18n("Copies the currently selected phrases from the history to the clipboard"));
218 phraseListCut->setWhatsThis (i18n("Copies the currently selected phrases from the history to the clipboard"));
220 phraselistSelectAll = actionCollection()->addAction("phraselist_select_all");
221 phraselistSelectAll->setText(i18n("Select &All Entries"));
222 connect(phraselistSelectAll, SIGNAL(triggered(bool)), phraseList, SLOT(selectAllEntries()));
223 phraselistSelectAll->setToolTip(i18n("Selects all phrases in the history"));
224 phraselistSelectAll->setWhatsThis (i18n("Selects all phrases in the history"));
226 phraselistDeselectAll = actionCollection()->addAction("phraselist_deselect_all");
227 phraselistDeselectAll->setText(i18n("D&eselect All Entries"));
228 connect(phraselistDeselectAll, SIGNAL(triggered(bool)), phraseList, SLOT(deselectAllEntries()));
229 phraselistDeselectAll->setToolTip(i18n("Deselects all phrases in the history"));
230 phraselistDeselectAll->setWhatsThis (i18n("Deselects all phrases in the history"));
232 // The popup menu of the edit field
233 // The popup menu of the edit field will automatically get created.
235 // use the absolute path to your kmouthui.rc file for testing purpose in createGUI();
236 createGUI();
239 void KMouthApp::initStatusBar()
241 ///////////////////////////////////////////////////////////////////
242 // STATUSBAR
243 // TODO: add your own items you need for displaying current application status.
244 statusBar()->insertItem(i18n("Ready."), ID_STATUS_MSG);
247 void KMouthApp::initPhraseList()
249 ////////////////////////////////////////////////////////////////////
250 // create the main widget here that is managed by KTMainWindow's view-region and
251 // connect the widget to your document to display document contents.
253 phraseList = new PhraseList(this);
254 setCentralWidget(phraseList);
257 void KMouthApp::openDocumentFile(const KUrl& url)
259 slotStatusMsg(i18n("Opening file..."));
261 phraseList->open (url);
262 slotStatusMsg(i18n("Ready."));
265 void KMouthApp::saveOptions() {
266 if (isConfigured) {
267 KConfigGroup cg( config, "General Options");
268 cg.writeEntry("Geometry", size());
269 cg.writeEntry("Show Menubar", viewMenuBar->isChecked());
270 // FIXME: Toolbar disabled so it will compile.
271 // cg.writeEntry("Show Toolbar", viewToolBar->isChecked());
272 cg.writeEntry("Show Phrasebook Bar", viewPhrasebookBar->isChecked());
273 cg.writeEntry("Show Statusbar",viewStatusBar->isChecked());
274 // FIXME: KToolBar no longer has barPos() method.
275 // cg.writeEntry("ToolBarPos", (int) toolBar("mainToolBar")->barPos());
277 if (phraseList != 0)
278 phraseList->saveCompletionOptions(config.data());
279 optionsDialog->saveOptions(config.data());
280 cg.changeGroup( "mainToolBar" );
281 toolBar("mainToolBar")->saveSettings( cg );
282 cg.changeGroup( "phrasebookBar");
283 toolBar("phrasebookBar")->saveSettings( cg );
288 void KMouthApp::readOptions()
290 KConfigGroup cg( config, "General Options");
292 // bar status settings
293 bool bViewMenubar = cg.readEntry("Show Menubar", true);
294 viewMenuBar->setChecked(bViewMenubar);
295 slotViewMenuBar();
297 // FIXME: Toolbar disabled so it will compile.
298 // bool bViewToolbar = cg.readEntry("Show Toolbar", QVariant(true)).toBool();
299 // viewToolBar->setChecked(bViewToolbar);
300 // slotViewToolBar();
302 bool bViewPhrasebookbar = cg.readEntry("Show Phrasebook Bar", true);
303 viewPhrasebookBar->setChecked(bViewPhrasebookbar);
305 bool bViewStatusbar = cg.readEntry("Show Statusbar", true);
306 viewStatusBar->setChecked(bViewStatusbar);
307 slotViewStatusBar();
310 // bar position settings
311 // FIXME:
312 // KToolBar::BarPosition toolBarPos;
313 // toolBarPos=(KToolBar::BarPosition) cg.readEntry("ToolBarPos", int(KToolBar::Top));
314 // toolBar("mainToolBar")->setBarPos(toolBarPos);
316 QSize size=cg.readEntry("Geometry",QSize());
317 if(!size.isEmpty())
319 resize(size);
322 optionsDialog->readOptions(config.data());
324 toolBar("mainToolBar")->applySettings (config->group( "mainToolBar" ) );
325 toolBar("phrasebookBar")->applySettings (config->group( "phrasebookBar") );
327 QString standardBook = KGlobal::dirs()->findResource("appdata", "standard.phrasebook");
328 if (!standardBook.isEmpty()) {
329 PhraseBook book;
330 book.open(KUrl::fromPathOrUrl( standardBook ));
331 slotPhrasebookConfirmed(book);
333 if (phraseList != 0)
334 phraseList->readCompletionOptions(config.data());
337 bool KMouthApp::queryClose()
339 return true;
342 bool KMouthApp::queryExit()
344 saveOptions();
345 return true;
348 void KMouthApp::enableMenuEntries(bool existSelectedEntries, bool existDeselectedEntries) {
349 bool existEntries = existSelectedEntries | existDeselectedEntries;
350 fileSaveAs->setEnabled (existEntries);
351 filePrint->setEnabled (existEntries);
353 phraselistSelectAll->setEnabled (existDeselectedEntries);
355 phraselistDeselectAll->setEnabled (existSelectedEntries);
356 phraseListSpeak->setEnabled (existSelectedEntries);
357 phraseListRemove->setEnabled (existSelectedEntries);
358 phraseListCut->setEnabled (existSelectedEntries);
359 phraseListCopy->setEnabled (existSelectedEntries);
362 /////////////////////////////////////////////////////////////////////
363 // SLOT IMPLEMENTATION
364 /////////////////////////////////////////////////////////////////////
366 void KMouthApp::slotFileOpen() {
367 slotStatusMsg(i18n("Opening file..."));
369 phraseList->open();
371 slotStatusMsg(i18n("Ready."));
374 void KMouthApp::slotFileSaveAs() {
375 slotStatusMsg(i18n("Saving history with a new filename..."));
377 phraseList->save();
379 slotStatusMsg(i18n("Ready."));
382 void KMouthApp::slotFilePrint()
384 slotStatusMsg(i18n("Printing..."));
386 if (printer == 0) {
387 printer = new QPrinter();
390 QPrintDialog *printDialog = KdePrint::createPrintDialog(printer, this);
392 if (printDialog->exec())
394 phraseList->print(printer);
397 slotStatusMsg(i18n("Ready."));
400 void KMouthApp::slotFileQuit()
402 slotStatusMsg(i18n("Exiting..."));
403 saveOptions();
404 // close the first window, the list makes the next one the first again.
405 // This ensures that queryClose() is called on each window to ask for closing
406 KMainWindow* w;
407 if (!memberList().isEmpty())
409 for (int i = 0; i < memberList().size(); ++i)
411 // only close the window if the closeEvent is accepted. If the user presses Cancel on the saveModified() dialog,
412 // the window and the application stay open.
413 w = memberList().at(i);
414 if(!w->close())
415 break;
416 #ifdef __GNUC__
417 #warning "kde4: how remove it ?.???"
418 #endif
419 //memberList()->removeRef(w);
424 void KMouthApp::slotEditPhrasebook () {
425 PhraseBookDialog *phraseBookDialog = PhraseBookDialog::get();
426 // As we do not know whether the we are already connected to the slot,
427 // we first disconnect and then connect again.
428 disconnect (phraseBookDialog, SIGNAL(phrasebookConfirmed (PhraseBook &)),
429 this, SLOT(slotPhrasebookConfirmed (PhraseBook &)));
430 connect (phraseBookDialog, SIGNAL(phrasebookConfirmed (PhraseBook &)),
431 this, SLOT(slotPhrasebookConfirmed (PhraseBook &)));
433 // As we do not know whether the phrase book edit window is already open,
434 // we first open and then raise it, so that it is surely the top window.
435 phraseBookDialog->show();
436 phraseBookDialog->raise();
439 void KMouthApp::slotViewMenuBar() {
440 slotStatusMsg(i18n("Toggling menubar..."));
442 if(!viewMenuBar->isChecked())
443 menuBar()->hide();
444 else
445 menuBar()->show();
447 slotStatusMsg(i18n("Ready."));
450 void KMouthApp::slotViewToolBar()
452 slotStatusMsg(i18n("Toggling toolbar..."));
453 ///////////////////////////////////////////////////////////////////
454 // turn Toolbar on or off
455 if(!viewToolBar->isChecked())
457 toolBar("mainToolBar")->hide();
459 else
461 toolBar("mainToolBar")->show();
464 slotStatusMsg(i18n("Ready."));
467 void KMouthApp::slotViewPhrasebookBar()
469 slotStatusMsg(i18n("Toggling phrasebook bar..."));
470 ///////////////////////////////////////////////////////////////////
471 // turn Toolbar on or off
472 if(!viewPhrasebookBar->isChecked())
474 toolBar("phrasebookBar")->hide();
476 else
478 toolBar("phrasebookBar")->show();
481 slotStatusMsg(i18n("Ready."));
484 void KMouthApp::slotViewStatusBar()
486 slotStatusMsg(i18n("Toggle the statusbar..."));
487 ///////////////////////////////////////////////////////////////////
488 //turn Statusbar on or off
489 if(!viewStatusBar->isChecked())
491 statusBar()->hide();
493 else
495 statusBar()->show();
498 slotStatusMsg(i18n("Ready."));
501 void KMouthApp::slotConfigureTTS() {
502 phraseList->saveWordCompletion();
503 optionsDialog->show();
507 void KMouthApp::slotStatusMsg(const QString &text)
509 ///////////////////////////////////////////////////////////////////
510 // change status message permanently
511 statusBar()->clearMessage();
512 statusBar()->changeItem(text, ID_STATUS_MSG);
515 void KMouthApp::slotPhrasebookConfirmed (PhraseBook &book) {
516 QString name = "phrasebooks";
517 QMenu *popup = (QMenu *)factory()->container(name, this);
518 KToolBar *toolbar = toolBar ("phrasebookBar");
520 delete phrases;
521 phrases = new KActionCollection (actionCollection());
522 book.addToGUI (popup, toolbar, phrases, this, SLOT(slotPhraseSelected (const QString &)));
524 QString bookLocation = KGlobal::dirs()->saveLocation ("appdata", "/");
525 if (!bookLocation.isNull() && !bookLocation.isEmpty()) {
526 book.save (KUrl::fromPathOrUrl( bookLocation + "standard.phrasebook" ));
530 void KMouthApp::slotConfigurationChanged()
532 optionsDialog->saveOptions (config.data());
535 void KMouthApp::slotPhraseSelected (const QString &phrase) {
536 phraseList->insert (phrase);
537 if (optionsDialog->isSpeakImmediately())
538 phraseList->speak ();
541 TextToSpeechSystem *KMouthApp::getTTSSystem() const {
542 return optionsDialog->getTTSSystem();
545 #include "kmouth.moc"