File indexing completed on 2024-05-05 05:52:22

0001 /***************************************************************************
0002    pluginKatexmltools.cpp
0003    copyright            : (C) 2001-2002 by Daniel Naber
0004    email                : daniel.naber@t-online.de
0005 ***************************************************************************/
0006 
0007 /***************************************************************************
0008  This program is free software; you can redistribute it and/or
0009  modify it under the terms of the GNU General Public License
0010  as published by the Free Software Foundation; either version 2
0011  of the License, or ( at your option ) any later version.
0012 
0013  This program is distributed in the hope that it will be useful,
0014  but WITHOUT ANY WARRANTY; without even the implied warranty of
0015  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0016  GNU General Public License for more details.
0017 
0018  You should have received a copy of the GNU General Public License
0019  along with this program; if not, write to the Free Software
0020  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0021  ***************************************************************************/
0022 
0023 #pragma once
0024 
0025 #include "pseudo_dtd.h"
0026 
0027 #include <ktexteditor/application.h>
0028 #include <ktexteditor/codecompletionmodel.h>
0029 #include <ktexteditor/codecompletionmodelcontrollerinterface.h>
0030 #include <ktexteditor/document.h>
0031 #include <ktexteditor/mainwindow.h>
0032 #include <ktexteditor/plugin.h>
0033 #include <ktexteditor/view.h>
0034 
0035 #include <QString>
0036 #include <QVariantList>
0037 
0038 class QComboBox;
0039 class QPushButton;
0040 
0041 class PluginKateXMLTools : public KTextEditor::Plugin
0042 {
0043     Q_OBJECT
0044 
0045 public:
0046     explicit PluginKateXMLTools(QObject *parent = nullptr, const QVariantList & = QVariantList());
0047     ~PluginKateXMLTools() override;
0048     QObject *createView(KTextEditor::MainWindow *mainWindow) override;
0049 };
0050 
0051 class PluginKateXMLToolsCompletionModel : public KTextEditor::CodeCompletionModel, public KTextEditor::CodeCompletionModelControllerInterface
0052 {
0053     Q_OBJECT
0054     Q_INTERFACES(KTextEditor::CodeCompletionModelControllerInterface)
0055 
0056 public:
0057     PluginKateXMLToolsCompletionModel(QObject *parent);
0058     ~PluginKateXMLToolsCompletionModel() override;
0059 
0060     //
0061     // KTextEditor::CodeCompletionModel
0062     //
0063 public:
0064     int columnCount(const QModelIndex &) const override;
0065     int rowCount(const QModelIndex &parent) const override;
0066     QModelIndex parent(const QModelIndex &index) const override;
0067     QModelIndex index(int row, int column, const QModelIndex &parent) const override;
0068     QVariant data(const QModelIndex &idx, int role) const override;
0069 
0070     void executeCompletionItem(KTextEditor::View *view, const KTextEditor::Range &word, const QModelIndex &index) const override;
0071 
0072     //
0073     // KTextEditor::CodeCompletionModelControllerInterface
0074     //
0075 public:
0076     bool shouldStartCompletion(KTextEditor::View *view, const QString &insertedText, bool userInsertion, const KTextEditor::Cursor &position) override;
0077 
0078 public Q_SLOTS:
0079 
0080     void getDTD();
0081 
0082     void slotInsertElement();
0083     void slotCloseElement();
0084 
0085     void slotFinished(KJob *job);
0086     void slotData(KIO::Job *, const QByteArray &data);
0087 
0088     void completionInvoked(KTextEditor::View *kv, const KTextEditor::Range &range, InvocationType invocationType) override;
0089 
0090     /// Connected to the document manager, to manage the dtd collection.
0091     void slotDocumentDeleted(KTextEditor::Document *doc);
0092 
0093 protected:
0094     QString currentModeToString() const;
0095     static QStringList sortQStringList(QStringList list);
0096     // bool eventFilter( QObject *object, QEvent *event );
0097 
0098     static QString insideTag(KTextEditor::View &kv);
0099     static QString insideAttribute(KTextEditor::View &kv);
0100 
0101     static bool isOpeningTag(const QString &tag);
0102     static bool isClosingTag(const QString &tag);
0103     static bool isEmptyTag(const QString &tag);
0104     static bool isQuote(const QString &ch);
0105 
0106     static QString getParentElement(KTextEditor::View &view, int skipCharacters);
0107 
0108     enum Mode { none, entities, attributevalues, attributes, elements, closingtag };
0109     enum PopupMode { noPopup, tagname, attributename, attributevalue, entityname };
0110 
0111     enum Level { groupNode = 1 };
0112 
0113     /// Assign the PseudoDTD @p dtd to the Kate::View @p view
0114     void assignDTD(PseudoDTD *dtd, KTextEditor::View *view);
0115 
0116     /// temporary placeholder for the metaDTD file
0117     QString m_dtdString;
0118     /// temporary placeholder for the view to assign a DTD to while the file is loaded
0119     KTextEditor::View *m_viewToAssignTo;
0120     /// URL of the last loaded meta DTD
0121     QString m_urlString;
0122 
0123     QStringList m_allowed;
0124 
0125     Mode m_mode;
0126     int m_correctPos;
0127 
0128     /// maps KTE::Document -> DTD
0129     QHash<KTextEditor::Document *, PseudoDTD *> m_docDtds;
0130 
0131     /// maps DTD filename -> DTD
0132     QHash<QString, PseudoDTD *> m_dtds;
0133 };
0134 
0135 class PluginKateXMLToolsView : public QObject, public KXMLGUIClient
0136 {
0137     Q_OBJECT
0138 
0139 public:
0140     explicit PluginKateXMLToolsView(KTextEditor::MainWindow *mainWin);
0141     ~PluginKateXMLToolsView() override;
0142 
0143 protected:
0144     KTextEditor::MainWindow *m_mainWindow;
0145     PluginKateXMLToolsCompletionModel m_model;
0146 };
0147 
0148 class InsertElement : public QDialog
0149 {
0150     Q_OBJECT
0151 
0152 public:
0153     InsertElement(const QStringList &completions, QWidget *parent);
0154     ~InsertElement() override;
0155 
0156     QString text() const;
0157 
0158 private Q_SLOTS:
0159     void slotHistoryTextChanged(const QString &);
0160 
0161 private:
0162     QComboBox *m_cmbElements;
0163     QPushButton *m_okButton;
0164 };
0165 
0166 // kate: space-indent on; indent-width 4; replace-tabs on; mixed-indent off;