File indexing completed on 2024-04-28 16:26:35

0001 /**************************************************************************************************
0002     begin                : Sun Dec 28 2003
0003     copyright            : (C) 2003 by Jeroen Wijnhout (Jeroen.Wijnhout@kdemail.net)
0004                                2005-2007 by Holger Danielsson (holger.danielsson@versanet.de)
0005                                2008-2022 by Michel Ludwig (michel.ludwig@kdemail.net)
0006  **************************************************************************************************/
0007 
0008 /***************************************************************************
0009  *                                                                         *
0010  *   This program is free software; you can redistribute it and/or modify  *
0011  *   it under the terms of the GNU General Public License as published by  *
0012  *   the Free Software Foundation; either version 2 of the License, or     *
0013  *   (at your option) any later version.                                   *
0014  *                                                                         *
0015   ***************************************************************************/
0016 
0017 #ifndef STRUCTUREWIDGET_H
0018 #define STRUCTUREWIDGET_H
0019 
0020 #include <QList>
0021 #include <QStackedWidget>
0022 #include <QToolTip>
0023 #include <QTreeWidget>
0024 
0025 #include <QMenu>
0026 #include <KService>
0027 
0028 #include "documentinfo.h"
0029 #include "parser/latexparser.h"
0030 
0031 //2007-02-15: dani
0032 // - class StructureViewItem not only saves the cursor position of the parameter,
0033 //   but also the real cursor position of the command
0034 
0035 class KileInfo;
0036 
0037 /**
0038  * ListView items that can hold some additional information appropriate for the Structure View. The
0039  * additional information is: line number, title string.
0040  **/
0041 namespace KileWidget
0042 {
0043 
0044 class StructureViewItem : public QTreeWidgetItem
0045 {
0046 public:
0047     StructureViewItem(QTreeWidgetItem *parent, const QString &title, const QUrl &url, uint line, uint m_column, int type, int level, uint startline, uint startcol);
0048     StructureViewItem(QTreeWidget *parent, const QString &label);
0049     explicit StructureViewItem(const QString &label, QTreeWidgetItem *parent = Q_NULLPTR);
0050 
0051     /** @returns the title of this element (for a label it return the label), without the (line ...) part **/
0052     const QString& title() const {
0053         return m_title;
0054     }
0055     /** @returns the line number of the structure element. **/
0056     uint line() const {
0057         return m_line;
0058     }
0059     /** @returns the column number of the structure element, right after the { **/
0060     uint column() const {
0061         return m_column;
0062     }
0063     /** @returns the type of element, see @ref KileStruct **/
0064     int type() const {
0065         return m_type;
0066     }
0067     uint startline() const {
0068         return m_startline;
0069     }
0070     uint startcol() const {
0071         return m_startcol;
0072     }
0073     /**@returns the file in which this item was found*/
0074     const QUrl &url() const {
0075         return m_url;
0076     }
0077     void setURL(const QUrl &url) {
0078         m_url = url;
0079     }
0080 
0081     int level() const {
0082         return m_level;
0083     }
0084     const QString &label() const {
0085         return m_label;
0086     }
0087 
0088     void setTitle(const QString &title);
0089     void setLabel(const QString &label);
0090 
0091 private:
0092     QString  m_title;
0093     QUrl     m_url;
0094     uint     m_line;
0095     uint     m_column;
0096     int      m_type, m_level;
0097     uint     m_startline;
0098     uint     m_startcol;
0099     QString  m_label;
0100 
0101     void setItemEntry();
0102 };
0103 
0104 class KileReferenceData
0105 {
0106 public:
0107     KileReferenceData() {}
0108     KileReferenceData(const QString &name, uint line, uint column) : m_name(name), m_line(line), m_column(column) {}
0109     ~KileReferenceData() {}
0110 
0111     const QString &name() const {
0112         return m_name;
0113     }
0114     uint line() const {
0115         return m_line;
0116     }
0117     uint column() const {
0118         return m_column;
0119     }
0120 
0121 private:
0122     QString m_name;
0123     uint m_line;
0124     uint m_column;
0125 };
0126 
0127 class StructureWidget; //forward declaration
0128 
0129 class StructureView : public QTreeWidget
0130 {
0131     Q_OBJECT
0132 
0133 public:
0134     StructureView(StructureWidget *stack, KileDocument::Info *docinfo);
0135     ~StructureView();
0136 
0137     void activate();
0138     void cleanUp(bool preserveState = true);
0139     void showReferences(KileInfo *ki);
0140 
0141     QUrl url() const {
0142         return m_docinfo->url();
0143     }
0144     void updateRoot();
0145 
0146 public Q_SLOTS:
0147     void addItem(const QString &title, uint line, uint column, int type, int level, uint startline, uint startcol,
0148                  const QString &pix, const QString &folder = "root");
0149     void slotConfigChanged();
0150 
0151 protected:
0152     virtual void contextMenuEvent(QContextMenuEvent *event) override;
0153 
0154 private:
0155     StructureViewItem* parentFor(int lev, const QString &fldr);
0156 
0157     void init();
0158     StructureViewItem* createFolder(const QString &folder);
0159     StructureViewItem* folder(const QString &folder);
0160 
0161     void saveState();
0162     bool shouldBeOpen(StructureViewItem *item, const QString &folder, int level);
0163 
0164 private:
0165     StructureWidget             *m_stack;
0166     KileDocument::Info          *m_docinfo;
0167     QMap<QString, StructureViewItem *>  m_folders;
0168     QMap<QString, bool>         m_openByTitle;
0169     QMap<uint, bool>            m_openByLine;
0170     QMap<QString, bool>         m_openByFolders;
0171     StructureViewItem           *m_parent[7], *m_root;
0172     QList<KileReferenceData> m_references;
0173     bool m_openStructureLabels;
0174     bool m_openStructureReferences;
0175     bool m_openStructureBibitems;
0176     bool m_openStructureTodo;
0177     bool m_showStructureLabels;
0178 
0179     int m_lastType;
0180     uint m_lastLine;
0181     StructureViewItem *m_lastSectioning;
0182     StructureViewItem *m_lastFloat;
0183     StructureViewItem *m_lastFrame;
0184     StructureViewItem *m_lastFrameEnv;
0185 
0186     bool m_stop;
0187 };
0188 
0189 class StructureWidget : public QStackedWidget
0190 {
0191     friend class StructureView;
0192 
0193     Q_OBJECT
0194 
0195 public:
0196     StructureWidget(KileInfo*, QWidget *parent, const char *name = Q_NULLPTR);
0197     ~StructureWidget();
0198 
0199     int level();
0200     KileInfo *info() {
0201         return m_ki;
0202     }
0203 
0204     bool findSectioning(StructureViewItem *item, KTextEditor::Document *doc, int row,
0205                         int col, bool backwards, bool checkLevel, int &sectRow, int &sectCol);
0206     void updateUrl(KileDocument::Info *docinfo);
0207 
0208     void updateAfterParsing(KileDocument::Info *info, const std::list<KileParser::StructureViewItem*>& items);
0209 
0210     enum { SectioningCut = 10, SectioningCopy = 11, SectioningPaste = 12,
0211            SectioningSelect = 13, SectioningDelete = 14,
0212            SectioningComment = 15,
0213            SectioningPreview = 16,
0214            SectioningGraphicsOther = 100, SectioningGraphicsOfferlist = 101
0215          };
0216 
0217 public Q_SLOTS:
0218     void slotClicked(QTreeWidgetItem *);
0219     void slotDoubleClicked(QTreeWidgetItem *);
0220 
0221     void addDocumentInfo(KileDocument::Info *);
0222     void closeDocumentInfo(KileDocument::Info *);
0223     void update(KileDocument::Info *);
0224     void update(KileDocument::Info *, bool);
0225     void clean(KileDocument::Info *);
0226     void updateReferences(KileDocument::Info *);
0227 
0228     /**
0229     * Clears the structure widget and empties the map between KileDocument::Info objects and their structure trees (QListViewItem).
0230     **/
0231     void clear();
0232 
0233 Q_SIGNALS:
0234     void sendText(const QString&);
0235     void setCursor(const QUrl&, int, int);
0236     void fileOpen(const QUrl&, const QString&);
0237     void fileNew(const QUrl&);
0238     void configChanged();
0239     void sectioningPopup(KileWidget::StructureViewItem *item, int id);
0240 
0241 protected:
0242     void viewContextMenuEvent(StructureView *view, QContextMenuEvent *event);
0243 
0244 private:
0245     KileInfo                            *m_ki;
0246     KileDocument::Info                      *m_docinfo;
0247     QMap<KileDocument::Info *, StructureView*>          m_map;
0248     StructureView                           *m_default;
0249     StructureViewItem *m_popupItem;
0250     QMenu *m_showingContextMenu;
0251     QString m_popupInfo;
0252     KService::List m_offerList;
0253 
0254     StructureView* viewFor(KileDocument::Info *info);
0255     bool viewExistsFor(KileDocument::Info *info);
0256 
0257     void slotPopupSectioning(int id);
0258     void slotPopupGraphics(int id);
0259 
0260 private Q_SLOTS:
0261     void handleDocumentParsingStarted();
0262     void handleDocumentParsingCompleted();
0263 };
0264 }
0265 
0266 #endif