1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
/***************************************************************************
MetaDataList.cpp - list with meta data objects
-------------------
begin : Sat Mar 06 2010
copyright : (C) 2010 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* 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 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#include <algorithm>
#include "libkwave/MetaDataList.h"
#include "libkwave/String.h"
#include "libkwave/Utils.h"
//***************************************************************************
Kwave::MetaDataList::MetaDataList()
:QMap<QString, Kwave::MetaData>()
{
}
//***************************************************************************
Kwave::MetaDataList::MetaDataList(const Kwave::MetaData &meta)
:QMap<QString, Kwave::MetaData>()
{
add(meta);
}
//***************************************************************************
Kwave::MetaDataList::~MetaDataList()
{
}
//***************************************************************************
static bool isLessThan(const Kwave::MetaData &m1, const Kwave::MetaData &m2)
{
return m1.firstSample() < m2.firstSample();
}
//***************************************************************************
QList<Kwave::MetaData> Kwave::MetaDataList::toSortedList() const
{
QList<Kwave::MetaData> list = this->values();
if (!list.isEmpty())
std::stable_sort(list.begin(), list.end(), isLessThan);
return list;
}
//***************************************************************************
Kwave::MetaDataList Kwave::MetaDataList::selectByType(const QString &type) const
{
return selectByValue(Kwave::MetaData::STDPROP_TYPE, type);
}
//***************************************************************************
Kwave::MetaDataList Kwave::MetaDataList::selectByRange(
sample_index_t first, sample_index_t last) const
{
Kwave::MetaDataList list;
Iterator it(*this);
while (it.hasNext()) {
it.next();
const Kwave::MetaData &m = it.value();
if (m.scope() == Kwave::MetaData::Position) {
if (m.hasProperty(Kwave::MetaData::STDPROP_POS)) {
// check for position within the range
bool pos_ok = false;
const sample_index_t pos =
m[Kwave::MetaData::STDPROP_POS].toLongLong(&pos_ok);
if (pos_ok && (pos >= first) && (pos <= last))
list.add(m);
}
}
}
return list;
}
//***************************************************************************
Kwave::MetaDataList Kwave::MetaDataList::selectByPosition(
sample_index_t pos) const
{
Kwave::MetaDataList list;
Iterator it(*this);
while (it.hasNext()) {
it.next();
const Kwave::MetaData &m = it.value();
if (m.scope() == Kwave::MetaData::Position) {
if (m.hasProperty(Kwave::MetaData::STDPROP_POS)) {
// check for position within the range
bool pos_ok = false;
const sample_index_t p =
m[Kwave::MetaData::STDPROP_POS].toLongLong(&pos_ok);
if (pos_ok && (p == pos))
list.add(m);
}
}
}
return list;
}
//***************************************************************************
Kwave::MetaDataList Kwave::MetaDataList::selectByProperty(
const QString &property) const
{
Kwave::MetaDataList list;
Iterator it(*this);
while (it.hasNext()) {
it.next();
const Kwave::MetaData &m = it.value();
if (m.hasProperty(property))
list.add(m);
}
return list;
}
//***************************************************************************
Kwave::MetaDataList Kwave::MetaDataList::selectByValue(
const QString &property, QVariant value) const
{
Kwave::MetaDataList list;
Iterator it(*this);
while (it.hasNext()) {
it.next();
const Kwave::MetaData &m = it.value();
if (m.hasProperty(property) && (m[property] == value))
list.add(m);
}
return list;
}
//***************************************************************************
bool Kwave::MetaDataList::contains(const Kwave::MetaData &metadata) const
{
QString id = metadata.id();
Iterator it(*this);
while (it.hasNext()) {
it.next();
const Kwave::MetaData &m = it.value();
if (m.id() == id)
return true;
}
return false;
}
//***************************************************************************
void Kwave::MetaDataList::replace(const Kwave::MetaDataList &list)
{
if (list.isEmpty()) return;
// find out which meta data types are affected
QStringList types;
foreach (const Kwave::MetaData &meta, list) {
QString type = meta[Kwave::MetaData::STDPROP_TYPE].toString();
if (!types.contains(type)) {
// remember this type in our list
types.append(type);
// remove all elements of that type that are not in the new list
MutableIterator it(*this);
while (it.hasNext()) {
it.next();
Kwave::MetaData &m = it.value();
if (m[Kwave::MetaData::STDPROP_TYPE] == type) {
if (!list.contains(m)) {
it.remove();
}
}
}
}
}
// now the same as in add() has to be done
add(list);
}
//***************************************************************************
void Kwave::MetaDataList::add(const Kwave::MetaData &metadata)
{
if (!metadata.isNull())
(*this)[metadata.id()] = metadata;
else
remove(metadata);
}
//***************************************************************************
void Kwave::MetaDataList::add(const Kwave::MetaDataList &list)
{
foreach (const Kwave::MetaData &metadata, list)
add(metadata);
}
//***************************************************************************
void Kwave::MetaDataList::remove(const Kwave::MetaData &metadata)
{
if (contains(metadata))
QMap<QString, Kwave::MetaData>::remove(metadata.id());
}
//***************************************************************************
void Kwave::MetaDataList::remove(const Kwave::MetaDataList &list)
{
foreach (const Kwave::MetaData &metadata, list)
remove(metadata);
}
//***************************************************************************
void Kwave::MetaDataList::cropByRange(sample_index_t first,
sample_index_t last)
{
MutableIterator it(*this);
while (it.hasNext()) {
it.next();
Kwave::MetaData &m = it.value();
if (m.scope() & Kwave::MetaData::Position) {
// if the meta data is bound to a position, remove it if
// it is out of scope
const QVariant v_pos = m[Kwave::MetaData::STDPROP_POS];
bool ok = false;
sample_index_t pos = static_cast<sample_index_t>(
v_pos.toULongLong(&ok));
if (!ok) continue;
if ((pos < first) || (pos > last)) {
// out of the selected area -> remove
it.remove();
}
}
}
}
//***************************************************************************
Kwave::MetaDataList Kwave::MetaDataList::copy(sample_index_t offset,
sample_index_t length) const
{
if (!length)
return Kwave::MetaDataList(); // no range selected - empty list
Kwave::MetaDataList list(*this);
list.cropByRange(offset, offset + length - 1);
return list;
}
//***************************************************************************
void Kwave::MetaDataList::deleteRange(sample_index_t offset,
sample_index_t length)
{
const sample_index_t del_first = offset;
const sample_index_t del_last = offset + length - 1;
if (!length) return;
MutableIterator it(*this);
while (it.hasNext()) {
it.next();
Kwave::MetaData &meta = it.value();
sample_index_t meta_first = meta.firstSample();
sample_index_t meta_last = meta.lastSample();
// check: range overlap?
if ((meta_first > del_last) || (meta_last < del_first))
continue;
// position bound -> remove completely
if (meta.hasProperty(Kwave::MetaData::STDPROP_POS))
it.remove();
}
}
//***************************************************************************
void Kwave::MetaDataList::shiftLeft(sample_index_t offset,
sample_index_t shift)
{
MutableIterator it(*this);
while (it.hasNext()) {
it.next();
Kwave::MetaData &meta = it.value();
// check: is it before the offset ?
sample_index_t meta_first = meta.firstSample();
if (meta_first < offset)
continue;
// position bound -> move position
if (meta.hasProperty(Kwave::MetaData::STDPROP_POS)) {
bool ok = false;
sample_index_t pos = static_cast<sample_index_t>(
meta[Kwave::MetaData::STDPROP_POS].toULongLong(&ok));
if (!ok) continue;
if (pos >= shift) {
// shift position left
meta[Kwave::MetaData::STDPROP_POS] = pos - shift;
} else {
// do not produce negative coordinates
// -> moving into negative means deleting!
it.remove();
}
}
}
}
//***************************************************************************
void Kwave::MetaDataList::shiftRight(sample_index_t offset,
sample_index_t shift)
{
MutableIterator it(*this);
it.toBack();
while (it.hasPrevious()) {
it.previous();
Kwave::MetaData &meta = it.value();
// check: is it before the offset ?
sample_index_t meta_last = meta.lastSample();
if (meta_last < offset)
continue;
// position bound -> move position
if (meta.hasProperty(Kwave::MetaData::STDPROP_POS)) {
bool ok = false;
sample_index_t pos = static_cast<sample_index_t>(
meta[Kwave::MetaData::STDPROP_POS].toULongLong(&ok));
if (!ok) continue;
Q_ASSERT(pos + shift >= pos);
if (pos + shift >= pos) {
// shift position right
meta[Kwave::MetaData::STDPROP_POS] = pos + shift;
} else {
// do not produce a coordinate overflow
// -> moving outside range means deleting!
it.remove();
}
}
}
}
//***************************************************************************
void Kwave::MetaDataList::scalePositions(double scale)
{
MutableIterator it(*this);
while (it.hasNext()) {
it.next();
Kwave::MetaData &meta = it.value();
// position bound -> move position
if (meta.hasProperty(Kwave::MetaData::STDPROP_POS)) {
bool ok = false;
sample_index_t pos = static_cast<sample_index_t>(
meta[Kwave::MetaData::STDPROP_POS].toULongLong(&ok));
if (!ok) continue;
sample_index_t scaled_pos = static_cast<sample_index_t>(
static_cast<double>(pos) * scale);
if (scaled_pos <= SAMPLE_INDEX_MAX) {
// scale position
meta[Kwave::MetaData::STDPROP_POS] = scaled_pos;
} else {
// do not produce a coordinate overflow
// -> moving outside range means deleting!
it.remove();
}
}
}
}
//***************************************************************************
void Kwave::MetaDataList::dump() const
{
qDebug("--- meta data ---");
Iterator it(*this);
while (it.hasNext()) {
it.next();
const Kwave::MetaData &meta = it.value();
qDebug("* meta data #%s", DBG(it.key()));
meta.dump();
}
qDebug("-----------------");
}
//***************************************************************************
//***************************************************************************
|