svgui  1.9
ColourDatabase.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2007 QMUL.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "ColourDatabase.h"
00017 #include "base/XmlExportable.h"
00018 
00019 #include <QPainter>
00020 
00021 ColourDatabase
00022 ColourDatabase::m_instance;
00023 
00024 ColourDatabase *
00025 ColourDatabase::getInstance()
00026 {
00027     return &m_instance;
00028 }
00029 
00030 ColourDatabase::ColourDatabase()
00031 {
00032 }
00033 
00034 int
00035 ColourDatabase::getColourCount() const
00036 {
00037     return m_colours.size();
00038 }
00039 
00040 QString
00041 ColourDatabase::getColourName(int c) const
00042 {
00043     if (c < 0 || size_t(c) >= m_colours.size()) return "";
00044     return m_colours[c].name;
00045 }
00046 
00047 QColor
00048 ColourDatabase::getColour(int c) const
00049 {
00050     if (c < 0 || size_t(c) >= m_colours.size()) return Qt::black;
00051     return m_colours[c].colour;
00052 }
00053 
00054 QColor
00055 ColourDatabase::getColour(QString name) const
00056 {
00057     for (ColourList::const_iterator i = m_colours.begin();
00058          i != m_colours.end(); ++i) {
00059         if (i->name == name) return i->colour;
00060     }
00061 
00062     return Qt::black;
00063 }
00064 
00065 int
00066 ColourDatabase::getColourIndex(QString name) const
00067 {
00068     int index = 0;
00069     for (ColourList::const_iterator i = m_colours.begin();
00070          i != m_colours.end(); ++i) {
00071         if (i->name == name) return index;
00072         ++index;
00073     }
00074 
00075     return -1;
00076 }
00077 
00078 int
00079 ColourDatabase::getColourIndex(QColor c) const
00080 {
00081     int index = 0;
00082     for (ColourList::const_iterator i = m_colours.begin();
00083          i != m_colours.end(); ++i) {
00084         if (i->colour == c) return index;
00085         ++index;
00086     }
00087 
00088     return -1;
00089 }
00090 
00091 bool
00092 ColourDatabase::useDarkBackground(int c) const
00093 {
00094     if (c < 0 || size_t(c) >= m_colours.size()) return false;
00095     return m_colours[c].darkbg;
00096 }
00097 
00098 void
00099 ColourDatabase::setUseDarkBackground(int c, bool dark)
00100 {
00101     if (c < 0 || size_t(c) >= m_colours.size()) return;
00102     if (m_colours[c].darkbg != dark) {
00103         m_colours[c].darkbg = dark;
00104         emit colourDatabaseChanged();
00105     }
00106 }
00107 
00108 int
00109 ColourDatabase::addColour(QColor c, QString name)
00110 {
00111     int index = 0;
00112     for (ColourList::iterator i = m_colours.begin();
00113          i != m_colours.end(); ++i) {
00114         if (i->name == name) {
00115             i->colour = c;
00116             return index;
00117         }
00118         ++index;
00119     }
00120 
00121     ColourRec rec;
00122     rec.colour = c;
00123     rec.name = name;
00124     rec.darkbg = false;
00125     m_colours.push_back(rec);
00126     emit colourDatabaseChanged();
00127     return index;
00128 }
00129 
00130 void
00131 ColourDatabase::removeColour(QString name)
00132 {
00133     for (ColourList::iterator i = m_colours.begin();
00134          i != m_colours.end(); ++i) {
00135         if (i->name == name) {
00136             m_colours.erase(i);
00137             return;
00138         }
00139     }
00140 }
00141 
00142 void
00143 ColourDatabase::getStringValues(int index,
00144                                 QString &colourName,
00145                                 QString &colourSpec,
00146                                 QString &darkbg) const
00147 {
00148     colourName = "";
00149     colourSpec = "";
00150     if (index < 0 || size_t(index) >= m_colours.size()) return;
00151 
00152     colourName = getColourName(index);
00153     QColor c = getColour(index);
00154     colourSpec = XmlExportable::encodeColour(c.red(), c.green(), c.blue());
00155     darkbg = useDarkBackground(index) ? "true" : "false";
00156 }
00157 
00158 int
00159 ColourDatabase::putStringValues(QString colourName,
00160                                 QString colourSpec,
00161                                 QString darkbg)
00162 {
00163     int index = -1;
00164     if (colourSpec != "") {
00165         QColor colour(colourSpec);
00166         index = getColourIndex(colour);
00167         if (index < 0) {
00168             index = addColour(colour,
00169                               colourName == "" ? colourSpec : colourName);
00170         }
00171     } else if (colourName != "") {
00172         index = getColourIndex(colourName);
00173     }
00174     if (index >= 0) {
00175         setUseDarkBackground(index, darkbg == "true");
00176     }
00177     return index;
00178 }
00179 
00180 void
00181 ColourDatabase::getColourPropertyRange(int *min, int *max) const
00182 {
00183     ColourDatabase *db = getInstance();
00184     if (min) *min = 0;
00185     if (max) {
00186         *max = 0;
00187         if (db->getColourCount() > 0) *max = db->getColourCount()-1;
00188     }
00189 }
00190 
00191 QPixmap
00192 ColourDatabase::getExamplePixmap(int index, QSize size) const
00193 {
00194     QPixmap pmap(size);
00195     pmap.fill(useDarkBackground(index) ? Qt::black : Qt::white);
00196     QPainter paint(&pmap);
00197     QColor colour(getColour(index));
00198     paint.setPen(colour);
00199     paint.setBrush(colour);
00200     int margin = 2;
00201     if (size.width() < 4 || size.height() < 4) margin = 0;
00202     else if (size.width() < 8 || size.height() < 8) margin = 1;
00203     paint.drawRect(margin, margin,
00204                    size.width() - margin*2 - 1, size.height() - margin*2 - 1);
00205     return pmap;
00206 }
00207