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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
|
// --------------------------------------------------------------------------
// 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 $
// --------------------------------------------------------------------------
// OpenMS
#include <OpenMS/VISUAL/SpectrumCanvas.h>
#include <OpenMS/VISUAL/SpectrumWidget.h>
#include <OpenMS/VISUAL/AxisWidget.h>
#include <OpenMS/SYSTEM/FileWatcher.h>
#include <OpenMS/VISUAL/MetaDataBrowser.h>
// QT
#include <QtGui/QPainter>
#include <QtGui/QPaintEvent>
#include <QtGui/QBitmap>
#include <QtGui/QWheelEvent>
#include <QtGui/QMessageBox>
#include <QtGui/QPushButton>
#include <QtGui/QFontMetrics>
#include <QtGui/QFontMetrics>
#include <iostream>
using namespace std;
namespace OpenMS
{
SpectrumCanvas::SpectrumCanvas(const Param & /*preferences*/, QWidget * parent) :
QWidget(parent),
DefaultParamHandler("SpectrumCanvas"),
buffer_(),
action_mode_(AM_TRANSLATE),
intensity_mode_(IM_NONE),
layers_(),
mz_to_x_axis_(true),
visible_area_(AreaType::empty),
overall_data_range_(DRange<3>::empty),
show_grid_(true),
zoom_stack_(),
zoom_pos_(zoom_stack_.end()),
update_buffer_(false),
current_layer_(0),
spectrum_widget_(0),
percentage_factor_(1.0),
snap_factors_(1, 1.0),
rubber_band_(QRubberBand::Rectangle, this),
context_add_(0),
show_timing_(false),
selected_peak_(),
measurement_start_()
{
//Prevent filling background
setAttribute(Qt::WA_OpaquePaintEvent);
// get mouse coordinates while mouse moves over diagramm and for focus handling
setMouseTracking(TRUE);
setFocusPolicy(Qt::StrongFocus);
setMinimumSize(200, 200);
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
//reserve enough space to avoid copying layer data
layers_.reserve(10);
//set common defaults for all canvases
defaults_.setValue("default_path", ".", "Default path for loading/storing data.");
//Set focus policy in order to get keyboard events
//Set 'whats this' text
setWhatsThis("Translate: Translate mode is activated by default. Hold down the left mouse key and move the mouse to translate. Arrow keys can be used for translation independent of the current mode.\n\n"
"Zoom: Zoom mode is activated with the CTRL key. CTRL+/CTRL- are used to traverse the zoom stack (or mouse wheel). Pressing Backspace resets the zoom.\n\n"
"Measure: Measure mode is activated with the SHIFT key. To measure the distace between data points, press the left mouse button on a point and drag the mouse to another point.\n\n"
);
//set move cursor and connect signal that updates the cursor automatically
updateCursor_();
connect(this, SIGNAL(actionModeChange()), this, SLOT(updateCursor_()));
}
SpectrumCanvas::~SpectrumCanvas()
{
//cout << "DEST SpectrumCanvas" << endl;
}
void SpectrumCanvas::resizeEvent(QResizeEvent * /* e */)
{
#ifdef DEBUG_TOPPVIEW
cout << "BEGIN " << __PRETTY_FUNCTION__ << endl;
#endif
buffer_ = QImage(width(), height(), QImage::Format_RGB32);
update_buffer_ = true;
updateScrollbars_();
update_(__PRETTY_FUNCTION__);
#ifdef DEBUG_TOPPVIEW
cout << "END " << __PRETTY_FUNCTION__ << endl;
#endif
}
void SpectrumCanvas::setFilters(const DataFilters & filters)
{
//set filters
layers_[current_layer_].filters = filters;
//update the content
update_buffer_ = true;
update_(__PRETTY_FUNCTION__);
}
void SpectrumCanvas::showGridLines(bool show)
{
show_grid_ = show;
update_buffer_ = true;
update_(__PRETTY_FUNCTION__);
}
void SpectrumCanvas::intensityModeChange_()
{
recalculateSnapFactor_();
update_buffer_ = true;
update_(__PRETTY_FUNCTION__);
}
void SpectrumCanvas::mzToXAxis(bool mz_to_x_axis)
{
mz_to_x_axis_ = mz_to_x_axis;
//swap axes if necessary
if (spectrum_widget_)
{
spectrum_widget_->updateAxes();
}
updateScrollbars_();
update_buffer_ = true;
update_(__PRETTY_FUNCTION__);
}
void SpectrumCanvas::changeVisibleArea_(const AreaType & new_area, bool repaint, bool add_to_stack)
{
//store old zoom state
if (add_to_stack)
{
// if we scrolled in between zooming we want to store the last position before zooming as well
if ((zoom_stack_.size() > 0)
&& (zoom_stack_.back() != visible_area_))
{
zoomAdd_(visible_area_);
}
// add current zoom
zoomAdd_(new_area);
}
if (new_area != visible_area_)
{
visible_area_ = new_area;
updateScrollbars_();
emit visibleAreaChanged(new_area);
}
if (repaint)
{
update_buffer_ = true;
update_(__PRETTY_FUNCTION__);
}
}
void SpectrumCanvas::updateScrollbars_()
{
}
void SpectrumCanvas::wheelEvent(QWheelEvent * e)
{
zoom_(e->x(), e->y(), e->delta() > 0);
e->accept();
}
void SpectrumCanvas::zoom_(int x, int y, bool zoom_in)
{
const PointType::CoordinateType zoom_factor = zoom_in ? 0.8 : 1.0 / 0.8;
AreaType new_area;
for (int dim = 0; dim < AreaType::DIMENSION; dim++)
{
new_area.min_[dim] = visible_area_.min_[dim] + (1.0 - zoom_factor) * (visible_area_.max_[dim] - visible_area_.min_[dim])
* ((dim == 0) == isMzToXAxis()
? (PointType::CoordinateType)x / width()
: (PointType::CoordinateType)(height() - y) / height());
new_area.max_[dim] = new_area.min_[dim] + zoom_factor * (visible_area_.max_[dim] - visible_area_.min_[dim]);
new_area.min_[dim] = std::max(new_area.min_[dim], overall_data_range_.min_[dim]);
new_area.max_[dim] = std::min(new_area.max_[dim], overall_data_range_.max_[dim]);
}
if (new_area != visible_area_)
{
zoomAdd_(new_area);
zoom_pos_ = --zoom_stack_.end(); // set to last position
changeVisibleArea_(*zoom_pos_);
}
}
void SpectrumCanvas::zoomBack_()
{
//cout << "Zoom out" << endl;
//cout << " - pos before:" << (zoom_pos_-zoom_stack_.begin()) << endl;
//cout << " - size before:" << zoom_stack_.size() << endl;
if (zoom_pos_ != zoom_stack_.begin())
{
--zoom_pos_;
changeVisibleArea_(*zoom_pos_);
}
//cout << " - pos after:" << (zoom_pos_-zoom_stack_.begin()) << endl;
}
void SpectrumCanvas::zoomForward_()
{
//cout << "Zoom in" << endl;
//cout << " - pos before:" << (zoom_pos_-zoom_stack_.begin()) << endl;
//cout << " - size before:" << zoom_stack_.size() <<endl;
// if at end of zoom level then simply add a new zoom
if (zoom_pos_ == zoom_stack_.end() || (zoom_pos_ + 1) == zoom_stack_.end())
{
AreaType new_area;
// distance of areas center to border times a zoom factor of 0.8
AreaType::CoordinateType size0 = visible_area_.width() / 2 * 0.8;
AreaType::CoordinateType size1 = visible_area_.height() / 2 * 0.8;
new_area.setMinX(visible_area_.center()[0] - size0);
new_area.setMinY(visible_area_.center()[1] - size1);
new_area.setMaxX(visible_area_.center()[0] + size0);
new_area.setMaxY(visible_area_.center()[1] + size1);
zoomAdd_(new_area);
zoom_pos_ = --zoom_stack_.end(); // set to last position
}
else // goto next zoom level
{
++zoom_pos_;
}
changeVisibleArea_(*zoom_pos_);
//cout << " - pos after:" << (zoom_pos_-zoom_stack_.begin()) << endl;
}
void SpectrumCanvas::zoomAdd_(const AreaType & area)
{
//cout << "Adding to stack" << endl;
//cout << " - pos before:" << (zoom_pos_-zoom_stack_.begin()) << endl;
//cout << " - size before:" << zoom_stack_.size() <<endl;
if (zoom_pos_ != zoom_stack_.end() && (zoom_pos_ + 1) != zoom_stack_.end())
{
//cout << " - removing from:" << ((zoom_pos_+1)-zoom_stack_.begin()) << endl;
zoom_stack_.erase(zoom_pos_ + 1, zoom_stack_.end());
}
zoom_stack_.push_back(area);
zoom_pos_ = zoom_stack_.end();
--zoom_pos_;
//cout << " - pos after:" << (zoom_pos_-zoom_stack_.begin()) << endl;
//cout << " - size after:" << zoom_stack_.size() <<endl;
}
void SpectrumCanvas::zoomClear_()
{
zoom_stack_.clear();
zoom_pos_ = zoom_stack_.end();
}
void SpectrumCanvas::resetZoom(bool repaint)
{
AreaType tmp;
tmp.assign(overall_data_range_);
zoomClear_();
changeVisibleArea_(tmp, repaint, true);
}
void SpectrumCanvas::setVisibleArea(AreaType area)
{
//cout << __PRETTY_FUNCTION__ << endl;
changeVisibleArea_(area);
}
void SpectrumCanvas::paintGridLines_(QPainter & painter)
{
if (!show_grid_ || !spectrum_widget_)
{
return;
}
QPen p1(QColor(130, 130, 130));
p1.setStyle(Qt::DashLine);
QPen p2(QColor(170, 170, 170));
p2.setStyle(Qt::DotLine);
painter.save();
unsigned int xl, xh, yl, yh; //width/height of the diagram area, x, y coordinates of lo/hi x,y values
xl = 0;
xh = width();
yl = height();
yh = 0;
// drawing of grid lines and associated text
for (Size j = 0; j != spectrum_widget_->xAxis()->gridLines().size(); j++)
{
// style definitions
switch (j)
{
case 0: // style settings for big intervals
painter.setPen(p1);
break;
case 1: // style settings for small intervals
painter.setPen(p2);
break;
default:
std::cout << "empty vertical grid line vector error!" << std::endl;
painter.setPen(QPen(QColor(0, 0, 0)));
break;
}
for (std::vector<double>::const_iterator it = spectrum_widget_->xAxis()->gridLines()[j].begin(); it != spectrum_widget_->xAxis()->gridLines()[j].end(); ++it)
{
int x = static_cast<int>(Math::intervalTransformation(*it, spectrum_widget_->xAxis()->getAxisMinimum(), spectrum_widget_->xAxis()->getAxisMaximum(), xl, xh));
painter.drawLine(x, yl, x, yh);
}
}
for (Size j = 0; j != spectrum_widget_->yAxis()->gridLines().size(); j++)
{
// style definitions
switch (j)
{
case 0: // style settings for big intervals
painter.setPen(p1);
break;
case 1: // style settings for small intervals
painter.setPen(p2);
break;
default:
std::cout << "empty vertical grid line vector error!" << std::endl;
painter.setPen(QPen(QColor(0, 0, 0)));
break;
}
for (std::vector<double>::const_iterator it = spectrum_widget_->yAxis()->gridLines()[j].begin(); it != spectrum_widget_->yAxis()->gridLines()[j].end(); ++it)
{
int y = static_cast<int>(Math::intervalTransformation(*it, spectrum_widget_->yAxis()->getAxisMinimum(), spectrum_widget_->yAxis()->getAxisMaximum(), yl, yh));
painter.drawLine(xl, y, xh, y);
}
}
painter.restore();
}
Size SpectrumCanvas::activeLayerIndex() const
{
return current_layer_;
}
bool SpectrumCanvas::addLayer(ExperimentSharedPtrType map, const String & filename)
{
layers_.resize(layers_.size() + 1);
layers_.back().param = param_;
layers_.back().filename = filename;
layers_.back().getPeakData() = map;
if (layers_.back().getPeakData()->getChromatograms().size() != 0
&& layers_.back().getPeakData()->size() != 0)
{
// TODO : handle this case better
LOG_WARN << "Your input data contains chromatograms and spectra, falling back to display spectra only." << std::endl;
}
if (layers_.back().getPeakData()->getChromatograms().size() != 0
&& layers_.back().getPeakData()->size() == 0)
{
layers_.back().type = LayerData::DT_CHROMATOGRAM;
}
else
{
layers_.back().type = LayerData::DT_PEAK;
}
return finishAdding_();
}
bool SpectrumCanvas::addLayer(FeatureMapSharedPtrType map, const String & filename)
{
layers_.resize(layers_.size() + 1);
layers_.back().param = param_;
layers_.back().filename = filename;
layers_.back().getFeatureMap() = map;
layers_.back().type = LayerData::DT_FEATURE;
return finishAdding_();
}
bool SpectrumCanvas::addLayer(ConsensusMapSharedPtrType map, const String & filename)
{
layers_.resize(layers_.size() + 1);
layers_.back().param = param_;
layers_.back().filename = filename;
layers_.back().getConsensusMap() = map;
layers_.back().type = LayerData::DT_CONSENSUS;
return finishAdding_();
}
bool SpectrumCanvas::addLayer(vector<PeptideIdentification> & peptides,
const String & filename)
{
layers_.resize(layers_.size() + 1);
layers_.back().param = param_;
layers_.back().filename = filename;
layers_.back().peptides.swap(peptides);
layers_.back().type = LayerData::DT_IDENT;
return finishAdding_();
}
void SpectrumCanvas::setLayerName(Size i, const String & name)
{
OPENMS_PRECONDITION(i < layers_.size(), "SpectrumCanvas::setLayerName(i, name) index overflow");
getLayer_(i).name = name;
if (i == 0 && spectrum_widget_)
{
spectrum_widget_->setWindowTitle(name.toQString());
}
}
String SpectrumCanvas::getLayerName(const Size i)
{
OPENMS_PRECONDITION(i < layers_.size(), "SpectrumCanvas::getLayerName(i) index overflow");
return getLayer_(i).name;
}
void SpectrumCanvas::changeVisibility(Size i, bool b)
{
OPENMS_PRECONDITION(i < layers_.size(), "SpectrumCanvas::changeVisibility(i, b) index overflow");
LayerData & layer = getLayer_(i);
if (layer.visible != b)
{
layer.visible = b;
update_buffer_ = true;
update_(__PRETTY_FUNCTION__);
}
}
void SpectrumCanvas::changeLayerFilterState(Size i, bool b)
{
OPENMS_PRECONDITION(i < layers_.size(), "SpectrumCanvas::changeVisibility(i,b) index overflow");
LayerData & layer = getLayer_(i);
if (layer.filters.isActive() != b)
{
layer.filters.setActive(b);
update_buffer_ = true;
update_(__PRETTY_FUNCTION__);
}
}
const DRange<3> & SpectrumCanvas::getDataRange()
{
return overall_data_range_;
}
void SpectrumCanvas::recalculateRanges_(UInt mz_dim, UInt rt_dim, UInt it_dim)
{
overall_data_range_ = DRange<3>::empty;
DRange<3>::PositionType m_min = overall_data_range_.minPosition();
DRange<3>::PositionType m_max = overall_data_range_.maxPosition();
for (Size layer_index = 0; layer_index < getLayerCount(); ++layer_index)
{
if (getLayer(layer_index).type == LayerData::DT_PEAK || getLayer(layer_index).type == LayerData::DT_CHROMATOGRAM)
{
const ExperimentType & map = *getLayer(layer_index).getPeakData();
if (map.getMinMZ() < m_min[mz_dim]) m_min[mz_dim] = map.getMinMZ();
if (map.getMaxMZ() > m_max[mz_dim]) m_max[mz_dim] = map.getMaxMZ();
if (map.getMinRT() < m_min[rt_dim]) m_min[rt_dim] = map.getMinRT();
if (map.getMaxRT() > m_max[rt_dim]) m_max[rt_dim] = map.getMaxRT();
if (map.getMinInt() < m_min[it_dim]) m_min[it_dim] = map.getMinInt();
if (map.getMaxInt() > m_max[it_dim]) m_max[it_dim] = map.getMaxInt();
}
else if (getLayer(layer_index).type == LayerData::DT_FEATURE)
{
const FeatureMapType & map = *getLayer(layer_index).getFeatureMap();
if (map.getMin()[1] < m_min[mz_dim]) m_min[mz_dim] = map.getMin()[1];
if (map.getMax()[1] > m_max[mz_dim]) m_max[mz_dim] = map.getMax()[1];
if (map.getMin()[0] < m_min[rt_dim]) m_min[rt_dim] = map.getMin()[0];
if (map.getMax()[0] > m_max[rt_dim]) m_max[rt_dim] = map.getMax()[0];
if (map.getMinInt() < m_min[it_dim]) m_min[it_dim] = map.getMinInt();
if (map.getMaxInt() > m_max[it_dim]) m_max[it_dim] = map.getMaxInt();
}
else if (getLayer(layer_index).type == LayerData::DT_CONSENSUS)
{
const ConsensusMapType & map = *getLayer(layer_index).getConsensusMap();
if (map.getMin()[1] < m_min[mz_dim]) m_min[mz_dim] = map.getMin()[1];
if (map.getMax()[1] > m_max[mz_dim]) m_max[mz_dim] = map.getMax()[1];
if (map.getMin()[0] < m_min[rt_dim]) m_min[rt_dim] = map.getMin()[0];
if (map.getMax()[0] > m_max[rt_dim]) m_max[rt_dim] = map.getMax()[0];
if (map.getMinInt() < m_min[it_dim]) m_min[it_dim] = map.getMinInt();
if (map.getMaxInt() > m_max[it_dim]) m_max[it_dim] = map.getMaxInt();
}
else if (getLayer(layer_index).type == LayerData::DT_IDENT)
{
const vector<PeptideIdentification> & peptides =
getLayer(layer_index).peptides;
for (vector<PeptideIdentification>::const_iterator it =
peptides.begin(); it != peptides.end(); ++it)
{
DoubleReal rt = (DoubleReal) it->getMetaValue("RT");
DoubleReal mz = getIdentificationMZ_(layer_index, *it);
if (mz < m_min[mz_dim]) m_min[mz_dim] = mz;
if (mz > m_max[mz_dim]) m_max[mz_dim] = mz;
if (rt < m_min[rt_dim]) m_min[rt_dim] = rt;
if (rt > m_max[rt_dim]) m_max[rt_dim] = rt;
}
}
}
//Add 1% margin to RT in order to display all the data
DoubleReal margin = 0.01 * std::max(1.0, m_max[rt_dim] - m_min[rt_dim]);
m_min[rt_dim] -= margin;
m_max[rt_dim] += margin;
overall_data_range_.setMin(m_min);
overall_data_range_.setMax(m_max);
}
DoubleReal SpectrumCanvas::getSnapFactor()
{
return snap_factors_[0];
}
DoubleReal SpectrumCanvas::getPercentageFactor()
{
return percentage_factor_;
}
void SpectrumCanvas::recalculateSnapFactor_()
{
}
void SpectrumCanvas::horizontalScrollBarChange(int /*value*/)
{
}
void SpectrumCanvas::verticalScrollBarChange(int /*value*/)
{
}
void SpectrumCanvas::update_(const char *)
{
update();
}
//this does not work anymore, probably due to Qt::StrongFocus :(
void SpectrumCanvas::focusOutEvent(QFocusEvent * /*e*/)
{
// Alt/Shift pressed and focus lost => change back action mode
if (action_mode_ != AM_TRANSLATE)
{
action_mode_ = AM_TRANSLATE;
emit actionModeChange();
}
//reset peaks
selected_peak_.clear();
measurement_start_.clear();
//update
update_(__PRETTY_FUNCTION__);
}
void SpectrumCanvas::leaveEvent(QEvent * /*e*/)
{
//release keyboard, when the mouse pointer leaves
releaseKeyboard();
}
void SpectrumCanvas::enterEvent(QEvent * /*e*/)
{
//grab keyboard, as we need to handle key presses
grabKeyboard();
}
void SpectrumCanvas::keyReleaseEvent(QKeyEvent * e)
{
// Alt/Shift released => change back action mode
if (e->key() == Qt::Key_Control || e->key() == Qt::Key_Shift)
{
action_mode_ = AM_TRANSLATE;
emit actionModeChange();
e->accept();
}
e->ignore();
}
void SpectrumCanvas::keyPressEvent(QKeyEvent * e)
{
// Alt/Shift pressed => change action mode
if (e->key() == Qt::Key_Control)
{
e->accept();
action_mode_ = AM_ZOOM;
emit actionModeChange();
}
else if (e->key() == Qt::Key_Shift)
{
e->accept();
action_mode_ = AM_MEASURE;
emit actionModeChange();
}
// CTRL+/CTRL- => Zoom stack
if ((e->modifiers() & Qt::ControlModifier) && (e->key() == Qt::Key_Plus))
{
e->accept();
zoomForward_();
}
else if ((e->modifiers() & Qt::ControlModifier) && (e->key() == Qt::Key_Minus))
{
e->accept();
zoomBack_();
}
// Arrow keys => translate
else if (e->key() == Qt::Key_Left)
{
e->accept();
translateLeft_();
}
else if (e->key() == Qt::Key_Right)
{
e->accept();
translateRight_();
}
else if (e->key() == Qt::Key_Up)
{
e->accept();
translateForward_();
}
else if (e->key() == Qt::Key_Down)
{
e->accept();
translateBackward_();
}
//Backspace to reset zoom
else if (e->key() == Qt::Key_Backspace)
{
e->accept();
resetZoom();
}
// CTRL+ALT+T => activate timing mode
if ((e->modifiers() & Qt::ControlModifier) && (e->modifiers() & Qt::AltModifier) && (e->key() == Qt::Key_T))
{
e->accept();
show_timing_ = !show_timing_;
}
releaseKeyboard(); // ensure that the key event is passed on to parent widget
e->ignore();
}
void SpectrumCanvas::translateLeft_()
{
}
void SpectrumCanvas::translateRight_()
{
}
void SpectrumCanvas::translateForward_()
{
}
void SpectrumCanvas::translateBackward_()
{
}
void SpectrumCanvas::setAdditionalContextMenu(QMenu * menu)
{
context_add_ = menu;
}
void SpectrumCanvas::getVisiblePeakData(ExperimentType & map) const
{
//clear output experiment
map.clear(true);
const LayerData & layer = getCurrentLayer();
if (layer.type == LayerData::DT_PEAK)
{
const AreaType & area = getVisibleArea();
const ExperimentType & peaks = *layer.getPeakData();
//copy experimental settings
map.ExperimentalSettings::operator=(peaks);
//reserve space for the correct number of spectra in RT range
ExperimentType::ConstIterator begin = layer.getPeakData()->RTBegin(area.minPosition()[1]);
ExperimentType::ConstIterator end = layer.getPeakData()->RTEnd(area.maxPosition()[1]);
//Exception for Spectrum1DCanvas, here we copy the currently visualized spectrum
bool is_1d = (getName() == "Spectrum1DCanvas");
if (is_1d)
{
begin = layer.getPeakData()->begin() + layer.getCurrentSpectrumIndex();
end = begin + 1;
}
map.reserve(end - begin);
//copy spectra
for (ExperimentType::ConstIterator it = begin; it != end; ++it)
{
SpectrumType spectrum;
//copy spectrum meta information
spectrum.SpectrumSettings::operator=(* it);
spectrum.setRT(it->getRT());
spectrum.setMSLevel(it->getMSLevel());
spectrum.setPrecursors(it->getPrecursors());
//copy peak information
if (!is_1d && it->getMSLevel() > 1 && !it->getPrecursors().empty()) //MS^n (n>1) spectra are copied if their precursor is in the m/z range
{
if (it->getPrecursors()[0].getMZ() >= area.minPosition()[0] && it->getPrecursors()[0].getMZ() <= area.maxPosition()[0])
{
spectrum.insert(spectrum.begin(), it->begin(), it->end());
map.addSpectrum(spectrum);
}
}
else // MS1(0) spectra are cropped to the m/z range
{
for (SpectrumType::ConstIterator it2 = it->MZBegin(area.minPosition()[0]); it2 != it->MZEnd(area.maxPosition()[0]); ++it2)
{
if (layer.filters.passes(*it, it2 - it->begin()))
{
spectrum.push_back(*it2);
}
}
map.addSpectrum(spectrum);
}
// do not use map.addSpectrum() here, otherwise empty spectra which did not pass the filters above will be added
}
}
else if (layer.type == LayerData::DT_CHROMATOGRAM)
{
//TODO CHROM
}
}
void SpectrumCanvas::getVisibleFeatureData(FeatureMapType & map) const
{
//clear output experiment
map.clear(true);
const LayerData & layer = getCurrentLayer();
if (layer.type == LayerData::DT_FEATURE)
{
//copy meta data
map.setIdentifier(layer.getFeatureMap()->getIdentifier());
map.setProteinIdentifications(layer.getFeatureMap()->getProteinIdentifications());
//Visible area
DoubleReal min_rt = getVisibleArea().minPosition()[1];
DoubleReal max_rt = getVisibleArea().maxPosition()[1];
DoubleReal min_mz = getVisibleArea().minPosition()[0];
DoubleReal max_mz = getVisibleArea().maxPosition()[0];
//copy features
for (FeatureMapType::ConstIterator it = layer.getFeatureMap()->begin(); it != layer.getFeatureMap()->end(); ++it)
{
if (layer.filters.passes(*it)
&& it->getRT() >= min_rt
&& it->getRT() <= max_rt
&& it->getMZ() >= min_mz
&& it->getMZ() <= max_mz)
{
map.push_back(*it);
}
}
}
}
void SpectrumCanvas::getVisibleConsensusData(ConsensusMapType & map) const
{
//clear output experiment
map.clear(true);
const LayerData & layer = getCurrentLayer();
if (layer.type == LayerData::DT_CONSENSUS)
{
//copy file descriptions
map.getFileDescriptions() = layer.getConsensusMap()->getFileDescriptions();
//Visible area
DoubleReal min_rt = getVisibleArea().minPosition()[1];
DoubleReal max_rt = getVisibleArea().maxPosition()[1];
DoubleReal min_mz = getVisibleArea().minPosition()[0];
DoubleReal max_mz = getVisibleArea().maxPosition()[0];
//copy features
for (ConsensusMapType::ConstIterator it = layer.getConsensusMap()->begin(); it != layer.getConsensusMap()->end(); ++it)
{
if (layer.filters.passes(*it)
&& it->getRT() >= min_rt
&& it->getRT() <= max_rt
&& it->getMZ() >= min_mz
&& it->getMZ() <= max_mz)
{
map.push_back(*it);
}
}
}
}
void SpectrumCanvas::getVisibleIdentifications(vector<PeptideIdentification> &
peptides) const
{
//clear output experiment
peptides.clear();
const LayerData & layer = getCurrentLayer();
if (layer.type == LayerData::DT_IDENT)
{
//Visible area
DoubleReal min_rt = getVisibleArea().minPosition()[1];
DoubleReal max_rt = getVisibleArea().maxPosition()[1];
DoubleReal min_mz = getVisibleArea().minPosition()[0];
DoubleReal max_mz = getVisibleArea().maxPosition()[0];
//copy features
for (vector<PeptideIdentification>::const_iterator it =
layer.peptides.begin(); it != layer.peptides.end(); ++it)
{
DoubleReal rt = (DoubleReal) it->getMetaValue("RT");
DoubleReal mz = getIdentificationMZ_(current_layer_, *it);
// TODO: if (layer.filters.passes(*it) && ...)
if ((rt >= min_rt) && (rt <= max_rt) &&
(mz >= min_mz) && (mz <= max_mz))
{
peptides.push_back(*it);
}
}
}
}
void SpectrumCanvas::showMetaData(bool modifiable, Int index)
{
LayerData & layer = getCurrentLayer_();
MetaDataBrowser dlg(modifiable, this);
if (index == -1)
{
if (layer.type == LayerData::DT_PEAK)
{
dlg.add(*layer.getPeakData());
//Exception for Spectrum1DCanvas, here we add the meta data of the one spectrum
if (getName() == "Spectrum1DCanvas")
{
dlg.add((*layer.getPeakData())[layer.getCurrentSpectrumIndex()]);
}
}
else if (layer.type == LayerData::DT_FEATURE)
{
dlg.add(*layer.getFeatureMap());
}
else if (layer.type == LayerData::DT_CONSENSUS)
{
dlg.add(*layer.getConsensusMap());
}
else if (layer.type == LayerData::DT_CHROMATOGRAM)
{
//TODO CHROM
}
else if (layer.type == LayerData::DT_IDENT)
{
// TODO IDENT
}
}
else //show element meta data
{
if (layer.type == LayerData::DT_PEAK)
{
dlg.add((*layer.getPeakData())[index]);
}
else if (layer.type == LayerData::DT_FEATURE)
{
dlg.add((*layer.getFeatureMap())[index]);
}
else if (layer.type == LayerData::DT_CONSENSUS)
{
dlg.add((*layer.getConsensusMap())[index]);
}
else if (layer.type == LayerData::DT_CHROMATOGRAM)
{
//TODO CHROM
}
else if (layer.type == LayerData::DT_IDENT)
{
// TODO IDENT
}
}
//if the meta data was modified, set the flag
if (modifiable && dlg.exec())
{
modificationStatus_(activeLayerIndex(), true);
}
}
void SpectrumCanvas::updateCursor_()
{
switch (action_mode_)
{
case AM_TRANSLATE:
setCursor(QCursor(QPixmap(":/cursor_move.png"), 0, 0));
break;
case AM_ZOOM:
setCursor(QCursor(QPixmap(":/cursor_zoom.png"), 0, 0));
break;
case AM_MEASURE:
setCursor(QCursor(QPixmap(":/cursor_measure.png"), 0, 0));
break;
}
}
void SpectrumCanvas::modificationStatus_(Size layer_index, bool modified)
{
LayerData & layer = getLayer_(layer_index);
if (layer.modified != modified)
{
layer.modified = modified;
#ifdef DEBUG_TOPPVIEW
cout << "BEGIN " << __PRETTY_FUNCTION__ << endl;
cout << "emit: layerModificationChange" << endl;
cout << "END " << __PRETTY_FUNCTION__ << endl;
#endif
emit layerModficationChange(activeLayerIndex(), modified);
}
}
void SpectrumCanvas::drawText_(QPainter & painter, QStringList text)
{
painter.save();
//font
QFont font("Courier");
painter.setFont(font);
//determine width and height of the box we need
QFontMetrics metrics(painter.font());
int line_spacing = metrics.lineSpacing();
int height = 6 + text.size() * line_spacing;
int width = 4;
for (int i = 0; i < text.size(); ++i)
{
width = std::max(width, 4 + metrics.width(text[i]));
}
//draw backgrond for text
painter.fillRect(2, 3, width, height, QColor(255, 255, 255, 200));
//draw text
painter.setPen(Qt::black);
for (int i = 0; i < text.size(); ++i)
{
painter.drawText(3, 3 + (i + 1) * line_spacing, text[i]);
}
painter.restore();
}
DoubleReal SpectrumCanvas::getIdentificationMZ_(const Size layer_index,
const PeptideIdentification &
peptide) const
{
if (getLayerFlag(layer_index, LayerData::I_PEPTIDEMZ))
{
const PeptideHit & hit = peptide.getHits().front();
Int charge = hit.getCharge();
return hit.getSequence().getMonoWeight(Residue::Full, charge) / charge;
}
else
{
return (DoubleReal) peptide.getMetaValue("MZ");
}
}
} //namespace
|