Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / editors / sc-ide / core / doc_manager.hpp
blob93f3db25335880c9c26aaf73ec5c7a3a647ee6ad
1 /*
2 SuperCollider Qt IDE
3 Copyright (c) 2012 Jakob Leben & Tim Blechmann
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef SCIDE_DOC_MANAGER_HPP_INCLUDED
22 #define SCIDE_DOC_MANAGER_HPP_INCLUDED
24 #include "../widgets/code_editor/highlighter.hpp"
26 #include <QDateTime>
27 #include <QFileSystemWatcher>
28 #include <QHash>
29 #include <QList>
30 #include <QMetaType>
31 #include <QObject>
32 #include <QStringList>
33 #include <QTextDocument>
34 #include <QPlainTextDocumentLayout>
35 #include <QUuid>
37 namespace ScIDE {
39 namespace Settings { class Manager; }
41 class Main;
42 class DocumentManager;
44 class Document : public QObject
46 Q_OBJECT
48 friend class DocumentManager;
50 public:
51 Document();
53 QTextDocument *textDocument() { return mDoc; }
54 const QByteArray & id() { return mId; }
55 const QString & filePath() { return mFilePath; }
56 const QString & title() { return mTitle; }
58 QFont defaultFont() const { return mDoc->defaultFont(); }
59 void setDefaultFont( const QFont & font );
61 int indentWidth() const { return mIndentWidth; }
62 void setIndentWidth( int numSpaces );
64 void deleteTrailingSpaces();
66 public slots:
67 void applySettings( Settings::Manager * );
68 void resetDefaultFont();
70 signals:
71 void defaultFontChanged();
73 private:
74 QByteArray mId;
75 QTextDocument *mDoc;
76 QString mFilePath;
77 QString mTitle;
78 QDateTime mSaveTime;
79 int mIndentWidth;
82 class DocumentManager : public QObject
84 Q_OBJECT
86 public:
87 typedef QList< Document * > DocumentList;
89 DocumentManager( Main *, Settings::Manager * );
90 QList<Document*> documents() {
91 return mDocHash.values();
94 void create();
95 void close( Document * );
96 bool save( Document * );
97 bool saveAs( Document *, const QString & path );
98 bool reload( Document * );
99 const QStringList & recents() const { return mRecent; }
101 public slots:
102 // initialCursorPosition -1 means "don't change position if already open"
103 Document * open( const QString & path, int initialCursorPosition = -1, int selectionLength = 0, bool addToRecent = true );
104 void clearRecents();
105 void storeSettings( Settings::Manager * );
107 Q_SIGNALS:
108 void opened( Document *, int cursorPosition, int selectionLength );
109 void closed( Document * );
110 void saved( Document * );
111 void showRequest( Document *, int pos = -1, int selectionLength = 0 );
112 void changedExternally( Document * );
113 void recentsChanged();
115 private slots:
116 void onFileChanged( const QString & path );
118 private:
119 Document * createDocument();
120 bool doSaveAs( Document *, const QString & path );
121 void addToRecent( Document * );
122 void loadRecentDocuments( Settings::Manager * );
124 typedef QHash<QByteArray, Document*>::iterator DocIterator;
126 QHash<QByteArray, Document*> mDocHash;
127 QFileSystemWatcher mFsWatcher;
129 QStringList mRecent;
130 static const int mMaxRecent = 10;
133 } // namespace ScIDE
135 Q_DECLARE_METATYPE( ScIDE::Document* );
137 #endif // SCIDE_DOC_MANAGER_HPP_INCLUDED