ColourComboBox.cpp
Go to the documentation of this file.
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 
43 void
45 {
46  m_unsetEntry = entry;
47 
48  rebuild();
49 
50  blockSignals(true);
51  int ix = currentIndex();
52  setCurrentIndex(ix + 1);
53  blockSignals(false);
54 }
55 
56 void
58 {
59  int index = comboIndex;
60  if (m_unsetEntry != "") {
61  index = comboIndex - 1; // so index is the colour index
62  }
63 
65  index < int(ColourDatabase::getInstance()->getColourCount())) {
66  emit colourChanged(index);
67  return;
68  }
69 
70  QColor newColour = QColorDialog::getColor();
71  if (!newColour.isValid()) return;
72 
73  ColourNameDialog dialog(tr("Name New Colour"),
74  tr("Enter a name for the new colour:"),
75  newColour, newColour.name(), this);
76  dialog.showDarkBackgroundCheckbox(tr("Prefer black background for this colour"));
77  if (dialog.exec() == QDialog::Accepted) {
80  int index = db->addColour(newColour, dialog.getColourName());
81  db->setUseDarkBackground(index, dialog.isDarkBackgroundChecked());
82  // addColour will have called back on rebuild(), and the new
83  // colour will be at the index previously occupied by Add New
84  // Colour, which is our current index
85  emit colourChanged(currentIndex());
86  }
87 }
88 
89 void
91 {
92  blockSignals(true);
93 
94  int ix = currentIndex();
95 
96  clear();
97 
98  if (m_unsetEntry != "") {
99  addItem(m_unsetEntry);
100  }
101 
102  int size = (QFontMetrics(QFont()).height() * 2) / 3;
103  if (size < 12) size = 12;
104 
106  for (int i = 0; i < db->getColourCount(); ++i) {
107  QString name = db->getColourName(i);
108  addItem(db->getExamplePixmap(i, QSize(size, size)), name);
109  }
110 
112  addItem(tr("Add New Colour..."));
113  }
114 
115  setCurrentIndex(ix);
116 
117  if (count() < 18) {
118  setMaxVisibleItems(count());
119  } else {
120  setMaxVisibleItems(10);
121  }
122 
123  blockSignals(false);
124 }
125 
Very trivial enhancement to QComboBox to make it emit signals when the mouse enters and leaves (for c...
QString getColourName(int c) const
Return the name of the colour at index c.
void includeUnsetEntry(QString label)
Add an entry at the top of the combo for "no colour selected", with the given label.
bool m_withAddNewColourEntry
void comboActivated(int)
void colourChanged(int colourIndex)
Emitted when the current index is changed.
void setUseDarkBackground(int c, bool dark)
Mark the colour at index c as using a dark background.
int getColourCount() const
Return the number of colours in the database.
ColourComboBox(bool withAddNewColourEntry, QWidget *parent=0)
int addColour(QColor c, QString name)
Add a colour to the database, with the associated name.
QString m_unsetEntry
void showDarkBackgroundCheckbox(QString text)
static ColourDatabase * getInstance()
QPixmap getExamplePixmap(int c, QSize size) const
Generate a swatch pixmap illustrating the colour at index c.