1 /*************************************************************************** 2 * Copyright (C) 2002 by Gunnar Schmi Dt <kmouth@schmi-dt.de * 3 * (C) 2015 by Jeremy Whiting <jpwhiting@kde.org> * 4 * * 5 * This program is free software; you can redistribute it and/or modify * 6 * it under the terms of the GNU General Public License as published by * 7 * the Free Software Foundation; either version 2 of the License, or * 8 * (at your option) any later version. * 9 * * 10 * This program is distributed in the hope that it will be useful, * 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 * GNU General Public License for more details. * 14 * * 15 * You should have received a copy of the GNU General Public License * 16 * along with this program; if not, write to the * 17 * Free Software Foundation, Inc., * 18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * 19 ***************************************************************************/ 20 21 #ifndef PHRASEBOOK_H 22 #define PHRASEBOOK_H 23 24 #include <QObject> 25 #include <QMenu> 26 #include <QPrinter> 27 #include <QTextStream> 28 #include <QXmlInputSource> 29 30 #include <QAction> 31 #include <KActionCollection> 32 #include <QIcon> 33 #include <KToolBar> 34 35 class QUrl; 36 37 struct StandardBook { 38 QString name; 39 QString path; 40 QString filename; 41 }; 42 typedef QList<StandardBook> StandardBookList; 43 44 /** 45 * The class Phrase represents one phrase in a phrase book. 46 * @author Gunnar Schmi Dt 47 */ 48 class Phrase 49 { 50 friend class PhraseBookParser; 51 public: 52 Phrase(); 53 Phrase(const QString &phrase); 54 Phrase(const QString &phrase, const QString &shortcut); 55 56 QString getPhrase() const; 57 QString getShortcut() const; 58 59 void setPhrase(const QString &phrase); 60 void setShortcut(const QString &shortcut); 61 62 private: 63 QString phrase; 64 QString shortcut; 65 }; 66 67 /** 68 * The class PhraseBookEntry implements a phrase book entry. That can be either 69 * a phrase or a start tag a sub phrase book. 70 * @author Gunnar Schmi Dt 71 */ 72 class PhraseBookEntry 73 { 74 public: 75 PhraseBookEntry(); 76 explicit PhraseBookEntry(const Phrase &phrase, int level = 1, bool isPhrase = true); ~PhraseBookEntry()77 ~PhraseBookEntry() {} 78 79 void setPhrase(Phrase phrase, int level = 1, bool isPhrase = true); 80 81 bool isPhrase() const; 82 Phrase getPhrase() const; 83 int getLevel() const; 84 85 private: 86 bool isPhraseValue; 87 Phrase phrase; 88 int level; 89 }; 90 91 typedef QList<PhraseBookEntry> PhraseBookEntryList; 92 93 /** 94 * The class PhraseBook implements a phrase book. It mainly stores a 95 * token list where each token is a phrase book entry (either a phrase 96 * or a sub phrase book). The entries are placed into a tree structure 97 * as follows: 98 * 99 * The level of each entry tells the level in the tree (level=0 is the top 100 * level), each sub book in level i directly or indirectly contains all 101 * following entries until an entry of level at most i or the end of the 102 * token list. 103 * 104 * @author Gunnar Schmi Dt 105 */ 106 class PhraseBook : public PhraseBookEntryList 107 { 108 public: PhraseBook()109 PhraseBook() : PhraseBookEntryList() {} ~PhraseBook()110 ~PhraseBook() {} 111 112 /** opens a file containing a phrase book. Returns true if successful. */ 113 bool open(const QUrl &url); 114 115 /** decodes a phrase book. Returns true if successful. */ 116 bool decode(const QString &xml); 117 118 /** decodes a phrase book. Returns true if successful. */ 119 bool decode(QXmlInputSource &source); 120 121 /** Writes the phrases to a file. Returns true if successful. */ 122 bool save(const QUrl &url); 123 124 /** Writes the phrases to a file. Returns true if successful. */ 125 bool save(const QUrl &url, bool asPhrasebook); 126 127 /** Writes the phrases to a QTextStream. */ 128 void save(QTextStream &stream, bool asPhrasebook); 129 130 /** Prints the phrases. */ 131 void print(QPrinter *pPrinter); 132 133 /** Shows a file selector and writes the phrases to a file. 134 * @return 1, if the file got successfully written, 135 * 0, if the user canceled the operation, 136 * -1, if there was an error when saving the file. 137 */ 138 int save(QWidget *parent, const QString &title, QUrl &url, bool phrasebookFirst = true); 139 140 /** encodes the phrase book. Returns the encoded xml code. */ 141 QString encode(); 142 143 /** Stores all entries in a QStringList. All hierarchy information and all 144 * shortcuts are ignored during this operation. 145 */ 146 QStringList toStringList(); 147 148 /** Adds the entries of the book to both the given popup menu and the given 149 * toolbar. The corresponding actions will be inserted into phrases. 150 */ 151 void addToGUI(QMenu *popup, KToolBar *toolbar, 152 KActionCollection *phrases, 153 QObject *receiver, const char *slot) const; 154 155 /** Inserts book into a new sub phrase book. 156 * @param name the name of the new sub phrase book. 157 * @param book the phrase book to insert. 158 */ 159 void insert(const QString &name, const PhraseBook &book); 160 161 static StandardBookList standardPhraseBooks(); 162 163 private: 164 static QString displayPath(const QString &path); 165 }; 166 167 class PhraseAction : public QAction 168 { 169 Q_OBJECT 170 public: PhraseAction(const QString & phrase,const QString & cut,const QObject * receiver,const char * slot,KActionCollection * parent)171 PhraseAction(const QString& phrase, const QString& cut, const QObject* receiver, const char* slot, KActionCollection* parent) 172 : QAction(QIcon::fromTheme(QStringLiteral("phrase")), phrase, parent) 173 { 174 this->setShortcut(cut); 175 this->phrase = phrase; 176 connect(this, &QAction::triggered, this, &PhraseAction::slotTriggered); 177 connect(this, SIGNAL(slotActivated(const QString &)), receiver, slot); 178 parent->addAction(phrase, this); 179 } ~PhraseAction()180 ~PhraseAction() override 181 { 182 } 183 184 public Q_SLOTS: slotTriggered()185 void slotTriggered() 186 { 187 // trigger(); 188 Q_EMIT slotActivated(phrase); 189 } 190 191 Q_SIGNALS: 192 void slotActivated(const QString &phrase); 193 194 private: 195 QString phrase; 196 }; 197 198 #endif 199