1 /***************************************************************************
2 phrasebookdialog.cpp - description
4 begin : Don Sep 19 2002
5 copyright : (C) 2002 by Gunnar Schmi Dt
6 email : kmouth@schmi-dt.de
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
18 #include "phrasebookdialog.h"
19 #include "phrasetree.h"
21 // include files for Qt
22 #include <QApplication>
25 #include <QRadioButton>
29 #include <q3groupbox.h>
31 #include <q3valuestack.h>
32 #include <q3ptrstack.h>
33 #include <ktoolbarpopupaction.h>
35 #include <QGridLayout>
38 #include <QVBoxLayout>
39 #include <Q3ListViewItem>
40 // include files for KDE
42 #include <kxmlguifactory.h>
45 #include <kapplication.h>
46 #include <k3listview.h>
47 #include <kiconloader.h>
49 #include <kmessagebox.h>
50 #include <kfiledialog.h>
51 #include <kstandarddirs.h>
52 #include <kdesktopfile.h>
53 #include <kactionmenu.h>
54 #include <kstandardaction.h>
56 namespace PhraseBookPrivate
{
63 CheckBookItem::CheckBookItem (Q3ListViewItem
*parent
, Q3ListViewItem
*last
,
64 const QString
&text
, const QString
&name
, const QString
&filename
)
65 : Q3CheckListItem (parent
, text
, Q3CheckListItem::CheckBox
)
68 setText(PhraseBookPrivate::name
, name
);
69 setText(PhraseBookPrivate::filename
, filename
);
72 if (filename
.isNull() || filename
.isEmpty())
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
)
85 setText(PhraseBookPrivate::name
, name
);
86 setText(PhraseBookPrivate::filename
, filename
);
89 if (filename
.isNull() || filename
.isEmpty())
96 CheckBookItem::~CheckBookItem () {
99 void CheckBookItem::activate() {
100 Q3ListView
*lv
= listView();
102 if ((lv
!= 0) && (!lv
->isEnabled()) || (!isEnabled()))
109 void CheckBookItem::stateChange (bool on
) {
110 Q3ListViewItem
*item
= firstChild();
112 Q3ListViewItem
*parent
= this->parent();
115 ((CheckBookItem
*)parent
)->childChange (0, 1);
117 ((CheckBookItem
*)parent
)->childChange (0, -1);
120 else propagateStateChange();
123 void CheckBookItem::propagateStateChange () {
124 Q3ListViewItem
*item
= firstChild();
126 if (isOn() != ((Q3CheckListItem
*)item
)->isOn())
127 ((Q3CheckListItem
*)item
)->setOn (isOn());
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();
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
)
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
;
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
) {
195 parent
= stack
.pop();
197 for (; it2
!= dirs
.end(); ++it2
) {
199 Q3ListViewItem
*newParent
;
201 newParent
= new CheckBookItem (books
, last
, *it2
, *it2
, QString());
203 newParent
= new CheckBookItem (parent
, last
, *it2
, *it2
, QString());
207 currentNamePath
= dirs
;
209 Q3ListViewItem
*book
;
211 book
= new CheckBookItem (books
, last
, (*it
).name
, (*it
).name
, (*it
).filename
);
213 book
= new CheckBookItem (parent
, last
, (*it
).name
, (*it
).name
, (*it
).filename
);
218 void InitialPhraseBookWidget::createBook () {
220 Q3ListViewItem
*item
= books
->firstChild();
222 if (item
->firstChild() != 0) {
223 item
= item
->firstChild();
226 if (((Q3CheckListItem
*)item
)->isOn()) {
227 PhraseBook localBook
;
228 localBook
.open(KUrl( item
->text(PhraseBookPrivate::filename
)));
232 while ((item
!= 0) && (item
->nextSibling() == 0)) {
233 item
= item
->parent();
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
)
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
);
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 ()
281 setObjectName("phraseEditDialog");
282 setCaption (i18n("Phrase Book"));
285 initStandardPhraseBooks();
286 QString standardBook
= KGlobal::dirs()->findResource("appdata", "standard.phrasebook");
287 if (!standardBook
.isNull() && !standardBook
.isEmpty()) {
289 book
.open(KUrl( standardBook
));
291 treeView
->addBook(0, 0, &book
);
292 treeView
->setCurrentItem(treeView
->firstChild());
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();
343 void PhraseBookDialog::initActions() {
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"));
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
) {
430 KDesktopFile
*dirDesc
= new KDesktopFile("data", path
+"/.directory");
431 QString name
= dirDesc
->readName();
434 if (name
.isNull() || name
.isEmpty())
435 dispPath
+= '/' + file
.fileName ();
437 dispPath
+= '/' + name
;
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
) {
454 if (pbook
.open (KUrl( *it
))) {
456 book
.name
= (*pbook
.begin()).getPhrase().getPhrase();
458 book
.path
= displayPath(*it
);
461 bookNames
+= book
.path
+ '/' + book
.name
;
462 bookMap
[book
.path
+ '/' + book
.name
] = book
;
468 StandardBookList result
;
469 for (it
= bookNames
.begin(); it
!= bookNames
.end(); ++it
)
470 result
+= bookMap
[*it
];
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
) {
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
) {
499 #warning "kde4: correct newparent objectname ?"
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
);
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()))
523 Q3ListViewItemIterator
it(treeView
);
524 while (it
.current()) {
525 Q3ListViewItem
*item
= it
.current();
526 if (item
->isSelected() && (item
!= 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);
559 #warning "kde4 port it"
561 //buttonBox->keyButton->setShortcut(currentItem->cut(), false);
562 if (shortcut
.isEmpty() || shortcut
.isNull()) {
563 buttonBox
->noKey
->setChecked (true);
564 buttonBox
->customKey
->setChecked (false);
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);
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
) {
602 return (answer
== KMessageBox::No
);
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()));
622 #warning "kde4: port it"
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
) {
639 phrasebookChanged
= true;
642 void PhraseBookDialog::setShortcut( const KShortcut
& cut
) {
643 // Check whether the shortcut is valid
644 const QList
<QKeySequence
> cutList
= cut
.toList();
645 for (int i
= 0; i
< cutList
.count(); i
++) {
646 const QKeySequence
& seq
= cutList
[i
];
647 //const KKey& key = seq.key(0);
649 #warning "kde 4 port it";
652 if (key
.modFlags() == 0 && key
.sym() < 0x3000
653 && QChar(key
.sym()).isLetterOrNumber())
655 QString s
= i18n("In order to use the '%1' key as a shortcut, "
656 "it must be combined with the "
657 "Win, Alt, Ctrl, and/or Shift keys.", QChar(key
.sym()));
658 KMessageBox::sorry( this, s
, i18n("Invalid Shortcut Key") );
663 PhraseTreeItem
*currentItem
= selectedItem (treeView
);
664 // If key isn't already in use,
665 if (!treeView
->isKeyPresent (cut
, currentItem
, true)) {
667 currentItem
->setCut (cut
);
669 buttonBox
->noKey
->setChecked (false);
670 buttonBox
->customKey
->setChecked (true);
671 buttonBox
->keyButton
->setKeySequence(currentItem
->cut().primary());
675 Q3ListViewItem
*PhraseBookDialog::addBook (Q3ListViewItem
*parent
, Q3ListViewItem
*after
, PhraseBook
*book
) {
676 Q3ListViewItem
*newItem
= treeView
->addBook(parent
, after
, book
);
678 treeView
->clearSelection();
679 treeView
->ensureItemVisible(newItem
);
680 treeView
->setCurrentItem (newItem
);
681 treeView
->setSelected (newItem
, true);
682 phrasebookChanged
= true;
687 Q3ListViewItem
*PhraseBookDialog::addBook (Q3ListViewItem
*item
, PhraseBook
*book
) {
689 return addBook(0, 0, book
);
690 else if (((PhraseTreeItem
*)item
)->isPhrase() || !item
->isOpen())
691 if (item
->parent() != 0)
692 return addBook(item
->parent(), item
, book
);
694 return addBook(0, item
, book
);
696 return addBook(item
, 0, book
);
699 void PhraseBookDialog::contextMenuRequested(Q3ListViewItem
*, const QPoint
&pos
, int) {
701 if (treeView
->hasSelectedItems())
702 name
= "phrasebook_popup_sel";
704 name
= "phrasebook_popup_nosel";
706 QMenu
*popup
= (QMenu
*)factory()->container(name
,this);
708 popup
->popup(pos
, 0);
712 void PhraseBookDialog::slotRemove () {
713 if (treeView
->hasSelectedItems() != 0)
714 treeView
->deleteSelectedItems();
716 phrasebookChanged
= true;
719 void PhraseBookDialog::slotCut () {
724 void PhraseBookDialog::slotCopy () {
726 treeView
->fillBook (&book
, true);
728 #warning "kde4: port to QMimeData"
730 QApplication::clipboard()->setData(new PhraseBookDrag(&book
));
733 void PhraseBookDialog::slotPaste () {
735 if (PhraseBookDrag::decode(QApplication::clipboard()->data(), &book
)) {
736 addBook (treeView
->currentItem(), &book
);
740 void PhraseBookDialog::slotDropped (QDropEvent
*e
, Q3ListViewItem
*parent
, Q3ListViewItem
*after
) {
742 if (PhraseBookDrag::decode(e
, &book
)) {
743 addBook(parent
, after
, &book
);
747 void PhraseBookDialog::slotMoved (Q3ListViewItem
*item
, Q3ListViewItem
*, Q3ListViewItem
*) {
748 treeView
->ensureItemVisible(item
);
749 treeView
->setSelected (item
, true);
750 phrasebookChanged
= true;
753 void PhraseBookDialog::slotAddPhrasebook () {
755 Phrase
phrase(i18n("(New Phrase Book)"), "");
756 book
+= PhraseBookEntry(phrase
, 0, false);
758 Q3ListViewItem
*item
= addBook (treeView
->currentItem(), &book
);
759 item
->setOpen (true);
760 buttonBox
->lineEdit
->selectAll();
761 buttonBox
->lineEdit
->setFocus();
764 void PhraseBookDialog::slotAddPhrase () {
766 Phrase
phrase(i18n("(New Phrase)"), "");
767 book
+= PhraseBookEntry(phrase
, 0, true);
769 addBook (treeView
->currentItem(), &book
);
770 buttonBox
->lineEdit
->selectAll();
771 buttonBox
->lineEdit
->setFocus();
774 void PhraseBookDialog::slotSave () {
776 treeView
->fillBook (&book
, false);
777 emit
phrasebookConfirmed (book
);
778 phrasebookChanged
= false;
781 void PhraseBookDialog::slotImportPhrasebook () {
782 KUrl url
=KFileDialog::getOpenUrl(KUrl(),
783 i18n("*.phrasebook|Phrase Books (*.phrasebook)\n*.txt|Plain Text Files (*.txt)\n*|All Files"), this, i18n("Import Phrasebook"));
785 slotImportPhrasebook (url
);
788 void PhraseBookDialog::slotImportPhrasebook (const KUrl
&url
) {
792 addBook(treeView
->currentItem(), &book
);
794 KMessageBox::sorry(this,i18n("There was an error loading file\n%1", url
.url() ));
798 void PhraseBookDialog::slotExportPhrasebook () {
800 treeView
->fillBook (&book
, treeView
->hasSelectedItems());
803 if (book
.save (this, i18n("Export Phrase Book"), url
) == -1)
804 KMessageBox::sorry(this,i18n("There was an error saving file\n%1", url
.url() ));
807 void PhraseBookDialog::slotPrint()
810 if (printer
.setup(this)) {
812 treeView
->fillBook (&book
, treeView
->hasSelectedItems());
814 book
.print(&printer
);
818 #include "phrasebookdialog.moc"