UnitDatabase.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "UnitDatabase.h"
17 
20 
23 {
24  return &m_instance;
25 }
26 
28  m_nextId(0)
29 {
30 }
31 
32 QStringList
34 {
35  QStringList list;
36  for (UnitMap::const_iterator i = m_units.begin(); i != m_units.end(); ++i) {
37  list.push_back(i->first);
38  }
39  return list;
40 }
41 
42 void
44 {
45  if (m_units.find(unit) == m_units.end()) {
46  m_units[unit] = m_nextId++;
47  emit unitDatabaseChanged();
48  }
49 }
50 
51 int
52 UnitDatabase::getUnitId(QString unit, bool registerNew)
53 {
54  if (m_units.find(unit) == m_units.end()) {
55  if (registerNew) registerUnit(unit);
56  else return -1;
57  }
58  return m_units[unit];
59 }
60 
61 QString
63 {
64  for (UnitMap::const_iterator i = m_units.begin(); i != m_units.end(); ++i) {
65  if (i->second == id) return i->first;
66  }
67  return "";
68 }
69 
int getUnitId(QString unit, bool registerNew=true)
Return the reference id for a given unit name.
void registerUnit(QString unit)
static UnitDatabase * getInstance()
void unitDatabaseChanged()
static UnitDatabase m_instance
Definition: UnitDatabase.h:58
QString getUnitById(int id)
QStringList getKnownUnits() const
UnitMap m_units
Definition: UnitDatabase.h:55