1 /***************************************************************************
2    pluginKatexmltools.cpp
3    copyright            : (C) 2001-2002 by Daniel Naber
4    email                : daniel.naber@t-online.de
5 ***************************************************************************/
6 
7 /***************************************************************************
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License
10  as published by the Free Software Foundation; either version 2
11  of the License, or ( at your option ) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  ***************************************************************************/
22 
23 #ifndef PLUGIN_KATEXMLTOOLS_H
24 #define PLUGIN_KATEXMLTOOLS_H
25 
26 #include "pseudo_dtd.h"
27 
28 #include <ktexteditor/application.h>
29 #include <ktexteditor/codecompletioninterface.h>
30 #include <ktexteditor/codecompletionmodel.h>
31 #include <ktexteditor/codecompletionmodelcontrollerinterface.h>
32 #include <ktexteditor/document.h>
33 #include <ktexteditor/mainwindow.h>
34 #include <ktexteditor/plugin.h>
35 #include <ktexteditor/view.h>
36 
37 #include <QString>
38 #include <QVariantList>
39 
40 class QComboBox;
41 class QPushButton;
42 
43 class PluginKateXMLTools : public KTextEditor::Plugin
44 {
45     Q_OBJECT
46 
47 public:
48     explicit PluginKateXMLTools(QObject *parent = nullptr, const QVariantList & = QVariantList());
49     ~PluginKateXMLTools() override;
50     QObject *createView(KTextEditor::MainWindow *mainWindow) override;
51 };
52 
53 class PluginKateXMLToolsCompletionModel : public KTextEditor::CodeCompletionModel, public KTextEditor::CodeCompletionModelControllerInterface
54 {
55     Q_OBJECT
56     Q_INTERFACES(KTextEditor::CodeCompletionModelControllerInterface)
57 
58 public:
59     PluginKateXMLToolsCompletionModel(QObject *parent);
60     ~PluginKateXMLToolsCompletionModel() override;
61 
62     //
63     // KTextEditor::CodeCompletionModel
64     //
65 public:
66     int columnCount(const QModelIndex &) const override;
67     int rowCount(const QModelIndex &parent) const override;
68     QModelIndex parent(const QModelIndex &index) const override;
69     QModelIndex index(int row, int column, const QModelIndex &parent) const override;
70     QVariant data(const QModelIndex &idx, int role) const override;
71 
72     void executeCompletionItem(KTextEditor::View *view, const KTextEditor::Range &word, const QModelIndex &index) const override;
73 
74     //
75     // KTextEditor::CodeCompletionModelControllerInterface
76     //
77 public:
78     bool shouldStartCompletion(KTextEditor::View *view, const QString &insertedText, bool userInsertion, const KTextEditor::Cursor &position) override;
79 
80 public Q_SLOTS:
81 
82     void getDTD();
83 
84     void slotInsertElement();
85     void slotCloseElement();
86 
87     void slotFinished(KJob *job);
88     void slotData(KIO::Job *, const QByteArray &data);
89 
90     void completionInvoked(KTextEditor::View *kv, const KTextEditor::Range &range, InvocationType invocationType) override;
91 
92     /// Connected to the document manager, to manage the dtd collection.
93     void slotDocumentDeleted(KTextEditor::Document *doc);
94 
95 protected:
96     QString currentModeToString() const;
97     static QStringList sortQStringList(QStringList list);
98     // bool eventFilter( QObject *object, QEvent *event );
99 
100     QString insideTag(KTextEditor::View &kv);
101     QString insideAttribute(KTextEditor::View &kv);
102 
103     static bool isOpeningTag(const QString &tag);
104     static bool isClosingTag(const QString &tag);
105     static bool isEmptyTag(const QString &tag);
106     static bool isQuote(const QString &ch);
107 
108     QString getParentElement(KTextEditor::View &view, int skipCharacters);
109 
110     enum Mode { none, entities, attributevalues, attributes, elements, closingtag };
111     enum PopupMode { noPopup, tagname, attributename, attributevalue, entityname };
112 
113     enum Level { groupNode = 1 };
114 
115     /// Assign the PseudoDTD @p dtd to the Kate::View @p view
116     void assignDTD(PseudoDTD *dtd, KTextEditor::View *view);
117 
118     /// temporary placeholder for the metaDTD file
119     QString m_dtdString;
120     /// temporary placeholder for the view to assign a DTD to while the file is loaded
121     KTextEditor::View *m_viewToAssignTo;
122     /// URL of the last loaded meta DTD
123     QString m_urlString;
124 
125     QStringList m_allowed;
126 
127     Mode m_mode;
128     int m_correctPos;
129 
130     // code completion stuff:
131     KTextEditor::CodeCompletionInterface *m_codeInterface = nullptr;
132 
133     /// maps KTE::Document -> DTD
134     QHash<KTextEditor::Document *, PseudoDTD *> m_docDtds;
135 
136     /// maps DTD filename -> DTD
137     QHash<QString, PseudoDTD *> m_dtds;
138 };
139 
140 class PluginKateXMLToolsView : public QObject, public KXMLGUIClient
141 {
142     Q_OBJECT
143 
144 public:
145     explicit PluginKateXMLToolsView(KTextEditor::MainWindow *mainWin);
146     ~PluginKateXMLToolsView() override;
147 
148 protected:
149     KTextEditor::MainWindow *m_mainWindow;
150     PluginKateXMLToolsCompletionModel m_model;
151 };
152 
153 class InsertElement : public QDialog
154 {
155     Q_OBJECT
156 
157 public:
158     InsertElement(const QStringList &completions, QWidget *parent);
159     ~InsertElement() override;
160 
161     QString text() const;
162 
163 private Q_SLOTS:
164     void slotHistoryTextChanged(const QString &);
165 
166 private:
167     QComboBox *m_cmbElements;
168     QPushButton *m_okButton;
169 };
170 
171 #endif // PLUGIN_KATEXMLTOOLS_H
172 
173 // kate: space-indent on; indent-width 4; replace-tabs on; mixed-indent off;
174