File indexing completed on 2024-04-28 03:40:30

0001 /***************************************************************************
0002  *   Copyright (C) 2002 by Gunnar Schmi Dt <kmouth@schmi-dt.de             *
0003  *             (C) 2015 by Jeremy Whiting <jpwhiting@kde.org>              *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
0019  ***************************************************************************/
0020 
0021 #ifndef PHRASEBOOK_H
0022 #define PHRASEBOOK_H
0023 
0024 #include <QIODevice>
0025 #include <QMenu>
0026 #include <QObject>
0027 #include <QPrinter>
0028 #include <QTextStream>
0029 
0030 #include <KActionCollection>
0031 #include <KToolBar>
0032 #include <QAction>
0033 #include <QIcon>
0034 
0035 class QUrl;
0036 
0037 struct StandardBook {
0038     QString name;
0039     QString path;
0040     QString filename;
0041 };
0042 typedef QList<StandardBook> StandardBookList;
0043 
0044 /**
0045  * The class Phrase represents one phrase in a phrase book.
0046  * @author Gunnar Schmi Dt
0047  */
0048 class Phrase
0049 {
0050 public:
0051     Phrase();
0052     explicit Phrase(const QString &phrase);
0053     explicit Phrase(const QString &phrase, const QString &shortcut);
0054 
0055     QString getPhrase() const;
0056     QString getShortcut() const;
0057 
0058     void setPhrase(const QString &phrase);
0059     void setShortcut(const QString &shortcut);
0060 
0061 private:
0062     QString phrase;
0063     QString shortcut;
0064 };
0065 
0066 /**
0067  * The class PhraseBookEntry implements a phrase book entry. That can be either
0068  * a phrase or a start tag a sub phrase book.
0069  * @author Gunnar Schmi Dt
0070  */
0071 class PhraseBookEntry
0072 {
0073 public:
0074     PhraseBookEntry();
0075     explicit PhraseBookEntry(const Phrase &phrase, int level = 1, bool isPhrase = true);
0076     ~PhraseBookEntry()
0077     {
0078     }
0079 
0080     void setPhrase(Phrase phrase, int level = 1, bool isPhrase = true);
0081 
0082     bool isPhrase() const;
0083     Phrase getPhrase() const;
0084     int getLevel() const;
0085 
0086 private:
0087     bool isPhraseValue;
0088     Phrase phrase;
0089     int level;
0090 };
0091 
0092 typedef QList<PhraseBookEntry> PhraseBookEntryList;
0093 
0094 /**
0095  * The class PhraseBook implements a phrase book. It mainly stores a
0096  * token list where each token is a phrase book entry (either a phrase
0097  * or a sub phrase book). The entries are placed into a tree structure
0098  * as follows:
0099  *
0100  * The level of each entry tells the level in the tree (level=0 is the top
0101  * level), each sub book in level i directly or indirectly contains all
0102  * following entries until an entry of level at most i or the end of the
0103  * token list.
0104  *
0105  * @author Gunnar Schmi Dt
0106  */
0107 class PhraseBook : public PhraseBookEntryList
0108 {
0109 public:
0110     PhraseBook()
0111         : PhraseBookEntryList()
0112     {
0113     }
0114     ~PhraseBook()
0115     {
0116     }
0117 
0118     /** opens a file containing a phrase book. Returns true if successful. */
0119     bool open(const QUrl &url);
0120 
0121     /** decodes a phrase book. Returns true if successful. */
0122     bool decode(QIODevice *source);
0123 
0124     /** Writes the phrases to a file. Returns true if successful. */
0125     bool save(const QUrl &url);
0126 
0127     /** Writes the phrases to a file. Returns true if successful. */
0128     bool save(const QUrl &url, bool asPhrasebook);
0129 
0130     /** Writes the phrases to a QTextStream. */
0131     void save(QTextStream &stream, bool asPhrasebook);
0132 
0133     /** Prints the phrases. */
0134     void print(QPrinter *pPrinter);
0135 
0136     /** Shows a file selector and writes the phrases to a file.
0137      *  @return 1, if the file got successfully written,
0138      *          0, if the user canceled the operation,
0139      *         -1, if there was an error when saving the file.
0140      */
0141     int save(QWidget *parent, const QString &title, QUrl &url, bool phrasebookFirst = true);
0142 
0143     /** encodes the phrase book. Returns the encoded xml code. */
0144     QString encode();
0145 
0146     /** Stores all entries in a QStringList. All hierarchy information and all
0147      * shortcuts are ignored during this operation.
0148      */
0149     QStringList toStringList();
0150 
0151     /** Adds the entries of the book to both the given popup menu and the given
0152      * toolbar. The corresponding actions will be inserted into phrases.
0153      */
0154     void addToGUI(QMenu *popup, KToolBar *toolbar, KActionCollection *phrases, QObject *receiver, const char *slot) const;
0155 
0156     /** Inserts book into a new sub phrase book.
0157      * @param name the name of the new sub phrase book.
0158      * @param book the phrase book to insert.
0159      */
0160     void insert(const QString &name, const PhraseBook &book);
0161 
0162     static StandardBookList standardPhraseBooks();
0163 
0164 private:
0165     static QString displayPath(const QString &path);
0166 };
0167 
0168 class PhraseAction : public QAction
0169 {
0170     Q_OBJECT
0171 public:
0172     PhraseAction(const QString &phrase, const QString &cut, const QObject *receiver, const char *slot, KActionCollection *parent)
0173         : QAction(QIcon::fromTheme(QStringLiteral("phrase")), phrase, parent)
0174     {
0175         this->setShortcut(cut);
0176         this->phrase = phrase;
0177         connect(this, &QAction::triggered, this, &PhraseAction::slotTriggered);
0178         connect(this, SIGNAL(slotActivated(const QString &)), receiver, slot);
0179         parent->addAction(phrase, this);
0180     }
0181     ~PhraseAction() override
0182     {
0183     }
0184 
0185 public Q_SLOTS:
0186     void slotTriggered()
0187     {
0188         //      trigger();
0189         Q_EMIT slotActivated(phrase);
0190     }
0191 
0192 Q_SIGNALS:
0193     void slotActivated(const QString &phrase);
0194 
0195 private:
0196     QString phrase;
0197 };
0198 
0199 #endif