scide: cancel ScRequests when recompiling the class library
[supercollider.git] / editors / sc-ide / widgets / editor_box.cpp
blob5d6b5f896f62d26a8d08f83b6c97fa11e1e54b4b
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 #include "editor_box.hpp"
22 #include "code_editor/editor.hpp"
23 #include "../../core/main.hpp"
25 #include <QPainter>
27 namespace ScIDE {
29 QPointer<CodeEditorBox> CodeEditorBox::gActiveBox;
31 CodeEditorBox::CodeEditorBox(QWidget *parent) :
32 QWidget(parent)
34 setFocusPolicy(Qt::StrongFocus);
35 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
37 mLayout = new QStackedLayout();
38 setLayout(mLayout);
40 connect(Main::documentManager(), SIGNAL(closed(Document*)),
41 this, SLOT(onDocumentClosed(Document*)));
44 void CodeEditorBox::setDocument(Document *doc, int pos)
46 if (!doc)
47 return;
49 CodeEditor *editor = currentEditor();
50 bool switchEditor = !editor || editor->document() != doc;
52 if (switchEditor)
54 editor = editorForDocument(doc);
55 if (!editor) {
56 editor = new CodeEditor(doc);
57 editor->installEventFilter(this);
58 mHistory.prepend(editor);
59 mLayout->addWidget(editor);
61 else {
62 mHistory.removeOne(editor);
63 mHistory.prepend(editor);
65 mLayout->setCurrentWidget(editor);
66 setFocusProxy(editor);
69 if (pos != -1)
70 editor->showPosition(pos);
72 if (switchEditor)
73 emit currentChanged(editor);
76 void CodeEditorBox::onDocumentClosed(Document *doc)
78 CodeEditor * editor = editorForDocument(doc);
79 if (editor) {
80 bool wasCurrent = editor == currentEditor();
81 mHistory.removeAll(editor);
82 delete editor;
83 if (wasCurrent) {
84 editor = currentEditor();
85 if (editor)
86 mLayout->setCurrentWidget(editor);
87 setFocusProxy(editor);
88 emit currentChanged(editor);
93 CodeEditor *CodeEditorBox::currentEditor()
95 if (mHistory.count())
96 return mHistory.first();
97 else
98 return 0;
101 int CodeEditorBox::historyIndexOf(Document *doc)
103 int count = mHistory.count();
104 for(int idx = 0; idx < count; ++idx)
105 if (mHistory[idx]->document() == doc)
106 return idx;
107 return -1;
110 CodeEditor *CodeEditorBox::editorForDocument(Document* doc)
112 foreach(CodeEditor *editor, mHistory)
113 if (editor->document() == doc)
114 return editor;
115 return 0;
118 bool CodeEditorBox::eventFilter( QObject *object, QEvent *event )
120 switch(event->type()) {
121 case QEvent::FocusIn:
122 setActive();
123 default:;
126 return QWidget::eventFilter(object, event);
129 void CodeEditorBox::focusInEvent( QFocusEvent * )
131 setActive();
134 Document * CodeEditorBox::currentDocument()
136 CodeEditor *editor = currentEditor();
137 return editor ? editor->document() : 0;
140 void CodeEditorBox::paintEvent( QPaintEvent * )
142 if (mLayout->currentWidget() == 0) {
143 QPainter painter(this);
144 painter.setRenderHint( QPainter::Antialiasing, true );
145 int colorRatio = isActive() ? 160 : 125;
146 painter.setBrush( palette().color(QPalette::Window).darker(colorRatio) );
147 painter.setPen( Qt::NoPen );
148 painter.drawRoundedRect( rect().adjusted(4, 4, -4, -4), 4, 4 );
152 } // namesapce ScIDE