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 "multi_editor.hpp"
22 #include "editor_box.hpp"
23 #include "main_window.hpp"
24 #include "lookup_dialog.hpp"
25 #include "code_editor/editor.hpp"
26 #include "util/multi_splitter.hpp"
27 #include "../core/doc_manager.hpp"
28 #include "../core/sig_mux.hpp"
29 #include "../core/main.hpp"
30 #include "../core/sc_process.hpp"
31 #include "../core/session_manager.hpp"
33 #include "yaml-cpp/node.h"
34 #include "yaml-cpp/parser.h"
36 #include <QApplication>
38 #include <QHBoxLayout>
39 #include <QVBoxLayout>
46 #include <QHeaderView>
48 #include <QTreeWidget>
49 #include <QStandardItemModel>
54 class DocumentSelectPopUp
: public QDialog
57 explicit DocumentSelectPopUp(const CodeEditorBox::History
& history
, QWidget
* parent
):
58 QDialog(parent
, Qt::Popup
)
60 mModel
= new QStandardItemModel(this);
61 populateModel(history
);
63 mListView
= new QListView();
64 mListView
->setModel(mModel
);
65 mListView
->setFrameShape(QFrame::NoFrame
);
67 QHBoxLayout
*layout
= new QHBoxLayout(this);
68 layout
->addWidget(mListView
);
69 layout
->setContentsMargins(1,1,1,1);
71 connect(mListView
, SIGNAL(activated(QModelIndex
)), this, SLOT(accept()));
73 mListView
->setFocus(Qt::OtherFocusReason
);
75 QModelIndex nextIndex
= mModel
->index(1, 0);
76 mListView
->setCurrentIndex(nextIndex
);
78 mListView
->setEditTriggers(QAbstractItemView::NoEditTriggers
);
81 Document
* exec( const QPoint
& pos
)
85 return currentDocument();
91 void keyReleaseEvent (QKeyEvent
* ke
)
93 // adapted from qtcreator
94 if (ke
->modifiers() == 0
95 /*HACK this is to overcome some event inconsistencies between platforms*/
96 || (ke
->modifiers() == Qt::AltModifier
97 && (ke
->key() == Qt::Key_Alt
|| ke
->key() == -1))) {
101 QDialog::keyPressEvent(ke
);
104 void keyPressEvent(QKeyEvent
* ke
)
108 int row
= mListView
->currentIndex().row() + 1;
109 if (!mModel
->hasIndex(row
, 0))
112 QModelIndex nextIndex
= mModel
->index(row
, 0);
113 mListView
->setCurrentIndex(nextIndex
);
118 case Qt::Key_Backtab
: {
119 int row
= mListView
->currentIndex().row() - 1;
120 if (!mModel
->hasIndex(row
, 0))
121 row
= mModel
->rowCount() - 1;
123 QModelIndex nextIndex
= mModel
->index(row
, 0);
124 mListView
->setCurrentIndex(nextIndex
);
137 QDialog::keyPressEvent(ke
);
140 Document
* currentDocument()
142 QStandardItem
* currentItem
= mModel
->itemFromIndex(mListView
->currentIndex());
144 return currentItem
? currentItem
->data().value
<Document
*>()
148 void populateModel( const CodeEditorBox::History
& history
)
150 QList
<Document
*> displayDocuments
;
151 foreach(CodeEditor
*editor
, history
)
152 displayDocuments
<< editor
->document();
154 QList
<Document
*> managerDocuments
= Main::instance()->documentManager()->documents();
155 foreach(Document
*document
, managerDocuments
)
156 if (!displayDocuments
.contains(document
))
157 displayDocuments
<< document
;
159 foreach (Document
* document
, displayDocuments
) {
160 QStandardItem
* item
= new QStandardItem(document
->title());
161 item
->setData(QVariant::fromValue(document
));
162 mModel
->appendRow(item
);
166 QListView
*mListView
;
167 QStandardItemModel
*mModel
;
170 MultiEditor::MultiEditor( Main
*main
, QWidget
* parent
) :
172 mDocManager(main
->documentManager()),
173 mSigMux(new SignalMultiplexer(this)),
174 mBoxSigMux(new SignalMultiplexer(this)),
175 mDocModifiedIcon( QApplication::style()->standardIcon(QStyle::SP_DialogSaveButton
) )
178 mTabs
->setDocumentMode(true);
179 mTabs
->setTabsClosable(true);
180 mTabs
->setMovable(true);
181 mTabs
->setUsesScrollButtons(true);
182 mTabs
->setDrawBase(false);
184 CodeEditorBox
*defaultBox
= newBox();
186 mSplitter
= new MultiSplitter();
187 mSplitter
->addWidget(defaultBox
);
189 QVBoxLayout
*l
= new QVBoxLayout
;
190 l
->setContentsMargins(0,0,0,0);
193 l
->addWidget(mSplitter
);
196 connect(mDocManager
, SIGNAL(opened(Document
*, int)),
197 this, SLOT(onOpen(Document
*, int)));
198 connect(mDocManager
, SIGNAL(closed(Document
*)),
199 this, SLOT(onClose(Document
*)));
200 connect(mDocManager
, SIGNAL(saved(Document
*)),
201 this, SLOT(update(Document
*)));
202 connect(mDocManager
, SIGNAL(showRequest(Document
*,int)),
203 this, SLOT(show(Document
*,int))),
205 connect(mTabs
, SIGNAL(currentChanged(int)),
206 this, SLOT(onCurrentTabChanged(int)));
207 connect(mTabs
, SIGNAL(tabCloseRequested(int)),
208 this, SLOT(onCloseRequest(int)));
210 mSigMux
->connect(SIGNAL(modificationChanged(bool)),
211 this, SLOT(onModificationChanged(bool)));
213 mBoxSigMux
->connect(SIGNAL(currentChanged(CodeEditor
*)),
214 this, SLOT(onCurrentEditorChanged(CodeEditor
*)));
216 connect(this, SIGNAL(currentDocumentChanged(Document
*)), mDocManager
, SLOT(activeDocumentChanged(Document
*)));
220 setCurrentBox( defaultBox
); // will updateActions();
222 applySettings(main
->settings());
225 void MultiEditor::createActions()
227 Settings::Manager
*settings
= Main::instance()->settings();
228 settings
->beginGroup("IDE/shortcuts");
234 mActions
[Undo
] = act
= new QAction(
235 QIcon::fromTheme("edit-undo"), tr("&Undo"), this);
236 act
->setShortcut(tr("Ctrl+Z", "Undo"));
237 act
->setStatusTip(tr("Undo last editing action"));
238 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(undo()));
239 mSigMux
->connect(SIGNAL(undoAvailable(bool)), act
, SLOT(setEnabled(bool)));
241 mActions
[Redo
] = act
= new QAction(
242 QIcon::fromTheme("edit-redo"), tr("Re&do"), this);
243 act
->setShortcut(tr("Ctrl+Shift+Z", "Redo"));
244 act
->setStatusTip(tr("Redo next editing action"));
245 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(redo()));
246 mSigMux
->connect(SIGNAL(redoAvailable(bool)), act
, SLOT(setEnabled(bool)));
248 mActions
[Cut
] = act
= new QAction(
249 QIcon::fromTheme("edit-cut"), tr("Cu&t"), this);
250 act
->setShortcut(tr("Ctrl+X", "Cut"));
251 act
->setStatusTip(tr("Cut text to clipboard"));
252 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(cut()));
253 mSigMux
->connect(SIGNAL(copyAvailable(bool)), act
, SLOT(setEnabled(bool)));
255 mActions
[Copy
] = act
= new QAction(
256 QIcon::fromTheme("edit-copy"), tr("&Copy"), this);
257 act
->setShortcut(tr("Ctrl+C", "Copy"));
258 act
->setStatusTip(tr("Copy text to clipboard"));
259 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(copy()));
260 mSigMux
->connect(SIGNAL(copyAvailable(bool)), act
, SLOT(setEnabled(bool)));
262 mActions
[Paste
] = act
= new QAction(
263 QIcon::fromTheme("edit-paste"), tr("&Paste"), this);
264 act
->setShortcut(tr("Ctrl+V", "Paste"));
265 act
->setStatusTip(tr("Paste text from clipboard"));
266 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(paste()));
268 mActions
[IndentLineOrRegion
] = act
= new QAction(
269 QIcon::fromTheme("format-indent-line"), tr("Indent Line or Region"), this);
270 act
->setStatusTip(tr("Indent Line or Region"));
271 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(indent()));
273 mActions
[TriggerAutoCompletion
] = act
= new QAction(tr("Trigger Autocompletion"), this);
274 act
->setStatusTip(tr("Suggest possible completions of text at cursor"));
275 act
->setShortcut(tr("Ctrl+Space", "Trigger Autocompletion"));
276 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(triggerAutoCompletion()));
278 mActions
[TriggerMethodCallAid
] = act
= new QAction(tr("Trigger Method Call Aid"), this);
279 act
->setStatusTip(tr("Show arguments for currently typed method call"));
280 act
->setShortcut(tr("Alt+Space", "Trigger Method Call Aid"));
281 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(triggerMethodCallAid()));
283 mActions
[ToggleComment
] = act
= new QAction(
284 QIcon::fromTheme("edit-comment"), tr("Toggle &Comment"), this);
285 act
->setShortcut(tr("Ctrl+/", "Toggle Comment"));
286 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
287 act
->setStatusTip(tr("Toggle Comment"));
288 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(toggleComment()));
290 mActions
[ToggleOverwriteMode
] = act
= new QAction(
291 QIcon::fromTheme("edit-overwrite"), tr("Toggle &Overwrite Mode"), this);
292 act
->setShortcut(tr("Insert", "Toggle Overwrite Mode"));
293 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
294 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(toggleOverwriteMode()));
296 mActions
[CopyLineUp
] = act
= new QAction(
297 QIcon::fromTheme("edit-copylineup"), tr("Copy Line Up"), this);
298 act
->setShortcut(tr("Ctrl+Alt+Up", "Copy Line Up"));
299 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
300 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(copyLineUp()));
302 mActions
[CopyLineDown
] = act
= new QAction(
303 QIcon::fromTheme("edit-copylinedown"), tr("Copy Line Down"), this);
304 act
->setShortcut(tr("Ctrl+Alt+Down", "Copy Line Up"));
305 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
306 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(copyLineDown()));
308 mActions
[MoveLineUp
] = act
= new QAction(
309 QIcon::fromTheme("edit-movelineup"), tr("move Line Up"), this);
310 act
->setShortcut(tr("Ctrl+Shift+Up", "Move Line Up"));
311 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
312 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(moveLineUp()));
314 mActions
[MoveLineDown
] = act
= new QAction(
315 QIcon::fromTheme("edit-movelinedown"), tr("Move Line Down"), this);
316 act
->setShortcut(tr("Ctrl+Shift+Down", "Move Line Up"));
317 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
318 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(moveLineDown()));
320 mActions
[GotoPreviousBlock
] = act
= new QAction(
321 QIcon::fromTheme("edit-gotopreviousblock"), tr("Go to Previous Block"), this);
322 act
->setShortcut(tr("Ctrl+[", "Go to Previous Block"));
323 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
324 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(gotoPreviousBlock()));
326 mActions
[GotoNextBlock
] = act
= new QAction(
327 QIcon::fromTheme("edit-gotonextblock"), tr("Go to Next Block"), this);
328 act
->setShortcut(tr("Ctrl+]", "Go to Next Block"));
329 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
330 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(gotoNextBlock()));
334 mActions
[EnlargeFont
] = act
= new QAction(
335 QIcon::fromTheme("zoom-in"), tr("&Enlarge Font"), this);
336 act
->setShortcut(tr("Ctrl++", "Enlarge font"));
337 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
338 act
->setStatusTip(tr("Increase displayed font size"));
339 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(zoomIn()));
341 mActions
[ShrinkFont
] = act
= new QAction(
342 QIcon::fromTheme("zoom-out"), tr("&Shrink Font"), this);
343 act
->setShortcut( tr("Ctrl+-", "Shrink font"));
344 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
345 act
->setStatusTip(tr("Decrease displayed font size"));
346 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(zoomOut()));
348 mActions
[ResetFontSize
] = act
= new QAction(
349 QIcon::fromTheme("zoom-reset"), tr("&Reset Font Size"), this);
350 act
->setShortcut( tr("Ctrl+0", "Reset font"));
351 act
->setStatusTip(tr("Reset displayed font size"));
352 mSigMux
->connect(act
, SIGNAL(triggered()), SLOT(resetFontSize()));
354 mActions
[ShowWhitespace
] = act
= new QAction(tr("Show Spaces and Tabs"), this);
355 act
->setCheckable(true);
356 mSigMux
->connect(act
, SIGNAL(triggered(bool)), SLOT(setShowWhitespace(bool)));
358 mActions
[NextDocument
] = act
= new QAction(tr("Next Document"), this);
359 act
->setShortcut( tr("Alt+Right", "Next Document"));
360 connect(act
, SIGNAL(triggered()), this, SLOT(showNextDocument()));
362 mActions
[PreviousDocument
] = act
= new QAction(tr("Previous Document"), this);
363 act
->setShortcut( tr("Alt+Left", "Next Document"));
364 connect(act
, SIGNAL(triggered()), this, SLOT(showPreviousDocument()));
366 mActions
[SwitchDocument
] = act
= new QAction(tr("Switch Document"), this);
367 act
->setShortcut( tr("Ctrl+Tab", "Switch Document"));
368 connect(act
, SIGNAL(triggered()), this, SLOT(switchDocument()));
370 mActions
[SplitHorizontally
] = act
= new QAction(tr("Split To Right"), this);
371 connect(act
, SIGNAL(triggered()), this, SLOT(splitHorizontally()));
373 mActions
[SplitVertically
] = act
= new QAction(tr("Split To Bottom"), this);
374 connect(act
, SIGNAL(triggered()), this, SLOT(splitVertically()));
376 mActions
[RemoveCurrentSplit
] = act
= new QAction(tr("Remove Current Split"), this);
377 connect(act
, SIGNAL(triggered()), this, SLOT(removeCurrentSplit()));
379 mActions
[RemoveAllSplits
] = act
= new QAction(tr("Remove All Splits"), this);
380 connect(act
, SIGNAL(triggered()), this, SLOT(removeAllSplits()));
384 mActions
[EvaluateCurrentDocument
] = act
= new QAction(
385 QIcon::fromTheme("media-playback-start"), tr("Evaluate &File"), this);
386 act
->setStatusTip(tr("Evaluate current File"));
387 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
388 connect(act
, SIGNAL(triggered()), this, SLOT(evaluateDocument()));
390 mActions
[EvaluateRegion
] = act
= new QAction(
391 QIcon::fromTheme("media-playback-start"), tr("&Evaluate Selection, Line or Region"), this);
392 act
->setShortcut(tr("Ctrl+Return", "Evaluate region"));
393 act
->setStatusTip(tr("Evaluate current region"));
394 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
395 connect(act
, SIGNAL(triggered()), this, SLOT(evaluateRegion()));
397 mActions
[EvaluateLine
] = act
= new QAction(
398 QIcon::fromTheme("media-playback-startline"), tr("&Evaluate Line"), this);
399 act
->setShortcut(tr("Shift+Ctrl+Return", "Evaluate line"));
400 act
->setStatusTip(tr("Evaluate current line"));
401 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
402 connect(act
, SIGNAL(triggered()), this, SLOT(evaluateLine()));
404 mActions
[OpenDefinition
] = act
= new QAction(tr("Open Class/Method Definition"), this);
405 act
->setShortcut(tr("Ctrl+I", "Open definition of selected class or method"));
406 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
407 connect(act
, SIGNAL(triggered(bool)), this, SLOT(openDefinition()));
409 settings
->endGroup(); // IDE/shortcuts
411 for (int i
= 0; i
< ActionRoleCount
; ++i
)
412 settings
->addAction( mActions
[i
] );
414 // These actions are not added to any menu, so they have to be added
415 // at least to this widget, in order for the shortcuts to always respond:
416 addAction(mActions
[TriggerAutoCompletion
]);
417 addAction(mActions
[TriggerMethodCallAid
]);
418 addAction(mActions
[SwitchDocument
]);
420 // These actions have to be added because to the widget because they have
421 // Qt::WidgetWithChildrenShortcut context:
422 addAction(mActions
[EnlargeFont
]);
423 addAction(mActions
[ShrinkFont
]);
424 addAction(mActions
[OpenDefinition
]);
425 addAction(mActions
[EvaluateCurrentDocument
]);
426 addAction(mActions
[EvaluateRegion
]);
427 addAction(mActions
[EvaluateLine
]);
428 addAction(mActions
[ToggleComment
]);
429 addAction(mActions
[ToggleOverwriteMode
]);
430 addAction(mActions
[CopyLineUp
]);
431 addAction(mActions
[CopyLineDown
]);
432 addAction(mActions
[MoveLineUp
]);
433 addAction(mActions
[MoveLineDown
]);
434 addAction(mActions
[GotoPreviousBlock
]);
435 addAction(mActions
[GotoNextBlock
]);
438 void MultiEditor::updateActions()
440 CodeEditor
*editor
= currentEditor();
441 QTextDocument
*doc
= editor
? editor
->textDocument() : 0;
443 mActions
[Undo
]->setEnabled( doc
&& doc
->isUndoAvailable() );
444 mActions
[Redo
]->setEnabled( doc
&& doc
->isRedoAvailable() );
445 mActions
[Copy
]->setEnabled( editor
&& editor
->textCursor().hasSelection() );
446 mActions
[Cut
]->setEnabled( mActions
[Copy
]->isEnabled() );
447 mActions
[Paste
]->setEnabled( editor
);
448 mActions
[ToggleComment
]->setEnabled( editor
);
449 mActions
[ToggleOverwriteMode
]->setEnabled( editor
);
450 mActions
[CopyLineUp
]->setEnabled( editor
);
451 mActions
[CopyLineDown
]->setEnabled( editor
);
452 mActions
[MoveLineUp
]->setEnabled( editor
);
453 mActions
[MoveLineDown
]->setEnabled( editor
);
454 mActions
[GotoPreviousBlock
]->setEnabled( editor
);
455 mActions
[GotoNextBlock
]->setEnabled( editor
);
457 mActions
[IndentLineOrRegion
]->setEnabled( editor
);
458 mActions
[EnlargeFont
]->setEnabled( editor
);
459 mActions
[ShrinkFont
]->setEnabled( editor
);
460 mActions
[OpenDefinition
]->setEnabled( editor
);
461 mActions
[EvaluateCurrentDocument
]->setEnabled( editor
);
462 mActions
[EvaluateRegion
]->setEnabled( editor
);
463 mActions
[EvaluateLine
]->setEnabled( editor
);
464 mActions
[ResetFontSize
]->setEnabled( editor
);
465 mActions
[ShowWhitespace
]->setEnabled( editor
);
466 mActions
[ShowWhitespace
]->setChecked( editor
&& editor
->showWhitespace() );
469 void MultiEditor::applySettings( Settings::Manager
*s
)
471 s
->beginGroup("IDE/editor");
472 mStepForwardEvaluation
= s
->value("stepForwardEvaluation").toBool();
476 static QVariantList
saveBoxState( CodeEditorBox
*box
)
478 // save editors in reverse order - first one is last shown.
479 QVariantList boxData
;
480 int idx
= box
->history().count();
482 CodeEditor
*editor
= box
->history()[idx
];
483 if (!editor
->document()->filePath().isEmpty()) {
484 QVariantMap editorData
;
485 editorData
.insert("file", editor
->document()->filePath());
486 editorData
.insert("position", editor
->textCursor().position());
487 boxData
.append( editorData
);
493 static QVariantMap
saveSplitterState( QSplitter
*splitter
)
495 QVariantMap splitterData
;
497 splitterData
.insert( "state", splitter
->saveState().toBase64() );
499 QVariantList childrenData
;
501 int childCount
= splitter
->count();
502 for (int idx
= 0; idx
< childCount
; idx
++) {
503 QWidget
*child
= splitter
->widget(idx
);
505 CodeEditorBox
*box
= qobject_cast
<CodeEditorBox
*>(child
);
507 QVariantList boxData
= saveBoxState(box
);
508 childrenData
.append( QVariant(boxData
) );
512 QSplitter
*childSplitter
= qobject_cast
<QSplitter
*>(child
);
514 QVariantMap childSplitterData
= saveSplitterState(childSplitter
);
515 childrenData
.append( QVariant(childSplitterData
) );
519 splitterData
.insert( "elements", childrenData
);
524 void MultiEditor::saveSession( Session
*session
)
526 session
->remove( "editors" );
527 session
->setValue( "editors", saveSplitterState(mSplitter
) );
530 void MultiEditor::loadBoxState( CodeEditorBox
*box
, const QVariantList
& data
)
532 mCurrentEditorBox
= box
;
533 foreach( QVariant docVar
, data
)
535 QVariantMap docData
= docVar
.value
<QVariantMap
>();
536 QString docPath
= docData
.value("file").toString();
537 int docPos
= docData
.value("position").toInt();
538 Main::instance()->documentManager()->open( docPath
, docPos
);
542 void MultiEditor::loadSplitterState( QSplitter
*splitter
, const QVariantMap
& data
)
544 QByteArray state
= QByteArray::fromBase64( data
.value("state").value
<QByteArray
>() );
546 QVariantList childrenData
= data
.value("elements").value
<QVariantList
>();
547 foreach (const QVariant
& childVar
, childrenData
) {
548 if (childVar
.type() == QVariant::List
) {
549 CodeEditorBox
*childBox
= newBox();
550 splitter
->addWidget(childBox
);
551 QVariantList childBoxData
= childVar
.value
<QVariantList
>();
552 loadBoxState( childBox
, childBoxData
);
554 else if (childVar
.type() == QVariant::Map
) {
555 QSplitter
*childSplitter
= new QSplitter
;
556 splitter
->addWidget(childSplitter
);
557 QVariantMap childSplitterData
= childVar
.value
<QVariantMap
>();
558 loadSplitterState( childSplitter
, childSplitterData
);
562 if (!splitter
->restoreState(state
))
563 qWarning("MultiEditor: could not restore splitter state!");
566 void MultiEditor::switchSession( Session
*session
)
568 DocumentManager
*docManager
= Main::instance()->documentManager();
569 QList
<Document
*> docs
= docManager
->documents();
570 foreach (Document
*doc
, docs
)
571 docManager
->close(doc
);
574 mSplitter
= new MultiSplitter();
576 if (session
&& session
->contains("editors")) {
577 QVariantMap splitterData
= session
->value("editors").value
<QVariantMap
>();
578 loadSplitterState( mSplitter
, splitterData
);
581 CodeEditorBox
*firstBox
= 0;
583 if (mSplitter
->count()) {
584 firstBox
= mSplitter
->findChild
<CodeEditorBox
>();
586 qWarning("Session seems to contain invalid editor split data!");
588 mSplitter
= new MultiSplitter();
594 mSplitter
->addWidget( firstBox
);
597 mCurrentEditorBox
= 0; // ensure complete update
598 setCurrentBox( firstBox
);
600 layout()->addWidget(mSplitter
);
602 firstBox
->setFocus(Qt::OtherFocusReason
); // ensure focus
605 void MultiEditor::setCurrent( Document
*doc
)
607 int tabIdx
= tabForDocument(doc
);
609 mTabs
->setCurrentIndex(tabIdx
);
612 void MultiEditor::showNextDocument()
614 int currentIndex
= mTabs
->currentIndex();
615 mTabs
->setCurrentIndex( qMin(currentIndex
+ 1, mTabs
->count() - 1) );
618 void MultiEditor::showPreviousDocument()
620 int currentIndex
= mTabs
->currentIndex();
621 mTabs
->setCurrentIndex( qMax(0, currentIndex
- 1) );
624 void MultiEditor::switchDocument()
626 CodeEditorBox
*box
= currentBox();
628 DocumentSelectPopUp
* popup
= new DocumentSelectPopUp(box
->history(), this);
630 QRect
popupRect(0,0,300,200);
631 popupRect
.moveCenter(rect().center());
632 popup
->resize(popupRect
.size());
633 QPoint globalPosition
= mapToGlobal(popupRect
.topLeft());
635 Document
* selectedDocument
= popup
->exec(globalPosition
);
637 if (selectedDocument
)
638 box
->setDocument(selectedDocument
);
641 void MultiEditor::onOpen( Document
*doc
, int pos
)
643 QTextDocument
*tdoc
= doc
->textDocument();
646 if(tdoc
->isModified())
647 icon
= mDocModifiedIcon
;
649 int newTabIndex
= mTabs
->addTab( icon
, doc
->title() );
650 mTabs
->setTabData( newTabIndex
, QVariant::fromValue
<Document
*>(doc
) );
652 currentBox()->setDocument(doc
, pos
);
653 currentBox()->setFocus(Qt::OtherFocusReason
);
656 void MultiEditor::onClose( Document
*doc
)
658 int tabIdx
= tabForDocument(doc
);
660 mTabs
->removeTab(tabIdx
);
661 // TODO: each box should switch document according to their own history
664 void MultiEditor::show( Document
*doc
, int pos
)
666 currentBox()->setDocument(doc
, pos
);
667 currentBox()->setFocus(Qt::OtherFocusReason
);
670 void MultiEditor::update( Document
*doc
)
672 int tabIdx
= tabForDocument(doc
);
674 mTabs
->setTabText(tabIdx
, doc
->title());
677 void MultiEditor::onCloseRequest( int index
)
679 Document
*doc
= documentForTab(index
);
681 MainWindow::close(doc
);
684 void MultiEditor::onCurrentTabChanged( int index
)
689 Document
*doc
= documentForTab(index
);
693 CodeEditorBox
*curBox
= currentBox();
694 curBox
->setDocument(doc
);
695 curBox
->setFocus(Qt::OtherFocusReason
);
698 void MultiEditor::onCurrentEditorChanged(CodeEditor
*editor
)
700 setCurrentEditor(editor
);
703 void MultiEditor::onBoxActivated(CodeEditorBox
*box
)
708 void MultiEditor::onModificationChanged( bool modified
)
710 Q_ASSERT(currentEditor());
712 int tabIdx
= tabForDocument( currentEditor()->document() );
718 icon
= mDocModifiedIcon
;
719 mTabs
->setTabIcon( tabIdx
, icon
);
722 void MultiEditor::evaluateRegion()
724 CodeEditor
* editor
= currentEditor();
730 // Try current selection
731 QTextCursor cursor
= editor
->textCursor();
732 if (cursor
.hasSelection())
733 text
= cursor
.selectedText();
735 // If no selection, try current region
736 cursor
= editor
->currentRegion();
737 if (!cursor
.isNull()) {
738 // if region is in a single line, evaluate complete line
740 QTextCursor
selectionStart (cursor
);
741 selectionStart
.setPosition(cursor
.selectionStart());
743 QTextCursor
selectionEnd (cursor
);
744 selectionEnd
.setPosition(cursor
.selectionEnd());
746 if (selectionStart
.block() == selectionEnd
.block())
747 cursor
.select(QTextCursor::LineUnderCursor
);
749 text
= cursor
.selectedText();
751 // If no current region, try current line
752 cursor
= editor
->textCursor();
753 text
= cursor
.block().text();
754 if( mStepForwardEvaluation
) {
755 QTextCursor newCursor
= cursor
;
756 newCursor
.movePosition(QTextCursor::NextBlock
);
757 newCursor
.movePosition(QTextCursor::EndOfBlock
);
758 editor
->setTextCursor(newCursor
);
760 // Adjust cursor for code blinking:
761 cursor
.movePosition(QTextCursor::StartOfBlock
);
762 cursor
.movePosition(QTextCursor::EndOfBlock
, QTextCursor::KeepAnchor
);
769 text
.replace( QChar( 0x2029 ), QChar( '\n' ) );
771 Main::evaluateCode(text
);
773 editor
->blinkCode( cursor
);
776 void MultiEditor::evaluateLine()
778 CodeEditor
* editor
= currentEditor();
784 // Try current selection
785 QTextCursor cursor
= editor
->textCursor();
786 cursor
.select(QTextCursor::LineUnderCursor
);
787 text
= cursor
.selectedText();
789 if( mStepForwardEvaluation
) {
790 QTextCursor newCursor
= cursor
;
791 newCursor
.movePosition(QTextCursor::NextBlock
);
792 newCursor
.movePosition(QTextCursor::EndOfBlock
);
793 editor
->setTextCursor(newCursor
);
799 // Adjust cursor for code blinking:
800 cursor
.movePosition(QTextCursor::StartOfBlock
);
801 cursor
.movePosition(QTextCursor::EndOfBlock
, QTextCursor::KeepAnchor
);
803 text
.replace( QChar( 0x2029 ), QChar( '\n' ) );
805 Main::evaluateCode(text
);
807 editor
->blinkCode( cursor
);
810 void MultiEditor::evaluateDocument()
812 CodeEditor
* editor
= currentEditor();
816 QString documentText
= editor
->textDocument()->toPlainText();
817 Main::evaluateCode(documentText
);
820 void MultiEditor::openDefinition(const QString
&string
)
822 QString definitionString
= string
.trimmed();
823 if (definitionString
.isEmpty())
826 LookupDialog
dialog(this);
827 dialog
.query(definitionString
);
831 void MultiEditor::openDefinition()
833 CodeEditor
* editor
= currentEditor();
834 QTextCursor textCursor
= editor
->textCursor();
836 if (!textCursor
.hasSelection())
837 textCursor
.select(QTextCursor::WordUnderCursor
);
839 openDefinition(textCursor
.selectedText());
842 bool MultiEditor::openDocumentation(const QString
&string
)
844 QString symbol
= string
.trimmed();
845 if (symbol
.isEmpty())
848 QString code
= QString("HelpBrowser.openHelpFor(\"%1\")").arg(symbol
);
849 Main::evaluateCode(code
, true);
853 bool MultiEditor::openDocumentation()
855 CodeEditor
* editor
= currentEditor();
859 QTextCursor textCursor
= editor
->textCursor();
861 if (!textCursor
.hasSelection())
862 textCursor
.select(QTextCursor::WordUnderCursor
);
864 return openDocumentation(textCursor
.selectedText());
867 Document
* MultiEditor::documentForTab( int index
)
869 return mTabs
->tabData(index
).value
<Document
*>();
872 int MultiEditor::tabForDocument( Document
* doc
)
874 int tabCount
= mTabs
->count();
875 for (int idx
= 0; idx
< tabCount
; ++idx
) {
876 Document
*tabDoc
= documentForTab(idx
);
877 if (tabDoc
&& tabDoc
== doc
)
883 CodeEditorBox
*MultiEditor::newBox()
885 CodeEditorBox
*box
= new CodeEditorBox();
887 connect(box
, SIGNAL(activated(CodeEditorBox
*)),
888 this, SLOT(onBoxActivated(CodeEditorBox
*)));
893 void MultiEditor::setCurrentBox( CodeEditorBox
* box
)
895 if (mCurrentEditorBox
== box
)
898 mCurrentEditorBox
= box
;
899 mBoxSigMux
->setCurrentObject(box
);
900 setCurrentEditor( box
->currentEditor() );
902 mCurrentEditorBox
->setActive();
905 void MultiEditor::setCurrentEditor( CodeEditor
* editor
)
908 int tabIndex
= tabForDocument(editor
->document());
910 mTabs
->setCurrentIndex(tabIndex
);
913 mSigMux
->setCurrentObject(editor
);
916 Document
*currentDocument
= editor
? editor
->document() : 0;
917 Main::scProcess()->setActiveDocument(currentDocument
);
918 emit
currentDocumentChanged(currentDocument
);
921 CodeEditor
*MultiEditor::currentEditor()
923 return currentBox()->currentEditor();
926 void MultiEditor::split( Qt::Orientation splitDirection
)
928 CodeEditorBox
*box
= newBox();
929 CodeEditorBox
*curBox
= currentBox();
930 CodeEditor
*curEditor
= curBox
->currentEditor();
933 box
->setDocument(curEditor
->document(), curEditor
->textCursor().position());
935 mSplitter
->insertWidget(box
, curBox
, splitDirection
);
936 box
->setFocus( Qt::OtherFocusReason
);
939 void MultiEditor::removeCurrentSplit()
941 int boxCount
= mSplitter
->findChildren
<CodeEditorBox
*>().count();
943 // Do not allow removing the one and only box.
946 CodeEditorBox
*box
= currentBox();
947 mSplitter
->removeWidget(box
);
949 // switch current box to first box found:
950 box
= mSplitter
->findChild
<CodeEditorBox
>();
953 box
->setFocus( Qt::OtherFocusReason
);
956 void MultiEditor::removeAllSplits()
958 CodeEditorBox
*box
= currentBox();
960 Q_ASSERT(mSplitter
->count());
961 if (mSplitter
->count() == 1 && mSplitter
->widget(0) == box
)
965 MultiSplitter
*newSplitter
= new MultiSplitter
;
966 newSplitter
->addWidget(box
);
969 mSplitter
= newSplitter
;
970 layout()->addWidget(newSplitter
);
972 box
->setFocus( Qt::OtherFocusReason
);