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>
34 * The class Phrase represents one phrase in a phrase book.
35 * @author Gunnar Schmi Dt
38 friend class PhraseBookParser
;
41 Phrase (const QString
&phrase
);
42 Phrase (const QString
&phrase
, const QString
&shortcut
);
44 QString
getPhrase() const;
45 QString
getShortcut() const;
47 void setPhrase (const QString
&phrase
);
48 void setShortcut (const QString
&shortcut
);
56 * The class PhraseBookEntry implements a phrase book entry. That can be either
57 * a phrase or a start tag a sub phrase book.
58 * @author Gunnar Schmi Dt
60 class PhraseBookEntry
{
63 PhraseBookEntry (Phrase phrase
, int level
= 1, bool isPhrase
= true);
64 ~PhraseBookEntry () {};
66 void setPhrase (Phrase phrase
, int level
= 1, bool isPhrase
= true);
68 bool isPhrase() const;
69 Phrase
getPhrase() const;
78 typedef QList
<PhraseBookEntry
> PhraseBookEntryList
;
81 * The class PhraseBook implements a phrase book. It mainly stores a
82 * token list where each token is a phrase book entry (either a phrase
83 * or a sub phrase book). The entries are placed into a tree structure
86 * The level of each entry tells the level in the tree (level=0 is the top
87 * level), each sub book in level i directly or indirectly contains all
88 * following entries until an entry of level at most i or the end of the
91 * @author Gunnar Schmi Dt
93 class PhraseBook
: public PhraseBookEntryList
{
95 PhraseBook() : PhraseBookEntryList() {};
98 /** opens a file containing a phrase book. Returns true if successful. */
99 bool open (const KUrl
&url
);
101 /** decodes a phrase book. Returns true if successful. */
102 bool decode (const QString
&xml
);
104 /** decodes a phrase book. Returns true if successful. */
105 bool decode (QXmlInputSource
&source
);
107 /** Writes the phrases to a file. Returns true if successful. */
108 bool save (const KUrl
&url
);
110 /** Writes the phrases to a file. Returns true if successful. */
111 bool save (const KUrl
&url
, bool asPhrasebook
);
113 /** Writes the phrases to a QTextStream. */
114 void save (QTextStream
&stream
, bool asPhrasebook
);
116 /** Prints the phrases. */
117 void print (KPrinter
*pPrinter
);
119 /** Shows a file selector and writes the phrases to a file.
120 * @return 1, if the file got successfully written,
121 * 0, if the user canceled the operation,
122 * -1, if there was an error when saving the file.
124 int save (QWidget
*parent
, const QString
&title
, KUrl
&url
, bool phrasebookFirst
= true);
126 /** encodes the phrase book. Returns the encoded xml code. */
129 /** Stores all entries in a QStringList. All hierarchy information and all
130 * shortcuts are ignored during this operation.
132 QStringList
toStringList();
134 /** Adds the entries of the book to both the given popup menu and the given
135 * toolbar. The corresponding actions will be inserted into phrases.
137 void addToGUI (QMenu
*popup
, KToolBar
*toolbar
,
138 KActionCollection
*phrases
,
139 QObject
*receiver
, const char *slot
) const;
141 /** Inserts book into a new sub phrase book.
142 * @param name the name of the new sub phrase book.
143 * @param book the phrase book to insert.
145 void insert (const QString
&name
, const PhraseBook
&book
);
149 * The class PhraseBookDrag implements drag and drop support for phrase books.
150 * @author Gunnar Schmi Dt
152 class PhraseBookDrag
: public Q3DragObject
{
155 PhraseBookDrag (PhraseBook
*book
, QWidget
*dragSource
= 0, const char *name
= 0);
156 PhraseBookDrag (QWidget
*dragSource
= 0, const char *name
= 0);
159 virtual void setBook (PhraseBook
*book
);
161 const char *format (int i
) const;
162 virtual QByteArray
encodedData (const char *) const;
164 static bool canDecode (const QMimeSource
*e
);
165 static bool decode (const QMimeSource
*e
, PhraseBook
*book
);
169 Q3TextDrag xmlphrasebook
;
174 class PhraseAction
: public KAction
{
177 PhraseAction (const QString
& phrase
, const QString
& cut
, const QObject
* receiver
, const char* slot
, KActionCollection
* parent
)
178 : KAction (phrase
, "phrase", KShortcut(cut
), 0, 0, parent
, phrase
.latin1()) {
179 this->phrase
= phrase
;
180 connect (this, SIGNAL(slotActivated (const QString
&)), receiver
, slot
);
186 void slotActivated () {
187 KAction::slotActivated();
188 emit
slotActivated (phrase
);
192 void slotActivated (const QString
&phrase
);