src/gui/text/qtextdocument_p.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
   868     if (!undoEnabled || (undo && undoState == 0) || (!undo && undoState == undoStack.size()))
   868     if (!undoEnabled || (undo && undoState == 0) || (!undo && undoState == undoStack.size()))
   869         return -1;
   869         return -1;
   870 
   870 
   871     undoEnabled = false;
   871     undoEnabled = false;
   872     beginEditBlock();
   872     beginEditBlock();
       
   873     int editPos = -1;
   873     while (1) {
   874     while (1) {
   874         if (undo)
   875         if (undo)
   875             --undoState;
   876             --undoState;
   876         QTextUndoCommand &c = undoStack[undoState];
   877         QTextUndoCommand &c = undoStack[undoState];
   877         int resetBlockRevision = c.pos;
   878         int resetBlockRevision = c.pos;
   879 	switch(c.command) {
   880 	switch(c.command) {
   880         case QTextUndoCommand::Inserted:
   881         case QTextUndoCommand::Inserted:
   881             remove(c.pos, c.length, (QTextUndoCommand::Operation)c.operation);
   882             remove(c.pos, c.length, (QTextUndoCommand::Operation)c.operation);
   882             PMDEBUG("   erase: from %d, length %d", c.pos, c.length);
   883             PMDEBUG("   erase: from %d, length %d", c.pos, c.length);
   883             c.command = QTextUndoCommand::Removed;
   884             c.command = QTextUndoCommand::Removed;
       
   885             editPos = c.pos;
   884 	    break;
   886 	    break;
   885         case QTextUndoCommand::Removed:
   887         case QTextUndoCommand::Removed:
   886             PMDEBUG("   insert: format %d (from %d, length %d, strpos=%d)", c.format, c.pos, c.length, c.strPos);
   888             PMDEBUG("   insert: format %d (from %d, length %d, strpos=%d)", c.format, c.pos, c.length, c.strPos);
   887             insert_string(c.pos, c.strPos, c.length, c.format, (QTextUndoCommand::Operation)c.operation);
   889             insert_string(c.pos, c.strPos, c.length, c.format, (QTextUndoCommand::Operation)c.operation);
   888             c.command = QTextUndoCommand::Inserted;
   890             c.command = QTextUndoCommand::Inserted;
       
   891             editPos = c.pos + c.length;
   889 	    break;
   892 	    break;
   890 	case QTextUndoCommand::BlockInserted:
   893 	case QTextUndoCommand::BlockInserted:
   891 	case QTextUndoCommand::BlockAdded:
   894 	case QTextUndoCommand::BlockAdded:
   892             remove_block(c.pos, &c.blockFormat, c.command, (QTextUndoCommand::Operation)c.operation);
   895             remove_block(c.pos, &c.blockFormat, c.command, (QTextUndoCommand::Operation)c.operation);
   893             PMDEBUG("   blockremove: from %d", c.pos);
   896             PMDEBUG("   blockremove: from %d", c.pos);
   894 	    if (c.command == QTextUndoCommand::BlockInserted)
   897 	    if (c.command == QTextUndoCommand::BlockInserted)
   895 		c.command = QTextUndoCommand::BlockRemoved;
   898 		c.command = QTextUndoCommand::BlockRemoved;
   896 	    else
   899 	    else
   897 		c.command = QTextUndoCommand::BlockDeleted;
   900 		c.command = QTextUndoCommand::BlockDeleted;
       
   901             editPos = c.pos;
   898 	    break;
   902 	    break;
   899 	case QTextUndoCommand::BlockRemoved:
   903 	case QTextUndoCommand::BlockRemoved:
   900 	case QTextUndoCommand::BlockDeleted:
   904 	case QTextUndoCommand::BlockDeleted:
   901             PMDEBUG("   blockinsert: charformat %d blockformat %d (pos %d, strpos=%d)", c.format, c.blockFormat, c.pos, c.strPos);
   905             PMDEBUG("   blockinsert: charformat %d blockformat %d (pos %d, strpos=%d)", c.format, c.blockFormat, c.pos, c.strPos);
   902             insert_block(c.pos, c.strPos, c.format, c.blockFormat, (QTextUndoCommand::Operation)c.operation, c.command);
   906             insert_block(c.pos, c.strPos, c.format, c.blockFormat, (QTextUndoCommand::Operation)c.operation, c.command);
   903             resetBlockRevision += 1;
   907             resetBlockRevision += 1;
   904 	    if (c.command == QTextUndoCommand::BlockRemoved)
   908 	    if (c.command == QTextUndoCommand::BlockRemoved)
   905 		c.command = QTextUndoCommand::BlockInserted;
   909 		c.command = QTextUndoCommand::BlockInserted;
   906 	    else
   910 	    else
   907 		c.command = QTextUndoCommand::BlockAdded;
   911 		c.command = QTextUndoCommand::BlockAdded;
       
   912             editPos = c.pos + 1;
   908 	    break;
   913 	    break;
   909 	case QTextUndoCommand::CharFormatChanged: {
   914 	case QTextUndoCommand::CharFormatChanged: {
   910             resetBlockRevision = -1; // ## TODO
   915             resetBlockRevision = -1; // ## TODO
   911             PMDEBUG("   charFormat: format %d (from %d, length %d)", c.format, c.pos, c.length);
   916             PMDEBUG("   charFormat: format %d (from %d, length %d)", c.format, c.pos, c.length);
   912             FragmentIterator it = find(c.pos);
   917             FragmentIterator it = find(c.pos);
   913             Q_ASSERT(!it.atEnd());
   918             Q_ASSERT(!it.atEnd());
   914 
   919 
   915             int oldFormat = it.value()->format;
   920             int oldFormat = it.value()->format;
   916             setCharFormat(c.pos, c.length, formats.charFormat(c.format));
   921             setCharFormat(c.pos, c.length, formats.charFormat(c.format));
   917             c.format = oldFormat;
   922             c.format = oldFormat;
       
   923             editPos = c.pos + c.length;
   918 	    break;
   924 	    break;
   919 	}
   925 	}
   920 	case QTextUndoCommand::BlockFormatChanged: {
   926 	case QTextUndoCommand::BlockFormatChanged: {
   921             resetBlockRevision = -1; // ## TODO
   927             resetBlockRevision = -1; // ## TODO
   922             PMDEBUG("   blockformat: format %d pos %d", c.format, c.pos);
   928             PMDEBUG("   blockformat: format %d pos %d", c.format, c.pos);
   935                     group->blockInserted(it);
   941                     group->blockInserted(it);
   936             } else if (group) {
   942             } else if (group) {
   937                 group->blockFormatChanged(it);
   943                 group->blockFormatChanged(it);
   938             }
   944             }
   939             documentChange(it.position(), it.length());
   945             documentChange(it.position(), it.length());
       
   946             editPos = -1;
   940 	    break;
   947 	    break;
   941 	}
   948 	}
   942 	case QTextUndoCommand::GroupFormatChange: {
   949 	case QTextUndoCommand::GroupFormatChange: {
   943             resetBlockRevision = -1; // ## TODO
   950             resetBlockRevision = -1; // ## TODO
   944             PMDEBUG("   group format change");
   951             PMDEBUG("   group format change");
   945             QTextObject *object = objectForIndex(c.objectIndex);
   952             QTextObject *object = objectForIndex(c.objectIndex);
   946             int oldFormat = formats.objectFormatIndex(c.objectIndex);
   953             int oldFormat = formats.objectFormatIndex(c.objectIndex);
   947             changeObjectFormat(object, c.format);
   954             changeObjectFormat(object, c.format);
   948             c.format = oldFormat;
   955             c.format = oldFormat;
       
   956             editPos = -1;
   949 	    break;
   957 	    break;
   950 	}
   958 	}
   951 	case QTextUndoCommand::Custom:
   959 	case QTextUndoCommand::Custom:
   952             resetBlockRevision = -1; // ## TODO
   960             resetBlockRevision = -1; // ## TODO
   953             if (undo)
   961             if (undo)
   954                 c.custom->undo();
   962                 c.custom->undo();
   955             else
   963             else
   956                 c.custom->redo();
   964                 c.custom->redo();
       
   965             editPos = -1;
   957 	    break;
   966 	    break;
   958 	default:
   967 	default:
   959 	    Q_ASSERT(false);
   968 	    Q_ASSERT(false);
   960         }
   969         }
   961 
   970 
   977                 );
   986                 );
   978         if (!inBlock)
   987         if (!inBlock)
   979             break;
   988             break;
   980     }
   989     }
   981     undoEnabled = true;
   990     undoEnabled = true;
   982     int editPos = -1;
   991     if (editPos < 0 && docChangeFrom >= 0) {
   983     if (docChangeFrom >= 0) {
       
   984         editPos = qMin(docChangeFrom + docChangeLength, length() - 1);
   992         editPos = qMin(docChangeFrom + docChangeLength, length() - 1);
   985     }
   993     }
   986     endEditBlock();
   994     endEditBlock();
   987     emitUndoAvailable(isUndoAvailable());
   995     emitUndoAvailable(isUndoAvailable());
   988     emitRedoAvailable(isRedoAvailable());
   996     emitRedoAvailable(isRedoAvailable());
  1112     Q_ASSERT(editBlock > 0);
  1120     Q_ASSERT(editBlock > 0);
  1113     if (--editBlock)
  1121     if (--editBlock)
  1114         return;
  1122         return;
  1115 
  1123 
  1116     if (undoEnabled && undoState > 0) {
  1124     if (undoEnabled && undoState > 0) {
       
  1125         const bool wasBlocking = !undoStack[undoState - 1].block_end;
  1117         if (undoStack[undoState - 1].block_part) {
  1126         if (undoStack[undoState - 1].block_part) {
  1118             undoStack[undoState - 1].block_end = true;
  1127             undoStack[undoState - 1].block_end = true;
  1119             emit document()->undoCommandAdded();
  1128             if (wasBlocking)
       
  1129                 emit document()->undoCommandAdded();
  1120         }
  1130         }
  1121     }
  1131     }
  1122 
  1132 
  1123     finishEdit();
  1133     finishEdit();
  1124 }
  1134 }