1 /***************************************************************************
2 phrasebook.h - 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 ***************************************************************************/
22 #include <q3dragobject.h>
25 #include <QTextStream>
29 #include <kactioncollection.h>
35 * The class Phrase represents one phrase in a phrase book.
36 * @author Gunnar Schmi Dt
39 friend class PhraseBookParser
;
42 Phrase (const QString
&phrase
);
43 Phrase (const QString
&phrase
, const QString
&shortcut
);
45 QString
getPhrase() const;
46 QString
getShortcut() const;
48 void setPhrase (const QString
&phrase
);
49 void setShortcut (const QString
&shortcut
);
57 * The class PhraseBookEntry implements a phrase book entry. That can be either
58 * a phrase or a start tag a sub phrase book.
59 * @author Gunnar Schmi Dt
61 class PhraseBookEntry
{
64 PhraseBookEntry (Phrase phrase
, int level
= 1, bool isPhrase
= true);
65 ~PhraseBookEntry () {}
67 void setPhrase (Phrase phrase
, int level
= 1, bool isPhrase
= true);
69 bool isPhrase() const;
70 Phrase
getPhrase() const;
79 typedef QList
<PhraseBookEntry
> PhraseBookEntryList
;
82 * The class PhraseBook implements a phrase book. It mainly stores a
83 * token list where each token is a phrase book entry (either a phrase
84 * or a sub phrase book). The entries are placed into a tree structure
87 * The level of each entry tells the level in the tree (level=0 is the top
88 * level), each sub book in level i directly or indirectly contains all
89 * following entries until an entry of level at most i or the end of the
92 * @author Gunnar Schmi Dt
94 class PhraseBook
: public PhraseBookEntryList
{
96 PhraseBook() : PhraseBookEntryList() {}
99 /** opens a file containing a phrase book. Returns true if successful. */
100 bool open (const KUrl
&url
);
102 /** decodes a phrase book. Returns true if successful. */
103 bool decode (const QString
&xml
);
105 /** decodes a phrase book. Returns true if successful. */
106 bool decode (QXmlInputSource
&source
);
108 /** Writes the phrases to a file. Returns true if successful. */
109 bool save (const KUrl
&url
);
111 /** Writes the phrases to a file. Returns true if successful. */
112 bool save (const KUrl
&url
, bool asPhrasebook
);
114 /** Writes the phrases to a QTextStream. */
115 void save (QTextStream
&stream
, bool asPhrasebook
);
117 /** Prints the phrases. */
118 void print (KPrinter
*pPrinter
);
120 /** Shows a file selector and writes the phrases to a file.
121 * @return 1, if the file got successfully written,
122 * 0, if the user canceled the operation,
123 * -1, if there was an error when saving the file.
125 int save (QWidget
*parent
, const QString
&title
, KUrl
&url
, bool phrasebookFirst
= true);
127 /** encodes the phrase book. Returns the encoded xml code. */
130 /** Stores all entries in a QStringList. All hierarchy information and all
131 * shortcuts are ignored during this operation.
133 QStringList
toStringList();
135 /** Adds the entries of the book to both the given popup menu and the given
136 * toolbar. The corresponding actions will be inserted into phrases.
138 void addToGUI (QMenu
*popup
, KToolBar
*toolbar
,
139 KActionCollection
*phrases
,
140 QObject
*receiver
, const char *slot
) const;
142 /** Inserts book into a new sub phrase book.
143 * @param name the name of the new sub phrase book.
144 * @param book the phrase book to insert.
146 void insert (const QString
&name
, const PhraseBook
&book
);
150 * The class PhraseBookDrag implements drag and drop support for phrase books.
151 * @author Gunnar Schmi Dt
153 class PhraseBookDrag
: public Q3DragObject
{
156 PhraseBookDrag (PhraseBook
*book
, QWidget
*dragSource
= 0, const char *name
= 0);
157 PhraseBookDrag (QWidget
*dragSource
= 0, const char *name
= 0);
160 virtual void setBook (PhraseBook
*book
);
162 const char *format (int i
) const;
163 virtual QByteArray
encodedData (const char *) const;
165 static bool canDecode (const QMimeSource
*e
);
166 static bool decode (const QMimeSource
*e
, PhraseBook
*book
);
170 Q3TextDrag xmlphrasebook
;
175 class PhraseAction
: public KAction
{
178 PhraseAction (const QString
& phrase
, const QString
& cut
, const QObject
* receiver
, const char* slot
, KActionCollection
* parent
)
179 : KAction (KIcon("phrase"), phrase
, parent
) {
180 this->setShortcut(cut
);
181 this->phrase
= phrase
;
182 connect (this, SIGNAL(slotActivated (const QString
&)), receiver
, slot
);
183 parent
->addAction(phrase
, this);
189 void slotTriggered () {
191 emit
slotActivated (phrase
);
195 void slotActivated (const QString
&phrase
);