annotate widgets/ColourComboBox.cpp @ 1614:c6f5c822b10d

Fix potential divide-by-zero (depending on a race elsewhere)
author Chris Cannam
date Tue, 30 Jun 2020 10:56:56 +0100
parents 85f04c956f03
children
rev   line source
Chris@1196 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1196 2
Chris@1196 3 /*
Chris@1196 4 Sonic Visualiser
Chris@1196 5 An audio file viewer and annotation editor.
Chris@1196 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1196 7 This file copyright 2007-2016 QMUL.
Chris@1196 8
Chris@1196 9 This program is free software; you can redistribute it and/or
Chris@1196 10 modify it under the terms of the GNU General Public License as
Chris@1196 11 published by the Free Software Foundation; either version 2 of the
Chris@1196 12 License, or (at your option) any later version. See the file
Chris@1196 13 COPYING included with this distribution for more information.
Chris@1196 14 */
Chris@1196 15
Chris@1196 16 #include "ColourComboBox.h"
Chris@1196 17
Chris@1196 18 #include "ColourNameDialog.h"
Chris@1196 19
Chris@1196 20 #include "layer/ColourDatabase.h"
Chris@1196 21
Chris@1196 22 #include "base/Debug.h"
Chris@1196 23
Chris@1196 24 #include <QFontMetrics>
Chris@1196 25 #include <QColorDialog>
Chris@1196 26
Chris@1196 27 #include <iostream>
Chris@1196 28
Chris@1196 29 using namespace std;
Chris@1196 30
Chris@1196 31 ColourComboBox::ColourComboBox(bool withAddNewColourEntry, QWidget *parent) :
Chris@1196 32 NotifyingComboBox(parent),
Chris@1196 33 m_withAddNewColourEntry(withAddNewColourEntry)
Chris@1196 34 {
Chris@1196 35 setEditable(false);
Chris@1196 36 rebuild();
Chris@1196 37
Chris@1196 38 connect(this, SIGNAL(activated(int)), this, SLOT(comboActivated(int)));
Chris@1196 39 connect(ColourDatabase::getInstance(), SIGNAL(colourDatabaseChanged()),
Chris@1196 40 this, SLOT(rebuild()));
Chris@1196 41 }
Chris@1196 42
Chris@1196 43 void
Chris@1579 44 ColourComboBox::includeUnsetEntry(QString entry)
Chris@1196 45 {
Chris@1579 46 m_unsetEntry = entry;
Chris@1579 47
Chris@1579 48 rebuild();
Chris@1579 49
Chris@1579 50 blockSignals(true);
Chris@1579 51 int ix = currentIndex();
Chris@1579 52 setCurrentIndex(ix + 1);
Chris@1579 53 blockSignals(false);
Chris@1579 54 }
Chris@1579 55
Chris@1579 56 void
Chris@1579 57 ColourComboBox::comboActivated(int comboIndex)
Chris@1579 58 {
Chris@1579 59 int index = comboIndex;
Chris@1579 60 if (m_unsetEntry != "") {
Chris@1579 61 index = comboIndex - 1; // so index is the colour index
Chris@1579 62 }
Chris@1579 63
Chris@1196 64 if (!m_withAddNewColourEntry ||
Chris@1266 65 index < int(ColourDatabase::getInstance()->getColourCount())) {
Chris@1266 66 emit colourChanged(index);
Chris@1266 67 return;
Chris@1196 68 }
Chris@1196 69
Chris@1196 70 QColor newColour = QColorDialog::getColor();
Chris@1196 71 if (!newColour.isValid()) return;
Chris@1196 72
Chris@1196 73 ColourNameDialog dialog(tr("Name New Colour"),
Chris@1196 74 tr("Enter a name for the new colour:"),
Chris@1196 75 newColour, newColour.name(), this);
Chris@1196 76 dialog.showDarkBackgroundCheckbox(tr("Prefer black background for this colour"));
Chris@1196 77 if (dialog.exec() == QDialog::Accepted) {
Chris@1196 78 //!!! command
Chris@1196 79 ColourDatabase *db = ColourDatabase::getInstance();
Chris@1196 80 int index = db->addColour(newColour, dialog.getColourName());
Chris@1196 81 db->setUseDarkBackground(index, dialog.isDarkBackgroundChecked());
Chris@1266 82 // addColour will have called back on rebuild(), and the new
Chris@1266 83 // colour will be at the index previously occupied by Add New
Chris@1266 84 // Colour, which is our current index
Chris@1266 85 emit colourChanged(currentIndex());
Chris@1196 86 }
Chris@1196 87 }
Chris@1196 88
Chris@1196 89 void
Chris@1196 90 ColourComboBox::rebuild()
Chris@1196 91 {
Chris@1196 92 blockSignals(true);
Chris@1196 93
Chris@1196 94 int ix = currentIndex();
Chris@1196 95
Chris@1196 96 clear();
Chris@1196 97
Chris@1579 98 if (m_unsetEntry != "") {
Chris@1579 99 addItem(m_unsetEntry);
Chris@1579 100 }
Chris@1579 101
Chris@1196 102 int size = (QFontMetrics(QFont()).height() * 2) / 3;
Chris@1196 103 if (size < 12) size = 12;
Chris@1196 104
Chris@1196 105 ColourDatabase *db = ColourDatabase::getInstance();
Chris@1196 106 for (int i = 0; i < db->getColourCount(); ++i) {
Chris@1266 107 QString name = db->getColourName(i);
Chris@1266 108 addItem(db->getExamplePixmap(i, QSize(size, size)), name);
Chris@1196 109 }
Chris@1196 110
Chris@1196 111 if (m_withAddNewColourEntry) {
Chris@1266 112 addItem(tr("Add New Colour..."));
Chris@1196 113 }
Chris@1196 114
Chris@1196 115 setCurrentIndex(ix);
Chris@1579 116
Chris@1579 117 if (count() < 18) {
Chris@1579 118 setMaxVisibleItems(count());
Chris@1579 119 } else {
Chris@1579 120 setMaxVisibleItems(10);
Chris@1579 121 }
Chris@1196 122
Chris@1196 123 blockSignals(false);
Chris@1196 124 }
Chris@1196 125