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 "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     colourSpec = XmlExportable::encodeColour(getColour(index));
00154     darkbg = useDarkBackground(index) ? "true" : "false";
00155 }
00156 
00157 int
00158 ColourDatabase::putStringValues(QString colourName,
00159                                 QString colourSpec,
00160                                 QString darkbg)
00161 {
00162     int index = -1;
00163     if (colourSpec != "") {
00164         QColor colour(colourSpec);
00165         index = getColourIndex(colour);
00166         if (index < 0) {
00167             index = addColour(colour,
00168                               colourName == "" ? colourSpec : colourName);
00169         }
00170     } else if (colourName != "") {
00171         index = getColourIndex(colourName);
00172     }
00173     if (index >= 0) {
00174         setUseDarkBackground(index, darkbg == "true");
00175     }
00176     return index;
00177 }
00178 
00179 void
00180 ColourDatabase::getColourPropertyRange(int *min, int *max) const
00181 {
00182     ColourDatabase *db = getInstance();
00183     if (min) *min = 0;
00184     if (max) {
00185         *max = 0;
00186         if (db->getColourCount() > 0) *max = db->getColourCount()-1;
00187     }
00188 }
00189 
00190 QPixmap
00191 ColourDatabase::getExamplePixmap(int index, QSize size) const
00192 {
00193     QPixmap pmap(size);
00194     pmap.fill(useDarkBackground(index) ? Qt::black : Qt::white);
00195     QPainter paint(&pmap);
00196     QColor colour(getColour(index));
00197     paint.setPen(colour);
00198     paint.setBrush(colour);
00199     int margin = 2;
00200     if (size.width() < 4 || size.height() < 4) margin = 0;
00201     else if (size.width() < 8 || size.height() < 8) margin = 1;
00202     paint.drawRect(margin, margin,
00203                    size.width() - margin*2 - 1, size.height() - margin*2 - 1);
00204     return pmap;
00205 }
00206 

Generated on Wed Feb 20 15:45:25 2008 for SonicVisualiser by  doxygen 1.5.1