Logo Search packages:      
Sourcecode: tagua version File versions  Download package

void Game::undo (  ) 

undo

Definition at line 196 of file game.cpp.

References Index::flipVariation(), Index::prev(), and GamePrivate::Entry::variations.

Referenced by truncate().

                {
  if(undo_pos <= 0) {
    kError() << "Nothing to undo";
    return;
  }

  bool last_undo = undo_pos == 1;
  bool now_redo = undo_pos == (int)undo_history.size();

  undo_pos--;
  UndoOp* op = &(undo_history[undo_pos]);

  if(boost::get<UndoAdd>(op)) {
    UndoAdd *a = boost::get<UndoAdd>(op);

    if(a->index.atVariationStart() ) {
      Entry* e = fetch(a->index.prev());
      Q_ASSERT(e);

      int v = a->index.nested.back().variation;
      Q_ASSERT(e->variations.count(v) == 1);
      Q_ASSERT(e->variations[v].size() == 1);

      e->variations.erase(v);
    }
    else {
      int at;
      std::vector<Entry>* vec = fetchRef(a->index, &at);
      Q_ASSERT(vec);
      Q_ASSERT((int)vec->size() == at+1);

      vec->pop_back();
    }

    if(current == a->index) {
      current = current.prev();
      onCurrentIndexChanged();
    }

    onRemoved(a->index);
  }
  else if(boost::get<UndoPromote>(op)) {
    UndoPromote *p = boost::get<UndoPromote>(op);

    int at;
    std::vector<Entry>* vec = fetchRef(p->index, &at);
    Q_ASSERT(vec);
    Q_ASSERT((*vec)[at].variations.count(p->variation)==1);

    History vold = (*vec)[at].variations[p->variation];
    History vnew;
    for(int i=at+1; i<(int)vec->size(); i++)
      vnew.push_back((*vec)[i]);
    while((int)vec->size()>at+1)
      vec->pop_back();
    for(int i=0; i<(int)vold.size(); i++)
      vec->push_back(vold[i]);
    (*vec)[at].variations[p->variation] = vnew;

    current = current.flipVariation(p->index, p->variation);
    onPromoteVariation(p->index, p->variation);
    //onCurrentIndexChanged();
  }
  else if(boost::get<UndoTruncate>(op)) {
    UndoTruncate *t = boost::get<UndoTruncate>(op);

    int at;
    std::vector<Entry>* vec = fetchRef(t->index, &at);
    Q_ASSERT(vec);
    Q_ASSERT((int)vec->size() == at+1);
    Q_ASSERT((*vec)[at].variations.empty());

    for(int i=0;i<(int)t->history.size();i++)
      vec->push_back(t->history[i]);
    (*vec)[at].variations = t->variations;
    (*vec)[at].vcomments = t->vcomments;

    if(t->history.size())
      onAdded(t->index.next());
    for(Variations::iterator it = t->variations.begin(); it != t->variations.end(); ++it)
      onAdded(t->index.next(it->first));
    for(VComments::iterator it = t->vcomments.begin(); it != t->vcomments.end(); ++it)
      onSetVComment(t->index, it->first, it->second);
  }
  else if(boost::get<UndoRemove>(op)) {
    UndoRemove *r = boost::get<UndoRemove>(op);

    Entry *e = fetch(r->index);
    e->variations[r->variation] = r->history;
    onAdded(r->index.next(r->variation));
    if(!r->vcomment.isEmpty()) {
      e->vcomments[r->variation] = r->vcomment;
      onSetVComment(r->index, r->variation, r->vcomment);
    }
  }
  else if(boost::get<UndoClear>(op)) {
    UndoClear *c = boost::get<UndoClear>(op);

    Entry *e = fetch(c->index);
    e->variations = c->variations;
    e->vcomments = c->vcomments;
    for(Variations::iterator it = c->variations.begin(); it != c->variations.end(); ++it)
      onAdded(c->index.next(it->first));
    for(VComments::iterator it = c->vcomments.begin(); it != c->vcomments.end(); ++it)
      onSetVComment(c->index, it->first, it->second);
  }
  else if(boost::get<UndoSetComment>(op)) {
    UndoSetComment *sc = boost::get<UndoSetComment>(op);
    Entry *e = fetch(sc->index);
    Q_ASSERT(e);

    if(sc->variation == -1) {
      e->comment = sc->old_comment;
      onSetComment(sc->index, sc->old_comment);
    }
    else {
      if(sc->old_comment.isEmpty())
        e->vcomments.erase(sc->variation);
      else
        e->vcomments[sc->variation] = sc->old_comment;
      onSetVComment(sc->index, sc->variation, sc->old_comment);
    }
  }
  else
    kError() << "Unexpected type in boost::variant";

  if(last_undo)
    onAvailableUndo(false);
  if(now_redo)
    onAvailableRedo(true);
}


Generated by  Doxygen 1.6.0   Back to index