Port to new api
[kdeaccessibility.git] / kmouth / phrasebook / phrasebookdialog.cpp
blob3685e9d1a7db9212099f980627eb996afeebdc88
1 /***************************************************************************
2 phrasebookdialog.cpp - description
3 -------------------
4 begin : Don Sep 19 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 <QApplication>
20 #include <QLayout>
21 #include <QClipboard>
22 #include <QRadioButton>
23 #include <QEvent>
24 #include <QPainter>
25 #include <QStyle>
26 #include <q3groupbox.h>
27 #include <QMenu>
28 #include <q3valuestack.h>
29 #include <q3ptrstack.h>
30 #include <ktoolbarpopupaction.h>
31 //Added by qt3to4:
32 #include <QGridLayout>
33 #include <QDropEvent>
34 #include <QLabel>
35 #include <QVBoxLayout>
36 #include <Q3ListViewItem>
37 // include files for KDE
38 #include <kmenu.h>
39 #include <kxmlguifactory.h>
40 #include <klocale.h>
41 #include <kaction.h>
42 #include <kapplication.h>
43 #include <k3listview.h>
44 #include <kiconloader.h>
45 #include <kguiitem.h>
46 #include <kmessagebox.h>
47 #include <kfiledialog.h>
48 #include <kstandarddirs.h>
49 #include <kdesktopfile.h>
50 #include <kactionmenu.h>
51 #include <kstandardaction.h>
53 #include "phrasebookdialog.h"
54 #include "phrasetree.h"
56 namespace PhraseBookPrivate {
57 enum columns {
58 name = 1,
59 filename = 2
63 CheckBookItem::CheckBookItem (Q3ListViewItem *parent, Q3ListViewItem *last,
64 const QString &text, const QString &name, const QString &filename)
65 : Q3CheckListItem (parent, text, Q3CheckListItem::CheckBox)
67 moveItem (last);
68 setText(PhraseBookPrivate::name, name);
69 setText(PhraseBookPrivate::filename, filename);
70 setSelectable(false);
72 if (filename.isNull() || filename.isEmpty())
73 numberOfBooks = 0;
74 else
75 numberOfBooks = 1;
76 selectedBooks = 0;
77 ((CheckBookItem*)parent)->childChange (numberOfBooks, selectedBooks);
80 CheckBookItem::CheckBookItem (Q3ListView *parent, Q3ListViewItem *last,
81 const QString &text, const QString &name, const QString &filename)
82 : Q3CheckListItem (parent, text, Q3CheckListItem::CheckBox)
84 moveItem (last);
85 setText(PhraseBookPrivate::name, name);
86 setText(PhraseBookPrivate::filename, filename);
87 setSelectable(false);
89 if (filename.isNull() || filename.isEmpty())
90 numberOfBooks = 0;
91 else
92 numberOfBooks = 1;
93 selectedBooks = 0;
96 CheckBookItem::~CheckBookItem () {
99 void CheckBookItem::activate() {
100 Q3ListView *lv = listView();
102 if ((lv != 0) && (!lv->isEnabled()) || (!isEnabled()))
103 return;
105 setOn (!isOn());
106 ignoreDoubleClick();
109 void CheckBookItem::stateChange (bool on) {
110 Q3ListViewItem *item = firstChild();
111 if (item == 0) {
112 Q3ListViewItem *parent = this->parent();
113 if (parent != 0) {
114 if (on)
115 ((CheckBookItem*)parent)->childChange (0, 1);
116 else
117 ((CheckBookItem*)parent)->childChange (0, -1);
120 else propagateStateChange();
123 void CheckBookItem::propagateStateChange () {
124 Q3ListViewItem *item = firstChild();
125 while (item != 0) {
126 if (isOn() != ((Q3CheckListItem*)item)->isOn())
127 ((Q3CheckListItem*)item)->setOn (isOn());
128 else
129 ((CheckBookItem*)item)->propagateStateChange ();
130 item = item->nextSibling();
134 void CheckBookItem::childChange (int numberDiff, int selDiff) {
135 numberOfBooks += numberDiff;
136 selectedBooks += selDiff;
137 Q3ListViewItem *parent = this->parent();
138 if (parent != 0)
139 ((CheckBookItem*)parent)->childChange (numberDiff, selDiff);
141 QString text = i18np(" (%2 of 1 book selected)",
142 " (%2 of %1 books selected)",
143 numberOfBooks, selectedBooks);
144 setText(0, this->text(PhraseBookPrivate::name)+text);
147 /***************************************************************************/
149 InitialPhraseBookWidget::InitialPhraseBookWidget (QWidget *parent, const char *name)
150 : QWidget(parent)
152 setObjectName(name);
153 QVBoxLayout *mainLayout = new QVBoxLayout (this);
154 mainLayout->setSpacing(KDialog::spacingHint());
155 QLabel *label = new QLabel (i18n("Please decide which phrase books you need:"), this);
156 label->setObjectName("booksTitle");
157 mainLayout->addWidget (label);
159 books = new K3ListView (this);
160 books->setSorting (-1);
161 books->setItemsMovable (false);
162 books->setDragEnabled (false);
163 books->setAcceptDrops (false);
164 books->addColumn (i18n("Book"));
165 books->setRootIsDecorated (true);
166 books->setAllColumnsShowFocus (true);
167 books->setSelectionMode (Q3ListView::Multi);
168 mainLayout->addWidget (books);
170 initStandardPhraseBooks();
173 InitialPhraseBookWidget::~InitialPhraseBookWidget () {
176 void InitialPhraseBookWidget::initStandardPhraseBooks() {
177 StandardBookList bookPaths = PhraseBookDialog::standardPhraseBooks();
179 Q3ListViewItem *parent = 0;
180 Q3ListViewItem *last = 0;
181 QStringList currentNamePath;
182 currentNamePath<<"";
183 Q3PtrStack<Q3ListViewItem> stack;
184 StandardBookList::iterator it;
185 for (it = bookPaths.begin(); it != bookPaths.end(); ++it) {
186 QString namePath = (*it).path;
187 QStringList dirs = namePath.split( "/");
189 QStringList::iterator it1=currentNamePath.begin();
190 QStringList::iterator it2=dirs.begin();
191 for (; (it1 != currentNamePath.end())
192 && (it1 != dirs.end()) && (*it1 == *it2); ++it1, ++it2);
193 for (; it1 != currentNamePath.end(); ++it1) {
194 last = parent;
195 parent = stack.pop();
197 for (; it2 != dirs.end(); ++it2) {
198 stack.push (parent);
199 Q3ListViewItem *newParent;
200 if (parent == 0)
201 newParent = new CheckBookItem (books, last, *it2, *it2, QString());
202 else
203 newParent = new CheckBookItem (parent, last, *it2, *it2, QString());
204 parent = newParent;
205 last = 0;
207 currentNamePath = dirs;
209 Q3ListViewItem *book;
210 if (parent == 0)
211 book = new CheckBookItem (books, last, (*it).name, (*it).name, (*it).filename);
212 else
213 book = new CheckBookItem (parent, last, (*it).name, (*it).name, (*it).filename);
214 last = book;
218 void InitialPhraseBookWidget::createBook () {
219 PhraseBook book;
220 Q3ListViewItem *item = books->firstChild();
221 while (item != 0) {
222 if (item->firstChild() != 0) {
223 item = item->firstChild();
225 else {
226 if (((Q3CheckListItem*)item)->isOn()) {
227 PhraseBook localBook;
228 localBook.open(KUrl( item->text(PhraseBookPrivate::filename )));
229 book += localBook;
232 while ((item != 0) && (item->nextSibling() == 0)) {
233 item = item->parent();
235 if (item != 0)
236 item = item->nextSibling();
240 QString bookLocation = KGlobal::dirs()->saveLocation ("appdata", "/");
241 if (!bookLocation.isNull() && !bookLocation.isEmpty()) {
242 book.save (KUrl( bookLocation + "standard.phrasebook" ));
246 /***************************************************************************/
248 ButtonBoxWidget::ButtonBoxWidget (QWidget *parent, const char *name)
249 : QWidget(parent)
251 setupUi(this);
252 setObjectName(name);
253 keyButtonPlaceLayout = new QGridLayout (keyButtonPlace);
254 keyButtonPlaceLayout->setObjectName("keyButtonPlaceLayout");
255 keyButtonPlaceLayout->setMargin(0);
256 keyButtonPlaceLayout->setSpacing(0);
258 keyButton = new KKeySequenceWidget (keyButtonPlace);
259 keyButtonPlaceLayout->addWidget (keyButton, 1,1);
260 keyButton->setWhatsThis( i18n("By clicking on this button you can select the keyboard shortcut associated with the selected phrase."));
262 group = new Q3ButtonGroup (phrasebox);
263 group->hide();
264 group->setExclusive (true);
265 group->insert (noKey);
266 group->insert (customKey);
269 ButtonBoxWidget::~ButtonBoxWidget () {
272 /***************************************************************************/
274 namespace PhraseBookPrivate {
275 PhraseBookDialog *instance = 0;
278 PhraseBookDialog::PhraseBookDialog ()
279 : KXmlGuiWindow (0)
281 setObjectName("phraseEditDialog");
282 setCaption (i18n("Phrase Book"));
283 initGUI();
284 initActions();
285 initStandardPhraseBooks();
286 QString standardBook = KGlobal::dirs()->findResource("appdata", "standard.phrasebook");
287 if (!standardBook.isNull() && !standardBook.isEmpty()) {
288 PhraseBook book;
289 book.open(KUrl( standardBook ));
290 treeView->clear();
291 treeView->addBook(0, 0, &book);
292 treeView->setCurrentItem(treeView->firstChild());
293 selectionChanged();
294 phrasebookChanged = false;
296 // i18n("Edit Phrase Book")
299 PhraseBookDialog *PhraseBookDialog::get() {
300 if (PhraseBookPrivate::instance == 0)
301 PhraseBookPrivate::instance = new PhraseBookDialog();
302 return PhraseBookPrivate::instance;
305 PhraseBookDialog::~PhraseBookDialog() {
306 PhraseBookPrivate::instance = 0;
309 void PhraseBookDialog::initGUI () {
310 QWidget *page = new QWidget( this );
311 setCentralWidget(page);
312 QVBoxLayout *mainLayout = new QVBoxLayout (page);
313 mainLayout->setMargin(0);
315 treeView = new PhraseTree (page, "phrasetree");
316 treeView->setSorting (-1);
317 treeView->setItemsMovable (true);
318 treeView->setDragEnabled (true);
319 treeView->setAcceptDrops (true);
320 treeView->addColumn (i18n("Phrase"));
321 treeView->addColumn (i18n("Shortcut"));
322 treeView->setRootIsDecorated (true);
323 treeView->setAllColumnsShowFocus (true);
324 treeView->setSelectionMode (Q3ListView::Extended);
325 treeView->setWhatsThis( i18n("This list contains the current phrase book in a tree structure. You can select and modify individual phrases and sub phrase books"));
326 connect (treeView, SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
327 connect (treeView, SIGNAL(contextMenuRequested (Q3ListViewItem *, const QPoint &, int)), this, SLOT(contextMenuRequested (Q3ListViewItem *, const QPoint &, int)));
328 connect (treeView, SIGNAL(dropped (QDropEvent *, Q3ListViewItem *, Q3ListViewItem *)), this, SLOT(slotDropped (QDropEvent *, Q3ListViewItem *, Q3ListViewItem *)));
329 connect (treeView, SIGNAL(moved (Q3ListViewItem *, Q3ListViewItem *, Q3ListViewItem *)), this, SLOT(slotMoved (Q3ListViewItem *, Q3ListViewItem *, Q3ListViewItem *)));
330 mainLayout->addWidget (treeView);
332 buttonBox = new ButtonBoxWidget (page, "buttonbox");
333 connect (buttonBox->lineEdit, SIGNAL(textChanged(const QString &)), SLOT(slotTextChanged(const QString &)));
334 connect (buttonBox->noKey, SIGNAL(clicked()), SLOT(slotNoKey()));
335 connect (buttonBox->customKey, SIGNAL(clicked()), SLOT(slotCustomKey()));
336 connect (buttonBox->keyButton, SIGNAL(capturedShortcut(const KShortcut&)), SLOT(capturedShortcut(const KShortcut&)));
337 mainLayout->addWidget (buttonBox);
339 treeView->setFocus();
340 selectionChanged();
343 void PhraseBookDialog::initActions() {
344 // The file menu
345 fileNewPhrase = actionCollection()->addAction("file_new_phrase");
346 fileNewPhrase->setIcon(KIcon("phrase_new"));
347 fileNewPhrase->setText(i18n("&New Phrase"));
348 connect(fileNewPhrase, SIGNAL(triggered(bool)), this, SLOT(slotAddPhrase()));
349 fileNewPhrase->setToolTip(i18n("Adds a new phrase"));
350 fileNewPhrase->setWhatsThis (i18n("Adds a new phrase"));
352 fileNewBook = actionCollection()->addAction("file_new_book");
353 fileNewBook->setIcon(KIcon("phrasebook_new"));
354 fileNewBook->setText(i18n("New Phrase &Book"));
355 connect(fileNewBook, SIGNAL(triggered(bool)), this, SLOT(slotAddPhrasebook()));
356 fileNewBook->setToolTip(i18n("Adds a new phrase book into which other books and phrases can be placed"));
357 fileNewBook->setWhatsThis (i18n("Adds a new phrase book into which other books and phrases can be placed"));
359 fileSave = KStandardAction::save(this, SLOT(slotSave()), actionCollection());
360 fileSave->setToolTip(i18n("Saves the phrase book onto the hard disk"));
361 fileSave->setWhatsThis (i18n("Saves the phrase book onto the hard disk"));
363 fileImport = actionCollection()->addAction("file_import");
364 fileImport->setIcon(KIcon("phrasebook_open"));
365 fileImport->setText(i18n("&Import..."));
366 connect(fileImport, SIGNAL(triggered(bool)), this, SLOT(slotImportPhrasebook()));
367 fileImport->setToolTip(i18n("Imports a file and adds its contents to the phrase book"));
368 fileImport->setWhatsThis (i18n("Imports a file and adds its contents to the phrase book"));
370 toolbarImport = new KToolBarPopupAction(KIcon("phrasebook_open"), i18n("&Import..."), this);
371 actionCollection()->addAction("toolbar_import", toolbarImport);
372 connect(toolbarImport, SIGNAL(triggered(bool)), this, SLOT(slotImportPhrasebook()));
373 toolbarImport->setToolTip(i18n("Imports a file and adds its contents to the phrase book"));
374 toolbarImport->setWhatsThis (i18n("Imports a file and adds its contents to the phrase book"));
376 fileImportStandardBook = actionCollection()->add<KActionMenu>("file_import_standard_book");
377 fileImportStandardBook->setIcon(KIcon("phrasebook_open"));
378 fileImportStandardBook->setText(i18n("I&mport Standard Phrase Book"));
379 fileImportStandardBook->setToolTip(i18n("Imports a standard phrase book and adds its contents to the phrase book"));
380 fileImportStandardBook->setWhatsThis (i18n("Imports a standard phrase book and adds its contents to the phrase book"));
382 fileExport = actionCollection()->addAction("file_export");
383 fileExport->setIcon(KIcon("phrasebook_save"));
384 fileExport->setText(i18n("&Export..."));
385 connect(fileExport, SIGNAL(triggered(bool)), this, SLOT(slotExportPhrasebook()));
386 fileExport->setToolTip(i18n("Exports the currently selected phrase(s) or phrase book(s) into a file"));
387 fileExport->setWhatsThis (i18n("Exports the currently selected phrase(s) or phrase book(s) into a file"));
389 filePrint = KStandardAction::print(this, SLOT(slotPrint()), actionCollection());
390 filePrint->setToolTip(i18n("Prints the currently selected phrase(s) or phrase book(s)"));
391 filePrint->setWhatsThis (i18n("Prints the currently selected phrase(s) or phrase book(s)"));
393 fileClose = KStandardAction::close(this, SLOT(close()), actionCollection());
394 fileClose->setToolTip(i18n("Closes the window"));
395 fileClose->setWhatsThis (i18n("Closes the window"));
397 // The edit menu
398 editCut = KStandardAction::cut(this, SLOT(slotCut()), actionCollection());
399 editCut->setToolTip(i18n("Cuts the currently selected entries from the phrase book and puts it to the clipboard"));
400 editCut->setWhatsThis (i18n("Cuts the currently selected entries from the phrase book and puts it to the clipboard"));
402 editCopy = KStandardAction::copy(this, SLOT(slotCopy()), actionCollection());
403 editCopy->setToolTip(i18n("Copies the currently selected entry from the phrase book to the clipboard"));
404 editCopy->setWhatsThis (i18n("Copies the currently selected entry from the phrase book to the clipboard"));
406 editPaste = KStandardAction::paste(this, SLOT(slotPaste()), actionCollection());
407 editPaste->setToolTip(i18n("Pastes the clipboard contents to actual position"));
408 editPaste->setWhatsThis (i18n("Pastes the clipboard contents to actual position"));
410 editDelete = actionCollection()->addAction("edit_delete");
411 editDelete->setIcon(KIcon("edit-delete"));
412 editDelete->setText(i18n("&Delete"));
413 connect(editDelete, SIGNAL(triggered(bool)), this, SLOT(slotRemove()));
414 editDelete->setToolTip(i18n("Deletes the selected entries from the phrase book"));
415 editDelete->setWhatsThis (i18n("Deletes the selected entries from the phrase book"));
417 // use the absolute path to your kmouthui.rc file for testing purpose in createGUI();
418 createGUI("phrasebookdialogui.rc");
421 QString PhraseBookDialog::displayPath (QString filename) {
422 QFileInfo file(filename);
423 QString path = file.path();
424 QString dispPath = "";
425 int position = path.indexOf("/kmouth/books/")+QString("/kmouth/books/").length();
427 while (path.length() > position) {
428 file.setFile(path);
430 KDesktopFile *dirDesc = new KDesktopFile("data", path+"/.directory");
431 QString name = dirDesc->readName();
432 delete dirDesc;
434 if (name.isNull() || name.isEmpty())
435 dispPath += '/' + file.fileName ();
436 else
437 dispPath += '/' + name;
439 path = file.path();
441 return dispPath;
444 StandardBookList PhraseBookDialog::standardPhraseBooks() {
445 QStringList bookPaths = KGlobal::mainComponent().dirs()->findAllResources (
446 "data", "kmouth/books/*.phrasebook",
447 KStandardDirs::Recursive |
448 KStandardDirs::NoDuplicates);
449 QStringList bookNames;
450 QMap<QString,StandardBook> bookMap;
451 QStringList::iterator it;
452 for (it = bookPaths.begin(); it != bookPaths.end(); ++it) {
453 PhraseBook pbook;
454 if (pbook.open (KUrl( *it ))) {
455 StandardBook book;
456 book.name = (*pbook.begin()).getPhrase().getPhrase();
458 book.path = displayPath(*it);
459 book.filename = *it;
461 bookNames += book.path + '/' + book.name;
462 bookMap [book.path + '/' + book.name] = book;
466 bookNames.sort();
468 StandardBookList result;
469 for (it = bookNames.begin(); it != bookNames.end(); ++it)
470 result += bookMap [*it];
472 return result;
475 void PhraseBookDialog::initStandardPhraseBooks () {
476 StandardBookList bookPaths = standardPhraseBooks();
478 KActionMenu *parent = fileImportStandardBook;
479 QStringList currentNamePath;
480 currentNamePath<< "x";
481 Q3PtrStack<KActionMenu> stack;
482 StandardBookList::iterator it;
483 for (it = bookPaths.begin(); it != bookPaths.end(); ++it) {
484 KUrl url;
485 url.setPath((*it).filename);
487 QString namePath = "x/"+(*it).path;
488 QStringList dirs = namePath.split( "/");
490 QStringList::iterator it1=currentNamePath.begin();
491 QStringList::iterator it2=dirs.begin();
492 for (; (it1 != currentNamePath.end())
493 && (it1 != dirs.end()) && (*it1 == *it2); ++it1, ++it2);
494 for (; it1 != currentNamePath.end(); ++it1)
495 parent = stack.pop();
496 for (; it2 != dirs.end(); ++it2) {
497 stack.push (parent);
498 #ifdef __GNUC__
499 #warning "kde4: correct newparent objectname ?"
500 #endif
501 KActionMenu *newParent = actionCollection()->add<KActionMenu>("tmp_menu");
502 newParent->setText(*it2);
503 parent->addAction(newParent);
504 if (parent == fileImportStandardBook)
505 toolbarImport->popupMenu()->addAction(newParent);
506 parent = newParent;
508 currentNamePath = dirs;
510 KAction *book = new StandardPhraseBookInsertAction (
511 url, (*it).name, this, SLOT(slotImportPhrasebook (const KUrl &)), actionCollection());
512 parent->addAction(book);
513 if (parent == fileImportStandardBook)
514 toolbarImport->popupMenu()->addAction(book);
518 PhraseTreeItem *selectedItem (Q3ListView *treeView) {
519 PhraseTreeItem *currentItem = (PhraseTreeItem *)treeView->currentItem();
520 if ((currentItem == 0) || (!currentItem->isSelected()))
521 return 0;
523 Q3ListViewItemIterator it(treeView);
524 while (it.current()) {
525 Q3ListViewItem *item = it.current();
526 if (item->isSelected() && (item != currentItem))
527 return 0;
528 ++it;
530 return currentItem;
533 void PhraseBookDialog::selectionChanged () {
534 bool modified = phrasebookChanged;
535 PhraseTreeItem *currentItem = selectedItem (treeView);
536 if (currentItem == 0) {
537 buttonBox->textLabel->setText (i18n("Text of the &phrase:"));
538 buttonBox->textLabel->setEnabled(false);
539 buttonBox->group->setEnabled(false);
540 buttonBox->lineEdit->setText("");
541 buttonBox->lineEdit->setEnabled(false);
542 buttonBox->shortcutLabel->setEnabled(false);
543 //buttonBox->keyButton->setShortcut("", false);
544 buttonBox->keyButton->setEnabled(false);
545 buttonBox->noKey->setChecked (false);
546 buttonBox->noKey->setEnabled (false);
547 buttonBox->customKey->setChecked (false);
548 buttonBox->customKey->setEnabled (false);
550 else if (currentItem->isPhrase()) {
551 buttonBox->textLabel->setText (i18n("Text of the &phrase:"));
552 buttonBox->textLabel->setEnabled(true);
553 buttonBox->group->setEnabled(true);
554 buttonBox->lineEdit->setText(currentItem->text(0));
555 buttonBox->lineEdit->setEnabled(true);
556 buttonBox->shortcutLabel->setEnabled(true);
557 QString shortcut = currentItem->text(1);
558 #ifdef __GNUC__
559 #warning "kde4 port it"
560 #endif
561 //buttonBox->keyButton->setShortcut(currentItem->cut(), false);
562 if (shortcut.isEmpty() || shortcut.isNull()) {
563 buttonBox->noKey->setChecked (true);
564 buttonBox->customKey->setChecked (false);
566 else {
567 buttonBox->noKey->setChecked (false);
568 buttonBox->customKey->setChecked (true);
570 buttonBox->keyButton->setEnabled(true);
571 buttonBox->noKey->setEnabled(true);
572 buttonBox->customKey->setEnabled(true);
574 else {
575 buttonBox->textLabel->setText (i18n("Name of the &phrase book:"));
576 buttonBox->textLabel->setEnabled(true);
577 buttonBox->group->setEnabled(true);
578 buttonBox->lineEdit->setText(currentItem->text(0));
579 buttonBox->lineEdit->setEnabled(true);
580 buttonBox->shortcutLabel->setEnabled(false);
581 //buttonBox->keyButton->setShortcut("", false);
582 buttonBox->keyButton->setEnabled(false);
583 buttonBox->noKey->setChecked (false);
584 buttonBox->noKey->setEnabled (false);
585 buttonBox->customKey->setChecked (false);
586 buttonBox->customKey->setEnabled (false);
588 phrasebookChanged = modified;
591 bool PhraseBookDialog::queryClose() {
592 if (phrasebookChanged) {
593 int answer = KMessageBox::questionYesNoCancel (this,
594 i18n("<qt>There are unsaved changes.<br>Do you want to apply the changes before closing the \"phrase book\" window or discard the changes?</qt>"),
595 i18n("Closing \"Phrase Book\" Window"),
596 KStandardGuiItem::apply(), KStandardGuiItem::discard(),
597 KStandardGuiItem::cancel(), "AutomaticSave");
598 if (answer == KMessageBox::Yes) {
599 slotSave();
600 return true;
602 return (answer == KMessageBox::No);
604 return true;
607 void PhraseBookDialog::slotTextChanged (const QString &s) {
608 PhraseTreeItem *currentItem = selectedItem (treeView);
609 if (currentItem != 0)
610 currentItem->setText(0, s);
611 phrasebookChanged = true;
614 void PhraseBookDialog::slotNoKey() {
615 buttonBox->noKey->setChecked (true);
616 buttonBox->customKey->setChecked (false);
618 PhraseTreeItem *currentItem = selectedItem (treeView);
619 if (currentItem != 0) {
620 currentItem->setCut (KShortcut(QString()));
621 #ifdef __GNUC__
622 #warning "kde4: port it"
623 #endif
624 //buttonBox->keyButton->setShortcut(currentItem->cut(), false);
626 phrasebookChanged = true;
629 void PhraseBookDialog::slotCustomKey() {
630 buttonBox->keyButton->keySequence();
633 void PhraseBookDialog::capturedShortcut (const KShortcut& cut) {
634 if (cut.isEmpty()) {
635 slotNoKey();
637 else
638 setShortcut (cut);
639 phrasebookChanged = true;
642 void PhraseBookDialog::setShortcut( const KShortcut& cut ) {
643 // Check whether the shortcut is valid
644 for (int i = 0; i < cut.count(); i++) {
645 const QKeySequence& seq = cut[i];
646 //const KKey& key = seq.key(0);
647 #ifdef __GNUC__
648 #warning "kde 4 port it";
649 #endif
650 #if 0
651 if (key.modFlags() == 0 && key.sym() < 0x3000
652 && QChar(key.sym()).isLetterOrNumber())
654 QString s = i18n("In order to use the '%1' key as a shortcut, "
655 "it must be combined with the "
656 "Win, Alt, Ctrl, and/or Shift keys.", QChar(key.sym()));
657 KMessageBox::sorry( this, s, i18n("Invalid Shortcut Key") );
658 return;
660 #endif
662 PhraseTreeItem *currentItem = selectedItem (treeView);
663 // If key isn't already in use,
664 if (!treeView->isKeyPresent (cut, currentItem, true)) {
665 // Set new key code
666 currentItem->setCut (cut);
667 // Update display
668 buttonBox->noKey->setChecked (false);
669 buttonBox->customKey->setChecked (true);
670 buttonBox->keyButton->setKeySequence(currentItem->cut().primary());
674 Q3ListViewItem *PhraseBookDialog::addBook (Q3ListViewItem *parent, Q3ListViewItem *after, PhraseBook *book) {
675 Q3ListViewItem *newItem = treeView->addBook(parent, after, book);
676 if (newItem != 0) {
677 treeView->clearSelection();
678 treeView->ensureItemVisible(newItem);
679 treeView->setCurrentItem (newItem);
680 treeView->setSelected (newItem, true);
681 phrasebookChanged = true;
683 return newItem;
686 Q3ListViewItem *PhraseBookDialog::addBook (Q3ListViewItem *item, PhraseBook *book) {
687 if (item == 0)
688 return addBook(0, 0, book);
689 else if (((PhraseTreeItem *)item)->isPhrase() || !item->isOpen())
690 if (item->parent() != 0)
691 return addBook(item->parent(), item, book);
692 else
693 return addBook(0, item, book);
694 else
695 return addBook(item, 0, book);
698 void PhraseBookDialog::contextMenuRequested(Q3ListViewItem *, const QPoint &pos, int) {
699 QString name;
700 if (treeView->hasSelectedItems())
701 name = "phrasebook_popup_sel";
702 else
703 name = "phrasebook_popup_nosel";
705 QMenu *popup = (QMenu *)factory()->container(name,this);
706 if (popup != 0) {
707 popup->popup(pos, 0);
711 void PhraseBookDialog::slotRemove () {
712 if (treeView->hasSelectedItems() != 0)
713 treeView->deleteSelectedItems();
714 selectionChanged();
715 phrasebookChanged = true;
718 void PhraseBookDialog::slotCut () {
719 slotCopy();
720 slotRemove();
723 void PhraseBookDialog::slotCopy () {
724 PhraseBook book;
725 treeView->fillBook (&book, true);
726 #ifdef __GNUC__
727 #warning "kde4: port to QMimeData"
728 #endif
729 QApplication::clipboard()->setData(new PhraseBookDrag(&book));
732 void PhraseBookDialog::slotPaste () {
733 PhraseBook book;
734 if (PhraseBookDrag::decode(QApplication::clipboard()->data(), &book)) {
735 addBook (treeView->currentItem(), &book);
739 void PhraseBookDialog::slotDropped (QDropEvent *e, Q3ListViewItem *parent, Q3ListViewItem *after) {
740 PhraseBook book;
741 if (PhraseBookDrag::decode(e, &book)) {
742 addBook(parent, after, &book);
746 void PhraseBookDialog::slotMoved (Q3ListViewItem *item, Q3ListViewItem *, Q3ListViewItem *) {
747 treeView->ensureItemVisible(item);
748 treeView->setSelected (item, true);
749 phrasebookChanged = true;
752 void PhraseBookDialog::slotAddPhrasebook () {
753 PhraseBook book;
754 Phrase phrase(i18n("(New Phrase Book)"), "");
755 book += PhraseBookEntry(phrase, 0, false);
757 Q3ListViewItem *item = addBook (treeView->currentItem(), &book);
758 item->setOpen (true);
759 buttonBox->lineEdit->selectAll();
760 buttonBox->lineEdit->setFocus();
763 void PhraseBookDialog::slotAddPhrase () {
764 PhraseBook book;
765 Phrase phrase(i18n("(New Phrase)"), "");
766 book += PhraseBookEntry(phrase, 0, true);
768 addBook (treeView->currentItem(), &book);
769 buttonBox->lineEdit->selectAll();
770 buttonBox->lineEdit->setFocus();
773 void PhraseBookDialog::slotSave () {
774 book.clear();
775 treeView->fillBook (&book, false);
776 emit phrasebookConfirmed (book);
777 phrasebookChanged = false;
780 void PhraseBookDialog::slotImportPhrasebook () {
781 KUrl url=KFileDialog::getOpenUrl(KUrl(),
782 i18n("*.phrasebook|Phrase Books (*.phrasebook)\n*.txt|Plain Text Files (*.txt)\n*|All Files"), this, i18n("Import Phrasebook"));
784 slotImportPhrasebook (url);
787 void PhraseBookDialog::slotImportPhrasebook (const KUrl &url) {
788 if(!url.isEmpty()) {
789 PhraseBook book;
790 if (book.open (url))
791 addBook(treeView->currentItem(), &book);
792 else
793 KMessageBox::sorry(this,i18n("There was an error loading file\n%1", url.url() ));
797 void PhraseBookDialog::slotExportPhrasebook () {
798 PhraseBook book;
799 treeView->fillBook (&book, treeView->hasSelectedItems());
801 KUrl url;
802 if (book.save (this, i18n("Export Phrase Book"), url) == -1)
803 KMessageBox::sorry(this,i18n("There was an error saving file\n%1", url.url() ));
806 void PhraseBookDialog::slotPrint()
808 KPrinter printer;
809 if (printer.setup(this)) {
810 PhraseBook book;
811 treeView->fillBook (&book, treeView->hasSelectedItems());
813 book.print(&printer);
817 #include "phrasebookdialog.moc"