File indexing completed on 2024-12-08 11:07:09

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.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 
0011 #ifndef DOCMANAGER_H
0012 #define DOCMANAGER_H
0013 
0014 #include "view.h"
0015 
0016 #include <QPointer>
0017 #include <QUrl>
0018 
0019 class CircuitDocument;
0020 class DocManager;
0021 class DocManagerIface;
0022 class Document;
0023 class FlowCodeDocument;
0024 class KTechlab;
0025 class MechanicsDocument;
0026 class TextDocument;
0027 class View;
0028 class ViewArea;
0029 
0030 typedef QList<Document *> DocumentList;
0031 typedef QMap<QUrl, Document *> URLDocumentMap;
0032 
0033 /**
0034 @author David Saxton
0035 */
0036 class DocManager : public QObject
0037 {
0038     Q_OBJECT
0039 
0040     friend class KtlTestsAppFixture;
0041 
0042 public:
0043     static DocManager *self();
0044     ~DocManager() override;
0045 
0046     /**
0047      * Attempts to close all open documents, returning true if successful
0048      */
0049     bool closeAll();
0050     /**
0051      * Goes to the given line in the given text file (if the file exists)
0052      */
0053     void gotoTextLine(const QUrl &url, int line);
0054     /**
0055      * Attempts to open the document at the given url.
0056      * @param viewArea if non-null, will open the new view into the ViewArea
0057      */
0058     Document *openURL(const QUrl &url, ViewArea *viewArea = nullptr);
0059     /**
0060      * Returns the focused View
0061      */
0062     View *getFocusedView() const
0063     {
0064         return p_focusedView;
0065     }
0066     /**
0067      * Returns the focused Document (the document of the focused view)
0068      */
0069     Document *getFocusedDocument() const;
0070     /**
0071      * Get a unique name, e.g. Untitled (circuit) - n" depending on the types
0072      * of Document and whether it is the first one or not
0073      * @param type Document::DocumentType - type of Document
0074      */
0075     QString untitledName(int type);
0076     /**
0077      * Checks to see if a document with the given URL is already open, and
0078      * returns a pointer to that Document if so - otherwises returns null
0079      * @see associateDocument
0080      */
0081     Document *findDocument(const QUrl &url) const;
0082     /**
0083      * Associates a url with a pointer to a document. When findFile is called
0084      * with the given url, it will return a pointer to this document if it still
0085      * exists.
0086      * @see findDocument
0087      */
0088     void associateDocument(const QUrl &url, Document *document);
0089     /**
0090      * Gives the given document focus. If it has no open views, one will be
0091      * created for it if viewAreaForNew is non-null
0092      */
0093     void giveDocumentFocus(Document *toFocus, ViewArea *viewAreaForNew = nullptr);
0094     void removeDocumentAssociations(Document *document);
0095     void disableContextActions();
0096 
0097 public slots:
0098     /**
0099      * Creates an empty text document (with an open view)
0100      */
0101     TextDocument *createTextDocument();
0102     /**
0103      * Creates an empty circuit document (with an open view), and shows the
0104      * component selector.
0105      */
0106     CircuitDocument *createCircuitDocument();
0107     /**
0108      * Creates an empty flowcode document (with an open view), and shows the
0109      * flowpart selector.
0110      */
0111     FlowCodeDocument *createFlowCodeDocument();
0112     /**
0113      * Creates an empty mechanics document (with an open view), and shows the
0114      * mechanics selector.
0115      */
0116     MechanicsDocument *createMechanicsDocument();
0117 
0118 signals:
0119     /**
0120      * Emitted when a file is successfully opened
0121      */
0122     void fileOpened(const QUrl &url);
0123 
0124 protected slots:
0125     /**
0126      * Does the appropriate enabling / disabling of actions, connections, etc
0127      */
0128     void slotViewFocused(View *view);
0129     /**
0130      * Does the appropriate enabling / disabling of actions, connections, etc
0131      */
0132     void slotViewUnfocused();
0133     void documentDestroyed(QObject *obj);
0134 
0135 protected:
0136     /**
0137      * This function should be called after creating a new document to add it
0138      * to the appropriate lists and connect it up as appropriate
0139      */
0140     void handleNewDocument(Document *document, ViewArea *viewArea = nullptr);
0141     /**
0142      * Takes the document, creates a new view and shoves it in a new
0143      * ViewContainer
0144      */
0145     View *createNewView(Document *document, ViewArea *viewArea = nullptr);
0146     CircuitDocument *openCircuitFile(const QUrl &url, ViewArea *viewArea = nullptr);
0147     FlowCodeDocument *openFlowCodeFile(const QUrl &url, ViewArea *viewArea = nullptr);
0148     MechanicsDocument *openMechanicsFile(const QUrl &url, ViewArea *viewArea = nullptr);
0149     TextDocument *openTextFile(const QUrl &url, ViewArea *viewArea = nullptr);
0150 
0151     DocumentList m_documentList;
0152     URLDocumentMap m_associatedDocuments;
0153 
0154     // Keeps track of how many
0155     // new files have been made
0156     // for the purpose of making
0157     // titles of the form Untitled (n)
0158     int m_countCircuit;
0159     int m_countFlowCode;
0160     int m_countMechanics;
0161     int m_countOther;
0162 
0163     QPointer<View> p_focusedView;
0164     QPointer<Document> p_connectedDocument;
0165     DocManagerIface *m_pIface;
0166     unsigned m_nextDocumentID;
0167 
0168 private:
0169     DocManager();
0170     static DocManager *m_pSelf;
0171 };
0172 
0173 #endif