summaryrefslogtreecommitdiffstats
path: root/src/docmanager.h
blob: c6b28ec766646e3f7d9f61bfcc667de1bcbf9427 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/***************************************************************************
 *   Copyright (C) 2005 by David Saxton                                    *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#ifndef DOCMANAGER_H
#define DOCMANAGER_H

#include <kurl.h>
#include <tqguardedptr.h>

class CircuitDocument;
class DocManager;
class DocManagerIface;
class Document;
class FlowCodeDocument;
class KTechlab;
class MechanicsDocument;
class TextDocument;
class View;
class ViewArea;

class TDEAction;

typedef TQValueList<Document*> DocumentList;
typedef TQMap< KURL, Document* > URLDocumentMap;
typedef TQValueList<TDEAction*> TDEActionList;

/**
@author David Saxton
*/
class DocManager : public TQObject
{
Q_OBJECT
  
public:
	static DocManager * self( KTechlab * ktechlab = 0l );
	~DocManager();
	
	/**
	 * Attempts to close all open documents, returning true if successful
	 */
	bool closeAll();
	/**
	 * Goes to the given line in the given text file (if the file exists)
	 */
	void gotoTextLine( const KURL &url, int line );
	/**
	 * Attempts to open the document at the given url.
	 * @param ViewArea if non-null, will open the new view into the ViewArea
	 */
	Document* openURL( const KURL &url, ViewArea *viewArea = 0l );
	/**
	 * Returns the focused View
	 */
	View *getFocusedView() const { return p_focusedView; }
	/**
	 * Returns the focused Document (the document of the focused view)
	 */
	Document *getFocusedDocument() const;
	/**
	 * Get a unique name, e.g. Untitled (circuit) - n" depending on the types
	 * of Document and whether it is the first one or not
	 * @param type Document::DocumentType - type of Document
	 */
	TQString untitledName( int type );
	/**
	 * Checks to see if a document with the given URL is already open, and
	 * returns a pointer to that Document if so - otherwises returns null
	 * @see associateDocument
	 */
	Document *findDocument( const KURL &url ) const;
	/**
	 * Associates a url with a pointer to a document. When findFile is called
	 * with the given url, it will return a pointer to this document if it still
	 * exists.
	 * @see findDocument
	 */
	void associateDocument( const KURL &url, Document *document );
	/**
	 * Gives the given document focus. If it has no open views, one will be
	 * created for it if viewAreaForNew is non-null
	 */
	void giveDocumentFocus( Document * toFocus, ViewArea * viewAreaForNew = 0l );
	void removeDocumentAssociations( Document *document );
	void disableContextActions();
	
public slots:
	/**
	 * Creates an empty text document (with an open view)
	 */
	TextDocument *createTextDocument();
	/**
	 * Creates an empty circuit document (with an open view), and shows the
	 * component selector.
	 */
	CircuitDocument *createCircuitDocument();
	/**
	 * Creates an empty flowcode document (with an open view), and shows the
	 * flowpart selector.
	 */
	FlowCodeDocument *createFlowCodeDocument();
	/**
	 * Creates an empty mechanics document (with an open view), and shows the
	 * mechanics selector.
	 */
	MechanicsDocument *createMechanicsDocument();
	
signals:
	/**
	 * Emitted when a file is successfully opened
	 */
	void fileOpened( const KURL &url );
	
protected slots:
	/**
	 * Does the appropriate enabling / disabling of actions, connections, etc
	 */
	void slotViewFocused( View *view );
	/**
	 * Does the appropriate enabling / disabling of actions, connections, etc
	 */
	void slotViewUnfocused();
	void documentDestroyed( TQObject *obj );
	
protected:
	/**
	 * This function should be called after creating a new document to add it
	 * to the appropriate lists and connect it up as appropriate
	 */
	void handleNewDocument( Document *document, ViewArea *viewArea = 0l );
	/**
	 * Takes the document, creates a new view and shoves it in a new
	 * ViewContainer
	 */
	View *createNewView( Document *document, ViewArea *viewArea = 0l );
	CircuitDocument *openCircuitFile( const KURL &url, ViewArea *viewArea = 0l );
	FlowCodeDocument *openFlowCodeFile( const KURL &url, ViewArea *viewArea = 0l );
	MechanicsDocument *openMechanicsFile( const KURL &url, ViewArea *viewArea = 0l );
	TextDocument *openTextFile( const KURL &url, ViewArea *viewArea = 0l );
	
	DocumentList m_documentList;
	URLDocumentMap m_associatedDocuments;
	
	// Keeps track of how many 
	// new files have been made
	// for the purpose of making 
	// titles of the form Untitled (n)
	int m_countCircuit;
	int m_countFlowCode;
	int m_countMechanics;
	int m_countOther;
	
	KTechlab * const p_ktechlab;
	TQGuardedPtr<View> p_focusedView;
	TQGuardedPtr<Document> p_connectedDocument;
	DocManagerIface *m_pIface;
	unsigned m_nextDocumentID;
	
private:
	DocManager( KTechlab *ktechlab );
	static DocManager * m_pSelf;
};

#endif