Chris@1196: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1196: Chris@1196: /* Chris@1196: Sonic Visualiser Chris@1196: An audio file viewer and annotation editor. Chris@1196: Centre for Digital Music, Queen Mary, University of London. Chris@1196: This file copyright 2007-2016 QMUL. Chris@1196: Chris@1196: This program is free software; you can redistribute it and/or Chris@1196: modify it under the terms of the GNU General Public License as Chris@1196: published by the Free Software Foundation; either version 2 of the Chris@1196: License, or (at your option) any later version. See the file Chris@1196: COPYING included with this distribution for more information. Chris@1196: */ Chris@1196: Chris@1196: #include "ColourComboBox.h" Chris@1196: Chris@1196: #include "ColourNameDialog.h" Chris@1196: Chris@1196: #include "layer/ColourDatabase.h" Chris@1196: Chris@1196: #include "base/Debug.h" Chris@1196: Chris@1196: #include Chris@1196: #include Chris@1196: Chris@1196: #include Chris@1196: Chris@1196: using namespace std; Chris@1196: Chris@1196: ColourComboBox::ColourComboBox(bool withAddNewColourEntry, QWidget *parent) : Chris@1196: NotifyingComboBox(parent), Chris@1196: m_withAddNewColourEntry(withAddNewColourEntry) Chris@1196: { Chris@1196: setEditable(false); Chris@1196: rebuild(); Chris@1196: Chris@1196: connect(this, SIGNAL(activated(int)), this, SLOT(comboActivated(int))); Chris@1196: connect(ColourDatabase::getInstance(), SIGNAL(colourDatabaseChanged()), Chris@1196: this, SLOT(rebuild())); Chris@1196: Chris@1196: if (count() < 20 && count() > maxVisibleItems()) { Chris@1266: setMaxVisibleItems(count()); Chris@1196: } Chris@1196: } Chris@1196: Chris@1196: void Chris@1196: ColourComboBox::comboActivated(int index) Chris@1196: { Chris@1196: if (!m_withAddNewColourEntry || Chris@1266: index < int(ColourDatabase::getInstance()->getColourCount())) { Chris@1266: emit colourChanged(index); Chris@1266: return; Chris@1196: } Chris@1196: Chris@1196: QColor newColour = QColorDialog::getColor(); Chris@1196: if (!newColour.isValid()) return; Chris@1196: Chris@1196: ColourNameDialog dialog(tr("Name New Colour"), Chris@1196: tr("Enter a name for the new colour:"), Chris@1196: newColour, newColour.name(), this); Chris@1196: dialog.showDarkBackgroundCheckbox(tr("Prefer black background for this colour")); Chris@1196: if (dialog.exec() == QDialog::Accepted) { Chris@1196: //!!! command Chris@1196: ColourDatabase *db = ColourDatabase::getInstance(); Chris@1196: int index = db->addColour(newColour, dialog.getColourName()); Chris@1196: db->setUseDarkBackground(index, dialog.isDarkBackgroundChecked()); Chris@1266: // addColour will have called back on rebuild(), and the new Chris@1266: // colour will be at the index previously occupied by Add New Chris@1266: // Colour, which is our current index Chris@1266: emit colourChanged(currentIndex()); Chris@1196: } Chris@1196: } Chris@1196: Chris@1196: void Chris@1196: ColourComboBox::rebuild() Chris@1196: { Chris@1196: blockSignals(true); Chris@1196: Chris@1196: int ix = currentIndex(); Chris@1196: Chris@1196: clear(); Chris@1196: Chris@1196: int size = (QFontMetrics(QFont()).height() * 2) / 3; Chris@1196: if (size < 12) size = 12; Chris@1196: Chris@1196: ColourDatabase *db = ColourDatabase::getInstance(); Chris@1196: for (int i = 0; i < db->getColourCount(); ++i) { Chris@1266: QString name = db->getColourName(i); Chris@1266: addItem(db->getExamplePixmap(i, QSize(size, size)), name); Chris@1196: } Chris@1196: Chris@1196: if (m_withAddNewColourEntry) { Chris@1266: addItem(tr("Add New Colour...")); Chris@1196: } Chris@1196: Chris@1196: setCurrentIndex(ix); Chris@1196: Chris@1196: blockSignals(false); Chris@1196: } Chris@1196: