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
|
/***************************************************************************
* copyright : (C) 2012 by Tim Hoffmann *
* http://texstudio.sourceforge.net/ *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "cursorhistory.h"
#include "qdocumentcursor.h"
CursorHistory::CursorHistory(LatexDocuments *docs) :
QObject(docs), m_backAction(0), m_forwardAction(0), m_maxLength(30), m_insertionEnabled(true)
{
connect(docs, SIGNAL(aboutToDeleteDocument(LatexDocument *)), this, SLOT(aboutToDeleteDoc(LatexDocument *)));
currentEntry = history.end();
}
/*!
Inserts the cursor behind the current entry
*/
bool CursorHistory::insertPos(QDocumentCursor cur, bool deleteBehindCurrent)
{
if (!m_insertionEnabled) return false;
if (!cur.isValid()) return false;
CursorPosition pos(cur);
connectUnique(pos.doc(), SIGNAL(destroyed(QObject *)), this, SLOT(documentClosed(QObject *)));
// TODO destroyed() may be duplicate to aboutToDeleteDocument() - needs more testing. anyway it does not harm
connectUnique(pos.doc(), SIGNAL(lineDeleted(QDocumentLineHandle *)), this, SLOT(lineDeleted(QDocumentLineHandle *)));
connectUnique(pos.doc(), SIGNAL(lineRemoved(QDocumentLineHandle *)), this, SLOT(lineDeleted(QDocumentLineHandle *)));
if (deleteBehindCurrent && currentEntry != history.end()) {
currentEntry++;
currentEntry = history.erase(currentEntry, history.end());
}
if (currentEntry == history.end() && currentEntry != history.begin()) currentEntry--;
// do not insert neighboring duplicates
if (currentEntryValid() && (*currentEntry).equals(pos)) {
updateNavActions();
return false;
}
CursorPosList::iterator it = prevValidEntry(currentEntry);
if (it != history.end() && (*it).isValid() && (*it).equals(pos)) {
updateNavActions();
return false;
}
if (history.count() >= m_maxLength) {
if (currentEntry == history.begin()) {
history.removeLast();
} else {
history.removeFirst();
}
}
currentEntry++;
history.insert(currentEntry, pos);
updateNavActions();
return true;
}
QDocumentCursor CursorHistory::currentPos()
{
if (!currentEntryValid()) {
validate();
}
if (!currentEntryValid()) {
qDebug() << "invalid current position in CursorHistory";
return QDocumentCursor();
}
return (*currentEntry).toCursor();
}
void CursorHistory::setInsertionEnabled(bool b)
{
m_insertionEnabled = b;
}
/*!
Register an action as backward action. This is only used to set the enabled state of the action depending on if back is possible.
It does not actually connect the action to the back() slot because it is unspecified if other slots connected to the action are executed
before or after back().
Therefore the user has to call back() manually, or, if it is unabigous, connect the trigger signal of the action to the back() slot himself.
*/
void CursorHistory::setBackAction(QAction *back)
{
m_backAction = back;
updateNavActions();
}
/*!
Register an action as forward action. This is only used to set the enabled state of the action depending on if forward is possible.
It does not actually connect the action to the forward() slot because it is unspecified if other slots connected to the action are executed
before or after forward().
Therefore the user has to call forward() manually, or, if it is unabigous, connect the trigger signal of the action to the forward() slot himself.
*/
void CursorHistory::setForwardAction(QAction *forward)
{
m_forwardAction = forward;
updateNavActions();
}
void CursorHistory::clear()
{
history.clear();
currentEntry = history.end();
updateNavActions();
}
QDocumentCursor CursorHistory::back(const QDocumentCursor ¤tCursor)
{
if (currentEntry == history.begin()) {
updateNavActions();
return QDocumentCursor();
}
// insert currentCursor to be able to go back
if (currentCursor.isValid() && insertPos(currentCursor, false)) {
currentEntry--;
}
CursorPosition pos(currentCursor);
if (pos.isValid() && !pos.equals(*currentEntry)) {
updateNavActions();
return currentPos();
}
currentEntry = prevValidEntry(currentEntry);
updateNavActions();
return currentPos();
}
QDocumentCursor CursorHistory::forward(const QDocumentCursor ¤tCursor)
{
Q_UNUSED(currentCursor);
CursorPosList::iterator next = nextValidEntry(currentEntry);
if (currentEntry == history.end() || next == history.end()) {
updateNavActions();
return QDocumentCursor();
}
currentEntry = next;
updateNavActions();
return currentPos();
}
void CursorHistory::aboutToDeleteDoc(LatexDocument *doc)
{
// remove all entries with document from list.
for (CursorPosList::iterator it = history.begin(); it != history.end(); ) {
if ( (*it).doc() == doc ) {
if (currentEntry == it)
currentEntry = nextValidEntry(currentEntry);
removeEntry(it);
} else ++it;
}
updateNavActions();
}
void CursorHistory::documentClosed(QObject *obj)
{
LatexDocument *doc = qobject_cast<LatexDocument *>(obj);
if (doc)
aboutToDeleteDoc(doc);
}
void CursorHistory::lineDeleted(QDocumentLineHandle *dlh)
{
for (CursorPosList::iterator it = history.begin(); it != history.end(); ++it) {
if ( (*it).dlh() == dlh ) {
if (currentEntry == it)
currentEntry = nextValidEntry(currentEntry);
removeEntry(it);
}
}
updateNavActions();
}
void CursorHistory::updateNavActions()
{
if (m_backAction) {
m_backAction->setEnabled(currentEntry != history.begin());
}
if (m_forwardAction) {
m_forwardAction->setEnabled(nextValidEntry(currentEntry) != history.end());
}
}
void CursorHistory::removeEntry(CursorPosList::iterator &it)
{
Q_ASSERT(it != history.end());
if (currentEntry == it) {
currentEntry = nextValidEntry(currentEntry);
}
Q_ASSERT(currentEntry != it);
it = history.erase(it);
}
bool CursorHistory::currentEntryValid()
{
if (currentEntry == history.end()) return false;
return (*currentEntry).isValid();
}
/*!
Removes all invalid entries from history
*/
void CursorHistory::validate()
{
CursorPosList::iterator it = history.begin();
while (it != history.end()) {
if (!(*it).isValid()) {
if (it == currentEntry) currentEntry++;
qDebug() << "removed invalid cursorHistory entry" << (*it).doc()->getFileName();
Q_ASSERT(currentEntry != it);
it = history.erase(it);
} else {
++it;
}
}
}
CursorPosList::iterator CursorHistory::prevValidEntry(const CursorPosList::iterator &start)
{
CursorPosList::iterator it = start;
while (true) {
if (it == history.begin()) return it;
--it;
if ((*it).isValid()) {
return it;
}
bool moveCurrent = (currentEntry == it);
it = history.erase(it);
if (moveCurrent) currentEntry = it;
}
Q_ASSERT(0);
return history.end(); // never reached
}
CursorPosList::iterator CursorHistory::nextValidEntry(const CursorPosList::iterator &start)
{
CursorPosList::iterator it = start;
if (it == history.end()) return it;
++it;
while (true) {
if (it == history.end()) return it;
if ((*it).isValid()) {
return it;
}
bool moveCurrent = (currentEntry == it);
it = history.erase(it);
if (moveCurrent) currentEntry = it;
}
Q_ASSERT(0);
return history.end(); // never reached
}
void CursorHistory::debugPrint()
{
qDebug() << "*** Cursor History ***";
CursorPosList::iterator it = history.begin();
while (it != history.end()) {
CursorPosition pos = *it;
qDebug() << ((it == currentEntry) ? "*" : " ") << pos.doc()->getFileName() << pos.oldLineNumber() << "col:" << pos.columnNumber();
it++;
}
qDebug() << ((it == currentEntry) ? "*" : " ") << "end";
}
|