1 /*
2     SuperCollider Qt IDE
3     Copyright (c) 2012 Jakob Leben & Tim Blechmann
4     http://www.audiosynth.com
5 
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.
10 
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.
15 
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
19 */
20 
21 #pragma once
22 
23 #include <QLabel>
24 #include <QMainWindow>
25 #include <QProcess>
26 #include <QStatusBar>
27 
28 #include "util/status_box.hpp"
29 
30 namespace ScIDE {
31 
32 class Main;
33 class MultiEditor;
34 class ToolBox;
35 class TextFindReplacePanel;
36 class GoToLineTool;
37 class PostDocklet;
38 class DocumentsDocklet;
39 class HelpBrowserDocklet;
40 class CmdLine;
41 class Document;
42 class DocumentsDialog;
43 struct Session;
44 class ClockStatusBox;
45 class ScServer;
46 class ScProcess;
47 class GenericCodeEditor;
48 
49 namespace Settings {
50 class Manager;
51 }
52 
53 class MainWindow : public QMainWindow {
54     Q_OBJECT
55 
56 public:
57     enum ActionRole {
58         // File
59         Quit = 0,
60         DocNew,
61         DocOpen,
62         DocOpenStartup,
63         DocOpenSupportDir,
64         DocSave,
65         DocSaveAs,
66         DocSaveAsExtension,
67         DocSaveAll,
68         DocCloseAll,
69         DocReload,
70         ClearRecentDocs,
71 
72         // Sessions
73         NewSession,
74         SaveSessionAs,
75         ManageSessions,
76         OpenSessionSwitchDialog,
77 
78         // Edit
79         Find,
80         Replace,
81 
82         // View
83         ShowCmdLine,
84         CmdLineForCursor,
85         ShowGoToLineTool,
86         CloseToolBox,
87         ShowFullScreen,
88         FocusPostWindow,
89 
90         // Settings
91         ShowSettings,
92 
93         // Language
94         LookupImplementation,
95         LookupImplementationForCursor,
96         LookupReferences,
97         LookupReferencesForCursor,
98 
99         // Help
100         Help,
101         HelpAboutIDE,
102         ReportABug,
103 
104 #ifdef SC_USE_QTWEBENGINE
105         // These QtWebEngine-only actions are branched at the preprocessor
106         // level so that accidental invocations of these actions in other code
107         // are caught at compile time.
108         LookupDocumentationForCursor,
109         LookupDocumentation,
110 #endif // SC_USE_QTWEBENGINE
111 
112         ShowAbout,
113         ShowAboutQT,
114 
115         ActionCount
116     };
117 
118     explicit MainWindow(Main*);
119 
120     QAction* action(ActionRole);
121 
122     bool quit();
123 
124     void saveWindowState();
125     void restoreWindowState();
126     void focusCodeEditor();
127     bool promptSaveDocs();
128 
129 #ifdef SC_USE_QTWEBENGINE
130     HelpBrowserDocklet* helpBrowserDocklet() { return mHelpBrowserDocklet; }
131 #endif
132     PostDocklet* postDocklet() { return mPostDocklet; }
133 
134     static MainWindow* instance() { return mInstance; }
135     Settings::Manager* setting();
136 
137     static bool close(Document*);
138     static bool save(Document*, bool forceChoose = false, bool saveInExtensionFolder = false);
139     static bool reload(Document*);
140 
141     void restoreDocuments();
142 
143 public Q_SLOTS:
144     void newSession();
145     void saveCurrentSessionAs();
146     void openSessionsDialog();
147 
148     void newDocument();
149     void openDocument();
150     void saveDocument();
151     void saveDocumentAs();
152     void saveDocumentAsExtension();
153     void saveAllDocuments();
154     void reloadDocument();
155     void closeDocument();
156     void closeAllDocuments();
157 
158     void showCmdLine();
159     void showCmdLine(const QString&);
160     void showFindTool();
161     void showReplaceTool();
162     void showGoToLineTool();
163     void hideToolBox();
164 
165     void showSettings();
166 
167 signals:
168     void evaluateCode(const QString&, bool silent = true);
169 
170 public Q_SLOTS:
171     void showStatusMessage(QString const& string);
172 
173 private Q_SLOTS:
174     void openStartupFile();
175     void openUserSupportDirectory();
176 
177     void switchSession(Session* session);
178     void saveSession(Session* session);
179     void onInterpreterStateChanged(QProcess::ProcessState);
180     void onQuit();
181     void onCurrentDocumentChanged(Document*);
182     void onDocumentChangedExternally(Document*);
183     void onDocDialogFinished();
184     void updateRecentDocsMenu();
185     void onOpenRecentDocument(QAction*);
186     void onOpenSessionAction(QAction*);
187     void updateWindowTitle();
188     void toggleFullScreen();
189     void lookupImplementation();
190     void lookupImplementationForCursor();
191     void lookupReferences();
192     void lookupReferencesForCursor();
193     void openHelp();
194     void openHelpAboutIDE();
195     void doBugReport();
196     void lookupDocumentationForCursor();
197     void lookupDocumentation();
198     void applySettings(Settings::Manager*);
199     void storeSettings(Settings::Manager*);
200     void showSwitchSessionDialog();
201     void showAbout();
202     void showAboutQT();
203     void cmdLineForCursor();
204 
205 protected:
206     virtual void closeEvent(QCloseEvent* event);
207     virtual void dragEnterEvent(QDragEnterEvent*);
208     virtual void dropEvent(QDropEvent*);
209     virtual bool eventFilter(QObject*, QEvent*);
210 
211 private:
212     void createActions();
213     void createMenus();
214     template <class T> void saveWindowState(T* settings);
215     template <class T> void restoreWindowState(T* settings);
216     void updateSessionsMenu();
217     void updateClockWidget(bool isFullScreen);
218     void openSession(QString const& sessionName);
219     bool checkFileExtension(const QString& fpath);
220     void toggleInterpreterActions(bool enabled);
221     void applyCursorBlinkingSettings(Settings::Manager*);
222     QString documentOpenPath() const;
223     QString documentSavePath(Document*) const;
224 
225     Main* mMain;
226 
227     QAction* mActions[ActionCount];
228     QMenu* mRecentDocsMenu;
229     QMenu* mSessionsMenu;
230 
231     MultiEditor* mEditors;
232 
233     // Tools
234     ToolBox* mToolBox;
235     CmdLine* mCmdLine;
236     GoToLineTool* mGoToLineTool;
237     TextFindReplacePanel* mFindReplaceTool;
238 
239     // Status bar
240     QStatusBar* mStatusBar;
241     StatusBox* mLangStatus;
242     StatusBox* mServerStatus;
243     ClockStatusBox* mClockLabel;
244 
245     // Docks
246     PostDocklet* mPostDocklet;
247     DocumentsDocklet* mDocumentsDocklet;
248 #ifdef SC_USE_QTWEBENGINE
249     HelpBrowserDocklet* mHelpBrowserDocklet;
250 #endif
251     DocumentsDialog* mDocDialog;
252 
253     QString mLastDocumentSavePath;
254 
255     static MainWindow* mInstance;
256 };
257 
258 class ClockStatusBox : public StatusLabel {
259 public:
260     ClockStatusBox(QWidget* parent = 0);
261     ~ClockStatusBox();
262 
263 private:
264     void timerEvent(QTimerEvent*);
265     void updateTime();
266     int mTimerId;
267 };
268 
269 } // namespace ScIDE
270