annotate widgets/ColourComboBox.cpp @ 1303:13f5f84fbfad

Collect the bits of bookkeeping for mouse wheel events, and use in all widgets
author Chris Cannam
date Fri, 22 Jun 2018 17:19:48 +0100
parents a34a2a25907c
children 85f04c956f03
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 if (count() < 20 && count() > maxVisibleItems()) {
Chris@1266 43 setMaxVisibleItems(count());
Chris@1196 44 }
Chris@1196 45 }
Chris@1196 46
Chris@1196 47 void
Chris@1196 48 ColourComboBox::comboActivated(int index)
Chris@1196 49 {
Chris@1196 50 if (!m_withAddNewColourEntry ||
Chris@1266 51 index < int(ColourDatabase::getInstance()->getColourCount())) {
Chris@1266 52 emit colourChanged(index);
Chris@1266 53 return;
Chris@1196 54 }
Chris@1196 55
Chris@1196 56 QColor newColour = QColorDialog::getColor();
Chris@1196 57 if (!newColour.isValid()) return;
Chris@1196 58
Chris@1196 59 ColourNameDialog dialog(tr("Name New Colour"),
Chris@1196 60 tr("Enter a name for the new colour:"),
Chris@1196 61 newColour, newColour.name(), this);
Chris@1196 62 dialog.showDarkBackgroundCheckbox(tr("Prefer black background for this colour"));
Chris@1196 63 if (dialog.exec() == QDialog::Accepted) {
Chris@1196 64 //!!! command
Chris@1196 65 ColourDatabase *db = ColourDatabase::getInstance();
Chris@1196 66 int index = db->addColour(newColour, dialog.getColourName());
Chris@1196 67 db->setUseDarkBackground(index, dialog.isDarkBackgroundChecked());
Chris@1266 68 // addColour will have called back on rebuild(), and the new
Chris@1266 69 // colour will be at the index previously occupied by Add New
Chris@1266 70 // Colour, which is our current index
Chris@1266 71 emit colourChanged(currentIndex());
Chris@1196 72 }
Chris@1196 73 }
Chris@1196 74
Chris@1196 75 void
Chris@1196 76 ColourComboBox::rebuild()
Chris@1196 77 {
Chris@1196 78 blockSignals(true);
Chris@1196 79
Chris@1196 80 int ix = currentIndex();
Chris@1196 81
Chris@1196 82 clear();
Chris@1196 83
Chris@1196 84 int size = (QFontMetrics(QFont()).height() * 2) / 3;
Chris@1196 85 if (size < 12) size = 12;
Chris@1196 86
Chris@1196 87 ColourDatabase *db = ColourDatabase::getInstance();
Chris@1196 88 for (int i = 0; i < db->getColourCount(); ++i) {
Chris@1266 89 QString name = db->getColourName(i);
Chris@1266 90 addItem(db->getExamplePixmap(i, QSize(size, size)), name);
Chris@1196 91 }
Chris@1196 92
Chris@1196 93 if (m_withAddNewColourEntry) {
Chris@1266 94 addItem(tr("Add New Colour..."));
Chris@1196 95 }
Chris@1196 96
Chris@1196 97 setCurrentIndex(ix);
Chris@1196 98
Chris@1196 99 blockSignals(false);
Chris@1196 100 }
Chris@1196 101