~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to kchart/kdchart/kdchartserializer/src/KDChartIdMapper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C++ -*-
2
 
   KDChart - a multi-platform charting engine
3
 
   */
4
 
 
5
 
/****************************************************************************
6
 
 ** Copyright (C) 2001-2003 Klarälvdalens Datakonsult AB.  All rights reserved.
7
 
 **
8
 
 ** This file is part of the KDChart library.
9
 
 **
10
 
 ** This file may be used under the terms of the GNU General Public
11
 
 ** License versions 2.0 or 3.0 as published by the Free Software
12
 
 ** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
13
 
 ** included in the packaging of this file.  Alternatively you may (at
14
 
 ** your option) use any later version of the GNU General Public
15
 
 ** License if such license has been publicly approved by
16
 
 ** Klarälvdalens Datakonsult AB (or its successors, if any).
17
 
 ** 
18
 
 ** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
19
 
 ** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
20
 
 ** A PARTICULAR PURPOSE. Klarälvdalens Datakonsult AB reserves all rights
21
 
 ** not expressly granted herein.
22
 
 ** 
23
 
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
24
 
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25
 
 **
26
 
 **********************************************************************/
27
 
 
28
 
#include <KDChartIdMapper.h>
29
 
#include <KDChartIdMapper_p.h>
30
 
 
31
 
#include <QDebug>
32
 
 
33
 
#define d d_func()
34
 
 
35
 
using namespace KDChart;
36
 
 
37
 
IdMapper::Private::Private( IdMapper* qq )
38
 
    : q( qq )
39
 
{
40
 
}
41
 
 
42
 
IdMapper::Private::~Private() {}
43
 
 
44
 
IdMapper::IdMapper()
45
 
    : _d( new Private( this ) )
46
 
{
47
 
    d->m_counterTag = QString::fromLatin1( "::C_O_U_N_T_E_R" );
48
 
}
49
 
 
50
 
IdMapper::~IdMapper()
51
 
{
52
 
    clear();
53
 
    delete _d; _d = 0;
54
 
}
55
 
 
56
 
void IdMapper::init()
57
 
{
58
 
}
59
 
 
60
 
void IdMapper::clear()
61
 
{
62
 
    // this space left empty intentionally
63
 
}
64
 
 
65
 
IdMapper* IdMapper::instance()
66
 
{
67
 
    static IdMapper instance;
68
 
    return &instance;
69
 
}
70
 
 
71
 
QString IdMapper::findOrMakeName(
72
 
        const void* id,
73
 
        const QString& baseName,
74
 
        bool& wasFound,
75
 
        bool useMapOfKnownElements )
76
 
{
77
 
    QString name = findName( id, useMapOfKnownElements );
78
 
    wasFound = ! name.isNull();
79
 
 
80
 
    if( ! wasFound ){
81
 
        QMap<const void*, QString>& map = useMapOfKnownElements ? d->m_mapOfKnownElements : d->m_unresolvedMap;
82
 
        // check if we have a counter stored already - if not we add one
83
 
        int counter = 1;
84
 
        QString counterName( baseName + d->m_counterTag );
85
 
        QMapIterator<const void*, QString> i( map );
86
 
        while( i.hasNext() ) {
87
 
            i.next();
88
 
            if( i.value() == counterName ){
89
 
                // we may cast away constness, since this is not
90
 
                // an external pointer but our own auxiliary counter entry
91
 
                int* storedCount = const_cast<int*>( static_cast<const int*>( i.key() ) );
92
 
                (*storedCount)++;
93
 
                counter = *storedCount;
94
 
            }
95
 
        }
96
 
        if( counter == 1 ){
97
 
            int* p = new int;
98
 
            *p = counter;
99
 
            map[ p ] = counterName;
100
 
        }
101
 
 
102
 
        // store a new name using the counter value, and return it
103
 
        name = baseName + ":" + QString::number( counter );
104
 
        map[ id ] = name;
105
 
    }
106
 
    return name;
107
 
}
108
 
 
109
 
 
110
 
QString IdMapper::findName( const void* id,
111
 
                            bool useMapOfKnownElements )const
112
 
{
113
 
    const QMap<const void*, QString>& map = useMapOfKnownElements ? d->m_mapOfKnownElements : d->m_unresolvedMap;
114
 
    if( map.contains( id ) )
115
 
        return map.value( id );
116
 
    return QString();
117
 
}
118
 
 
119
 
 
120
 
 
121
 
const void* IdMapper::findId( const QString& name,
122
 
                              bool useMapOfKnownElements )const
123
 
{
124
 
    const QMap<const void*, QString>& map = useMapOfKnownElements ? d->m_mapOfKnownElements : d->m_unresolvedMap;
125
 
    QMapIterator<const void*, QString> i( map );
126
 
    while( i.hasNext() ) {
127
 
        i.next();
128
 
        if( i.value() == name )
129
 
            return i.key();
130
 
    }
131
 
    return 0;
132
 
}
133
 
 
134
 
const QMap<const void*, QString> IdMapper::unresolvedMap()const
135
 
{
136
 
    return d->m_unresolvedMap;
137
 
}
138
 
 
139
 
void IdMapper::debugOut()const
140
 
{
141
 
    qDebug() << "IdMapper::debugOut():";
142
 
    qDebug() << "map of known elements:";
143
 
    QMapIterator<const void*, QString> i( d->m_mapOfKnownElements );
144
 
    while( i.hasNext() ) {
145
 
        i.next();
146
 
        qDebug() << "key:" << i.key() << "pointer:" << i.value();
147
 
    }
148
 
    qDebug() << "unresolved map:";
149
 
    QMapIterator<const void*, QString> i2( d->m_unresolvedMap );
150
 
    while( i2.hasNext() ) {
151
 
        i2.next();
152
 
        qDebug() << "key:" << i2.key() << "pointer:" << i2.value();
153
 
    }
154
 
}