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 Horizontally"), this);
371 connect(act
, SIGNAL(triggered()), this, SLOT(splitHorizontally()));
373 mActions
[SplitVertically
] = act
= new QAction(tr("Split Vertically"), 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()));
381 mActions
[EvaluateCurrentDocument
] = act
= new QAction(
382 QIcon::fromTheme("media-playback-start"), tr("Evaluate &File"), this);
383 act
->setStatusTip(tr("Evaluate current File"));
384 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
385 connect(act
, SIGNAL(triggered()), this, SLOT(evaluateDocument()));
387 mActions
[EvaluateRegion
] = act
= new QAction(
388 QIcon::fromTheme("media-playback-start"), tr("&Evaluate Selection, Line or Region"), this);
389 act
->setShortcut(tr("Ctrl+Return", "Evaluate region"));
390 act
->setStatusTip(tr("Evaluate current region"));
391 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
392 connect(act
, SIGNAL(triggered()), this, SLOT(evaluateRegion()));
394 mActions
[EvaluateLine
] = act
= new QAction(
395 QIcon::fromTheme("media-playback-startline"), tr("&Evaluate Line"), this);
396 act
->setShortcut(tr("Shift+Ctrl+Return", "Evaluate line"));
397 act
->setStatusTip(tr("Evaluate current line"));
398 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
399 connect(act
, SIGNAL(triggered()), this, SLOT(evaluateLine()));
401 mActions
[OpenDefinition
] = act
= new QAction(tr("Open Class/Method Definition"), this);
402 act
->setShortcut(tr("Ctrl+I", "Open definition of selected class or method"));
403 act
->setShortcutContext(Qt::WidgetWithChildrenShortcut
);
404 connect(act
, SIGNAL(triggered(bool)), this, SLOT(openDefinition()));
406 settings
->endGroup(); // IDE/shortcuts
408 for (int i
= 0; i
< ActionRoleCount
; ++i
)
409 settings
->addAction( mActions
[i
] );
411 // These actions are not added to any menu, so they have to be added
412 // at least to this widget, in order for the shortcuts to always respond:
413 addAction(mActions
[TriggerAutoCompletion
]);
414 addAction(mActions
[TriggerMethodCallAid
]);
415 addAction(mActions
[SwitchDocument
]);
417 // These actions have to be added because to the widget because they have
418 // Qt::WidgetWithChildrenShortcut context:
419 addAction(mActions
[EnlargeFont
]);
420 addAction(mActions
[ShrinkFont
]);
421 addAction(mActions
[OpenDefinition
]);
422 addAction(mActions
[EvaluateCurrentDocument
]);
423 addAction(mActions
[EvaluateRegion
]);
424 addAction(mActions
[EvaluateLine
]);
425 addAction(mActions
[ToggleComment
]);
426 addAction(mActions
[ToggleOverwriteMode
]);
427 addAction(mActions
[CopyLineUp
]);
428 addAction(mActions
[CopyLineDown
]);
429 addAction(mActions
[MoveLineUp
]);
430 addAction(mActions
[MoveLineDown
]);
431 addAction(mActions
[GotoPreviousBlock
]);
432 addAction(mActions
[GotoNextBlock
]);
435 void MultiEditor::updateActions()
437 CodeEditor
*editor
= currentEditor();
438 QTextDocument
*doc
= editor
? editor
->textDocument() : 0;
440 mActions
[Undo
]->setEnabled( doc
&& doc
->isUndoAvailable() );
441 mActions
[Redo
]->setEnabled( doc
&& doc
->isRedoAvailable() );
442 mActions
[Copy
]->setEnabled( editor
&& editor
->textCursor().hasSelection() );
443 mActions
[Cut
]->setEnabled( mActions
[Copy
]->isEnabled() );
444 mActions
[Paste
]->setEnabled( editor
);
445 mActions
[ToggleComment
]->setEnabled( editor
);
446 mActions
[ToggleOverwriteMode
]->setEnabled( editor
);
447 mActions
[CopyLineUp
]->setEnabled( editor
);
448 mActions
[CopyLineDown
]->setEnabled( editor
);
449 mActions
[MoveLineUp
]->setEnabled( editor
);
450 mActions
[MoveLineDown
]->setEnabled( editor
);
451 mActions
[GotoPreviousBlock
]->setEnabled( editor
);
452 mActions
[GotoNextBlock
]->setEnabled( editor
);
454 mActions
[IndentLineOrRegion
]->setEnabled( editor
);
455 mActions
[EnlargeFont
]->setEnabled( editor
);
456 mActions
[ShrinkFont
]->setEnabled( editor
);
457 mActions
[OpenDefinition
]->setEnabled( editor
);
458 mActions
[EvaluateCurrentDocument
]->setEnabled( editor
);
459 mActions
[EvaluateRegion
]->setEnabled( editor
);
460 mActions
[EvaluateLine
]->setEnabled( editor
);
461 mActions
[ResetFontSize
]->setEnabled( editor
);
462 mActions
[ShowWhitespace
]->setEnabled( editor
);
463 mActions
[ShowWhitespace
]->setChecked( editor
&& editor
->showWhitespace() );
466 void MultiEditor::applySettings( Settings::Manager
*s
)
468 s
->beginGroup("IDE/editor");
469 mStepForwardEvaluation
= s
->value("stepForwardEvaluation").toBool();
473 static QVariantList
saveBoxState( CodeEditorBox
*box
)
475 // save editors in reverse order - first one is last shown.
476 QVariantList boxData
;
477 int idx
= box
->history().count();
479 CodeEditor
*editor
= box
->history()[idx
];
480 if (!editor
->document()->filePath().isEmpty()) {
481 QVariantMap editorData
;
482 editorData
.insert("file", editor
->document()->filePath());
483 editorData
.insert("position", editor
->textCursor().position());
484 boxData
.append( editorData
);
490 static QVariantMap
saveSplitterState( QSplitter
*splitter
)
492 QVariantMap splitterData
;
494 splitterData
.insert( "state", splitter
->saveState().toBase64() );
496 QVariantList childrenData
;
498 int childCount
= splitter
->count();
499 for (int idx
= 0; idx
< childCount
; idx
++) {
500 QWidget
*child
= splitter
->widget(idx
);
502 CodeEditorBox
*box
= qobject_cast
<CodeEditorBox
*>(child
);
504 QVariantList boxData
= saveBoxState(box
);
505 childrenData
.append( QVariant(boxData
) );
509 QSplitter
*childSplitter
= qobject_cast
<QSplitter
*>(child
);
511 QVariantMap childSplitterData
= saveSplitterState(childSplitter
);
512 childrenData
.append( QVariant(childSplitterData
) );
516 splitterData
.insert( "elements", childrenData
);
521 void MultiEditor::saveSession( Session
*session
)
523 session
->remove( "editors" );
524 session
->setValue( "editors", saveSplitterState(mSplitter
) );
527 void MultiEditor::loadBoxState( CodeEditorBox
*box
, const QVariantList
& data
)
529 mCurrentEditorBox
= box
;
530 foreach( QVariant docVar
, data
)
532 QVariantMap docData
= docVar
.value
<QVariantMap
>();
533 QString docPath
= docData
.value("file").toString();
534 int docPos
= docData
.value("position").toInt();
535 Main::instance()->documentManager()->open( docPath
, docPos
);
539 void MultiEditor::loadSplitterState( QSplitter
*splitter
, const QVariantMap
& data
)
541 QByteArray state
= QByteArray::fromBase64( data
.value("state").value
<QByteArray
>() );
543 QVariantList childrenData
= data
.value("elements").value
<QVariantList
>();
544 foreach (const QVariant
& childVar
, childrenData
) {
545 if (childVar
.type() == QVariant::List
) {
546 CodeEditorBox
*childBox
= newBox();
547 splitter
->addWidget(childBox
);
548 QVariantList childBoxData
= childVar
.value
<QVariantList
>();
549 loadBoxState( childBox
, childBoxData
);
551 else if (childVar
.type() == QVariant::Map
) {
552 QSplitter
*childSplitter
= new QSplitter
;
553 splitter
->addWidget(childSplitter
);
554 QVariantMap childSplitterData
= childVar
.value
<QVariantMap
>();
555 loadSplitterState( childSplitter
, childSplitterData
);
559 if (!splitter
->restoreState(state
))
560 qWarning("MultiEditor: could not restore splitter state!");
563 void MultiEditor::switchSession( Session
*session
)
565 DocumentManager
*docManager
= Main::instance()->documentManager();
566 QList
<Document
*> docs
= docManager
->documents();
567 foreach (Document
*doc
, docs
)
568 docManager
->close(doc
);
571 mSplitter
= new MultiSplitter();
574 QVariantMap splitterData
= session
->value("editors").value
<QVariantMap
>();
575 loadSplitterState( mSplitter
, splitterData
);
578 CodeEditorBox
*firstBox
= 0;
580 if (mSplitter
->count()) {
581 firstBox
= mSplitter
->findChild
<CodeEditorBox
>();
583 qWarning("Session seems to contain invalid editor split data!");
585 mSplitter
= new MultiSplitter();
591 mSplitter
->addWidget( firstBox
);
594 mCurrentEditorBox
= 0; // ensure complete update
595 setCurrentBox( firstBox
);
597 layout()->addWidget(mSplitter
);
599 if (!firstBox
->currentDocument())
600 docManager
->create();
603 void MultiEditor::setCurrent( Document
*doc
)
605 int tabIdx
= tabForDocument(doc
);
607 mTabs
->setCurrentIndex(tabIdx
);
610 void MultiEditor::showNextDocument()
612 int currentIndex
= mTabs
->currentIndex();
613 mTabs
->setCurrentIndex( qMin(currentIndex
+ 1, mTabs
->count() - 1) );
616 void MultiEditor::showPreviousDocument()
618 int currentIndex
= mTabs
->currentIndex();
619 mTabs
->setCurrentIndex( qMax(0, currentIndex
- 1) );
622 void MultiEditor::switchDocument()
624 CodeEditorBox
*box
= currentBox();
626 DocumentSelectPopUp
* popup
= new DocumentSelectPopUp(box
->history(), this);
628 QRect
popupRect(0,0,300,200);
629 popupRect
.moveCenter(rect().center());
630 popup
->resize(popupRect
.size());
631 QPoint globalPosition
= mapToGlobal(popupRect
.topLeft());
633 Document
* selectedDocument
= popup
->exec(globalPosition
);
635 if (selectedDocument
)
636 box
->setDocument(selectedDocument
);
639 void MultiEditor::onOpen( Document
*doc
, int pos
)
641 QTextDocument
*tdoc
= doc
->textDocument();
644 if(tdoc
->isModified())
645 icon
= mDocModifiedIcon
;
647 int newTabIndex
= mTabs
->addTab( icon
, doc
->title() );
648 mTabs
->setTabData( newTabIndex
, QVariant::fromValue
<Document
*>(doc
) );
650 currentBox()->setDocument(doc
, pos
);
653 void MultiEditor::onClose( Document
*doc
)
655 int tabIdx
= tabForDocument(doc
);
657 mTabs
->removeTab(tabIdx
);
658 // TODO: each box should switch document according to their own history
661 void MultiEditor::show( Document
*doc
, int pos
)
663 currentBox()->setDocument(doc
, pos
);
666 void MultiEditor::update( Document
*doc
)
668 int tabIdx
= tabForDocument(doc
);
670 mTabs
->setTabText(tabIdx
, doc
->title());
673 void MultiEditor::onCloseRequest( int index
)
675 Document
*doc
= documentForTab(index
);
677 MainWindow::close(doc
);
680 void MultiEditor::onCurrentTabChanged( int index
)
685 Document
*doc
= documentForTab(index
);
689 CodeEditorBox
*curBox
= currentBox();
690 curBox
->setDocument(doc
);
691 curBox
->setFocus(Qt::OtherFocusReason
);
694 void MultiEditor::onCurrentEditorChanged(CodeEditor
*editor
)
696 setCurrentEditor(editor
);
699 void MultiEditor::onEditorFocusChanged(CodeEditor
*editor
, bool focused
)
702 CodeEditorBox
*box
= qobject_cast
<CodeEditorBox
*>( editor
->parent() );
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(editorFocusChanged(CodeEditor
*,bool)),
888 this, SLOT(onEditorFocusChanged(CodeEditor
*, bool)));
893 void MultiEditor::setCurrentBox( CodeEditorBox
* box
)
895 if (mCurrentEditorBox
== box
)
898 mCurrentEditorBox
= box
;
899 mBoxSigMux
->setCurrentObject(box
);
900 setCurrentEditor( box
->currentEditor() );
903 void MultiEditor::setCurrentEditor( CodeEditor
* editor
)
906 int tabIndex
= tabForDocument(editor
->document());
908 mTabs
->setCurrentIndex(tabIndex
);
911 mSigMux
->setCurrentObject(editor
);
914 Document
*currentDocument
= editor
? editor
->document() : 0;
915 Main::scProcess()->setActiveDocument(currentDocument
);
916 emit
currentDocumentChanged(currentDocument
);
919 CodeEditor
*MultiEditor::currentEditor()
921 return currentBox()->currentEditor();
924 void MultiEditor::split( Qt::Orientation splitDirection
)
926 CodeEditorBox
*curBox
= currentBox();
927 CodeEditor
*curEditor
= curBox
->currentEditor();
931 CodeEditorBox
*box
= newBox();
932 box
->setDocument(curEditor
->document(), curEditor
->textCursor().position());
933 mSplitter
->insertWidget(box
, curBox
, splitDirection
);
934 box
->setFocus( Qt::OtherFocusReason
);
937 void MultiEditor::removeCurrentSplit()
939 if (mSplitter
->count() < 2)
942 CodeEditorBox
*box
= currentBox();
943 mSplitter
->removeWidget(box
);
945 // switch current box to first box found:
946 box
= mSplitter
->findChild
<CodeEditorBox
>();
949 box
->setFocus( Qt::OtherFocusReason
);