Qt Virtual Chart Table (QVCT)
COverlay.cpp
Go to the documentation of this file.
1 // INDENTING (emacs/vi): -*- mode:c++; tab-width:2; c-basic-offset:2; intent-tabs-mode:nil; -*- ex: set tabstop=2 expandtab:
2 
3 /*
4  * Qt Virtual Chart Table (QVCT)
5  * Copyright (C) 2012 Cedric Dufour <http://cedric.dufour.name>
6  * Author: Cedric Dufour <http://cedric.dufour.name>
7  *
8  * The Qt Virtual Chart Table (QVCT) is free software:
9  * you can redistribute it and/or modify it under the terms of the GNU General
10  * Public License as published by the Free Software Foundation, Version 3.
11  *
12  * The Qt Virtual Chart Table (QVCT) is distributed in the hope
13  * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
14  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  *
16  * See the GNU General Public License for more details.
17  */
18 
19 // QT
20 #include <QColor>
21 #include <QPainter>
22 #include <QTreeWidgetItem>
23 #include <QWidget>
24 
25 // QVCT
26 #include "QVCTRuntime.hpp"
27 #include "charts/CChart.hpp"
28 #include "overlays/COverlay.hpp"
30 
31 
32 //------------------------------------------------------------------------------
33 // CONSTRUCTORS / DESTRUCTOR
34 //------------------------------------------------------------------------------
35 
36 COverlay::COverlay( const QString& _rqsName )
37  : QTreeWidgetItem( COverlayObject::OVERLAY )
38  , qsName( _rqsName )
39  , bVisible( true )
40  , poChartPixmap( 0 )
41  , bForceRedraw( true )
42 {
43  // Painter dependencies
44  qFont.setPixelSize( 12 );
45  qBrushText.setStyle( Qt::SolidPattern );
46  qBrushText.setColor( QColor( 255, 255, 255, 160 ) );
47  qPenText.setColor( QColor( 0, 0, 0, 192 ) );
48  qPenText.setStyle( Qt::SolidLine );
49  qPenText.setWidth( 1 );
50  qPenText.setCapStyle( Qt::RoundCap );
51  qBrushMarker.setStyle( Qt::SolidPattern );
52  qBrushMarker.setColor( QColor( 0, 0, 0, 128 ) );
53  qPenMarker.setColor( QColor( 0, 0, 0, 192 ) );
54  qPenMarker.setStyle( Qt::SolidLine );
55  qPenMarker.setWidth( 3 );
56  qPenMarker.setCapStyle( Qt::RoundCap );
57  qBrushMarkerSelected.setStyle( Qt::NoBrush );
58  qPenMarkerSelected.setColor( QColor( 192, 192, 0, 255 ) );
59  qPenMarkerSelected.setStyle( Qt::SolidLine );
60  qPenMarkerSelected.setWidth( 5 );
61  qPenMarkerSelected.setCapStyle( Qt::RoundCap );
62  qPenLine.setColor( QColor( 0, 0, 0, 192 ) );
63  qPenLine.setStyle( Qt::SolidLine );
64  qPenLine.setWidth( 5 );
65  qPenLine.setCapStyle( Qt::RoundCap );
66  qPenVector.setColor( QColor( 0, 0, 0, 192 ) );
67  qPenVector.setStyle( Qt::SolidLine );
68  qPenVector.setWidth( 3 );
69  qPenVector.setCapStyle( Qt::RoundCap );
70 
71  // Import settings
72  QColor __qColor = QVCTRuntime::useSettings()->getColorSelected();
73  __qColor.setAlpha( 255 );
74  qPenMarkerSelected.setColor( __qColor );
75 }
76 
77 
78 //------------------------------------------------------------------------------
79 // METHODS
80 //------------------------------------------------------------------------------
81 
82 //
83 // OTHER
84 //
85 
86 void COverlay::draw( const CChart* _poChart, QPainter* _pqPainter )
87 {
88  // Exit if we're not visible
89  if( !bVisible ) return;
90 
91  // Redraw pixmap only if required
92  QSize __qSizePixmap = _poChart->getDrawArea().toRect().size();
93  QPointF __qPointFDatPositionPixmap = _poChart->getDatPosition();
94  double __fdZoomPixmap = _poChart->getZoom();
95  if( poChartPixmap != _poChart
96  || qPixmapBuffer.size() != __qSizePixmap
97  || qPointFDatPositionPixmap != __qPointFDatPositionPixmap
98  || fdZoomPixmap != __fdZoomPixmap
99  || bForceRedraw )
100  {
101  //qDebug( "DEBUG[%s] Rendering", Q_FUNC_INFO );
102 
103  // Reset pixmap buffer
104  poChartPixmap = _poChart;
105  if( qPixmapBuffer.size() != __qSizePixmap ) qPixmapBuffer = QPixmap( __qSizePixmap );
106  qPixmapBuffer.fill( Qt::transparent );
107  qPointFDatPositionPixmap = __qPointFDatPositionPixmap;
108  fdZoomPixmap = __fdZoomPixmap;
109  bForceRedraw = false;
110 
111  // Create buffer painter
112  QPainter __qPainter( &qPixmapBuffer );
113  __qPainter.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing );
114  __qPainter.setFont( qFont );
115 
116  // Draw
117  drawContent( _poChart, &__qPainter );
118  }
119  _pqPainter->drawPixmap( 0, 0, qPixmapBuffer );
120 
121 }
122 
123 QString COverlay::newChildName( const QString& _rqsName, int __iZeroPrefix, bool __bForceSuffix ) const
124 {
125  QString __qsName;
126  int __iSuffix = 0;
127  bool __bValid;
128  do
129  {
130  __qsName = _rqsName;
131  if( __bForceSuffix || __iSuffix ) __qsName += "("+QString::number( __iSuffix+1 ).prepend( QString( __iZeroPrefix, '0' ) ).right( __iZeroPrefix+1 )+")";
132  __bValid = true;
133  for( int __i = QTreeWidgetItem::childCount()-1; __i >= 0; __i-- )
134  {
135  if( __qsName == ((COverlayObject*)QTreeWidgetItem::child( __i ))->getName() )
136  {
137  __bValid = false;
138  __iSuffix++;
139  break;
140  }
141  }
142  }
143  while( !__bValid );
144  return __qsName;
145 }
[UI] Chart (view)
Definition: CChart.hpp:44
double getZoom() const
Returns the current zoom factor.
Definition: CChart.hpp:139
QRectF getDrawArea() const
Returns the viewport draw area.
Definition: CChart.hpp:127
QPointF getDatPosition() const
Returns the current (center) position (dataset coordinates) of the chart.
Definition: CChart.hpp:135
Generic overlay object.
const CChart * poChartPixmap
Chart for which the last rendering was achieved.
Definition: COverlay.hpp:61
QPixmap qPixmapBuffer
Pixmap buffer used to render and cache this overlay's graphical content.
Definition: COverlay.hpp:59
bool bForceRedraw
Forces this overlay's rendering (not matter its cache content)
Definition: COverlay.hpp:67
virtual void drawContent(const CChart *_poChart, QPainter *_pqPainter) const =0
Draws this overlay's content.
QString newChildName(const QString &_rqsName, int __iZeroPrefix=0, bool __bForceSuffix=false) const
Returns a valid name for a new sibling of this object.
Definition: COverlay.cpp:123
QPen qPenLine
QPen used to draw lines on this overlay.
Definition: COverlay.hpp:91
QPen qPenVector
QPen used to draw vectors on this overlay.
Definition: COverlay.hpp:94
QPen qPenText
QPen used to draw text on this overlay.
Definition: COverlay.hpp:76
void draw(const CChart *_poChart, QPainter *_pqPainter)
Draws this overlay.
Definition: COverlay.cpp:86
bool bVisible
Overlay items' global visibility status.
Definition: COverlay.hpp:56
QPen qPenMarkerSelected
QPen used to identify selected markers on this overlay.
Definition: COverlay.hpp:88
QFont qFont
QFont used to draw text on this overlay.
Definition: COverlay.hpp:70
QBrush qBrushMarker
QBrush used to draw markers on this overlay.
Definition: COverlay.hpp:79
COverlay(const QString &_rqsName)
Definition: COverlay.cpp:36
QBrush qBrushMarkerSelected
QBrush used to identify selected markers on this overlay.
Definition: COverlay.hpp:85
QPointF qPointFDatPositionPixmap
Chart position at which the last rendering was achieved.
Definition: COverlay.hpp:63
double fdZoomPixmap
Zoom factor at which the last rendering was achieved.
Definition: COverlay.hpp:65
QBrush qBrushText
QBrush used to draw text on this overlay.
Definition: COverlay.hpp:73
QPen qPenMarker
QPen used to draw markers on this overlay.
Definition: COverlay.hpp:82
QColor getColorSelected()
[Color] Returns the selected objects base color
Definition: CSettings.hpp:422
static CSettings * useSettings()