File indexing completed on 2024-05-12 04:43:04

0001 /***************************************************************************
0002  *   Copyright (C) 2018 by Emmanuel Lepage Vallee                          *
0003  *   Author : Emmanuel Lepage Vallee <emmanuel.lepage@kde.org>             *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 3 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0017  **************************************************************************/
0018 #include "continuity_p.h"
0019 
0020 #include "index_p.h"
0021 
0022 void StateTracker::Continuity::remove(StateTracker::Index *i)
0023 {
0024     if (!i->m_pContinuity)
0025         return;
0026 
0027     // Free the existing StateTracker::Continuity
0028     if (i->m_pContinuity->size() == 1) {
0029         delete i->m_pContinuity;
0030         i->m_pContinuity = nullptr;
0031         return;
0032     }
0033 
0034     // Make sure first()/last() are not outdated
0035     if (i->m_pContinuity->m_pFirst == i)
0036         i->m_pContinuity->m_pFirst = i->nextSibling();
0037 
0038     if (i->m_pContinuity->m_pLast == i)
0039         i->m_pContinuity->m_pLast = i->previousSibling();
0040 
0041     i->m_pContinuity = nullptr;
0042 }
0043 
0044 void StateTracker::Continuity::setContinuity(StateTracker::Index *i, StateTracker::Continuity *c)
0045 {
0046     if (i->m_pContinuity == c)
0047         return;
0048 
0049     remove(i);
0050 
0051     i->m_pContinuity = c;
0052 }
0053 
0054 StateTracker::Continuity::Continuity(Index *first) :
0055     m_pFirst(first), m_pLast(first)
0056 {}
0057 
0058 int StateTracker::Continuity::size() const
0059 {
0060     if (!first())
0061         return 0;
0062 
0063     // +1 it starts at zero
0064     return (last()->effectiveRow() - first()->effectiveRow()) + 1;
0065 }
0066 
0067 StateTracker::Index *StateTracker::Continuity::first() const
0068 {
0069     return m_pFirst;
0070 }
0071 
0072 StateTracker::Index *StateTracker::Continuity::last() const
0073 {
0074     return m_pLast;
0075 }
0076 
0077 StateTracker::Continuity *StateTracker::Continuity::select(StateTracker::Index *i)
0078 {
0079     auto prev = i->previousSibling();
0080     auto next = i->nextSibling();
0081     auto old  = i->m_pContinuity;
0082 
0083     if (prev && i->isNeighbor(prev)) {
0084         if (prev->continuityTracker()->last() == prev)
0085             prev->continuityTracker()->m_pLast = i;
0086         i->m_pContinuity = prev->continuityTracker();
0087     }
0088     else if (next && i->isNeighbor(next)) {
0089         if (next->continuityTracker()->first() == next)
0090             next->continuityTracker()->m_pFirst = i;
0091         i->m_pContinuity = next->continuityTracker();
0092     }
0093     else if (!old)
0094         i->m_pContinuity = new Continuity(i);
0095 
0096     Q_ASSERT(i->m_pContinuity);
0097 
0098     return i->m_pContinuity;
0099 }
0100 
0101 void StateTracker::Continuity::split(StateTracker::Index *at)
0102 {
0103     Q_ASSERT(false); //TODO
0104     auto old = at->m_pContinuity;
0105     auto n   = new Continuity(at);
0106 
0107     do {
0108         at->m_pContinuity = n;
0109     } while((at = at->nextSibling()) && at->m_pContinuity == old);
0110 }
0111 
0112 void StateTracker::Continuity::merge(StateTracker::Index *one, StateTracker::Index *two)
0113 {
0114     Q_UNUSED(one)
0115     Q_UNUSED(two)
0116     Q_ASSERT(false); //TODO
0117 }