File indexing completed on 2024-05-26 16:15:48

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007 Fredy Yanardi <fyanardi@gmail.com>
0003  * Copyright (c) 2011 Boudewijn Rempt <boud@kogmbh.com>
0004  * Copyright (C) 2012 Inge Wallin <inge@lysator.liu.se>
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Library General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Library General Public License
0017  * along with this library; see the file COPYING.LIB.  If not, write to
0018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020  */
0021 
0022 #include "KoAnnotationManager.h"
0023 #include "KoAnnotation.h"
0024 
0025 #include "TextDebug.h"
0026 #include <QHash>
0027 
0028 class KoAnnotationManagerPrivate
0029 {
0030 public:
0031     KoAnnotationManagerPrivate() { }
0032     QHash<QString, KoAnnotation*> annotationHash;
0033     QList<QString> annotationNameList;
0034     int lastId;
0035 };
0036 
0037 KoAnnotationManager::KoAnnotationManager()
0038         : d(new KoAnnotationManagerPrivate)
0039 {
0040 }
0041 
0042 KoAnnotationManager::~KoAnnotationManager()
0043 {
0044     delete d;
0045 }
0046 
0047 void KoAnnotationManager::insert(const QString &name, KoAnnotation *annotation)
0048 {
0049     annotation->setName(name);
0050     d->annotationHash[name] = annotation;
0051     d->annotationNameList.append(name);
0052 }
0053 
0054 void KoAnnotationManager::remove(const QString &name)
0055 {
0056     d->annotationHash.remove(name);
0057     d->annotationNameList.removeAt(d->annotationNameList.indexOf(name));
0058 }
0059 
0060 void KoAnnotationManager::rename(const QString &oldName, const QString &newName)
0061 {
0062     QHash<QString, KoAnnotation*>::iterator i = d->annotationHash.begin();
0063 
0064     while (i != d->annotationHash.end()) {
0065         if (i.key() == oldName) {
0066             KoAnnotation *annotation = d->annotationHash.take(i.key());
0067             annotation->setName(newName);
0068             d->annotationHash.insert(newName, annotation);
0069             int listPos = d->annotationNameList.indexOf(oldName);
0070             d->annotationNameList.replace(listPos, newName);
0071             return;
0072         }
0073         ++i;
0074     }
0075 }
0076 
0077 KoAnnotation *KoAnnotationManager::annotation(const QString &name) const
0078 {
0079     KoAnnotation *annotation = d->annotationHash.value(name);
0080     return annotation;
0081 }
0082 
0083 QList<QString> KoAnnotationManager::annotationNameList() const
0084 {
0085     return d->annotationNameList;
0086 }