CMS 3D CMS Logo

VisCSCPlotWidgetAxisX.cc

Go to the documentation of this file.
00001 #include "VisReco/VisMuonCSC/interface/VisCSCPlotWidgetAxisX.h"
00002 #include "qpainter.h"
00003 #include <cassert>
00004 #include <cmath>
00005 #include <iostream>
00006 
00007 VisCSCPlotWidgetAxisX::VisCSCPlotWidgetAxisX(const int nbins, QWidget* parent, const char* name): QWidget(parent, name), 
00008 m_bins(nbins)
00009 {
00010         assert(m_bins>0);
00011 }
00012 
00013 VisCSCPlotWidgetAxisX::~VisCSCPlotWidgetAxisX(void)
00014 {
00015 }
00016 QSize VisCSCPlotWidgetAxisX::minimumSizeHint() const
00017 {
00018         // minimumSizeHint/sizeHint must be coordinated with y axis/VisCSCPlotWidget 
00019         return QSize( 80, 40 );
00020 }
00021 QSize VisCSCPlotWidgetAxisX::sizeHint() const
00022 {
00023         return QSize( 400, 50 );
00024 }
00025 void VisCSCPlotWidgetAxisX::paintEvent(QPaintEvent *)
00026 {
00027         QPainter painter(this);
00028         painter.setPen(Qt::blue);
00029         QFont sansFont( "Helvetica", 9 );
00030         painter.setFont( sansFont );
00031         QFontMetrics fm( sansFont );
00032 
00033         // assume every five bins is a good choice: change to 16 for CSC
00034         const int bin_spacing = 16;
00035         QPointArray scale( 4 * int(floor(float(m_bins) / bin_spacing)) );
00036         int yoffset = 2;
00037         int tickHeight = 4;
00038 
00039         // don't forget that the last pixel is width()-1
00040         for (int i = bin_spacing, j = 0; i <= m_bins; i += bin_spacing )
00041         {
00042                 // labels go under center of bin before tick marks
00043                 QString label = QString("%1").arg(i);
00044                 int labelWidth = fm.width(label);
00045                 int xtext = int(floor( (i - 0.5F)*width()/m_bins - labelWidth/2.F + 0.5F) );
00046                 // check to see last label doesn't stick over the edge
00047                 if (xtext > width()-1 - fm.width(label))
00048                 {
00049                         xtext = width()-1 - fm.width(label);
00050                 }
00051                 painter.drawText( xtext, fm.height(), label );
00052                 // draw tick marks
00053                 scale.setPoint(j++, int(float(i - bin_spacing)*(width()-1)/m_bins + 0.5F), yoffset+tickHeight );
00054                 scale.setPoint(j++, int(float(i - bin_spacing)*(width()-1)/m_bins + 0.5F), yoffset );
00055                 scale.setPoint(j++, int(float(i              )*(width()-1)/m_bins + 0.5F), yoffset );
00056                 scale.setPoint(j++, int(float(i              )*(width()-1)/m_bins + 0.5F), yoffset+tickHeight );
00057         }       
00058         painter.drawPolyline(scale);
00059 }

Generated on Tue Jun 9 17:50:20 2009 for CMSSW by  doxygen 1.5.4