| 1 | |
| 2 | /**************************************************************************** |
| 3 | ** |
| 4 | ** Copyright (C) 2011 Christian B. Huebschle & George M. Sheldrick |
| 5 | ** All rights reserved. |
| 6 | ** Contact: [email protected] |
| 7 | ** |
| 8 | ** This file is part of the ShelXle |
| 9 | ** |
| 10 | ** This file may be used under the terms of the GNU Lesser |
| 11 | ** General Public License version 2.1 as published by the Free Software |
| 12 | ** Foundation and appearing in the file COPYING included in the |
| 13 | ** packaging of this file. Please review the following information to |
| 14 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
| 15 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 16 | ** |
| 17 | ** |
| 18 | ****************************************************************************/ |
| 19 | #include "qpeakview.h" |
| 20 | #include <QtGui> |
| 21 | |
| 22 | QPeakView::QPeakView(QWidget *parent) : |
| 23 | QWidget(parent){ |
| 24 | //Dieses Widget soll einen Farbverlauf mit Peak-Hoehen darstellen |
| 25 | min=0; |
| 26 | focusHeight=-1.0; |
| 27 | max=80.0; |
| 28 | oco=cutoff=0.0; |
| 29 | fohi = fontMetrics().height(); |
| 30 | setMouseTracking(true); |
| 31 | //setCursor(QCursor(Qt::SplitVCursor)); |
| 32 | } |
| 33 | |
| 34 | void QPeakView::unhide(){ |
| 35 | printf("unhide\n"); |
| 36 | oco=cutoff=0;//qMin(0.0,min); |
| 37 | update(); |
| 38 | } |
| 39 | |
| 40 | void QPeakView::qpeaksInFocus(double height){ |
| 41 | focusHeight=-1.0; |
| 42 | if (height<min) {update();return;} |
| 43 | if (height>max) {update();return;} |
| 44 | focusHeight=height; |
| 45 | update(); |
| 46 | } |
| 47 | void QPeakView::cutOffChange(double coff){ |
| 48 | if (oco!=cutoff) emit cutOffChanged(coff); |
| 49 | oco=cutoff; |
| 50 | } |
| 51 | |
| 52 | void QPeakView::paintEvent(QPaintEvent *event){ |
| 53 | |
| 54 | QPainter p(this); |
| 55 | p.fillRect(event->rect(), (dark)?QColor("#44586A"):QColor("#eeeeee")); |
| 56 | p.fillRect(0,0,20,height(),Qt::black); |
| 57 | int h = height(); |
| 58 | int fh = p.fontMetrics().height(); |
| 59 | //fohi=fh; |
| 60 | fh=qMax(0,fh); |
| 61 | //printf("%d\n",fh); |
| 62 | double v = h / fh; |
| 63 | double w = v-1,u; |
| 64 | |
| 65 | QBrush b; |
| 66 | for (int i = 0; i < v; i++){ |
| 67 | u=(1.0-i/w)*max+(i/w)*min; |
| 68 | if (QString::number(focusHeight,'f',2)==QString::number((1.0-i/w)*max+(i/w)*min,'f',2)){ |
| 69 | p.setPen((dark)?Qt::green:Qt::blue); |
| 70 | b.setColor((dark)?Qt::black:Qt::lightGray); |
| 71 | p.fillRect(20,i*fh,50,fh,b); |
| 72 | } |
| 73 | else { |
| 74 | p.setPen((dark)?Qt::white:Qt::black); |
| 75 | if (u<0)p.setPen(Qt::red); |
| 76 | } |
| 77 | //printf("%f %f %f %f %f %d %d\n",v,w,u,min,max,h,fh); |
| 78 | if (cutoff>0) { |
| 79 | if (((u)<cutoff)&&(u>0)) b.setStyle(Qt::Dense3Pattern); |
| 80 | else b.setStyle(Qt::SolidPattern); |
| 81 | } |
| 82 | else { |
| 83 | if (((u)>cutoff)&&(u<0)) b.setStyle(Qt::Dense3Pattern); |
| 84 | else b.setStyle(Qt::SolidPattern); |
| 85 | } |
| 86 | b.setColor(farbverlauf(u)); |
| 87 | if (cutoff>0) |
| 88 | p.fillRect(0,i*fh,((u<cutoff)&&(u>0))?50:20,fh,b); |
| 89 | else p.fillRect(0,i*fh,((u>cutoff)&&(u<0))?50:20,fh,b); |
| 90 | |
| 91 | if (cutoff>0) {if ((u>=cutoff)||(u<0)) p.drawText((u<0)?12:20,i*fh,40,fh,Qt::AlignLeft,QString::number(u,'f',2));} |
| 92 | else {if ((u<cutoff)||(u>0)) p.drawText((u<0)?12:20,i*fh,40,fh,Qt::AlignLeft,QString::number(u,'f',2));} |
| 93 | |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void QPeakView::mousePressEvent ( QMouseEvent * event ){ |
| 98 | |
| 99 | if (event->buttons()==Qt::LeftButton){ |
| 100 | int h = height() - 2 * fohi; |
| 101 | double v = height() / fohi; |
| 102 | double w = v-1,u; |
| 103 | u = ((1.0 - (w-1) / w) * max + ( (w-1) / w ) * min)-min; |
| 104 | cutoff=((double)(h - event->pos().y()) / h) * max + |
| 105 | ((double)(event->pos().y()) / h ) * min + u; |
| 106 | //cutoff=qMax(min,cutoff); |
| 107 | //cutoff=qMin(max,cutoff); |
| 108 | //printf("coff %f mx %f mn %f u %f \n",cutoff, max, min, u); |
| 109 | cutOffChange(cutoff); |
| 110 | update(); |
| 111 | } |
| 112 | } |
| 113 | void QPeakView::wheelEvent ( QWheelEvent * event ){ |
| 114 | #if (QT_VERSION>0x050000) |
| 115 | double numSteps = event->angleDelta().y() / 12000.0; |
| 116 | #else |
| 117 | double numSteps = event->delta() / 12000.0; |
| 118 | #endif |
| 119 | if (event->modifiers()==Qt::NoModifier){ |
| 120 | double d = cutoff; |
| 121 | cutoff=(d+numSteps>=(min-0.05))?d+numSteps:d; |
| 122 | cutoff=qMin(cutoff,max+0.005); |
| 123 | cutOffChange(cutoff); |
| 124 | update(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void QPeakView::mouseMoveEvent ( QMouseEvent * event ){ |
| 129 | //event->CursorChange(); |
| 130 | |
| 131 | if (event->buttons()==Qt::NoButton){ |
| 132 | int h = height(); |
| 133 | double v = height() / fohi; |
| 134 | double w = v-1,u; |
| 135 | u = ((1.0 - (w-1) / w) * max + ( (w-1) / w ) * min)-min; |
| 136 | double co=((double)(h - event->pos().y()) / h) * max + |
| 137 | ((double)(event->pos().y()) / h ) * min + u; |
| 138 | emit findQP(co); |
| 139 | |
| 140 | } |
| 141 | if (event->buttons()==Qt::LeftButton){ |
| 142 | int h = height() - 2 * fohi; |
| 143 | double v = height() / fohi; |
| 144 | double w = v-1,u; |
| 145 | u = ((1.0 - (w-1) / w) * max + ( (w-1) / w ) * min)-min; |
| 146 | cutoff=((double)(h - event->pos().y()) / h) * max + |
| 147 | ((double)(event->pos().y()) / h ) * min + u; |
| 148 | //cutoff=qMax(min,cutoff); |
| 149 | //cutoff=qMin(max,cutoff); |
| 150 | //printf("coff %f mx %f mn %f u %f \n",cutoff, max, min, u); |
| 151 | |
| 152 | cutOffChange(cutoff); |
| 153 | update(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | QColor QPeakView::farbverlauf(double wrt){ |
| 158 | if (min>=max) max=min+0.001; |
| 159 | int rot,gruen,blau,alpha; |
| 160 | int lauf=0; |
| 161 | const float farbe[6][4]={{1.0f,0.0f,0.0f,1.0f}, |
| 162 | {1.0f,1.0f,0.0f,1.0f}, |
| 163 | {0.0f,1.0f,0.0f,1.0f}, |
| 164 | {0.0f,1.0f,1.0f,1.0f}, |
| 165 | {0.0f,0.0f,1.0f,1.0f}, |
| 166 | {1.0f,0.0f,1.0f,1.0f}}; |
| 167 | double nwrt=(wrt-min)/(max-min); |
| 168 | nwrt=(nwrt>=1.0)?0.99999:nwrt; |
| 169 | nwrt=(nwrt<=0.0)?0.00001:nwrt; |
| 170 | lauf=(int (nwrt/0.2)); |
| 171 | nwrt-=(0.2*lauf); |
| 172 | nwrt/=(0.2); |
| 173 | |
| 174 | rot=(int)(255*((1.0-nwrt)*farbe[lauf][0]+farbe[lauf+1][0]*nwrt)); |
| 175 | gruen=(int)(255*((1.0-nwrt)*farbe[lauf][1]+farbe[lauf+1][1]*nwrt)); |
| 176 | blau=(int)(255*((1.0-nwrt)*farbe[lauf][2]+farbe[lauf+1][2]*nwrt)); |
| 177 | alpha=255; |
| 178 | return QColor(rot,gruen,blau,alpha); |
| 179 | } |