1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
|
/***************************************************************************
* Copyright (C) 2002 by Gunnar Schmi Dt <kmouth@schmi-dt.de *
* (C) 2015 by Jeremy Whiting <jpwhiting@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
// application specific includes
#include "kmouth.h"
// include files for Qt
#include <QMenu>
#include <QMenuBar>
#include <QPrintDialog>
#include <QStandardPaths>
#include <QStatusBar>
// include files for KDE
#include <KActionCollection>
#include <KConfigGroup>
#include <KLocalizedString>
#include <KStandardAction>
#include <KStandardShortcut>
#include <KToggleAction>
#include <KToolBar>
#include <KXMLGUIFactory>
#include "configwizard.h"
#include "optionsdialog.h"
#include "phrasebook/phrasebook.h"
#include "phrasebook/phrasebookdialog.h"
#include "phraselist.h"
KMouthApp::KMouthApp(QWidget *, const QString &name)
: KXmlGuiWindow(nullptr)
{
setWindowIcon(QIcon::fromTheme(QStringLiteral("kmouth")));
setObjectName(name);
isConfigured = false;
///////////////////////////////////////////////////////////////////
// call inits to invoke all other construction parts
initStatusBar();
initPhraseList();
initActions();
optionsDialog = new OptionsDialog(this);
connect(optionsDialog, &OptionsDialog::configurationChanged, this, &KMouthApp::slotConfigurationChanged);
connect(optionsDialog, &OptionsDialog::configurationChanged, phraseList, &PhraseList::configureCompletion);
phrases = new KActionCollection(static_cast<QWidget *>(this));
readOptions();
ConfigWizard *wizard = new ConfigWizard(this);
if (wizard->configurationNeeded()) {
if (wizard->requestConfiguration()) {
isConfigured = true;
saveOptions();
wizard->saveConfig();
readOptions();
} else
isConfigured = false;
} else
isConfigured = true;
delete wizard;
if (isConfigured) {
phraseList->configureCompletion();
}
///////////////////////////////////////////////////////////////////
// disable actions at startup
fileSaveAs->setEnabled(false);
filePrint->setEnabled(false);
printer = nullptr;
}
KMouthApp::~KMouthApp()
{
delete printer;
}
bool KMouthApp::configured()
{
return isConfigured;
}
void KMouthApp::initActions()
{
// The "File" menu
fileOpen = actionCollection()->addAction(QStringLiteral("file_open"));
fileOpen->setIcon(QIcon::fromTheme(QStringLiteral("document-open")));
fileOpen->setText(i18n("&Open as History..."));
actionCollection()->setDefaultShortcuts(fileOpen, KStandardShortcut::open());
connect(fileOpen, &QAction::triggered, this, &KMouthApp::slotFileOpen);
fileOpen->setToolTip(i18n("Opens an existing file as history"));
fileOpen->setWhatsThis(i18n("Opens an existing file as history"));
fileSaveAs = actionCollection()->addAction(QStringLiteral("file_save_as"));
fileSaveAs->setIcon(QIcon::fromTheme(QStringLiteral("document-save")));
fileSaveAs->setText(i18n("Save &History As..."));
actionCollection()->setDefaultShortcuts(fileSaveAs, KStandardShortcut::save());
connect(fileSaveAs, &QAction::triggered, this, &KMouthApp::slotFileSaveAs);
fileSaveAs->setToolTip(i18n("Saves the actual history as..."));
fileSaveAs->setWhatsThis(i18n("Saves the actual history as..."));
filePrint = actionCollection()->addAction(QStringLiteral("file_print"));
filePrint->setIcon(QIcon::fromTheme(QStringLiteral("document-print")));
filePrint->setText(i18n("&Print History..."));
actionCollection()->setDefaultShortcuts(filePrint, KStandardShortcut::print());
connect(filePrint, &QAction::triggered, this, &KMouthApp::slotFilePrint);
filePrint->setToolTip(i18n("Prints out the actual history"));
filePrint->setWhatsThis(i18n("Prints out the actual history"));
fileQuit = KStandardAction::quit(this, SLOT(slotFileQuit()), actionCollection());
fileQuit->setToolTip(i18n("Quits the application"));
fileQuit->setWhatsThis(i18n("Quits the application"));
// The "Edit" menu
editCut = KStandardAction::cut(phraseList, SLOT(cut()), actionCollection());
editCut->setToolTip(i18n("Cuts the selected section and puts it to the clipboard"));
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."));
editCopy = KStandardAction::copy(phraseList, SLOT(copy()), actionCollection());
editCopy->setToolTip(i18n("Copies the selected section to the clipboard"));
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."));
editPaste = KStandardAction::paste(phraseList, SLOT(paste()), actionCollection());
editPaste->setToolTip(i18n("Pastes the clipboard contents to current position"));
editPaste->setWhatsThis(i18n("Pastes the clipboard contents at the current cursor position into the edit field."));
editSpeak = actionCollection()->addAction(QStringLiteral("edit_speak"));
editSpeak->setIcon(QIcon::fromTheme(QStringLiteral("text-speak")));
editSpeak->setText(i18nc("Start speaking", "&Speak"));
connect(editSpeak, &QAction::triggered, phraseList, &PhraseList::speak);
editSpeak->setToolTip(i18n("Speaks the currently active sentence(s)"));
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."));
// The "Phrase book" menu
phrasebookEdit = actionCollection()->addAction(QStringLiteral("phrasebook_edit"));
phrasebookEdit->setText(i18n("&Edit..."));
connect(phrasebookEdit, &QAction::triggered, this, &KMouthApp::slotEditPhrasebook);
// The "Options" menu
viewMenuBar = KStandardAction::showMenubar(this, SLOT(slotViewMenuBar()), actionCollection());
// FIXME: Disable so it will compile.
// viewToolBar = KStandardAction::showToolbar(this, SLOT(slotViewToolBar()), actionCollection());
// viewToolBar->setToolTip(i18n("Enables/disables the toolbar"));
// viewToolBar->setWhatsThis (i18n("Enables/disables the toolbar"));
viewPhrasebookBar = actionCollection()->add<KToggleAction>(QStringLiteral("showPhrasebookBar"));
viewPhrasebookBar->setText(i18n("Show P&hrasebook Bar"));
connect(viewPhrasebookBar, &QAction::triggered, this, &KMouthApp::slotViewPhrasebookBar);
viewPhrasebookBar->setToolTip(i18n("Enables/disables the phrasebook bar"));
viewPhrasebookBar->setWhatsThis(i18n("Enables/disables the phrasebook bar"));
viewStatusBar = KStandardAction::showStatusbar(this, SLOT(slotViewStatusBar()), actionCollection());
viewStatusBar->setToolTip(i18n("Enables/disables the statusbar"));
viewStatusBar->setWhatsThis(i18n("Enables/disables the statusbar"));
configureTTS = actionCollection()->addAction(QStringLiteral("configureTTS"));
configureTTS->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
configureTTS->setText(i18n("&Configure KMouth..."));
connect(configureTTS, &QAction::triggered, this, &KMouthApp::slotConfigureTTS);
configureTTS->setToolTip(i18n("Opens the configuration dialog"));
configureTTS->setWhatsThis(i18n("Opens the configuration dialog"));
// The "Help" menu
// The "Help" menu will automatically get created.
// The popup menu of the list of spoken sentences
phraseListSpeak = actionCollection()->addAction(QStringLiteral("phraselist_speak"));
phraseListSpeak->setIcon(QIcon::fromTheme(QStringLiteral("text-speak")));
phraseListSpeak->setText(i18n("&Speak"));
phraseListSpeak->setToolTip(i18n("Speaks the currently selected phrases in the history"));
connect(phraseListSpeak, &QAction::triggered, phraseList, &PhraseList::speakListSelection);
phraseListSpeak->setWhatsThis(i18n("Speaks the currently selected phrases in the history"));
phraseListRemove = actionCollection()->addAction(QStringLiteral("phraselist_remove"));
phraseListRemove->setIcon(QIcon::fromTheme(QStringLiteral("edit-delete")));
phraseListRemove->setText(i18n("&Delete"));
connect(phraseListRemove, &QAction::triggered, phraseList, &PhraseList::removeListSelection);
phraseListRemove->setToolTip(i18n("Deletes the currently selected phrases from the history"));
phraseListRemove->setWhatsThis(i18n("Deletes the currently selected phrases from the history"));
phraseListCut = actionCollection()->addAction(QStringLiteral("phraselist_cut"));
phraseListCut->setIcon(QIcon::fromTheme(QStringLiteral("edit-cut")));
phraseListCut->setText(i18n("Cu&t"));
connect(phraseListCut, &QAction::triggered, phraseList, &PhraseList::cutListSelection);
phraseListCut->setToolTip(i18n("Cuts the currently selected phrases from the history and puts them to the clipboard"));
phraseListCut->setWhatsThis(i18n("Cuts the currently selected phrases from the history and puts them to the clipboard"));
phraseListCopy = actionCollection()->addAction(QStringLiteral("phraselist_copy"));
phraseListCopy->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy")));
phraseListCopy->setText(i18n("&Copy"));
connect(phraseListCopy, &QAction::triggered, phraseList, &PhraseList::copyListSelection);
phraseListCut->setToolTip(i18n("Copies the currently selected phrases from the history to the clipboard"));
phraseListCut->setWhatsThis(i18n("Copies the currently selected phrases from the history to the clipboard"));
phraselistSelectAll = actionCollection()->addAction(QStringLiteral("phraselist_select_all"));
phraselistSelectAll->setText(i18n("Select &All Entries"));
connect(phraselistSelectAll, &QAction::triggered, phraseList, &PhraseList::selectAllEntries);
phraselistSelectAll->setToolTip(i18n("Selects all phrases in the history"));
phraselistSelectAll->setWhatsThis(i18n("Selects all phrases in the history"));
phraselistDeselectAll = actionCollection()->addAction(QStringLiteral("phraselist_deselect_all"));
phraselistDeselectAll->setText(i18n("D&eselect All Entries"));
connect(phraselistDeselectAll, &QAction::triggered, phraseList, &PhraseList::deselectAllEntries);
phraselistDeselectAll->setToolTip(i18n("Deselects all phrases in the history"));
phraselistDeselectAll->setWhatsThis(i18n("Deselects all phrases in the history"));
// The popup menu of the edit field
// The popup menu of the edit field will automatically get created.
// use the absolute path to your kmouthui.rc file for testing purpose in createGUI();
createGUI();
}
void KMouthApp::initStatusBar()
{
///////////////////////////////////////////////////////////////////
// STATUSBAR
// TODO: add your own items you need for displaying current application status.
m_statusLabel = new QLabel(i18nc("The job is done", "Ready."));
statusBar()->addPermanentWidget(m_statusLabel);
}
void KMouthApp::initPhraseList()
{
////////////////////////////////////////////////////////////////////
// create the main widget here that is managed by KTMainWindow's view-region and
// connect the widget to your document to display document contents.
phraseList = new PhraseList(this);
setCentralWidget(phraseList);
}
void KMouthApp::openDocumentFile(const QUrl &url)
{
slotStatusMsg(i18n("Opening file..."));
phraseList->open(url);
slotStatusMsg(i18nc("The job is done", "Ready."));
}
void KMouthApp::saveOptions()
{
if (isConfigured) {
KConfigGroup cg(KSharedConfig::openConfig(), QStringLiteral("General Options"));
cg.writeEntry("Geometry", size());
cg.writeEntry("Show Menubar", viewMenuBar->isChecked());
// FIXME: Toolbar disabled so it will compile.
// cg.writeEntry("Show Toolbar", viewToolBar->isChecked());
cg.writeEntry("Show Phrasebook Bar", viewPhrasebookBar->isChecked());
cg.writeEntry("Show Statusbar", viewStatusBar->isChecked());
// FIXME: KToolBar no longer has barPos() method.
// cg.writeEntry("ToolBarPos", (int) toolBar("mainToolBar")->barPos());
if (phraseList != nullptr)
phraseList->saveCompletionOptions();
optionsDialog->saveOptions();
KConfigGroup cg2(KSharedConfig::openConfig(), QStringLiteral("mainToolBar"));
toolBar(QStringLiteral("mainToolBar"))->saveSettings(cg2);
KConfigGroup cg3(KSharedConfig::openConfig(), QStringLiteral("phrasebookBar"));
toolBar(QStringLiteral("phrasebookBar"))->saveSettings(cg);
}
}
void KMouthApp::readOptions()
{
KConfigGroup cg(KSharedConfig::openConfig(), QStringLiteral("General Options"));
// bar status settings
bool bViewMenubar = cg.readEntry("Show Menubar", true);
viewMenuBar->setChecked(bViewMenubar);
slotViewMenuBar();
// FIXME: Toolbar disabled so it will compile.
// bool bViewToolbar = cg.readEntry("Show Toolbar", QVariant(true)).toBool();
// viewToolBar->setChecked(bViewToolbar);
// slotViewToolBar();
bool bViewPhrasebookbar = cg.readEntry("Show Phrasebook Bar", true);
viewPhrasebookBar->setChecked(bViewPhrasebookbar);
bool bViewStatusbar = cg.readEntry("Show Statusbar", true);
viewStatusBar->setChecked(bViewStatusbar);
slotViewStatusBar();
// bar position settings
// FIXME:
// KToolBar::BarPosition toolBarPos;
// toolBarPos=(KToolBar::BarPosition) cg.readEntry("ToolBarPos", int(KToolBar::Top));
// toolBar("mainToolBar")->setBarPos(toolBarPos);
QSize size = cg.readEntry("Geometry", QSize());
if (!size.isEmpty()) {
resize(size);
}
optionsDialog->readOptions();
toolBar(QStringLiteral("mainToolBar"))->applySettings(KSharedConfig::openConfig()->group(QStringLiteral("mainToolBar")));
toolBar(QStringLiteral("phrasebookBar"))->applySettings(KSharedConfig::openConfig()->group(QStringLiteral("phrasebookBar")));
slotPhrasebookConfirmed();
if (phraseList != nullptr)
phraseList->readCompletionOptions();
}
bool KMouthApp::queryClose()
{
saveOptions();
return true;
}
void KMouthApp::enableMenuEntries(bool existSelectedEntries, bool existDeselectedEntries)
{
bool existEntries = existSelectedEntries | existDeselectedEntries;
fileSaveAs->setEnabled(existEntries);
filePrint->setEnabled(existEntries);
phraselistSelectAll->setEnabled(existDeselectedEntries);
phraselistDeselectAll->setEnabled(existSelectedEntries);
phraseListSpeak->setEnabled(existSelectedEntries);
phraseListRemove->setEnabled(existSelectedEntries);
phraseListCut->setEnabled(existSelectedEntries);
phraseListCopy->setEnabled(existSelectedEntries);
}
/////////////////////////////////////////////////////////////////////
// SLOT IMPLEMENTATION
/////////////////////////////////////////////////////////////////////
void KMouthApp::slotFileOpen()
{
slotStatusMsg(i18n("Opening file..."));
phraseList->open();
slotStatusMsg(i18nc("The job is done", "Ready."));
}
void KMouthApp::slotFileSaveAs()
{
slotStatusMsg(i18n("Saving history with a new filename..."));
phraseList->save();
slotStatusMsg(i18nc("The job is done", "Ready."));
}
void KMouthApp::slotFilePrint()
{
slotStatusMsg(i18n("Printing..."));
if (printer == nullptr) {
printer = new QPrinter();
}
QPrintDialog *printDialog = new QPrintDialog(printer, nullptr);
if (printDialog->exec()) {
phraseList->print(printer);
}
slotStatusMsg(i18nc("The job is done", "Ready."));
}
void KMouthApp::slotFileQuit()
{
slotStatusMsg(i18nc("Shutting down the application", "Exiting..."));
saveOptions();
// close the first window, the list makes the next one the first again.
// This ensures that queryClose() is called on each window to ask for closing
if (!memberList().isEmpty()) {
for (int i = 0; i < memberList().size(); ++i) {
// only close the window if the closeEvent is accepted. If the user presses Cancel on the saveModified() dialog,
// the window and the application stay open.
KMainWindow *w = memberList().at(i);
if (!w->close())
break;
#ifdef __GNUC__
#warning "kde4: how remove it ?.???"
#endif
// memberList()->removeRef(w);
}
}
}
void KMouthApp::slotEditPhrasebook()
{
PhraseBookDialog *phraseBookDialog = PhraseBookDialog::get();
// As we do not know whether the we are already connected to the slot,
// we first disconnect and then connect again.
disconnect(phraseBookDialog, &PhraseBookDialog::phrasebookConfirmed, this, &KMouthApp::slotPhrasebookConfirmed);
connect(phraseBookDialog, &PhraseBookDialog::phrasebookConfirmed, this, &KMouthApp::slotPhrasebookConfirmed);
// As we do not know whether the phrase book edit window is already open,
// we first open and then raise it, so that it is surely the top window.
phraseBookDialog->show();
phraseBookDialog->raise();
}
void KMouthApp::slotViewMenuBar()
{
slotStatusMsg(i18n("Toggling menubar..."));
if (!viewMenuBar->isChecked())
menuBar()->hide();
else
menuBar()->show();
slotStatusMsg(i18nc("The job is done", "Ready."));
}
void KMouthApp::slotViewToolBar()
{
slotStatusMsg(i18n("Toggling toolbar..."));
///////////////////////////////////////////////////////////////////
// turn Toolbar on or off
if (!viewToolBar->isChecked()) {
toolBar(QStringLiteral("mainToolBar"))->hide();
} else {
toolBar(QStringLiteral("mainToolBar"))->show();
}
slotStatusMsg(i18nc("The job is done", "Ready."));
}
void KMouthApp::slotViewPhrasebookBar()
{
slotStatusMsg(i18n("Toggling phrasebook bar..."));
///////////////////////////////////////////////////////////////////
// turn Toolbar on or off
if (!viewPhrasebookBar->isChecked()) {
toolBar(QStringLiteral("phrasebookBar"))->hide();
} else {
toolBar(QStringLiteral("phrasebookBar"))->show();
}
slotStatusMsg(i18nc("The job is done", "Ready."));
}
void KMouthApp::slotViewStatusBar()
{
slotStatusMsg(i18n("Toggle the statusbar..."));
///////////////////////////////////////////////////////////////////
// turn Statusbar on or off
if (!viewStatusBar->isChecked()) {
statusBar()->hide();
} else {
statusBar()->show();
}
slotStatusMsg(i18nc("The job is done", "Ready."));
}
void KMouthApp::slotConfigureTTS()
{
phraseList->saveWordCompletion();
optionsDialog->show();
}
void KMouthApp::slotStatusMsg(const QString &text)
{
///////////////////////////////////////////////////////////////////
// change status message permanently
m_statusLabel->setText(text);
}
void KMouthApp::slotPhrasebookConfirmed()
{
QString standardBook = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("standard.phrasebook"));
if (!standardBook.isEmpty()) {
PhraseBook book;
book.open(QUrl::fromLocalFile(standardBook));
QString name = QStringLiteral("phrasebooks");
QMenu *popup = (QMenu *)factory()->container(name, this);
KToolBar *toolbar = toolBar(QStringLiteral("phrasebookBar"));
delete phrases;
phrases = new KActionCollection(actionCollection());
book.addToGUI(popup, toolbar, phrases, this, SLOT(slotPhraseSelected(QString)));
}
}
void KMouthApp::slotConfigurationChanged()
{
optionsDialog->saveOptions();
}
void KMouthApp::slotPhraseSelected(const QString &phrase)
{
phraseList->insert(phrase);
if (optionsDialog->isSpeakImmediately())
phraseList->speak();
}
TextToSpeechSystem *KMouthApp::getTTSSystem() const
{
return optionsDialog->getTTSSystem();
}
#include "moc_kmouth.cpp"
|