~ubuntu-branches/ubuntu/wily/openms/wily

« back to all changes in this revision

Viewing changes to source/VISUAL/Spectrum2DCanvas.C

  • Committer: Package Import Robot
  • Author(s): Filippo Rusconi
  • Date: 2013-12-20 11:30:16 UTC
  • mfrom: (5.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20131220113016-wre5g9bteeheq6he
Tags: 1.11.1-3
* remove version number from libbost development package names;
* ensure that AUTHORS is correctly shipped in all packages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// -*- mode: C++; tab-width: 2; -*-
2
 
// vi: set ts=2:
3
 
//
4
 
// --------------------------------------------------------------------------
5
 
//                   OpenMS Mass Spectrometry Framework
6
 
// --------------------------------------------------------------------------
7
 
//  Copyright (C) 2003-2011 -- Oliver Kohlbacher, Knut Reinert
8
 
//
9
 
//  This library is free software; you can redistribute it and/or
10
 
//  modify it under the terms of the GNU Lesser General Public
11
 
//  License as published by the Free Software Foundation; either
12
 
//  version 2.1 of the License, or (at your option) any later version.
13
 
//
14
 
//  This library 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 GNU
17
 
//  Lesser General Public License for more details.
18
 
//
19
 
//  You should have received a copy of the GNU Lesser General Public
20
 
//  License along with this library; if not, write to the Free Software
21
 
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
1
// --------------------------------------------------------------------------
 
2
//                   OpenMS -- Open-Source Mass Spectrometry
 
3
// --------------------------------------------------------------------------
 
4
// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
 
5
// ETH Zurich, and Freie Universitaet Berlin 2002-2013.
 
6
//
 
7
// This software is released under a three-clause BSD license:
 
8
//  * Redistributions of source code must retain the above copyright
 
9
//    notice, this list of conditions and the following disclaimer.
 
10
//  * Redistributions in binary form must reproduce the above copyright
 
11
//    notice, this list of conditions and the following disclaimer in the
 
12
//    documentation and/or other materials provided with the distribution.
 
13
//  * Neither the name of any author or any participating institution
 
14
//    may be used to endorse or promote products derived from this software
 
15
//    without specific prior written permission.
 
16
// For a full list of authors, refer to the file AUTHORS.
 
17
// --------------------------------------------------------------------------
 
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
19
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
20
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
21
// ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
 
22
// INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
23
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
24
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
25
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
26
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 
27
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
28
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
29
//
23
30
// --------------------------------------------------------------------------
24
31
// $Maintainer: Timo Sachsenberg$
61
68
 
62
69
namespace OpenMS
63
70
{
64
 
        using namespace Internal;
 
71
  using namespace Internal;
65
72
 
66
 
        Spectrum2DCanvas::Spectrum2DCanvas(const Param& preferences, QWidget* parent)
67
 
                : SpectrumCanvas(preferences, parent)
68
 
        {
 
73
  Spectrum2DCanvas::Spectrum2DCanvas(const Param & preferences, QWidget * parent) :
 
74
    SpectrumCanvas(preferences, parent)
 
75
  {
69
76
    //Parameter handling
70
77
    defaults_.setValue("background_color", "#ffffff", "Background color.");
71
78
    defaults_.setValue("interpolation_steps", 1000, "Number of interploation steps for peak gradient precalculation.");
72
 
    defaults_.setMinInt("interpolation_steps",1);
73
 
    defaults_.setMaxInt("interpolation_steps",1000);
 
79
    defaults_.setMinInt("interpolation_steps", 1);
 
80
    defaults_.setMaxInt("interpolation_steps", 1000);
74
81
    defaults_.setValue("dot:gradient", "Linear|0,#eeeeee;1,#ffea00;6,#ff0000;14,#aa00ff;23,#5500ff;100,#000000", "Multi-color gradient for peaks.");
75
82
    defaults_.setValue("dot:feature_icon", "circle", "Icon used for features and consensus features.");
76
83
    defaults_.setValidStrings("dot:feature_icon", StringList::create("diamond,square,circle,triangle"));
77
84
    defaults_.setValue("dot:feature_icon_size", 4, "Icon size used for features and consensus features.");
78
 
    defaults_.setMinInt("dot:feature_icon_size",1);
79
 
    defaults_.setMaxInt("dot:feature_icon_size",999);
80
 
    defaults_.setValue("mapping_of_mz_to","y_axis","Determines which axis is the m/z axis.");
81
 
                defaults_.setValidStrings("mapping_of_mz_to", StringList::create("x_axis,y_axis"));
82
 
                defaultsToParam_();
83
 
                setName("Spectrum2DCanvas");
84
 
                setParameters(preferences);
85
 
 
86
 
                projection_mz_.resize(1);
87
 
                projection_rt_.resize(1);
88
 
 
89
 
                //set preferences and update widgets accordingly
90
 
                if (String(param_.getValue("mapping_of_mz_to")) != "x_axis")
91
 
                {
92
 
                        mzToXAxis(false);
93
 
                }
94
 
                //connect preferences change to the right slot
95
 
    connect(this,SIGNAL(preferencesChange()),this,SLOT(currentLayerParametersChanged_()));
96
 
        }
97
 
 
98
 
        Spectrum2DCanvas::~Spectrum2DCanvas()
99
 
        {
100
 
        }
101
 
 
102
 
        void Spectrum2DCanvas::highlightPeak_(QPainter& painter, const PeakIndex& peak)
103
 
        {
104
 
                if (!peak.isValid()) return;
105
 
 
106
 
                //determine coordinates;
107
 
                QPoint pos;
108
 
                if (getCurrentLayer().type==LayerData::DT_FEATURE)
109
 
                {
 
85
    defaults_.setMinInt("dot:feature_icon_size", 1);
 
86
    defaults_.setMaxInt("dot:feature_icon_size", 999);
 
87
    defaults_.setValue("mapping_of_mz_to", "y_axis", "Determines which axis is the m/z axis.");
 
88
    defaults_.setValidStrings("mapping_of_mz_to", StringList::create("x_axis,y_axis"));
 
89
    defaultsToParam_();
 
90
    setName("Spectrum2DCanvas");
 
91
    setParameters(preferences);
 
92
 
 
93
    projection_mz_.resize(1);
 
94
    projection_rt_.resize(1);
 
95
 
 
96
    //set preferences and update widgets accordingly
 
97
    if (String(param_.getValue("mapping_of_mz_to")) != "x_axis")
 
98
    {
 
99
      mzToXAxis(false);
 
100
    }
 
101
    //connect preferences change to the right slot
 
102
    connect(this, SIGNAL(preferencesChange()), this, SLOT(currentLayerParametersChanged_()));
 
103
  }
 
104
 
 
105
  Spectrum2DCanvas::~Spectrum2DCanvas()
 
106
  {
 
107
  }
 
108
 
 
109
  void Spectrum2DCanvas::highlightPeak_(QPainter & painter, const PeakIndex & peak)
 
110
  {
 
111
    if (!peak.isValid())
 
112
      return;
 
113
 
 
114
    //determine coordinates;
 
115
    QPoint pos;
 
116
    if (getCurrentLayer().type == LayerData::DT_FEATURE)
 
117
    {
110
118
      dataToWidget_(peak.getFeature(*getCurrentLayer().getFeatureMap()).getMZ(), peak.getFeature(*getCurrentLayer().getFeatureMap()).getRT(), pos);
111
 
                }
112
 
                else if (getCurrentLayer().type==LayerData::DT_PEAK)
113
 
                {
 
119
    }
 
120
    else if (getCurrentLayer().type == LayerData::DT_PEAK)
 
121
    {
114
122
      dataToWidget_(peak.getPeak(*getCurrentLayer().getPeakData()).getMZ(), peak.getSpectrum(*getCurrentLayer().getPeakData()).getRT(), pos);
115
 
                }
116
 
                else if (getCurrentLayer().type==LayerData::DT_CONSENSUS)
117
 
                {
 
123
    }
 
124
    else if (getCurrentLayer().type == LayerData::DT_CONSENSUS)
 
125
    {
118
126
      dataToWidget_(peak.getFeature(*getCurrentLayer().getConsensusMap()).getMZ(), peak.getFeature(*getCurrentLayer().getConsensusMap()).getRT(), pos);
119
 
                }
120
 
                else if (getCurrentLayer().type==LayerData::DT_CHROMATOGRAM)
121
 
                {
122
 
      const LayerData& layer = getCurrentLayer();
 
127
    }
 
128
    else if (getCurrentLayer().type == LayerData::DT_CHROMATOGRAM)
 
129
    {
 
130
      const LayerData & layer = getCurrentLayer();
123
131
      const ExperimentSharedPtrType exp = layer.getPeakData();
124
132
 
125
133
      // create iterator on chromatogram spectrum passed by PeakIndex
126
134
      vector<MSChromatogram<> >::const_iterator chrom_it = exp->getChromatograms().begin();
127
135
      chrom_it += peak.spectrum;
128
136
      dataToWidget_(chrom_it->getPrecursor().getMZ(), chrom_it->front().getRT(), pos);
129
 
                }
130
 
                else if (getCurrentLayer().type==LayerData::DT_IDENT)
131
 
                {
132
 
                        //TODO IDENT
133
 
                }
 
137
    }
 
138
    else if (getCurrentLayer().type == LayerData::DT_IDENT)
 
139
    {
 
140
      //TODO IDENT
 
141
    }
134
142
 
135
 
                //paint highlighed peak
136
 
                painter.save();
137
 
                painter.setPen(QPen(Qt::red, 2));
 
143
    //paint highlighed peak
 
144
    painter.save();
 
145
    painter.setPen(QPen(Qt::red, 2));
138
146
 
139
147
    if (getCurrentLayer().type == LayerData::DT_CHROMATOGRAM) // highlight: chromatogram
140
148
    {
141
 
      const LayerData& layer = getCurrentLayer();
 
149
      const LayerData & layer = getCurrentLayer();
142
150
      const ExperimentSharedPtrType exp = layer.getPeakData();
143
151
 
144
152
      vector<MSChromatogram<> >::const_iterator iter = exp->getChromatograms().begin();
145
153
      iter += peak.spectrum;
146
154
 
147
 
      painter.drawRect(pos.x() -5, pos.y() - 5, ( int((iter->back().getRT() - iter->front().getRT()) / visible_area_.height() * width())) + 10, 10);
 
155
      painter.drawRect(pos.x() - 5, pos.y() - 5, (int((iter->back().getRT() - iter->front().getRT()) / visible_area_.height() * width())) + 10, 10);
148
156
    }
149
157
    else // highlight: peak, feature, consensus feature
150
158
    {
151
159
      painter.drawEllipse(pos.x() - 5, pos.y() - 5, 10, 10);
152
160
    }
153
 
                
154
 
                //restore painter
155
 
                painter.restore();
156
 
        }
157
 
 
158
 
        PeakIndex Spectrum2DCanvas::findNearestPeak_(const QPoint& pos)
159
 
        {
160
 
                ///no layers => return invalid peak index
161
 
                if (layers_.empty()) return PeakIndex();
162
 
 
163
 
                //Constructing the area corrects swapped mapping of RT and m/z
164
 
                AreaType area (widgetToData_(pos - QPoint(5,5)),widgetToData_(pos + QPoint(5,5)));
165
 
 
166
 
                float max_int = -1 * numeric_limits<float>::max();
167
 
 
168
 
                if (getCurrentLayer().type==LayerData::DT_PEAK)
169
 
                {
170
 
       for (ExperimentType::ConstAreaIterator i = getCurrentLayer().getPeakData()->areaBeginConst(area.minPosition()[1],area.maxPosition()[1],
171
 
                                                                                                  area.minPosition()[0],area.maxPosition()[0]);
 
161
 
 
162
    //restore painter
 
163
    painter.restore();
 
164
  }
 
165
 
 
166
  PeakIndex Spectrum2DCanvas::findNearestPeak_(const QPoint & pos)
 
167
  {
 
168
    ///no layers => return invalid peak index
 
169
    if (layers_.empty())
 
170
      return PeakIndex();
 
171
 
 
172
    //Constructing the area corrects swapped mapping of RT and m/z
 
173
    AreaType area(widgetToData_(pos - QPoint(5, 5)), widgetToData_(pos + QPoint(5, 5)));
 
174
 
 
175
    float max_int = -1 * numeric_limits<float>::max();
 
176
    PeakIndex max_pi;
 
177
 
 
178
    if (getCurrentLayer().type == LayerData::DT_PEAK)
 
179
    {
 
180
      for (ExperimentType::ConstAreaIterator i = getCurrentLayer().getPeakData()->areaBeginConst(area.minPosition()[1], area.maxPosition()[1],
 
181
                                                                                                 area.minPosition()[0], area.maxPosition()[0]);
172
182
           i != getCurrentLayer().getPeakData()->areaEndConst();
173
 
                                         ++i)
174
 
                        {
175
 
                                PeakIndex pi = i.getPeakIndex();
176
 
        if (i->getIntensity() > max_int && getCurrentLayer().filters.passes((*getCurrentLayer().getPeakData())[pi.spectrum],pi.peak))
177
 
                                {
178
 
                                        //cout << "new max: " << i.getRT() << " " << i->getMZ() << endl;
179
 
                                        max_int = i->getIntensity();
180
 
                                        return i.getPeakIndex();
181
 
                                }
182
 
                        }
183
 
                }
184
 
                else if (getCurrentLayer().type==LayerData::DT_FEATURE)
185
 
                {
 
183
           ++i)
 
184
      {
 
185
        PeakIndex pi = i.getPeakIndex();
 
186
        if (i->getIntensity() > max_int && getCurrentLayer().filters.passes((*getCurrentLayer().getPeakData())[pi.spectrum], pi.peak))
 
187
        {
 
188
          //cout << "new max: " << i.getRT() << " " << i->getMZ() << endl;
 
189
          max_int = i->getIntensity();
 
190
          max_pi = pi;
 
191
        }
 
192
      }
 
193
    }
 
194
    else if (getCurrentLayer().type == LayerData::DT_FEATURE)
 
195
    {
186
196
      for (FeatureMapType::ConstIterator i = getCurrentLayer().getFeatureMap()->begin();
187
197
           i != getCurrentLayer().getFeatureMap()->end();
188
 
                                   ++i)
189
 
                        {
190
 
                                if ( i->getRT() >= area.minPosition()[1] &&
191
 
                                                 i->getRT() <= area.maxPosition()[1] &&
192
 
                                                 i->getMZ() >= area.minPosition()[0] &&
193
 
                                                 i->getMZ() <= area.maxPosition()[0] &&
194
 
                                                 getCurrentLayer().filters.passes(*i) )
195
 
                                {
196
 
                                        if (i->getIntensity() > max_int)
197
 
                                        {
198
 
                                                max_int = i->getIntensity();
199
 
 
200
 
            return PeakIndex(i-getCurrentLayer().getFeatureMap()->begin());
201
 
                                        }
202
 
                                }
203
 
                        }
204
 
                }
205
 
                else if (getCurrentLayer().type==LayerData::DT_CONSENSUS)
206
 
                {
 
198
           ++i)
 
199
      {
 
200
        if (i->getRT() >= area.minPosition()[1] &&
 
201
            i->getRT() <= area.maxPosition()[1] &&
 
202
            i->getMZ() >= area.minPosition()[0] &&
 
203
            i->getMZ() <= area.maxPosition()[0] &&
 
204
            getCurrentLayer().filters.passes(*i))
 
205
        {
 
206
          if (i->getIntensity() > max_int)
 
207
          {
 
208
            max_int = i->getIntensity();
 
209
            max_pi = PeakIndex(i - getCurrentLayer().getFeatureMap()->begin());
 
210
          }
 
211
        }
 
212
      }
 
213
    }
 
214
    else if (getCurrentLayer().type == LayerData::DT_CONSENSUS)
 
215
    {
207
216
      for (ConsensusMapType::ConstIterator i = getCurrentLayer().getConsensusMap()->begin();
208
217
           i != getCurrentLayer().getConsensusMap()->end();
209
 
                                   ++i)
210
 
                        {
 
218
           ++i)
 
219
      {
211
220
        // consensus feature in visible area?
212
 
                                if ( i->getRT() >= area.minPosition()[1] &&
213
 
                                                 i->getRT() <= area.maxPosition()[1] &&
214
 
                                                 i->getMZ() >= area.minPosition()[0] &&
215
 
                                                 i->getMZ() <= area.maxPosition()[0] &&
216
 
                                                 getCurrentLayer().filters.passes(*i) )
217
 
                                {
218
 
                                        if (i->getIntensity() > max_int)
219
 
                                        {            
220
 
                                                max_int = i->getIntensity();
221
 
            return PeakIndex(i - getCurrentLayer().getConsensusMap()->begin());
222
 
                                        }
223
 
                                }
224
 
                        }
225
 
                }
226
 
                else if (getCurrentLayer().type==LayerData::DT_CHROMATOGRAM)
227
 
                {
228
 
      const LayerData& layer = getCurrentLayer();
 
221
        if (i->getRT() >= area.minPosition()[1] &&
 
222
            i->getRT() <= area.maxPosition()[1] &&
 
223
            i->getMZ() >= area.minPosition()[0] &&
 
224
            i->getMZ() <= area.maxPosition()[0] &&
 
225
            getCurrentLayer().filters.passes(*i))
 
226
        {
 
227
          if (i->getIntensity() > max_int)
 
228
          {
 
229
            max_int = i->getIntensity();
 
230
            max_pi = PeakIndex(i - getCurrentLayer().getConsensusMap()->begin());
 
231
          }
 
232
        }
 
233
      }
 
234
    }
 
235
    else if (getCurrentLayer().type == LayerData::DT_CHROMATOGRAM)
 
236
    {
 
237
      const LayerData & layer = getCurrentLayer();
229
238
 
230
239
      MSExperiment<Peak1D> exp;
231
240
      exp = *layer.getPeakData();
233
242
 
234
243
      for (vector<MSChromatogram<> >::const_iterator iter = exp.getChromatograms().begin(); iter != exp.getChromatograms().end(); ++iter)
235
244
      {
 
245
        if (iter->empty()) {continue;} // ensure that empty chromatograms are not examined (iter->front = segfault)
236
246
        MSChromatogram<>::ConstIterator cit = iter->begin();
237
247
 
238
248
        // for (MSChromatogram<>::ConstIterator cit = iter->begin(); cit != iter->end(); ++cit)
240
250
        //   cout << "Chrom Values RT/INT: " << cit->getRT() << "/" << " " << cit->getIntensity() << endl;
241
251
        //  }
242
252
 
243
 
        if ( mz_origin != iter->getPrecursor().getMZ())
 
253
        if (mz_origin != iter->getPrecursor().getMZ())
244
254
        {
245
255
          mz_origin = iter->getPrecursor().getMZ();
246
256
        }
247
257
 
248
 
        for (int i = int(iter->front().getRT()); i <= int(iter->back().getRT()) ; i++ )
 
258
        for (int i = int(iter->front().getRT()); i <= int(iter->back().getRT()); i++)
249
259
        {
250
 
          if( i >= area.minPosition()[1] &&
 
260
          if (i >= area.minPosition()[1] &&
251
261
              i <= area.maxPosition()[1] &&
252
262
              mz_origin >= area.minPosition()[0] &&
253
263
              mz_origin <= area.maxPosition()[0])
254
264
          {
255
 
            return PeakIndex(iter-exp.getChromatograms().begin(), cit-iter->begin());
 
265
            return PeakIndex(iter - exp.getChromatograms().begin(), cit - iter->begin());
256
266
          }
257
267
        }
258
268
      }
259
 
                }
260
 
                else if (getCurrentLayer().type==LayerData::DT_IDENT)
261
 
                {
262
 
                        //TODO IDENT
263
 
                }
264
 
 
265
 
                return PeakIndex();
266
 
        }
267
 
 
268
 
        void Spectrum2DCanvas::paintDots_(Size layer_index, QPainter& painter)
269
 
        {
270
 
                const LayerData& layer = getLayer(layer_index);
271
 
 
272
 
                //update factors (snap and percentage)
273
 
                DoubleReal snap_factor = snap_factors_[layer_index];
274
 
                percentage_factor_ = 1.0;
275
 
                if (intensity_mode_ == IM_PERCENTAGE)
276
 
                {
277
 
      if (layer.type == LayerData::DT_PEAK && layer.getPeakData()->getMaxInt()>0.0)
278
 
                        {
279
 
        percentage_factor_ = overall_data_range_.maxPosition()[2]/layer.getPeakData()->getMaxInt();
280
 
                        }
281
 
      else if (layer.type == LayerData::DT_FEATURE && layer.getFeatureMap()->getMaxInt()>0.0)
282
 
                        {
283
 
        percentage_factor_ = overall_data_range_.maxPosition()[2]/layer.getFeatureMap()->getMaxInt();
284
 
                        }
285
 
      else if (layer.type == LayerData::DT_CONSENSUS && layer.getConsensusMap()->getMaxInt()>0.0)
286
 
                        {
287
 
        percentage_factor_ = overall_data_range_.maxPosition()[2]/layer.getConsensusMap()->getMaxInt();
288
 
                        }
289
 
      else if (layer.type == LayerData::DT_CHROMATOGRAM && layer.getConsensusMap()->getMaxInt()>0.0)
290
 
                        {
 
269
    }
 
270
    else if (getCurrentLayer().type == LayerData::DT_IDENT)
 
271
    {
 
272
      //TODO IDENT
 
273
    }
 
274
 
 
275
    return max_pi;
 
276
  }
 
277
 
 
278
  void Spectrum2DCanvas::paintDots_(Size layer_index, QPainter & painter)
 
279
  {
 
280
    const LayerData & layer = getLayer(layer_index);
 
281
 
 
282
    //update factors (snap and percentage)
 
283
    DoubleReal snap_factor = snap_factors_[layer_index];
 
284
    percentage_factor_ = 1.0;
 
285
    if (intensity_mode_ == IM_PERCENTAGE)
 
286
    {
 
287
      if (layer.type == LayerData::DT_PEAK && layer.getPeakData()->getMaxInt() > 0.0)
 
288
      {
 
289
        percentage_factor_ = overall_data_range_.maxPosition()[2] / layer.getPeakData()->getMaxInt();
 
290
      }
 
291
      else if (layer.type == LayerData::DT_FEATURE && layer.getFeatureMap()->getMaxInt() > 0.0)
 
292
      {
 
293
        percentage_factor_ = overall_data_range_.maxPosition()[2] / layer.getFeatureMap()->getMaxInt();
 
294
      }
 
295
      else if (layer.type == LayerData::DT_CONSENSUS && layer.getConsensusMap()->getMaxInt() > 0.0)
 
296
      {
 
297
        percentage_factor_ = overall_data_range_.maxPosition()[2] / layer.getConsensusMap()->getMaxInt();
 
298
      }
 
299
      else if (layer.type == LayerData::DT_CHROMATOGRAM && layer.getConsensusMap()->getMaxInt() > 0.0)
 
300
      {
291
301
        //TODO CHROM not sure if needed here
292
 
                        }
293
 
                }
294
 
 
295
 
                //temporary variables
296
 
                Int image_width = buffer_.width();
297
 
                Int image_height = buffer_.height();
298
 
                
299
 
                if (layer.type==LayerData::DT_PEAK) //peaks
300
 
                {
 
302
      }
 
303
    }
 
304
 
 
305
    //temporary variables
 
306
    Int image_width = buffer_.width();
 
307
    Int image_height = buffer_.height();
 
308
 
 
309
    if (layer.type == LayerData::DT_PEAK)   //peaks
 
310
    {
301
311
      // renaming some values for readability
302
 
      const ExperimentType& peak_map = *layer.getPeakData();
 
312
      const ExperimentType & peak_map = *layer.getPeakData();
303
313
      const DoubleReal rt_min = visible_area_.minPosition()[1];
304
314
      const DoubleReal rt_max = visible_area_.maxPosition()[1];
305
315
      const DoubleReal mz_min = visible_area_.minPosition()[0];
311
321
        return;
312
322
      }
313
323
 
314
 
                        //determine number of pixels for each dimension
 
324
      //determine number of pixels for each dimension
315
325
      Size rt_pixel_count = image_height;
316
326
      Size mz_pixel_count = image_width;
317
 
                        if(!isMzToXAxis())
318
 
                        {
319
 
                                rt_pixel_count = image_width;
320
 
                                mz_pixel_count = image_height;
321
 
                        }
322
 
                        
323
 
                        //-----------------------------------------------------------------------------------------------
 
327
      if (!isMzToXAxis())
 
328
      {
 
329
        rt_pixel_count = image_width;
 
330
        mz_pixel_count = image_height;
 
331
      }
 
332
 
 
333
      //-----------------------------------------------------------------------------------------------
324
334
      // Determine number of shown scans (MS1)
325
335
      Size n_ms1_scans = 0;
326
 
      for (ExperimentType::ConstIterator it = peak_map.RTBegin(rt_min); it != peak_map.RTEnd(rt_max); ++it)
327
 
                        {
 
336
      for (ExperimentType::ConstIterator it = peak_map.RTBegin(rt_min); it != peak_map.end() && it != peak_map.RTEnd(rt_max); ++it)
 
337
      {
328
338
        if (it->getMSLevel() == 1)
329
339
        {
330
340
          ++n_ms1_scans;
331
341
        }
332
 
                        }
333
 
 
334
 
      if (n_ms1_scans == 0)
335
 
      {
336
 
        return;
337
342
      }
338
343
 
339
344
      // create iterator on scan in the middle of the visible map
 
345
      Size n_peaks_in_middle_scan(0);
340
346
      ExperimentType::ConstIterator it = peak_map.RTBegin(rt_min) + n_ms1_scans / 2;
341
 
 
342
 
      Size n_peaks_in_middle_scan = 0;
343
 
      for  (ExperimentType::SpectrumType::ConstIterator it2 = it->MZBegin(mz_min); it2 != it->MZEnd(mz_max); ++it2)
 
347
      if (it != peak_map.end())
344
348
      {
345
 
        ++n_peaks_in_middle_scan;
 
349
        for (ExperimentType::SpectrumType::ConstIterator it2 = it->MZBegin(mz_min); it2 != it->end() && it2 != it->MZEnd(mz_max); ++it2)
 
350
        {
 
351
          ++n_peaks_in_middle_scan;
 
352
        }
346
353
      }
347
354
 
348
355
      // determine spacing for whole data
349
356
      DoubleReal min_spacing_mz = 1.0;
350
357
      DoubleReal average_spacing_rt = 1.0;
351
358
      Size n_scans = peak_map.size();
352
 
                        {           
 
359
      {
353
360
        vector<Real> mz_spacing;
354
361
        for (Size i = 0; i != n_scans; ++i)
355
362
        {
356
 
          // skipp non MS1 and empty spectra
357
 
          Size ms_level = peak_map[i].getMSLevel() ;
 
363
          // skip non MS1 and empty spectra
 
364
          Size ms_level = peak_map[i].getMSLevel();
358
365
          Size n_peaks = peak_map[i].size();
359
366
          if (ms_level != 1 || n_peaks < 2)
360
367
          {
361
368
            continue;
362
 
          }          
 
369
          }
363
370
          DoubleReal current_mz_spacing =  (peak_map[i][n_peaks - 1].getMZ() - peak_map[i][0].getMZ()) / n_peaks;
364
371
          mz_spacing.push_back(current_mz_spacing);
365
372
        }
373
380
 
374
381
        {
375
382
          vector<Real> rts;
376
 
          for(Size i = 0; i != n_scans; ++i)
 
383
          for (Size i = 0; i != n_scans; ++i)
377
384
          {
378
 
            // skipp non MS1 and empty spectra
379
 
            Size ms_level = peak_map[i].getMSLevel() ;
 
385
            // skip non MS1 and empty spectra
 
386
            Size ms_level = peak_map[i].getMSLevel();
380
387
            Size n_peaks = peak_map[i].size();
381
388
            if (ms_level != 1 || n_peaks == 0)
382
389
            {
387
394
          sort(rts.begin(), rts.end());
388
395
          if (rts.size() > 2)
389
396
          {
390
 
            average_spacing_rt = (rts[rts.size() - 1] - rts[0])/(DoubleReal)rts.size();
 
397
            average_spacing_rt = (rts[rts.size() - 1] - rts[0]) / (DoubleReal)rts.size();
391
398
          }
392
 
                                }
393
 
                        }
 
399
        }
 
400
      }
394
401
 
395
402
      // Determine whether several peaks are expected to be drawn on the same pixel
396
403
      if (n_peaks_in_middle_scan > mz_pixel_count || n_ms1_scans > rt_pixel_count)
397
 
                        {
 
404
      {
398
405
        // overlapping data points expected: draw maximum intensity
399
406
        paintMaximumIntensities_(layer_index, rt_pixel_count, mz_pixel_count, painter);
400
 
                        }
401
 
                        else
402
 
                        {
 
407
      }
 
408
      else
 
409
      {
 
410
        // calculate pixel width and height in rt/mz coordinates
 
411
        QPoint p1, p2;
 
412
        dataToWidget_(1, 1, p1);
 
413
        dataToWidget_(0, 0, p2);
 
414
        DoubleReal pixel_width = abs(p1.x() - p2.x());
 
415
        DoubleReal pixel_height = abs(p1.y() - p2.y());
 
416
 
 
417
        // when data is zoomed in to single peaks these are visualized as circles
 
418
        DoubleReal pen_width = qMax( 1.0, isMzToXAxis()
 
419
            ? min(pixel_width * min_spacing_mz, pixel_height * average_spacing_rt)
 
420
            : min(pixel_width * average_spacing_rt, pixel_height * min_spacing_mz) );
 
421
 
403
422
        // few data points expected: more expensive drawing of all datapoints (circles or points depending on zoom level)
404
 
        paintAllIntensities_(layer_index, min_spacing_mz, average_spacing_rt, painter);
405
 
                        }
 
423
        paintAllIntensities_(layer_index, pen_width, painter);
 
424
      }
406
425
 
407
 
                        //-----------------------------------------------------------------
408
 
                        //draw precursor peaks
 
426
      //-----------------------------------------------------------------
 
427
      //draw precursor peaks
409
428
      if (getLayerFlag(layer_index, LayerData::P_PRECURSORS))
410
 
                        {
 
429
      {
411
430
        paintPrecursorPeaks_(layer_index, painter);
412
 
                        }
413
 
                }
414
 
                else if (layer.type==LayerData::DT_FEATURE) //features
415
 
                {
 
431
      }
 
432
    }
 
433
    else if (layer.type == LayerData::DT_FEATURE)   //features
 
434
    {
416
435
      paintFeatureData_(layer_index, painter);
417
 
                }
418
 
                else if (layer.type==LayerData::DT_CONSENSUS)// consensus features
419
 
                {
420
 
                        String icon = layer.param.getValue("dot:feature_icon");
421
 
                        Size icon_size = layer.param.getValue("dot:feature_icon_size");
422
 
                        
 
436
    }
 
437
    else if (layer.type == LayerData::DT_CONSENSUS)  // consensus features
 
438
    {
 
439
      String icon = layer.param.getValue("dot:feature_icon");
 
440
      Size icon_size = layer.param.getValue("dot:feature_icon_size");
 
441
 
423
442
      for (ConsensusMapType::ConstIterator i = layer.getConsensusMap()->begin();
424
443
           i != layer.getConsensusMap()->end();
425
 
                                   ++i)
426
 
                        {
427
 
                                if ( i->getRT() >= visible_area_.minPosition()[1] &&
428
 
                                                 i->getRT() <= visible_area_.maxPosition()[1] &&
429
 
                                                 i->getMZ() >= visible_area_.minPosition()[0] &&
430
 
                                                 i->getMZ() <= visible_area_.maxPosition()[0] &&
431
 
                                                 layer.filters.passes(*i))
432
 
                                {
 
444
           ++i)
 
445
      {
 
446
        if (i->getRT() >= visible_area_.minPosition()[1] &&
 
447
            i->getRT() <= visible_area_.maxPosition()[1] &&
 
448
            i->getMZ() >= visible_area_.minPosition()[0] &&
 
449
            i->getMZ() <= visible_area_.maxPosition()[0] &&
 
450
            layer.filters.passes(*i))
 
451
        {
433
452
          // determine color
434
 
          QRgb color;
 
453
          QColor color;
435
454
          if (i->metaValueExists(5))
436
455
          {
437
 
           color = QColor(i->getMetaValue(5).toQString()).rgb();
 
456
            color = QColor(i->getMetaValue(5).toQString());
438
457
          }
439
458
          else
440
459
          {
441
460
            // use intensity as color
442
 
           color = heightColor_(i->getIntensity(), layer.gradient, snap_factor);
 
461
            color = heightColor_(i->getIntensity(), layer.gradient, snap_factor);
443
462
          }
444
463
 
445
464
          // paint
446
 
                                        QPoint pos;
447
 
                                        dataToWidget_(i->getMZ(),i->getRT(),pos);
448
 
                                        if (pos.x()>0 && pos.y()>0 && pos.x()<image_width-1 && pos.y()<image_height-1)
449
 
                                        {
450
 
                                                paintIcon_(pos, color, icon, icon_size, painter);
451
 
                                        }
452
 
                                }
453
 
                        }
454
 
                }
455
 
    else if (layer.type == LayerData::DT_CHROMATOGRAM)// chromatograms
456
 
                {
 
465
          QPoint pos;
 
466
          dataToWidget_(i->getMZ(), i->getRT(), pos);
 
467
          if (pos.x() > 0 && pos.y() > 0 && pos.x() < image_width - 1 && pos.y() < image_height - 1)
 
468
          {
 
469
            paintIcon_(pos, color.rgb(), icon, icon_size, painter);
 
470
          }
 
471
        }
 
472
      }
 
473
    }
 
474
    else if (layer.type == LayerData::DT_CHROMATOGRAM) // chromatograms
 
475
    {
457
476
      const MSExperiment<Peak1D> exp = *layer.getPeakData();
458
 
                        //TODO CHROM implement layer filters
459
 
                        //TODO CHROM implement faster painting
 
477
      //TODO CHROM implement layer filters
 
478
      //TODO CHROM implement faster painting
460
479
 
461
480
      // paint chromatogram rt start and end as line
462
481
      float mz_origin = 0;
465
484
 
466
485
      for (vector<MSChromatogram<> >::const_iterator iter = exp.getChromatograms().begin(); iter != exp.getChromatograms().end(); ++iter)
467
486
      {
468
 
        if ( mz_origin != iter->getPrecursor().getMZ())
 
487
        if (mz_origin != iter->getPrecursor().getMZ())
469
488
        {
470
489
          mz_origin = iter->getPrecursor().getMZ();
471
 
          min_rt = iter->front().getRT();
472
 
          max_rt = iter->back().getRT();
 
490
          if (!iter->empty())
 
491
          {
 
492
            min_rt = iter->front().getRT();
 
493
            max_rt = iter->back().getRT();
 
494
          }
473
495
        }
474
496
 
475
497
        QPoint posi;
476
498
        QPoint posi2;
477
499
 
478
 
        dataToWidget_(iter->getPrecursor().getMZ(), min_rt , posi);
479
 
        dataToWidget_(iter->getPrecursor().getMZ(), max_rt , posi2);
 
500
        dataToWidget_(iter->getPrecursor().getMZ(), min_rt, posi);
 
501
        dataToWidget_(iter->getPrecursor().getMZ(), max_rt, posi2);
480
502
 
481
503
        painter.drawLine(posi.x(), posi.y(), posi2.x(), posi2.y());
482
504
      }
483
505
    }
484
 
                else if (layer.type==LayerData::DT_IDENT) // peptide identifications
485
 
                {
486
 
                        paintIdentifications_(layer_index, painter);
487
 
                }
488
 
        }
 
506
    else if (layer.type == LayerData::DT_IDENT)   // peptide identifications
 
507
    {
 
508
      paintIdentifications_(layer_index, painter);
 
509
    }
 
510
  }
489
511
 
490
 
  void Spectrum2DCanvas::paintPrecursorPeaks_(Size layer_index, QPainter& painter)
 
512
  void Spectrum2DCanvas::paintPrecursorPeaks_(Size layer_index, QPainter & painter)
491
513
  {
492
 
    const LayerData& layer = getLayer(layer_index);
493
 
    const ExperimentType& peak_map = *layer.getPeakData();
 
514
    const LayerData & layer = getLayer(layer_index);
 
515
    const ExperimentType & peak_map = *layer.getPeakData();
494
516
 
495
517
    for (ExperimentType::ConstIterator i = peak_map.RTBegin(visible_area_.minPosition()[1]);
496
 
      i != peak_map.RTEnd(visible_area_.maxPosition()[1]);
497
 
      ++i)
 
518
         i != peak_map.RTEnd(visible_area_.maxPosition()[1]);
 
519
         ++i)
498
520
    {
499
521
      //this is an MS/MS scan
500
 
      if (i->getMSLevel()==2 && !i->getPrecursors().empty())
 
522
      if (i->getMSLevel() == 2 && !i->getPrecursors().empty())
501
523
      {
 
524
        QPoint pos_ms2;
 
525
        dataToWidget_(i->getPrecursors()[0].getMZ(), i->getRT(), pos_ms2);   // position of precursor in MS2
 
526
 
502
527
        ExperimentType::ConstIterator prec = peak_map.getPrecursorSpectrum(i);
503
528
 
504
529
        if (prec != peak_map.end())
505
530
        {
506
531
          QPoint pos_ms1;
507
532
          dataToWidget_(i->getPrecursors()[0].getMZ(), prec->getRT(), pos_ms1);  // position of precursor in MS1
508
 
          QPoint pos_ms2;
509
 
          dataToWidget_(i->getPrecursors()[0].getMZ(), i->getRT(), pos_ms2);   // position of precursor in MS2
510
533
          QPen p;
511
534
          p.setColor(Qt::black);
512
535
          painter.setPen(p);
513
536
 
514
537
          // diamond shape in MS1
515
 
          painter.drawLine(pos_ms1.x(), pos_ms1.y()+3, pos_ms1.x()+3, pos_ms1.y());
516
 
          painter.drawLine(pos_ms1.x()+3, pos_ms1.y(), pos_ms1.x(), pos_ms1.y()-3);
517
 
          painter.drawLine(pos_ms1.x(), pos_ms1.y()-3, pos_ms1.x()-3, pos_ms1.y());
518
 
          painter.drawLine(pos_ms1.x()-3, pos_ms1.y(), pos_ms1.x(), pos_ms1.y()+3);
 
538
          painter.drawLine(pos_ms1.x(), pos_ms1.y() + 3, pos_ms1.x() + 3, pos_ms1.y());
 
539
          painter.drawLine(pos_ms1.x() + 3, pos_ms1.y(), pos_ms1.x(), pos_ms1.y() - 3);
 
540
          painter.drawLine(pos_ms1.x(), pos_ms1.y() - 3, pos_ms1.x() - 3, pos_ms1.y());
 
541
          painter.drawLine(pos_ms1.x() - 3, pos_ms1.y(), pos_ms1.x(), pos_ms1.y() + 3);
519
542
 
520
543
          // rt position of corresponding MS2
521
 
          painter.drawLine(pos_ms2.x()-3, pos_ms2.y(), pos_ms2.x()+3, pos_ms2.y());
 
544
          painter.drawLine(pos_ms2.x() - 3, pos_ms2.y(), pos_ms2.x() + 3, pos_ms2.y());
522
545
          painter.drawLine(pos_ms1.x(), pos_ms1.y(), pos_ms2.x(), pos_ms2.y());
523
546
        }
 
547
        else // no preceding MS1
 
548
        {
 
549
          // rt position of corresponding MS2 (cross)
 
550
          painter.drawLine(pos_ms2.x() - 3, pos_ms2.y(), pos_ms2.x() + 3, pos_ms2.y());
 
551
          painter.drawLine(pos_ms2.x(), pos_ms2.y() - 3, pos_ms2.x(), pos_ms2.y() + 3);
 
552
        }
524
553
      }
525
554
    }
526
555
  }
527
556
 
528
 
  void Spectrum2DCanvas::paintAllIntensities_(Size layer_index, DoubleReal minimum_spacing_mz, DoubleReal average_spacing_rt, QPainter& painter)
 
557
  void Spectrum2DCanvas::paintAllIntensities_(Size layer_index, DoubleReal pen_width, QPainter & painter)
529
558
  {
530
 
    const LayerData& layer = getLayer(layer_index);
 
559
    const LayerData & layer = getLayer(layer_index);
531
560
    Int image_width = buffer_.width();
532
561
    Int image_height = buffer_.height();
 
562
    QVector<QPolygon> coloredPoints( layer.gradient.precalculatedSize() );
533
563
 
534
 
    const ExperimentType& map = *layer.getPeakData();
 
564
    const ExperimentType & map = *layer.getPeakData();
535
565
    const DoubleReal rt_min = visible_area_.minPosition()[1];
536
566
    const DoubleReal rt_max = visible_area_.maxPosition()[1];
537
567
    const DoubleReal mz_min = visible_area_.minPosition()[0];
539
569
 
540
570
    DoubleReal snap_factor = snap_factors_[layer_index];
541
571
 
542
 
    // calculate pixel width and height in rt/mz coordinates
543
 
    QPoint p1, p2;
544
 
    dataToWidget_(1, 1, p1);
545
 
    dataToWidget_(0, 0, p2);
546
 
    DoubleReal pixel_width = abs(p1.x()-p2.x());
547
 
    DoubleReal pixel_height = abs(p1.y()-p2.y());
548
 
 
549
 
    // when data is zoomed in to single peaks these are visualized as circles
550
 
    Int circle_size = 0;
551
 
 
552
 
    if(isMzToXAxis())
553
 
    {
554
 
      circle_size = min((Int)(pixel_width * minimum_spacing_mz),(Int)(pixel_height * average_spacing_rt))/2.0;
555
 
    } else
556
 
    {
557
 
      circle_size = min((Int)(pixel_width * average_spacing_rt),(Int)(pixel_height * minimum_spacing_mz))/2.0;
558
 
    }
559
 
 
560
572
    for (ExperimentType::ConstAreaIterator i = map.areaBeginConst(rt_min, rt_max, mz_min, mz_max);
561
573
         i != map.areaEndConst();
562
574
         ++i)
563
575
    {
564
576
      PeakIndex pi = i.getPeakIndex();
565
 
      if (layer.filters.passes(map[pi.spectrum],pi.peak))
 
577
      if (layer.filters.passes(map[pi.spectrum], pi.peak))
566
578
      {
567
579
        QPoint pos;
568
580
        dataToWidget_(i->getMZ(), i.getRT(), pos);
569
 
        if (pos.x()>0 && pos.y()>0 && pos.x()<image_width-1 && pos.y()<image_height-1)
 
581
        if (pos.x() > 0 && pos.y() > 0 && pos.x() < image_width - 1 && pos.y() < image_height - 1)
570
582
        {
571
 
          QRgb color = heightColor_(i->getIntensity(), layer.gradient, snap_factor);
572
 
 
573
 
          if (circle_size <= 1)
574
 
          {
575
 
            painter.setPen(QColor(color));
576
 
            painter.drawPoint(pos.x(), pos.y());
577
 
          } else
578
 
          {
579
 
            painter.setPen(Qt::NoPen);
580
 
            painter.setBrush(QBrush(color));
581
 
 
582
 
            painter.drawChord(
583
 
                QRect(pos.x()-(int)circle_size/2, pos.y()-(int)circle_size/2,
584
 
                      circle_size, circle_size),
585
 
                      0,
586
 
                      16*360
587
 
                );
588
 
          }
 
583
          // store point in the array of its color
 
584
          Int colorIndex = precalculatedColorIndex_(i->getIntensity(), layer.gradient, snap_factor);
 
585
          coloredPoints[ colorIndex ].push_back( pos );
589
586
        }
590
587
      }
591
588
    }
592
 
    painter.setBrush(QBrush());
 
589
    // draw point arrays from minimum to maximum intensity,
 
590
    // avoiding low-intensity points obscuring the high-intensity ones
 
591
    painter.save();
 
592
    QPen newPointsPen;
 
593
    newPointsPen.setWidthF( pen_width );
 
594
    for ( Int colorIx = 0; colorIx < coloredPoints.size(); colorIx++ ) {
 
595
        const QPolygon& pointsArr = coloredPoints[colorIx];
 
596
        if ( pointsArr.size() ) {
 
597
            newPointsPen.setColor( layer.gradient.precalculatedColorByIndex( colorIx ) );
 
598
            painter.setPen( newPointsPen );
 
599
            painter.drawPoints( pointsArr );
 
600
        }
 
601
    }
 
602
    painter.restore();
 
603
  }
 
604
 
 
605
  void Spectrum2DCanvas::paintMaximumIntensities_(Size layer_index, Size rt_pixel_count, Size mz_pixel_count, QPainter & painter)
 
606
  {
 
607
    //set painter to black (we operate directly on the pixels for all colored data)
593
608
    painter.setPen(Qt::black);
 
609
    //temporary variables
 
610
    Int image_width = buffer_.width();
 
611
    Int image_height = buffer_.height();
 
612
 
 
613
    const LayerData & layer = getLayer(layer_index);
 
614
    const ExperimentType & map = *layer.getPeakData();
 
615
    const DoubleReal rt_min = visible_area_.minPosition()[1];
 
616
    const DoubleReal rt_max = visible_area_.maxPosition()[1];
 
617
    const DoubleReal mz_min = visible_area_.minPosition()[0];
 
618
    const DoubleReal mz_max = visible_area_.maxPosition()[0];
 
619
 
 
620
    DoubleReal snap_factor = snap_factors_[layer_index];
 
621
 
 
622
    //calculate pixel size in data coordinates
 
623
    DoubleReal rt_step_size = (rt_max - rt_min) / rt_pixel_count;
 
624
    DoubleReal mz_step_size = (mz_max - mz_min) / mz_pixel_count;
 
625
 
 
626
    //iterate over all pixels (RT dimension)
 
627
    Size scan_index = 0;
 
628
    for (Size rt = 0; rt < rt_pixel_count; ++rt)
 
629
    {
 
630
      DoubleReal rt_start = rt_min + rt_step_size * rt;
 
631
      DoubleReal rt_end = rt_start + rt_step_size;
 
632
      //cout << "rt: " << rt << " (" << rt_start << " - " << rt_end << ")" << endl;
 
633
 
 
634
      //determine the relevant spectra and reserve an array for the peak indices
 
635
      vector<Size> scan_indices, peak_indices;
 
636
      for (Size i = scan_index; i < map.size(); ++i)
 
637
      {
 
638
        if (map[i].getRT() >= rt_end)
 
639
        {
 
640
          scan_index = i;   //store last scan index for next RT pixel
 
641
          break;
 
642
        }
 
643
        if (map[i].getMSLevel() == 1 && map[i].size() > 0)
 
644
        {
 
645
          scan_indices.push_back(i);
 
646
          peak_indices.push_back(map[i].MZBegin(mz_min) - map[i].begin());
 
647
        }
 
648
        //set the scan index past the end. Otherwise the last scan will be repeated for all following RTs
 
649
        if (i == map.size() - 1)
 
650
          scan_index = i + 1;
 
651
      }
 
652
      //cout << "  scans: " << scan_indices.size() << endl;
 
653
 
 
654
      if (scan_indices.empty())
 
655
        continue;
 
656
 
 
657
      //iterate over all pixels (m/z dimension)
 
658
      for (Size mz = 0; mz < mz_pixel_count; ++mz)
 
659
      {
 
660
        DoubleReal mz_start = mz_min + mz_step_size * mz;
 
661
        DoubleReal mz_end = mz_start + mz_step_size;
 
662
 
 
663
        //iterate over all relevant peaks in all relevant scans
 
664
        Real max = -1.0;
 
665
        for (Size i = 0; i < scan_indices.size(); ++i)
 
666
        {
 
667
          Size s = scan_indices[i];
 
668
          Size p = peak_indices[i];
 
669
          for (; p < map[s].size(); ++p)
 
670
          {
 
671
            if (map[s][p].getMZ() >= mz_end)
 
672
              break;
 
673
            if (map[s][p].getIntensity() > max && layer.filters.passes(map[s], p))
 
674
            {
 
675
              max = map[s][p].getIntensity();
 
676
            }
 
677
          }
 
678
          peak_indices[i] = p;   //store last peak index for next m/z pixel
 
679
        }
 
680
 
 
681
        //draw to buffer
 
682
        if (max >= 0.0)
 
683
        {
 
684
          QPoint pos;
 
685
          dataToWidget_(mz_start + 0.5 * mz_step_size, rt_start + 0.5 * rt_step_size, pos);
 
686
          if (pos.y() < image_height && pos.x() < image_width)
 
687
          {
 
688
            buffer_.setPixel(pos.x(), pos.y(), heightColor_(max, layer.gradient, snap_factor).rgb());
 
689
          }
 
690
        }
 
691
      }
 
692
    }
594
693
  }
595
694
 
596
 
  void Spectrum2DCanvas::paintMaximumIntensities_(Size layer_index, Size rt_pixel_count, Size mz_pixel_count, QPainter& painter)
597
 
  {
598
 
      //set painter to black (we operate directly on the pixels for all colored data)
599
 
      painter.setPen(Qt::black);
600
 
      //temporary variables
601
 
      Int image_width = buffer_.width();
602
 
      Int image_height = buffer_.height();
603
 
 
604
 
      const LayerData& layer = getLayer(layer_index);
605
 
      const ExperimentType& map = *layer.getPeakData();
606
 
      const DoubleReal rt_min = visible_area_.minPosition()[1];
607
 
      const DoubleReal rt_max = visible_area_.maxPosition()[1];
608
 
      const DoubleReal mz_min = visible_area_.minPosition()[0];
609
 
      const DoubleReal mz_max = visible_area_.maxPosition()[0];
610
 
 
611
 
      DoubleReal snap_factor = snap_factors_[layer_index];
612
 
 
613
 
      //calculate pixel size in data coordinates
614
 
      DoubleReal rt_step_size = (rt_max - rt_min) / rt_pixel_count;
615
 
      DoubleReal mz_step_size = (mz_max - mz_min) / mz_pixel_count;
616
 
 
617
 
      //iterate over all pixels (RT dimension)
618
 
      Size scan_index = 0;
619
 
      for (Size rt=0; rt<rt_pixel_count; ++rt)
620
 
      {
621
 
        DoubleReal rt_start = rt_min + rt_step_size * rt;
622
 
        DoubleReal rt_end = rt_start + rt_step_size;
623
 
        //cout << "rt: " << rt << " (" << rt_start << " - " << rt_end << ")" << endl;
624
 
 
625
 
        //determine the relevant spectra and reserve an array for the peak indices
626
 
        vector<Size> scan_indices, peak_indices;
627
 
        for (Size i=scan_index; i<map.size(); ++i)
628
 
        {
629
 
          if (map[i].getRT()>=rt_end)
630
 
          {
631
 
            scan_index = i; //store last scan index for next RT pixel
632
 
            break;
633
 
          }
634
 
          if (map[i].getMSLevel()==1 && map[i].size()>0)
635
 
          {
636
 
            scan_indices.push_back(i);
637
 
            peak_indices.push_back(map[i].MZBegin(mz_min) - map[i].begin());
638
 
          }
639
 
          //set the scan index past the end. Otherwise the last scan will be repeated for all following RTs
640
 
          if (i==map.size()-1) scan_index=i+1;
641
 
        }
642
 
        //cout << "  scans: " << scan_indices.size() << endl;
643
 
 
644
 
        if (scan_indices.empty()) continue;
645
 
 
646
 
        //iterate over all pixels (m/z dimension)
647
 
        for (Size mz=0; mz<mz_pixel_count; ++mz)
648
 
        {
649
 
          DoubleReal mz_start = mz_min + mz_step_size * mz;
650
 
          DoubleReal mz_end = mz_start + mz_step_size;
651
 
 
652
 
          //iterate over all relevant peaks in all relevant scans
653
 
          Real max = -1.0;
654
 
          for (Size i=0; i<scan_indices.size(); ++i)
655
 
          {
656
 
            Size s = scan_indices[i];
657
 
            Size p = peak_indices[i];
658
 
            for(; p<map[s].size(); ++p)
659
 
            {
660
 
              if (map[s][p].getMZ()>=mz_end) break;
661
 
              if (map[s][p].getIntensity() > max && layer.filters.passes(map[s],p))
662
 
              {
663
 
                max = map[s][p].getIntensity();
664
 
              }
665
 
            }
666
 
            peak_indices[i] = p; //store last peak index for next m/z pixel
667
 
          }
668
 
 
669
 
          //draw to buffer
670
 
          if (max>=0.0)
671
 
          {
672
 
            QPoint pos;
673
 
            dataToWidget_(mz_start + 0.5 * mz_step_size, rt_start + 0.5 * rt_step_size, pos);
674
 
            if (pos.y()<image_height && pos.x()<image_width)
675
 
            {
676
 
              buffer_.setPixel(pos.x() , pos.y(), heightColor_(max, layer.gradient, snap_factor));
677
 
            }
678
 
          }
679
 
        }
680
 
      }
681
 
    }
682
 
 
683
695
  void Spectrum2DCanvas::paintFeatureData_(Size layer_index, QPainter& painter)
684
696
  {
685
697
    const LayerData& layer = getLayer(layer_index);
690
702
    int line_spacing = QFontMetrics(painter.font()).lineSpacing();
691
703
    String icon = layer.param.getValue("dot:feature_icon");
692
704
    Size icon_size = layer.param.getValue("dot:feature_icon_size");
693
 
    bool show_label = (layer.label!=LayerData::L_NONE);
694
 
    UInt num=0;
 
705
    bool show_label = (layer.label != LayerData::L_NONE);
 
706
    UInt num = 0;
695
707
    for (FeatureMapType::ConstIterator i = layer.getFeatureMap()->begin();
696
 
         i != layer.getFeatureMap()->end();
697
 
         ++i)
 
708
         i != layer.getFeatureMap()->end(); ++i)
698
709
    {
699
 
      if ( i->getRT() >= visible_area_.minPosition()[1] &&
700
 
           i->getRT() <= visible_area_.maxPosition()[1] &&
701
 
           i->getMZ() >= visible_area_.minPosition()[0] &&
702
 
           i->getMZ() <= visible_area_.maxPosition()[0] &&
703
 
           layer.filters.passes(*i))
 
710
      if (i->getRT() >= visible_area_.minPosition()[1] &&
 
711
          i->getRT() <= visible_area_.maxPosition()[1] &&
 
712
          i->getMZ() >= visible_area_.minPosition()[0] &&
 
713
          i->getMZ() <= visible_area_.maxPosition()[0] &&
 
714
          layer.filters.passes(*i))
704
715
      {
705
 
        //determine color
706
 
        QRgb color;
 
716
        // determine color
 
717
        QColor color;
707
718
        if (i->metaValueExists(5))
708
719
        {
709
 
          color = QColor(i->getMetaValue(5).toQString()).rgb();
 
720
          color = QColor(i->getMetaValue(5).toQString());
710
721
        }
711
722
        else
712
723
        {
713
724
          color = heightColor_(i->getIntensity(), layer.gradient, snap_factor);
714
725
        }
715
 
        //paint
 
726
        // paint
716
727
        QPoint pos;
717
 
        dataToWidget_(i->getMZ(),i->getRT(),pos);
718
 
        if (pos.x()>0 && pos.y()>0 && pos.x()<image_width-1 && pos.y()<image_height-1)
 
728
        dataToWidget_(i->getMZ(), i->getRT(), pos);
 
729
        if (pos.x() > 0 && pos.y() > 0 && pos.x() < image_width - 1 && pos.y() < image_height - 1)
719
730
        {
720
 
          paintIcon_(pos, color, icon, icon_size, painter);
 
731
          paintIcon_(pos, color.rgb(), icon, icon_size, painter);
721
732
        }
722
 
        //labels
 
733
        // labels
723
734
        if (show_label)
724
735
        {
725
 
          if (layer.label==LayerData::L_INDEX)
726
 
          {
727
 
            painter.drawText(pos.x()+10,pos.y()+10,QString::number(num));
728
 
          }
729
 
          else if (layer.label==LayerData::L_ID)
730
 
          {
731
 
            if (i->getPeptideIdentifications().size() && i->getPeptideIdentifications()[0].getHits().size())
732
 
            {
733
 
              painter.drawText(pos.x()+10,pos.y()+10,i->getPeptideIdentifications()[0].getHits()[0].getSequence().toString().toQString());
734
 
            }
735
 
          }
736
 
          else if (layer.label==LayerData::L_ID_ALL)
737
 
          {
738
 
            if (i->getPeptideIdentifications().size() )
739
 
            {
740
 
              for (Size j=0; j< i->getPeptideIdentifications()[0].getHits().size(); ++j)
741
 
              {
742
 
                painter.drawText(pos.x()+10,pos.y()+10+int(j)*line_spacing,i->getPeptideIdentifications()[0].getHits()[j].getSequence().toString().toQString());
743
 
              }
744
 
            }
745
 
          }
746
 
          else if (layer.label==LayerData::L_META_LABEL)
747
 
          {
748
 
            painter.drawText(pos.x()+10,pos.y()+10,i->getMetaValue(3).toQString());
 
736
          if (layer.label == LayerData::L_INDEX)
 
737
          {
 
738
            painter.setPen(Qt::darkBlue);
 
739
            painter.drawText(pos.x() + 10, pos.y() + 10, QString::number(num));
 
740
          }
 
741
          else if ((layer.label == LayerData::L_ID || layer.label == LayerData::L_ID_ALL) && !i->getPeptideIdentifications().empty() && !i->getPeptideIdentifications()[0].getHits().empty())
 
742
          {
 
743
            painter.setPen(Qt::darkGreen);
 
744
            Size maxHits = (layer.label == LayerData::L_ID_ALL) ? i->getPeptideIdentifications()[0].getHits().size() : 1;
 
745
            for (Size j = 0; j < maxHits; ++j)
 
746
            {
 
747
              painter.drawText(pos.x() + 10, pos.y() + 10 + int(j) * line_spacing, i->getPeptideIdentifications()[0].getHits()[j].getSequence().toString().toQString());
 
748
            }
 
749
          }
 
750
          else if (layer.label == LayerData::L_META_LABEL)
 
751
          {
 
752
            painter.setPen(Qt::darkBlue);
 
753
            painter.drawText(pos.x() + 10, pos.y() + 10, i->getMetaValue(3).toQString());
749
754
          }
750
755
        }
751
756
      }
753
758
    }
754
759
  }
755
760
 
756
 
  void Spectrum2DCanvas::paintIcon_(const QPoint& pos, const QRgb& color, const String& icon, Size s, QPainter& p) const
757
 
        {
758
 
                p.save();
759
 
                p.setPen(color);
760
 
                p.setBrush(QBrush(QColor(color),Qt::SolidPattern));
761
 
                
762
 
                int s_half = (int)s/2;
763
 
                
764
 
                if (icon=="diamond")
765
 
                {
766
 
                        QPolygon pol;
767
 
                        pol.putPoints(0,4,pos.x() + s_half, pos.y(),
768
 
                                                                                                pos.x()         , pos.y() + s_half,
769
 
                                                                                                pos.x() - (int)s_half, pos.y()         ,
770
 
                                                                                                pos.x()         , pos.y() - (int)s_half);
771
 
                        p.drawConvexPolygon(pol);
772
 
                }
773
 
                else if (icon=="square")
774
 
                {
775
 
                        QPolygon pol;
776
 
                        pol.putPoints(0,4,pos.x() + s_half, pos.y() + s_half,
777
 
                                                                                                pos.x() - s_half, pos.y() + s_half,
778
 
                                                                                                pos.x() - s_half, pos.y() - s_half,
779
 
                                                                                                pos.x() + s_half, pos.y() - s_half);
780
 
                        p.drawConvexPolygon(pol);
781
 
                }
782
 
                else if (icon=="circle")
783
 
                {
784
 
                        p.drawEllipse(QRectF(pos.x()-s_half,pos.y()-s_half,s,s));
785
 
                }
786
 
                else if (icon=="triangle")
787
 
                {
788
 
                        QPolygon pol;
789
 
                        pol.putPoints(0,3,pos.x()         , pos.y() + s_half,
790
 
                                                                                                pos.x() + s_half, pos.y() - (int)s_half,
791
 
                                                                                                pos.x() - (int)s_half, pos.y() - (int)s_half);
792
 
                        p.drawConvexPolygon(pol);
793
 
                }
794
 
                p.restore();
795
 
        }
796
 
 
797
 
        void Spectrum2DCanvas::paintTraceConvexHulls_(Size layer_index, QPainter& painter)
798
 
        {
799
 
                painter.setPen(Qt::black);
800
 
 
801
 
                const LayerData& layer = getLayer(layer_index);
802
 
    for (FeatureMapType::ConstIterator i = layer.getFeatureMap()->begin(); i != layer.getFeatureMap()->end(); ++i)
803
 
                {
804
 
                        if ( i->getRT() >= visible_area_.minPosition()[1] &&
805
 
                                         i->getRT() <= visible_area_.maxPosition()[1] &&
806
 
                                         i->getMZ() >= visible_area_.minPosition()[0] &&
807
 
                                         i->getMZ() <= visible_area_.maxPosition()[0] &&
808
 
                                         layer.filters.passes(*i)
809
 
                                 )
810
 
                        {
811
 
                                paintConvexHulls_(i->getConvexHulls(),painter);
812
 
                        }
813
 
                }
814
 
        }
815
 
 
816
 
        void Spectrum2DCanvas::paintFeatureConvexHulls_(Size layer_index, QPainter& painter)
817
 
        {
818
 
                painter.setPen(Qt::black);
819
 
                const LayerData& layer = getLayer(layer_index);
820
 
    for (FeatureMapType::ConstIterator i = layer.getFeatureMap()->begin(); i != layer.getFeatureMap()->end(); ++i)
821
 
                {
822
 
                        if ( i->getRT() >= visible_area_.minPosition()[1] &&
823
 
                                         i->getRT() <= visible_area_.maxPosition()[1] &&
824
 
                                         i->getMZ() >= visible_area_.minPosition()[0] &&
825
 
                                         i->getMZ() <= visible_area_.maxPosition()[0] &&
826
 
                                         layer.filters.passes(*i))
827
 
                        {
828
 
                                //paint hull points
829
 
                                ConvexHull2D hull = i->getConvexHull();
830
 
                                ConvexHull2D::PointArrayType ch_points = hull.getHullPoints();
831
 
                                QPolygon points;
832
 
                                points.resize((int)ch_points.size());
833
 
 
834
 
                                UInt index=0;
835
 
                                QPoint pos;
836
 
                                //iterate over hull points
837
 
 
838
 
                                for(ConvexHull2D::PointArrayType::const_iterator it=ch_points.begin(); it!=ch_points.end(); ++it, ++index)
839
 
                                {
840
 
                                        dataToWidget_(it->getY(), it->getX(),pos);
841
 
                                        points.setPoint(index, pos);
842
 
                                }
843
 
                                //cout << "Hull: " << hull << " Points: " << points.size()<<endl;
844
 
                                painter.drawPolygon(points);
845
 
                        }
846
 
                }
847
 
        }
848
 
 
849
 
        void Spectrum2DCanvas::paintIdentifications_(Size layer_index, QPainter& painter)
850
 
        {
851
 
                const LayerData& layer = getLayer(layer_index);
852
 
                vector<PeptideIdentification>::const_iterator pep_begin, pep_end;
853
 
                if (layer.type == LayerData::DT_FEATURE)
854
 
                {
 
761
  void Spectrum2DCanvas::paintIcon_(const QPoint & pos, const QRgb & color, const String & icon, Size s, QPainter & p) const
 
762
  {
 
763
    p.save();
 
764
    p.setPen(color);
 
765
    p.setBrush(QBrush(QColor(color), Qt::SolidPattern));
 
766
 
 
767
    int s_half = (int)s / 2;
 
768
 
 
769
    if (icon == "diamond")
 
770
    {
 
771
      QPolygon pol;
 
772
      pol.putPoints(0, 4, pos.x() + s_half, pos.y(),
 
773
                    pos.x(), pos.y() + s_half,
 
774
                    pos.x() - (int)s_half, pos.y(),
 
775
                    pos.x(), pos.y() - (int)s_half);
 
776
      p.drawConvexPolygon(pol);
 
777
    }
 
778
    else if (icon == "square")
 
779
    {
 
780
      QPolygon pol;
 
781
      pol.putPoints(0, 4, pos.x() + s_half, pos.y() + s_half,
 
782
                    pos.x() - s_half, pos.y() + s_half,
 
783
                    pos.x() - s_half, pos.y() - s_half,
 
784
                    pos.x() + s_half, pos.y() - s_half);
 
785
      p.drawConvexPolygon(pol);
 
786
    }
 
787
    else if (icon == "circle")
 
788
    {
 
789
      p.drawEllipse(QRectF(pos.x() - s_half, pos.y() - s_half, s, s));
 
790
    }
 
791
    else if (icon == "triangle")
 
792
    {
 
793
      QPolygon pol;
 
794
      pol.putPoints(0, 3, pos.x(), pos.y() + s_half,
 
795
                    pos.x() + s_half, pos.y() - (int)s_half,
 
796
                    pos.x() - (int)s_half, pos.y() - (int)s_half);
 
797
      p.drawConvexPolygon(pol);
 
798
    }
 
799
    p.restore();
 
800
  }
 
801
 
 
802
  void Spectrum2DCanvas::paintTraceConvexHulls_(Size layer_index, QPainter & painter)
 
803
  {
 
804
    painter.setPen(Qt::black);
 
805
 
 
806
    const LayerData & layer = getLayer(layer_index);
 
807
    for (FeatureMapType::ConstIterator i = layer.getFeatureMap()->begin(); i != layer.getFeatureMap()->end(); ++i)
 
808
    {
 
809
      if (i->getRT() >= visible_area_.minPosition()[1] &&
 
810
          i->getRT() <= visible_area_.maxPosition()[1] &&
 
811
          i->getMZ() >= visible_area_.minPosition()[0] &&
 
812
          i->getMZ() <= visible_area_.maxPosition()[0] &&
 
813
          layer.filters.passes(*i)
 
814
          )
 
815
      {
 
816
        bool hasIdentifications = i->getPeptideIdentifications().size()>0
 
817
                               && i->getPeptideIdentifications()[0].getHits().size()>0;
 
818
        paintConvexHulls_(i->getConvexHulls(), hasIdentifications, painter);
 
819
      }
 
820
    }
 
821
  }
 
822
 
 
823
  void Spectrum2DCanvas::paintFeatureConvexHulls_(Size layer_index, QPainter & painter)
 
824
  {
 
825
    const LayerData & layer = getLayer(layer_index);
 
826
    for (FeatureMapType::ConstIterator i = layer.getFeatureMap()->begin(); i != layer.getFeatureMap()->end(); ++i)
 
827
    {
 
828
      if (i->getRT() >= visible_area_.minPosition()[1] &&
 
829
          i->getRT() <= visible_area_.maxPosition()[1] &&
 
830
          i->getMZ() >= visible_area_.minPosition()[0] &&
 
831
          i->getMZ() <= visible_area_.maxPosition()[0] &&
 
832
          layer.filters.passes(*i))
 
833
      {
 
834
        //paint hull points
 
835
        ConvexHull2D hull = i->getConvexHull();
 
836
        ConvexHull2D::PointArrayType ch_points = hull.getHullPoints();
 
837
        QPolygon points;
 
838
        points.resize((int)ch_points.size());
 
839
 
 
840
        UInt index = 0;
 
841
        QPoint pos;
 
842
        //iterate over hull points
 
843
 
 
844
        for (ConvexHull2D::PointArrayType::const_iterator it = ch_points.begin(); it != ch_points.end(); ++it, ++index)
 
845
        {
 
846
          dataToWidget_(it->getY(), it->getX(), pos);
 
847
          points.setPoint(index, pos);
 
848
        }
 
849
        //cout << "Hull: " << hull << " Points: " << points.size()<<endl;
 
850
        bool hasIdentifications = i->getPeptideIdentifications().size()>0
 
851
                               && i->getPeptideIdentifications()[0].getHits().size()>0;
 
852
        painter.setPen( hasIdentifications ? Qt::darkGreen : Qt::darkBlue );
 
853
        painter.drawPolygon(points);
 
854
      }
 
855
    }
 
856
  }
 
857
 
 
858
  void Spectrum2DCanvas::paintIdentifications_(Size layer_index, QPainter & painter)
 
859
  {
 
860
    const LayerData & layer = getLayer(layer_index);
 
861
    vector<PeptideIdentification>::const_iterator pep_begin, pep_end;
 
862
    if (layer.type == LayerData::DT_FEATURE)
 
863
    {
855
864
      pep_begin = layer.getFeatureMap()->getUnassignedPeptideIdentifications().begin();
856
865
      pep_end = layer.getFeatureMap()->getUnassignedPeptideIdentifications().end();
857
 
                }
858
 
                else if (layer.type == LayerData::DT_IDENT)
859
 
                {
860
 
                        pep_begin = layer.peptides.begin();
861
 
                        pep_end = layer.peptides.end();
862
 
                }
863
 
                else return;
864
 
 
865
 
                painter.setPen(Qt::black);
866
 
 
867
 
                for (; pep_begin != pep_end; ++pep_begin)
868
 
                {
869
 
      if ( !pep_begin->getHits().empty() )
870
 
                        {
871
 
                                if (!pep_begin->metaValueExists("RT") || 
872
 
                                                !pep_begin->metaValueExists("MZ"))
873
 
                                {
874
 
                                        // TODO: show error message here
875
 
                                        continue;
876
 
                                }
877
 
                                DoubleReal rt = (DoubleReal) pep_begin->getMetaValue("RT");
878
 
                                if (rt < visible_area_.minPosition()[1] || rt > visible_area_.maxPosition()[1]) continue;                               
879
 
                                DoubleReal mz = getIdentificationMZ_(layer_index, *pep_begin);
880
 
                                if (mz < visible_area_.minPosition()[0] || mz > visible_area_.maxPosition()[0]) continue;
881
 
                                
882
 
                                //draw dot
883
 
                                QPoint pos;
884
 
                                dataToWidget_(mz, rt, pos);
885
 
                                painter.drawLine(pos.x(), pos.y() - 1.0, pos.x(), pos.y() + 1.0);
886
 
                                painter.drawLine(pos.x() - 1.0, pos.y(), pos.x() + 1.0, pos.y());
887
 
                                
888
 
                                //draw sequence
889
 
                                String sequence = pep_begin->getHits()[0].getSequence().toString();
890
 
                                if (pep_begin->getHits().size() > 1) sequence += "...";
891
 
                                painter.drawText(pos.x() + 10.0, pos.y() + 10.0, sequence.toQString());
892
 
                        }
893
 
                }
894
 
        }
895
 
        
896
 
        void Spectrum2DCanvas::paintConvexHulls_(const vector<ConvexHull2D>& hulls, QPainter& painter)
 
866
    }
 
867
    else if (layer.type == LayerData::DT_IDENT)
 
868
    {
 
869
      pep_begin = layer.peptides.begin();
 
870
      pep_end = layer.peptides.end();
 
871
    }
 
872
    else
 
873
      return;
 
874
 
 
875
    painter.setPen(Qt::darkRed);
 
876
 
 
877
    for (; pep_begin != pep_end; ++pep_begin)
 
878
    {
 
879
      if (!pep_begin->getHits().empty())
 
880
      {
 
881
        if (!pep_begin->metaValueExists("RT") ||
 
882
            !pep_begin->metaValueExists("MZ"))
 
883
        {
 
884
          // TODO: show error message here
 
885
          continue;
 
886
        }
 
887
        DoubleReal rt = (DoubleReal) pep_begin->getMetaValue("RT");
 
888
        if (rt < visible_area_.minPosition()[1] || rt > visible_area_.maxPosition()[1])
 
889
          continue;
 
890
        DoubleReal mz = getIdentificationMZ_(layer_index, *pep_begin);
 
891
        if (mz < visible_area_.minPosition()[0] || mz > visible_area_.maxPosition()[0])
 
892
          continue;
 
893
 
 
894
        //draw dot
 
895
        QPoint pos;
 
896
        dataToWidget_(mz, rt, pos);
 
897
        painter.drawLine(pos.x(), pos.y() - 1.0, pos.x(), pos.y() + 1.0);
 
898
        painter.drawLine(pos.x() - 1.0, pos.y(), pos.x() + 1.0, pos.y());
 
899
 
 
900
        //draw sequence
 
901
        String sequence = pep_begin->getHits()[0].getSequence().toString();
 
902
        if (pep_begin->getHits().size() > 1)
 
903
          sequence += "...";
 
904
        painter.drawText(pos.x() + 10.0, pos.y() + 10.0, sequence.toQString());
 
905
      }
 
906
    }
 
907
  }
 
908
 
 
909
  void Spectrum2DCanvas::paintConvexHulls_(const vector<ConvexHull2D> & hulls, bool hasIdentifications, QPainter & painter)
897
910
  {
898
 
                QPolygon points;
 
911
    QPolygon points;
899
912
 
900
 
                //iterate over all convex hulls
901
 
                for (Size hull=0; hull<hulls.size(); ++hull)
902
 
                {
903
 
                        ConvexHull2D::PointArrayType ch_points = hulls[hull].getHullPoints();
904
 
                        points.resize((int)ch_points.size());
905
 
                        UInt index=0;
906
 
                        QPoint pos;
907
 
                        //iterate over hull points
908
 
                        for(ConvexHull2D::PointArrayType::const_iterator it=ch_points.begin(); it!=ch_points.end(); ++it, ++index)
909
 
                        {
910
 
                                dataToWidget_(it->getY(), it->getX(),pos);
911
 
                                points.setPoint(index, pos);
912
 
                        }
 
913
    //iterate over all convex hulls
 
914
    for (Size hull = 0; hull < hulls.size(); ++hull)
 
915
    {
 
916
      ConvexHull2D::PointArrayType ch_points = hulls[hull].getHullPoints();
 
917
      points.resize((int)ch_points.size());
 
918
      UInt index = 0;
 
919
      QPoint pos;
 
920
      //iterate over hull points
 
921
      for (ConvexHull2D::PointArrayType::const_iterator it = ch_points.begin(); it != ch_points.end(); ++it, ++index)
 
922
      {
 
923
        dataToWidget_(it->getY(), it->getX(), pos);
 
924
        points.setPoint(index, pos);
 
925
      }
913
926
      painter.setPen(QPen(Qt::white, 5, Qt::DotLine, Qt::RoundCap, Qt::RoundJoin));
914
927
      painter.drawPolygon(points);
915
 
      painter.setPen(QPen(Qt::red, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
916
 
                        painter.drawPolygon(points);
917
 
                }
 
928
      painter.setPen(QPen( hasIdentifications ? Qt::green : Qt::blue, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
 
929
      painter.drawPolygon(points);
 
930
    }
918
931
  }
919
932
 
920
 
        void Spectrum2DCanvas::paintConsensusElements_(Size layer_index, QPainter& p)
921
 
        {
922
 
                const LayerData& layer = getLayer(layer_index);
 
933
  void Spectrum2DCanvas::paintConsensusElements_(Size layer_index, QPainter & p)
 
934
  {
 
935
    const LayerData & layer = getLayer(layer_index);
923
936
 
924
937
    for (ConsensusMapType::ConstIterator i = layer.getConsensusMap()->begin(); i != layer.getConsensusMap()->end(); ++i)
925
 
                {
926
 
                        paintConsensusElement_(layer_index, *i, p, true);
927
 
                }
928
 
        }
929
 
 
930
 
        void Spectrum2DCanvas::paintConsensusElement_(Size layer_index, const ConsensusFeature& cf, QPainter& p, bool use_buffer)
931
 
        {
932
 
                Int image_width = buffer_.width();
933
 
                Int image_height = buffer_.height();
934
 
 
935
 
                const LayerData& layer = getLayer(layer_index);
936
 
 
937
 
                if ( isConsensusFeatureVisible_(cf, layer_index) && layer.filters.passes(cf))
938
 
                {
939
 
                        //calculate position of consensus feature (centroid)
940
 
                        QPoint consensus_pos;
941
 
                        dataToWidget_(cf.getMZ(), cf.getRT(),consensus_pos);
942
 
                        //iterate over elements
943
 
                        for (ConsensusFeature::HandleSetType::const_iterator element=cf.begin(); element!=cf.end(); ++element)
944
 
                        {
945
 
                                //calculate position of consensus element
946
 
                                QPoint pos;
947
 
                                dataToWidget_(element->getMZ(), element->getRT(),pos);
948
 
                                //paint line
949
 
                                p.drawLine(consensus_pos,pos);
950
 
                                //paint point
951
 
                                if (pos.x()>0 && pos.y()>0 && pos.x()<image_width-1 && pos.y()<image_height-1)
952
 
                                {
953
 
                                        // use buffer only when not highlighting
954
 
                                        if (use_buffer)
955
 
                                        {
956
 
                                                buffer_.setPixel(pos.x()   ,pos.y()   ,Qt::black);
957
 
                                                buffer_.setPixel(pos.x()-1 ,pos.y()   ,Qt::black);
958
 
                                                buffer_.setPixel(pos.x()+1 ,pos.y()   ,Qt::black);
959
 
                                                buffer_.setPixel(pos.x()   ,pos.y()-1 ,Qt::black);
960
 
                                                buffer_.setPixel(pos.x()   ,pos.y()+1 ,Qt::black);
961
 
                                        }
962
 
                                        else
963
 
                                        {
964
 
                                                p.drawPoint(pos.x()   ,pos.y());
965
 
                                                p.drawPoint(pos.x()-1 ,pos.y());
966
 
                                                p.drawPoint(pos.x()+1 ,pos.y());
967
 
                                                p.drawPoint(pos.x()   ,pos.y()-1);
968
 
                                                p.drawPoint(pos.x()   ,pos.y()+1);
969
 
                                        }
970
 
                                }
971
 
                        }
972
 
                }
973
 
 
974
 
        }
975
 
 
976
 
        bool Spectrum2DCanvas::isConsensusFeatureVisible_(const ConsensusFeature& ce, Size layer_index)
977
 
        {
978
 
                // check the centroid first
979
 
                if (ce.getRT() >= visible_area_.minPosition()[1] &&
980
 
                                ce.getRT() <= visible_area_.maxPosition()[1] &&
981
 
                                ce.getMZ() >= visible_area_.minPosition()[0] &&
982
 
                                ce.getMZ() <= visible_area_.maxPosition()[0])
983
 
                {
984
 
                        return true;
985
 
                }
986
 
 
987
 
                // if element-flag is set, check if any of the consensus elements is visible
988
 
                if (getLayerFlag(layer_index, LayerData::C_ELEMENTS))
989
 
                {
990
 
                        ConsensusFeature::HandleSetType::const_iterator element=ce.getFeatures().begin();
991
 
                        for (; element != ce.getFeatures().end(); ++element)
992
 
                        {
993
 
                                if (element->getRT() >= visible_area_.minPosition()[1] &&
994
 
                                                element->getRT() <= visible_area_.maxPosition()[1] &&
995
 
                                                element->getMZ() >= visible_area_.minPosition()[0] &&
996
 
                                                element->getMZ() <= visible_area_.maxPosition()[0])
997
 
                                {
998
 
                                        return true;
999
 
                                }
1000
 
                        }
1001
 
                }
1002
 
                return false;
1003
 
        }
1004
 
 
1005
 
        void Spectrum2DCanvas::intensityModeChange_()
1006
 
        {
 
938
    {
 
939
      paintConsensusElement_(layer_index, *i, p, true);
 
940
    }
 
941
  }
 
942
 
 
943
  void Spectrum2DCanvas::paintConsensusElement_(Size layer_index, const ConsensusFeature & cf, QPainter & p, bool use_buffer)
 
944
  {
 
945
    Int image_width = buffer_.width();
 
946
    Int image_height = buffer_.height();
 
947
 
 
948
    const LayerData & layer = getLayer(layer_index);
 
949
 
 
950
    if (isConsensusFeatureVisible_(cf, layer_index) && layer.filters.passes(cf))
 
951
    {
 
952
      //calculate position of consensus feature (centroid)
 
953
      QPoint consensus_pos;
 
954
      dataToWidget_(cf.getMZ(), cf.getRT(), consensus_pos);
 
955
      //iterate over elements
 
956
      for (ConsensusFeature::HandleSetType::const_iterator element = cf.begin(); element != cf.end(); ++element)
 
957
      {
 
958
        //calculate position of consensus element
 
959
        QPoint pos;
 
960
        dataToWidget_(element->getMZ(), element->getRT(), pos);
 
961
        //paint line
 
962
        p.drawLine(consensus_pos, pos);
 
963
        //paint point
 
964
        if (pos.x() > 0 && pos.y() > 0 && pos.x() < image_width - 1 && pos.y() < image_height - 1)
 
965
        {
 
966
          // use buffer only when not highlighting
 
967
          if (use_buffer)
 
968
          {
 
969
            buffer_.setPixel(pos.x(), pos.y(), Qt::black);
 
970
            buffer_.setPixel(pos.x() - 1, pos.y(), Qt::black);
 
971
            buffer_.setPixel(pos.x() + 1, pos.y(), Qt::black);
 
972
            buffer_.setPixel(pos.x(), pos.y() - 1, Qt::black);
 
973
            buffer_.setPixel(pos.x(), pos.y() + 1, Qt::black);
 
974
          }
 
975
          else
 
976
          {
 
977
            p.drawPoint(pos.x(), pos.y());
 
978
            p.drawPoint(pos.x() - 1, pos.y());
 
979
            p.drawPoint(pos.x() + 1, pos.y());
 
980
            p.drawPoint(pos.x(), pos.y() - 1);
 
981
            p.drawPoint(pos.x(), pos.y() + 1);
 
982
          }
 
983
        }
 
984
      }
 
985
    }
 
986
 
 
987
  }
 
988
 
 
989
  bool Spectrum2DCanvas::isConsensusFeatureVisible_(const ConsensusFeature & ce, Size layer_index)
 
990
  {
 
991
    // check the centroid first
 
992
    if (ce.getRT() >= visible_area_.minPosition()[1] &&
 
993
        ce.getRT() <= visible_area_.maxPosition()[1] &&
 
994
        ce.getMZ() >= visible_area_.minPosition()[0] &&
 
995
        ce.getMZ() <= visible_area_.maxPosition()[0])
 
996
    {
 
997
      return true;
 
998
    }
 
999
 
 
1000
    // if element-flag is set, check if any of the consensus elements is visible
 
1001
    if (getLayerFlag(layer_index, LayerData::C_ELEMENTS))
 
1002
    {
 
1003
      ConsensusFeature::HandleSetType::const_iterator element = ce.getFeatures().begin();
 
1004
      for (; element != ce.getFeatures().end(); ++element)
 
1005
      {
 
1006
        if (element->getRT() >= visible_area_.minPosition()[1] &&
 
1007
            element->getRT() <= visible_area_.maxPosition()[1] &&
 
1008
            element->getMZ() >= visible_area_.minPosition()[0] &&
 
1009
            element->getMZ() <= visible_area_.maxPosition()[0])
 
1010
        {
 
1011
          return true;
 
1012
        }
 
1013
      }
 
1014
    }
 
1015
    return false;
 
1016
  }
 
1017
 
 
1018
  void Spectrum2DCanvas::intensityModeChange_()
 
1019
  {
1007
1020
    for (Size i = 0; i < layers_.size(); ++i)
1008
 
                {
 
1021
    {
1009
1022
      recalculateDotGradient_(i);
1010
 
                }
1011
 
                SpectrumCanvas::intensityModeChange_();
1012
 
        }
 
1023
    }
 
1024
    SpectrumCanvas::intensityModeChange_();
 
1025
  }
1013
1026
 
1014
1027
  void Spectrum2DCanvas::recalculateDotGradient_(Size layer)
1015
 
        {
 
1028
  {
1016
1029
    getLayer_(layer).gradient.fromString(getLayer_(layer).param.getValue("dot:gradient"));
1017
1030
    if (intensity_mode_ == IM_LOG)
1018
1031
    {
1019
 
      DoubleReal min_intensity = getMinIntensity(layer);    
 
1032
      DoubleReal min_intensity = getMinIntensity(layer);
1020
1033
      getLayer_(layer).gradient.activatePrecalculationMode(std::log(min_intensity + 1), std::log(overall_data_range_.maxPosition()[2]) + 1, param_.getValue("interpolation_steps"));
1021
 
    } else
 
1034
    }
 
1035
    else
1022
1036
    {
1023
1037
      getLayer_(layer).gradient.activatePrecalculationMode(getMinIntensity(layer), overall_data_range_.maxPosition()[2], param_.getValue("interpolation_steps"));
1024
1038
    }
1025
 
        }
 
1039
  }
1026
1040
 
1027
1041
  void Spectrum2DCanvas::recalculateCurrentLayerDotGradient()
1028
1042
  {
1029
1043
    recalculateDotGradient_(current_layer_);
1030
1044
  }
1031
1045
 
1032
 
        void Spectrum2DCanvas::updateProjections()
1033
 
        {
1034
 
                //find the last (visible) peak layers
1035
 
                Size layer_count = 0;
1036
 
                Size last_layer = 0;
1037
 
                Size visible_layer_count = 0;
1038
 
                Size visible_last_layer = 0;
1039
 
                for (Size i=0; i<getLayerCount(); ++i)
1040
 
                {
1041
 
                        if (getLayer(i).type==LayerData::DT_PEAK)
1042
 
                        {
1043
 
                                layer_count++;
1044
 
                                last_layer=i;
1045
 
                                
1046
 
                                if (getLayer(i).visible)
1047
 
                                {
1048
 
                                        visible_layer_count++;
1049
 
                                        visible_last_layer=i;
1050
 
                                }
1051
 
                        }
 
1046
  void Spectrum2DCanvas::updateProjections()
 
1047
  {
 
1048
    //find the last (visible) peak layers
 
1049
    Size layer_count = 0;
 
1050
    Size last_layer = 0;
 
1051
    Size visible_layer_count = 0;
 
1052
    Size visible_last_layer = 0;
 
1053
    for (Size i = 0; i < getLayerCount(); ++i)
 
1054
    {
 
1055
      if (getLayer(i).type == LayerData::DT_PEAK)
 
1056
      {
 
1057
        layer_count++;
 
1058
        last_layer = i;
 
1059
 
 
1060
        if (getLayer(i).visible)
 
1061
        {
 
1062
          visible_layer_count++;
 
1063
          visible_last_layer = i;
 
1064
        }
 
1065
      }
1052
1066
      if (getLayer(i).type == LayerData::DT_CHROMATOGRAM)
1053
 
                        {
1054
 
                                //TODO CHROM
1055
 
                        }
1056
 
                }
1057
 
 
1058
 
                //try to find the right layer to project
1059
 
                const LayerData* layer = 0;             
1060
 
                //first choice: current layer
1061
 
                if (layer_count!=0 && getCurrentLayer().type==LayerData::DT_PEAK)
1062
 
                {
1063
 
                        layer = &(getCurrentLayer());
1064
 
                }
1065
 
                //second choice: the only peak layer
1066
 
                else if (layer_count==1)
1067
 
                {
1068
 
                        layer = &(getLayer(last_layer));
1069
 
                }
1070
 
                //third choice: the only visible peak layer
1071
 
                else if (visible_layer_count==1)
1072
 
                {
1073
 
                        layer = &(getLayer(visible_last_layer));
1074
 
                }
1075
 
                //no layer with peaks: disable projections
1076
 
                else
1077
 
                {
1078
 
                        emit toggleProjections();
1079
 
                        return;
1080
 
                }
1081
 
                
1082
 
                //create projection data
1083
 
                map<float, float> rt;
1084
 
                map<int, float> mzint;
1085
 
                map<int,int> mzcount;
1086
 
                map<int,float> mzsum;
1087
 
 
1088
 
                UInt peak_count = 0;
1089
 
                DoubleReal intensity_max = 0.0;
1090
 
                DoubleReal intensity_sum = 0.0;
1091
 
 
1092
 
          // divide visible range into 100 bins (much faster than using a constant, e.g. 0.05, leading to many peaks for large maps without more information)
 
1067
      {
 
1068
        //TODO CHROM
 
1069
      }
 
1070
    }
 
1071
 
 
1072
    //try to find the right layer to project
 
1073
    const LayerData * layer = 0;
 
1074
    //first choice: current layer
 
1075
    if (layer_count != 0 && getCurrentLayer().type == LayerData::DT_PEAK)
 
1076
    {
 
1077
      layer = &(getCurrentLayer());
 
1078
    }
 
1079
    //second choice: the only peak layer
 
1080
    else if (layer_count == 1)
 
1081
    {
 
1082
      layer = &(getLayer(last_layer));
 
1083
    }
 
1084
    //third choice: the only visible peak layer
 
1085
    else if (visible_layer_count == 1)
 
1086
    {
 
1087
      layer = &(getLayer(visible_last_layer));
 
1088
    }
 
1089
    //no layer with peaks: disable projections
 
1090
    else
 
1091
    {
 
1092
      emit toggleProjections();
 
1093
      return;
 
1094
    }
 
1095
 
 
1096
    //create projection data
 
1097
    map<float, float> rt;
 
1098
    map<int, float> mzint;
 
1099
    map<int, int> mzcount;
 
1100
    map<int, float> mzsum;
 
1101
 
 
1102
    UInt peak_count = 0;
 
1103
    DoubleReal intensity_max = 0.0;
 
1104
    DoubleReal intensity_sum = 0.0;
 
1105
 
 
1106
    // divide visible range into 100 bins (much faster than using a constant, e.g. 0.05, leading to many peaks for large maps without more information)
1093
1107
    float range = visible_area_.maxPosition()[0] - visible_area_.minPosition()[0];
1094
 
    float mult = 100.0f/(range<=0 ? 1 : range); 
 
1108
    float mult = 100.0f / (range <= 0 ? 1 : range);
1095
1109
 
1096
 
    for (ExperimentType::ConstAreaIterator i = layer->getPeakData()->areaBeginConst(visible_area_.minPosition()[1],visible_area_.maxPosition()[1],visible_area_.minPosition()[0],visible_area_.maxPosition()[0]);
 
1110
    for (ExperimentType::ConstAreaIterator i = layer->getPeakData()->areaBeginConst(visible_area_.minPosition()[1], visible_area_.maxPosition()[1], visible_area_.minPosition()[0], visible_area_.maxPosition()[0]);
1097
1111
         i != layer->getPeakData()->areaEndConst();
1098
1112
         ++i)
1099
 
                {
1100
 
                        PeakIndex pi = i.getPeakIndex();
1101
 
      if (layer->filters.passes((*layer->getPeakData())[pi.spectrum],pi.peak))
1102
 
                        {
1103
 
                                //sum
1104
 
                                ++peak_count;
1105
 
                                intensity_sum += i->getIntensity();
1106
 
                        mzint[int(i->getMZ()*mult)] += i->getIntensity();
1107
 
                                mzcount[int(i->getMZ()*mult)]++;
1108
 
                                mzsum[int(i->getMZ()*mult)] += i->getMZ();
1109
 
 
1110
 
                                rt[i.getRT()] += i->getIntensity();
1111
 
                                //max
1112
 
                                intensity_max = max(intensity_max,(DoubleReal)(i->getIntensity()));
1113
 
                        }
1114
 
                }
1115
 
 
1116
 
                // write to spectra
1117
 
                projection_mz_[0].resize(mzint.size()+2);
1118
 
                projection_mz_[0][0].setMZ(visible_area_.minPosition()[0]);
1119
 
                projection_mz_[0][0].setIntensity(0.0);
1120
 
                projection_mz_[0][1].setMZ(visible_area_.maxPosition()[0]);
1121
 
                projection_mz_[0][1].setIntensity(0.0);
1122
 
                projection_rt_[0].resize(rt.size()+2);
1123
 
                projection_rt_[0][0].setMZ(visible_area_.minPosition()[1]);
1124
 
                projection_rt_[0][0].setIntensity(0.0);
1125
 
                projection_rt_[0][1].setMZ(visible_area_.maxPosition()[1]);
1126
 
                projection_rt_[0][1].setIntensity(0.0);
1127
 
 
1128
 
                Size i = 2;
1129
 
                map<int,float>::iterator intit = mzint.begin();
1130
 
                map<int,int>::iterator cit = mzcount.begin();
1131
 
 
1132
 
                for (map<int, float>::iterator it = mzsum.begin(); it != mzsum.end(); ++it)
1133
 
                {
1134
 
                        projection_mz_[0][i].setMZ(it->second/cit->second);
1135
 
                        projection_mz_[0][i].setIntensity(intit->second);
 
1113
    {
 
1114
      PeakIndex pi = i.getPeakIndex();
 
1115
      if (layer->filters.passes((*layer->getPeakData())[pi.spectrum], pi.peak))
 
1116
      {
 
1117
        //sum
 
1118
        ++peak_count;
 
1119
        intensity_sum += i->getIntensity();
 
1120
        mzint[int(i->getMZ() * mult)] += i->getIntensity();
 
1121
        mzcount[int(i->getMZ() * mult)]++;
 
1122
        mzsum[int(i->getMZ() * mult)] += i->getMZ();
 
1123
 
 
1124
        rt[i.getRT()] += i->getIntensity();
 
1125
        //max
 
1126
        intensity_max = max(intensity_max, (DoubleReal)(i->getIntensity()));
 
1127
      }
 
1128
    }
 
1129
 
 
1130
    // write to spectra
 
1131
    projection_mz_[0].resize(mzint.size() + 2);
 
1132
    projection_mz_[0][0].setMZ(visible_area_.minPosition()[0]);
 
1133
    projection_mz_[0][0].setIntensity(0.0);
 
1134
    projection_mz_[0][1].setMZ(visible_area_.maxPosition()[0]);
 
1135
    projection_mz_[0][1].setIntensity(0.0);
 
1136
    projection_rt_[0].resize(rt.size() + 2);
 
1137
    projection_rt_[0][0].setMZ(visible_area_.minPosition()[1]);
 
1138
    projection_rt_[0][0].setIntensity(0.0);
 
1139
    projection_rt_[0][1].setMZ(visible_area_.maxPosition()[1]);
 
1140
    projection_rt_[0][1].setIntensity(0.0);
 
1141
 
 
1142
    Size i = 2;
 
1143
    map<int, float>::iterator intit = mzint.begin();
 
1144
    map<int, int>::iterator cit = mzcount.begin();
 
1145
 
 
1146
    for (map<int, float>::iterator it = mzsum.begin(); it != mzsum.end(); ++it)
 
1147
    {
 
1148
      projection_mz_[0][i].setMZ(it->second / cit->second);
 
1149
      projection_mz_[0][i].setIntensity(intit->second);
1136
1150
      ++intit;
1137
1151
      ++cit;
1138
 
                        ++i;
1139
 
                }
 
1152
      ++i;
 
1153
    }
1140
1154
 
1141
 
                i = 2;
1142
 
                for (map<float, float>::iterator it = rt.begin(); it != rt.end(); ++it)
1143
 
                {
1144
 
                        projection_rt_[0][i].setMZ(it->first);
1145
 
                        projection_rt_[0][i].setIntensity(it->second);
1146
 
                        ++i;
1147
 
                }
 
1155
    i = 2;
 
1156
    for (map<float, float>::iterator it = rt.begin(); it != rt.end(); ++it)
 
1157
    {
 
1158
      projection_rt_[0][i].setMZ(it->first);
 
1159
      projection_rt_[0][i].setIntensity(it->second);
 
1160
      ++i;
 
1161
    }
1148
1162
 
1149
1163
    ExperimentSharedPtrType projection_mz_sptr(new ExperimentType(projection_mz_));
1150
1164
    ExperimentSharedPtrType projection_rt_sptr(new ExperimentType(projection_rt_));
1151
1165
 
1152
 
                if (isMzToXAxis())
1153
 
                {
 
1166
    if (isMzToXAxis())
 
1167
    {
1154
1168
      emit showProjectionHorizontal(projection_mz_sptr, Spectrum1DCanvas::DM_PEAKS);
1155
1169
      emit showProjectionVertical(projection_rt_sptr, Spectrum1DCanvas::DM_CONNECTEDLINES);
1156
 
                }
1157
 
                else
1158
 
                {
 
1170
    }
 
1171
    else
 
1172
    {
1159
1173
      emit showProjectionHorizontal(projection_rt_sptr, Spectrum1DCanvas::DM_CONNECTEDLINES);
1160
1174
      emit showProjectionVertical(projection_mz_sptr, Spectrum1DCanvas::DM_PEAKS);
1161
 
                }
1162
 
                showProjectionInfo(peak_count, intensity_sum, intensity_max);
1163
 
        }
1164
 
 
1165
 
        bool Spectrum2DCanvas::finishAdding_()
1166
 
        {
1167
 
                //unselect all peaks
1168
 
                selected_peak_.clear();
1169
 
                measurement_start_.clear();
1170
 
 
1171
 
                current_layer_ = getLayerCount()-1;
1172
 
 
1173
 
                if (layers_.back().type==LayerData::DT_PEAK) //peak data
1174
 
                {
1175
 
                        update_buffer_ = true;
1176
 
                        //Abort if no data points are contained
1177
 
      if (currentPeakData_()->size()==0 || currentPeakData_()->getSize()==0)
1178
 
                        {
1179
 
                                layers_.resize(getLayerCount()-1);
1180
 
        if (current_layer_!=0)
 
1175
    }
 
1176
    showProjectionInfo(peak_count, intensity_sum, intensity_max);
 
1177
  }
 
1178
 
 
1179
  bool Spectrum2DCanvas::finishAdding_()
 
1180
  {
 
1181
    //unselect all peaks
 
1182
    selected_peak_.clear();
 
1183
    measurement_start_.clear();
 
1184
 
 
1185
    current_layer_ = getLayerCount() - 1;
 
1186
 
 
1187
    if (layers_.back().type == LayerData::DT_PEAK)   //peak data
 
1188
    {
 
1189
      update_buffer_ = true;
 
1190
      //Abort if no data points are contained
 
1191
      if ((currentPeakData_()->size() == 0 || currentPeakData_()->getSize() == 0) && currentPeakData_()->getDataRange().isEmpty())
 
1192
      {
 
1193
        layers_.resize(getLayerCount() - 1);
 
1194
        if (current_layer_ != 0)
1181
1195
        {
1182
 
          current_layer_ = current_layer_-1;
 
1196
          current_layer_ = current_layer_ - 1;
1183
1197
        }
1184
 
                                QMessageBox::critical(this,"Error","Cannot add a dataset that contains no survey scans. Aborting!");
1185
 
                                return false;
1186
 
                        }
1187
 
                }
1188
 
                else if (layers_.back().type==LayerData::DT_FEATURE)//feature data
1189
 
                {
 
1198
        QMessageBox::critical(this, "Error", "Cannot add a dataset that contains no survey scans. Aborting!");
 
1199
        return false;
 
1200
      }
 
1201
      if ((currentPeakData_()->getSize() == 0) && (!currentPeakData_()->getDataRange().isEmpty()))
 
1202
      {
 
1203
        setLayerFlag(LayerData::P_PRECURSORS, true); // show precursors if no MS1 data is contained
 
1204
      }
 
1205
    }
 
1206
    else if (layers_.back().type == LayerData::DT_FEATURE)  //feature data
 
1207
    {
1190
1208
      getCurrentLayer_().getFeatureMap()->updateRanges();
1191
 
                        setLayerFlag(LayerData::F_HULL,true);
 
1209
      setLayerFlag(LayerData::F_HULL, true);
1192
1210
 
1193
 
                        //Abort if no data points are contained
1194
 
      if (getCurrentLayer_().getFeatureMap()->size()==0)
1195
 
                        {
1196
 
                                layers_.resize(getLayerCount()-1);
1197
 
        if (current_layer_!=0)
 
1211
      //Abort if no data points are contained
 
1212
      if (getCurrentLayer_().getFeatureMap()->size() == 0)
 
1213
      {
 
1214
        layers_.resize(getLayerCount() - 1);
 
1215
        if (current_layer_ != 0)
1198
1216
        {
1199
 
          current_layer_ = current_layer_-1;
 
1217
          current_layer_ = current_layer_ - 1;
1200
1218
        }
1201
 
                                QMessageBox::critical(this,"Error","Cannot add an empty dataset. Aborting!");
1202
 
                                return false;
1203
 
                        }
1204
 
                }
1205
 
                else if (layers_.back().type==LayerData::DT_CONSENSUS)//consensus feature data
1206
 
                {
 
1219
        QMessageBox::critical(this, "Error", "Cannot add an empty dataset. Aborting!");
 
1220
        return false;
 
1221
      }
 
1222
    }
 
1223
    else if (layers_.back().type == LayerData::DT_CONSENSUS)  //consensus feature data
 
1224
    {
1207
1225
      getCurrentLayer_().getConsensusMap()->updateRanges();
1208
1226
 
1209
 
                        //Abort if no data points are contained
1210
 
      if (getCurrentLayer_().getConsensusMap()->size()==0)
1211
 
                        {
1212
 
                                layers_.resize(getLayerCount()-1);
1213
 
                                if (current_layer_!=0) current_layer_ = current_layer_-1;
1214
 
                                QMessageBox::critical(this,"Error","Cannot add an empty dataset. Aborting!");
1215
 
                                return false;
1216
 
                        }
1217
 
                }
1218
 
                else if (layers_.back().type==LayerData::DT_CHROMATOGRAM)//chromatogram data
1219
 
                {
 
1227
      //Abort if no data points are contained
 
1228
      if (getCurrentLayer_().getConsensusMap()->size() == 0)
 
1229
      {
 
1230
        layers_.resize(getLayerCount() - 1);
 
1231
        if (current_layer_ != 0)
 
1232
          current_layer_ = current_layer_ - 1;
 
1233
        QMessageBox::critical(this, "Error", "Cannot add an empty dataset. Aborting!");
 
1234
        return false;
 
1235
      }
 
1236
    }
 
1237
    else if (layers_.back().type == LayerData::DT_CHROMATOGRAM)  //chromatogram data
 
1238
    {
1220
1239
 
1221
 
                        //TODO CHROM 
 
1240
      //TODO CHROM
1222
1241
      currentPeakData_()->sortChromatograms(true);
1223
1242
      currentPeakData_()->updateRanges(1);
1224
1243
 
1225
 
                        update_buffer_ = true;
 
1244
      update_buffer_ = true;
1226
1245
 
1227
 
                        //Abort if no data points are contained
 
1246
      //Abort if no data points are contained
1228
1247
      if (currentPeakData_()->getChromatograms().empty())
1229
 
                        {
1230
 
                                layers_.resize(getLayerCount()-1);
1231
 
                                if (current_layer_!=0) current_layer_ = current_layer_-1;
1232
 
                                QMessageBox::critical(this,"Error","Cannot add a dataset that contains no chromatograms. Aborting!");
1233
 
                                return false;
1234
 
                        }
1235
 
                }
1236
 
                else if (layers_.back().type==LayerData::DT_IDENT) // identification data
1237
 
                {
1238
 
                        //Abort if no data points are contained
1239
 
                        if (getCurrentLayer_().peptides.empty())
1240
 
                        {
1241
 
                                layers_.resize(getLayerCount()-1);
1242
 
                                if (current_layer_!=0) current_layer_ = current_layer_-1;
1243
 
                                QMessageBox::critical(this,"Error","Cannot add an empty dataset. Aborting!");
1244
 
                                return false;
1245
 
                        }
1246
 
                }
1247
 
 
1248
 
                //Warn if negative intensities are contained
1249
 
                if (getMinIntensity(current_layer_)<0.0)
1250
 
                {
1251
 
                        QMessageBox::warning(this,"Warning","This dataset contains negative intensities. Use it at your own risk!");
1252
 
                }
1253
 
 
1254
 
                //overall values update
1255
 
                recalculateRanges_(0,1,2);
 
1248
      {
 
1249
        layers_.resize(getLayerCount() - 1);
 
1250
        if (current_layer_ != 0)
 
1251
          current_layer_ = current_layer_ - 1;
 
1252
        QMessageBox::critical(this, "Error", "Cannot add a dataset that contains no chromatograms. Aborting!");
 
1253
        return false;
 
1254
      }
 
1255
    }
 
1256
    else if (layers_.back().type == LayerData::DT_IDENT)   // identification data
 
1257
    {
 
1258
      //Abort if no data points are contained
 
1259
      if (getCurrentLayer_().peptides.empty())
 
1260
      {
 
1261
        layers_.resize(getLayerCount() - 1);
 
1262
        if (current_layer_ != 0)
 
1263
          current_layer_ = current_layer_ - 1;
 
1264
        QMessageBox::critical(this, "Error", "Cannot add an empty dataset. Aborting!");
 
1265
        return false;
 
1266
      }
 
1267
    }
 
1268
 
 
1269
    //Warn if negative intensities are contained
 
1270
    if (getMinIntensity(current_layer_) < 0.0)
 
1271
    {
 
1272
      QMessageBox::warning(this, "Warning", "This dataset contains negative intensities. Use it at your own risk!");
 
1273
    }
 
1274
 
 
1275
    //overall values update
 
1276
    recalculateRanges_(0, 1, 2);
1256
1277
    if (layers_.size() == 1)
1257
1278
    {
1258
1279
      resetZoom(false); //no repaint as this is done in intensityModeChange_() anyway
1259
1280
    }
1260
1281
 
1261
 
                if (getLayerCount()==2)
1262
 
                {
1263
 
                        setIntensityMode(IM_PERCENTAGE);
1264
 
                }
1265
 
                intensityModeChange_();
1266
 
 
1267
 
                emit layerActivated(this);
1268
 
 
1269
 
                return true;
1270
 
        }
1271
 
 
1272
 
        void Spectrum2DCanvas::removeLayer(Size layer_index )
1273
 
        {
1274
 
                if (layer_index >= getLayerCount())
1275
 
                {
1276
 
                        return;
1277
 
                }
 
1282
    if (getLayerCount() == 2)
 
1283
    {
 
1284
      setIntensityMode(IM_PERCENTAGE);
 
1285
    }
 
1286
    intensityModeChange_();
 
1287
 
 
1288
    emit layerActivated(this);
 
1289
 
 
1290
    return true;
 
1291
  }
 
1292
 
 
1293
  void Spectrum2DCanvas::removeLayer(Size layer_index)
 
1294
  {
 
1295
    if (layer_index >= getLayerCount())
 
1296
    {
 
1297
      return;
 
1298
    }
1278
1299
 
1279
1300
    // remove the data
1280
 
                layers_.erase(layers_.begin()+layer_index);
 
1301
    layers_.erase(layers_.begin() + layer_index);
1281
1302
 
1282
1303
    // update visible area and boundaries
1283
1304
    DRange<3> old_data_range = overall_data_range_;
1284
 
                recalculateRanges_(0,1,2);
 
1305
    recalculateRanges_(0, 1, 2);
1285
1306
 
1286
1307
    // only reset zoom if data range has been changed
1287
1308
    if (old_data_range != overall_data_range_)
1292
1313
    // update current layer if it became invalid
1293
1314
    if (current_layer_ != 0 && current_layer_ >= getLayerCount())
1294
1315
    {
1295
 
      current_layer_ = getLayerCount()-1;
 
1316
      current_layer_ = getLayerCount() - 1;
1296
1317
    }
1297
1318
 
1298
 
                if (layers_.empty())
1299
 
                {
1300
 
                        overall_data_range_ = DRange<3>::empty;
1301
 
                        update_buffer_ = true;
1302
 
                        update_(__PRETTY_FUNCTION__);
1303
 
                        return;
1304
 
                }
 
1319
    if (layers_.empty())
 
1320
    {
 
1321
      overall_data_range_ = DRange<3>::empty;
 
1322
      update_buffer_ = true;
 
1323
      update_(__PRETTY_FUNCTION__);
 
1324
      return;
 
1325
    }
1305
1326
 
1306
1327
    // unselect all peaks
1307
1328
    selected_peak_.clear();
1308
1329
    measurement_start_.clear();
1309
1330
 
1310
 
                intensityModeChange_();
1311
 
 
1312
 
                emit layerActivated(this);
1313
 
        }
1314
 
 
1315
 
        //change the current layer
1316
 
        void Spectrum2DCanvas::activateLayer(Size layer_index)
1317
 
        {
 
1331
    intensityModeChange_();
 
1332
 
 
1333
    emit layerActivated(this);
 
1334
  }
 
1335
 
 
1336
  //change the current layer
 
1337
  void Spectrum2DCanvas::activateLayer(Size layer_index)
 
1338
  {
1318
1339
    if (layer_index >= getLayerCount() || layer_index == current_layer_)
1319
 
                {
 
1340
    {
1320
1341
      return;
1321
 
                }
 
1342
    }
1322
1343
 
1323
1344
    // unselect all peaks
1324
 
                selected_peak_.clear();
1325
 
                measurement_start_.clear();
1326
 
 
1327
 
                current_layer_ = layer_index;
1328
 
                emit layerActivated(this);
1329
 
 
1330
 
                update_(__PRETTY_FUNCTION__);
1331
 
        }
1332
 
 
1333
 
        void Spectrum2DCanvas::recalculateSnapFactor_()
1334
 
        {
1335
 
                snap_factors_ = vector<DoubleReal>(getLayerCount(),1.0);
1336
 
 
1337
 
                if (intensity_mode_ == IM_SNAP)
1338
 
                {
1339
 
                        for (Size i=0; i<getLayerCount(); i++)
1340
 
                        {
1341
 
                                if (getLayer(i).visible)
1342
 
                                {
1343
 
                                        DoubleReal local_max  = -numeric_limits<DoubleReal>::max();
1344
 
                                        if (getLayer(i).type==LayerData::DT_PEAK)
1345
 
                                        {
1346
 
                                                for (ExperimentType::ConstAreaIterator it = getLayer(i).getPeakData()->areaBeginConst(visible_area_.minPosition()[1],visible_area_.maxPosition()[1],visible_area_.minPosition()[0],visible_area_.maxPosition()[0]);
1347
 
                                                                 it != getLayer(i).getPeakData()->areaEndConst();
1348
 
                                                                 ++it)
1349
 
                                                {
1350
 
                                                        PeakIndex pi = it.getPeakIndex();
1351
 
                                                        if (it->getIntensity() > local_max && getLayer(i).filters.passes((*getLayer(i).getPeakData())[pi.spectrum],pi.peak))
1352
 
                                                        {
1353
 
                                                                local_max = it->getIntensity();
1354
 
                                                        }
1355
 
                                                }
1356
 
                                        }
1357
 
                                        else if (getLayer(i).type==LayerData::DT_FEATURE) // features
1358
 
                                        {
 
1345
    selected_peak_.clear();
 
1346
    measurement_start_.clear();
 
1347
 
 
1348
    current_layer_ = layer_index;
 
1349
    emit layerActivated(this);
 
1350
 
 
1351
    update_(__PRETTY_FUNCTION__);
 
1352
  }
 
1353
 
 
1354
  void Spectrum2DCanvas::recalculateSnapFactor_()
 
1355
  {
 
1356
    snap_factors_ = vector<DoubleReal>(getLayerCount(), 1.0);
 
1357
 
 
1358
    if (intensity_mode_ == IM_SNAP)
 
1359
    {
 
1360
      for (Size i = 0; i < getLayerCount(); i++)
 
1361
      {
 
1362
        if (getLayer(i).visible)
 
1363
        {
 
1364
          DoubleReal local_max  = -numeric_limits<DoubleReal>::max();
 
1365
          if (getLayer(i).type == LayerData::DT_PEAK)
 
1366
          {
 
1367
            for (ExperimentType::ConstAreaIterator it = getLayer(i).getPeakData()->areaBeginConst(visible_area_.minPosition()[1], visible_area_.maxPosition()[1], visible_area_.minPosition()[0], visible_area_.maxPosition()[0]);
 
1368
                 it != getLayer(i).getPeakData()->areaEndConst();
 
1369
                 ++it)
 
1370
            {
 
1371
              PeakIndex pi = it.getPeakIndex();
 
1372
              if (it->getIntensity() > local_max && getLayer(i).filters.passes((*getLayer(i).getPeakData())[pi.spectrum], pi.peak))
 
1373
              {
 
1374
                local_max = it->getIntensity();
 
1375
              }
 
1376
            }
 
1377
          }
 
1378
          else if (getLayer(i).type == LayerData::DT_FEATURE)         // features
 
1379
          {
1359
1380
            for (FeatureMapType::ConstIterator it = getLayer(i).getFeatureMap()->begin();
1360
1381
                 it != getLayer(i).getFeatureMap()->end();
1361
 
                                                           ++it)
1362
 
                                                {
1363
 
                                                        if ( it->getRT() >= visible_area_.minPosition()[1] &&
1364
 
                                                                         it->getRT() <= visible_area_.maxPosition()[1] &&
1365
 
                                                                         it->getMZ() >= visible_area_.minPosition()[0] &&
1366
 
                                                                         it->getMZ() <= visible_area_.maxPosition()[0] &&
1367
 
                                                                         getLayer(i).filters.passes(*it) &&
1368
 
                                                                         it->getIntensity() > local_max)
1369
 
                                                        {
1370
 
                                                                local_max = it->getIntensity();
1371
 
                                                        }
1372
 
                                                }
1373
 
                                        }
1374
 
                                        else if (getLayer(i).type==LayerData::DT_CONSENSUS) // consensus
1375
 
                                        {
 
1382
                 ++it)
 
1383
            {
 
1384
              if (it->getRT() >= visible_area_.minPosition()[1] &&
 
1385
                  it->getRT() <= visible_area_.maxPosition()[1] &&
 
1386
                  it->getMZ() >= visible_area_.minPosition()[0] &&
 
1387
                  it->getMZ() <= visible_area_.maxPosition()[0] &&
 
1388
                  getLayer(i).filters.passes(*it) &&
 
1389
                  it->getIntensity() > local_max)
 
1390
              {
 
1391
                local_max = it->getIntensity();
 
1392
              }
 
1393
            }
 
1394
          }
 
1395
          else if (getLayer(i).type == LayerData::DT_CONSENSUS)         // consensus
 
1396
          {
1376
1397
            for (ConsensusMapType::ConstIterator it = getLayer(i).getConsensusMap()->begin();
1377
1398
                 it != getLayer(i).getConsensusMap()->end();
1378
 
                                                           ++it)
1379
 
                                                {
1380
 
                                                        if ( it->getRT() >= visible_area_.minPosition()[1] &&
1381
 
                                                                         it->getRT() <= visible_area_.maxPosition()[1] &&
1382
 
                                                                         it->getMZ() >= visible_area_.minPosition()[0] &&
1383
 
                                                                         it->getMZ() <= visible_area_.maxPosition()[0] &&
1384
 
                                                                         getLayer(i).filters.passes(*it) &&
1385
 
                                                                         it->getIntensity() > local_max)
1386
 
                                                        {
1387
 
                                                                local_max = it->getIntensity();
1388
 
                                                        }
1389
 
                                                }
1390
 
                                        }
1391
 
                                        else if (getLayer(i).type==LayerData::DT_CHROMATOGRAM) // chromatogr.
1392
 
                                        {
1393
 
                                                //TODO CHROM
1394
 
                                        }
1395
 
                                        else if (getLayer(i).type==LayerData::DT_IDENT) // identifications
1396
 
                                        {
1397
 
                                                //TODO IDENT
1398
 
                                        }
1399
 
 
1400
 
                                        if (local_max>0.0)
1401
 
                                        {
1402
 
                                                snap_factors_[i] = overall_data_range_.maxPosition()[2]/local_max;
1403
 
                                        }
1404
 
                                }
1405
 
                        }
1406
 
                }
1407
 
        }
1408
 
 
1409
 
        void Spectrum2DCanvas::updateScrollbars_()
1410
 
        {
1411
 
                if (isMzToXAxis())
1412
 
                {
1413
 
                        emit updateHScrollbar(overall_data_range_.minPosition()[0],visible_area_.minPosition()[0],visible_area_.maxPosition()[0],overall_data_range_.maxPosition()[0]);
1414
 
                        emit updateVScrollbar(overall_data_range_.minPosition()[1],visible_area_.minPosition()[1],visible_area_.maxPosition()[1],overall_data_range_.maxPosition()[1]);
1415
 
                }
1416
 
                else
1417
 
                {
1418
 
                        emit updateVScrollbar(overall_data_range_.minPosition()[0],visible_area_.minPosition()[0],visible_area_.maxPosition()[0],overall_data_range_.maxPosition()[0]);
1419
 
                        emit updateHScrollbar(overall_data_range_.minPosition()[1],visible_area_.minPosition()[1],visible_area_.maxPosition()[1],overall_data_range_.maxPosition()[1]);
1420
 
                }
1421
 
        }
1422
 
 
1423
 
        void Spectrum2DCanvas::horizontalScrollBarChange(int value)
1424
 
        {
1425
 
                AreaType new_area = visible_area_;
1426
 
                if (isMzToXAxis())
1427
 
                {
1428
 
                        new_area.setMinX(value);
1429
 
                        new_area.setMaxX(value + (visible_area_.maxX() - visible_area_.minX()));
1430
 
                        //cout << __PRETTY_FUNCTION__ << endl;
1431
 
                        changeVisibleArea_(new_area);
1432
 
                }
1433
 
                else
1434
 
                {
1435
 
                        new_area.setMinY(value);
1436
 
                        new_area.setMaxY(value + (visible_area_.maxY() - visible_area_.minY()));
1437
 
                        //cout << __PRETTY_FUNCTION__ << endl;
1438
 
                        changeVisibleArea_(new_area);
1439
 
                }
1440
 
        }
1441
 
 
1442
 
        void Spectrum2DCanvas::verticalScrollBarChange(int value)
1443
 
        {
1444
 
                AreaType new_area = visible_area_;
1445
 
                if (!isMzToXAxis())
1446
 
                {
1447
 
                        new_area.setMinX(value);
1448
 
                        new_area.setMaxX(value + (visible_area_.maxX() - visible_area_.minX()));
1449
 
                        //cout << __PRETTY_FUNCTION__ << endl;
1450
 
                        changeVisibleArea_(new_area);
1451
 
                }
1452
 
                else
1453
 
                {
1454
 
                        new_area.setMinY(value);
1455
 
                        new_area.setMaxY(value + (visible_area_.maxY() - visible_area_.minY()));
1456
 
                        //cout << __PRETTY_FUNCTION__ << endl;
1457
 
                        changeVisibleArea_(new_area);
1458
 
                }
1459
 
        }
1460
 
 
1461
 
        void Spectrum2DCanvas::paintEvent(QPaintEvent* e)
1462
 
        {
1463
 
                //Only fill background if no layer is present
1464
 
                if (getLayerCount()==0)
1465
 
                {
1466
 
                        QPainter painter;
1467
 
                        painter.begin(this);
1468
 
                        painter.fillRect(0,0,this->width(),this->height(),QColor(param_.getValue("background_color").toQString()));
1469
 
                        painter.end();
1470
 
                        e->accept();
1471
 
                        return;
1472
 
                }
 
1399
                 ++it)
 
1400
            {
 
1401
              if (it->getRT() >= visible_area_.minPosition()[1] &&
 
1402
                  it->getRT() <= visible_area_.maxPosition()[1] &&
 
1403
                  it->getMZ() >= visible_area_.minPosition()[0] &&
 
1404
                  it->getMZ() <= visible_area_.maxPosition()[0] &&
 
1405
                  getLayer(i).filters.passes(*it) &&
 
1406
                  it->getIntensity() > local_max)
 
1407
              {
 
1408
                local_max = it->getIntensity();
 
1409
              }
 
1410
            }
 
1411
          }
 
1412
          else if (getLayer(i).type == LayerData::DT_CHROMATOGRAM)         // chromatogr.
 
1413
          {
 
1414
            //TODO CHROM
 
1415
          }
 
1416
          else if (getLayer(i).type == LayerData::DT_IDENT)         // identifications
 
1417
          {
 
1418
            //TODO IDENT
 
1419
          }
 
1420
 
 
1421
          if (local_max > 0.0)
 
1422
          {
 
1423
            snap_factors_[i] = overall_data_range_.maxPosition()[2] / local_max;
 
1424
          }
 
1425
        }
 
1426
      }
 
1427
    }
 
1428
  }
 
1429
 
 
1430
  void Spectrum2DCanvas::updateScrollbars_()
 
1431
  {
 
1432
    if (isMzToXAxis())
 
1433
    {
 
1434
      emit updateHScrollbar(overall_data_range_.minPosition()[0], visible_area_.minPosition()[0], visible_area_.maxPosition()[0], overall_data_range_.maxPosition()[0]);
 
1435
      emit updateVScrollbar(overall_data_range_.minPosition()[1], visible_area_.minPosition()[1], visible_area_.maxPosition()[1], overall_data_range_.maxPosition()[1]);
 
1436
    }
 
1437
    else
 
1438
    {
 
1439
      emit updateVScrollbar(overall_data_range_.minPosition()[0], visible_area_.minPosition()[0], visible_area_.maxPosition()[0], overall_data_range_.maxPosition()[0]);
 
1440
      emit updateHScrollbar(overall_data_range_.minPosition()[1], visible_area_.minPosition()[1], visible_area_.maxPosition()[1], overall_data_range_.maxPosition()[1]);
 
1441
    }
 
1442
  }
 
1443
 
 
1444
  void Spectrum2DCanvas::horizontalScrollBarChange(int value)
 
1445
  {
 
1446
    AreaType new_area = visible_area_;
 
1447
    if (isMzToXAxis())
 
1448
    {
 
1449
      new_area.setMinX(value);
 
1450
      new_area.setMaxX(value + (visible_area_.maxX() - visible_area_.minX()));
 
1451
      //cout << __PRETTY_FUNCTION__ << endl;
 
1452
      changeVisibleArea_(new_area);
 
1453
      emit layerZoomChanged(this);
 
1454
    }
 
1455
    else
 
1456
    {
 
1457
      new_area.setMinY(value);
 
1458
      new_area.setMaxY(value + (visible_area_.maxY() - visible_area_.minY()));
 
1459
      //cout << __PRETTY_FUNCTION__ << endl;
 
1460
      changeVisibleArea_(new_area);
 
1461
      emit layerZoomChanged(this);
 
1462
    }
 
1463
  }
 
1464
 
 
1465
  void Spectrum2DCanvas::verticalScrollBarChange(int value)
 
1466
  {
 
1467
    AreaType new_area = visible_area_;
 
1468
    if (!isMzToXAxis())
 
1469
    {
 
1470
      new_area.setMinX(value);
 
1471
      new_area.setMaxX(value + (visible_area_.maxX() - visible_area_.minX()));
 
1472
      //cout << __PRETTY_FUNCTION__ << endl;
 
1473
      changeVisibleArea_(new_area);
 
1474
      emit layerZoomChanged(this);
 
1475
    }
 
1476
    else
 
1477
    {
 
1478
      new_area.setMinY(value);
 
1479
      new_area.setMaxY(value + (visible_area_.maxY() - visible_area_.minY()));
 
1480
      //cout << __PRETTY_FUNCTION__ << endl;
 
1481
      changeVisibleArea_(new_area);
 
1482
      emit layerZoomChanged(this);
 
1483
    }
 
1484
  }
 
1485
 
 
1486
  void Spectrum2DCanvas::paintEvent(QPaintEvent * e)
 
1487
  {
 
1488
    //Only fill background if no layer is present
 
1489
    if (getLayerCount() == 0)
 
1490
    {
 
1491
      QPainter painter;
 
1492
      painter.begin(this);
 
1493
      painter.fillRect(0, 0, this->width(), this->height(), QColor(param_.getValue("background_color").toQString()));
 
1494
      painter.end();
 
1495
      e->accept();
 
1496
      return;
 
1497
    }
1473
1498
 
1474
1499
#ifdef DEBUG_TOPPVIEW
1475
 
                cout << "BEGIN " << __PRETTY_FUNCTION__ << endl;
1476
 
          cout << "  Visible area -- m/z: " << visible_area_.minX() << " - " << visible_area_.maxX() << " rt: " << visible_area_.minY() << " - " << visible_area_.maxY() << endl;
1477
 
          cout << "  Overall area -- m/z: " << overall_data_range_.minPosition()[0] << " - " << overall_data_range_.maxPosition()[0] << " rt: " << overall_data_range_.minPosition()[1] << " - " << overall_data_range_.maxPosition()[1] << endl;
 
1500
    cout << "BEGIN " << __PRETTY_FUNCTION__ << endl;
 
1501
    cout << "  Visible area -- m/z: " << visible_area_.minX() << " - " << visible_area_.maxX() << " rt: " << visible_area_.minY() << " - " << visible_area_.maxY() << endl;
 
1502
    cout << "  Overall area -- m/z: " << overall_data_range_.minPosition()[0] << " - " << overall_data_range_.maxPosition()[0] << " rt: " << overall_data_range_.minPosition()[1] << " - " << overall_data_range_.maxPosition()[1] << endl;
1478
1503
#endif
1479
 
                
1480
 
                //timing
1481
 
                QTime overall_timer;
1482
 
                if (show_timing_)
1483
 
                {
1484
 
                        overall_timer.start();
1485
 
                        if (update_buffer_)
1486
 
                        {
1487
 
                                cout << "Updating buffer:" << endl;                             
1488
 
                        }
1489
 
                        else
1490
 
                        {
1491
 
                                cout << "Copying buffer:" << endl;                              
1492
 
                        }
1493
 
                }
1494
 
                
1495
 
                QPainter painter;
1496
 
                if (update_buffer_)
1497
 
                {
1498
 
                        update_buffer_ = false;
1499
 
 
1500
 
                        //recalculate snap factor
1501
 
                        recalculateSnapFactor_();
1502
 
 
1503
 
                        buffer_.fill(QColor(param_.getValue("background_color").toQString()).rgb());
1504
 
                        painter.begin(&buffer_);
1505
 
                        QTime layer_timer;
1506
 
                        
1507
 
                        for (Size i=0; i<getLayerCount(); i++)
1508
 
                        {
1509
 
                                //timing
1510
 
                                if (show_timing_)
1511
 
                                {
1512
 
                                        layer_timer.start();
1513
 
                                }
1514
 
                                
1515
 
                                if (getLayer(i).visible)
1516
 
                                {
1517
 
                                        if (getLayer(i).type==LayerData::DT_PEAK)
1518
 
                                        {
1519
 
                                                paintDots_(i, painter);
1520
 
                                        }
1521
 
                                        else if (getLayer(i).type==LayerData::DT_FEATURE)
1522
 
                                        {
1523
 
                                                //cout << "dot feature layer: " << i << endl;
1524
 
                                                if (getLayerFlag(i,LayerData::F_HULLS))
1525
 
                                                {
1526
 
                                                        paintTraceConvexHulls_(i, painter);
1527
 
                                                }
1528
 
                                                if (getLayerFlag(i,LayerData::F_HULL))
1529
 
                                                {
1530
 
                                                        paintFeatureConvexHulls_(i, painter);
1531
 
                                                }
1532
 
                                                if (getLayerFlag(i,LayerData::F_UNASSIGNED))
1533
 
                                                {
1534
 
                                                        paintIdentifications_(i, painter);
1535
 
                                                }
1536
 
                                                paintDots_(i, painter);
1537
 
                                        }
1538
 
                                        else if (getLayer(i).type==LayerData::DT_CONSENSUS)
1539
 
                                        {
1540
 
                                                if (getLayerFlag(i,LayerData::C_ELEMENTS))
1541
 
                                                {
1542
 
                                                        paintConsensusElements_(i, painter);
1543
 
                                                }
1544
 
                                                paintDots_(i, painter);
1545
 
                                        }
1546
 
                                        else if (getLayer(i).type==LayerData::DT_CHROMATOGRAM)
1547
 
                                        {
1548
 
                                                // TODO CHROM
1549
 
                                                paintDots_(i, painter);
1550
 
                                        }
1551
 
                                        else if (getLayer(i).type == LayerData::DT_IDENT)
1552
 
                                        {
1553
 
                                                paintDots_(i, painter);
1554
 
                                        }
1555
 
                                }
1556
 
                                //timing
1557
 
                                if (show_timing_)
1558
 
                                {
1559
 
                                        cout << "  -layer " << i << " time: " << layer_timer.elapsed() << " ms" << endl;
1560
 
                                }
1561
 
                        }
1562
 
                        paintGridLines_(painter);
1563
 
                        painter.end();
1564
 
                }
1565
 
 
1566
 
                painter.begin(this);
1567
 
 
1568
 
                //copy peak data from buffer
1569
 
                QVector<QRect> rects = e->region().rects();
1570
 
                for (int i = 0; i < (int)rects.size(); ++i)
1571
 
                {
1572
 
                        painter.drawImage(rects[i].topLeft(), buffer_, rects[i]);
1573
 
                }
1574
 
 
1575
 
                //draw measurement peak
1576
 
                if (action_mode_==AM_MEASURE && measurement_start_.isValid())
1577
 
                {
1578
 
                        painter.setPen(Qt::black);
1579
 
 
1580
 
                        QPoint line_begin, line_end;
1581
 
 
1582
 
                        if (selected_peak_.isValid())
1583
 
                        {
1584
 
                                if (getCurrentLayer().type==LayerData::DT_FEATURE)
1585
 
                                {
 
1504
 
 
1505
    //timing
 
1506
    QTime overall_timer;
 
1507
    if (show_timing_)
 
1508
    {
 
1509
      overall_timer.start();
 
1510
      if (update_buffer_)
 
1511
      {
 
1512
        cout << "Updating buffer:" << endl;
 
1513
      }
 
1514
      else
 
1515
      {
 
1516
        cout << "Copying buffer:" << endl;
 
1517
      }
 
1518
    }
 
1519
 
 
1520
    QPainter painter;
 
1521
    if (update_buffer_)
 
1522
    {
 
1523
      update_buffer_ = false;
 
1524
 
 
1525
      //recalculate snap factor
 
1526
      recalculateSnapFactor_();
 
1527
 
 
1528
      buffer_.fill(QColor(param_.getValue("background_color").toQString()).rgb());
 
1529
      painter.begin(&buffer_);
 
1530
      QTime layer_timer;
 
1531
 
 
1532
      for (Size i = 0; i < getLayerCount(); i++)
 
1533
      {
 
1534
        //timing
 
1535
        if (show_timing_)
 
1536
        {
 
1537
          layer_timer.start();
 
1538
        }
 
1539
 
 
1540
        if (getLayer(i).visible)
 
1541
        {
 
1542
          if (getLayer(i).type == LayerData::DT_PEAK)
 
1543
          {
 
1544
            paintDots_(i, painter);
 
1545
          }
 
1546
          else if (getLayer(i).type == LayerData::DT_FEATURE)
 
1547
          {
 
1548
            //cout << "dot feature layer: " << i << endl;
 
1549
            if (getLayerFlag(i, LayerData::F_HULLS))
 
1550
            {
 
1551
              paintTraceConvexHulls_(i, painter);
 
1552
            }
 
1553
            if (getLayerFlag(i, LayerData::F_HULL))
 
1554
            {
 
1555
              paintFeatureConvexHulls_(i, painter);
 
1556
            }
 
1557
            if (getLayerFlag(i, LayerData::F_UNASSIGNED))
 
1558
            {
 
1559
              paintIdentifications_(i, painter);
 
1560
            }
 
1561
            paintDots_(i, painter);
 
1562
          }
 
1563
          else if (getLayer(i).type == LayerData::DT_CONSENSUS)
 
1564
          {
 
1565
            if (getLayerFlag(i, LayerData::C_ELEMENTS))
 
1566
            {
 
1567
              paintConsensusElements_(i, painter);
 
1568
            }
 
1569
            paintDots_(i, painter);
 
1570
          }
 
1571
          else if (getLayer(i).type == LayerData::DT_CHROMATOGRAM)
 
1572
          {
 
1573
            // TODO CHROM
 
1574
            paintDots_(i, painter);
 
1575
          }
 
1576
          else if (getLayer(i).type == LayerData::DT_IDENT)
 
1577
          {
 
1578
            paintDots_(i, painter);
 
1579
          }
 
1580
        }
 
1581
        //timing
 
1582
        if (show_timing_)
 
1583
        {
 
1584
          cout << "  -layer " << i << " time: " << layer_timer.elapsed() << " ms" << endl;
 
1585
        }
 
1586
      }
 
1587
      paintGridLines_(painter);
 
1588
      painter.end();
 
1589
    }
 
1590
 
 
1591
    painter.begin(this);
 
1592
 
 
1593
    //copy peak data from buffer
 
1594
    QVector<QRect> rects = e->region().rects();
 
1595
    for (int i = 0; i < (int)rects.size(); ++i)
 
1596
    {
 
1597
      painter.drawImage(rects[i].topLeft(), buffer_, rects[i]);
 
1598
    }
 
1599
 
 
1600
    //draw measurement peak
 
1601
    if (action_mode_ == AM_MEASURE && measurement_start_.isValid())
 
1602
    {
 
1603
      painter.setPen(Qt::black);
 
1604
 
 
1605
      QPoint line_begin, line_end;
 
1606
 
 
1607
      if (selected_peak_.isValid())
 
1608
      {
 
1609
        if (getCurrentLayer().type == LayerData::DT_FEATURE)
 
1610
        {
1586
1611
          dataToWidget_(selected_peak_.getFeature(*getCurrentLayer().getFeatureMap()).getMZ(), selected_peak_.getFeature(*getCurrentLayer().getFeatureMap()).getRT(), line_begin);
1587
 
                                }
1588
 
                                else if (getCurrentLayer().type==LayerData::DT_PEAK)
1589
 
                                {
 
1612
        }
 
1613
        else if (getCurrentLayer().type == LayerData::DT_PEAK)
 
1614
        {
1590
1615
          dataToWidget_(selected_peak_.getPeak(*getCurrentLayer().getPeakData()).getMZ(), selected_peak_.getSpectrum(*getCurrentLayer().getPeakData()).getRT(), line_begin);
1591
 
                                }
1592
 
                                else if (getCurrentLayer().type==LayerData::DT_CONSENSUS)
1593
 
                                {
 
1616
        }
 
1617
        else if (getCurrentLayer().type == LayerData::DT_CONSENSUS)
 
1618
        {
1594
1619
          dataToWidget_(selected_peak_.getFeature(*getCurrentLayer().getConsensusMap()).getMZ(), selected_peak_.getFeature(*getCurrentLayer().getConsensusMap()).getRT(), line_begin);
1595
 
                                }
1596
 
                                else if (getCurrentLayer().type==LayerData::DT_CHROMATOGRAM)
1597
 
                                {
1598
 
                                        //TODO CHROM
1599
 
                                }
1600
 
                        }
1601
 
                        else
1602
 
                        {
1603
 
                                line_begin = last_mouse_pos_;
1604
 
                        }
1605
 
 
1606
 
                        if (getCurrentLayer().type==LayerData::DT_FEATURE)
1607
 
                        {
1608
 
        dataToWidget_(measurement_start_.getFeature(*getCurrentLayer().getFeatureMap()).getMZ(),measurement_start_.getFeature(*getCurrentLayer().getFeatureMap()).getRT(),line_end);
1609
 
                        }
1610
 
                        else if (getCurrentLayer().type==LayerData::DT_PEAK)
1611
 
                        {
1612
 
        dataToWidget_(measurement_start_.getPeak(*getCurrentLayer().getPeakData()).getMZ(),measurement_start_.getSpectrum(*getCurrentLayer().getPeakData()).getRT(),line_end);
1613
 
                        }
1614
 
                        else if (getCurrentLayer().type==LayerData::DT_CONSENSUS)
1615
 
                        {
1616
 
        dataToWidget_(measurement_start_.getFeature(*getCurrentLayer().getConsensusMap()).getMZ(),measurement_start_.getFeature(*getCurrentLayer().getConsensusMap()).getRT(),line_end);
1617
 
                        }
1618
 
                        else if (getCurrentLayer().type==LayerData::DT_CHROMATOGRAM)
1619
 
                        {
1620
 
                                //TODO CHROM
1621
 
                        }
1622
 
                        painter.drawLine(line_begin, line_end);
1623
 
 
1624
 
                        highlightPeak_(painter, measurement_start_);
1625
 
                }
1626
 
                
1627
 
                //draw convex hulls or consensus feature elements
1628
 
                if (selected_peak_.isValid())
1629
 
                {
1630
 
                        if (getCurrentLayer().type==LayerData::DT_FEATURE)
1631
 
                        {
1632
 
                                painter.setPen(QPen(Qt::red, 2));
1633
 
        paintConvexHulls_(selected_peak_.getFeature(*getCurrentLayer().getFeatureMap()). getConvexHulls(),painter);
1634
 
                        }
1635
 
                        else if (getCurrentLayer().type==LayerData::DT_CONSENSUS && getLayerFlag(current_layer_,LayerData::C_ELEMENTS))
1636
 
                        {
1637
 
                                painter.setPen(QPen(Qt::red, 2));
1638
 
        paintConsensusElement_(current_layer_, selected_peak_.getFeature(*getCurrentLayer().getConsensusMap()),painter,false);
1639
 
                        }
1640
 
                }
1641
 
 
1642
 
                if (action_mode_==AM_MEASURE || action_mode_==AM_TRANSLATE)
1643
 
                {
 
1620
        }
 
1621
        else if (getCurrentLayer().type == LayerData::DT_CHROMATOGRAM)
 
1622
        {
 
1623
          //TODO CHROM
 
1624
        }
 
1625
      }
 
1626
      else
 
1627
      {
 
1628
        line_begin = last_mouse_pos_;
 
1629
      }
 
1630
 
 
1631
      if (getCurrentLayer().type == LayerData::DT_FEATURE)
 
1632
      {
 
1633
        dataToWidget_(measurement_start_.getFeature(*getCurrentLayer().getFeatureMap()).getMZ(), measurement_start_.getFeature(*getCurrentLayer().getFeatureMap()).getRT(), line_end);
 
1634
      }
 
1635
      else if (getCurrentLayer().type == LayerData::DT_PEAK)
 
1636
      {
 
1637
        dataToWidget_(measurement_start_.getPeak(*getCurrentLayer().getPeakData()).getMZ(), measurement_start_.getSpectrum(*getCurrentLayer().getPeakData()).getRT(), line_end);
 
1638
      }
 
1639
      else if (getCurrentLayer().type == LayerData::DT_CONSENSUS)
 
1640
      {
 
1641
        dataToWidget_(measurement_start_.getFeature(*getCurrentLayer().getConsensusMap()).getMZ(), measurement_start_.getFeature(*getCurrentLayer().getConsensusMap()).getRT(), line_end);
 
1642
      }
 
1643
      else if (getCurrentLayer().type == LayerData::DT_CHROMATOGRAM)
 
1644
      {
 
1645
        //TODO CHROM
 
1646
      }
 
1647
      painter.drawLine(line_begin, line_end);
 
1648
 
 
1649
      highlightPeak_(painter, measurement_start_);
 
1650
    }
 
1651
 
 
1652
    //draw convex hulls or consensus feature elements
 
1653
    if (selected_peak_.isValid())
 
1654
    {
 
1655
      if (getCurrentLayer().type == LayerData::DT_FEATURE)
 
1656
      {
 
1657
        painter.setPen(QPen(Qt::red, 2));
 
1658
        const Feature& f = selected_peak_.getFeature(*getCurrentLayer().getFeatureMap());
 
1659
        paintConvexHulls_(f.getConvexHulls(),
 
1660
                          f.getPeptideIdentifications().size() && f.getPeptideIdentifications()[0].getHits().size(),
 
1661
                          painter);
 
1662
      }
 
1663
      else if (getCurrentLayer().type == LayerData::DT_CONSENSUS && getLayerFlag(current_layer_, LayerData::C_ELEMENTS))
 
1664
      {
 
1665
        painter.setPen(QPen(Qt::red, 2));
 
1666
        paintConsensusElement_(current_layer_, selected_peak_.getFeature(*getCurrentLayer().getConsensusMap()), painter, false);
 
1667
      }
 
1668
    }
 
1669
 
 
1670
    if (action_mode_ == AM_MEASURE || action_mode_ == AM_TRANSLATE)
 
1671
    {
1644
1672
      highlightPeak_(painter, selected_peak_);
1645
 
                }
1646
 
                
1647
 
                //draw delta for measuring
1648
 
                if (action_mode_==AM_MEASURE && measurement_start_.isValid())
1649
 
                {
1650
 
                        drawDeltas_(painter, measurement_start_, selected_peak_);
1651
 
                }
1652
 
                else
1653
 
                {
1654
 
                        drawCoordinates_(painter, selected_peak_);
1655
 
                }
1656
 
 
1657
 
                painter.end();
 
1673
    }
 
1674
 
 
1675
    //draw delta for measuring
 
1676
    if (action_mode_ == AM_MEASURE && measurement_start_.isValid())
 
1677
    {
 
1678
      drawDeltas_(painter, measurement_start_, selected_peak_);
 
1679
    }
 
1680
    else
 
1681
    {
 
1682
      drawCoordinates_(painter, selected_peak_);
 
1683
    }
 
1684
 
 
1685
    painter.end();
1658
1686
#ifdef DEBUG_TOPPVIEW
1659
 
                cout << "END   " << __PRETTY_FUNCTION__ << endl;
 
1687
    cout << "END   " << __PRETTY_FUNCTION__ << endl;
1660
1688
#endif
1661
 
                if (show_timing_)
1662
 
                {
1663
 
                        cout << "  -overall time: " << overall_timer.elapsed() << " ms" << endl << endl;
1664
 
                }
1665
 
        }
1666
 
 
1667
 
 
1668
 
        void Spectrum2DCanvas::drawCoordinates_(QPainter& painter, const PeakIndex& peak)
1669
 
        {
1670
 
                if (!peak.isValid()) return;
1671
 
                
1672
 
                //determine coordinates;
1673
 
                DoubleReal mz = 0.0;
1674
 
                DoubleReal rt = 0.0;
1675
 
                Real it = 0.0;
1676
 
                Int charge = 0;
1677
 
                DoubleReal quality = 0.0;
1678
 
    Size size = 0;    
 
1689
    if (show_timing_)
 
1690
    {
 
1691
      cout << "  -overall time: " << overall_timer.elapsed() << " ms" << endl << endl;
 
1692
    }
 
1693
  }
 
1694
 
 
1695
  void Spectrum2DCanvas::drawCoordinates_(QPainter & painter, const PeakIndex & peak)
 
1696
  {
 
1697
    if (!peak.isValid())
 
1698
      return;
 
1699
 
 
1700
    //determine coordinates;
 
1701
    DoubleReal mz = 0.0;
 
1702
    DoubleReal rt = 0.0;
 
1703
    Real it = 0.0;
 
1704
    Int charge = 0;
 
1705
    DoubleReal quality = 0.0;
 
1706
    Size size = 0;
 
1707
    const Feature* f = NULL;
 
1708
    const ConsensusFeature* cf = NULL;
1679
1709
    ConsensusFeature::HandleSetType sub_features;
1680
1710
 
1681
 
    switch(getCurrentLayer().type)
1682
 
    {
1683
 
    case LayerData::DT_FEATURE :
1684
 
      {
1685
 
        const Feature& f = peak.getFeature(*getCurrentLayer().getFeatureMap());
1686
 
        mz = f.getMZ();
1687
 
        rt = f.getRT();
1688
 
        it = f.getIntensity();
1689
 
        charge  = f.getCharge();
1690
 
        quality = f.getOverallQuality();
1691
 
      }
1692
 
      break;
1693
 
    case LayerData::DT_PEAK :
1694
 
      {
1695
 
        const Peak1D& p = peak.getPeak(*getCurrentLayer().getPeakData());
1696
 
        const MSSpectrum<>& s = peak.getSpectrum(*getCurrentLayer().getPeakData());
1697
 
        mz = p.getMZ();
1698
 
        rt = s.getRT();
1699
 
        it = p.getIntensity();
1700
 
      }
1701
 
      break;
1702
 
    case LayerData::DT_CONSENSUS :
1703
 
      {
1704
 
        const ConsensusFeature& cf = peak.getFeature(*getCurrentLayer().getConsensusMap());
1705
 
 
1706
 
        mz = cf.getMZ();
1707
 
        rt = cf.getRT();
1708
 
        it = cf.getIntensity();
1709
 
        charge  = cf.getCharge();
1710
 
        quality = cf.getQuality();
1711
 
        sub_features = cf.getFeatures();
1712
 
        size =  sub_features.size();
1713
 
      }
1714
 
      break;
1715
 
 
1716
 
    case LayerData::DT_CHROMATOGRAM :
1717
 
      {
1718
 
        const LayerData& layer = getCurrentLayer();
1719
 
 
1720
 
        MSExperiment<Peak1D> exp;
1721
 
        exp = *layer.getPeakData();
1722
 
 
1723
 
        vector<MSChromatogram<> >::const_iterator iter = exp.getChromatograms().begin();
1724
 
        iter += peak.spectrum;
1725
 
 
1726
 
        mz = iter->getPrecursor().getMZ();
1727
 
        rt = iter->front().getRT();
1728
 
      }
1729
 
      break;
1730
 
 
1731
 
    case LayerData::DT_IDENT :
 
1711
    switch (getCurrentLayer().type)
 
1712
    {
 
1713
    case LayerData::DT_FEATURE:
 
1714
    {
 
1715
      f = &peak.getFeature(*getCurrentLayer().getFeatureMap());
 
1716
      mz = f->getMZ();
 
1717
      rt = f->getRT();
 
1718
      it = f->getIntensity();
 
1719
      charge  = f->getCharge();
 
1720
      quality = f->getOverallQuality();
 
1721
    }
 
1722
    break;
 
1723
 
 
1724
    case LayerData::DT_PEAK:
 
1725
    {
 
1726
      const Peak1D & p = peak.getPeak(*getCurrentLayer().getPeakData());
 
1727
      const MSSpectrum<> & s = peak.getSpectrum(*getCurrentLayer().getPeakData());
 
1728
      mz = p.getMZ();
 
1729
      rt = s.getRT();
 
1730
      it = p.getIntensity();
 
1731
    }
 
1732
    break;
 
1733
 
 
1734
    case LayerData::DT_CONSENSUS:
 
1735
    {
 
1736
      cf = &peak.getFeature(*getCurrentLayer().getConsensusMap());
 
1737
 
 
1738
      mz = cf->getMZ();
 
1739
      rt = cf->getRT();
 
1740
      it = cf->getIntensity();
 
1741
      charge  = cf->getCharge();
 
1742
      quality = cf->getQuality();
 
1743
      sub_features = cf->getFeatures();
 
1744
      size =  sub_features.size();
 
1745
    }
 
1746
    break;
 
1747
 
 
1748
    case LayerData::DT_CHROMATOGRAM:
 
1749
    {
 
1750
      const LayerData & layer = getCurrentLayer();
 
1751
 
 
1752
      MSExperiment<Peak1D> exp;
 
1753
      exp = *layer.getPeakData();
 
1754
 
 
1755
      vector<MSChromatogram<> >::const_iterator iter = exp.getChromatograms().begin();
 
1756
      iter += peak.spectrum;
 
1757
 
 
1758
      mz = iter->getPrecursor().getMZ();
 
1759
      rt = iter->front().getRT();
 
1760
    }
 
1761
    break;
 
1762
 
 
1763
    case LayerData::DT_IDENT:
1732
1764
      // TODO implement
1733
1765
      break;
1734
1766
 
1736
1768
      break;
1737
1769
    }
1738
1770
 
1739
 
                //draw text                     
1740
 
                QStringList lines;
1741
 
    lines.push_back("RT: " + QString::number(rt,'f',2));
1742
 
    lines.push_back("m/z: " + QString::number(mz,'f',2));
1743
 
                lines.push_back("Int: " + QString::number(it,'f',2));
 
1771
    //draw text
 
1772
    QStringList lines;
 
1773
    lines.push_back("RT: " + QString::number(rt, 'f', 2));
 
1774
    lines.push_back("m/z: " + QString::number(mz, 'f', 2));
 
1775
    lines.push_back("Int: " + QString::number(it, 'f', 2));
1744
1776
 
1745
 
    if (getCurrentLayer().type == LayerData::DT_FEATURE || getCurrentLayer().type==LayerData::DT_CONSENSUS)
1746
 
                {
1747
 
                        lines.push_back("Charge: " + QString::number(charge));
1748
 
                        lines.push_back("Quality: " + QString::number(quality,'f',4));
1749
 
                }
 
1777
    if (getCurrentLayer().type == LayerData::DT_FEATURE || getCurrentLayer().type == LayerData::DT_CONSENSUS)
 
1778
    {
 
1779
      lines.push_back("Charge: " + QString::number(charge));
 
1780
      lines.push_back("Quality: " + QString::number(quality, 'f', 4));
 
1781
      // peptide identifications
 
1782
      const PeptideIdentification* pis = NULL;
 
1783
      if ( f && f->getPeptideIdentifications().size() > 0 ) {
 
1784
        pis = &f->getPeptideIdentifications()[0];
 
1785
      }
 
1786
      else if ( cf && cf->getPeptideIdentifications().size() > 0 ) {
 
1787
        pis = &cf->getPeptideIdentifications()[0];
 
1788
      }
 
1789
      if ( pis && pis->getHits().size() ) {
 
1790
          Size nHits = pis->getHits().size();
 
1791
          for (Size j = 0; j < nHits; ++j)
 
1792
          {
 
1793
            lines.push_back( "Peptide" + ( nHits > 1 ? "[" + QString::number(j+1) + "]" : "" ) + ": "
 
1794
                             + pis->getHits()[j].getSequence().toString().toQString() );
 
1795
          }
 
1796
      }
 
1797
    }
1750
1798
 
1751
1799
    if (getCurrentLayer().type == LayerData::DT_CONSENSUS)
1752
1800
    {
1753
1801
      lines.push_back("Size: " + QString::number(size));
1754
 
      for ( ConsensusFeature::HandleSetType::const_iterator it = sub_features.begin(); it != sub_features.end(); ++it)
 
1802
      for (ConsensusFeature::HandleSetType::const_iterator it = sub_features.begin(); it != sub_features.end(); ++it)
1755
1803
      {
1756
 
        lines.push_back("Feature m/z:" + QString::number(it->getMZ(),'f',2) +
1757
 
                               "  rt:" + QString::number(it->getRT(),'f',2) +
1758
 
                               "   q:" + QString::number(it->getCharge(),'f',2) +
1759
 
                               "  intensity:" + QString::number(it->getIntensity(),'f',2));
 
1804
        lines.push_back("Feature m/z:" + QString::number(it->getMZ(), 'f', 2) +
 
1805
                        "  rt:" + QString::number(it->getRT(), 'f', 2) +
 
1806
                        "   q:" + QString::number(it->getCharge(), 'f', 2) +
 
1807
                        "  intensity:" + QString::number(it->getIntensity(), 'f', 2));
1760
1808
      }
1761
1809
    }
1762
1810
 
1763
 
                drawText_(painter, lines);
1764
 
        }
1765
 
 
1766
 
        void Spectrum2DCanvas::drawDeltas_(QPainter& painter, const PeakIndex& start, const PeakIndex& end)
1767
 
        {
1768
 
                if (!start.isValid()) return;
1769
 
                
1770
 
                //determine coordinates;
1771
 
                DoubleReal mz = 0.0;
1772
 
                DoubleReal rt = 0.0;
1773
 
                Real it = 0.0;
1774
 
                if (getCurrentLayer().type==LayerData::DT_FEATURE)
1775
 
                {
1776
 
                        if (end.isValid())
1777
 
                        {
 
1811
    drawText_(painter, lines);
 
1812
  }
 
1813
 
 
1814
  void Spectrum2DCanvas::drawDeltas_(QPainter & painter, const PeakIndex & start, const PeakIndex & end)
 
1815
  {
 
1816
    if (!start.isValid())
 
1817
      return;
 
1818
 
 
1819
    //determine coordinates;
 
1820
    DoubleReal mz = 0.0;
 
1821
    DoubleReal rt = 0.0;
 
1822
    Real it = 0.0;
 
1823
    if (getCurrentLayer().type == LayerData::DT_FEATURE)
 
1824
    {
 
1825
      if (end.isValid())
 
1826
      {
1778
1827
        mz = end.getFeature(*getCurrentLayer().getFeatureMap()).getMZ() - start.getFeature(*getCurrentLayer().getFeatureMap()).getMZ();
1779
1828
        rt = end.getFeature(*getCurrentLayer().getFeatureMap()).getRT() - start.getFeature(*getCurrentLayer().getFeatureMap()).getRT();
1780
1829
        it = end.getFeature(*getCurrentLayer().getFeatureMap()).getIntensity() / start.getFeature(*getCurrentLayer().getFeatureMap()).getIntensity();
1781
 
                        }
1782
 
                        else
1783
 
                        {
1784
 
                                PointType point = widgetToData_(last_mouse_pos_);
 
1830
      }
 
1831
      else
 
1832
      {
 
1833
        PointType point = widgetToData_(last_mouse_pos_);
1785
1834
        mz = point[0] - start.getFeature(*getCurrentLayer().getFeatureMap()).getMZ();
1786
1835
        rt = point[1] - start.getFeature(*getCurrentLayer().getFeatureMap()).getRT();
1787
 
                                it = std::numeric_limits<DoubleReal>::quiet_NaN();
1788
 
                        }
1789
 
                }
1790
 
                else if (getCurrentLayer().type==LayerData::DT_PEAK)
1791
 
                {
1792
 
                        if (end.isValid())
1793
 
                        {
 
1836
        it = std::numeric_limits<DoubleReal>::quiet_NaN();
 
1837
      }
 
1838
    }
 
1839
    else if (getCurrentLayer().type == LayerData::DT_PEAK)
 
1840
    {
 
1841
      if (end.isValid())
 
1842
      {
1794
1843
        mz = end.getPeak(*getCurrentLayer().getPeakData()).getMZ() - start.getPeak(*getCurrentLayer().getPeakData()).getMZ();
1795
1844
        rt = end.getSpectrum(*getCurrentLayer().getPeakData()).getRT() - start.getSpectrum(*getCurrentLayer().getPeakData()).getRT();
1796
1845
        it = end.getPeak(*getCurrentLayer().getPeakData()).getIntensity() / start.getPeak(*getCurrentLayer().getPeakData()).getIntensity();
1797
 
                        }
1798
 
                        else
1799
 
                        {
1800
 
                                PointType point = widgetToData_(last_mouse_pos_);
 
1846
      }
 
1847
      else
 
1848
      {
 
1849
        PointType point = widgetToData_(last_mouse_pos_);
1801
1850
        mz = point[0] - start.getPeak(*getCurrentLayer().getPeakData()).getMZ();
1802
1851
        rt = point[1] - start.getSpectrum(*getCurrentLayer().getPeakData()).getRT();
1803
 
                                it = std::numeric_limits<DoubleReal>::quiet_NaN();
1804
 
                        }
1805
 
                }
1806
 
                else if (getCurrentLayer().type==LayerData::DT_CONSENSUS)
1807
 
                {
1808
 
                        if (end.isValid())
1809
 
                        {
 
1852
        it = std::numeric_limits<DoubleReal>::quiet_NaN();
 
1853
      }
 
1854
    }
 
1855
    else if (getCurrentLayer().type == LayerData::DT_CONSENSUS)
 
1856
    {
 
1857
      if (end.isValid())
 
1858
      {
1810
1859
        mz = end.getFeature(*getCurrentLayer().getConsensusMap()).getMZ() - start.getFeature(*getCurrentLayer().getConsensusMap()).getMZ();
1811
1860
        rt = end.getFeature(*getCurrentLayer().getConsensusMap()).getRT() - start.getFeature(*getCurrentLayer().getConsensusMap()).getRT();
1812
1861
        it = end.getFeature(*getCurrentLayer().getConsensusMap()).getIntensity() / start.getFeature(*getCurrentLayer().getConsensusMap()).getIntensity();
1813
 
                        }
1814
 
                        else
1815
 
                        {
1816
 
                                PointType point = widgetToData_(last_mouse_pos_);
 
1862
      }
 
1863
      else
 
1864
      {
 
1865
        PointType point = widgetToData_(last_mouse_pos_);
1817
1866
        mz = point[0] - start.getFeature(*getCurrentLayer().getConsensusMap()).getMZ();
1818
1867
        rt = point[1] - start.getFeature(*getCurrentLayer().getConsensusMap()).getRT();
1819
 
                                it = std::numeric_limits<DoubleReal>::quiet_NaN();
1820
 
                        }
1821
 
                }
1822
 
                else if (getCurrentLayer().type==LayerData::DT_CHROMATOGRAM)
1823
 
                {
1824
 
                        //TODO CHROM
1825
 
                }
1826
 
                else if (getCurrentLayer().type == LayerData::DT_IDENT)
1827
 
                {
1828
 
                        // TODO IDENT
1829
 
                }
1830
 
 
1831
 
                //draw text                     
1832
 
                QStringList lines;
1833
 
                lines.push_back("RT delta:  " + QString::number(rt,'f',2));
1834
 
                lines.push_back("m/z delta: " + QString::number(mz,'f',6));
1835
 
                if (boost::math::isinf(it) || boost::math::isnan(it))
1836
 
                {
1837
 
                        lines.push_back("Int ratio: n/a");
1838
 
                }
1839
 
                else
1840
 
                {
1841
 
                        lines.push_back("Int ratio: " + QString::number(it,'f',2));                     
1842
 
                }
1843
 
                drawText_(painter, lines);
1844
 
        }
1845
 
 
1846
 
 
1847
 
        void Spectrum2DCanvas::mousePressEvent(QMouseEvent* e)
1848
 
        {
1849
 
                last_mouse_pos_ = e->pos();
1850
 
 
1851
 
                if (e->button() == Qt::LeftButton)
1852
 
                {
1853
 
                        if (action_mode_ == AM_MEASURE)
1854
 
                        {
1855
 
                                if (selected_peak_.isValid())
1856
 
                                {
1857
 
                                        measurement_start_ = selected_peak_;
1858
 
                                }
1859
 
                                else
1860
 
                                {
1861
 
                                        measurement_start_.clear();
1862
 
                                }
1863
 
                        }
1864
 
                        else if (action_mode_ == AM_ZOOM)
1865
 
                        {
1866
 
                                //translate (if not moving features)
1867
 
                                if (!getCurrentLayer().type==LayerData::DT_FEATURE || !selected_peak_.isValid())
1868
 
                                {
1869
 
                                        rubber_band_.setGeometry(QRect(e->pos(),QSize()));
1870
 
                                        rubber_band_.show();
1871
 
                                }
1872
 
                        }
1873
 
                }
1874
 
        }
1875
 
 
1876
 
        void Spectrum2DCanvas::mouseMoveEvent(QMouseEvent* e)
1877
 
        {
1878
 
                grabKeyboard(); // (re-)grab keyboard after it has been released by unhandled key
1879
 
                QPoint pos = e->pos();
1880
 
                PointType data_pos = widgetToData_(pos);
1881
 
                emit sendCursorStatus( data_pos[0], data_pos[1]);
1882
 
                                        
1883
 
          PeakIndex near_peak = findNearestPeak_(pos);
1884
 
 
1885
 
                //highlight current peak and display peak coordinates
 
1868
        it = std::numeric_limits<DoubleReal>::quiet_NaN();
 
1869
      }
 
1870
    }
 
1871
    else if (getCurrentLayer().type == LayerData::DT_CHROMATOGRAM)
 
1872
    {
 
1873
      //TODO CHROM
 
1874
    }
 
1875
    else if (getCurrentLayer().type == LayerData::DT_IDENT)
 
1876
    {
 
1877
      // TODO IDENT
 
1878
    }
 
1879
 
 
1880
    //draw text
 
1881
    QStringList lines;
 
1882
    lines.push_back("RT delta:  " + QString::number(rt, 'f', 2));
 
1883
    lines.push_back("m/z delta: " + QString::number(mz, 'f', 6));
 
1884
    if (boost::math::isinf(it) || boost::math::isnan(it))
 
1885
    {
 
1886
      lines.push_back("Int ratio: n/a");
 
1887
    }
 
1888
    else
 
1889
    {
 
1890
      lines.push_back("Int ratio: " + QString::number(it, 'f', 2));
 
1891
    }
 
1892
    drawText_(painter, lines);
 
1893
  }
 
1894
 
 
1895
  void Spectrum2DCanvas::mousePressEvent(QMouseEvent * e)
 
1896
  {
 
1897
    last_mouse_pos_ = e->pos();
 
1898
 
 
1899
    if (e->button() == Qt::LeftButton)
 
1900
    {
 
1901
      if (action_mode_ == AM_MEASURE)
 
1902
      {
 
1903
        if (selected_peak_.isValid())
 
1904
        {
 
1905
          measurement_start_ = selected_peak_;
 
1906
        }
 
1907
        else
 
1908
        {
 
1909
          measurement_start_.clear();
 
1910
        }
 
1911
      }
 
1912
      else if (action_mode_ == AM_ZOOM)
 
1913
      {
 
1914
        //translate (if not moving features)
 
1915
        if (!getCurrentLayer().type == LayerData::DT_FEATURE || !selected_peak_.isValid())
 
1916
        {
 
1917
          rubber_band_.setGeometry(QRect(e->pos(), QSize()));
 
1918
          rubber_band_.show();
 
1919
        }
 
1920
      }
 
1921
    }
 
1922
  }
 
1923
 
 
1924
  void Spectrum2DCanvas::mouseMoveEvent(QMouseEvent * e)
 
1925
  {
 
1926
    grabKeyboard();     // (re-)grab keyboard after it has been released by unhandled key
 
1927
    QPoint pos = e->pos();
 
1928
    PointType data_pos = widgetToData_(pos);
 
1929
    emit sendCursorStatus(data_pos[0], data_pos[1]);
 
1930
 
 
1931
    PeakIndex near_peak = findNearestPeak_(pos);
 
1932
 
 
1933
    //highlight current peak and display peak coordinates
1886
1934
    if (action_mode_ == AM_MEASURE || (action_mode_ == AM_TRANSLATE && !(e->buttons() & Qt::LeftButton)))
1887
 
                {
1888
 
                        //highlight peak
1889
 
                        selected_peak_ = near_peak;
1890
 
                        update_(__PRETTY_FUNCTION__);
1891
 
 
1892
 
                        //show meta data in status bar (if available)
1893
 
                        if (selected_peak_.isValid())
1894
 
                        {
1895
 
                                String status;
1896
 
                                if (getCurrentLayer().type==LayerData::DT_FEATURE)
1897
 
                                {
1898
 
                                        //add meta info
1899
 
          const FeatureMapType::FeatureType& f = selected_peak_.getFeature(*getCurrentLayer().getFeatureMap());
1900
 
                                        std::vector<String> keys;
1901
 
                                        f.getKeys(keys);
1902
 
                                        for (Size m=0; m<keys.size(); ++m)
1903
 
                                        {
1904
 
                                                status = status + " " + keys[m] + ": " + (String)(f.getMetaValue(keys[m]));
1905
 
                                        }
1906
 
                                }
1907
 
                                else if (getCurrentLayer().type==LayerData::DT_PEAK)
1908
 
                                {
1909
 
                                        //meta info
1910
 
          const ExperimentType::SpectrumType& s = selected_peak_.getSpectrum(*getCurrentLayer().getPeakData());
1911
 
                                        for (Size m=0; m<s.getFloatDataArrays().size();++m)
1912
 
                                        {
1913
 
                                                if (selected_peak_.peak < s.getFloatDataArrays()[m].size())
1914
 
                                                {
1915
 
                                                        status += s.getFloatDataArrays()[m].getName() + ": " + s.getFloatDataArrays()[m][selected_peak_.peak] + " ";
1916
 
                                                }
1917
 
                                        }
1918
 
                                        for (Size m=0; m<s.getIntegerDataArrays().size();++m)
1919
 
                                        {
1920
 
                                                if (selected_peak_.peak < s.getIntegerDataArrays()[m].size())
1921
 
                                                {
1922
 
                                                        status += s.getIntegerDataArrays()[m].getName() + ": " + s.getIntegerDataArrays()[m][selected_peak_.peak] + " ";
1923
 
                                                }
1924
 
                                        }
1925
 
                                        for (Size m=0; m<s.getStringDataArrays().size();++m)
1926
 
                                        {
1927
 
                                                if (selected_peak_.peak < s.getStringDataArrays()[m].size())
1928
 
                                                {
1929
 
                                                        status += s.getStringDataArrays()[m].getName() + ": " + s.getStringDataArrays()[m][selected_peak_.peak] + " ";
1930
 
                                                }
1931
 
                                        }
1932
 
                                }
1933
 
                                else if (getCurrentLayer().type==LayerData::DT_CONSENSUS)// ConsensusFeature
1934
 
                                {
1935
 
                                        //add meta info
1936
 
          const ConsensusFeature& f = selected_peak_.getFeature(*getCurrentLayer().getConsensusMap());
1937
 
                                        std::vector<String> keys;
1938
 
                                        f.getKeys(keys);
1939
 
                                        for (Size m=0; m<keys.size(); ++m)
1940
 
                                        {
1941
 
                                                status = status + " " + keys[m] + ": " + (String)(f.getMetaValue(keys[m]));
1942
 
                                        }
1943
 
                                }
1944
 
        else if (getCurrentLayer().type == LayerData::DT_CHROMATOGRAM)// chromatogram
1945
 
                                {
1946
 
                                        //TODO CHROM
1947
 
                                }
1948
 
                                if (status!="") emit sendStatusMessage(status, 0);
1949
 
                        }
1950
 
                }
1951
 
    else if (action_mode_ == AM_ZOOM)
1952
 
                {
1953
 
                        //Zoom mode => no peak should be selected
1954
 
                        selected_peak_.clear();
1955
 
                        update_(__PRETTY_FUNCTION__);
1956
 
                }
1957
 
 
1958
 
                if (action_mode_==AM_MEASURE)
1959
 
                {
1960
 
                        last_mouse_pos_ = pos;
1961
 
                }
1962
 
    else if (action_mode_ == AM_ZOOM)
1963
 
                {
1964
 
                        //if mouse button is held down, enlarge the selection
1965
 
                        if (e->buttons() & Qt::LeftButton)
1966
 
                        {
1967
 
                                rubber_band_.setGeometry(QRect(last_mouse_pos_,pos).normalized());
1968
 
                                rubber_band_.show(); //if the mouse button is pressed before the zoom key is pressed
1969
 
 
1970
 
                                update_(__PRETTY_FUNCTION__);
1971
 
                        }
1972
 
                }
1973
 
                else if (action_mode_ == AM_TRANSLATE)
1974
 
                {
1975
 
                        if (e->buttons() & Qt::LeftButton)
1976
 
                        {
1977
 
                                if (getCurrentLayer().modifiable && getCurrentLayer().type==LayerData::DT_FEATURE && selected_peak_.isValid()) //move feature
1978
 
                                {
1979
 
                                        PointType new_data = widgetToData_(pos);
1980
 
                                        DoubleReal mz = new_data[0];
1981
 
                                        DoubleReal rt = new_data[1];
1982
 
 
1983
 
                                        //restrict the movement to the data range
1984
 
                                        mz = max(mz,overall_data_range_.minPosition()[0]);
1985
 
                                        mz = min(mz,overall_data_range_.maxPosition()[0]);
1986
 
                                        rt = max(rt,overall_data_range_.minPosition()[1]);
1987
 
                                        rt = min(rt,overall_data_range_.maxPosition()[1]);
 
1935
    {
 
1936
      //highlight peak
 
1937
      selected_peak_ = near_peak;
 
1938
      update_(__PRETTY_FUNCTION__);
 
1939
 
 
1940
      //show meta data in status bar (if available)
 
1941
      if (selected_peak_.isValid())
 
1942
      {
 
1943
        String status;
 
1944
        if (getCurrentLayer().type == LayerData::DT_FEATURE)
 
1945
        {
 
1946
          //add meta info
 
1947
          const FeatureMapType::FeatureType & f = selected_peak_.getFeature(*getCurrentLayer().getFeatureMap());
 
1948
          std::vector<String> keys;
 
1949
          f.getKeys(keys);
 
1950
          for (Size m = 0; m < keys.size(); ++m)
 
1951
          {
 
1952
            status = status + " " + keys[m] + ": " + (String)(f.getMetaValue(keys[m]));
 
1953
          }
 
1954
        }
 
1955
        else if (getCurrentLayer().type == LayerData::DT_PEAK)
 
1956
        {
 
1957
          //meta info
 
1958
          const ExperimentType::SpectrumType & s = selected_peak_.getSpectrum(*getCurrentLayer().getPeakData());
 
1959
          for (Size m = 0; m < s.getFloatDataArrays().size(); ++m)
 
1960
          {
 
1961
            if (selected_peak_.peak < s.getFloatDataArrays()[m].size())
 
1962
            {
 
1963
              status += s.getFloatDataArrays()[m].getName() + ": " + s.getFloatDataArrays()[m][selected_peak_.peak] + " ";
 
1964
            }
 
1965
          }
 
1966
          for (Size m = 0; m < s.getIntegerDataArrays().size(); ++m)
 
1967
          {
 
1968
            if (selected_peak_.peak < s.getIntegerDataArrays()[m].size())
 
1969
            {
 
1970
              status += s.getIntegerDataArrays()[m].getName() + ": " + s.getIntegerDataArrays()[m][selected_peak_.peak] + " ";
 
1971
            }
 
1972
          }
 
1973
          for (Size m = 0; m < s.getStringDataArrays().size(); ++m)
 
1974
          {
 
1975
            if (selected_peak_.peak < s.getStringDataArrays()[m].size())
 
1976
            {
 
1977
              status += s.getStringDataArrays()[m].getName() + ": " + s.getStringDataArrays()[m][selected_peak_.peak] + " ";
 
1978
            }
 
1979
          }
 
1980
        }
 
1981
        else if (getCurrentLayer().type == LayerData::DT_CONSENSUS)      // ConsensusFeature
 
1982
        {
 
1983
          //add meta info
 
1984
          const ConsensusFeature & f = selected_peak_.getFeature(*getCurrentLayer().getConsensusMap());
 
1985
          std::vector<String> keys;
 
1986
          f.getKeys(keys);
 
1987
          for (Size m = 0; m < keys.size(); ++m)
 
1988
          {
 
1989
            status = status + " " + keys[m] + ": " + (String)(f.getMetaValue(keys[m]));
 
1990
          }
 
1991
        }
 
1992
        else if (getCurrentLayer().type == LayerData::DT_CHROMATOGRAM) // chromatogram
 
1993
        {
 
1994
          //TODO CHROM
 
1995
        }
 
1996
        if (status != "")
 
1997
          emit sendStatusMessage(status, 0);
 
1998
      }
 
1999
    }
 
2000
    else if (action_mode_ == AM_ZOOM)
 
2001
    {
 
2002
      //Zoom mode => no peak should be selected
 
2003
      selected_peak_.clear();
 
2004
      update_(__PRETTY_FUNCTION__);
 
2005
    }
 
2006
 
 
2007
    if (action_mode_ == AM_MEASURE)
 
2008
    {
 
2009
      last_mouse_pos_ = pos;
 
2010
    }
 
2011
    else if (action_mode_ == AM_ZOOM)
 
2012
    {
 
2013
      //if mouse button is held down, enlarge the selection
 
2014
      if (e->buttons() & Qt::LeftButton)
 
2015
      {
 
2016
        rubber_band_.setGeometry(QRect(last_mouse_pos_, pos).normalized());
 
2017
        rubber_band_.show();         //if the mouse button is pressed before the zoom key is pressed
 
2018
 
 
2019
        update_(__PRETTY_FUNCTION__);
 
2020
      }
 
2021
    }
 
2022
    else if (action_mode_ == AM_TRANSLATE)
 
2023
    {
 
2024
      if (e->buttons() & Qt::LeftButton)
 
2025
      {
 
2026
        if (getCurrentLayer().modifiable && getCurrentLayer().type == LayerData::DT_FEATURE && selected_peak_.isValid())       //move feature
 
2027
        {
 
2028
          PointType new_data = widgetToData_(pos);
 
2029
          DoubleReal mz = new_data[0];
 
2030
          DoubleReal rt = new_data[1];
 
2031
 
 
2032
          //restrict the movement to the data range
 
2033
          mz = max(mz, overall_data_range_.minPosition()[0]);
 
2034
          mz = min(mz, overall_data_range_.maxPosition()[0]);
 
2035
          rt = max(rt, overall_data_range_.minPosition()[1]);
 
2036
          rt = min(rt, overall_data_range_.maxPosition()[1]);
1988
2037
 
1989
2038
          (*getCurrentLayer_().getFeatureMap())[selected_peak_.peak].setRT(rt);
1990
2039
          (*getCurrentLayer_().getFeatureMap())[selected_peak_.peak].setMZ(mz);
1991
2040
 
1992
 
                                        update_buffer_ = true;
1993
 
                                        update_(__PRETTY_FUNCTION__);
 
2041
          update_buffer_ = true;
 
2042
          update_(__PRETTY_FUNCTION__);
1994
2043
          modificationStatus_(activeLayerIndex(), true);
1995
 
                                }
1996
 
                                else //translate
1997
 
                                {
1998
 
                                        //calculate data coordinates of shift
1999
 
                                        PointType old_data = widgetToData_(last_mouse_pos_);
2000
 
                                        PointType new_data = widgetToData_(pos);
2001
 
                                        //calculate x shift
2002
 
                                        DoubleReal shift = old_data.getX() - new_data.getX();
2003
 
                                        DoubleReal newLoX = visible_area_.minX() + shift;
2004
 
                                        DoubleReal newHiX = visible_area_.maxX() + shift;
2005
 
                                        // check if we are falling out of bounds
2006
 
                                        if (newLoX < overall_data_range_.minX())
2007
 
                                        {
2008
 
                                                newLoX = overall_data_range_.minX();
2009
 
                                                newHiX = newLoX + visible_area_.width();
2010
 
                                        }
2011
 
                                        if (newHiX > overall_data_range_.maxX())
2012
 
                                        {
2013
 
                                                newHiX = overall_data_range_.maxX();
2014
 
                                                newLoX = newHiX - visible_area_.width();
2015
 
                                        }
2016
 
                                        //calculate y shift
2017
 
                                        shift = old_data.getY() - new_data.getY();
2018
 
                                        DoubleReal newLoY = visible_area_.minY() + shift;
2019
 
                                        DoubleReal newHiY = visible_area_.maxY() + shift;
2020
 
                                        // check if we are falling out of bounds
2021
 
                                        if (newLoY < overall_data_range_.minY())
2022
 
                                        {
2023
 
                                                newLoY = overall_data_range_.minY();
2024
 
                                                newHiY = newLoY + visible_area_.height();
2025
 
                                        }
2026
 
                                        if (newHiY > overall_data_range_.maxY())
2027
 
                                        {
2028
 
                                                newHiY = overall_data_range_.maxY();
2029
 
                                                newLoY = newHiY - visible_area_.height();
2030
 
                                        }
2031
 
 
2032
 
                                        //change area
2033
 
                                        //cout << "New area: x " << newLoX <<"-"<< newHiX << " - y "<<newLoY <<"-"<< newHiY << endl;
2034
 
                                        //cout << __PRETTY_FUNCTION__ << endl;
2035
 
                                        changeVisibleArea_(AreaType(newLoX,newLoY,newHiX,newHiY));
2036
 
                                        last_mouse_pos_ = pos;
2037
 
                                }
2038
 
                        }
2039
 
                }
2040
 
        }
2041
 
 
2042
 
        void Spectrum2DCanvas::mouseReleaseEvent(QMouseEvent* e)
2043
 
        {
2044
 
                if (e->button() == Qt::LeftButton)
2045
 
                {
2046
 
                        if (action_mode_==AM_MEASURE)
2047
 
                        {
2048
 
                                if (!selected_peak_.isValid())
2049
 
                                {
2050
 
                                        measurement_start_.clear();
2051
 
                                }
2052
 
                                measurement_start_.clear();
2053
 
                                update_(__PRETTY_FUNCTION__);
2054
 
                        }
2055
 
                  else if (action_mode_ == AM_ZOOM)
2056
 
                        {
2057
 
                                rubber_band_.hide();
2058
 
                                QRect rect = rubber_band_.geometry();
2059
 
                                if (rect.width()!=0 && rect.height()!=0)
2060
 
                                {
2061
 
                                        AreaType area(widgetToData_(rect.topLeft()), widgetToData_(rect.bottomRight()));
2062
 
                                        //cout << __PRETTY_FUNCTION__ << endl;
2063
 
                                        changeVisibleArea_(area, true, true);
2064
 
                                }
2065
 
                        }
2066
 
                }
2067
 
        }
2068
 
 
2069
 
        void Spectrum2DCanvas::contextMenuEvent(QContextMenuEvent* e)
2070
 
        {
 
2044
        }
 
2045
        else         //translate
 
2046
        {
 
2047
          //calculate data coordinates of shift
 
2048
          PointType old_data = widgetToData_(last_mouse_pos_);
 
2049
          PointType new_data = widgetToData_(pos);
 
2050
          //calculate x shift
 
2051
          DoubleReal shift = old_data.getX() - new_data.getX();
 
2052
          DoubleReal newLoX = visible_area_.minX() + shift;
 
2053
          DoubleReal newHiX = visible_area_.maxX() + shift;
 
2054
          // check if we are falling out of bounds
 
2055
          if (newLoX < overall_data_range_.minX())
 
2056
          {
 
2057
            newLoX = overall_data_range_.minX();
 
2058
            newHiX = newLoX + visible_area_.width();
 
2059
          }
 
2060
          if (newHiX > overall_data_range_.maxX())
 
2061
          {
 
2062
            newHiX = overall_data_range_.maxX();
 
2063
            newLoX = newHiX - visible_area_.width();
 
2064
          }
 
2065
          //calculate y shift
 
2066
          shift = old_data.getY() - new_data.getY();
 
2067
          DoubleReal newLoY = visible_area_.minY() + shift;
 
2068
          DoubleReal newHiY = visible_area_.maxY() + shift;
 
2069
          // check if we are falling out of bounds
 
2070
          if (newLoY < overall_data_range_.minY())
 
2071
          {
 
2072
            newLoY = overall_data_range_.minY();
 
2073
            newHiY = newLoY + visible_area_.height();
 
2074
          }
 
2075
          if (newHiY > overall_data_range_.maxY())
 
2076
          {
 
2077
            newHiY = overall_data_range_.maxY();
 
2078
            newLoY = newHiY - visible_area_.height();
 
2079
          }
 
2080
 
 
2081
          //change area
 
2082
          //cout << "New area: x " << newLoX <<"-"<< newHiX << " - y "<<newLoY <<"-"<< newHiY << endl;
 
2083
          //cout << __PRETTY_FUNCTION__ << endl;
 
2084
          changeVisibleArea_(AreaType(newLoX, newLoY, newHiX, newHiY));
 
2085
          emit layerZoomChanged(this);
 
2086
          last_mouse_pos_ = pos;
 
2087
        }
 
2088
      }
 
2089
    }
 
2090
  }
 
2091
 
 
2092
  void Spectrum2DCanvas::mouseReleaseEvent(QMouseEvent * e)
 
2093
  {
 
2094
    if (e->button() == Qt::LeftButton)
 
2095
    {
 
2096
      if (action_mode_ == AM_MEASURE)
 
2097
      {
 
2098
        if (!selected_peak_.isValid())
 
2099
        {
 
2100
          measurement_start_.clear();
 
2101
        }
 
2102
        measurement_start_.clear();
 
2103
        update_(__PRETTY_FUNCTION__);
 
2104
      }
 
2105
      else if (action_mode_ == AM_ZOOM)
 
2106
      {
 
2107
        rubber_band_.hide();
 
2108
        QRect rect = rubber_band_.geometry();
 
2109
        if (rect.width() != 0 && rect.height() != 0)
 
2110
        {
 
2111
          AreaType area(widgetToData_(rect.topLeft()), widgetToData_(rect.bottomRight()));
 
2112
          //cout << __PRETTY_FUNCTION__ << endl;
 
2113
          changeVisibleArea_(area, true, true);
 
2114
          emit layerZoomChanged(this);
 
2115
        }
 
2116
      }
 
2117
    }
 
2118
  }
 
2119
 
 
2120
  void Spectrum2DCanvas::contextMenuEvent(QContextMenuEvent * e)
 
2121
  {
2071
2122
    //Abort if there are no layers
2072
2123
    if (layers_.empty())
2073
2124
    {
2075
2126
    }
2076
2127
 
2077
2128
    DoubleReal mz = widgetToData_(e->pos())[0];
2078
 
                DoubleReal rt = widgetToData_(e->pos())[1];
2079
 
 
2080
 
                const LayerData& layer = getCurrentLayer();
2081
 
 
2082
 
                QMenu* context_menu = new QMenu(this);
2083
 
 
2084
 
                QAction* a = 0;
2085
 
                QAction* result = 0;
2086
 
 
2087
 
                //Display name and warn if current layer invisible
2088
 
                String layer_name = String("Layer: ") + layer.name;
2089
 
                if (!layer.visible)
2090
 
                {
2091
 
                        layer_name += " (invisible)";
2092
 
                }
2093
 
                context_menu->addAction(layer_name.toQString())->setEnabled(false);
2094
 
                context_menu->addSeparator();
2095
 
 
2096
 
                context_menu->addAction("Layer meta data");
2097
 
 
2098
 
                QMenu* settings_menu = new QMenu("Settings");
2099
 
                settings_menu->addAction("Show/hide grid lines");
2100
 
                settings_menu->addAction("Show/hide axis legends");
 
2129
    DoubleReal rt = widgetToData_(e->pos())[1];
 
2130
 
 
2131
    const LayerData & layer = getCurrentLayer();
 
2132
 
 
2133
    QMenu * context_menu = new QMenu(this);
 
2134
 
 
2135
    QAction * a = 0;
 
2136
    QAction * result = 0;
 
2137
 
 
2138
    //Display name and warn if current layer invisible
 
2139
    String layer_name = String("Layer: ") + layer.name;
 
2140
    if (!layer.visible)
 
2141
    {
 
2142
      layer_name += " (invisible)";
 
2143
    }
 
2144
    context_menu->addAction(layer_name.toQString())->setEnabled(false);
 
2145
    context_menu->addSeparator();
 
2146
 
 
2147
    context_menu->addAction("Layer meta data");
 
2148
 
 
2149
    QMenu * settings_menu = new QMenu("Settings");
 
2150
    settings_menu->addAction("Show/hide grid lines");
 
2151
    settings_menu->addAction("Show/hide axis legends");
2101
2152
    context_menu->addSeparator();
2102
2153
 
2103
2154
    context_menu->addAction("Switch to 3D view");
2104
2155
 
2105
 
                //-------------------PEAKS----------------------------------
2106
 
                if (layer.type==LayerData::DT_PEAK)
2107
 
                {     
2108
 
                        //add settings
2109
 
                        settings_menu->addSeparator();
2110
 
                        settings_menu->addAction("Show/hide projections");
2111
 
                        settings_menu->addAction("Show/hide MS/MS precursors");
 
2156
    //-------------------PEAKS----------------------------------
 
2157
    if (layer.type == LayerData::DT_PEAK)
 
2158
    {
 
2159
      //add settings
 
2160
      settings_menu->addSeparator();
 
2161
      settings_menu->addAction("Show/hide projections");
 
2162
      settings_menu->addAction("Show/hide MS/MS precursors");
2112
2163
 
2113
 
                        //add surrounding survey scans
2114
 
                        //find nearest survey scan
 
2164
      //add surrounding survey scans
 
2165
      //find nearest survey scan
2115
2166
      SignedSize size = getCurrentLayer().getPeakData()->size();
2116
 
      Int current = getCurrentLayer().getPeakData()->RTBegin(rt)-getCurrentLayer().getPeakData()->begin();
 
2167
      Int current = getCurrentLayer().getPeakData()->RTBegin(rt) - getCurrentLayer().getPeakData()->begin();
2117
2168
      if (current == size)  // if only one element is present RTBegin points to one after the last element (see RTBegin implementation)
2118
2169
      {
2119
2170
        current = 0;
2120
2171
      }
2121
2172
 
2122
 
                        SignedSize i=0;
2123
 
                        while (current+i<size || current-i>=0)
2124
 
                        {
2125
 
        if(current+i<size && (*getCurrentLayer().getPeakData())[current+i].getMSLevel()==1)
2126
 
                                {
2127
 
                                        current = current+i;
2128
 
                                        break;
2129
 
                                }
2130
 
        if(current-i>=0 && (*getCurrentLayer().getPeakData())[current-i].getMSLevel()==1)
2131
 
                                {
2132
 
                                        current = current-i;
2133
 
                                        break;
2134
 
                                }
2135
 
                                ++i;
2136
 
                        }
2137
 
                        //search for four scans in both directions
2138
 
                        vector<Int> indices;
2139
 
                        indices.push_back(current);
2140
 
                        i=1;
2141
 
                        while (current-i>=0 && indices.size()<5)
2142
 
                        {
2143
 
        if ((*getCurrentLayer().getPeakData())[current-i].getMSLevel()==1)
2144
 
                                {
2145
 
                                        indices.push_back(current-i);
2146
 
                                }
2147
 
                                ++i;
2148
 
                        }
2149
 
                        i=1;
2150
 
                        while (current+i<size && indices.size()<9)
2151
 
                        {
2152
 
        if ((*getCurrentLayer().getPeakData())[current+i].getMSLevel()==1)
2153
 
                                {
2154
 
                                        indices.push_back(current+i);
2155
 
                                }
2156
 
                                ++i;
2157
 
                        }
2158
 
                        sort(indices.rbegin(),indices.rend());
2159
 
                        QMenu* ms1_scans = context_menu->addMenu("Survey scan in 1D");
2160
 
                        QMenu* ms1_meta = context_menu->addMenu("Survey scan meta data");
2161
 
                        context_menu->addSeparator();
2162
 
                        for(i=0; i<(Int)indices.size(); ++i)
2163
 
                        {
2164
 
        if (indices[i]==current)
 
2173
      SignedSize i = 0;
 
2174
      while (current + i < size || current - i >= 0)
 
2175
      {
 
2176
        if (current + i < size && (*getCurrentLayer().getPeakData())[current + i].getMSLevel() == 1)
 
2177
        {
 
2178
          current = current + i;
 
2179
          break;
 
2180
        }
 
2181
        if (current - i >= 0 && (*getCurrentLayer().getPeakData())[current - i].getMSLevel() == 1)
 
2182
        {
 
2183
          current = current - i;
 
2184
          break;
 
2185
        }
 
2186
        ++i;
 
2187
      }
 
2188
      //search for four scans in both directions
 
2189
      vector<Int> indices;
 
2190
      indices.push_back(current);
 
2191
      i = 1;
 
2192
      while (current - i >= 0 && indices.size() < 5)
 
2193
      {
 
2194
        if ((*getCurrentLayer().getPeakData())[current - i].getMSLevel() == 1)
 
2195
        {
 
2196
          indices.push_back(current - i);
 
2197
        }
 
2198
        ++i;
 
2199
      }
 
2200
      i = 1;
 
2201
      while (current + i < size && indices.size() < 9)
 
2202
      {
 
2203
        if ((*getCurrentLayer().getPeakData())[current + i].getMSLevel() == 1)
 
2204
        {
 
2205
          indices.push_back(current + i);
 
2206
        }
 
2207
        ++i;
 
2208
      }
 
2209
      sort(indices.rbegin(), indices.rend());
 
2210
      QMenu * ms1_scans = context_menu->addMenu("Survey scan in 1D");
 
2211
      QMenu * ms1_meta = context_menu->addMenu("Survey scan meta data");
 
2212
      context_menu->addSeparator();
 
2213
      for (i = 0; i < (Int)indices.size(); ++i)
 
2214
      {
 
2215
        if (indices[i] == current)
2165
2216
        {
2166
2217
          ms1_scans->addSeparator();
2167
2218
        }
2168
2219
        a = ms1_scans->addAction(QString("RT: ") + QString::number((*getCurrentLayer().getPeakData())[indices[i]].getRT()));
2169
 
                                a->setData(indices[i]);
2170
 
        if (indices[i]==current)
 
2220
        a->setData(indices[i]);
 
2221
        if (indices[i] == current)
2171
2222
        {
2172
2223
          ms1_scans->addSeparator();
2173
2224
        }
2174
2225
 
2175
 
        if (indices[i]==current)
 
2226
        if (indices[i] == current)
2176
2227
        {
2177
2228
          ms1_meta->addSeparator();
2178
2229
        }
2179
2230
        a = ms1_meta->addAction(QString("RT: ") + QString::number((*getCurrentLayer().getPeakData())[indices[i]].getRT()));
2180
 
                                a->setData(indices[i]);
2181
 
        if (indices[i]==current)
 
2231
        a->setData(indices[i]);
 
2232
        if (indices[i] == current)
2182
2233
        {
2183
2234
          ms1_meta->addSeparator();
2184
2235
        }
2185
 
                        }
 
2236
      }
2186
2237
 
2187
 
                        //add surrounding fragment scans
2188
 
                        QMenu* msn_scans = new QMenu("fragment scan in 1D");
2189
 
                        QMenu* msn_meta = new QMenu("fragment scan meta data");
2190
 
                        DPosition<2> p1 = widgetToData_(e->pos()+ QPoint(10,10));
2191
 
                        DPosition<2> p2 = widgetToData_(e->pos()- QPoint(10,10));
2192
 
                        DoubleReal rt_min = min(p1[1],p2[1]);
2193
 
                        DoubleReal rt_max = max(p1[1],p2[1]);
2194
 
                        DoubleReal mz_min = min(p1[0],p2[0]);
2195
 
                        DoubleReal mz_max = max(p1[0],p2[0]);
2196
 
                        bool item_added = false;
2197
 
      for (ExperimentType::ConstIterator it=getCurrentLayer().getPeakData()->RTBegin(rt_min); it!=getCurrentLayer().getPeakData()->RTEnd(rt_max); ++it)
2198
 
                        {
2199
 
                                DoubleReal mz = 0.0;
2200
 
                                if (!it->getPrecursors().empty()) 
 
2238
      //add surrounding fragment scans
 
2239
      QMenu * msn_scans = new QMenu("fragment scan in 1D");
 
2240
      QMenu * msn_meta = new QMenu("fragment scan meta data");
 
2241
      DPosition<2> p1 = widgetToData_(e->pos() + QPoint(10, 10));
 
2242
      DPosition<2> p2 = widgetToData_(e->pos() - QPoint(10, 10));
 
2243
      DoubleReal rt_min = min(p1[1], p2[1]);
 
2244
      DoubleReal rt_max = max(p1[1], p2[1]);
 
2245
      DoubleReal mz_min = min(p1[0], p2[0]);
 
2246
      DoubleReal mz_max = max(p1[0], p2[0]);
 
2247
      bool item_added = false;
 
2248
      for (ExperimentType::ConstIterator it = getCurrentLayer().getPeakData()->RTBegin(rt_min); it != getCurrentLayer().getPeakData()->RTEnd(rt_max); ++it)
 
2249
      {
 
2250
        DoubleReal mz = 0.0;
 
2251
        if (!it->getPrecursors().empty())
2201
2252
        {
2202
2253
          mz = it->getPrecursors()[0].getMZ();
2203
2254
        }
2204
2255
 
2205
 
                                if (it->getMSLevel()>1 && mz >= mz_min && mz <= mz_max)
2206
 
                                {
2207
 
                                        a = msn_scans->addAction(QString("RT: ") + QString::number(it->getRT()) + " mz: " + QString::number(mz));
2208
 
                                        a->setData((int)(it-getCurrentLayer().getPeakData()->begin()));
2209
 
                                        a = msn_meta->addAction(QString("RT: ") + QString::number(it->getRT()) + " mz: " + QString::number(mz));
2210
 
                                        a->setData((int)(it-getCurrentLayer().getPeakData()->begin()));
2211
 
                                        item_added = true;
2212
 
                                }
2213
 
                        }
2214
 
                        if (item_added)
2215
 
                        {
2216
 
                                context_menu->addMenu(msn_scans);
2217
 
                                context_menu->addMenu(msn_meta);
2218
 
                                context_menu->addSeparator();
2219
 
                        }
2220
 
 
2221
 
                        finishContextMenu_(context_menu, settings_menu);
2222
 
 
2223
 
                        //evaluate menu
2224
 
                        if ((result = context_menu->exec(mapToGlobal(e->pos()))))
2225
 
                        {
2226
 
                                if (result->parent()==ms1_scans  || result->parent()==msn_scans)
2227
 
                                {
2228
 
                                        emit showSpectrumAs1D(result->data().toInt());
2229
 
                                }
2230
 
                                else if (result->parent()==ms1_meta || result->parent()==msn_meta)
2231
 
                                {
2232
 
                                        showMetaData(true, result->data().toInt());
2233
 
                                }
2234
 
                        }
2235
 
                }
2236
 
                //-------------------FEATURES----------------------------------
2237
 
                else if (layer.type==LayerData::DT_FEATURE)
2238
 
                {
2239
 
                        //add settings
2240
 
                        settings_menu->addSeparator();
2241
 
                        settings_menu->addAction("Show/hide convex hull");
2242
 
                        settings_menu->addAction("Show/hide trace convex hulls");
2243
 
                        settings_menu->addAction("Show/hide numbers/labels");
2244
 
                        settings_menu->addAction("Show/hide unassigned peptide hits");
2245
 
 
2246
 
                        //search for nearby features
2247
 
                        DPosition<2> p1 = widgetToData_(e->pos()+ QPoint(10,10));
2248
 
                        DPosition<2> p2 = widgetToData_(e->pos()- QPoint(10,10));
2249
 
                        DoubleReal rt_min = min(p1[1],p2[1]);
2250
 
                        DoubleReal rt_max = max(p1[1],p2[1]);
2251
 
                        DoubleReal mz_min = min(p1[0],p2[0]);
2252
 
                        DoubleReal mz_max = max(p1[0],p2[0]);
2253
 
 
2254
 
                        QMenu* meta = new QMenu("Feature meta data");
2255
 
                        bool present = false;
2256
 
      FeatureMapType& features = *getCurrentLayer_().getFeatureMap();
2257
 
                        //featre meta data menu
2258
 
                        for (FeatureMapType::Iterator it = features.begin(); it!=features.end(); ++it)
2259
 
                        {
2260
 
                                if (it->getMZ() <= mz_max && it->getMZ() >= mz_min && it->getRT() <= rt_max && it->getRT() >= rt_min)
2261
 
                                {
2262
 
                                        present=true;
2263
 
                                        a = meta->addAction(QString("RT: ") + QString::number(it->getRT()) + "  m/z:" + QString::number(it->getMZ()) + "  charge:" + QString::number(it->getCharge()));
2264
 
                                        a->setData((int)(it-features.begin()));
2265
 
                                }
2266
 
                        }
2267
 
                        if (present)
2268
 
                        {
2269
 
                                context_menu->addMenu(meta);
2270
 
                                context_menu->addSeparator();
2271
 
                        }
2272
 
                        
2273
 
                        //add modifiable flag
2274
 
                        settings_menu->addSeparator();
2275
 
                        settings_menu->addAction("Toggle edit/view mode");
2276
 
 
2277
 
                        finishContextMenu_(context_menu, settings_menu);
2278
 
 
2279
 
                        //evaluate menu
2280
 
                        if ((result = context_menu->exec(mapToGlobal(e->pos()))))
2281
 
                        {
2282
 
                                if (result->text().left(3)=="RT:")
2283
 
                                {
2284
 
                                        showMetaData(true,result->data().toInt());
2285
 
                                }
2286
 
                        }
2287
 
                }
2288
 
                //-------------------CONSENSUS FEATURES----------------------------------
2289
 
                else if (layer.type==LayerData::DT_CONSENSUS)
2290
 
                {
2291
 
                        //add settings
2292
 
                        settings_menu->addSeparator();
2293
 
                        settings_menu->addAction("Show/hide elements");
2294
 
 
2295
 
                        //search for nearby features
2296
 
                        DPosition<2> p1 = widgetToData_(e->pos()+ QPoint(10,10));
2297
 
                        DPosition<2> p2 = widgetToData_(e->pos()- QPoint(10,10));
2298
 
                        DoubleReal rt_min = min(p1[1],p2[1]);
2299
 
                        DoubleReal rt_max = max(p1[1],p2[1]);
2300
 
                        DoubleReal mz_min = min(p1[0],p2[0]);
2301
 
                        DoubleReal mz_max = max(p1[0],p2[0]);
2302
 
 
2303
 
                        QMenu* consens_meta = new QMenu("Consensus meta data");
2304
 
                        bool present = false;
2305
 
      ConsensusMapType& features = *getCurrentLayer_().getConsensusMap();
2306
 
                        //consensus feature meta data menu
2307
 
                        for (ConsensusMapType::Iterator it = features.begin(); it!=features.end(); ++it)
2308
 
                        {
2309
 
                                if (it->getMZ() <= mz_max && it->getMZ() >= mz_min && it->getRT() <= rt_max && it->getRT() >= rt_min)
2310
 
                                {
2311
 
                                        present=true;
2312
 
 
2313
 
                                        a = consens_meta->addAction(QString("RT: ") + QString::number(it->getRT()) + "  m/z:" + QString::number(it->getMZ()) + "  charge:" + QString::number(it->getCharge()));
2314
 
                                        a->setData((int)(it-features.begin()));
2315
 
                                }
2316
 
                        }
2317
 
                        if (present)
2318
 
                        {
2319
 
                                context_menu->addMenu(consens_meta);
2320
 
                                context_menu->addSeparator();
2321
 
                        }
2322
 
 
2323
 
                        finishContextMenu_(context_menu, settings_menu);
2324
 
 
2325
 
                        if ((result = context_menu->exec(mapToGlobal(e->pos()))))
2326
 
                        {
2327
 
                                if (result->text().left(3)=="RT:")
2328
 
                                {
2329
 
                                        showMetaData(true, result->data().toInt());
2330
 
                                }
2331
 
                        }
2332
 
                }
2333
 
                //------------------CHROMATOGRAMS----------------------------------
 
2256
        if (it->getMSLevel() > 1 && mz >= mz_min && mz <= mz_max)
 
2257
        {
 
2258
          a = msn_scans->addAction(QString("RT: ") + QString::number(it->getRT()) + " mz: " + QString::number(mz));
 
2259
          a->setData((int)(it - getCurrentLayer().getPeakData()->begin()));
 
2260
          a = msn_meta->addAction(QString("RT: ") + QString::number(it->getRT()) + " mz: " + QString::number(mz));
 
2261
          a->setData((int)(it - getCurrentLayer().getPeakData()->begin()));
 
2262
          item_added = true;
 
2263
        }
 
2264
      }
 
2265
      if (item_added)
 
2266
      {
 
2267
        context_menu->addMenu(msn_scans);
 
2268
        context_menu->addMenu(msn_meta);
 
2269
        context_menu->addSeparator();
 
2270
      }
 
2271
 
 
2272
      finishContextMenu_(context_menu, settings_menu);
 
2273
 
 
2274
      //evaluate menu
 
2275
      if ((result = context_menu->exec(mapToGlobal(e->pos()))))
 
2276
      {
 
2277
        if (result->parent() == ms1_scans  || result->parent() == msn_scans)
 
2278
        {
 
2279
          emit showSpectrumAs1D(result->data().toInt());
 
2280
        }
 
2281
        else if (result->parent() == ms1_meta || result->parent() == msn_meta)
 
2282
        {
 
2283
          showMetaData(true, result->data().toInt());
 
2284
        }
 
2285
      }
 
2286
    }
 
2287
    //-------------------FEATURES----------------------------------
 
2288
    else if (layer.type == LayerData::DT_FEATURE)
 
2289
    {
 
2290
      //add settings
 
2291
      settings_menu->addSeparator();
 
2292
      settings_menu->addAction("Show/hide convex hull");
 
2293
      settings_menu->addAction("Show/hide trace convex hulls");
 
2294
      settings_menu->addAction("Show/hide numbers/labels");
 
2295
      settings_menu->addAction("Show/hide unassigned peptide hits");
 
2296
 
 
2297
      //search for nearby features
 
2298
      DPosition<2> p1 = widgetToData_(e->pos() + QPoint(10, 10));
 
2299
      DPosition<2> p2 = widgetToData_(e->pos() - QPoint(10, 10));
 
2300
      DoubleReal rt_min = min(p1[1], p2[1]);
 
2301
      DoubleReal rt_max = max(p1[1], p2[1]);
 
2302
      DoubleReal mz_min = min(p1[0], p2[0]);
 
2303
      DoubleReal mz_max = max(p1[0], p2[0]);
 
2304
 
 
2305
      QMenu * meta = new QMenu("Feature meta data");
 
2306
      bool present = false;
 
2307
      FeatureMapType & features = *getCurrentLayer_().getFeatureMap();
 
2308
      //featre meta data menu
 
2309
      for (FeatureMapType::Iterator it = features.begin(); it != features.end(); ++it)
 
2310
      {
 
2311
        if (it->getMZ() <= mz_max && it->getMZ() >= mz_min && it->getRT() <= rt_max && it->getRT() >= rt_min)
 
2312
        {
 
2313
          present = true;
 
2314
          a = meta->addAction(QString("RT: ") + QString::number(it->getRT()) + "  m/z:" + QString::number(it->getMZ()) + "  charge:" + QString::number(it->getCharge()));
 
2315
          a->setData((int)(it - features.begin()));
 
2316
        }
 
2317
      }
 
2318
      if (present)
 
2319
      {
 
2320
        context_menu->addMenu(meta);
 
2321
        context_menu->addSeparator();
 
2322
      }
 
2323
 
 
2324
      //add modifiable flag
 
2325
      settings_menu->addSeparator();
 
2326
      settings_menu->addAction("Toggle edit/view mode");
 
2327
 
 
2328
      finishContextMenu_(context_menu, settings_menu);
 
2329
 
 
2330
      //evaluate menu
 
2331
      if ((result = context_menu->exec(mapToGlobal(e->pos()))))
 
2332
      {
 
2333
        if (result->text().left(3) == "RT:")
 
2334
        {
 
2335
          showMetaData(true, result->data().toInt());
 
2336
        }
 
2337
      }
 
2338
    }
 
2339
    //-------------------CONSENSUS FEATURES----------------------------------
 
2340
    else if (layer.type == LayerData::DT_CONSENSUS)
 
2341
    {
 
2342
      //add settings
 
2343
      settings_menu->addSeparator();
 
2344
      settings_menu->addAction("Show/hide elements");
 
2345
 
 
2346
      //search for nearby features
 
2347
      DPosition<2> p1 = widgetToData_(e->pos() + QPoint(10, 10));
 
2348
      DPosition<2> p2 = widgetToData_(e->pos() - QPoint(10, 10));
 
2349
      DoubleReal rt_min = min(p1[1], p2[1]);
 
2350
      DoubleReal rt_max = max(p1[1], p2[1]);
 
2351
      DoubleReal mz_min = min(p1[0], p2[0]);
 
2352
      DoubleReal mz_max = max(p1[0], p2[0]);
 
2353
 
 
2354
      QMenu * consens_meta = new QMenu("Consensus meta data");
 
2355
      bool present = false;
 
2356
      ConsensusMapType & features = *getCurrentLayer_().getConsensusMap();
 
2357
      //consensus feature meta data menu
 
2358
      for (ConsensusMapType::Iterator it = features.begin(); it != features.end(); ++it)
 
2359
      {
 
2360
        if (it->getMZ() <= mz_max && it->getMZ() >= mz_min && it->getRT() <= rt_max && it->getRT() >= rt_min)
 
2361
        {
 
2362
          present = true;
 
2363
 
 
2364
          a = consens_meta->addAction(QString("RT: ") + QString::number(it->getRT()) + "  m/z:" + QString::number(it->getMZ()) + "  charge:" + QString::number(it->getCharge()));
 
2365
          a->setData((int)(it - features.begin()));
 
2366
        }
 
2367
      }
 
2368
      if (present)
 
2369
      {
 
2370
        context_menu->addMenu(consens_meta);
 
2371
        context_menu->addSeparator();
 
2372
      }
 
2373
 
 
2374
      finishContextMenu_(context_menu, settings_menu);
 
2375
 
 
2376
      if ((result = context_menu->exec(mapToGlobal(e->pos()))))
 
2377
      {
 
2378
        if (result->text().left(3) == "RT:")
 
2379
        {
 
2380
          showMetaData(true, result->data().toInt());
 
2381
        }
 
2382
      }
 
2383
    }
 
2384
    //------------------CHROMATOGRAMS----------------------------------
2334
2385
    else if (layer.type == LayerData::DT_CHROMATOGRAM)
2335
 
                {
 
2386
    {
2336
2387
      settings_menu->addSeparator();
2337
2388
      settings_menu->addAction("Show/hide projections");
2338
2389
      settings_menu->addAction("Show/hide MS/MS precursors");
2340
2391
      MSExperiment<Peak1D> exp;
2341
2392
      exp = *layer.getPeakData();
2342
2393
 
 
2394
      int CHROMATOGRAM_SHOW_MZ_RANGE = 10;
 
2395
 
2343
2396
      // collect all precursor that fall into the mz rt window
2344
2397
      typedef std::set<Precursor, Precursor::MZLess> PCSetType;
2345
2398
      PCSetType precursor_in_rt_mz_window;
2346
2399
      for (vector<MSChromatogram<> >::const_iterator iter = exp.getChromatograms().begin(); iter != exp.getChromatograms().end(); ++iter)
2347
2400
      {
2348
 
        if ( mz + 10.0 >= iter->getPrecursor().getMZ() &&
2349
 
             mz - 10.0 <= iter->getPrecursor().getMZ() &&
2350
 
             rt >= iter->front().getRT() &&
2351
 
             rt <= iter->back().getRT())
 
2401
        if (mz + CHROMATOGRAM_SHOW_MZ_RANGE >= iter->getPrecursor().getMZ() &&
 
2402
            mz - CHROMATOGRAM_SHOW_MZ_RANGE <= iter->getPrecursor().getMZ() &&
 
2403
            rt >= iter->front().getRT() &&
 
2404
            rt <= iter->back().getRT())
2352
2405
        {
2353
2406
          precursor_in_rt_mz_window.insert(iter->getPrecursor());
2354
2407
        }
2367
2420
        }
2368
2421
      }
2369
2422
 
2370
 
          /*
2371
 
            // dump precursor and associated products
2372
 
            for (map<Precursor, vector<Size>, Precursor::MZLess >::iterator mit = map_precursor_to_chrom_idx.begin(); mit != map_precursor_to_chrom_idx.end(); ++mit)
2373
 
            {
2374
 
              cout << "Precursor: " << mit->first << endl;
2375
 
              for (vector<Size>::iterator vit = mit->second.begin(); vit != mit->second.end(); ++vit)
2376
 
              {
2377
 
                cout << "  Product mz: " << exp.getChromatograms()[*vit].getMZ() << endl;
2378
 
              }
2379
 
            }
2380
 
            */
2381
 
 
2382
 
      QMenu* msn_chromatogram  = 0;
2383
 
      QMenu* msn_chromatogram_meta = 0;
2384
 
 
2385
 
      if ( !map_precursor_to_chrom_idx.empty() )
 
2423
      /*
 
2424
        // dump precursor and associated products
 
2425
        for (map<Precursor, vector<Size>, Precursor::MZLess >::iterator mit = map_precursor_to_chrom_idx.begin(); mit != map_precursor_to_chrom_idx.end(); ++mit)
 
2426
        {
 
2427
          cout << "Precursor: " << mit->first << endl;
 
2428
          for (vector<Size>::iterator vit = mit->second.begin(); vit != mit->second.end(); ++vit)
 
2429
          {
 
2430
            cout << "  Product mz: " << exp.getChromatograms()[*vit].getMZ() << endl;
 
2431
          }
 
2432
        }
 
2433
        */
 
2434
 
 
2435
      QMenu * msn_chromatogram  = 0;
 
2436
      QMenu * msn_chromatogram_meta = 0;
 
2437
 
 
2438
      if (!map_precursor_to_chrom_idx.empty())
2386
2439
      {
2387
2440
        msn_chromatogram = context_menu->addMenu("Chromatogram");
2388
2441
        msn_chromatogram_meta = context_menu->addMenu("Chromatogram meta data");
2389
2442
        context_menu->addSeparator();
2390
2443
 
2391
 
        for (map<Precursor, vector<Size>, Precursor::MZLess >::iterator mit = map_precursor_to_chrom_idx.begin(); mit != map_precursor_to_chrom_idx.end(); ++mit)
 
2444
        for (map<Precursor, vector<Size>, Precursor::MZLess>::iterator mit = map_precursor_to_chrom_idx.begin(); mit != map_precursor_to_chrom_idx.end(); ++mit)
2392
2445
        {
2393
 
          QMenu* msn_precursor = msn_chromatogram->addMenu(QString("Precursor m/z: ") + QString::number(mit->first.getMZ()));  // neuer Eintrag für jeden Precursor
 
2446
          // Show the peptide sequence if available, otherwise show the m/z and charge only
 
2447
          QString precursor_string = QString("Precursor m/z: (")  + String(mit->first.getCharge()).toQString() + ") " + QString::number(mit->first.getMZ());
 
2448
          if (mit->first.metaValueExists("peptide_sequence"))
 
2449
          {
 
2450
            precursor_string = QString::number(mit->first.getMZ()) + " : " + String(mit->first.getMetaValue("peptide_sequence")).toQString() + " (" + QString::number(mit->first.getCharge()) + "+)";
 
2451
          }
 
2452
          QMenu * msn_precursor = msn_chromatogram->addMenu(precursor_string);  // neuer Eintrag für jeden Precursor
2394
2453
 
2395
2454
          // Show all: iterate over all chromatograms corresponding to the current precursor and add action containing all chromatograms
2396
2455
          a = msn_precursor->addAction(QString("Show all"));
2419
2478
        {
2420
2479
          if (result->text() == "Show all")
2421
2480
          {
2422
 
            const QList<QVariant>& res = result->data().toList();
 
2481
            std::vector<int> chrom_indices;
 
2482
            const QList<QVariant> & res = result->data().toList();
2423
2483
            for (Int i = 0; i != res.size(); ++i)
2424
2484
            {
2425
 
              // cout << "Show all:" << endl;
2426
 
              cout << res[i].toInt() << endl;
 
2485
              chrom_indices.push_back(res[i].toInt());
 
2486
              cout << "chrom_indices: " << res[i].toInt() << std::endl;
2427
2487
            }
2428
 
          } else // Show single chromatogram
 
2488
            emit showSpectrumAs1D(chrom_indices);
 
2489
          }
 
2490
          else   // Show single chromatogram
2429
2491
          {
2430
2492
            //cout << "Chromatogram result " << result->data().toInt() << endl;
2431
2493
            emit showSpectrumAs1D(result->data().toInt());
2436
2498
          showMetaData(true, result->data().toInt());
2437
2499
        }
2438
2500
      }
2439
 
                }
2440
 
                
2441
 
                //common actions of peaks and features
2442
 
                if (result)
2443
 
                {
2444
 
                        if (result->text() == "Preferences")
2445
 
                        {
2446
 
                                showCurrentLayerPreferences();
2447
 
                        }
2448
 
                        else if (result->text() == "Show/hide grid lines")
2449
 
                        {
2450
 
                                showGridLines(!gridLinesShown());
2451
 
                        }
2452
 
                        else if (result->text() == "Show/hide axis legends")
2453
 
                        {
2454
 
                                emit changeLegendVisibility();
2455
 
                        }
2456
 
                        else if (result->text()=="Layer" || result->text()=="Visible layer data")
2457
 
                        {
2458
 
                                saveCurrentLayer(result->text()=="Visible layer data");
2459
 
                        }
2460
 
                        else if (result->text()=="As image")
2461
 
                        {
2462
 
                                spectrum_widget_->saveAsImage();
2463
 
                        }
2464
 
                        else if (result->text()=="Show/hide projections")
2465
 
                        {
2466
 
                                emit toggleProjections();
2467
 
                        }
2468
 
                        else if (result->text()=="Show/hide MS/MS precursors")
2469
 
                        {
2470
 
                                setLayerFlag(LayerData::P_PRECURSORS,!getLayerFlag(LayerData::P_PRECURSORS));
2471
 
                        }
2472
 
                        else if (result->text()=="Show/hide convex hull")
2473
 
                        {
2474
 
                                setLayerFlag(LayerData::F_HULL,!getLayerFlag(LayerData::F_HULL));
2475
 
                        }
2476
 
                        else if (result->text()=="Show/hide trace convex hulls")
2477
 
                        {
2478
 
                                setLayerFlag(LayerData::F_HULLS,!getLayerFlag(LayerData::F_HULLS));
2479
 
                        }
2480
 
                        else if (result->text()=="Show/hide unassigned peptide hits")
2481
 
                        {
2482
 
                                setLayerFlag(LayerData::F_UNASSIGNED,!getLayerFlag(LayerData::F_UNASSIGNED));
2483
 
                        }
2484
 
                        else if (result->text()=="Show/hide numbers/labels")
2485
 
                        {
2486
 
                                if (layer.label==LayerData::L_NONE) 
2487
 
                                {
2488
 
                                        getCurrentLayer_().label=LayerData::L_META_LABEL;
2489
 
                                }
2490
 
                                else 
2491
 
                                {
2492
 
                                        getCurrentLayer_().label=LayerData::L_NONE;
2493
 
                                }
2494
 
                        }
2495
 
                        else if (result->text()=="Toggle edit/view mode")
2496
 
                        {
 
2501
    }
 
2502
 
 
2503
    //common actions of peaks and features
 
2504
    if (result)
 
2505
    {
 
2506
      if (result->text() == "Preferences")
 
2507
      {
 
2508
        showCurrentLayerPreferences();
 
2509
      }
 
2510
      else if (result->text() == "Show/hide grid lines")
 
2511
      {
 
2512
        showGridLines(!gridLinesShown());
 
2513
      }
 
2514
      else if (result->text() == "Show/hide axis legends")
 
2515
      {
 
2516
        emit changeLegendVisibility();
 
2517
      }
 
2518
      else if (result->text() == "Layer" || result->text() == "Visible layer data")
 
2519
      {
 
2520
        saveCurrentLayer(result->text() == "Visible layer data");
 
2521
      }
 
2522
      else if (result->text() == "As image")
 
2523
      {
 
2524
        spectrum_widget_->saveAsImage();
 
2525
      }
 
2526
      else if (result->text() == "Show/hide projections")
 
2527
      {
 
2528
        emit toggleProjections();
 
2529
      }
 
2530
      else if (result->text() == "Show/hide MS/MS precursors")
 
2531
      {
 
2532
        setLayerFlag(LayerData::P_PRECURSORS, !getLayerFlag(LayerData::P_PRECURSORS));
 
2533
      }
 
2534
      else if (result->text() == "Show/hide convex hull")
 
2535
      {
 
2536
        setLayerFlag(LayerData::F_HULL, !getLayerFlag(LayerData::F_HULL));
 
2537
      }
 
2538
      else if (result->text() == "Show/hide trace convex hulls")
 
2539
      {
 
2540
        setLayerFlag(LayerData::F_HULLS, !getLayerFlag(LayerData::F_HULLS));
 
2541
      }
 
2542
      else if (result->text() == "Show/hide unassigned peptide hits")
 
2543
      {
 
2544
        setLayerFlag(LayerData::F_UNASSIGNED, !getLayerFlag(LayerData::F_UNASSIGNED));
 
2545
      }
 
2546
      else if (result->text() == "Show/hide numbers/labels")
 
2547
      {
 
2548
        if (layer.label == LayerData::L_NONE)
 
2549
        {
 
2550
          getCurrentLayer_().label = LayerData::L_META_LABEL;
 
2551
        }
 
2552
        else
 
2553
        {
 
2554
          getCurrentLayer_().label = LayerData::L_NONE;
 
2555
        }
 
2556
      }
 
2557
      else if (result->text() == "Toggle edit/view mode")
 
2558
      {
2497
2559
        getCurrentLayer_().modifiable = !getCurrentLayer_().modifiable;
2498
 
                        }
2499
 
                        else if (result->text()=="Show/hide elements")
2500
 
                        {
2501
 
                                setLayerFlag(LayerData::C_ELEMENTS,!getLayerFlag(LayerData::C_ELEMENTS));
2502
 
                        }
2503
 
                        else if (result->text()=="Layer meta data")
2504
 
                        {
2505
 
                                showMetaData(true);
2506
 
                        }
2507
 
      else if (result->text()=="Switch to 3D view")
 
2560
      }
 
2561
      else if (result->text() == "Show/hide elements")
 
2562
      {
 
2563
        setLayerFlag(LayerData::C_ELEMENTS, !getLayerFlag(LayerData::C_ELEMENTS));
 
2564
      }
 
2565
      else if (result->text() == "Layer meta data")
 
2566
      {
 
2567
        showMetaData(true);
 
2568
      }
 
2569
      else if (result->text() == "Switch to 3D view")
2508
2570
      {
2509
2571
        emit showCurrentPeaksAs3D();
2510
2572
      }
2511
 
                }
2512
 
 
2513
 
                e->accept();
2514
 
        }
2515
 
 
2516
 
        void Spectrum2DCanvas::finishContextMenu_(QMenu* context_menu, QMenu* settings_menu)
2517
 
        {
2518
 
                //finish settings menu
2519
 
                settings_menu->addSeparator();
2520
 
                settings_menu->addAction("Preferences");
2521
 
 
2522
 
                //create save menu
2523
 
                QMenu* save_menu = new QMenu("Save");
2524
 
                save_menu->addAction("Layer");
2525
 
                save_menu->addAction("Visible layer data");
2526
 
                save_menu->addAction("As image");
2527
 
 
2528
 
                //add settings menu
2529
 
                context_menu->addMenu(save_menu);
2530
 
                context_menu->addMenu(settings_menu);
2531
 
 
2532
 
                //add external context menu
2533
 
                if (context_add_)
2534
 
                {
2535
 
                        context_menu->addSeparator();
2536
 
                        context_menu->addMenu(context_add_);
2537
 
                }
2538
 
        }
2539
 
 
2540
 
 
2541
 
        void Spectrum2DCanvas::showCurrentLayerPreferences()
2542
 
        {
2543
 
                Internal::Spectrum2DPrefDialog dlg(this);
2544
 
                LayerData& layer = getCurrentLayer_();
2545
 
 
2546
 
                ColorSelector* bg_color = dlg.findChild<ColorSelector*>("bg_color");
2547
 
                QComboBox* mapping = dlg.findChild<QComboBox*>("mapping");
2548
 
                MultiGradientSelector* gradient = dlg.findChild<MultiGradientSelector*>("gradient");
2549
 
                QComboBox* feature_icon = dlg.findChild<QComboBox*>("feature_icon");
2550
 
                QSpinBox* feature_icon_size = dlg.findChild<QSpinBox*>("feature_icon_size");
2551
 
 
2552
 
                bg_color->setColor(QColor(param_.getValue("background_color").toQString()));
2553
 
                if (isMzToXAxis())
2554
 
                {
2555
 
                        mapping->setCurrentIndex(0);
2556
 
                }
2557
 
                else
2558
 
                {
2559
 
                        mapping->setCurrentIndex(1);
2560
 
                }
2561
 
                gradient->gradient().fromString(layer.param.getValue("dot:gradient"));
2562
 
                feature_icon->setCurrentIndex(feature_icon->findText(layer.param.getValue("dot:feature_icon").toQString()));
2563
 
                feature_icon_size->setValue((int)layer.param.getValue("dot:feature_icon_size"));
2564
 
                
2565
 
                if (dlg.exec())
2566
 
                {
2567
 
                        param_.setValue("background_color",bg_color->getColor().name());
2568
 
                        layer.param.setValue("dot:feature_icon", feature_icon->currentText());
2569
 
                        layer.param.setValue("dot:feature_icon_size", feature_icon_size->value());
2570
 
                        if ((mapping->currentIndex()==0 && !isMzToXAxis()) || (mapping->currentIndex()==1 && isMzToXAxis()))
2571
 
                        {
2572
 
                                mzToXAxis(!isMzToXAxis());
2573
 
                        }
2574
 
                        layer.param.setValue("dot:gradient",gradient->gradient().toString());
2575
 
 
2576
 
                  emit preferencesChange();
2577
 
                }
2578
 
        }
 
2573
    }
 
2574
 
 
2575
    e->accept();
 
2576
  }
 
2577
 
 
2578
  void Spectrum2DCanvas::finishContextMenu_(QMenu * context_menu, QMenu * settings_menu)
 
2579
  {
 
2580
    //finish settings menu
 
2581
    settings_menu->addSeparator();
 
2582
    settings_menu->addAction("Preferences");
 
2583
 
 
2584
    //create save menu
 
2585
    QMenu * save_menu = new QMenu("Save");
 
2586
    save_menu->addAction("Layer");
 
2587
    save_menu->addAction("Visible layer data");
 
2588
    save_menu->addAction("As image");
 
2589
 
 
2590
    //add settings menu
 
2591
    context_menu->addMenu(save_menu);
 
2592
    context_menu->addMenu(settings_menu);
 
2593
 
 
2594
    //add external context menu
 
2595
    if (context_add_)
 
2596
    {
 
2597
      context_menu->addSeparator();
 
2598
      context_menu->addMenu(context_add_);
 
2599
    }
 
2600
  }
 
2601
 
 
2602
  void Spectrum2DCanvas::showCurrentLayerPreferences()
 
2603
  {
 
2604
    Internal::Spectrum2DPrefDialog dlg(this);
 
2605
    LayerData & layer = getCurrentLayer_();
 
2606
 
 
2607
    ColorSelector * bg_color = dlg.findChild<ColorSelector *>("bg_color");
 
2608
    QComboBox * mapping = dlg.findChild<QComboBox *>("mapping");
 
2609
    MultiGradientSelector * gradient = dlg.findChild<MultiGradientSelector *>("gradient");
 
2610
    QComboBox * feature_icon = dlg.findChild<QComboBox *>("feature_icon");
 
2611
    QSpinBox * feature_icon_size = dlg.findChild<QSpinBox *>("feature_icon_size");
 
2612
 
 
2613
    bg_color->setColor(QColor(param_.getValue("background_color").toQString()));
 
2614
    if (isMzToXAxis())
 
2615
    {
 
2616
      mapping->setCurrentIndex(0);
 
2617
    }
 
2618
    else
 
2619
    {
 
2620
      mapping->setCurrentIndex(1);
 
2621
    }
 
2622
    gradient->gradient().fromString(layer.param.getValue("dot:gradient"));
 
2623
    feature_icon->setCurrentIndex(feature_icon->findText(layer.param.getValue("dot:feature_icon").toQString()));
 
2624
    feature_icon_size->setValue((int)layer.param.getValue("dot:feature_icon_size"));
 
2625
 
 
2626
    if (dlg.exec())
 
2627
    {
 
2628
      param_.setValue("background_color", bg_color->getColor().name());
 
2629
      layer.param.setValue("dot:feature_icon", feature_icon->currentText());
 
2630
      layer.param.setValue("dot:feature_icon_size", feature_icon_size->value());
 
2631
      if ((mapping->currentIndex() == 0 && !isMzToXAxis()) || (mapping->currentIndex() == 1 && isMzToXAxis()))
 
2632
      {
 
2633
        mzToXAxis(!isMzToXAxis());
 
2634
      }
 
2635
      layer.param.setValue("dot:gradient", gradient->gradient().toString());
 
2636
 
 
2637
      emit preferencesChange();
 
2638
    }
 
2639
  }
2579
2640
 
2580
2641
  void Spectrum2DCanvas::currentLayerParametersChanged_()
2581
 
        {
 
2642
  {
2582
2643
    recalculateDotGradient_(activeLayerIndex());
2583
2644
 
2584
 
                update_buffer_ = true;
2585
 
                update_(__PRETTY_FUNCTION__);
2586
 
        }
 
2645
    update_buffer_ = true;
 
2646
    update_(__PRETTY_FUNCTION__);
 
2647
  }
2587
2648
 
2588
 
        void Spectrum2DCanvas::saveCurrentLayer(bool visible)
2589
 
        {
2590
 
                const LayerData& layer = getCurrentLayer();
 
2649
  void Spectrum2DCanvas::saveCurrentLayer(bool visible)
 
2650
  {
 
2651
    const LayerData & layer = getCurrentLayer();
2591
2652
 
2592
2653
    //determine proposed filename
2593
 
                String proposed_name = param_.getValue("default_path");
2594
 
    if (visible==false && layer.filename!="")
2595
 
    {
2596
 
        proposed_name = layer.filename;
2597
 
    }
2598
 
 
2599
 
                if (layer.type==LayerData::DT_PEAK) //peak data
2600
 
                {
2601
 
                        QString selected_filter = "";
2602
 
        QString file_name = QFileDialog::getSaveFileName(this, "Save file", proposed_name.toQString(),"mzML files (*.mzML);;mzData files (*.mzData);;mzXML files (*.mzXML);;All files (*)", &selected_filter);
2603
 
                        if (!file_name.isEmpty())
2604
 
                        {
2605
 
                                // check whether a file type suffix has been given
2606
 
                                // first check mzData and mzXML then mzML
2607
 
                                // if the setting is at "All files" 
2608
 
                                // mzML will be used
2609
 
                                String upper_filename = file_name;
2610
 
                                upper_filename.toUpper();
2611
 
                                if (selected_filter == "mzData files (*.mzData)")
2612
 
                                {
2613
 
                                        if (!upper_filename.hasSuffix(".MZDATA"))
2614
 
                                        {
2615
 
                                                file_name += ".mzData";
2616
 
                                        }
2617
 
                                }
2618
 
                                else if (selected_filter == "mzXML files (*.mzXML)")
2619
 
                                {
2620
 
                                        if (!upper_filename.hasSuffix(".MZXML"))
2621
 
                                        {
2622
 
                                                file_name += ".mzXML";
2623
 
                                        }
2624
 
                                }
2625
 
                                else 
2626
 
                                {
2627
 
                                        if (!upper_filename.hasSuffix(".MZML"))
2628
 
                                        {
2629
 
                                                file_name += ".mzML";
2630
 
                                        }
2631
 
                                }
2632
 
 
2633
 
                if (visible) //only visible data
2634
 
                {
2635
 
                                        ExperimentType out;
2636
 
                                        getVisiblePeakData(out);
2637
 
                                        addDataProcessing_(out, DataProcessing::FILTERING);
2638
 
                                        FileHandler().storeExperiment(file_name,out,ProgressLogger::GUI);
2639
 
                                }
2640
 
                                else //all data
2641
 
                                {
2642
 
                                        FileHandler().storeExperiment(file_name,*layer.getPeakData(),ProgressLogger::GUI);
2643
 
                                }
2644
 
        modificationStatus_(activeLayerIndex(), false);
2645
 
                        }
2646
 
          }
2647
 
          else if (layer.type==LayerData::DT_FEATURE) //features
2648
 
          {
2649
 
                        QString file_name = QFileDialog::getSaveFileName(this, "Save file", proposed_name.toQString(),"FeatureXML files (*.featureXML);;All files (*)");
2650
 
                        if (!file_name.isEmpty())
2651
 
                        {
2652
 
                                // add suffix ".featureXML" if not given
2653
 
                                String upper_filename = file_name;
2654
 
                                upper_filename.toUpper();
2655
 
                                if (!upper_filename.hasSuffix(".FEATUREXML"))
2656
 
                                {
2657
 
                                        file_name += ".featureXML";
2658
 
                                }
2659
 
                        if (visible) //only visible data
2660
 
                        {
2661
 
                                        FeatureMapType out;
2662
 
                                        getVisibleFeatureData(out);
2663
 
                                        FeatureXMLFile().store(file_name,out);
2664
 
                                }
2665
 
                                else //all data
2666
 
                                {
2667
 
          FeatureXMLFile().store(file_name,*layer.getFeatureMap());
2668
 
                                }
2669
 
        modificationStatus_(activeLayerIndex(), false);
2670
 
                        }
2671
 
          }
2672
 
          else if (layer.type==LayerData::DT_CONSENSUS) //consensus feature data
2673
 
          {
2674
 
                        QString file_name = QFileDialog::getSaveFileName(this, "Save file", proposed_name.toQString(),"ConsensusXML files (*.consensusXML);;All files (*)");
2675
 
                        if (!file_name.isEmpty())
2676
 
                        {
2677
 
                                // add suffix ".consensusXML" if not given
2678
 
                                String upper_filename = file_name;
2679
 
                                upper_filename.toUpper();
2680
 
                                if (!upper_filename.hasSuffix(".CONSENSUSXML"))
2681
 
                                {
2682
 
                                        file_name += ".consensusXML";
2683
 
                                }
2684
 
 
2685
 
                        if (visible) //only visible data
2686
 
                        {
2687
 
                                        ConsensusMapType out;
2688
 
                                        getVisibleConsensusData(out);
2689
 
                                        ConsensusXMLFile().store(file_name,out);
2690
 
                                }
2691
 
                                else //all data
2692
 
                                {
2693
 
          ConsensusXMLFile().store(file_name,*layer.getConsensusMap());
2694
 
                                }
2695
 
        modificationStatus_(activeLayerIndex(), false);
2696
 
                        }
2697
 
          }
2698
 
          else if (layer.type==LayerData::DT_CHROMATOGRAM) //chromatograms
2699
 
          {
2700
 
                        //TODO CHROM
2701
 
                }
2702
 
        }
 
2654
    String proposed_name = param_.getValue("default_path");
 
2655
    if (visible == false && layer.filename != "")
 
2656
    {
 
2657
      proposed_name = layer.filename;
 
2658
    }
 
2659
 
 
2660
    if (layer.type == LayerData::DT_PEAK)   //peak data
 
2661
    {
 
2662
      QString selected_filter = "";
 
2663
      QString file_name = QFileDialog::getSaveFileName(this, "Save file", proposed_name.toQString(), "mzML files (*.mzML);;mzData files (*.mzData);;mzXML files (*.mzXML);;All files (*)", &selected_filter);
 
2664
      if (!file_name.isEmpty())
 
2665
      {
 
2666
        // check whether a file type suffix has been given
 
2667
        // first check mzData and mzXML then mzML
 
2668
        // if the setting is at "All files"
 
2669
        // mzML will be used
 
2670
        String upper_filename = file_name;
 
2671
        upper_filename.toUpper();
 
2672
        if (selected_filter == "mzData files (*.mzData)")
 
2673
        {
 
2674
          if (!upper_filename.hasSuffix(".MZDATA"))
 
2675
          {
 
2676
            file_name += ".mzData";
 
2677
          }
 
2678
        }
 
2679
        else if (selected_filter == "mzXML files (*.mzXML)")
 
2680
        {
 
2681
          if (!upper_filename.hasSuffix(".MZXML"))
 
2682
          {
 
2683
            file_name += ".mzXML";
 
2684
          }
 
2685
        }
 
2686
        else
 
2687
        {
 
2688
          if (!upper_filename.hasSuffix(".MZML"))
 
2689
          {
 
2690
            file_name += ".mzML";
 
2691
          }
 
2692
        }
 
2693
 
 
2694
        if (visible)     //only visible data
 
2695
        {
 
2696
          ExperimentType out;
 
2697
          getVisiblePeakData(out);
 
2698
          addDataProcessing_(out, DataProcessing::FILTERING);
 
2699
          FileHandler().storeExperiment(file_name, out, ProgressLogger::GUI);
 
2700
        }
 
2701
        else         //all data
 
2702
        {
 
2703
          FileHandler().storeExperiment(file_name, *layer.getPeakData(), ProgressLogger::GUI);
 
2704
        }
 
2705
        modificationStatus_(activeLayerIndex(), false);
 
2706
      }
 
2707
    }
 
2708
    else if (layer.type == LayerData::DT_FEATURE) //features
 
2709
    {
 
2710
      QString file_name = QFileDialog::getSaveFileName(this, "Save file", proposed_name.toQString(), "featureXML files (*.featureXML);;All files (*)");
 
2711
      if (!file_name.isEmpty())
 
2712
      {
 
2713
        // add suffix ".featureXML" if not given
 
2714
        String upper_filename = file_name;
 
2715
        upper_filename.toUpper();
 
2716
        if (!upper_filename.hasSuffix(".FEATUREXML"))
 
2717
        {
 
2718
          file_name += ".featureXML";
 
2719
        }
 
2720
        if (visible)     //only visible data
 
2721
        {
 
2722
          FeatureMapType out;
 
2723
          getVisibleFeatureData(out);
 
2724
          FeatureXMLFile().store(file_name, out);
 
2725
        }
 
2726
        else         //all data
 
2727
        {
 
2728
          FeatureXMLFile().store(file_name, *layer.getFeatureMap());
 
2729
        }
 
2730
        modificationStatus_(activeLayerIndex(), false);
 
2731
      }
 
2732
    }
 
2733
    else if (layer.type == LayerData::DT_CONSENSUS) //consensus feature data
 
2734
    {
 
2735
      QString file_name = QFileDialog::getSaveFileName(this, "Save file", proposed_name.toQString(), "consensusXML files (*.consensusXML);;All files (*)");
 
2736
      if (!file_name.isEmpty())
 
2737
      {
 
2738
        // add suffix ".consensusXML" if not given
 
2739
        String upper_filename = file_name;
 
2740
        upper_filename.toUpper();
 
2741
        if (!upper_filename.hasSuffix(".CONSENSUSXML"))
 
2742
        {
 
2743
          file_name += ".consensusXML";
 
2744
        }
 
2745
 
 
2746
        if (visible)     //only visible data
 
2747
        {
 
2748
          ConsensusMapType out;
 
2749
          getVisibleConsensusData(out);
 
2750
          ConsensusXMLFile().store(file_name, out);
 
2751
        }
 
2752
        else         //all data
 
2753
        {
 
2754
          ConsensusXMLFile().store(file_name, *layer.getConsensusMap());
 
2755
        }
 
2756
        modificationStatus_(activeLayerIndex(), false);
 
2757
      }
 
2758
    }
 
2759
    else if (layer.type == LayerData::DT_CHROMATOGRAM) //chromatograms
 
2760
    {
 
2761
      //TODO CHROM
 
2762
    }
 
2763
  }
2703
2764
 
2704
2765
  void Spectrum2DCanvas::updateLayer(Size i)
2705
 
        {    
 
2766
  {
2706
2767
    //update nearest peak
2707
2768
    selected_peak_.clear();
2708
 
                recalculateRanges_(0,1,2);
2709
 
                resetZoom(false); //no repaint as this is done in intensityModeChange_() anyway
2710
 
                intensityModeChange_();
 
2769
    recalculateRanges_(0, 1, 2);
 
2770
    resetZoom(false);     //no repaint as this is done in intensityModeChange_() anyway
 
2771
    intensityModeChange_();
2711
2772
    modificationStatus_(i, false);
2712
 
        }
2713
 
 
2714
 
        void Spectrum2DCanvas::translateLeft_()
2715
 
        {
2716
 
                DoubleReal shift = 0.05 * visible_area_.width();
2717
 
                DoubleReal newLo = visible_area_.minX() - shift;
2718
 
                DoubleReal newHi = visible_area_.maxX() - shift;
2719
 
                // check if we are falling out of bounds
2720
 
                if (newLo < overall_data_range_.minX())
2721
 
                {
2722
 
                        newLo = overall_data_range_.minX();
2723
 
                        newHi = newLo + visible_area_.width();
2724
 
                }
2725
 
                //change visible area
2726
 
                changeVisibleArea_(AreaType(newLo,visible_area_.minY(),newHi,visible_area_.maxY()));
2727
 
        }
2728
 
 
2729
 
        void Spectrum2DCanvas::translateRight_()
2730
 
        {
2731
 
                DoubleReal shift = 0.05 * visible_area_.width();
2732
 
                DoubleReal newLo = visible_area_.minX() + shift;
2733
 
                DoubleReal newHi = visible_area_.maxX() + shift;
2734
 
                // check if we are falling out of bounds
2735
 
                if (newHi > overall_data_range_.maxX())
2736
 
                {
2737
 
                        newHi = overall_data_range_.maxX();
2738
 
                        newLo = newHi - visible_area_.width();
2739
 
                }
2740
 
                //change visible area
2741
 
                changeVisibleArea_(AreaType(newLo,visible_area_.minY(),newHi,visible_area_.maxY()));
2742
 
        }
2743
 
 
2744
 
        void Spectrum2DCanvas::translateForward_()
2745
 
        {
2746
 
                DoubleReal shift = 0.05 * visible_area_.height();
2747
 
                DoubleReal newLo = visible_area_.minY() + shift;
2748
 
                DoubleReal newHi = visible_area_.maxY() + shift;
2749
 
                // check if we are falling out of bounds
2750
 
                if (newHi > overall_data_range_.maxY())
2751
 
                {
2752
 
                        newHi = overall_data_range_.maxY();
2753
 
                        newLo = newHi - visible_area_.height();
2754
 
                }
2755
 
                //change visible area
2756
 
                changeVisibleArea_(AreaType(visible_area_.minX(),newLo,visible_area_.maxX(),newHi));
2757
 
        }
2758
 
 
2759
 
        void Spectrum2DCanvas::translateBackward_()
2760
 
        {
2761
 
                DoubleReal shift = 0.05 * visible_area_.height();
2762
 
                DoubleReal newLo = visible_area_.minY() - shift;
2763
 
                DoubleReal newHi = visible_area_.maxY() - shift;
2764
 
                // check if we are falling out of bounds
2765
 
                if (newLo < overall_data_range_.minY())
2766
 
                {
2767
 
                        newLo = overall_data_range_.minY();
2768
 
                        newHi = newLo + visible_area_.height();
2769
 
                }
2770
 
                //change visible area
2771
 
                changeVisibleArea_(AreaType(visible_area_.minX(),newLo,visible_area_.maxX(),newHi));
2772
 
        }
2773
 
 
2774
 
 
2775
 
        void Spectrum2DCanvas::keyPressEvent(QKeyEvent* e)
2776
 
        {
2777
 
                // CTRL+ALT+H => hidden action
2778
 
                if ((e->modifiers() & Qt::ControlModifier) && (e->modifiers() & Qt::AltModifier) && (e->key()==Qt::Key_H))
2779
 
                {
2780
 
                        /*
2781
 
                        //Scaling with file size (layers)
2782
 
                        for (UInt i=0; i<getLayerCount(); ++i)
2783
 
                        {
2784
 
                                QTime timer;
2785
 
                                timer.start();
2786
 
                                for (UInt j=0; j<10; ++j)
2787
 
                                {
2788
 
                                        QPainter painter(&buffer_);
2789
 
                                        paintDots_(i, painter);
2790
 
                                }
2791
 
                                cout << "peaks: " << getLayer(i).peaks.getSize() << " time: " << timer.elapsed() / 10.0 << endl;
2792
 
                        }
2793
 
                        
2794
 
                        //Scaling with resolution
2795
 
                        for (UInt i=250; i<3001; i+=250)
2796
 
                        {
2797
 
                                resize(i,i);
2798
 
                                QTime timer;
2799
 
                                timer.start();
2800
 
                                QPainter painter(&buffer_);
2801
 
                                paintDots_(0, painter);
2802
 
                                cout << "pixels: " << i << " time: " << timer.elapsed() << endl;
2803
 
                        }
2804
 
                        */
2805
 
 
2806
 
                        e->accept();
2807
 
                        return;
2808
 
                }
2809
 
 
2810
 
                // Delete features
2811
 
                LayerData& layer = getCurrentLayer_();
2812
 
                if (getCurrentLayer().modifiable && layer.type==LayerData::DT_FEATURE && selected_peak_.isValid() && e->key()==Qt::Key_Delete)
2813
 
                {
2814
 
      layer.getFeatureMap()->erase(layer.getFeatureMap()->begin()+selected_peak_.peak);
2815
 
                        selected_peak_.clear();
2816
 
                        update_buffer_ = true;
2817
 
                        update_(__PRETTY_FUNCTION__);
2818
 
                        e->accept();
 
2773
  }
 
2774
 
 
2775
  void Spectrum2DCanvas::translateVisibleArea_( DoubleReal mzShiftRel, DoubleReal rtShiftRel )
 
2776
  {
 
2777
    DoubleReal rtShift = rtShiftRel * visible_area_.height();
 
2778
    DoubleReal mzShift = mzShiftRel * visible_area_.width();
 
2779
    AreaType newArea( visible_area_ );
 
2780
    // shift the visible area avoiding moving out data range bounds
 
2781
    if ( mzShift > 0 ) {
 
2782
        newArea.setMaxX( qMin( overall_data_range_.maxX(), visible_area_.maxX() + mzShift ) );
 
2783
        newArea.setMinX( qMax( overall_data_range_.minX(), newArea.maxX() - visible_area_.width() ) );
 
2784
    } else {
 
2785
        newArea.setMinX( qMax( overall_data_range_.minX(), visible_area_.minX() + mzShift ) );
 
2786
        newArea.setMaxX( qMin( overall_data_range_.maxX(), newArea.minX() + visible_area_.width() ) );
 
2787
    }
 
2788
    if ( rtShift > 0 ) {
 
2789
        newArea.setMaxY( qMin( overall_data_range_.maxY(), visible_area_.maxY() + rtShift ) );
 
2790
        newArea.setMinY( qMax( overall_data_range_.minY(), newArea.maxY() - visible_area_.height() ) );
 
2791
    } else {
 
2792
        newArea.setMinY( qMax( overall_data_range_.minY(), visible_area_.minY() + rtShift ) );
 
2793
        newArea.setMaxY( qMin( overall_data_range_.maxY(), newArea.minY() + visible_area_.height() ) );
 
2794
    }
 
2795
    //change visible area
 
2796
    changeVisibleArea_(newArea);
 
2797
    emit layerZoomChanged(this);
 
2798
  }
 
2799
 
 
2800
  void Spectrum2DCanvas::translateLeft_()
 
2801
  {
 
2802
    if ( isMzToXAxis() ) translateVisibleArea_( -0.05, 0.0 );
 
2803
    else translateVisibleArea_( 0.0, -0.05 );
 
2804
  }
 
2805
 
 
2806
  void Spectrum2DCanvas::translateRight_()
 
2807
  {
 
2808
    if ( isMzToXAxis() ) translateVisibleArea_( 0.05, 0.0 );
 
2809
    else translateVisibleArea_( 0.0, 0.05 );
 
2810
  }
 
2811
 
 
2812
  void Spectrum2DCanvas::translateForward_()
 
2813
  {
 
2814
    if ( isMzToXAxis() ) translateVisibleArea_( 0.0, 0.05 );
 
2815
    else translateVisibleArea_( 0.05, 0.0 );
 
2816
  }
 
2817
 
 
2818
  void Spectrum2DCanvas::translateBackward_()
 
2819
  {
 
2820
    if ( isMzToXAxis() ) translateVisibleArea_( 0.0, -0.05 );
 
2821
    else translateVisibleArea_( -0.05, 0.0 );
 
2822
  }
 
2823
 
 
2824
  void Spectrum2DCanvas::keyPressEvent(QKeyEvent * e)
 
2825
  {
 
2826
    // CTRL+ALT+H => hidden action
 
2827
    if ((e->modifiers() & Qt::ControlModifier) && (e->modifiers() & Qt::AltModifier) && (e->key() == Qt::Key_H))
 
2828
    {
 
2829
      /*
 
2830
      //Scaling with file size (layers)
 
2831
      for (UInt i=0; i<getLayerCount(); ++i)
 
2832
      {
 
2833
          QTime timer;
 
2834
          timer.start();
 
2835
          for (UInt j=0; j<10; ++j)
 
2836
          {
 
2837
              QPainter painter(&buffer_);
 
2838
              paintDots_(i, painter);
 
2839
          }
 
2840
          cout << "peaks: " << getLayer(i).peaks.getSize() << " time: " << timer.elapsed() / 10.0 << endl;
 
2841
      }
 
2842
 
 
2843
      //Scaling with resolution
 
2844
      for (UInt i=250; i<3001; i+=250)
 
2845
      {
 
2846
          resize(i,i);
 
2847
          QTime timer;
 
2848
          timer.start();
 
2849
          QPainter painter(&buffer_);
 
2850
          paintDots_(0, painter);
 
2851
          cout << "pixels: " << i << " time: " << timer.elapsed() << endl;
 
2852
      }
 
2853
      */
 
2854
 
 
2855
      e->accept();
 
2856
      return;
 
2857
    }
 
2858
 
 
2859
    // Delete features
 
2860
    LayerData & layer = getCurrentLayer_();
 
2861
    if (getCurrentLayer().modifiable && layer.type == LayerData::DT_FEATURE && selected_peak_.isValid() && e->key() == Qt::Key_Delete)
 
2862
    {
 
2863
      layer.getFeatureMap()->erase(layer.getFeatureMap()->begin() + selected_peak_.peak);
 
2864
      selected_peak_.clear();
 
2865
      update_buffer_ = true;
 
2866
      update_(__PRETTY_FUNCTION__);
 
2867
      e->accept();
2819
2868
 
2820
2869
      modificationStatus_(activeLayerIndex(), true);
2821
 
                }
2822
 
                else
2823
 
                {
2824
 
                        SpectrumCanvas::keyPressEvent(e);
2825
 
                }
2826
 
        }
2827
 
 
2828
 
        void Spectrum2DCanvas::keyReleaseEvent(QKeyEvent* e)
2829
 
        {
2830
 
                //zoom if in zoom mode and a valid rectangle is selected
2831
 
                if (action_mode_==AM_ZOOM && rubber_band_.isVisible())
2832
 
                {
2833
 
                        rubber_band_.hide();
2834
 
                        QRect rect = rubber_band_.geometry();
2835
 
                        if (rect.width()!=0 && rect.height()!=0)
2836
 
                        {
2837
 
                                AreaType area(widgetToData_(rect.topLeft()), widgetToData_(rect.bottomRight()));
2838
 
                                changeVisibleArea_(area, true, true);
2839
 
                        }
2840
 
                }
2841
 
                else if (action_mode_==AM_MEASURE)
2842
 
                {
2843
 
                        measurement_start_.clear();
2844
 
                        update_(__PRETTY_FUNCTION__);
2845
 
                }
2846
 
 
2847
 
                //do the normal stuff
2848
 
                SpectrumCanvas::keyReleaseEvent(e);
2849
 
        }
2850
 
 
2851
 
        void Spectrum2DCanvas::mouseDoubleClickEvent(QMouseEvent* e)
2852
 
        {
2853
 
                LayerData& current_layer = getCurrentLayer_();
2854
 
                
2855
 
                if (current_layer.modifiable && current_layer.type==LayerData::DT_FEATURE)
2856
 
                {
2857
 
                        Feature tmp;
2858
 
                        if (selected_peak_.isValid()) //edit existing feature
2859
 
                        {
2860
 
                                FeatureEditDialog dialog(this);
 
2870
    }
 
2871
    else
 
2872
    {
 
2873
      SpectrumCanvas::keyPressEvent(e);
 
2874
    }
 
2875
  }
 
2876
 
 
2877
  void Spectrum2DCanvas::keyReleaseEvent(QKeyEvent * e)
 
2878
  {
 
2879
    //zoom if in zoom mode and a valid rectangle is selected
 
2880
    if (action_mode_ == AM_ZOOM && rubber_band_.isVisible())
 
2881
    {
 
2882
      rubber_band_.hide();
 
2883
      QRect rect = rubber_band_.geometry();
 
2884
      if (rect.width() != 0 && rect.height() != 0)
 
2885
      {
 
2886
        AreaType area(widgetToData_(rect.topLeft()), widgetToData_(rect.bottomRight()));
 
2887
        changeVisibleArea_(area, true, true);
 
2888
        emit layerZoomChanged(this);
 
2889
      }
 
2890
    }
 
2891
    else if (action_mode_ == AM_MEASURE)
 
2892
    {
 
2893
      measurement_start_.clear();
 
2894
      update_(__PRETTY_FUNCTION__);
 
2895
    }
 
2896
 
 
2897
    //do the normal stuff
 
2898
    SpectrumCanvas::keyReleaseEvent(e);
 
2899
  }
 
2900
 
 
2901
  void Spectrum2DCanvas::mouseDoubleClickEvent(QMouseEvent * e)
 
2902
  {
 
2903
    LayerData & current_layer = getCurrentLayer_();
 
2904
 
 
2905
    if (current_layer.modifiable && current_layer.type == LayerData::DT_FEATURE)
 
2906
    {
 
2907
      Feature tmp;
 
2908
      if (selected_peak_.isValid())       //edit existing feature
 
2909
      {
 
2910
        FeatureEditDialog dialog(this);
2861
2911
        dialog.setFeature((*current_layer.getFeatureMap())[selected_peak_.peak]);
2862
 
                                if (dialog.exec())
2863
 
                                {
2864
 
                                        tmp = dialog.getFeature();
 
2912
        if (dialog.exec())
 
2913
        {
 
2914
          tmp = dialog.getFeature();
2865
2915
          (*current_layer.getFeatureMap())[selected_peak_.peak] = tmp;
2866
 
                                }
2867
 
                        }
2868
 
                        else //create new feature
2869
 
                        {
2870
 
                                tmp.setRT(widgetToData_(e->pos())[1]);
2871
 
                                tmp.setMZ(widgetToData_(e->pos())[0]);
2872
 
                                FeatureEditDialog dialog(this);
2873
 
                                dialog.setFeature(tmp);
2874
 
                                if (dialog.exec())
2875
 
                                {
2876
 
                                        tmp = dialog.getFeature();
 
2916
        }
 
2917
      }
 
2918
      else       //create new feature
 
2919
      {
 
2920
        tmp.setRT(widgetToData_(e->pos())[1]);
 
2921
        tmp.setMZ(widgetToData_(e->pos())[0]);
 
2922
        FeatureEditDialog dialog(this);
 
2923
        dialog.setFeature(tmp);
 
2924
        if (dialog.exec())
 
2925
        {
 
2926
          tmp = dialog.getFeature();
2877
2927
          current_layer.getFeatureMap()->push_back(tmp);
2878
 
                                }
2879
 
                        }
2880
 
                        
2881
 
                        //update gradient if the min/max intensity changes
2882
 
      if (tmp.getIntensity()<current_layer.getFeatureMap()->getMinInt() || tmp.getIntensity()>current_layer.getFeatureMap()->getMaxInt())
2883
 
                        {
 
2928
        }
 
2929
      }
 
2930
 
 
2931
      //update gradient if the min/max intensity changes
 
2932
      if (tmp.getIntensity() < current_layer.getFeatureMap()->getMinInt() || tmp.getIntensity() > current_layer.getFeatureMap()->getMaxInt())
 
2933
      {
2884
2934
        current_layer.getFeatureMap()->updateRanges();
2885
 
                                recalculateRanges_(0,1,2);
2886
 
                                intensityModeChange_();
2887
 
                        }
2888
 
                        else //just repaint to show the changes
2889
 
                        {
2890
 
                                update_buffer_ = true;
2891
 
                                update_(__PRETTY_FUNCTION__);
2892
 
                        }
2893
 
                        
 
2935
        recalculateRanges_(0, 1, 2);
 
2936
        intensityModeChange_();
 
2937
      }
 
2938
      else       //just repaint to show the changes
 
2939
      {
 
2940
        update_buffer_ = true;
 
2941
        update_(__PRETTY_FUNCTION__);
 
2942
      }
 
2943
 
2894
2944
      modificationStatus_(activeLayerIndex(), true);
2895
 
                }
2896
 
        }
 
2945
    }
 
2946
  }
2897
2947
 
2898
2948
  void Spectrum2DCanvas::mergeIntoLayer(Size i, FeatureMapSharedPtrType map)
2899
 
        {
2900
 
                OPENMS_PRECONDITION(i < layers_.size(), "Spectrum2DCanvas::mergeIntoLayer(i, map) index overflow");
2901
 
                OPENMS_PRECONDITION(layers_[i].type==LayerData::DT_FEATURE, "Spectrum2DCanvas::mergeIntoLayer(i, map) non-feature layer selected");
2902
 
                //reserve enough space
2903
 
    layers_[i].getFeatureMap()->reserve(layers_[i].getFeatureMap()->size()+map->size());
2904
 
                //add features
2905
 
    for (Size j=0; j<map->size(); ++j)
2906
 
                {
 
2949
  {
 
2950
    OPENMS_PRECONDITION(i < layers_.size(), "Spectrum2DCanvas::mergeIntoLayer(i, map) index overflow");
 
2951
    OPENMS_PRECONDITION(layers_[i].type == LayerData::DT_FEATURE, "Spectrum2DCanvas::mergeIntoLayer(i, map) non-feature layer selected");
 
2952
    //reserve enough space
 
2953
    layers_[i].getFeatureMap()->reserve(layers_[i].getFeatureMap()->size() + map->size());
 
2954
    //add features
 
2955
    for (Size j = 0; j < map->size(); ++j)
 
2956
    {
2907
2957
      layers_[i].getFeatureMap()->push_back((*map)[j]);
2908
 
                }
2909
 
                //update the layer and overall ranges (if necessary)
 
2958
    }
 
2959
    //update the layer and overall ranges (if necessary)
2910
2960
    RangeManager<2>::PositionType min_pos_old = layers_[i].getFeatureMap()->getMin();
2911
2961
    RangeManager<2>::PositionType max_pos_old = layers_[i].getFeatureMap()->getMax();
2912
2962
    DoubleReal min_int_old = layers_[i].getFeatureMap()->getMinInt();
2913
2963
    DoubleReal max_int_old = layers_[i].getFeatureMap()->getMaxInt();
2914
2964
    layers_[i].getFeatureMap()->updateRanges();
2915
 
    if(min_pos_old>layers_[i].getFeatureMap()->getMin() || max_pos_old<layers_[i].getFeatureMap()->getMax())
2916
 
                {
2917
 
                        recalculateRanges_(0,1,2);
2918
 
                        resetZoom(true);
2919
 
                }
2920
 
    if(min_int_old>layers_[i].getFeatureMap()->getMinInt() || max_int_old<layers_[i].getFeatureMap()->getMaxInt())
2921
 
                {
2922
 
                        intensityModeChange_();
2923
 
                }
2924
 
        }
 
2965
    if (min_pos_old > layers_[i].getFeatureMap()->getMin() || max_pos_old < layers_[i].getFeatureMap()->getMax())
 
2966
    {
 
2967
      recalculateRanges_(0, 1, 2);
 
2968
      resetZoom(true);
 
2969
    }
 
2970
    if (min_int_old > layers_[i].getFeatureMap()->getMinInt() || max_int_old < layers_[i].getFeatureMap()->getMaxInt())
 
2971
    {
 
2972
      intensityModeChange_();
 
2973
    }
 
2974
  }
2925
2975
 
2926
2976
  void Spectrum2DCanvas::mergeIntoLayer(Size i, ConsensusMapSharedPtrType map)
2927
 
        {
2928
 
                OPENMS_PRECONDITION(i < layers_.size(), "Spectrum2DCanvas::mergeIntoLayer(i, map) index overflow");
2929
 
                OPENMS_PRECONDITION(layers_[i].type==LayerData::DT_CONSENSUS, "Spectrum2DCanvas::mergeIntoLayer(i, map) non-consensus-feature layer selected");
2930
 
                //reserve enough space
2931
 
    layers_[i].getConsensusMap()->reserve(layers_[i].getFeatureMap()->size()+map->size());
2932
 
                //add features
2933
 
    for (Size j=0; j<map->size(); ++j)
2934
 
                {
 
2977
  {
 
2978
    OPENMS_PRECONDITION(i < layers_.size(), "Spectrum2DCanvas::mergeIntoLayer(i, map) index overflow");
 
2979
    OPENMS_PRECONDITION(layers_[i].type == LayerData::DT_CONSENSUS, "Spectrum2DCanvas::mergeIntoLayer(i, map) non-consensus-feature layer selected");
 
2980
    //reserve enough space
 
2981
    layers_[i].getConsensusMap()->reserve(layers_[i].getFeatureMap()->size() + map->size());
 
2982
    //add features
 
2983
    for (Size j = 0; j < map->size(); ++j)
 
2984
    {
2935
2985
      layers_[i].getConsensusMap()->push_back((*map)[j]);
2936
 
                }
2937
 
                //update the layer and overall ranges (if necessary)
 
2986
    }
 
2987
    //update the layer and overall ranges (if necessary)
2938
2988
    RangeManager<2>::PositionType min_pos_old = layers_[i].getConsensusMap()->getMin();
2939
2989
    RangeManager<2>::PositionType max_pos_old = layers_[i].getConsensusMap()->getMax();
2940
2990
    DoubleReal min_int_old = layers_[i].getConsensusMap()->getMinInt();
2941
2991
    DoubleReal max_int_old = layers_[i].getConsensusMap()->getMaxInt();
2942
2992
    layers_[i].getConsensusMap()->updateRanges();
2943
 
    if(min_pos_old>layers_[i].getConsensusMap()->getMin() || max_pos_old<layers_[i].getConsensusMap()->getMax())
2944
 
                {
2945
 
                        recalculateRanges_(0,1,2);
2946
 
                        resetZoom(true);
2947
 
                }
2948
 
    if(min_int_old>layers_[i].getConsensusMap()->getMinInt() || max_int_old<layers_[i].getConsensusMap()->getMaxInt())
2949
 
                {
2950
 
                        intensityModeChange_();
2951
 
                }
2952
 
        }
 
2993
    if (min_pos_old > layers_[i].getConsensusMap()->getMin() || max_pos_old < layers_[i].getConsensusMap()->getMax())
 
2994
    {
 
2995
      recalculateRanges_(0, 1, 2);
 
2996
      resetZoom(true);
 
2997
    }
 
2998
    if (min_int_old > layers_[i].getConsensusMap()->getMinInt() || max_int_old < layers_[i].getConsensusMap()->getMaxInt())
 
2999
    {
 
3000
      intensityModeChange_();
 
3001
    }
 
3002
  }
2953
3003
 
2954
 
        void Spectrum2DCanvas::mergeIntoLayer(Size i, vector<PeptideIdentification>& peptides)
2955
 
        {
2956
 
                OPENMS_PRECONDITION(i < layers_.size(), "Spectrum2DCanvas::mergeIntoLayer(i, peptides) index overflow");
2957
 
                OPENMS_PRECONDITION(layers_[i].type==LayerData::DT_IDENT, "Spectrum2DCanvas::mergeIntoLayer(i, peptides) non-identification layer selected");
2958
 
                // reserve enough space
2959
 
                layers_[i].peptides.reserve(layers_[i].peptides.size() + peptides.size());
2960
 
                // insert peptides
2961
 
                layers_[i].peptides.insert(layers_[i].peptides.end(), peptides.begin(), 
2962
 
                                                                                                                         peptides.end());
2963
 
                // update the layer and overall ranges
2964
 
                recalculateRanges_(0,1,2);
2965
 
                resetZoom(true);
2966
 
        }
 
3004
  void Spectrum2DCanvas::mergeIntoLayer(Size i, vector<PeptideIdentification> & peptides)
 
3005
  {
 
3006
    OPENMS_PRECONDITION(i < layers_.size(), "Spectrum2DCanvas::mergeIntoLayer(i, peptides) index overflow");
 
3007
    OPENMS_PRECONDITION(layers_[i].type == LayerData::DT_IDENT, "Spectrum2DCanvas::mergeIntoLayer(i, peptides) non-identification layer selected");
 
3008
    // reserve enough space
 
3009
    layers_[i].peptides.reserve(layers_[i].peptides.size() + peptides.size());
 
3010
    // insert peptides
 
3011
    layers_[i].peptides.insert(layers_[i].peptides.end(), peptides.begin(),
 
3012
                               peptides.end());
 
3013
    // update the layer and overall ranges
 
3014
    recalculateRanges_(0, 1, 2);
 
3015
    resetZoom(true);
 
3016
  }
2967
3017
 
2968
3018
} //namespace OpenMS
2969