File indexing completed on 2024-05-26 16:11:33

0001 /*
0002  *  Copyright (c) 2009 Cyrille Berger <cberger@cberger.net>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation;
0007  * either version 2, or (at your option) any later version of the License.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public License
0015  * along with this library; see the file COPYING.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include <KLocalizedString>
0021 
0022 #include "SectionGroup.h"
0023 #include "Section.h"
0024 #include "RootSection.h"
0025 
0026 int SectionGroup::s_count = 0;
0027 
0028 SectionGroup::SectionGroup(SectionGroup* parent) : m_parent(parent)
0029 {
0030 }
0031 
0032 SectionGroup::SectionGroup(const SectionGroup& _rhs) : m_parent(0)
0033 {
0034     foreach(Section * section, _rhs.m_children) {
0035         insertSection(new Section(*section));
0036     }
0037 }
0038 
0039 SectionGroup::~SectionGroup()
0040 {
0041 }
0042 
0043 void SectionGroup::insertSection(Section* page, int index)
0044 {
0045     if(page->sectionParent() == this) return;
0046     if(page->sectionParent()) page->sectionParent()->removeSection(page);
0047     m_children.insert(index, page);
0048     page->setSectionParent(this);
0049 }
0050 
0051 void SectionGroup::insertSection(Section* page, Section* before)
0052 {
0053     if(before == 0) {
0054         insertSection(page, m_children.count());
0055     } else {
0056         insertSection(page, m_children.indexOf(before));
0057     }
0058 }
0059 
0060 void SectionGroup::removeSection(Section* page)
0061 {
0062     page->setSectionParent(0);
0063     m_children.removeAll(page);
0064 }
0065 
0066 QList<Section*> SectionGroup::sections() const
0067 {
0068     return m_children;
0069 }
0070 
0071 Section* SectionGroup::newSection(Section* before)
0072 {
0073     SectionGroup* root = this;
0074     while(root->sectionParent()) root = root->sectionParent();
0075     Section* section = new Section(dynamic_cast<RootSection*>(root)) ;
0076     insertSection(section, before);
0077     section->setName(nextName());
0078     return section;
0079 }
0080 
0081 QString SectionGroup::nextName()
0082 {
0083     return i18n("Untitled %1", ++s_count);
0084 }
0085 
0086 SectionGroup* SectionGroup::sectionParent()
0087 {
0088     return m_parent;
0089 }
0090 
0091 void SectionGroup::setSectionParent(SectionGroup* parent)
0092 {
0093     m_parent = parent;
0094 }
0095 
0096 void SectionGroup::sectionAdded(Section* page)
0097 {
0098     if(m_parent)
0099         m_parent->sectionAdded(page);
0100 }
0101 
0102 void SectionGroup::sectionRemoved(Section* page)
0103 {
0104     if(m_parent)
0105         m_parent->sectionRemoved(page);
0106 }
0107 
0108 int SectionGroup::indexOf(Section* section)
0109 {
0110     return m_children.indexOf(section);
0111 }
0112 
0113 Section* SectionGroup::nextSection(Section* section)
0114 {
0115     int idx = indexOf(section);
0116     idx += 1;
0117     Q_ASSERT(idx >= 0);
0118     if(idx < m_children.count()) {
0119         return m_children[idx];
0120     } else {
0121         return 0;
0122     }
0123 }