File indexing completed on 2024-04-21 15:55:32

0001 /*************************************************************************************
0002     Copyright (C) 2003 by Jeroen Wijnhout (Jeroen.Wijnhout@kdemail.net)
0003               (C) 2006-2022 by Michel Ludwig (michel.ludwig@kdemail.net)
0004  *************************************************************************************/
0005 
0006 /***************************************************************************
0007  *                                                                         *
0008  *   This program is free software; you can redistribute it and/or modify  *
0009  *   it under the terms of the GNU General Public License as published by  *
0010  *   the Free Software Foundation; either version 2 of the License, or     *
0011  *   (at your option) any later version.                                   *
0012  *                                                                         *
0013  ***************************************************************************/
0014 
0015 #ifndef DOCUMENTINFO_H
0016 #define DOCUMENTINFO_H
0017 
0018 #include <QHash>
0019 
0020 #include <KTextEditor/Document>
0021 #include <QUrl>
0022 
0023 #include "kiledebug.h"
0024 
0025 #include <latexcmd.h>
0026 
0027 #include "kileconstants.h"
0028 #include "kileextensions.h"
0029 #include "livepreview_utils.h"
0030 #include "outputinfo.h"
0031 
0032 #define TEX_CAT0 '\\'
0033 #define TEX_CAT1 '{'
0034 #define TEX_CAT2 '}'
0035 #define TEX_CAT3 '$'
0036 #define TEX_CAT4 '&'
0037 #define TEX_CAT6 '#'
0038 #define TEX_CAT7 '^'
0039 #define TEX_CAT8 '_'
0040 #define TEX_CAT13 '~'
0041 #define TEX_CAT14 '%'
0042 
0043 #define SIZE_STAT_ARRAY 6
0044 
0045 namespace KileDocument {
0046 class EditorExtension;
0047 }
0048 namespace KileConfiguration {
0049 class Manager;
0050 }
0051 namespace KileCodeCompletion {
0052 class LaTeXCompletionModel;
0053 class AbbreviationCompletionModel;
0054 class Manager;
0055 }
0056 namespace KileAbbreviation {
0057 class Manager;
0058 }
0059 namespace KileTool {
0060 class LivePreviewManager;
0061 }
0062 namespace KileParser {
0063 class ParserOutput;
0064 class Manager;
0065 }
0066 namespace KileView {
0067 class Manager;
0068 }
0069 
0070 namespace KileStruct
0071 {
0072 //Different types of elements in the structure view
0073 enum
0074 {
0075     None = 0x1, Label = 0x2, Sect = 0x4, Input = 0x8,
0076     BibItem = 0x10, Bibliography = 0x20, Package = 0x40, NewCommand = 0x80,
0077     Graphics = 0x100, Reference = 0x200, BeginEnv = 0x400, EndEnv = 0x800,
0078     BeginFloat = 0x1000, EndFloat = 0x2000,  Caption = 0x4000, BeamerFrame = 0x8000,
0079     BeamerBeginFrame = 0x10000, BeamerEndFrame = 0x20000, BeamerFrametitle = 0x40000, BeamerBeginBlock = 0x80000,
0080     ToDo = 0x100000, FixMe = 0x200000, NewEnvironment = 0x400000
0081 };
0082 
0083 //Different levels (in the parent-child hierarchy) in the structure view
0084 enum
0085 {
0086     Hidden = -4, NotSpecified = -3, Object = -2, File = -1
0087 };
0088 }
0089 
0090 /**
0091  * A convenience class to store info about how LaTeX elements should appear in the
0092  * structure view. A QMap<QString, KileStructData> should be created, so that the
0093  * actual LaTeX elements can be mapped to this class.
0094  **/
0095 class KileStructData
0096 {
0097 public:
0098     explicit KileStructData(int lvl = 0, int tp = KileStruct::None, const QString &px = QString(), const QString &fldr = "root" )
0099         : level(lvl), type(tp), pix(px), folder(fldr) {}
0100     /** At which level the element should be visible **/
0101     int             level;
0102     /** The type of element (see @ref KileStruct) **/
0103     int             type;
0104     /** The name of the icon that goes with this element. The icon is located using SmallIcon(pix). **/
0105     QString     pix, folder;
0106 };
0107 
0108 /**
0109  * KileDocument::Info is a decorator class for the Document class. We can't derive a class from an interface
0110  * without implementing the interface, a decorator class is a way to add some functionality to the Document class.
0111  **/
0112 
0113 namespace KileDocument
0114 {
0115 
0116 struct BracketResult
0117 {
0118     BracketResult() : line(0), col(0) {}
0119     QString option, value;
0120     int line, col;
0121 };
0122 
0123 struct TodoResult
0124 {
0125     int type;
0126     uint colTag;
0127     uint colComment;
0128     QString comment;
0129 };
0130 
0131 class Info : public QObject
0132 {
0133     Q_OBJECT
0134 
0135 public:
0136     static bool containsInvalidCharacters(const QUrl&);
0137     static QUrl repairInvalidCharacters(const QUrl&, QWidget *mainWidget, bool checkForFileExistence = true);
0138     static QUrl repairExtension(const QUrl &url, QWidget *mainWidget, bool checkForFileExistence = true);
0139     static QUrl makeValidTeXURL(const QUrl &url, QWidget *mainWidget, bool istexfile, bool checkForFileExistence = true);
0140     static QUrl renameIfExist(const QUrl &url, QWidget *mainWidget);
0141 
0142 public:
0143     Info();
0144     ~Info();
0145 
0146     QStringList labels() const {
0147         return m_labels;
0148     }
0149     QStringList bibItems() const {
0150         return m_bibItems;
0151     }
0152     QStringList dependencies() const {
0153         return m_deps;
0154     }
0155     QStringList bibliographies() const {
0156         return m_bibliography;
0157     }
0158     QStringList packages() const {
0159         return m_packages;
0160     }
0161     QStringList newCommands() const {
0162         return m_newCommands;
0163     }
0164     QStringList asyFigures() const {
0165         return m_asyFigures;
0166     }
0167 
0168     bool openStructureLabels() {
0169         return m_openStructureLabels;
0170     }
0171     bool openStructureReferences() {
0172         return m_openStructureReferences;
0173     }
0174     bool openStructureBibitems() {
0175         return m_openStructureBibitems;
0176     }
0177     bool openStructureTodo() {
0178         return m_openStructureTodo;
0179     }
0180 
0181     bool showStructureLabels() {
0182         return m_showStructureLabels;
0183     }
0184 
0185     const QMap<QString, KileStructData>& dictStructLevel() {
0186         return m_dictStructLevel;
0187     }
0188 
0189     const QString & preamble() const {
0190         return m_preamble;
0191     }
0192 
0193     virtual bool isLaTeXRoot() {
0194         return m_bIsRoot;
0195     }
0196 
0197     virtual QUrl url();
0198 
0199     virtual void updateStructLevelInfo();
0200 
0201     void setBaseDirectory(const QUrl &url);
0202     const QUrl &getBaseDirectory() const;
0203 
0204     virtual bool isTextDocument();
0205     virtual Type getType();
0206 
0207     /**
0208      * Returns a file filter suitable for loading and saving files of this class' type.
0209      **/
0210     virtual std::list<Extensions::ExtensionType> getFileFilter() const;
0211 
0212     virtual bool isDocumentTypePromotionAllowed();
0213     void setDocumentTypePromotionAllowed(bool b);
0214 
0215     /**
0216      * Returns true iff new parsing is required.
0217      **/
0218     bool isDirty() const;
0219     void setDirty(bool b);
0220 
0221     virtual void installParserOutput(KileParser::ParserOutput *parserOutput);
0222 
0223 public Q_SLOTS:
0224     /**
0225      * Never call this function directly, use KileWidget::Structure::update(KileDocument::Info *, bool) instead
0226      **/
0227     virtual void updateStruct();
0228     virtual void updateBibItems();
0229 
0230 Q_SIGNALS:
0231     void urlChanged(KileDocument::Info* info, const QUrl &url);
0232     void isrootChanged(bool);
0233 
0234     void foundItem(const QString &title, uint line, uint column, int type, int level, uint startline, uint startcol,
0235                    const QString & pix, const QString & folder);
0236     void depChanged();
0237     void completed(KileDocument::Info* info);
0238     void parsingStarted(int maxValue);
0239     void parsingComplete();
0240     void parsingUpdate(int value);
0241 
0242 protected Q_SLOTS:
0243     void slotCompleted();
0244 
0245 protected:
0246     void count(const QString& line, long *stat);
0247 
0248     enum State {
0249         stStandard = 0, stComment = 1, stControlSequence = 3, stControlSymbol = 4,
0250         stCommand = 5, stEnvironment = 6
0251     };
0252 
0253     bool                        m_bIsRoot;
0254     bool                        m_dirty;
0255     QStringList                 m_labels;
0256     QStringList                 m_bibItems;
0257     QStringList                 m_deps, m_depsPrev;
0258     QStringList                 m_bibliography;
0259     QStringList                 m_packages;
0260     QStringList                 m_newCommands;
0261     QStringList                 m_asyFigures;
0262     QString                     m_preamble;
0263     QMap<QString,KileStructData>            m_dictStructLevel;
0264     KConfig                     *m_config;
0265     bool m_showStructureLabels;
0266     bool m_showStructureBibitems;
0267     bool m_showStructureGraphics;
0268     bool m_showStructureFloats;
0269     bool m_showStructureReferences;
0270     bool m_showStructureInputFiles;
0271     bool m_showStructureTodo;
0272     bool m_showSectioningLabels;
0273     bool m_openStructureLabels;
0274     bool m_openStructureReferences;
0275     bool m_openStructureBibitems;
0276     bool m_openStructureTodo;
0277     QUrl                        m_baseDirectory;
0278     bool                        documentTypePromotionAllowed;
0279     Extensions *m_extensions;
0280 };
0281 
0282 
0283 /**
0284  * The URL of a text document is managed directly by the corresponding KTextEditor::Document.
0285  **/
0286 class TextInfo : public Info
0287 {
0288     Q_OBJECT
0289 public:
0290     /**
0291      * @param defaultMode the mode that will be set automatically
0292      *                    once a new document is installed
0293      **/
0294     TextInfo(Extensions *extensions,
0295              KileAbbreviation::Manager *abbreviationManager,
0296              KileParser::Manager *parserManager,
0297              const QString& defaultMode = QString());
0298     virtual ~TextInfo();
0299 
0300     /**
0301      * @returns the document for which this class is a decorator
0302      **/
0303     const KTextEditor::Document* getDoc() const;
0304     KTextEditor::Document* getDoc();
0305     const KTextEditor::Document* getDocument() const;
0306     KTextEditor::Document* getDocument();
0307     void setDoc(KTextEditor::Document *doc);
0308     void setDocument(KTextEditor::Document *doc);
0309     void detach();
0310 
0311     /**
0312      * Used by @ref KileDocInfoDlg to display statistics of the Document.
0313      * @returns an array with some statistical data of the document.
0314      * The array is filled as follows: [0] = #c in words, [1] = #c in latex commands and environments,
0315        [2] = #c whitespace, [3] = #words, [4] = # latex_commands, [5] = latex_environments **/
0316 
0317     virtual const long* getStatistics(KTextEditor::View *view = Q_NULLPTR);
0318 
0319     /**
0320      * @returns the URL of the KTextEditor::Document if not null, an empty QUrl otherwise
0321      **/
0322     virtual QUrl url() override;
0323 
0324     virtual Type getType() override;
0325 
0326     virtual bool isTextDocument() override;
0327 
0328     void setHighlightingMode(const QString& highlight = QString());
0329 
0330     void setMode(const QString& mode = QString());
0331 
0332     void setDefaultMode(const QString& string);
0333 
0334     /**
0335      * "Overridden" method that installs custom event filters by using the "installEventFilters"
0336      * method. It also installs signal connections by using the "installSignalConnections"
0337      * method.
0338      * @warning Only this method should be used to create new views for text documents !
0339      * @return Q_NULLPTR if no document is set (m_doc == NULL)
0340      **/
0341     KTextEditor::View* createView(QWidget *parent, const char *name = Q_NULLPTR);
0342 
0343     void startAbbreviationCompletion(KTextEditor::View *view);
0344 
0345     /**
0346      * Returns the contents of the document if a KTextEditor::Document is present. Otherwise,
0347      * the contents supplied via @ref setDocumentContents is returned.
0348      **/
0349     const QStringList documentContents() const;
0350     void setDocumentContents(const QStringList& contents);
0351 
0352 Q_SIGNALS:
0353     void documentDetached(KTextEditor::Document*);
0354     void aboutToBeDestroyed(KileDocument::TextInfo*);
0355 
0356 protected Q_SLOTS:
0357     void slotFileNameChanged();
0358     void slotViewDestroyed(QObject *object);
0359     void activateDefaultMode();
0360 
0361     void makeDirtyIfModified();
0362 
0363 protected:
0364     KTextEditor::Document               *m_doc;
0365     bool                        m_dirty;
0366     long                        *m_arStatistics;
0367     QString                     m_defaultMode;
0368     QHash<KTextEditor::View*, QList<QObject*> > m_eventFilterHash;
0369     KileAbbreviation::Manager           *m_abbreviationManager;
0370     KileCodeCompletion::AbbreviationCompletionModel *m_abbreviationCodeCompletionModel;
0371     KileParser::Manager             *m_parserManager;
0372 
0373     QString matchBracket(QChar c, int &, int &);
0374     QString getTextline(uint line, TodoResult &todo);
0375     void searchTodoComment(const QString &s, uint startpos, TodoResult &todo);
0376 
0377     /**
0378      * Creates the event filters that should be used on a view. Subclasses can override
0379      * this method to provide custom event filters. The default implementation does nothing and
0380      * returns an empty list. The event filters that are returned by this method are managed by
0381      * the "installEventFilters", "removeInstalledEventFilters" methods.
0382      * @warning The event filters that are created must be children of the view!
0383      * @param view the view that is considered
0384      **/
0385     virtual QList<QObject*> createEventFilters(KTextEditor::View *view);
0386 
0387     /**
0388      * Installs event filters on a view. The function "createEventFilters(KTextEditor::View *view)
0389      * function is used for a specific view.
0390      * @param view the view that is considered
0391      **/
0392     virtual void installEventFilters(KTextEditor::View *view);
0393 
0394     /**
0395      * Removes the event filters that were previously installed by the "installEventFilters"
0396      * function.
0397      * @param view the view that is considered
0398      **/
0399     virtual void removeInstalledEventFilters(KTextEditor::View *view);
0400 
0401     /**
0402      * Installs the event filters on all the views that are currently open for the
0403      * managed document object. The function "installEventFilters(KTextEditor::View *view)
0404      * function is used for a specific view.
0405      **/
0406     void installEventFilters();
0407 
0408     /**
0409      * Removes the event filters from all the views that are currently open for the
0410      * managed document object.
0411      **/
0412     void removeInstalledEventFilters();
0413 
0414     /**
0415      * Installs signal connections on a view.
0416      */
0417     virtual void installSignalConnections(KTextEditor::View *view);
0418 
0419     /**
0420      * Disconnects the signals that were previously connected with the
0421      * "installSignalConnections" function.
0422      */
0423     virtual void removeSignalConnections(KTextEditor::View *view);
0424 
0425     /**
0426      * Installs signal connections on all the views that are currently open for the
0427      * managed document object. The function "installSignalConnections(KTextEditor::View *view)
0428      * function is used for a specific view.
0429      **/
0430     void installSignalConnections();
0431 
0432     /**
0433      * Removes signal connections from all the views that are currently open for the
0434      * managed document object.
0435      **/
0436     void removeSignalConnections();
0437 
0438     /**
0439      * Register code completion models on a view.
0440      */
0441     virtual void registerCodeCompletionModels(KTextEditor::View *view);
0442 
0443     /**
0444      * Unregisters the code completion models that were previously registered by the
0445      * "registerCodeCompletionModels" method.
0446      */
0447     virtual void unregisterCodeCompletionModels(KTextEditor::View *view);
0448 
0449     /**
0450      * Register code completion models on all the views that are currently open for the
0451      * managed document object. The function "registerCodeCompletionModels(KTextEditor::View *view)
0452      * function is used for a specific view.
0453      **/
0454     void registerCodeCompletionModels();
0455 
0456     /**
0457      * Unregister the code completion models from all the views that are currently open for the
0458      * managed document object.
0459      **/
0460     void unregisterCodeCompletionModels();
0461 
0462 private:
0463     QStringList m_documentContents;
0464 };
0465 
0466 
0467 
0468 class LaTeXInfo : public TextInfo, public KileTool::LivePreviewUserStatusHandler, public LaTeXOutputHandler
0469 {
0470     Q_OBJECT
0471 
0472 public:
0473     /**
0474      * @param eventFilter the event filter that will be installed on managed documents
0475      **/
0476     LaTeXInfo(Extensions *extensions,
0477               KileAbbreviation::Manager *abbreviationManager,
0478               LatexCommands *commands,
0479               KileDocument::EditorExtension *editorExtension,
0480               KileConfiguration::Manager *manager,
0481               KileCodeCompletion::Manager *codeCompletionManager,
0482               KileTool::LivePreviewManager *livePreviewManager,
0483               KileView::Manager *viewManager,
0484               KileParser::Manager *parserManager);
0485 
0486     virtual ~LaTeXInfo();
0487 
0488     virtual Type getType() override;
0489 
0490     virtual std::list<Extensions::ExtensionType> getFileFilter() const override;
0491 
0492     void startLaTeXCompletion(KTextEditor::View *view);
0493 
0494     virtual void installParserOutput(KileParser::ParserOutput *parserOutput) override;
0495 
0496 public Q_SLOTS:
0497     virtual void updateStruct() override;
0498 
0499 protected:
0500     LatexCommands *m_commands;
0501     EditorExtension *m_editorExtension;
0502     KileConfiguration::Manager *m_configurationManager;
0503     QObject *m_eventFilter;
0504     KileCodeCompletion::LaTeXCompletionModel *m_latexCompletionModel;
0505     KileTool::LivePreviewManager *m_livePreviewManager;
0506     KileView::Manager *m_viewManager;
0507 
0508     virtual void updateStructLevelInfo() override;
0509     virtual void checkChangedDeps();
0510 
0511     /**
0512      * Creates a custom event filter.
0513      */
0514     virtual QList<QObject*> createEventFilters(KTextEditor::View *view) override;
0515 
0516     virtual void installSignalConnections(KTextEditor::View *view) override;
0517     virtual void removeSignalConnections(KTextEditor::View *view) override;
0518 
0519     virtual void registerCodeCompletionModels(KTextEditor::View *view) override;
0520     virtual void unregisterCodeCompletionModels(KTextEditor::View *view) override;
0521 
0522 private:
0523     BracketResult matchBracket(int &, int &);
0524 };
0525 
0526 
0527 
0528 class BibInfo : public TextInfo
0529 {
0530     Q_OBJECT
0531 
0532 public:
0533     BibInfo (Extensions *extensions,
0534              KileAbbreviation::Manager *abbreviationManager,
0535              KileParser::Manager *parserManager,
0536              LatexCommands* commands);
0537     virtual ~BibInfo();
0538 
0539     virtual bool isLaTeXRoot() override;
0540 
0541     virtual Type getType() override;
0542 
0543     virtual std::list<Extensions::ExtensionType> getFileFilter() const override;
0544 
0545     virtual void installParserOutput(KileParser::ParserOutput *parserOutput) override;
0546 
0547 public Q_SLOTS:
0548     virtual void updateStruct() override;
0549 };
0550 
0551 class ScriptInfo : public TextInfo
0552 {
0553     Q_OBJECT
0554 
0555 public:
0556     ScriptInfo(Extensions *extensions,
0557                KileAbbreviation::Manager *abbreviationManager,
0558                KileParser::Manager *parserManager);
0559 
0560     virtual ~ScriptInfo();
0561 
0562     virtual bool isLaTeXRoot() override;
0563 
0564     virtual Type getType() override;
0565 
0566     virtual std::list<Extensions::ExtensionType> getFileFilter() const override;
0567 };
0568 
0569 }
0570 #endif