tracks.cpp 5.65 KB
Newer Older
1 2 3 4
/** ===========================================================
 * @file
 *
 * This file is a part of kipi-plugins project
Gilles Caulier's avatar
Gilles Caulier committed
5
 *
6 7 8 9
 *
 * @date   2006-09-19
 * @brief  Track file loading and managing
 *
Gilles Caulier's avatar
Gilles Caulier committed
10
 * @author Copyright (C) 2006-2015 by Gilles Caulier
11
 *         <a href="mailto:caulier dot gilles at gmail dot com">caulier dot gilles at gmail dot com</a>
Gilles Caulier's avatar
polish  
Gilles Caulier committed
12
 * @author Copyright (C) 2010-2014 by Michael G. Hansen
13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *         <a href="mailto:mike at mghansen dot de">mike at mghansen dot de</a>
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * ============================================================ */

Gilles Caulier's avatar
port ++  
Gilles Caulier committed
27
#include "tracks.h"
28 29 30

// Qt includes

31
#include <QtConcurrent/QtConcurrentMap>
32 33 34 35 36
#include <QFuture>
#include <QFutureWatcher>

// KDE includes

Gilles Caulier's avatar
polish  
Gilles Caulier committed
37
#include <klocalizedstring.h>
38 39 40

// local includes

41
#include "trackreader.h"
42 43 44 45 46 47 48 49

namespace KGeoMap
{

// TrackManager::TrackPoint ---------------------------------------------------

bool TrackManager::TrackPoint::EarlierThan(const TrackPoint& a, const TrackPoint& b)
{
Gilles Caulier's avatar
port ++  
Gilles Caulier committed
50
    return (a.dateTime < b.dateTime);
51 52 53 54
}

// TrackManager ---------------------------------------------------------------

55
class Q_DECL_HIDDEN TrackManager::Private
56 57 58 59
{
public:

    Private()
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
60
      : trackLoadFutureWatcher(nullptr),
61 62
        trackLoadFuture(),
        trackList(),
63
        loadErrorFiles(),
64
        nextTrackId(1),
65 66
        nextTrackColor(0),
        visibility(true)
67 68 69
    {
    }

70
    QFutureWatcher<TrackReader::TrackReadResult>* trackLoadFutureWatcher;
Gilles Caulier's avatar
polish  
Gilles Caulier committed
71 72 73
    QFuture<TrackReader::TrackReadResult>         trackLoadFuture;
    TrackManager::Track::List                     trackPendingList;
    TrackManager::Track::List                     trackList;
Teemu Rytilahti's avatar
Teemu Rytilahti committed
74
    QList<QPair<QUrl, QString> >                  loadErrorFiles;
Gilles Caulier's avatar
polish  
Gilles Caulier committed
75 76 77 78

    Id                                            nextTrackId;
    int                                           nextTrackColor;
    bool                                          visibility;
79 80 81
};

TrackManager::TrackManager(QObject* const parent)
82 83
    : QObject(parent),
      d(new Private)
84 85 86 87 88 89 90
{
}

TrackManager::~TrackManager()
{
}

91 92 93 94 95 96 97
bool TrackManager::getVisibility() const
{
    return d->visibility;
}

void TrackManager::setVisibility(const bool value)
{
Gilles Caulier's avatar
polish  
Gilles Caulier committed
98
    if (d->visibility == value)
99 100 101 102 103 104 105 106
    {
        return;
    }

    d->visibility = value;
    emit(signalVisibilityChanged(value));
}

107 108
void TrackManager::clear()
{
Yuri Chornoivan's avatar
Yuri Chornoivan committed
109
    /// @todo send a signal
110 111 112 113 114 115 116 117
    d->trackList.clear();
}

const TrackManager::Track& TrackManager::getTrack(const int index) const
{
    return d->trackList.at(index);
}

Teemu Rytilahti's avatar
Teemu Rytilahti committed
118
void TrackManager::loadTrackFiles(const QList<QUrl>& urls)
119
{
120
    d->trackLoadFutureWatcher = new QFutureWatcher<TrackReader::TrackReadResult>(this);
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

    connect(d->trackLoadFutureWatcher, SIGNAL(resultsReadyAt(int,int)),
            this, SLOT(slotTrackFilesReadyAt(int,int)));

    connect(d->trackLoadFutureWatcher, SIGNAL(finished()),
            this, SLOT(slotTrackFilesFinished()));

    d->trackLoadFuture = QtConcurrent::mapped(urls, TrackReader::loadTrackFile);
    d->trackLoadFutureWatcher->setFuture(d->trackLoadFuture);

    // results are reported to slotTrackFilesReadyAt
}

void TrackManager::slotTrackFilesReadyAt(int beginIndex, int endIndex)
{
Gilles Caulier's avatar
Gilles Caulier committed
136
    //const int nFilesBefore = d->trackList.count();
137 138

    // note that endIndex is exclusive!
Gilles Caulier's avatar
Gilles Caulier committed
139
    for (int i = beginIndex; i < endIndex; ++i)
140
    {
141
        const TrackReader::TrackReadResult nextFile = d->trackLoadFuture.resultAt(i);
142 143 144

        if (nextFile.isValid)
        {
145
            Track nextTrack = nextFile.track;
Gilles Caulier's avatar
Gilles Caulier committed
146
            nextTrack.id    = getNextFreeTrackId();
147
            nextTrack.color = getNextFreeTrackColor();
148
            d->trackPendingList << nextTrack;
149 150 151
        }
        else
        {
Teemu Rytilahti's avatar
Teemu Rytilahti committed
152
            d->loadErrorFiles << QPair<QUrl, QString>(nextFile.track.url, nextFile.loadError);
153 154 155 156 157 158 159 160
        }
    }
}

void TrackManager::slotTrackFilesFinished()
{
    d->trackLoadFutureWatcher->deleteLater();

161 162
    d->trackList << d->trackPendingList;
    QList<TrackChanges> trackChanges;
163

164 165 166 167
    Q_FOREACH(const Track& track, d->trackPendingList)
    {
        trackChanges << TrackChanges(track.id, ChangeAdd);
    }
168

169 170
    d->trackPendingList.clear();

171
    emit(signalAllTrackFilesReady());
172
    emit(signalTracksChanged(trackChanges));
173 174 175 176 177 178 179 180 181 182 183 184
}

TrackManager::Track::List TrackManager::getTrackList() const
{
    return d->trackList;
}

int TrackManager::trackCount() const
{
    return d->trackList.count();
}

Teemu Rytilahti's avatar
Teemu Rytilahti committed
185
QList<QPair<QUrl, QString> > TrackManager::readLoadErrors()
186
{
Teemu Rytilahti's avatar
Teemu Rytilahti committed
187
    const QList<QPair<QUrl, QString> > result = d->loadErrorFiles;
188 189 190 191 192
    d->loadErrorFiles.clear();

    return result;
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
quint64 TrackManager::getNextFreeTrackId()
{
    const quint64 nextId = d->nextTrackId;
    ++(d->nextTrackId);

    return nextId;
}

TrackManager::Track TrackManager::getTrackById(const quint64 trackId) const
{
    Q_FOREACH(const Track& track, d->trackList)
    {
        if (track.id == trackId)
        {
            return track;
        }
    }

    return TrackManager::Track();
}

214 215 216 217 218 219 220
QColor TrackManager::getNextFreeTrackColor()
{
    QList<QColor> colorList;
    colorList << Qt::red << Qt::blue << Qt::green << Qt::magenta;

    const QColor nextColor = colorList.at(d->nextTrackColor);
    ++(d->nextTrackColor);
221

222 223 224 225 226 227 228 229
    if (d->nextTrackColor >= colorList.count())
    {
        d->nextTrackColor = 0;
    }

    return nextColor;
}

230
} // namespace KGeoMap