1 
2 /****************************************************************************
3 **
4 ** Copyright (C) 2011 Christian B. Huebschle & George M. Sheldrick
5 ** All rights reserved.
6 ** Contact: chuebsch@moliso.de
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 
QPeakView(QWidget * parent)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 
unhide()34 void QPeakView::unhide(){
35   printf("unhide\n");
36     oco=cutoff=0;//qMin(0.0,min);
37     update();
38 }
39 
qpeaksInFocus(double height)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 }
cutOffChange(double coff)47 void QPeakView::cutOffChange(double coff){
48     if (oco!=cutoff) emit cutOffChanged(coff);
49     oco=cutoff;
50 }
51 
paintEvent(QPaintEvent * event)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 
mousePressEvent(QMouseEvent * event)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 }
wheelEvent(QWheelEvent * event)113 void QPeakView::wheelEvent ( QWheelEvent * event ){
114 
115     double numSteps = event->delta() / 12000.0;
116     if (event->modifiers()==Qt::NoModifier){
117       double d = cutoff;
118       cutoff=(d+numSteps>=(min-0.05))?d+numSteps:d;
119       cutoff=qMin(cutoff,max+0.005);
120       cutOffChange(cutoff);
121       update();
122     }
123 }
124 
mouseMoveEvent(QMouseEvent * event)125 void QPeakView::mouseMoveEvent ( QMouseEvent * event ){
126     //event->CursorChange();
127 
128     if (event->buttons()==Qt::NoButton){
129         int h = height();
130         double v =  height() / fohi;
131         double w = v-1,u;
132         u = ((1.0 - (w-1) / w) * max + ( (w-1) / w ) * min)-min;
133         double co=((double)(h - event->pos().y()) / h) * max +
134                ((double)(event->pos().y()) / h ) * min + u;
135         emit findQP(co);
136 
137     }
138     if (event->buttons()==Qt::LeftButton){
139         int h = height() - 2 * fohi;
140         double v =  height() / fohi;
141         double w = v-1,u;
142         u = ((1.0 - (w-1) / w) * max + ( (w-1) / w ) * min)-min;
143         cutoff=((double)(h - event->pos().y()) / h) * max +
144                ((double)(event->pos().y()) / h ) * min + u;
145         //cutoff=qMax(min,cutoff);
146         //cutoff=qMin(max,cutoff);
147         //printf("coff %f mx %f mn %f u %f \n",cutoff, max, min, u);
148 
149         cutOffChange(cutoff);
150         update();
151     }
152 }
153 
farbverlauf(double wrt)154 QColor QPeakView::farbverlauf(double wrt){
155     if (min>=max) max=min+0.001;
156     int rot,gruen,blau,alpha;
157     int lauf=0;
158     const float farbe[6][4]={{1.0f,0.0f,0.0f,1.0f},
159                           {1.0f,1.0f,0.0f,1.0f},
160                           {0.0f,1.0f,0.0f,1.0f},
161                           {0.0f,1.0f,1.0f,1.0f},
162                           {0.0f,0.0f,1.0f,1.0f},
163                           {1.0f,0.0f,1.0f,1.0f}};
164     double nwrt=(wrt-min)/(max-min);
165     nwrt=(nwrt>=1.0)?0.99999:nwrt;
166     nwrt=(nwrt<=0.0)?0.00001:nwrt;
167     lauf=(int (nwrt/0.2));
168     nwrt-=(0.2*lauf);
169     nwrt/=(0.2);
170 
171     rot=(int)(255*((1.0-nwrt)*farbe[lauf][0]+farbe[lauf+1][0]*nwrt));
172     gruen=(int)(255*((1.0-nwrt)*farbe[lauf][1]+farbe[lauf+1][1]*nwrt));
173     blau=(int)(255*((1.0-nwrt)*farbe[lauf][2]+farbe[lauf+1][2]*nwrt));
174     alpha=255;
175     return QColor(rot,gruen,blau,alpha);
176 }
177