Q3ValueStack->QStack
[kdeaccessibility.git] / kmouth / phrasebook / phrasebook.h
blob50c60b02fb9cd48902605e7abf08885167db5bf6
1 /***************************************************************************
2 phrasebook.h - 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 #ifndef PHRASEBOOK_H
19 #define PHRASEBOOK_H
21 #include <qobject.h>
22 #include <q3dragobject.h>
23 #include <qxml.h>
24 //Added by qt3to4:
25 #include <QTextStream>
26 #include <Q3PopupMenu>
28 #include <kaction.h>
29 #include <kprinter.h>
31 class KURL;
33 /**
34 * The class Phrase represents one phrase in a phrase book.
35 * @author Gunnar Schmi Dt
37 class Phrase {
38 friend class PhraseBookParser;
39 public:
40 Phrase();
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);
50 private:
51 QString phrase;
52 QString shortcut;
55 /**
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 {
61 public:
62 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;
70 int getLevel() const;
72 private:
73 bool isPhraseValue;
74 Phrase phrase;
75 int level;
78 typedef QList<PhraseBookEntry> PhraseBookEntryList;
80 /**
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
84 * as follows:
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
89 * token list.
91 * @author Gunnar Schmi Dt
93 class PhraseBook : public PhraseBookEntryList {
94 public:
95 PhraseBook() : PhraseBookEntryList() {};
96 ~PhraseBook() {};
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. */
127 QString encode ();
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 (Q3PopupMenu *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 {
153 Q_OBJECT
154 public:
155 PhraseBookDrag (PhraseBook *book, QWidget *dragSource = 0, const char *name = 0);
156 PhraseBookDrag (QWidget *dragSource = 0, const char *name = 0);
157 ~PhraseBookDrag ();
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);
167 private:
168 bool isEmpty;
169 Q3TextDrag xmlphrasebook;
170 Q3TextDrag xml;
171 Q3TextDrag plain;
174 class PhraseAction : public KAction {
175 Q_OBJECT
176 public:
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);
182 ~PhraseAction () {
185 public slots:
186 void slotActivated () {
187 KAction::slotActivated();
188 emit slotActivated (phrase);
191 signals:
192 void slotActivated (const QString &phrase);
194 private:
195 QString phrase;
198 #endif