Chris@376: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@376: Chris@376: /* Chris@376: Sonic Visualiser Chris@376: An audio file viewer and annotation editor. Chris@376: Centre for Digital Music, Queen Mary, University of London. Chris@376: This file copyright 2007 QMUL. Chris@376: Chris@376: This program is free software; you can redistribute it and/or Chris@376: modify it under the terms of the GNU General Public License as Chris@376: published by the Free Software Foundation; either version 2 of the Chris@376: License, or (at your option) any later version. See the file Chris@376: COPYING included with this distribution for more information. Chris@376: */ Chris@376: Chris@1407: #ifndef SV_COLOUR_DATABASE_H Chris@1407: #define SV_COLOUR_DATABASE_H Chris@376: Chris@376: #include Chris@376: #include Chris@376: #include Chris@376: #include Chris@376: #include Chris@376: #include Chris@376: Chris@376: class ColourDatabase : public QObject Chris@376: { Chris@376: Q_OBJECT Chris@376: Chris@376: public: Chris@376: static ColourDatabase *getInstance(); Chris@376: Chris@1445: /** Chris@1445: * Return the number of colours in the database. Chris@1445: */ Chris@376: int getColourCount() const; Chris@1445: Chris@1445: /** Chris@1445: * Return the name of the colour at index c. Chris@1445: */ Chris@376: QString getColourName(int c) const; Chris@1445: Chris@1445: /** Chris@1445: * Return the colour at index c. Chris@1445: */ Chris@376: QColor getColour(int c) const; Chris@1445: Chris@1445: /** Chris@1445: * Return the colour with the given name, if found in the Chris@1445: * database. If not found, return Qt::black. Chris@1445: */ Chris@376: QColor getColour(QString name) const; Chris@1445: Chris@1445: /** Chris@1445: * Return the index of the colour with the given name, if found in Chris@1445: * the database. If not found, return -1. Chris@1445: */ Chris@1445: int getColourIndex(QString name) const; Chris@1445: Chris@1445: /** Chris@1445: * Return the index of the given colour, if found in the Chris@1445: * database. If not found, return -1. Note that it is possible for Chris@1445: * a colour to appear more than once in the database: names have Chris@1445: * to be unique in the database, but colours don't. This always Chris@1445: * returns the first match. Chris@1445: */ Chris@1445: int getColourIndex(QColor c) const; Chris@1445: Chris@1445: /** Chris@1445: * Return true if the given colour exists in the database. Chris@1445: */ Chris@376: bool haveColour(QColor c) const; Chris@376: Chris@1578: enum WithBackgroundMode { Chris@1578: WithAnyBackground, Chris@1578: WithDarkBackground, Chris@1578: WithLightBackground Chris@1578: }; Chris@1578: Chris@1445: /** Chris@1445: * Return the index of the colour in the database that is closest Chris@1578: * to the given one, by some simple measure. This always returns Chris@1578: * some valid index, unless the database is empty, in which case Chris@1578: * it returns -1. Chris@1445: */ Chris@1578: int getNearbyColourIndex(QColor c, Chris@1578: WithBackgroundMode mode = WithAnyBackground) const; Chris@1445: Chris@1445: /** Chris@1445: * Add a colour to the database, with the associated name. Return Chris@1445: * the index of the colour in the database. Names are unique Chris@1445: * within the database: if another colour exists already with the Chris@1445: * given name, its colour value is replaced with the given Chris@1445: * one. Colours may appear more than once under different names. Chris@1445: */ Chris@1445: int addColour(QColor c, QString name); Chris@1445: Chris@1445: /** Chris@1445: * Remove the colour with the given name from the database. Chris@1445: */ Chris@1445: void removeColour(QString); Chris@1445: Chris@1445: /** Chris@1445: * Return true if the colour at index c is marked as using a dark Chris@1445: * background. Such colours are presumably "bright" ones, but all Chris@1445: * this reports is whether the colour has been marked with Chris@1445: * setUseDarkBackground, not any intrinsic property of the colour. Chris@1445: */ Chris@376: bool useDarkBackground(int c) const; Chris@1445: Chris@1445: /** Chris@1445: * Mark the colour at index c as using a dark Chris@1445: * background. Generally this should be called for "bright" Chris@1445: * colours. Chris@1445: */ Chris@376: void setUseDarkBackground(int c, bool dark); Chris@376: Chris@1445: /** Chris@1445: * Return a colour that contrasts with the one at index c, Chris@1578: * according to some simple algorithm. The returned colour is not Chris@1578: * necessarily in the database; pass it to getNearbyColourIndex if Chris@1578: * you need one that is. Chris@1445: */ Chris@376: QColor getContrastingColour(int c) const; Chris@376: Chris@376: // for use in XML export Chris@376: void getStringValues(int index, Chris@376: QString &colourName, Chris@376: QString &colourSpec, Chris@376: QString &darkbg) const; Chris@376: Chris@376: // for use in XML import Chris@376: int putStringValues(QString colourName, Chris@376: QString colourSpec, Chris@376: QString darkbg); Chris@376: Chris@376: // for use by PropertyContainer getPropertyRangeAndValue methods Chris@376: void getColourPropertyRange(int *min, int *max) const; Chris@376: Chris@1445: /** Chris@1445: * Generate a swatch pixmap illustrating the colour at index c. Chris@1445: */ Chris@1445: QPixmap getExamplePixmap(int c, QSize size) const; Chris@376: Chris@376: signals: Chris@376: void colourDatabaseChanged(); Chris@376: Chris@376: protected: Chris@376: ColourDatabase(); Chris@376: Chris@376: struct ColourRec { Chris@376: QColor colour; Chris@376: QString name; Chris@376: bool darkbg; Chris@376: }; Chris@376: Chris@376: typedef std::vector ColourList; Chris@376: ColourList m_colours; Chris@376: Chris@376: static ColourDatabase m_instance; Chris@376: }; Chris@376: Chris@376: #endif