libpappsomspp
Library for mass spectrometry
basecolormapplotwidget.cpp
Go to the documentation of this file.
1 /* This code comes right from the msXpertSuite software project.
2  *
3  * msXpertSuite - mass spectrometry software suite
4  * -----------------------------------------------
5  * Copyright(C) 2009,...,2018 Filippo Rusconi
6  *
7  * http://www.msxpertsuite.org
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  *
22  * END software license
23  */
24 
25 
26 /////////////////////// StdLib includes
27 #include <vector>
28 
29 
30 /////////////////////// Qt includes
31 #include <QVector>
32 
33 
34 /////////////////////// Local includes
35 #include "basecolormapplotwidget.h"
36 #include "../../trace/maptrace.h"
37 #include "../../pappsoexception.h"
38 
39 
40 namespace pappso
41 {
42 
43 
45  const QString &x_axis_label,
46  const QString &y_axis_label)
47  : BasePlotWidget(parent, x_axis_label, y_axis_label)
48 {
49 }
50 
51 
52 //! Destruct \c this BaseColorMapPlotWidget instance.
53 /*!
54 
55  The destruction involves clearing the history, deleting all the axis range
56  history items for x and y axes.
57 
58 */
60 {
61 }
62 
63 
64 void
66  const ColorMapPlotConfig &color_map_config)
67 {
68  m_colorMapPlotConfig = color_map_config;
69 }
70 
71 
72 const ColorMapPlotConfig &
74 {
75  return m_colorMapPlotConfig;
76 }
77 
78 
79 QCPColorMap *
81  std::shared_ptr<std::map<double, MapTrace>> double_map_trace_map_sp,
82  ColorMapPlotConfig color_map_plot_config,
83  const QColor &color)
84 {
85  // qDebug() << "Adding color map with config:" <<
86  // color_map_plot_config.toString();
87 
88  if(!color.isValid())
89  throw PappsoException(
90  QString("The color to be used for the plot graph is invalid."));
91 
92  QCPColorMap *color_map_p = new QCPColorMap(xAxis, yAxis);
93 
94  // Do not forget to copy the config!
95 
96  m_colorMapPlotConfig = color_map_plot_config;
97 
98 #if 0
99  // This is the code on the QCustomPlot documentation and it works fine.
100  QCPColorMap *color_map_p = new QCPColorMap(xAxis, yAxis);
101 
102  color_map_p->data()->setSize(50, 50);
103  color_map_p->data()->setRange(QCPRange(0, 2), QCPRange(0, 2));
104  for(int x = 0; x < 50; ++x)
105  for(int y = 0; y < 50; ++y)
106  color_map_p->data()->setCell(x, y, qCos(x / 10.0) + qSin(y / 10.0));
107  color_map_p->setGradient(QCPColorGradient::gpPolar);
108  color_map_p->rescaleDataRange(true);
109  rescaleAxes();
110  replot();
111 #endif
112 
113  // Connect the signal of selection change so that we can re-emit it for the
114  // widget that is using *this widget.
115 
116  connect(color_map_p,
117  static_cast<void (QCPAbstractPlottable::*)(bool)>(
118  &QCPAbstractPlottable::selectionChanged),
119  [this, color_map_p]() {
120  emit plottableSelectionChangedSignal(color_map_p,
121  color_map_p->selected());
122  });
123 
124  // qDebug() << "Configuring the color map with this config:"
125  //<< color_map_plot_config.toString();
126 
127  color_map_p->data()->setSize(color_map_plot_config.keyCellCount,
128  color_map_plot_config.mzCellCount);
129 
130  color_map_p->data()->setRange(QCPRange(color_map_plot_config.minKeyValue,
131  color_map_plot_config.maxKeyValue),
132  QCPRange(color_map_plot_config.minMzValue,
133  color_map_plot_config.maxMzValue));
134  color_map_p->data()->fill(0.0);
135 
136  // We have now to fill the color map.
137 
138  for(auto &&pair : *double_map_trace_map_sp)
139  {
140 
141  // The first value is the key and the second value is the MapTrace into
142  // which we need to iterated and for each point (double mz, double
143  // intensity) create a map cell.
144 
145  double dt_or_rt_key = pair.first;
146  MapTrace map_trace = pair.second;
147 
148  for(auto &&data_point_pair : map_trace)
149  {
150  double mz = data_point_pair.first;
151  double intensity = data_point_pair.second;
152 
153  // We are filling dynamically the color map. If a cell had already
154  // something in, then we need to take that into account. This is
155  // because we let QCustomPlot handle the fuzzy transition between
156  // color map plot cells.
157 
158  double prev_intensity = color_map_p->data()->data(dt_or_rt_key, mz);
159 
160  // qDebug() << "Setting tri-point:" << dt_or_rt_key << "," << mz <<
161  // ","
162  //<< prev_intensity + intensity;
163 
164  color_map_p->data()->setData(
165  dt_or_rt_key, mz, prev_intensity + intensity);
166  }
167  }
168 
169  // At this point we have finished filling-up the color map.
170 
171  // The gpThermal is certainly one of the best.
172 
173  color_map_p->setGradient(QCPColorGradient::gpThermal);
174 
175  color_map_p->rescaleDataRange(true);
176 
177  color_map_p->rescaleAxes();
179 
180  // The pen of the color map itself is of no use. Instead the user will see the
181  // color of the axes' labels.
182 
183  QPen pen = xAxis->basePen();
184  pen.setColor(color);
185 
186  xAxis->setBasePen(pen);
187  xAxis->setLabelColor(color);
188  xAxis->setTickLabelColor(color);
189 
190  yAxis->setBasePen(pen);
191  yAxis->setLabelColor(color);
192  yAxis->setTickLabelColor(color);
193 
194  // And now set the color map's pen to the same color, even if we do not use
195  // it, we need it for coloring the plots that might be integrated from this
196  // color map.
197 
198  color_map_p->setPen(pen);
199 
200  replot();
201 
202  return color_map_p;
203 }
204 
205 
206 void
208 {
209  // qDebug() << __FILE__ << __LINE__ << __FUNCTION__ << "()" ;
210 
211  QCPColorMap *color_map_p = static_cast<QCPColorMap *>(plottable(0));
212 
213  QCPColorMapData *origData = color_map_p->data();
214 
215  int keySize = origData->keySize();
216  int valueSize = origData->valueSize();
217 
218  // qDebug() << __FILE__ << __LINE__ << __FUNCTION__ << "()"
219  //<< "Orig data size:" << keySize << valueSize;
220 
221  QCPRange keyRange = origData->keyRange();
222  QCPRange valueRange = origData->valueRange();
223 
224  // qDebug() << __FILE__ << __LINE__ << __FUNCTION__ << "()"
225  //<< "Value at cell 80,650:" << origData->cell(80,650);
226 
227  // Transposed map.
228  QCPColorMapData *newData =
229  new QCPColorMapData(valueSize, keySize, valueRange, keyRange);
230 
231  for(int iter = 0; iter < keySize; ++iter)
232  {
233  for(int jter = 0; jter < valueSize; ++jter)
234  {
235  double cellData = origData->cell(iter, jter);
236 
237  newData->setCell(jter, iter, cellData);
238  }
239  }
240 
241  // qDebug() << __FILE__ << __LINE__ << __FUNCTION__ << "()"
242  //<< "New data size:" << newData->keySize() << newData->valueSize();
243 
244  // At this point the transposition has been done.
245 
246  color_map_p->data()->clear();
247  color_map_p->rescaleDataRange(true);
248 
249  // Now we need to invert the labels and data kinds.
250 
253  m_colorMapPlotConfig.yAxisDataKind = temp_data_kind;
254 
255  QString temp_axis_label = xAxis->label();
256  xAxis->setLabel(yAxis->label());
257  yAxis->setLabel(temp_axis_label);
258 
259  // Will take ownership of the newData.
260  color_map_p->setData(newData);
261 
262  // qDebug() << __FILE__ << __LINE__ << __FUNCTION__ << "()"
263  //<< "Value at cell 80,650:" << newData->cell(80,650)
264  //<< "Value at cell 650, 80:" << newData->cell(650,80);
265 
266  // QCPAxis *p_keyAxis = mp_colorMap->keyAxis();
267  // QCPAxis *p_valueAxis = mp_colorMap->valueAxis();
268 
269  // mp_colorMap->setKeyAxis(p_valueAxis);
270  // mp_colorMap->setValueAxis(p_keyAxis);
271 
272  color_map_p->rescaleAxes();
273 
274  replot();
275 }
276 
277 
278 DataKind
280 {
282 }
283 
284 
285 DataKind
287 {
289 }
290 
291 
292 void
293 BaseColorMapPlotWidget::setPlottingColor(QCPAbstractPlottable *plottable_p,
294  const QColor &new_color)
295 {
296  Q_UNUSED(plottable_p);
297 
298  // The pen of the color map itself is of no use. Instead the user will see the
299  // color of the axes' labels.
300 
301  QPen pen = xAxis->basePen();
302  pen.setColor(new_color);
303 
304  xAxis->setBasePen(pen);
305  xAxis->setLabelColor(new_color);
306  xAxis->setTickLabelColor(new_color);
307 
308  yAxis->setBasePen(pen);
309  yAxis->setLabelColor(new_color);
310  yAxis->setTickLabelColor(new_color);
311 
312  // And now set the color map's pen to the same color, even if we do not use
313  // it, we need it for coloring the plots that might be integrated from this
314  // color map.
315 
316  QCPColorMap *color_map_p = static_cast<QCPColorMap *>(plottable(0));
317 
318  color_map_p->setPen(pen);
319 
320  replot();
321 }
322 
323 
324 QColor
326 {
327  Q_UNUSED(index);
328 
329  QPen pen = xAxis->basePen();
330  return pen.color();
331 }
332 
333 
334 } // namespace pappso
pappso::ColorMapPlotConfig
Definition: colormapplotconfig.h:22
pappso::ColorMapPlotConfig::keyCellCount
std::size_t keyCellCount
Definition: colormapplotconfig.h:26
pappso::BaseColorMapPlotWidget::transposeAxes
virtual void transposeAxes()
Definition: basecolormapplotwidget.cpp:207
pappso::ColorMapPlotConfig::yAxisDataKind
DataKind yAxisDataKind
Definition: colormapplotconfig.h:24
pappso::BaseColorMapPlotWidget::~BaseColorMapPlotWidget
virtual ~BaseColorMapPlotWidget()
Destruct this BaseColorMapPlotWidget instance.
Definition: basecolormapplotwidget.cpp:59
pappso::BaseColorMapPlotWidget::setPlottingColor
virtual void setPlottingColor(QCPAbstractPlottable *plottable_p, const QColor &new_color) override
Definition: basecolormapplotwidget.cpp:293
pappso::ColorMapPlotConfig::maxKeyValue
double maxKeyValue
Definition: colormapplotconfig.h:30
pappso::BasePlotWidget
Definition: baseplotwidget.h:136
pappso::BaseColorMapPlotWidget::xAxisDataKind
DataKind xAxisDataKind() const
Definition: basecolormapplotwidget.cpp:279
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks
Definition: aa.cpp:39
pappso::DataKind
DataKind
Definition: types.h:192
pappso::BaseColorMapPlotWidget::setColorMapPlotConfig
virtual void setColorMapPlotConfig(const ColorMapPlotConfig &color_map_config)
Definition: basecolormapplotwidget.cpp:65
pappso::BaseColorMapPlotWidget::getPlottingColor
virtual QColor getPlottingColor(int index=0) const override
Definition: basecolormapplotwidget.cpp:325
pappso::MapTrace
Definition: maptrace.h:33
pappso::PeptideIonCter::y
@ y
basecolormapplotwidget.h
pappso::ColorMapPlotConfig::minKeyValue
double minKeyValue
Definition: colormapplotconfig.h:29
pappso::ColorMapPlotConfig::xAxisDataKind
DataKind xAxisDataKind
Definition: colormapplotconfig.h:23
pappso::PeptideIonCter::x
@ x
pappso::BasePlotWidget::plottableSelectionChangedSignal
void plottableSelectionChangedSignal(QCPAbstractPlottable *plottable_p, bool selected)
pappso::BaseColorMapPlotWidget::yAxisDataKind
DataKind yAxisDataKind() const
Definition: basecolormapplotwidget.cpp:286
pappso::BasePlotWidget::resetAxesRangeHistory
virtual void resetAxesRangeHistory()
Definition: baseplotwidget.cpp:366
pappso::ColorMapPlotConfig::maxMzValue
double maxMzValue
Definition: colormapplotconfig.h:33
pappso::BaseColorMapPlotWidget::addColorMap
virtual QCPColorMap * addColorMap(std::shared_ptr< std::map< double, MapTrace >> double_map_trace_map_sp, ColorMapPlotConfig color_map_plot_config, const QColor &color)
Definition: basecolormapplotwidget.cpp:80
pappso::ColorMapPlotConfig::minMzValue
double minMzValue
Definition: colormapplotconfig.h:32
pappso::PrecisionUnit::mz
@ mz
pappso::ColorMapPlotConfig::mzCellCount
std::size_t mzCellCount
Definition: colormapplotconfig.h:27
pappso::BaseColorMapPlotWidget::getColorMapPlotConfig
virtual const ColorMapPlotConfig & getColorMapPlotConfig()
Definition: basecolormapplotwidget.cpp:73
pappso::BaseColorMapPlotWidget::m_colorMapPlotConfig
ColorMapPlotConfig m_colorMapPlotConfig
Definition: basecolormapplotwidget.h:94
pappso::BaseColorMapPlotWidget::BaseColorMapPlotWidget
BaseColorMapPlotWidget(QWidget *parent, const QString &x_axis_label, const QString &y_axis_label)
Definition: basecolormapplotwidget.cpp:44
pappso::PappsoException
Definition: pappsoexception.h:63