Chris@277: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@277: Chris@277: /* Chris@277: Sonic Visualiser Chris@277: An audio file viewer and annotation editor. Chris@277: Centre for Digital Music, Queen Mary, University of London. Chris@277: This file copyright 2007 QMUL. Chris@277: Chris@277: This program is free software; you can redistribute it and/or Chris@277: modify it under the terms of the GNU General Public License as Chris@277: published by the Free Software Foundation; either version 2 of the Chris@277: License, or (at your option) any later version. See the file Chris@277: COPYING included with this distribution for more information. Chris@277: */ Chris@277: Chris@277: #include "ColourDatabase.h" Chris@277: #include "XmlExportable.h" Chris@277: Chris@278: #include Chris@278: Chris@277: ColourDatabase Chris@277: ColourDatabase::m_instance; Chris@277: Chris@277: ColourDatabase * Chris@277: ColourDatabase::getInstance() Chris@277: { Chris@277: return &m_instance; Chris@277: } Chris@277: Chris@277: ColourDatabase::ColourDatabase() Chris@277: { Chris@277: } Chris@277: Chris@277: int Chris@277: ColourDatabase::getColourCount() const Chris@277: { Chris@277: return m_colours.size(); Chris@277: } Chris@277: Chris@277: QString Chris@277: ColourDatabase::getColourName(int c) const Chris@277: { Chris@277: if (c < 0 || size_t(c) >= m_colours.size()) return ""; Chris@277: return m_colours[c].name; Chris@277: } Chris@277: Chris@277: QColor Chris@277: ColourDatabase::getColour(int c) const Chris@277: { Chris@277: if (c < 0 || size_t(c) >= m_colours.size()) return Qt::black; Chris@277: return m_colours[c].colour; Chris@277: } Chris@277: Chris@277: QColor Chris@277: ColourDatabase::getColour(QString name) const Chris@277: { Chris@277: for (ColourList::const_iterator i = m_colours.begin(); Chris@277: i != m_colours.end(); ++i) { Chris@277: if (i->name == name) return i->colour; Chris@277: } Chris@277: Chris@277: return Qt::black; Chris@277: } Chris@277: Chris@277: int Chris@277: ColourDatabase::getColourIndex(QString name) const Chris@277: { Chris@277: int index = 0; Chris@277: for (ColourList::const_iterator i = m_colours.begin(); Chris@277: i != m_colours.end(); ++i) { Chris@277: if (i->name == name) return index; Chris@277: ++index; Chris@277: } Chris@277: Chris@277: return -1; Chris@277: } Chris@277: Chris@277: int Chris@277: ColourDatabase::getColourIndex(QColor c) const Chris@277: { Chris@277: int index = 0; Chris@277: for (ColourList::const_iterator i = m_colours.begin(); Chris@277: i != m_colours.end(); ++i) { Chris@277: if (i->colour == c) return index; Chris@277: ++index; Chris@277: } Chris@277: Chris@277: return -1; Chris@277: } Chris@277: Chris@277: bool Chris@277: ColourDatabase::useDarkBackground(int c) const Chris@277: { Chris@277: if (c < 0 || size_t(c) >= m_colours.size()) return false; Chris@277: return m_colours[c].darkbg; Chris@277: } Chris@277: Chris@277: void Chris@277: ColourDatabase::setUseDarkBackground(int c, bool dark) Chris@277: { Chris@277: if (c < 0 || size_t(c) >= m_colours.size()) return; Chris@278: if (m_colours[c].darkbg != dark) { Chris@278: m_colours[c].darkbg = dark; Chris@278: emit colourDatabaseChanged(); Chris@278: } Chris@277: } Chris@277: Chris@277: int Chris@277: ColourDatabase::addColour(QColor c, QString name) Chris@277: { Chris@277: int index = 0; Chris@277: for (ColourList::iterator i = m_colours.begin(); Chris@277: i != m_colours.end(); ++i) { Chris@277: if (i->name == name) { Chris@277: i->colour = c; Chris@277: return index; Chris@277: } Chris@277: ++index; Chris@277: } Chris@277: Chris@277: ColourRec rec; Chris@277: rec.colour = c; Chris@277: rec.name = name; Chris@277: rec.darkbg = false; Chris@277: m_colours.push_back(rec); Chris@277: emit colourDatabaseChanged(); Chris@277: return index; Chris@277: } Chris@277: Chris@277: void Chris@277: ColourDatabase::removeColour(QString name) Chris@277: { Chris@277: for (ColourList::iterator i = m_colours.begin(); Chris@277: i != m_colours.end(); ++i) { Chris@277: if (i->name == name) { Chris@277: m_colours.erase(i); Chris@277: return; Chris@277: } Chris@277: } Chris@277: } Chris@277: Chris@277: void Chris@277: ColourDatabase::getStringValues(int index, Chris@277: QString &colourName, Chris@277: QString &colourSpec, Chris@277: QString &darkbg) const Chris@277: { Chris@277: colourName = ""; Chris@277: colourSpec = ""; Chris@277: if (index < 0 || size_t(index) >= m_colours.size()) return; Chris@277: Chris@277: colourName = getColourName(index); Chris@277: colourSpec = XmlExportable::encodeColour(getColour(index)); Chris@277: darkbg = useDarkBackground(index) ? "true" : "false"; Chris@277: } Chris@277: Chris@277: int Chris@277: ColourDatabase::putStringValues(QString colourName, Chris@277: QString colourSpec, Chris@277: QString darkbg) Chris@277: { Chris@277: int index = -1; Chris@277: if (colourSpec != "") { Chris@277: QColor colour(colourSpec); Chris@277: index = getColourIndex(colour); Chris@277: if (index < 0) { Chris@277: index = addColour(colour, Chris@277: colourName == "" ? colourSpec : colourName); Chris@277: } Chris@277: } else if (colourName != "") { Chris@277: index = getColourIndex(colourName); Chris@277: } Chris@277: if (index >= 0) { Chris@277: setUseDarkBackground(index, darkbg == "true"); Chris@277: } Chris@277: return index; Chris@277: } Chris@277: Chris@277: void Chris@277: ColourDatabase::getColourPropertyRange(int *min, int *max) const Chris@277: { Chris@277: ColourDatabase *db = getInstance(); Chris@277: if (min) *min = 0; Chris@277: if (max) { Chris@277: *max = 0; Chris@277: if (db->getColourCount() > 0) *max = db->getColourCount()-1; Chris@277: } Chris@277: } Chris@277: Chris@278: QPixmap Chris@278: ColourDatabase::getExamplePixmap(int index, QSize size) const Chris@278: { Chris@278: QPixmap pmap(size); Chris@278: pmap.fill(useDarkBackground(index) ? Qt::black : Qt::white); Chris@278: QPainter paint(&pmap); Chris@278: QColor colour(getColour(index)); Chris@278: paint.setPen(colour); Chris@278: paint.setBrush(colour); Chris@278: int margin = 2; Chris@278: if (size.width() < 4 || size.height() < 4) margin = 0; Chris@278: else if (size.width() < 8 || size.height() < 8) margin = 1; Chris@278: paint.drawRect(margin, margin, Chris@278: size.width() - margin*2 - 1, size.height() - margin*2 - 1); Chris@278: return pmap; Chris@278: } Chris@278: