comparison widgets/ColourComboBox.cpp @ 1196:b1e3ee5f1be6 levelpanwidget

Introduce colour combobox class, to replace ad-hoc code in property box
author Chris Cannam
date Fri, 16 Dec 2016 13:08:33 +0000
parents
children a34a2a25907c
comparison
equal deleted inserted replaced
1195:51bb2582c2cc 1196:b1e3ee5f1be6
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2007-2016 QMUL.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "ColourComboBox.h"
17
18 #include "ColourNameDialog.h"
19
20 #include "layer/ColourDatabase.h"
21
22 #include "base/Debug.h"
23
24 #include <QFontMetrics>
25 #include <QColorDialog>
26
27 #include <iostream>
28
29 using namespace std;
30
31 ColourComboBox::ColourComboBox(bool withAddNewColourEntry, QWidget *parent) :
32 NotifyingComboBox(parent),
33 m_withAddNewColourEntry(withAddNewColourEntry)
34 {
35 setEditable(false);
36 rebuild();
37
38 connect(this, SIGNAL(activated(int)), this, SLOT(comboActivated(int)));
39 connect(ColourDatabase::getInstance(), SIGNAL(colourDatabaseChanged()),
40 this, SLOT(rebuild()));
41
42 if (count() < 20 && count() > maxVisibleItems()) {
43 setMaxVisibleItems(count());
44 }
45 }
46
47 void
48 ColourComboBox::comboActivated(int index)
49 {
50 if (!m_withAddNewColourEntry ||
51 index < int(ColourDatabase::getInstance()->getColourCount())) {
52 emit colourChanged(index);
53 return;
54 }
55
56 QColor newColour = QColorDialog::getColor();
57 if (!newColour.isValid()) return;
58
59 ColourNameDialog dialog(tr("Name New Colour"),
60 tr("Enter a name for the new colour:"),
61 newColour, newColour.name(), this);
62 dialog.showDarkBackgroundCheckbox(tr("Prefer black background for this colour"));
63 if (dialog.exec() == QDialog::Accepted) {
64 //!!! command
65 ColourDatabase *db = ColourDatabase::getInstance();
66 int index = db->addColour(newColour, dialog.getColourName());
67 db->setUseDarkBackground(index, dialog.isDarkBackgroundChecked());
68 // addColour will have called back on rebuild(), and the new
69 // colour will be at the index previously occupied by Add New
70 // Colour, which is our current index
71 emit colourChanged(currentIndex());
72 }
73 }
74
75 void
76 ColourComboBox::rebuild()
77 {
78 blockSignals(true);
79
80 int ix = currentIndex();
81
82 clear();
83
84 int size = (QFontMetrics(QFont()).height() * 2) / 3;
85 if (size < 12) size = 12;
86
87 ColourDatabase *db = ColourDatabase::getInstance();
88 for (int i = 0; i < db->getColourCount(); ++i) {
89 QString name = db->getColourName(i);
90 addItem(db->getExamplePixmap(i, QSize(size, size)), name);
91 }
92
93 if (m_withAddNewColourEntry) {
94 addItem(tr("Add New Colour..."));
95 }
96
97 setCurrentIndex(ix);
98
99 blockSignals(false);
100 }
101