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 <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>
43 #include <kxmlguifactory.h>
46 #include <kapplication.h>
47 #include <k3listview.h>
48 #include <kiconloader.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
{
65 CheckBookItem::CheckBookItem (Q3ListViewItem
*parent
, Q3ListViewItem
*last
,
66 const QString
&text
, const QString
&name
, const QString
&filename
)
67 : Q3CheckListItem (parent
, text
, Q3CheckListItem::CheckBox
)
70 setText(PhraseBookPrivate::name
, name
);
71 setText(PhraseBookPrivate::filename
, filename
);
74 if (filename
.isNull() || filename
.isEmpty())
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
)
87 setText(PhraseBookPrivate::name
, name
);
88 setText(PhraseBookPrivate::filename
, filename
);
91 if (filename
.isNull() || filename
.isEmpty())
98 CheckBookItem::~CheckBookItem () {
101 void CheckBookItem::activate() {
102 Q3ListView
*lv
= listView();
104 if ((lv
!= 0) && (!lv
->isEnabled()) || (!isEnabled()))
111 void CheckBookItem::stateChange (bool on
) {
112 Q3ListViewItem
*item
= firstChild();
114 Q3ListViewItem
*parent
= this->parent();
117 ((CheckBookItem
*)parent
)->childChange (0, 1);
119 ((CheckBookItem
*)parent
)->childChange (0, -1);
122 else propagateStateChange();
125 void CheckBookItem::propagateStateChange () {
126 Q3ListViewItem
*item
= firstChild();
128 if (isOn() != ((Q3CheckListItem
*)item
)->isOn())
129 ((Q3CheckListItem
*)item
)->setOn (isOn());
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();
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
)
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
) {
197 parent
= stack
.pop();
199 for (; it2
!= dirs
.end(); ++it2
) {
201 Q3ListViewItem
*newParent
;
203 newParent
= new CheckBookItem (books
, last
, *it2
, *it2
, QString());
205 newParent
= new CheckBookItem (parent
, last
, *it2
, *it2
, QString());
209 currentNamePath
= dirs
;
211 Q3ListViewItem
*book
;
213 book
= new CheckBookItem (books
, last
, (*it
).name
, (*it
).name
, (*it
).filename
);
215 book
= new CheckBookItem (parent
, last
, (*it
).name
, (*it
).name
, (*it
).filename
);
220 void InitialPhraseBookWidget::createBook () {
222 Q3ListViewItem
*item
= books
->firstChild();
224 if (item
->firstChild() != 0) {
225 item
= item
->firstChild();
228 if (((Q3CheckListItem
*)item
)->isOn()) {
229 PhraseBook localBook
;
230 localBook
.open(KUrl( item
->text(PhraseBookPrivate::filename
)));
234 while ((item
!= 0) && (item
->nextSibling() == 0)) {
235 item
= item
->parent();
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
)
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
);
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 ()
283 setObjectName("phraseEditDialog");
284 setCaption (i18n("Phrase Book"));
287 initStandardPhraseBooks();
288 QString standardBook
= KGlobal::dirs()->findResource("appdata", "standard.phrasebook");
289 if (!standardBook
.isNull() && !standardBook
.isEmpty()) {
291 book
.open(KUrl( standardBook
));
293 treeView
->addBook(0, 0, &book
);
294 treeView
->setCurrentItem(treeView
->firstChild());
296 phrasebookChanged
= false;
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;
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();
348 void PhraseBookDialog::initActions() {
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"));
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
) {
435 KDesktopFile
*dirDesc
= new KDesktopFile("data", path
+"/.directory");
436 QString name
= dirDesc
->readName();
439 if (name
.isNull() || name
.isEmpty())
440 dispPath
+= '/' + file
.fileName ();
442 dispPath
+= '/' + name
;
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
) {
459 if (pbook
.open (KUrl( *it
))) {
461 book
.name
= (*pbook
.begin()).getPhrase().getPhrase();
463 book
.path
= displayPath(*it
);
466 bookNames
+= book
.path
+ '/' + book
.name
;
467 bookMap
[book
.path
+ '/' + book
.name
] = book
;
473 StandardBookList result
;
474 for (it
= bookNames
.begin(); it
!= bookNames
.end(); ++it
)
475 result
+= bookMap
[*it
];
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
) {
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
) {
504 #warning "kde4: correct newparent objectname ?"
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
);
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()))
528 Q3ListViewItemIterator
it(treeView
);
529 while (it
.current()) {
530 Q3ListViewItem
*item
= it
.current();
531 if (item
->isSelected() && (item
!= 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);
564 #warning "kde4 port it"
566 //buttonBox->keyButton->setShortcut(currentItem->cut(), false);
567 if (shortcut
.isEmpty() || shortcut
.isNull()) {
568 buttonBox
->noKey
->setChecked (true);
569 buttonBox
->customKey
->setChecked (false);
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);
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
) {
607 return (answer
== KMessageBox::No
);
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()));
627 #warning "kde4: port it"
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
) {
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);
654 #warning "kde 4 port it";
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") );
668 PhraseTreeItem
*currentItem
= selectedItem (treeView
);
669 // If key isn't already in use,
670 if (!treeView
->isKeyPresent (cut
, currentItem
, true)) {
672 currentItem
->setCut (cut
);
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
);
683 treeView
->clearSelection();
684 treeView
->ensureItemVisible(newItem
);
685 treeView
->setCurrentItem (newItem
);
686 treeView
->setSelected (newItem
, true);
687 phrasebookChanged
= true;
692 Q3ListViewItem
*PhraseBookDialog::addBook (Q3ListViewItem
*item
, PhraseBook
*book
) {
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
);
699 return addBook(0, item
, book
);
701 return addBook(item
, 0, book
);
704 void PhraseBookDialog::contextMenuRequested(Q3ListViewItem
*, const QPoint
&pos
, int) {
706 if (treeView
->hasSelectedItems())
707 name
= "phrasebook_popup_sel";
709 name
= "phrasebook_popup_nosel";
711 QMenu
*popup
= (QMenu
*)factory()->container(name
,this);
713 popup
->popup(pos
, 0);
717 void PhraseBookDialog::slotRemove () {
718 if (treeView
->hasSelectedItems() != 0)
719 treeView
->deleteSelectedItems();
721 phrasebookChanged
= true;
724 void PhraseBookDialog::slotCut () {
729 void PhraseBookDialog::slotCopy () {
731 treeView
->fillBook (&book
, true);
733 #warning "kde4: port to QMimeData"
735 QApplication::clipboard()->setData(new PhraseBookDrag(&book
));
738 void PhraseBookDialog::slotPaste () {
740 if (PhraseBookDrag::decode(QApplication::clipboard()->data(), &book
)) {
741 addBook (treeView
->currentItem(), &book
);
745 void PhraseBookDialog::slotDropped (QDropEvent
*e
, Q3ListViewItem
*parent
, Q3ListViewItem
*after
) {
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 () {
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 () {
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 () {
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
) {
797 addBook(treeView
->currentItem(), &book
);
799 KMessageBox::sorry(this,i18n("There was an error loading file\n%1", url
.url() ));
803 void PhraseBookDialog::slotExportPhrasebook () {
805 treeView
->fillBook (&book
, treeView
->hasSelectedItems());
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()
815 printer
= new QPrinter();
818 QPrintDialog
*printDialog
= KdePrint::createPrintDialog(printer
, this);
820 if (printDialog
->exec()) {
822 treeView
->fillBook (&book
, treeView
->hasSelectedItems());
829 #include "phrasebookdialog.moc"