~ubuntu-branches/ubuntu/raring/voxbo/raring

« back to all changes in this revision

Viewing changes to vbview/vbqt_scalewidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2010-06-06 11:33:11 UTC
  • Revision ID: james.westby@ubuntu.com-20100606113311-v3c13imdkkd5n7ae
Tags: upstream-1.8.5~svn1172
ImportĀ upstreamĀ versionĀ 1.8.5~svn1172

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
// vbqt_scalewidget.cpp
 
3
// Copyright (c) 2010 by The VoxBo Development Team
 
4
 
 
5
// This file is part of VoxBo
 
6
// 
 
7
// VoxBo is free software: you can redistribute it and/or modify it
 
8
// under the terms of the GNU General Public License as published by
 
9
// the Free Software Foundation, either version 3 of the License, or
 
10
// (at your option) any later version.
 
11
// 
 
12
// VoxBo is distributed in the hope that it will be useful, but
 
13
// WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
// General Public License for more details.
 
16
// 
 
17
// You should have received a copy of the GNU General Public License
 
18
// along with VoxBo.  If not, see <http://www.gnu.org/licenses/>.
 
19
// 
 
20
// For general information on VoxBo, including the latest complete
 
21
// source code and binary distributions, manual, and associated files,
 
22
// see the VoxBo home page at: http://www.voxbo.org/
 
23
// 
 
24
// original version written by Dan Kimberg
 
25
 
 
26
using namespace std;
 
27
 
 
28
#include <QApplication>
 
29
#include <QPixmap>
 
30
#include <QMouseEvent>
 
31
#include <QFrame>
 
32
#include <QMenu>
 
33
#include "qcolordialog.h"
 
34
#include "qcursor.h"
 
35
#include "qclipboard.h"
 
36
#include <QFileDialog>
 
37
#include <iostream>
 
38
#include "vbqt_scalewidget.moc.h"
 
39
 
 
40
VBScalewidget::VBScalewidget(QWidget *parent)
 
41
  : QFrame(parent)
 
42
{
 
43
  setFrameStyle(QFrame::Box|QFrame::Plain);
 
44
  q_low=0.0;
 
45
  q_high=10.0;
 
46
  q_negcolor1=QColor(0,0,200);
 
47
  q_negcolor2=QColor(0,0,255);
 
48
  q_poscolor1=QColor(200,0,0);
 
49
  q_poscolor2=QColor(255,0,0);
 
50
}
 
51
 
 
52
void
 
53
VBScalewidget::paintEvent(QPaintEvent *)
 
54
{
 
55
  QPainter pp(this);
 
56
  drawcolorbars(&pp,QRect(0,0,width(),height()),12);
 
57
}
 
58
 
 
59
void
 
60
VBScalewidget::setscale(float low,float high,QColor negcolor1,QColor negcolor2,
 
61
         QColor poscolor1,QColor poscolor2)
 
62
{
 
63
  q_low=low;
 
64
  q_high=high;
 
65
  q_negcolor1=negcolor1;
 
66
  q_negcolor2=negcolor2;
 
67
  q_poscolor1=poscolor1;
 
68
  q_poscolor2=poscolor2;
 
69
  update();
 
70
}
 
71
 
 
72
void
 
73
VBScalewidget::setscale(float low,float high,QColor poscolor1,QColor poscolor2)
 
74
{
 
75
  q_low=low;
 
76
  q_high=high;
 
77
  q_negcolor1.setAlpha(0);
 
78
  q_negcolor2.setAlpha(0);
 
79
  q_poscolor1=poscolor1;
 
80
  q_poscolor2=poscolor2;
 
81
  update();
 
82
}
 
83
 
 
84
void
 
85
VBScalewidget::drawContents(QPainter *paint)
 
86
{
 
87
  drawcolorbars(paint,contentsRect(),12);
 
88
}
 
89
 
 
90
void
 
91
VBScalewidget::drawcolorbars(QPainter *paint,QRect myrect,int fontsize)
 
92
{
 
93
  f_vertical=0;
 
94
  if (myrect.height() > myrect.width()) f_vertical=1;
 
95
  paint->setPen(QColor(0,0,0));
 
96
  paint->setBackgroundColor(QColor(255,255,255));
 
97
  paint->eraseRect(myrect);
 
98
  QFont ff("Helvetica");
 
99
  ff.setPixelSize(fontsize);
 
100
  paint->setFont(ff);
 
101
  const int fontwidth=QFontMetrics(ff).width("9.9999");
 
102
  const int ISPACE=QFontMetrics(ff).width("i");
 
103
 
 
104
  bool f_neg=q_negcolor1.alpha();
 
105
 
 
106
  char tmp[64];
 
107
  int xoff=myrect.left();
 
108
  int yoff=myrect.top();
 
109
  const int SPACE=fontsize/2;
 
110
 
 
111
  if (f_vertical && f_neg) {
 
112
    int barsize=(myrect.height()-SPACE-SPACE-SPACE)/2;
 
113
    posrect.setRect(xoff+SPACE,yoff+SPACE,myrect.width()-SPACE-fontwidth,barsize);
 
114
    negrect.setRect(xoff+SPACE,yoff+SPACE+barsize+SPACE,myrect.width()-SPACE-fontwidth,barsize);
 
115
  }
 
116
  else if (f_vertical && !f_neg) {
 
117
    int barsize=myrect.height()-SPACE-SPACE;
 
118
    posrect.setRect(xoff+SPACE,yoff+SPACE,myrect.width()-SPACE-fontwidth,barsize);
 
119
  }
 
120
  if (!f_vertical && f_neg) {
 
121
    int barsize=(myrect.width()-SPACE-SPACE-SPACE)/2;
 
122
    negrect.setRect(xoff+SPACE,yoff+SPACE,barsize,myrect.height()-SPACE-fontsize);
 
123
    posrect.setRect(xoff+SPACE+barsize+SPACE,yoff+SPACE,barsize,myrect.height()-SPACE-fontsize);
 
124
  }
 
125
  if (!f_vertical && !f_neg) {
 
126
    int barsize=myrect.width()-SPACE-SPACE;
 
127
    posrect.setRect(xoff+SPACE,yoff+SPACE,barsize,myrect.height()-SPACE-fontsize);
 
128
  }
 
129
 
 
130
  // black rect that will eventually leave just the hairline
 
131
  paint->fillRect(posrect,QColor(0,0,0));
 
132
  if (f_neg) paint->fillRect(negrect,QColor(0,0,0));
 
133
 
 
134
  // now the colors and labels
 
135
  if (f_neg && f_vertical) {
 
136
    shaderect(paint,negrect,q_negcolor1,q_negcolor2);
 
137
    paint->setPen(QColor(0,0,0));
 
138
    sprintf(tmp,"%g",q_high*-1); 
 
139
    paint->drawText(negrect.right()+ISPACE,negrect.top(),myrect.width()-negrect.right(),negrect.height(),Qt::AlignLeft|Qt::AlignBottom,QString(tmp));
 
140
    sprintf(tmp,"%g",q_low*-1);
 
141
    paint->drawText(negrect.right()+ISPACE,negrect.top(),myrect.width()-negrect.right(),negrect.height(),Qt::AlignLeft|Qt::AlignTop,QString(tmp));
 
142
  }
 
143
  else if (f_neg && !f_vertical) {
 
144
    shaderect(paint,negrect,q_negcolor2,q_negcolor1);
 
145
    paint->setPen(QColor(0,0,0));
 
146
    sprintf(tmp,"%g",q_high*-1);
 
147
    paint->drawText(negrect.left(),negrect.bottom(),negrect.width(),myrect.height()-negrect.bottom(),Qt::AlignLeft|Qt::AlignTop,QString(tmp));
 
148
    sprintf(tmp,"%g",q_low*-1);
 
149
    paint->drawText(negrect.left(),negrect.bottom(),negrect.width(),myrect.height()-negrect.bottom(),Qt::AlignRight|Qt::AlignTop,QString(tmp));
 
150
  }
 
151
  // NOW THE POSITIVE BAR
 
152
  if (f_vertical) {
 
153
    shaderect(paint,posrect,q_poscolor2,q_poscolor1);
 
154
    paint->setPen(QColor(0,0,0));
 
155
    sprintf(tmp,"%g",q_low);
 
156
    paint->drawText(posrect.right()+ISPACE,posrect.top(),myrect.width()-posrect.right(),posrect.height(),Qt::AlignLeft|Qt::AlignBottom,QString(tmp));
 
157
    sprintf(tmp,"%g",q_high);
 
158
    paint->drawText(posrect.right()+ISPACE,posrect.top(),myrect.width()-posrect.right(),posrect.height(),Qt::AlignLeft|Qt::AlignTop,QString(tmp));
 
159
  }
 
160
  else {
 
161
    shaderect(paint,posrect,q_poscolor1,q_poscolor2);
 
162
    paint->setPen(QColor(0,0,0));
 
163
    sprintf(tmp,"%g",q_low);
 
164
    paint->drawText(posrect.left(),posrect.bottom(),posrect.width(),myrect.height()-posrect.bottom(),Qt::AlignLeft|Qt::AlignTop,QString(tmp));
 
165
    sprintf(tmp,"%g",q_high);
 
166
    paint->drawText(posrect.left(),posrect.bottom(),posrect.width(),myrect.height()-posrect.bottom(),Qt::AlignRight|Qt::AlignTop,QString(tmp));
 
167
  }
 
168
}
 
169
 
 
170
void
 
171
VBScalewidget::shaderect(QPainter *paint,QRect &r,QColor &c1,QColor &c2)
 
172
{
 
173
  int rr,gg,bb;
 
174
  float pct;
 
175
  // if it's horizontal, we go left to right, vertical we go top to bottom
 
176
  if (f_vertical) {
 
177
    for (int i=r.top()+1; i<r.bottom(); i++) {
 
178
      pct=(float)(i-(r.top()+1))/((r.bottom()-1)-(r.top()+1.0));
 
179
      rr=c1.red()+(int)(pct*(c2.red()-c1.red()));
 
180
      gg=c1.green()+(int)(pct*(c2.green()-c1.green()));
 
181
      bb=c1.blue()+(int)(pct*(c2.blue()-c1.blue()));
 
182
      paint->setPen(QColor(rr,gg,bb));
 
183
      paint->drawLine(r.left()+1,i,r.right()-1,i);
 
184
    }
 
185
  }
 
186
  else {
 
187
    for (int i=r.left()+1; i<r.right(); i++) {
 
188
      pct=(float)(i-(r.left()+1))/((r.right()-1)-(r.left()+1));
 
189
      rr=c1.red()+(int)(pct*(c2.red()-c1.red()));
 
190
      gg=c1.green()+(int)(pct*(c2.green()-c1.green()));
 
191
      bb=c1.blue()+(int)(pct*(c2.blue()-c1.blue()));
 
192
      paint->setPen(QColor(rr,gg,bb));
 
193
      paint->drawLine(i,r.top()+1,i,r.bottom()-1);
 
194
    }
 
195
  }
 
196
}
 
197
 
 
198
 
 
199
void
 
200
VBScalewidget::mousePressEvent(QMouseEvent *me)
 
201
{
 
202
  if (me->button()==Qt::RightButton) {
 
203
    contextmenu();
 
204
    return;
 
205
  }
 
206
}
 
207
 
 
208
void
 
209
VBScalewidget::mouseDoubleClickEvent(QMouseEvent *me)
 
210
{
 
211
  if (negrect.contains(me->x(),me->y())) {
 
212
    if ((f_vertical && me->y() < negrect.top()+(negrect.bottom()-negrect.top())/2) ||
 
213
        (!f_vertical && me->x() <negrect.left()+(negrect.right()-negrect.left())/2)) {
 
214
      QColor tmp=QColorDialog::getColor(q_negcolor2);
 
215
      if (tmp.isValid()) {
 
216
        q_negcolor2=tmp;
 
217
        emit newcolors(q_negcolor1,q_negcolor2,q_poscolor1,q_poscolor2);
 
218
        update(contentsRect());
 
219
      }
 
220
    }
 
221
    else {
 
222
      QColor tmp=QColorDialog::getColor(q_negcolor1);
 
223
      if (tmp.isValid()) {
 
224
        q_negcolor1=tmp;
 
225
        emit newcolors(q_negcolor1,q_negcolor2,q_poscolor1,q_poscolor2);
 
226
        update(contentsRect());
 
227
      }
 
228
    }
 
229
  }
 
230
  else if (posrect.contains(me->x(),me->y())) {
 
231
    if ((f_vertical && me->y() > posrect.top()+(posrect.bottom()-posrect.top())/2) ||
 
232
        (!f_vertical && me->x() < posrect.left()+(posrect.right()-posrect.left())/2)  ) {
 
233
      // p1
 
234
      QColor tmp=QColorDialog::getColor(q_poscolor1);
 
235
      if (tmp.isValid()) {
 
236
        q_poscolor1=tmp;
 
237
        emit newcolors(q_negcolor1,q_negcolor2,q_poscolor1,q_poscolor2);
 
238
        update(contentsRect());
 
239
      }
 
240
    }
 
241
    else {
 
242
      // p2
 
243
      QColor tmp=QColorDialog::getColor(q_poscolor2);
 
244
      if (tmp.isValid()) {
 
245
        q_poscolor2=tmp;
 
246
        emit newcolors(q_negcolor1,q_negcolor2,q_poscolor1,q_poscolor2);
 
247
        update(contentsRect());
 
248
      }
 
249
    }
 
250
  }
 
251
}
 
252
 
 
253
 
 
254
 
 
255
 
 
256
 
 
257
void
 
258
VBScalewidget::contextmenu()
 
259
{
 
260
  QMenu foo(this);
 
261
  QAction *copy1,*save1,*copy2,*save2,*copy3,*save3,*ret;
 
262
  copy1=foo.addAction("Copy to Clipboard");
 
263
  save1=foo.addAction("Save PNG");
 
264
  copy2=foo.addAction("Copy Clipboard (highres)");
 
265
  save2=foo.addAction("Save PNG (highres)");
 
266
  copy3=foo.addAction("Copy Clipboard vertical (highres)");
 
267
  save3=foo.addAction("Save PNG vertical (highres)");
 
268
  ret=foo.exec(QCursor::pos());
 
269
  QPixmap pm;
 
270
 
 
271
  // grab the lowres or highres pixmap
 
272
  if (ret==copy1 || ret==save1) {
 
273
    pm=QPixmap::grabWidget(this,contentsRect().left(),contentsRect().top(),
 
274
                           contentsRect().width(),contentsRect().height());
 
275
  }
 
276
  else if (ret==copy2 || ret==save2) {
 
277
    int ww=1200,hh=150,ff=48;
 
278
    pm.resize(ww,hh);
 
279
    QPainter pp(&pm);
 
280
    drawcolorbars(&pp,QRect(0,0,ww,hh),ff);
 
281
    pp.end();
 
282
  }
 
283
  else if (ret==copy3 || ret==save3) {
 
284
    int ww=250,hh=1200,ff=48;
 
285
    pm.resize(ww,hh);
 
286
    QPainter pp(&pm);
 
287
    drawcolorbars(&pp,QRect(0,0,ww,hh),ff);
 
288
    pp.end();
 
289
  }
 
290
  // copy to clipboard or save to file
 
291
  if (ret==copy1||ret==copy2||ret==copy3) {
 
292
    QClipboard *cb=QApplication::clipboard();
 
293
    cb->setPixmap(pm);    
 
294
  }
 
295
  else if (ret==save1||ret==save2||ret==save3) {
 
296
    QString s=QFileDialog::getSaveFileName(".","All (*.*)",this,"save image file","Choose a filename for the scale widget");
 
297
    if (s!=QString::null)
 
298
      pm.save(s.latin1(),"PNG");;
 
299
  }
 
300
}