1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
|
#include "editors.h"
#include "txstabwidget.h"
#include "mostQtHeaders.h"
#include "utilsSystem.h"
#include "latexeditorview.h"
/*!
* This class manages all editors and their positioning in tabWidgets.
*
* It maintains the knowledge of the currentEditor() and the currentTabWidget().
* Also, it sends appropriate signals to the editors and tabWidgets and provides
* singals to hook to these changes.
*
* Currently this class serves two purposes:
*
* 1) It maintains and abstract order of editors. Editors can be grouped (currently
* implemented as tabs in tabWidgets). The groups are ordered and the editors
* within a group are ordered, thus providing a complete order of all editors.
* The main purpose of this class it to provide an interface to this order, which
* can be used in the main program without having to know details on the actual
* organization of groups.
*
* 2) It contains all tabWidgets and thus editors providing a simple horizontal or
* vertical layout of the tab groups (there are currently 2 groups).
* This purpose is secondary and mainly implemented for an easier transition from
* the single tabGroup to more complex editor layouts. It may be changed in the
* future and will probably move to a separate class. The public interface of the
* class should depend on this as little as possible.
*/
Editors::Editors(QWidget *parent) :
QWidget(parent), splitter(0), currentGroupIndex(-1)
{
splitter = new QSplitter();
splitter->setOrientation(Qt::Horizontal);
splitter->setChildrenCollapsible(false);
splitter->setHandleWidth(0);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(splitter);
changes = new EditorChangeProxy(this);
connect(changes, SIGNAL(currentEditorChanged()), this, SIGNAL(currentEditorChanged()));
connect(changes, SIGNAL(listOfEditorsChanged()), this, SIGNAL(listOfEditorsChanged()));
connect(this, SIGNAL(editorsReordered()), this, SIGNAL(listOfEditorsChanged()));
}
/*!
* Adds the given tabWidget. This take ownership of the tabWidget.
*/
void Editors::addTabWidget(TxsTabWidget *w)
{
splitter->addWidget(w);
tabGroups.append(w);
if (currentGroupIndex < 0) {
currentGroupIndex = 0;
w->setActive(true);
} else {
if (w->isEmpty()) w->hide();
}
connect(w, SIGNAL(currentEditorChanged()), changes, SLOT(currentEditorChange()));
connect(w, SIGNAL(editorAboutToChangeByTabClick(LatexEditorView *, LatexEditorView *)), this, SLOT(onEditorChangeByTabClick(LatexEditorView *, LatexEditorView *)));
connect(w, SIGNAL(activationRequested()), SLOT(activateTabWidgetFromSender()));
connect(w, SIGNAL(closeEditorRequested(LatexEditorView*)), SLOT(requestCloseEditor(LatexEditorView *)));
connect(w, SIGNAL(tabBarContextMenuRequested(QPoint)), SLOT(tabBarContextMenu(QPoint)));
connect(w, SIGNAL(tabMoved(int, int)), SIGNAL(editorsReordered()));
}
void Editors::addEditor(LatexEditorView *edView, bool asCurrent)
{
insertEditor(edView, currentTabWidget(), -1, asCurrent);
}
void Editors::insertEditor(LatexEditorView *edView, int index, bool asCurrent)
{
if (index >= 0) {
for (int group = 0; group < tabGroups.length(); group++) {
if (tabGroups[group]->count() > index) {
insertEditor(edView, tabGroups[group], index, asCurrent);
return;
}
index -= tabGroups[group]->count();
}
// index out of range: append
}
// append to last non-empty group
for (int group = tabGroups.length() - 1; group >= 0; group--) {
if (!tabGroups[group]->isEmpty() || group == 0) {
insertEditor(edView, tabGroups[group], -1, asCurrent);
break;
}
}
}
void Editors::moveEditor(LatexEditorView *edView, Editors::Position pos)
{
switch (pos) {
case AbsoluteFront:
moveToTabGroup(edView, tabGroups.first(), 0);
break;
case AbsoluteEnd: {
int lastNonHiddenIndex = tabGroups.size() - 1;
while (lastNonHiddenIndex > 0 && tabGroups[lastNonHiddenIndex]->isHidden()) {
lastNonHiddenIndex--;
}
if (lastNonHiddenIndex < 0) return;
moveToTabGroup(edView, tabGroups[lastNonHiddenIndex], -1);
break;
}
case GroupFront: {
TxsTabWidget *tw = tabWidgetFromEditor(edView);
moveToTabGroup(edView, tw, 0);
break;
}
case GroupEnd: {
TxsTabWidget *tw = tabWidgetFromEditor(edView);
moveToTabGroup(edView, tw, -1);
break;
}
}
}
/*!
* Insert the editor in the given tabWidget at pos.
* If tabWidget is 0, insert into the current tabWidget.
* If pos == -1, append.
* If asCurrent, make the editor the current editor.
*/
void Editors::insertEditor(LatexEditorView *edView, TxsTabWidget *tabWidget, int pos, bool asCurrent)
{
bool b = changes->block();
if (!tabWidget) return;
if (!tabWidget->isVisible()) {
tabWidget->setVisible(true);
// distribute available space
QList<int> sizes;
for (int i = 0; i < splitter->count(); i++) {
sizes << 10; // excess space is distributed equally
}
splitter->setSizes(sizes);
}
tabWidget->insertEditor(edView, pos, asCurrent);
connect(edView, SIGNAL(focusReceived()), this, SLOT(setCurrentEditorFromSender()));
if (asCurrent) {
setCurrentEditor(edView);
}
if (b) changes->release();
}
/*! \brief request to close the given editor.
*
* The widget cannot decide if the tab can really be closed, which can only be
* determined at top level with user interaction if there are unsaved changes to
* the editor. So we propagate the request up.
*/
void Editors::requestCloseEditor(LatexEditorView *edView)
{
LatexEditorView *originalCurrent = currentEditor();
if (edView == originalCurrent) {
// request to close the current editor
emit closeCurrentEditorRequested();
} else {
// request to close a non-current editor
setCurrentEditor(edView);
emit closeCurrentEditorRequested();
if (currentEditor() != edView) {
// closing (of the originally non-current) editor was successful.
// Therefore we activate the originally current editor again.
setCurrentEditor(originalCurrent);
}
}
}
/*!
* Remove the editor from the list of editors
*/
void Editors::removeEditor(LatexEditorView *edView)
{
disconnect(edView, SIGNAL(focusReceived()), this, SLOT(setCurrentEditorFromSender()));
removeEditor(edView, tabWidgetFromEditor(edView));
}
/*!
* Remove the editor from the given tabWidget and hides the widget if its empty and not the only one.
*/
void Editors::removeEditor(LatexEditorView *edView, TxsTabWidget *tabWidget)
{
if (!tabWidget) return;
bool b = changes->block();
bool isLastEditorInGroup = (tabWidget->indexOf(edView) == tabWidget->count() - 1);
if (edView == currentEditor()) {
if (isLastEditorInGroup) {
activatePreviousEditor();
} else {
activateNextEditor();
}
}
tabWidget->removeEditor(edView);
if (tabWidget->isEmpty() && tabWidget != tabGroups[0]) {
tabWidget->hide();
}
if (b) changes->release();
}
bool Editors::containsEditor(LatexEditorView *edView) const
{
return tabGroupIndexFromEditor(edView) >= 0;
}
TxsTabWidget *Editors::currentTabWidget() const
{
if (currentGroupIndex >= 0 && currentGroupIndex < tabGroups.length())
return tabGroups[currentGroupIndex];
return 0;
}
LatexEditorView *Editors::currentEditor() const
{
TxsTabWidget *tabs = currentTabWidget();
if (!tabs) return 0;
return qobject_cast<LatexEditorView *>(tabs->currentWidget());
}
/*!
* Sets the current editor. It is expected that (containsEditor(edView) == true).
*/
void Editors::setCurrentEditor(LatexEditorView *edView)
{
if (currentEditor() == edView) {
return;
}
int group = tabGroupIndexFromEditor(edView);
if (group >= 0) {
bool b = changes->block();
setCurrentGroup(group);
tabGroups[group]->setCurrentEditor(edView);
edView->setFocus();
if (b) changes->release();
return;
}
// catch calls in which editor is not a member tab.
// TODO: such calls are deprecated as bad practice. We should avoid them in the long run. For the moment the fallback to do nothing is ok.
qDebug() << "Warning (deprecated call): TxsTabWidget::setCurrentEditor: editor not member of TxsTabWidget";
#ifndef QT_NO_DEBUG
txsWarning("Warning (deprecated call): TxsTabWidget::setCurrentEditor: editor not member of TxsTabWidget");
#endif
}
QList<LatexEditorView *> Editors::editors()
{
QList<LatexEditorView *> editors;
foreach (TxsTabWidget *tabGroup, tabGroups) {
editors.append(tabGroup->editors());
}
return editors;
}
void Editors::setCurrentEditorFromAction()
{
QAction *act = qobject_cast<QAction *>(sender());
REQUIRE(act);
setCurrentEditor(act->data().value<LatexEditorView *>());
}
void Editors::setCurrentEditorFromSender()
{
LatexEditorView *edView = qobject_cast<LatexEditorView *>(sender());
REQUIRE(edView);
setCurrentEditor(edView);
}
bool Editors::activateNextEditor()
{
if (currentGroupIndex < 0) return false;
bool b = changes->block();
if (!tabGroups[currentGroupIndex]->currentEditorViewIsLast()) {
tabGroups[currentGroupIndex]->gotoNextDocument();
} else {
// find the next non-empty group
int newIndex = currentGroupIndex;
for (int unused = 0; unused < tabGroups.length(); unused++) { // counter is only used as a stopping criterion
newIndex = (newIndex + 1) % tabGroups.length();
if (!tabGroups[newIndex]->isEmpty()) {
if (newIndex == currentGroupIndex && tabGroups[currentGroupIndex]->count() == 1) {
if (b) changes->release();
return false; // only one editor: we cannot change
}
setCurrentGroup(newIndex);
break;
}
}
tabGroups[currentGroupIndex]->gotoFirstDocument();
}
if (b) changes->release();
return true;
}
bool Editors::activatePreviousEditor()
{
if (currentGroupIndex < 0) return false;
bool b = changes->block();
if (!tabGroups[currentGroupIndex]->currentEditorViewIsFirst()) {
tabGroups[currentGroupIndex]->gotoPrevDocument();
} else {
// find the previous non-empty group
int newIndex = currentGroupIndex;
for (int unused = 0; unused < tabGroups.length(); unused++) { // counter is only used as a stopping criterion
newIndex = (newIndex == 0) ? (tabGroups.length() -1) : (newIndex - 1);
if (!tabGroups[newIndex]->isEmpty()) {
if (newIndex == currentGroupIndex && tabGroups[currentGroupIndex]->count() == 1) {
return false; // only one editor: we cannot change
}
setCurrentGroup(newIndex);
break;
}
}
tabGroups[currentGroupIndex]->gotoLastDocument();
}
if (b) changes->release();
return true;
}
/*!
* Activates the tabWidget that is the sender(). Returns true if the widget could be activated.
*/
bool Editors::activateTabWidgetFromSender()
{
TxsTabWidget *tabWidget = qobject_cast<TxsTabWidget *>(sender());
if (!tabWidget) return false;
LatexEditorView *edView = tabWidget->currentEditor();
if (!edView) return false;
setCurrentEditor(tabWidget->currentEditor());
return true;
}
/*!
* Context menu handler for tabbars.
*/
void Editors::tabBarContextMenu(const QPoint &point)
{
if (point.isNull()) return;
TxsTabWidget *tabGroup = qobject_cast<TxsTabWidget *>(sender());
if (!tabGroup) return;
QMenu menu;
foreach (LatexEditorView *edView, editors()) {
QAction *act = menu.addAction(edView->displayNameForUI());
act->setData(QVariant::fromValue<LatexEditorView *>(edView));
connect(act, SIGNAL(triggered()), SLOT(setCurrentEditorFromAction()));
}
menu.addSeparator();
QAction *act = menu.addAction(tr("Move to other view"));
LatexEditorView *editorUnderCursor = tabGroup->editorAt(point);
act->setData(QVariant::fromValue<LatexEditorView *>(editorUnderCursor));
if (!editorUnderCursor) act->setEnabled(false);
connect(act, SIGNAL(triggered()), SLOT(moveToOtherTabGroup()));
act = menu.addAction((splitter->orientation() == Qt::Horizontal) ? tr("Split Vertically") : tr("Split Horizontally"));
connect(act, SIGNAL(triggered()), SLOT(changeSplitOrientation()));
menu.exec(tabGroup->mapToGlobal(point));
}
void Editors::onEditorChangeByTabClick(LatexEditorView *from, LatexEditorView *to)
{
Q_UNUSED(from);
// the original event comes from a tab widget. from is the previously selected tab in that widget
// which has not been the current one one if the tab widget has not been the current
emit editorAboutToChangeByTabClick(currentEditor(), to);
}
/*!
* Called from and action with data() set to an editor. Moves the editor to the other tabGroup.
* This currently assumes a restriction to two tab groups.
*/
void Editors::moveToOtherTabGroup()
{
QAction *act = qobject_cast<QAction *>(sender());
REQUIRE(act);
LatexEditorView *edView = act->data().value<LatexEditorView *>();
if (!edView) return;
// NOTE: This code assumes exactly two tabGroups
int otherGroupIndex = (tabGroups[0] == tabWidgetFromEditor(edView)) ? 1 : 0;
moveToTabGroup(edView, tabGroups[otherGroupIndex], -1);
}
/*!
* Move the editor to the given tabWidget at position targetIndex (if < 0, append).
*/
void Editors::moveToTabGroup(LatexEditorView *edView, TxsTabWidget *target, int targetIndex)
{
if (!target || target->containsEditor(edView)) {
// only move within the tab
if (target == 0) target = tabWidgetFromEditor(edView);
if (targetIndex < 0) targetIndex = qMax(0, target->count() - 1);
target->moveTab(target->indexOf(edView), targetIndex);
emit editorsReordered();
} else {
bool b = changes->block();
TxsTabWidget *source = tabWidgetFromEditor(edView);
removeEditor(edView, source);
insertEditor(edView, target, targetIndex, true);
if (b) changes->release();
}
}
void Editors::changeSplitOrientation()
{
splitter->setOrientation((splitter->orientation() == Qt::Horizontal) ? Qt::Vertical : Qt::Horizontal);
}
/*!
* Returns the number of the tabGroup to which the povided editor belongs or -1.
*/
int Editors::tabGroupIndexFromEditor(LatexEditorView *edView) const
{
for (int group = 0; group < tabGroups.length(); group++) {
int i = tabGroups[group]->indexOf(edView);
if (i >= 0) {
return group;
}
}
return -1;
}
/*!
* Returns a pointer to the tabWidget to which the provided editor belongs or 0.
*/
TxsTabWidget *Editors::tabWidgetFromEditor(LatexEditorView *edView) const
{
int group = tabGroupIndexFromEditor(edView);
if (group < 0) return 0;
return tabGroups[group];
}
void Editors::setCurrentGroup(int index)
{
if (index == currentGroupIndex) return;
if (currentGroupIndex >= 0) {
tabGroups[currentGroupIndex]->setActive(false);
}
currentGroupIndex = index;
tabGroups[currentGroupIndex]->setActive(true);
}
/*! \class EditorChangeProxy
*
* This class handles changes of the currentEditor
*
* There are two ways that can lead to emission of the signal currentEditorChanged():
*
* 1) First, other components can connect to the slot editorChange(). These signals are
* just propagated, except if the EditorChangeProxy is blocked.
*
* 2) Second, one can block() the propagation of changes and release() later on. At release(),
* the proxy checks if the currentEditor has changed from the one at blocking. If so, the
* currentEditorChanged() signal is emitted.
* Use the block/release mechanism to perform complex actions that consist of multiple
* editor changes (potentially even with inconsistent intermediate states) during which
* you don't want to emit currentEditorChanged().
* You have to make sure that you release the block if and only if you acquired it. Therefore,
* blocking should always use this pattern:
*
* bool b = changes->block();
* ...
* if (b) changes->release();
*
* If you have return statements in the intermediate code, be sure to place the release command
* also before every return.
*/
EditorChangeProxy::EditorChangeProxy(Editors *e) : editors(e), currentEditorAtBlock(0), blocked(false) {}
//#define ecpDebug(x) qDebug(x)
#define ecpDebug(x)
bool EditorChangeProxy::block()
{
if (blocked) {
ecpDebug("already blocked");
return false;
}
blocked = true;
currentEditorAtBlock = editors->currentEditor();
listOfEditorsAtBlock = editors->editors();
ecpDebug("block activated");
return true;
}
void EditorChangeProxy::release()
{
ecpDebug("release");
if (blocked) {
blocked = false;
if (editors->currentEditor() != currentEditorAtBlock) {
ecpDebug("currentEditorChanged signaled at release");
emit currentEditorChanged();
}
if (editors->editors() != listOfEditorsAtBlock) {
ecpDebug("listOfEditorsChanged signaled at release");
emit listOfEditorsChanged();
}
} else {
// can only happen if the above mentioned blocking pattern was not used.
qDebug("WARNING: trying to realease an unblocked EditorChangeProxy. This hints at inconsistent code.");
}
}
void EditorChangeProxy::currentEditorChange()
{
if (!blocked) {
ecpDebug("currentEditorChanged passed");
emit currentEditorChanged();
} else {
ecpDebug("currentEditorChanged blocked");
}
}
void EditorChangeProxy::listOfEditorsChange()
{
if (!blocked) {
ecpDebug("listOfEditorsChanged passed");
emit listOfEditorsChanged();
} else {
ecpDebug("listOfEditorsChanged blocked");
}
}
#undef ecpDebug
|