File: HistogramWidget.C

package info (click to toggle)
openms 1.11.1-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 436,688 kB
  • ctags: 150,907
  • sloc: cpp: 387,126; xml: 71,547; python: 7,764; ansic: 2,626; php: 2,499; sql: 737; ruby: 342; sh: 325; makefile: 128
file content (341 lines) | stat: -rw-r--r-- 10,708 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// --------------------------------------------------------------------------
//                   OpenMS -- Open-Source Mass Spectrometry
// --------------------------------------------------------------------------
// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
// ETH Zurich, and Freie Universitaet Berlin 2002-2013.
//
// This software is released under a three-clause BSD license:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of any author or any participating institution
//    may be used to endorse or promote products derived from this software
//    without specific prior written permission.
// For a full list of authors, refer to the file AUTHORS.
// --------------------------------------------------------------------------
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
// INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// --------------------------------------------------------------------------
// $Maintainer: Timo Sachsenberg $
// $Authors: Marc Sturm $
// --------------------------------------------------------------------------


// Qt
#include <QtGui/QResizeEvent>
#include <QtGui/QMouseEvent>
#include <QtGui/QPaintEvent>
#include <QtGui/QPainter>
#include <QtGui/QMenu>

// STL
#include <iostream>

// OpenMS
#include <OpenMS/VISUAL/HistogramWidget.h>
#include <OpenMS/VISUAL/AxisWidget.h>

using namespace std;

namespace OpenMS
{
  using namespace Math;

  HistogramWidget::HistogramWidget(const Histogram<> & distribution, QWidget * parent) :
    QWidget(parent),
    dist_(distribution),
    show_splitters_(false),
    moving_splitter_(0),
    margin_(30),
    buffer_(),
    log_mode_(false)
  {
    left_splitter_ =  dist_.minBound();
    right_splitter_ = dist_.maxBound();
    setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    setMinimumSize(600, 450);
    bottom_axis_ = new AxisWidget(AxisPainter::BOTTOM, "", this);
    bottom_axis_->setMargin(margin_);
    bottom_axis_->setTickLevel(2);
    bottom_axis_->setAxisBounds(dist_.minBound(), dist_.maxBound());

    //signals and slots
    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(showContextMenu(const QPoint &)));
  }

  HistogramWidget::~HistogramWidget()
  {
    delete(bottom_axis_);
  }

  DoubleReal HistogramWidget::getLeftSplitter()
  {
    return left_splitter_;
  }

  DoubleReal HistogramWidget::getRightSplitter()
  {
    return right_splitter_;
  }

  void HistogramWidget::showSplitters(bool on)
  {
    show_splitters_ = on;
  }

  void HistogramWidget::setRightSplitter(DoubleReal pos)
  {
    right_splitter_ = min(dist_.maxBound(), pos);
  }

  void HistogramWidget::setLeftSplitter(DoubleReal pos)
  {
    left_splitter_ = max(dist_.minBound(), pos);
  }

  void HistogramWidget::setLegend(const String & legend)
  {
    bottom_axis_->setLegend(legend);
  }

  void HistogramWidget::mousePressEvent(QMouseEvent * e)
  {
    if (show_splitters_ && e->button() == Qt::LeftButton)
    {
      //left
      Int p = margin_ + UInt(((left_splitter_ - dist_.minBound()) / (dist_.maxBound() - dist_.minBound())) * (width() - 2 * margin_));
      //cout << "Mouse: " << e->x() << " p: " << p << " splitter: " << left_splitter_ << endl;
      if (e->x() >= p && e->x() <= p + 5)
      {
        moving_splitter_ = 1;
      }

      //right
      p = margin_ + UInt(((right_splitter_ - dist_.minBound()) / (dist_.maxBound() - dist_.minBound())) * (width() - 2 * margin_));
      if (e->x() <= p && e->x() >= p - 5)
      {
        moving_splitter_ = 2;
      }
    }
    else
    {
      e->ignore();
    }
  }

  void HistogramWidget::mouseMoveEvent(QMouseEvent * e)
  {
    if (show_splitters_ && (e->buttons() & Qt::LeftButton))
    {
      //left
      if (moving_splitter_ == 1)
      {
        left_splitter_ = DoubleReal(Int(e->x()) - Int(margin_)) / (width() - 2 * margin_) * (dist_.maxBound() - dist_.minBound()) + dist_.minBound();
        //upper bound
        if (left_splitter_ > right_splitter_ - (dist_.maxBound() - dist_.minBound()) / 50.0)
        {
          left_splitter_ = right_splitter_ - (dist_.maxBound() - dist_.minBound()) / 50.0;
        }
        //lower bound
        if (left_splitter_ < dist_.minBound())
        {
          left_splitter_ = dist_.minBound();
        }
        update();
      }

      //right
      if (moving_splitter_ == 2)
      {

        right_splitter_ = DoubleReal(Int(e->x()) - Int(margin_)) / (width() - 2 * margin_ + 2) * (dist_.maxBound() - dist_.minBound()) + dist_.minBound();
        //upper bound
        if (right_splitter_ < left_splitter_ + (dist_.maxBound() - dist_.minBound()) / 50.0)
        {
          right_splitter_ = left_splitter_ + (dist_.maxBound() - dist_.minBound()) / 50.0;
        }
        //lower bound
        if (right_splitter_ > dist_.maxBound())
        {
          right_splitter_ = dist_.maxBound();
        }
        update();
      }
    }
    else
    {
      e->ignore();
    }
  }

  void HistogramWidget::mouseReleaseEvent(QMouseEvent * e)
  {
    if (show_splitters_)
    {
      moving_splitter_ = 0;
    }
    else
    {
      e->ignore();
    }
  }

  void HistogramWidget::paintEvent(QPaintEvent * /*e*/)
  {
    //histogram from buffer
    QPainter painter2(this);
    painter2.drawPixmap(margin_, 0, buffer_);

    //y-axis label
    painter2.rotate(270);
    painter2.setPen(Qt::black);
    QString label = "count";
    if (log_mode_)
      label = "log ( count )";
    painter2.drawText(0, 0, -height(), margin_, Qt::AlignHCenter | Qt::AlignVCenter, label);
    painter2.end();

    //draw splitters
    if (show_splitters_)
    {
      QPainter painter(this);
      painter.setPen(Qt::black);
      QFont label_font;
      label_font.setPointSize(8);

      //cout << "Left splitter: " << left_splitter_<< " dist: " << dist_.minBound() << endl;
      //cout << "Right splitter: " << right_splitter_<< " dist: " << dist_.maxBound() << endl;

      //left
      UInt p =  UInt(((left_splitter_ - dist_.minBound()) / (dist_.maxBound() - dist_.minBound())) * (width() - 2 * margin_)) + margin_;
      //cout << "Left splitter position: " << p << endl;
      painter.drawLine(p, margin_ - 8, p, height() - bottom_axis_->height());
      painter.drawLine(p, margin_ - 8, p + 5, margin_ - 8);
      painter.drawLine(p + 5, margin_ - 8, p, margin_ - 3);
      painter.setFont(label_font);
      painter.drawText(p, margin_ - 8, "lower boundary");
      painter.setFont(QFont());

      //right
      p = UInt(((right_splitter_ - dist_.minBound()) / (dist_.maxBound() - dist_.minBound())) * (width() - 2 * margin_)) + margin_;
      painter.drawLine(p, margin_ - 8, p, height() - bottom_axis_->height());
      painter.drawLine(p, margin_ - 8, p - 5, margin_ - 8);
      painter.drawLine(p - 5, margin_ - 8, p, margin_ - 3);
      painter.setFont(label_font);
      painter.drawText(p, margin_ - 8, "upper boundary");
      painter.setFont(QFont());
    }
  }

  void HistogramWidget::resizeEvent(QResizeEvent * /*e*/)
  {
    buffer_ = QPixmap(width() - margin_, height() - bottom_axis_->height());
    bottom_axis_->setGeometry(margin_, height() - bottom_axis_->height(), width() - margin_, bottom_axis_->height());
    invalidate_();
  }

  void HistogramWidget::invalidate_()
  {
    //apply log trafo if needed
    Math::Histogram<> dist(dist_);
    if (log_mode_)
    {
      dist.applyLogTransformation(100.0);
    }

    QPainter painter(&buffer_);
    buffer_.fill(palette().window().color());
    UInt w = buffer_.width();
    UInt h = buffer_.height();
    UInt pen_width = std::min(margin_, UInt(0.5 * w / dist.size()));

    //draw distribution
    QPen pen;
    pen.setWidth(pen_width);
    pen.setColor(QColor(100, 125, 175));
    painter.setPen(pen);

    for (Size i = 0; i < dist.size(); ++i)
    {
      if (dist[i] != 0)
      {
        UInt bin_pos = UInt((DoubleReal(i) / (dist.size() - 1)) * (w - margin_));
        UInt bin_height = UInt(((DoubleReal)dist[i] / dist.maxValue()) * (h - margin_));
        painter.drawLine(bin_pos + 1, h, bin_pos + 1, h - bin_height);
      }
    }

    //calculate total intensity
    DoubleReal total_sum = 0;
    for (Size i = 0; i < dist.size(); ++i)
    {
      total_sum += dist[i];
    }

    // draw part of total intensity
    painter.setPen(Qt::red);
    QPoint last_point(1, h);
    QPoint point;
    DoubleReal int_sum = 0;
    for (Size i = 0; i < dist.size(); ++i)
    {
      int_sum += dist[i];
      point.setX(UInt((DoubleReal(i) / (dist.size() - 1)) * (w - margin_)));
      point.setY(UInt((1 - (int_sum / total_sum)) * (h - margin_) + margin_));
      painter.drawLine(last_point, point);
      last_point = point;
    }
    // draw coord system (on top distribution)
    painter.setPen(Qt::black);
    painter.drawLine(0, h - 1, w - margin_ + Int(0.5 * pen_width), h - 1);

    update();
  }

  void HistogramWidget::showContextMenu(const QPoint & pos)
  {
    //create menu
    QMenu menu(this);
    QAction * action = menu.addAction("Normal mode");
    if (!log_mode_)
      action->setEnabled(false);
    action = menu.addAction("Log mode");
    if (log_mode_)
      action->setEnabled(false);
    //execute
    QAction * result = menu.exec(mapToGlobal(pos));
    //change according to selected value
    if (result != 0)
    {
      if (result->text() == "Normal mode")
      {
        setLogMode(false);
      }
      else if (result->text() == "Log mode")
      {
        setLogMode(true);
      }
    }
  }

  void HistogramWidget::setLogMode(bool log_mode)
  {
    log_mode_ = log_mode;
    if (!buffer_.isNull())
      invalidate_();
  }

} //namespace OpenMS