📄 qwt_thermo.cpp
字号:
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- ***************************** * Qwt Widget Library * Copyright (C) 1997 Josef Wilgen * Copyright (C) 2002 Uwe Rathmann * * This library is free software; you can redistribute it and/or * modify it under the terms of the Qwt License, Version 1.0 *****************************************************************************/#include <qpainter.h>#include <qstyle.h>#include <qpixmap.h>#include <qdrawutil.h>#include "qwt_math.h"#include "qwt_paint_buffer.h"#include "qwt_thermo.h"//! ConstructorQwtThermo::QwtThermo(QWidget *parent, const char *name): QWidget(parent, name, WRepaintNoErase|WResizeNoErase){ init();}//! DestructorQwtThermo::~QwtThermo(){}//! initialize data membersvoid QwtThermo::init(){ // initialize data members d_orient = Qt::Vertical; d_scalePos = Left; d_scaleDist = 3; d_thermoWidth = 10; d_borderWidth = 2; d_maxValue = 1.0; d_minValue = 0.0; d_value = 0.0; d_alarmLevel = 0.0; d_alarmEnabled = 0; // initialize colors; d_fillColor = black; d_alarmColor = white; // initialize scales d_map.setDblRange(d_minValue, d_maxValue); scaleDraw()->setScale(d_minValue, d_maxValue, scaleMaxMajor(), scaleMaxMinor());}//! Set the current valuevoid QwtThermo::setValue(double v){ if (d_value != v) { d_value = v; update(); }}//! Qt paint eventvoid QwtThermo::paintEvent(QPaintEvent *e){ // Use double-buffering const QRect &ur = e->rect(); if ( ur.isValid() ) { QwtPaintBuffer paintBuffer(this, ur); draw(paintBuffer.painter(), ur); }}//! redraw the thermovoid QwtThermo::draw(QPainter *p, const QRect& ur){ if ( !d_thermoRect.contains(ur) ) { if (d_scalePos != None) scaleDraw()->draw(p); qDrawShadePanel(p, d_thermoRect.x() - d_borderWidth, d_thermoRect.y() - d_borderWidth, d_thermoRect.width() + 2*d_borderWidth, d_thermoRect.height() + 2*d_borderWidth, colorGroup(), TRUE, d_borderWidth,0); } drawThermo(p);}//! Qt resize event handlervoid QwtThermo::resizeEvent(QResizeEvent *){ layoutThermo( FALSE );}//! Recalculate the thermo's geometry and layout based on// the current rect and fonts.// \param update_geometry notify the layout system and call update// to redraw the scalevoid QwtThermo::layoutThermo( bool update_geometry ){ QRect r = this->rect(); int mbd = 0; if ( d_scalePos != None ) { int d1, d2; scaleDraw()->minBorderDist(fontMetrics(), d1, d2); mbd = QMAX(d1, d2); } if ( d_orient == Qt::Horizontal ) { switch ( d_scalePos ) { case None: d_thermoRect.setRect(r.x() + d_borderWidth, r.y() + d_borderWidth, r.width() - 2*d_borderWidth, r.height() - 2*d_borderWidth); break; case Bottom: d_thermoRect.setRect(r.x() + mbd + d_borderWidth, r.y() + d_borderWidth, r.width() - 2*(d_borderWidth + mbd), d_thermoWidth); scaleDraw()->setGeometry(d_thermoRect.x(), d_thermoRect.y() + d_thermoRect.height() + d_borderWidth + d_scaleDist, d_thermoRect.width(), QwtScaleDraw::Bottom); break; case Top: default: d_thermoRect.setRect(r.x() + mbd + d_borderWidth, r.y() + r.height() - d_thermoWidth - 2*d_borderWidth, r.width() - 2*(d_borderWidth + mbd), d_thermoWidth); scaleDraw()->setGeometry(d_thermoRect.x(), d_thermoRect.y() - d_borderWidth - d_scaleDist, d_thermoRect.width(), QwtScaleDraw::Top); break; } d_map.setIntRange(d_thermoRect.x(), d_thermoRect.x() + d_thermoRect.width() - 1); } else // Qt::Vertical { switch ( d_scalePos ) { case None: d_thermoRect.setRect(r.x() + d_borderWidth, r.y() + d_borderWidth, r.width() - 2*d_borderWidth, r.height() - 2*d_borderWidth); break; case Left: d_thermoRect.setRect(r.x() + r.width() - 2*d_borderWidth - d_thermoWidth, r.y() + mbd + d_borderWidth, d_thermoWidth, r.height() - 2*(d_borderWidth + mbd)); scaleDraw()->setGeometry(d_thermoRect.x() - d_scaleDist - d_borderWidth, d_thermoRect.y(), d_thermoRect.height(), QwtScaleDraw::Left); break; case Right: default: d_thermoRect.setRect(r.x() + d_borderWidth, r.y() + mbd + d_borderWidth, d_thermoWidth, r.height() - 2*(d_borderWidth + mbd)); scaleDraw()->setGeometry(d_thermoRect.x() + d_thermoRect.width() + d_borderWidth + d_scaleDist, d_thermoRect.y(), d_thermoRect.height(), QwtScaleDraw::Right); break; } d_map.setIntRange( d_thermoRect.y() + d_thermoRect.height() - 1, d_thermoRect.y()); } if ( update_geometry ) { updateGeometry(); update(); }}/*! \brief Change the thermometer's orientation The scale position None disables the scale. \param o orientation. Possible values are Qt::Horizontal and Qt::Vertical. The default value is Qt::Vertical. \param s Position of the scale. For a horizontal orientation, the scale position can be Top, Bottom or None. A vertical thermometer may have the scale positions Left, Right or None. The default is None.*/void QwtThermo::setOrientation(Qt::Orientation o, ScalePos s){ switch(o) { case Qt::Horizontal: d_orient = Qt::Horizontal; if ((s == None) || (s == Bottom) || (s == Top)) d_scalePos = s; else d_scalePos = None; break; case Qt::Vertical: d_orient = Qt::Vertical; if ((s == None) || (s == Left) || (s == Right)) d_scalePos = s; else d_scalePos = None; break; } layoutThermo();}//! notify changed fontvoid QwtThermo::fontChange(const QFont &f){ QWidget::fontChange( f ); layoutThermo();}//!notify changed scalevoid QwtThermo::scaleChange(){ update(); layoutThermo();}//! Redraw the thermometervoid QwtThermo::drawThermo(QPainter *p){ int alarm = 0, taval = 0; QRect fRect; QRect aRect; QRect bRect; int inverted = ( d_maxValue < d_minValue ); // // Determine if value exceeds alarm threshold. // Note: The alarm value is allowed to lie // outside the interval (minValue, maxValue). // if (d_alarmEnabled) { if (inverted) { alarm = ((d_alarmLevel >= d_maxValue) && (d_alarmLevel <= d_minValue) && (d_value >= d_alarmLevel)); } else { alarm = (( d_alarmLevel >= d_minValue) && (d_alarmLevel <= d_maxValue) && (d_value >= d_alarmLevel)); } } // // transform values // int tval = d_map.limTransform(d_value); if (alarm) taval = d_map.limTransform(d_alarmLevel);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -