File indexing completed on 2024-04-14 15:51:04

0001 /**
0002  * SPDX-FileCopyrightText: (C) 2003 Sébastien Laoût <slaout@linux62.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "noteselection.h"
0008 
0009 #include <QDebug>
0010 
0011 #include "note.h"
0012 #include "notecontent.h"
0013 
0014 /** Class NoteSelection: */
0015 
0016 NoteSelection *NoteSelection::nextStacked()
0017 {
0018     // First, search in the children:
0019     if (firstChild) {
0020         if (firstChild->note && firstChild->note->content())
0021             return firstChild;
0022         else
0023             return firstChild->nextStacked();
0024     }
0025     // Then, in the next:
0026     if (next) {
0027         if (next->note && next->note->content())
0028             return next;
0029         else
0030             return next->nextStacked();
0031     }
0032     // And finally, in the parent:
0033     NoteSelection *node = parent;
0034     while (node) {
0035         if (node->next) {
0036             if (node->next->note && node->next->note->content())
0037                 return node->next;
0038             else
0039                 return node->next->nextStacked();
0040         } else
0041             node = node->parent;
0042     }
0043     // Not found:
0044     return nullptr;
0045 }
0046 
0047 NoteSelection *NoteSelection::firstStacked()
0048 {
0049     if (!this)
0050         return nullptr;
0051 
0052     if (note && note->content())
0053         return this;
0054     else
0055         return nextStacked();
0056 }
0057 
0058 void NoteSelection::append(NoteSelection *node)
0059 {
0060     if (!this || !node)
0061         return;
0062 
0063     if (firstChild) {
0064         NoteSelection *last = firstChild;
0065         while (last->next)
0066             last = last->next;
0067         last->next = node;
0068     } else
0069         firstChild = node;
0070 
0071     while (node) {
0072         node->parent = this;
0073         node = node->next;
0074     }
0075 }
0076 
0077 int NoteSelection::count()
0078 {
0079     if (!this)
0080         return 0;
0081 
0082     int count = 0;
0083 
0084     for (NoteSelection *node = this; node; node = node->next)
0085         if (node->note && node->note->content())
0086             ++count;
0087         else
0088             count += node->firstChild->count();
0089 
0090     return count;
0091 }
0092 
0093 QList<Note *> NoteSelection::parentGroups()
0094 {
0095     QList<Note *> groups;
0096 
0097     // For each note:
0098     for (NoteSelection *node = firstStacked(); node; node = node->nextStacked())
0099         // For each parent groups of the note:
0100         for (Note *note = node->note->parentNote(); note; note = note->parentNote())
0101             // Add it (if it was not already in the list):
0102             if (!note->isColumn() && !groups.contains(note))
0103                 groups.append(note);
0104 
0105     return groups;
0106 }
0107 
0108 void debugSel(NoteSelection *sel, int n = 0)
0109 {
0110     for (NoteSelection *node = sel; node; node = node->next) {
0111         for (int i = 0; i < n; i++)
0112             qDebug() << "-";
0113         qDebug() << (node->firstChild ? "Group" : node->note->content()->toText(QString()));
0114         if (node->firstChild)
0115             debugSel(node->firstChild, n + 1);
0116     }
0117 }