scide: implement selectionLength for openDocument
[supercollider.git] / editors / sc-ide / core / doc_manager.hpp
blob2264c323038bf464afdf7745e93f981af9f2d669
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 = -1, bool addToRecent = true );
104 void clearRecents();
105 void storeSettings( Settings::Manager * );
107 Q_SIGNALS:
109 void opened( Document *, int cursorPosition, int selectionLength );
110 void closed( Document * );
111 void saved( Document * );
112 void showRequest( Document *, int pos = -1 );
113 void changedExternally( Document * );
114 void recentsChanged();
116 private slots:
117 void onFileChanged( const QString & path );
119 private:
120 Document * createDocument();
121 bool doSaveAs( Document *, const QString & path );
122 void addToRecent( Document * );
123 void loadRecentDocuments( Settings::Manager * );
125 typedef QHash<QByteArray, Document*>::iterator DocIterator;
127 QHash<QByteArray, Document*> mDocHash;
128 QFileSystemWatcher mFsWatcher;
130 QStringList mRecent;
131 static const int mMaxRecent = 10;
134 } // namespace ScIDE
136 Q_DECLARE_METATYPE( ScIDE::Document* );
138 #endif // SCIDE_DOC_MANAGER_HPP_INCLUDED