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"
29 QPointer
<CodeEditorBox
> CodeEditorBox::gActiveBox
;
31 CodeEditorBox::CodeEditorBox(QWidget
*parent
) :
34 setFocusPolicy(Qt::StrongFocus
);
35 setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Expanding
);
37 mLayout
= new QStackedLayout();
40 connect(Main::documentManager(), SIGNAL(closed(Document
*)),
41 this, SLOT(onDocumentClosed(Document
*)));
44 void CodeEditorBox::setDocument(Document
*doc
, int pos
)
49 CodeEditor
*editor
= currentEditor();
50 bool switchEditor
= !editor
|| editor
->document() != doc
;
54 editor
= editorForDocument(doc
);
56 editor
= new CodeEditor(doc
);
57 editor
->installEventFilter(this);
58 mHistory
.prepend(editor
);
59 mLayout
->addWidget(editor
);
62 mHistory
.removeOne(editor
);
63 mHistory
.prepend(editor
);
65 mLayout
->setCurrentWidget(editor
);
66 setFocusProxy(editor
);
70 editor
->showPosition(pos
);
73 emit
currentChanged(editor
);
76 void CodeEditorBox::onDocumentClosed(Document
*doc
)
78 CodeEditor
* editor
= editorForDocument(doc
);
80 bool wasCurrent
= editor
== currentEditor();
81 mHistory
.removeAll(editor
);
84 editor
= currentEditor();
86 mLayout
->setCurrentWidget(editor
);
87 setFocusProxy(editor
);
88 emit
currentChanged(editor
);
93 CodeEditor
*CodeEditorBox::currentEditor()
96 return mHistory
.first();
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
)
110 CodeEditor
*CodeEditorBox::editorForDocument(Document
* doc
)
112 foreach(CodeEditor
*editor
, mHistory
)
113 if (editor
->document() == doc
)
118 bool CodeEditorBox::eventFilter( QObject
*object
, QEvent
*event
)
120 switch(event
->type()) {
121 case QEvent::FocusIn
:
126 return QWidget::eventFilter(object
, event
);
129 void CodeEditorBox::focusInEvent( QFocusEvent
* )
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 );