Use QMenu
[kdeaccessibility.git] / kmouth / kmouth.cpp
blob37b405da8623c48e2a60ec0bbc6c8ff66fe0b4ea
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 <QMenu>
23 #include <kxmlguifactory.h>
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 <kmenu.h>
33 #include <kstandarddirs.h>
34 #include <kglobal.h>
36 // application specific includes
37 #include "kmouth.h"
38 #include "phraselist.h"
39 #include "phrasebook/phrasebook.h"
40 #include "phrasebook/phrasebookdialog.h"
41 #include "optionsdialog.h"
42 #include "configwizard.h"
44 #define ID_STATUS_MSG 1
46 KMouthApp::KMouthApp(QWidget* , const char* name):KMainWindow(0, name)
48 isConfigured = false;
49 config=KGlobal::config();
51 ///////////////////////////////////////////////////////////////////
52 // call inits to invoke all other construction parts
53 initStatusBar();
54 initPhraseList();
55 initActions();
56 optionsDialog = new OptionsDialog(this);
57 connect (optionsDialog, SIGNAL(configurationChanged ()),
58 this, SLOT(slotConfigurationChanged ()));
59 connect (optionsDialog, SIGNAL(configurationChanged ()),
60 phraseList, SLOT(configureCompletion ()));
62 phrases = new KActionCollection (this);
64 readOptions();
65 ConfigWizard *wizard = new ConfigWizard (this, "ConfigWizard", config);
66 if (wizard->configurationNeeded ()) {
67 if (wizard->requestConfiguration ()) {
68 isConfigured = true;
69 saveOptions();
70 wizard->saveConfig (config);
71 readOptions();
73 else
74 isConfigured = false;
76 else
77 isConfigured = true;
78 delete wizard;
80 if (isConfigured) {
81 phraseList->configureCompletion();
84 ///////////////////////////////////////////////////////////////////
85 // disable actions at startup
86 fileSaveAs->setEnabled(false);
87 filePrint->setEnabled(false);
90 KMouthApp::~KMouthApp()
95 bool KMouthApp::configured() {
96 return isConfigured;
99 void KMouthApp::initActions() {
100 // The "File" menu
101 fileOpen = new KAction(i18n("&Open as History..."), "phrasehistory_open", KStdAccel::open(), this, SLOT(slotFileOpen()), actionCollection(),"file_open");
102 fileOpen->setToolTip(i18n("Opens an existing file as history"));
103 fileOpen->setWhatsThis (i18n("Opens an existing file as history"));
105 fileSaveAs = new KAction(i18n("Save &History As..."), "phrasehistory_save", KStdAccel::save(), this, SLOT(slotFileSaveAs()), actionCollection(),"file_save_as");
106 fileSaveAs->setToolTip(i18n("Saves the actual history as..."));
107 fileSaveAs->setWhatsThis (i18n("Saves the actual history as..."));
109 filePrint = new KAction(i18n("&Print History..."), "phrasehistory_print", KStdAccel::print(), this, SLOT(slotFilePrint()), actionCollection(),"file_print");
110 filePrint->setToolTip(i18n("Prints out the actual history"));
111 filePrint->setWhatsThis (i18n("Prints out the actual history"));
113 fileQuit = KStdAction::quit(this, SLOT(slotFileQuit()), actionCollection());
114 fileQuit->setToolTip(i18n("Quits the application"));
115 fileQuit->setWhatsThis (i18n("Quits the application"));
117 // The "Edit" menu
118 editCut = KStdAction::cut(phraseList, SLOT(cut()), actionCollection());
119 editCut->setToolTip(i18n("Cuts the selected section and puts it to the clipboard"));
120 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."));
122 editCopy = KStdAction::copy(phraseList, SLOT(copy()), actionCollection());
123 editCopy->setToolTip(i18n("Copies the selected section to the clipboard"));
124 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."));
126 editPaste = KStdAction::paste(phraseList, SLOT(paste()), actionCollection());
127 editPaste->setToolTip(i18n("Pastes the clipboard contents to actual position"));
128 editPaste->setWhatsThis (i18n("Pastes the clipboard contents at the current cursor position into the edit field."));
130 editSpeak = new KAction (i18n("&Speak"), "speak", 0, phraseList, SLOT(speak()), actionCollection(),"edit_speak");
131 editSpeak->setToolTip(i18n("Speaks the currently active sentence(s)"));
132 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."));
134 // The "Phrase book" menu
135 phrasebookEdit = new KAction(i18n("&Edit..."), 0, 0, this, SLOT(slotEditPhrasebook()), actionCollection(),"phrasebook_edit");
137 // The "Options" menu
138 viewMenuBar = KStdAction::showMenubar(this, SLOT(slotViewMenuBar()), actionCollection());
139 // FIXME: Disable so it will compile.
140 // viewToolBar = KStdAction::showToolbar(this, SLOT(slotViewToolBar()), actionCollection());
141 // viewToolBar->setToolTip(i18n("Enables/disables the toolbar"));
142 // viewToolBar->setWhatsThis (i18n("Enables/disables the toolbar"));
144 viewPhrasebookBar = new KToggleAction (i18n("Show P&hrasebook Bar"), 0, 0, this, SLOT(slotViewPhrasebookBar()), actionCollection(), "showPhrasebookBar");
145 viewPhrasebookBar->setToolTip(i18n("Enables/disables the phrasebook bar"));
146 viewPhrasebookBar->setWhatsThis (i18n("Enables/disables the phrasebook bar"));
148 viewStatusBar = KStdAction::showStatusbar(this, SLOT(slotViewStatusBar()), actionCollection());
149 viewStatusBar->setToolTip(i18n("Enables/disables the statusbar"));
150 viewStatusBar->setWhatsThis (i18n("Enables/disables the statusbar"));
152 configureTTS = new KAction (i18n("&Configure KMouth..."), "configure", 0, this, SLOT(slotConfigureTTS()), actionCollection(), "configureTTS");
153 configureTTS->setToolTip(i18n("Opens the configuration dialog"));
154 configureTTS->setWhatsThis (i18n("Opens the configuration dialog"));
156 // The "Help" menu
157 // The "Help" menu will automatically get created.
159 // The popup menu of the list of spoken sentences
160 phraseListSpeak = new KAction (i18n("&Speak"), "speak", 0, phraseList, SLOT(speakListSelection()), actionCollection(), "phraselist_speak");
161 phraseListSpeak->setToolTip(i18n("Speaks the currently selected phrases in the history"));
162 phraseListSpeak->setWhatsThis (i18n("Speaks the currently selected phrases in the history"));
164 phraseListRemove = new KAction (i18n("&Delete"), "editdelete", 0, phraseList, SLOT(removeListSelection()), actionCollection(), "phraselist_remove");
165 phraseListRemove->setToolTip(i18n("Deletes the currently selected phrases from the history"));
166 phraseListRemove->setWhatsThis (i18n("Deletes the currently selected phrases from the history"));
168 phraseListCut = new KAction (i18n("Cu&t"), "editcut", 0, phraseList, SLOT(cutListSelection()), actionCollection(), "phraselist_cut");
169 phraseListCut->setToolTip(i18n("Cuts the currently selected phrases from the history and puts them to the clipboard"));
170 phraseListCut->setWhatsThis (i18n("Cuts the currently selected phrases from the history and puts them to the clipboard"));
172 phraseListCopy = new KAction (i18n("&Copy"), "editcopy", 0, phraseList, SLOT(copyListSelection()), actionCollection(), "phraselist_copy");
173 phraseListCut->setToolTip(i18n("Copies the currently selected phrases from the history to the clipboard"));
174 phraseListCut->setWhatsThis (i18n("Copies the currently selected phrases from the history to the clipboard"));
176 phraselistSelectAll = new KAction (i18n("Select &All Entries"), 0, 0, phraseList, SLOT(selectAllEntries()), actionCollection(),"phraselist_select_all");
177 phraselistSelectAll->setToolTip(i18n("Selects all phrases in the history"));
178 phraselistSelectAll->setWhatsThis (i18n("Selects all phrases in the history"));
180 phraselistDeselectAll = new KAction (i18n("D&eselect All Entries"), 0, 0, phraseList, SLOT(deselectAllEntries()), actionCollection(),"phraselist_deselect_all");
181 phraselistDeselectAll->setToolTip(i18n("Deselects all phrases in the history"));
182 phraselistDeselectAll->setWhatsThis (i18n("Deselects all phrases in the history"));
184 // The popup menu of the edit field
185 // The popup menu of the edit field will automatically get created.
187 // use the absolute path to your kmouthui.rc file for testing purpose in createGUI();
188 createGUI();
191 void KMouthApp::initStatusBar()
193 ///////////////////////////////////////////////////////////////////
194 // STATUSBAR
195 // TODO: add your own items you need for displaying current application status.
196 statusBar()->insertItem(i18n("Ready."), ID_STATUS_MSG);
199 void KMouthApp::initPhraseList()
201 ////////////////////////////////////////////////////////////////////
202 // create the main widget here that is managed by KTMainWindow's view-region and
203 // connect the widget to your document to display document contents.
205 phraseList = new PhraseList(this);
206 setCentralWidget(phraseList);
209 void KMouthApp::openDocumentFile(const KURL& url)
211 slotStatusMsg(i18n("Opening file..."));
213 phraseList->open (url);
214 slotStatusMsg(i18n("Ready."));
217 void KMouthApp::saveOptions() {
218 if (isConfigured) {
219 config->setGroup("General Options");
220 config->writeEntry("Geometry", size());
221 config->writeEntry("Show Menubar", viewMenuBar->isChecked());
222 // FIXME: Toolbar disabled so it will compile.
223 // config->writeEntry("Show Toolbar", viewToolBar->isChecked());
224 config->writeEntry("Show Phrasebook Bar", viewPhrasebookBar->isChecked());
225 config->writeEntry("Show Statusbar",viewStatusBar->isChecked());
226 config->writeEntry("ToolBarPos", (int) toolBar("mainToolBar")->barPos());
228 if (phraseList != 0)
229 phraseList->saveCompletionOptions(config);
230 optionsDialog->saveOptions(config);
231 toolBar("mainToolBar")->saveSettings (config, "mainToolBar");
232 toolBar("phrasebookBar")->saveSettings (config, "phrasebookBar");
237 void KMouthApp::readOptions()
239 config->setGroup("General Options");
241 // bar status settings
242 bool bViewMenubar = config->readBoolEntry("Show Menubar", true);
243 viewMenuBar->setChecked(bViewMenubar);
244 slotViewMenuBar();
246 // FIXME: Toolbar disabled so it will compile.
247 // bool bViewToolbar = config->readBoolEntry("Show Toolbar", true);
248 // viewToolBar->setChecked(bViewToolbar);
249 // slotViewToolBar();
251 bool bViewPhrasebookbar = config->readBoolEntry("Show Phrasebook Bar", true);
252 viewPhrasebookBar->setChecked(bViewPhrasebookbar);
254 bool bViewStatusbar = config->readBoolEntry("Show Statusbar", true);
255 viewStatusBar->setChecked(bViewStatusbar);
256 slotViewStatusBar();
259 // bar position settings
260 KToolBar::BarPosition toolBarPos;
261 toolBarPos=(KToolBar::BarPosition) config->readNumEntry("ToolBarPos", KToolBar::Top);
262 toolBar("mainToolBar")->setBarPos(toolBarPos);
264 QSize size=config->readSizeEntry("Geometry");
265 if(!size.isEmpty())
267 resize(size);
270 optionsDialog->readOptions(config);
272 toolBar("mainToolBar")->applySettings (config, "mainToolBar");
273 toolBar("phrasebookBar")->applySettings (config, "phrasebookBar");
275 QString standardBook = KApplication::kApplication()->dirs()->findResource("appdata", "standard.phrasebook");
276 if (!standardBook.isEmpty()) {
277 PhraseBook book;
278 book.open(KURL::fromPathOrURL( standardBook ));
279 slotPhrasebookConfirmed(book);
281 if (phraseList != 0)
282 phraseList->readCompletionOptions(config);
285 bool KMouthApp::queryClose()
287 return true;
290 bool KMouthApp::queryExit()
292 saveOptions();
293 return true;
296 void KMouthApp::enableMenuEntries(bool existSelectedEntries, bool existDeselectedEntries) {
297 bool existEntries = existSelectedEntries | existDeselectedEntries;
298 fileSaveAs->setEnabled (existEntries);
299 filePrint->setEnabled (existEntries);
301 phraselistSelectAll->setEnabled (existDeselectedEntries);
303 phraselistDeselectAll->setEnabled (existSelectedEntries);
304 phraseListSpeak->setEnabled (existSelectedEntries);
305 phraseListRemove->setEnabled (existSelectedEntries);
306 phraseListCut->setEnabled (existSelectedEntries);
307 phraseListCopy->setEnabled (existSelectedEntries);
310 /////////////////////////////////////////////////////////////////////
311 // SLOT IMPLEMENTATION
312 /////////////////////////////////////////////////////////////////////
314 void KMouthApp::slotFileOpen() {
315 slotStatusMsg(i18n("Opening file..."));
317 phraseList->open();
319 slotStatusMsg(i18n("Ready."));
322 void KMouthApp::slotFileSaveAs() {
323 slotStatusMsg(i18n("Saving history with a new filename..."));
325 phraseList->save();
327 slotStatusMsg(i18n("Ready."));
330 void KMouthApp::slotFilePrint()
332 slotStatusMsg(i18n("Printing..."));
334 KPrinter printer;
335 if (printer.setup(this))
337 phraseList->print(&printer);
340 slotStatusMsg(i18n("Ready."));
343 void KMouthApp::slotFileQuit()
345 slotStatusMsg(i18n("Exiting..."));
346 saveOptions();
347 // close the first window, the list makes the next one the first again.
348 // This ensures that queryClose() is called on each window to ask for closing
349 KMainWindow* w;
350 if (!memberList().isEmpty())
352 for (int i = 0; i < memberList().size(); ++i)
354 // only close the window if the closeEvent is accepted. If the user presses Cancel on the saveModified() dialog,
355 // the window and the application stay open.
356 w = memberList().at(i);
357 if(!w->close())
358 break;
359 #warning "kde4: how remove it ?.???"
360 //memberList()->removeRef(w);
365 void KMouthApp::slotEditPhrasebook () {
366 PhraseBookDialog *phraseBookDialog = PhraseBookDialog::get();
367 // As we do not know whether the we are already connected to the slot,
368 // we first disconnect and then connect again.
369 disconnect (phraseBookDialog, SIGNAL(phrasebookConfirmed (PhraseBook &)),
370 this, SLOT(slotPhrasebookConfirmed (PhraseBook &)));
371 connect (phraseBookDialog, SIGNAL(phrasebookConfirmed (PhraseBook &)),
372 this, SLOT(slotPhrasebookConfirmed (PhraseBook &)));
374 // As we do not know whether the phrase book edit window is already open,
375 // we first open and then raise it, so that it is surely the top window.
376 phraseBookDialog->show();
377 phraseBookDialog->raise();
380 void KMouthApp::slotViewMenuBar() {
381 slotStatusMsg(i18n("Toggling menubar..."));
383 if(!viewMenuBar->isChecked())
384 menuBar()->hide();
385 else
386 menuBar()->show();
388 slotStatusMsg(i18n("Ready."));
391 void KMouthApp::slotViewToolBar()
393 slotStatusMsg(i18n("Toggling toolbar..."));
394 ///////////////////////////////////////////////////////////////////
395 // turn Toolbar on or off
396 if(!viewToolBar->isChecked())
398 toolBar("mainToolBar")->hide();
400 else
402 toolBar("mainToolBar")->show();
405 slotStatusMsg(i18n("Ready."));
408 void KMouthApp::slotViewPhrasebookBar()
410 slotStatusMsg(i18n("Toggling phrasebook bar..."));
411 ///////////////////////////////////////////////////////////////////
412 // turn Toolbar on or off
413 if(!viewPhrasebookBar->isChecked())
415 toolBar("phrasebookBar")->hide();
417 else
419 toolBar("phrasebookBar")->show();
422 slotStatusMsg(i18n("Ready."));
425 void KMouthApp::slotViewStatusBar()
427 slotStatusMsg(i18n("Toggle the statusbar..."));
428 ///////////////////////////////////////////////////////////////////
429 //turn Statusbar on or off
430 if(!viewStatusBar->isChecked())
432 statusBar()->hide();
434 else
436 statusBar()->show();
439 slotStatusMsg(i18n("Ready."));
442 void KMouthApp::slotConfigureTTS() {
443 phraseList->saveWordCompletion();
444 optionsDialog->show();
448 void KMouthApp::slotStatusMsg(const QString &text)
450 ///////////////////////////////////////////////////////////////////
451 // change status message permanently
452 statusBar()->clear();
453 statusBar()->changeItem(text, ID_STATUS_MSG);
456 void KMouthApp::slotPhrasebookConfirmed (PhraseBook &book) {
457 QString name = "phrasebooks";
458 QMenu *popup = (QMenu *)factory()->container(name, this);
459 KToolBar *toolbar = toolBar ("phrasebookBar");
461 KActionPtrList actions = phrases->actions ();
462 KActionPtrList::iterator iter;
463 for (iter = actions.begin(); iter != actions.end(); ++iter) {
464 (*iter)->unplugAll();
466 delete phrases;
468 phrases = new KActionCollection (this, actionCollection());
469 book.addToGUI (popup, toolbar, phrases, this, SLOT(slotPhraseSelected (const QString &)));
471 QString bookLocation = KApplication::kApplication()->dirs()->saveLocation ("appdata", "/");
472 if (!bookLocation.isNull() && !bookLocation.isEmpty()) {
473 book.save (KURL::fromPathOrURL( bookLocation + "standard.phrasebook" ));
477 void KMouthApp::slotConfigurationChanged()
479 optionsDialog->saveOptions (config);
482 void KMouthApp::slotPhraseSelected (const QString &phrase) {
483 phraseList->insert (phrase);
484 if (optionsDialog->isSpeakImmediately())
485 phraseList->speak ();
488 TextToSpeechSystem *KMouthApp::getTTSSystem() const {
489 return optionsDialog->getTTSSystem();
492 #include "kmouth.moc"