Tweaks and attempts to get shape from shading working
[tecorrec.git] / tcColourMapWidget.cpp
blob446f77abddb5d08cc956a84ee07a9f0226becce3
1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * Tecorrec is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * Tecorrec is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with Tecorrec. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 /**
21 * @file tcColourMapWidget.cpp
22 * @brief Colour mapping widget.
25 #include "tcColourMapWidget.h"
27 #include <QGridLayout>
28 #include <QButtonGroup>
29 #include <QSignalMapper>
30 #include <QLabel>
31 #include <QPushButton>
32 #include <QRadioButton>
35 * Constructors + destructor
38 /// Construct with colour information.
39 tcColourMapWidget::tcColourMapWidget(const QStringList& outputBands, QWidget* parent)
40 : QScrollArea(parent)
41 , m_layout(0)
42 , m_inputSignalMapper(new QSignalMapper(this))
43 , m_inputGroupSignalMapper(new QSignalMapper(this))
44 , m_groupSignalMapper(new QSignalMapper(this))
45 , m_outputBandButtonGroups()
46 , m_numInputBands(0)
47 , m_numInputBandsPerGroup()
49 QWidget* widget = new QWidget(this);
50 m_layout = new QGridLayout(widget);
51 setWidget(widget);
52 setWidgetResizable(true);
54 foreach (QString band, outputBands)
56 QButtonGroup* buttonGroup = new QButtonGroup(this);
57 connect(buttonGroup, SIGNAL(buttonClicked(int)), m_groupSignalMapper, SLOT(map()));
58 m_groupSignalMapper->setMapping(buttonGroup, m_outputBandButtonGroups.size());
59 m_outputBandButtonGroups << buttonGroup;
61 m_layout->addWidget(new QLabel(band, this), 0, m_outputBandButtonGroups.size(), Qt::AlignHCenter);
63 connect(m_groupSignalMapper, SIGNAL(mapped(int)), this, SLOT(inputBandChangedSlot(int)));
64 connect(m_inputSignalMapper, SIGNAL(mapped(int)), this, SLOT(inputBandClickedSlot(int)));
65 connect(m_inputGroupSignalMapper, SIGNAL(mapped(int)), this, SIGNAL(inputGroupClicked(int)));
68 /// Destructor.
69 tcColourMapWidget::~tcColourMapWidget()
74 * Main interface
77 /// Clear bands.
78 void tcColourMapWidget::clearInputBands()
82 /// Add an input band.
83 void tcColourMapWidget::addInputBand(const QString& name, const QString& description)
85 ++m_numInputBands;
86 ++m_numInputBandsPerGroup.last();
87 QPushButton* mainLabel = new QPushButton(name, this);
88 mainLabel->setToolTip(description);
89 m_layout->addWidget(mainLabel, m_numInputBands+m_numInputBandsPerGroup.size(), 0);
90 connect(mainLabel, SIGNAL(clicked()), m_inputSignalMapper, SLOT(map()));
91 m_inputSignalMapper->setMapping(mainLabel, m_numInputBands-1);
92 for (int i = 0; i < m_outputBandButtonGroups.size(); ++i)
94 QRadioButton* radio = new QRadioButton(this);
95 m_outputBandButtonGroups[i]->addButton(radio, m_numInputBands-1);
96 m_layout->addWidget(radio, m_numInputBands+m_numInputBandsPerGroup.size(), 1 + i, Qt::AlignHCenter);
100 /// Add an input band group separator.
101 void tcColourMapWidget::addInputGroupSeparator(const QString& name)
103 m_numInputBandsPerGroup.push_back(0);
104 QPushButton* mainLabel = new QPushButton(QObject::tr("*** %1 ***").arg(name), this);
105 connect(mainLabel, SIGNAL(clicked()), m_inputGroupSignalMapper, SLOT(map()));
106 m_inputGroupSignalMapper->setMapping(mainLabel, m_numInputBandsPerGroup.size()-1);
107 m_layout->addWidget(mainLabel, m_numInputBands+m_numInputBandsPerGroup.size(), 0);
111 * Accessors
114 /// Get the index of the input band assigned to an output band.
115 int tcColourMapWidget::inputBand(int outputBand) const
117 return m_outputBandButtonGroups[outputBand]->checkedId();
121 * Public slots
124 /// Set the input band assigned to an output.
125 void tcColourMapWidget::setInputBand(int output, int input)
127 m_outputBandButtonGroups[output]->button(input)->setChecked(true);
131 * Private slots
134 /// Indicates that an input band has been clicked.
135 void tcColourMapWidget::inputBandClickedSlot(int input)
137 int group = inputGroup(&input);
138 emit inputBandClicked(input, group);
141 /// Indicates that the input band assigned to an output has changed.
142 void tcColourMapWidget::inputBandChangedSlot(int output)
144 int band = m_outputBandButtonGroups[output]->checkedId();
145 int group = inputGroup(&band);
146 emit inputBandChanged(output, band, group);
150 * Private functions
153 /// Get the input group and adjust the band number to be relative to the input group.
154 int tcColourMapWidget::inputGroup(int* band)
156 int result = -1;
157 foreach (int bands, m_numInputBandsPerGroup)
159 ++result;
160 if (*band < bands)
162 return result;
164 *band -= bands;
166 return result;