Remove this line
[kdeaccessibility.git] / kmouth / phrasebook / phrasebookdialog.cpp
blobb5866a677a8de9e4aa9a60c82fc93a79fe047bb3
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 "phrasebookdialog.h"
19 #include "phrasetree.h"
21 // include files for Qt
22 #include <QtGui/QApplication>
23 #include <QtGui/QLayout>
24 #include <QtGui/QClipboard>
25 #include <QtGui/QRadioButton>
26 #include <QtCore/QEvent>
27 #include <QtGui/QPainter>
28 #include <QtGui/QPrintDialog>
29 #include <QtGui/QStyle>
30 #include <Qt3Support/Q3GroupBox>
31 #include <QtGui/QMenu>
32 #include <Qt3Support/Q3ValueStack>
33 #include <Qt3Support/Q3PtrStack>
34 #include <QtGui/QGridLayout>
35 #include <QtGui/QDropEvent>
36 #include <QtGui/QLabel>
37 #include <QtGui/QVBoxLayout>
38 #include <Qt3Support/Q3ListViewItem>
40 // include files for KDE
41 #include <ktoolbarpopupaction.h>
42 #include <kmenu.h>
43 #include <kxmlguifactory.h>
44 #include <klocale.h>
45 #include <kaction.h>
46 #include <kapplication.h>
47 #include <k3listview.h>
48 #include <kiconloader.h>
49 #include <kguiitem.h>
50 #include <kmessagebox.h>
51 #include <kfiledialog.h>
52 #include <kstandarddirs.h>
53 #include <kdesktopfile.h>
54 #include <kactionmenu.h>
55 #include <kstandardaction.h>
56 #include <kdeprintdialog.h>
58 namespace PhraseBookPrivate {
59 enum columns {
60 name = 1,
61 filename = 2
65 CheckBookItem::CheckBookItem (Q3ListViewItem *parent, Q3ListViewItem *last,
66 const QString &text, const QString &name, const QString &filename)
67 : Q3CheckListItem (parent, text, Q3CheckListItem::CheckBox)
69 moveItem (last);
70 setText(PhraseBookPrivate::name, name);
71 setText(PhraseBookPrivate::filename, filename);
72 setSelectable(false);
74 if (filename.isNull() || filename.isEmpty())
75 numberOfBooks = 0;
76 else
77 numberOfBooks = 1;
78 selectedBooks = 0;
79 ((CheckBookItem*)parent)->childChange (numberOfBooks, selectedBooks);
82 CheckBookItem::CheckBookItem (Q3ListView *parent, Q3ListViewItem *last,
83 const QString &text, const QString &name, const QString &filename)
84 : Q3CheckListItem (parent, text, Q3CheckListItem::CheckBox)
86 moveItem (last);
87 setText(PhraseBookPrivate::name, name);
88 setText(PhraseBookPrivate::filename, filename);
89 setSelectable(false);
91 if (filename.isNull() || filename.isEmpty())
92 numberOfBooks = 0;
93 else
94 numberOfBooks = 1;
95 selectedBooks = 0;
98 CheckBookItem::~CheckBookItem () {
101 void CheckBookItem::activate() {
102 Q3ListView *lv = listView();
104 if ((lv != 0) && (!lv->isEnabled()) || (!isEnabled()))
105 return;
107 setOn (!isOn());
108 ignoreDoubleClick();
111 void CheckBookItem::stateChange (bool on) {
112 Q3ListViewItem *item = firstChild();
113 if (item == 0) {
114 Q3ListViewItem *parent = this->parent();
115 if (parent != 0) {
116 if (on)
117 ((CheckBookItem*)parent)->childChange (0, 1);
118 else
119 ((CheckBookItem*)parent)->childChange (0, -1);
122 else propagateStateChange();
125 void CheckBookItem::propagateStateChange () {
126 Q3ListViewItem *item = firstChild();
127 while (item != 0) {
128 if (isOn() != ((Q3CheckListItem*)item)->isOn())
129 ((Q3CheckListItem*)item)->setOn (isOn());
130 else
131 ((CheckBookItem*)item)->propagateStateChange ();
132 item = item->nextSibling();
136 void CheckBookItem::childChange (int numberDiff, int selDiff) {
137 numberOfBooks += numberDiff;
138 selectedBooks += selDiff;
139 Q3ListViewItem *parent = this->parent();
140 if (parent != 0)
141 ((CheckBookItem*)parent)->childChange (numberDiff, selDiff);
143 QString text = i18np(" (%2 of 1 book selected)",
144 " (%2 of %1 books selected)",
145 numberOfBooks, selectedBooks);
146 setText(0, this->text(PhraseBookPrivate::name)+text);
149 /***************************************************************************/
151 InitialPhraseBookWidget::InitialPhraseBookWidget (QWidget *parent, const char *name)
152 : QWidget(parent)
154 setObjectName(name);
155 QVBoxLayout *mainLayout = new QVBoxLayout (this);
156 mainLayout->setSpacing(KDialog::spacingHint());
157 QLabel *label = new QLabel (i18n("Please decide which phrase books you need:"), this);
158 label->setObjectName("booksTitle");
159 mainLayout->addWidget (label);
161 books = new K3ListView (this);
162 books->setSorting (-1);
163 books->setItemsMovable (false);
164 books->setDragEnabled (false);
165 books->setAcceptDrops (false);
166 books->addColumn (i18n("Book"));
167 books->setRootIsDecorated (true);
168 books->setAllColumnsShowFocus (true);
169 books->setSelectionMode (Q3ListView::Multi);
170 mainLayout->addWidget (books);
172 initStandardPhraseBooks();
175 InitialPhraseBookWidget::~InitialPhraseBookWidget () {
178 void InitialPhraseBookWidget::initStandardPhraseBooks() {
179 StandardBookList bookPaths = PhraseBookDialog::standardPhraseBooks();
181 Q3ListViewItem *parent = 0;
182 Q3ListViewItem *last = 0;
183 QStringList currentNamePath;
184 currentNamePath<<QString("");
185 Q3PtrStack<Q3ListViewItem> stack;
186 StandardBookList::iterator it;
187 for (it = bookPaths.begin(); it != bookPaths.end(); ++it) {
188 QString namePath = (*it).path;
189 QStringList dirs = namePath.split( '/');
191 QStringList::iterator it1=currentNamePath.begin();
192 QStringList::iterator it2=dirs.begin();
193 for (; (it1 != currentNamePath.end())
194 && (it1 != dirs.end()) && (*it1 == *it2); ++it1, ++it2);
195 for (; it1 != currentNamePath.end(); ++it1) {
196 last = parent;
197 parent = stack.pop();
199 for (; it2 != dirs.end(); ++it2) {
200 stack.push (parent);
201 Q3ListViewItem *newParent;
202 if (parent == 0)
203 newParent = new CheckBookItem (books, last, *it2, *it2, QString());
204 else
205 newParent = new CheckBookItem (parent, last, *it2, *it2, QString());
206 parent = newParent;
207 last = 0;
209 currentNamePath = dirs;
211 Q3ListViewItem *book;
212 if (parent == 0)
213 book = new CheckBookItem (books, last, (*it).name, (*it).name, (*it).filename);
214 else
215 book = new CheckBookItem (parent, last, (*it).name, (*it).name, (*it).filename);
216 last = book;
220 void InitialPhraseBookWidget::createBook () {
221 PhraseBook book;
222 Q3ListViewItem *item = books->firstChild();
223 while (item != 0) {
224 if (item->firstChild() != 0) {
225 item = item->firstChild();
227 else {
228 if (((Q3CheckListItem*)item)->isOn()) {
229 PhraseBook localBook;
230 localBook.open(KUrl( item->text(PhraseBookPrivate::filename )));
231 book += localBook;
234 while ((item != 0) && (item->nextSibling() == 0)) {
235 item = item->parent();
237 if (item != 0)
238 item = item->nextSibling();
242 QString bookLocation = KGlobal::dirs()->saveLocation ("appdata", "/");
243 if (!bookLocation.isNull() && !bookLocation.isEmpty()) {
244 book.save (KUrl( bookLocation + "standard.phrasebook" ));
248 /***************************************************************************/
250 ButtonBoxWidget::ButtonBoxWidget (QWidget *parent, const char *name)
251 : QWidget(parent)
253 setupUi(this);
254 setObjectName(name);
255 keyButtonPlaceLayout = new QGridLayout (keyButtonPlace);
256 keyButtonPlaceLayout->setObjectName("keyButtonPlaceLayout");
257 keyButtonPlaceLayout->setMargin(0);
258 keyButtonPlaceLayout->setSpacing(0);
260 keyButton = new KKeySequenceWidget (keyButtonPlace);
261 keyButtonPlaceLayout->addWidget (keyButton, 1,1);
262 keyButton->setWhatsThis( i18n("By clicking on this button you can select the keyboard shortcut associated with the selected phrase."));
264 group = new Q3ButtonGroup (phrasebox);
265 group->hide();
266 group->setExclusive (true);
267 group->insert (noKey);
268 group->insert (customKey);
271 ButtonBoxWidget::~ButtonBoxWidget () {
274 /***************************************************************************/
276 namespace PhraseBookPrivate {
277 PhraseBookDialog *instance = 0;
280 PhraseBookDialog::PhraseBookDialog ()
281 : KXmlGuiWindow (0)
283 setObjectName("phraseEditDialog");
284 setCaption (i18n("Phrase Book"));
285 initGUI();
286 initActions();
287 initStandardPhraseBooks();
288 QString standardBook = KGlobal::dirs()->findResource("appdata", "standard.phrasebook");
289 if (!standardBook.isNull() && !standardBook.isEmpty()) {
290 PhraseBook book;
291 book.open(KUrl( standardBook ));
292 treeView->clear();
293 treeView->addBook(0, 0, &book);
294 treeView->setCurrentItem(treeView->firstChild());
295 selectionChanged();
296 phrasebookChanged = false;
299 printer = 0;
300 // i18n("Edit Phrase Book")
303 PhraseBookDialog *PhraseBookDialog::get() {
304 if (PhraseBookPrivate::instance == 0)
305 PhraseBookPrivate::instance = new PhraseBookDialog();
306 return PhraseBookPrivate::instance;
309 PhraseBookDialog::~PhraseBookDialog() {
310 PhraseBookPrivate::instance = 0;
311 delete printer;
314 void PhraseBookDialog::initGUI () {
315 QWidget *page = new QWidget( this );
316 setCentralWidget(page);
317 QVBoxLayout *mainLayout = new QVBoxLayout (page);
318 mainLayout->setMargin(0);
320 treeView = new PhraseTree (page, "phrasetree");
321 treeView->setSorting (-1);
322 treeView->setItemsMovable (true);
323 treeView->setDragEnabled (true);
324 treeView->setAcceptDrops (true);
325 treeView->addColumn (i18n("Phrase"));
326 treeView->addColumn (i18n("Shortcut"));
327 treeView->setRootIsDecorated (true);
328 treeView->setAllColumnsShowFocus (true);
329 treeView->setSelectionMode (Q3ListView::Extended);
330 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"));
331 connect (treeView, SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
332 connect (treeView, SIGNAL(contextMenuRequested (Q3ListViewItem *, const QPoint &, int)), this, SLOT(contextMenuRequested (Q3ListViewItem *, const QPoint &, int)));
333 connect (treeView, SIGNAL(dropped (QDropEvent *, Q3ListViewItem *, Q3ListViewItem *)), this, SLOT(slotDropped (QDropEvent *, Q3ListViewItem *, Q3ListViewItem *)));
334 connect (treeView, SIGNAL(moved (Q3ListViewItem *, Q3ListViewItem *, Q3ListViewItem *)), this, SLOT(slotMoved (Q3ListViewItem *, Q3ListViewItem *, Q3ListViewItem *)));
335 mainLayout->addWidget (treeView);
337 buttonBox = new ButtonBoxWidget (page, "buttonbox");
338 connect (buttonBox->lineEdit, SIGNAL(textChanged(const QString &)), SLOT(slotTextChanged(const QString &)));
339 connect (buttonBox->noKey, SIGNAL(clicked()), SLOT(slotNoKey()));
340 connect (buttonBox->customKey, SIGNAL(clicked()), SLOT(slotCustomKey()));
341 connect (buttonBox->keyButton, SIGNAL(capturedShortcut(const KShortcut&)), SLOT(capturedShortcut(const KShortcut&)));
342 mainLayout->addWidget (buttonBox);
344 treeView->setFocus();
345 selectionChanged();
348 void PhraseBookDialog::initActions() {
349 // The file menu
350 fileNewPhrase = actionCollection()->addAction("file_new_phrase");
351 fileNewPhrase->setIcon(KIcon("phrase_new"));
352 fileNewPhrase->setText(i18n("&New Phrase"));
353 connect(fileNewPhrase, SIGNAL(triggered(bool)), this, SLOT(slotAddPhrase()));
354 fileNewPhrase->setToolTip(i18n("Adds a new phrase"));
355 fileNewPhrase->setWhatsThis (i18n("Adds a new phrase"));
357 fileNewBook = actionCollection()->addAction("file_new_book");
358 fileNewBook->setIcon(KIcon("phrasebook_new"));
359 fileNewBook->setText(i18n("New Phrase &Book"));
360 connect(fileNewBook, SIGNAL(triggered(bool)), this, SLOT(slotAddPhrasebook()));
361 fileNewBook->setToolTip(i18n("Adds a new phrase book into which other books and phrases can be placed"));
362 fileNewBook->setWhatsThis (i18n("Adds a new phrase book into which other books and phrases can be placed"));
364 fileSave = KStandardAction::save(this, SLOT(slotSave()), actionCollection());
365 fileSave->setToolTip(i18n("Saves the phrase book onto the hard disk"));
366 fileSave->setWhatsThis (i18n("Saves the phrase book onto the hard disk"));
368 fileImport = actionCollection()->addAction("file_import");
369 fileImport->setIcon(KIcon("phrasebook_open"));
370 fileImport->setText(i18n("&Import..."));
371 connect(fileImport, SIGNAL(triggered(bool)), this, SLOT(slotImportPhrasebook()));
372 fileImport->setToolTip(i18n("Imports a file and adds its contents to the phrase book"));
373 fileImport->setWhatsThis (i18n("Imports a file and adds its contents to the phrase book"));
375 toolbarImport = new KToolBarPopupAction(KIcon("phrasebook_open"), i18n("&Import..."), this);
376 actionCollection()->addAction("toolbar_import", toolbarImport);
377 connect(toolbarImport, SIGNAL(triggered(bool)), this, SLOT(slotImportPhrasebook()));
378 toolbarImport->setToolTip(i18n("Imports a file and adds its contents to the phrase book"));
379 toolbarImport->setWhatsThis (i18n("Imports a file and adds its contents to the phrase book"));
381 fileImportStandardBook = actionCollection()->add<KActionMenu>("file_import_standard_book");
382 fileImportStandardBook->setIcon(KIcon("phrasebook_open"));
383 fileImportStandardBook->setText(i18n("I&mport Standard Phrase Book"));
384 fileImportStandardBook->setToolTip(i18n("Imports a standard phrase book and adds its contents to the phrase book"));
385 fileImportStandardBook->setWhatsThis (i18n("Imports a standard phrase book and adds its contents to the phrase book"));
387 fileExport = actionCollection()->addAction("file_export");
388 fileExport->setIcon(KIcon("phrasebook_save"));
389 fileExport->setText(i18n("&Export..."));
390 connect(fileExport, SIGNAL(triggered(bool)), this, SLOT(slotExportPhrasebook()));
391 fileExport->setToolTip(i18n("Exports the currently selected phrase(s) or phrase book(s) into a file"));
392 fileExport->setWhatsThis (i18n("Exports the currently selected phrase(s) or phrase book(s) into a file"));
394 filePrint = KStandardAction::print(this, SLOT(slotPrint()), actionCollection());
395 filePrint->setToolTip(i18n("Prints the currently selected phrase(s) or phrase book(s)"));
396 filePrint->setWhatsThis (i18n("Prints the currently selected phrase(s) or phrase book(s)"));
398 fileClose = KStandardAction::close(this, SLOT(close()), actionCollection());
399 fileClose->setToolTip(i18n("Closes the window"));
400 fileClose->setWhatsThis (i18n("Closes the window"));
402 // The edit menu
403 editCut = KStandardAction::cut(this, SLOT(slotCut()), actionCollection());
404 editCut->setToolTip(i18n("Cuts the currently selected entries from the phrase book and puts it to the clipboard"));
405 editCut->setWhatsThis (i18n("Cuts the currently selected entries from the phrase book and puts it to the clipboard"));
407 editCopy = KStandardAction::copy(this, SLOT(slotCopy()), actionCollection());
408 editCopy->setToolTip(i18n("Copies the currently selected entry from the phrase book to the clipboard"));
409 editCopy->setWhatsThis (i18n("Copies the currently selected entry from the phrase book to the clipboard"));
411 editPaste = KStandardAction::paste(this, SLOT(slotPaste()), actionCollection());
412 editPaste->setToolTip(i18n("Pastes the clipboard contents to actual position"));
413 editPaste->setWhatsThis (i18n("Pastes the clipboard contents to actual position"));
415 editDelete = actionCollection()->addAction("edit_delete");
416 editDelete->setIcon(KIcon("edit-delete"));
417 editDelete->setText(i18n("&Delete"));
418 connect(editDelete, SIGNAL(triggered(bool)), this, SLOT(slotRemove()));
419 editDelete->setToolTip(i18n("Deletes the selected entries from the phrase book"));
420 editDelete->setWhatsThis (i18n("Deletes the selected entries from the phrase book"));
422 // use the absolute path to your kmouthui.rc file for testing purpose in createGUI();
423 createGUI("phrasebookdialogui.rc");
426 QString PhraseBookDialog::displayPath (QString filename) {
427 QFileInfo file(filename);
428 QString path = file.path();
429 QString dispPath = "";
430 int position = path.indexOf("/kmouth/books/")+QString("/kmouth/books/").length();
432 while (path.length() > position) {
433 file.setFile(path);
435 KDesktopFile *dirDesc = new KDesktopFile("data", path+"/.directory");
436 QString name = dirDesc->readName();
437 delete dirDesc;
439 if (name.isNull() || name.isEmpty())
440 dispPath += '/' + file.fileName ();
441 else
442 dispPath += '/' + name;
444 path = file.path();
446 return dispPath;
449 StandardBookList PhraseBookDialog::standardPhraseBooks() {
450 QStringList bookPaths = KGlobal::mainComponent().dirs()->findAllResources (
451 "data", "kmouth/books/*.phrasebook",
452 KStandardDirs::Recursive |
453 KStandardDirs::NoDuplicates);
454 QStringList bookNames;
455 QMap<QString,StandardBook> bookMap;
456 QStringList::iterator it;
457 for (it = bookPaths.begin(); it != bookPaths.end(); ++it) {
458 PhraseBook pbook;
459 if (pbook.open (KUrl( *it ))) {
460 StandardBook book;
461 book.name = (*pbook.begin()).getPhrase().getPhrase();
463 book.path = displayPath(*it);
464 book.filename = *it;
466 bookNames += book.path + '/' + book.name;
467 bookMap [book.path + '/' + book.name] = book;
471 bookNames.sort();
473 StandardBookList result;
474 for (it = bookNames.begin(); it != bookNames.end(); ++it)
475 result += bookMap [*it];
477 return result;
480 void PhraseBookDialog::initStandardPhraseBooks () {
481 StandardBookList bookPaths = standardPhraseBooks();
483 KActionMenu *parent = fileImportStandardBook;
484 QStringList currentNamePath;
485 currentNamePath<< "x";
486 Q3PtrStack<KActionMenu> stack;
487 StandardBookList::iterator it;
488 for (it = bookPaths.begin(); it != bookPaths.end(); ++it) {
489 KUrl url;
490 url.setPath((*it).filename);
492 QString namePath = "x/"+(*it).path;
493 QStringList dirs = namePath.split( '/');
495 QStringList::iterator it1=currentNamePath.begin();
496 QStringList::iterator it2=dirs.begin();
497 for (; (it1 != currentNamePath.end())
498 && (it1 != dirs.end()) && (*it1 == *it2); ++it1, ++it2);
499 for (; it1 != currentNamePath.end(); ++it1)
500 parent = stack.pop();
501 for (; it2 != dirs.end(); ++it2) {
502 stack.push (parent);
503 #ifdef __GNUC__
504 #warning "kde4: correct newparent objectname ?"
505 #endif
506 KActionMenu *newParent = actionCollection()->add<KActionMenu>("tmp_menu");
507 newParent->setText(*it2);
508 parent->addAction(newParent);
509 if (parent == fileImportStandardBook)
510 toolbarImport->popupMenu()->addAction(newParent);
511 parent = newParent;
513 currentNamePath = dirs;
515 KAction *book = new StandardPhraseBookInsertAction (
516 url, (*it).name, this, SLOT(slotImportPhrasebook (const KUrl &)), actionCollection());
517 parent->addAction(book);
518 if (parent == fileImportStandardBook)
519 toolbarImport->popupMenu()->addAction(book);
523 PhraseTreeItem *selectedItem (Q3ListView *treeView) {
524 PhraseTreeItem *currentItem = (PhraseTreeItem *)treeView->currentItem();
525 if ((currentItem == 0) || (!currentItem->isSelected()))
526 return 0;
528 Q3ListViewItemIterator it(treeView);
529 while (it.current()) {
530 Q3ListViewItem *item = it.current();
531 if (item->isSelected() && (item != currentItem))
532 return 0;
533 ++it;
535 return currentItem;
538 void PhraseBookDialog::selectionChanged () {
539 bool modified = phrasebookChanged;
540 PhraseTreeItem *currentItem = selectedItem (treeView);
541 if (currentItem == 0) {
542 buttonBox->textLabel->setText (i18n("Text of the &phrase:"));
543 buttonBox->textLabel->setEnabled(false);
544 buttonBox->group->setEnabled(false);
545 buttonBox->lineEdit->setText("");
546 buttonBox->lineEdit->setEnabled(false);
547 buttonBox->shortcutLabel->setEnabled(false);
548 //buttonBox->keyButton->setShortcut("", false);
549 buttonBox->keyButton->setEnabled(false);
550 buttonBox->noKey->setChecked (false);
551 buttonBox->noKey->setEnabled (false);
552 buttonBox->customKey->setChecked (false);
553 buttonBox->customKey->setEnabled (false);
555 else if (currentItem->isPhrase()) {
556 buttonBox->textLabel->setText (i18n("Text of the &phrase:"));
557 buttonBox->textLabel->setEnabled(true);
558 buttonBox->group->setEnabled(true);
559 buttonBox->lineEdit->setText(currentItem->text(0));
560 buttonBox->lineEdit->setEnabled(true);
561 buttonBox->shortcutLabel->setEnabled(true);
562 QString shortcut = currentItem->text(1);
563 #ifdef __GNUC__
564 #warning "kde4 port it"
565 #endif
566 //buttonBox->keyButton->setShortcut(currentItem->cut(), false);
567 if (shortcut.isEmpty() || shortcut.isNull()) {
568 buttonBox->noKey->setChecked (true);
569 buttonBox->customKey->setChecked (false);
571 else {
572 buttonBox->noKey->setChecked (false);
573 buttonBox->customKey->setChecked (true);
575 buttonBox->keyButton->setEnabled(true);
576 buttonBox->noKey->setEnabled(true);
577 buttonBox->customKey->setEnabled(true);
579 else {
580 buttonBox->textLabel->setText (i18n("Name of the &phrase book:"));
581 buttonBox->textLabel->setEnabled(true);
582 buttonBox->group->setEnabled(true);
583 buttonBox->lineEdit->setText(currentItem->text(0));
584 buttonBox->lineEdit->setEnabled(true);
585 buttonBox->shortcutLabel->setEnabled(false);
586 //buttonBox->keyButton->setShortcut("", false);
587 buttonBox->keyButton->setEnabled(false);
588 buttonBox->noKey->setChecked (false);
589 buttonBox->noKey->setEnabled (false);
590 buttonBox->customKey->setChecked (false);
591 buttonBox->customKey->setEnabled (false);
593 phrasebookChanged = modified;
596 bool PhraseBookDialog::queryClose() {
597 if (phrasebookChanged) {
598 int answer = KMessageBox::questionYesNoCancel (this,
599 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>"),
600 i18n("Closing \"Phrase Book\" Window"),
601 KStandardGuiItem::apply(), KStandardGuiItem::discard(),
602 KStandardGuiItem::cancel(), "AutomaticSave");
603 if (answer == KMessageBox::Yes) {
604 slotSave();
605 return true;
607 return (answer == KMessageBox::No);
609 return true;
612 void PhraseBookDialog::slotTextChanged (const QString &s) {
613 PhraseTreeItem *currentItem = selectedItem (treeView);
614 if (currentItem != 0)
615 currentItem->setText(0, s);
616 phrasebookChanged = true;
619 void PhraseBookDialog::slotNoKey() {
620 buttonBox->noKey->setChecked (true);
621 buttonBox->customKey->setChecked (false);
623 PhraseTreeItem *currentItem = selectedItem (treeView);
624 if (currentItem != 0) {
625 currentItem->setCut (KShortcut(QString()));
626 #ifdef __GNUC__
627 #warning "kde4: port it"
628 #endif
629 //buttonBox->keyButton->setShortcut(currentItem->cut(), false);
631 phrasebookChanged = true;
634 void PhraseBookDialog::slotCustomKey() {
635 buttonBox->keyButton->keySequence();
638 void PhraseBookDialog::capturedShortcut (const KShortcut& cut) {
639 if (cut.isEmpty()) {
640 slotNoKey();
642 else
643 setShortcut (cut);
644 phrasebookChanged = true;
647 void PhraseBookDialog::setShortcut( const KShortcut& cut ) {
648 // Check whether the shortcut is valid
649 const QList<QKeySequence> cutList = cut.toList();
650 for (int i = 0; i < cutList.count(); i++) {
651 const QKeySequence& seq = cutList[i];
652 //const KKey& key = seq.key(0);
653 #ifdef __GNUC__
654 #warning "kde 4 port it";
655 #endif
656 #if 0
657 if (key.modFlags() == 0 && key.sym() < 0x3000
658 && QChar(key.sym()).isLetterOrNumber())
660 QString s = i18n("In order to use the '%1' key as a shortcut, "
661 "it must be combined with the "
662 "Win, Alt, Ctrl, and/or Shift keys.", QChar(key.sym()));
663 KMessageBox::sorry( this, s, i18n("Invalid Shortcut Key") );
664 return;
666 #endif
668 PhraseTreeItem *currentItem = selectedItem (treeView);
669 // If key isn't already in use,
670 if (!treeView->isKeyPresent (cut, currentItem, true)) {
671 // Set new key code
672 currentItem->setCut (cut);
673 // Update display
674 buttonBox->noKey->setChecked (false);
675 buttonBox->customKey->setChecked (true);
676 buttonBox->keyButton->setKeySequence(currentItem->cut().primary());
680 Q3ListViewItem *PhraseBookDialog::addBook (Q3ListViewItem *parent, Q3ListViewItem *after, PhraseBook *book) {
681 Q3ListViewItem *newItem = treeView->addBook(parent, after, book);
682 if (newItem != 0) {
683 treeView->clearSelection();
684 treeView->ensureItemVisible(newItem);
685 treeView->setCurrentItem (newItem);
686 treeView->setSelected (newItem, true);
687 phrasebookChanged = true;
689 return newItem;
692 Q3ListViewItem *PhraseBookDialog::addBook (Q3ListViewItem *item, PhraseBook *book) {
693 if (item == 0)
694 return addBook(0, 0, book);
695 else if (((PhraseTreeItem *)item)->isPhrase() || !item->isOpen())
696 if (item->parent() != 0)
697 return addBook(item->parent(), item, book);
698 else
699 return addBook(0, item, book);
700 else
701 return addBook(item, 0, book);
704 void PhraseBookDialog::contextMenuRequested(Q3ListViewItem *, const QPoint &pos, int) {
705 QString name;
706 if (treeView->hasSelectedItems())
707 name = "phrasebook_popup_sel";
708 else
709 name = "phrasebook_popup_nosel";
711 QMenu *popup = (QMenu *)factory()->container(name,this);
712 if (popup != 0) {
713 popup->popup(pos, 0);
717 void PhraseBookDialog::slotRemove () {
718 if (treeView->hasSelectedItems() != 0)
719 treeView->deleteSelectedItems();
720 selectionChanged();
721 phrasebookChanged = true;
724 void PhraseBookDialog::slotCut () {
725 slotCopy();
726 slotRemove();
729 void PhraseBookDialog::slotCopy () {
730 PhraseBook book;
731 treeView->fillBook (&book, true);
732 #ifdef __GNUC__
733 #warning "kde4: port to QMimeData"
734 #endif
735 QApplication::clipboard()->setData(new PhraseBookDrag(&book));
738 void PhraseBookDialog::slotPaste () {
739 PhraseBook book;
740 if (PhraseBookDrag::decode(QApplication::clipboard()->data(), &book)) {
741 addBook (treeView->currentItem(), &book);
745 void PhraseBookDialog::slotDropped (QDropEvent *e, Q3ListViewItem *parent, Q3ListViewItem *after) {
746 PhraseBook book;
747 if (PhraseBookDrag::decode(e, &book)) {
748 addBook(parent, after, &book);
752 void PhraseBookDialog::slotMoved (Q3ListViewItem *item, Q3ListViewItem *, Q3ListViewItem *) {
753 treeView->ensureItemVisible(item);
754 treeView->setSelected (item, true);
755 phrasebookChanged = true;
758 void PhraseBookDialog::slotAddPhrasebook () {
759 PhraseBook book;
760 Phrase phrase(i18n("(New Phrase Book)"), "");
761 book += PhraseBookEntry(phrase, 0, false);
763 Q3ListViewItem *item = addBook (treeView->currentItem(), &book);
764 item->setOpen (true);
765 buttonBox->lineEdit->selectAll();
766 buttonBox->lineEdit->setFocus();
769 void PhraseBookDialog::slotAddPhrase () {
770 PhraseBook book;
771 Phrase phrase(i18n("(New Phrase)"), "");
772 book += PhraseBookEntry(phrase, 0, true);
774 addBook (treeView->currentItem(), &book);
775 buttonBox->lineEdit->selectAll();
776 buttonBox->lineEdit->setFocus();
779 void PhraseBookDialog::slotSave () {
780 book.clear();
781 treeView->fillBook (&book, false);
782 emit phrasebookConfirmed (book);
783 phrasebookChanged = false;
786 void PhraseBookDialog::slotImportPhrasebook () {
787 KUrl url=KFileDialog::getOpenUrl(KUrl(),
788 i18n("*.phrasebook|Phrase Books (*.phrasebook)\n*.txt|Plain Text Files (*.txt)\n*|All Files"), this, i18n("Import Phrasebook"));
790 slotImportPhrasebook (url);
793 void PhraseBookDialog::slotImportPhrasebook (const KUrl &url) {
794 if(!url.isEmpty()) {
795 PhraseBook book;
796 if (book.open (url))
797 addBook(treeView->currentItem(), &book);
798 else
799 KMessageBox::sorry(this,i18n("There was an error loading file\n%1", url.url() ));
803 void PhraseBookDialog::slotExportPhrasebook () {
804 PhraseBook book;
805 treeView->fillBook (&book, treeView->hasSelectedItems());
807 KUrl url;
808 if (book.save (this, i18n("Export Phrase Book"), url) == -1)
809 KMessageBox::sorry(this,i18n("There was an error saving file\n%1", url.url() ));
812 void PhraseBookDialog::slotPrint()
814 if (printer == 0) {
815 printer = new QPrinter();
818 QPrintDialog *printDialog = KdePrint::createPrintDialog(printer, this);
820 if (printDialog->exec()) {
821 PhraseBook book;
822 treeView->fillBook (&book, treeView->hasSelectedItems());
824 book.print(printer);
826 delete printDialog;
829 #include "phrasebookdialog.moc"