File indexing completed on 2024-04-21 05:43:43

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2006 by Jonathan Myers and David Saxton            *
0003  *   electronerd@electronerdia.net david@bluehaze.org                      *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "scopeviewbase.h"
0012 
0013 #include "oscilloscope.h"
0014 #include "oscilloscopedata.h"
0015 #include "probepositioner.h"
0016 
0017 #include <QPaintEvent>
0018 #include <QPainter>
0019 
0020 #include <ktechlab_debug.h>
0021 
0022 // for testing
0023 //#include <valgrind/callgrind.h>
0024 
0025 ScopeViewBase::ScopeViewBase(QWidget *parent)
0026     : QFrame(parent /* , Qt::WNoAutoErase*/)
0027     , b_needRedraw(true)
0028     , m_pixmap(nullptr)
0029     , m_halfOutputHeight(0.0)
0030 {
0031 }
0032 
0033 ScopeViewBase::~ScopeViewBase()
0034 {
0035     delete m_pixmap;
0036 }
0037 
0038 void ScopeViewBase::paintEvent(QPaintEvent *event)
0039 {
0040     QRect r = event->rect();
0041 
0042     if (b_needRedraw) {
0043         // CALLGRIND_TOGGLE_COLLECT();
0044 
0045         updateOutputHeight();
0046 
0047         if (!m_pixmap) {
0048             qCWarning(KTL_LOG) << "unexpected null pixmap in " << this;
0049             return;
0050         }
0051 
0052         QPainter p;
0053         // m_pixmap->fill( paletteBackgroundColor() ); // 2018.12.07
0054         m_pixmap->fill(palette().color(backgroundRole()));
0055         const bool startSuccess = p.begin(m_pixmap);
0056         if ((!startSuccess) || (!p.isActive())) {
0057             qCWarning(KTL_LOG) << " painter is not active";
0058         }
0059         p.setClipRegion(event->region());
0060 
0061         // let the subclass draw the background (grids, etc.)
0062         drawBackground(p);
0063 
0064         //      drawProbeMap(p, Oscilloscope::self()->m_logicProbeDataMap);
0065         //      drawProbeMap(p, Oscilloscope::self()->m_floatingProbeDataMap);
0066 
0067         p.setPen(Qt::black);
0068         p.drawRect(frameRect());
0069 
0070         b_needRedraw = false;
0071 
0072         // CALLGRIND_TOGGLE_COLLECT();
0073     }
0074 
0075     // bitBlt( this, r.x(), r.y(), m_pixmap, r.x(), r.y(), r.width(), r.height() ); // 2018.12.07
0076     QPainter p;
0077     const bool paintStarted = p.begin(this);
0078     if (!paintStarted) {
0079         qCWarning(KTL_LOG) << " failed to start painting ";
0080     }
0081     p.drawImage(r, m_pixmap->toImage(), r);
0082 }
0083 void ScopeViewBase::updateOutputHeight()
0084 {
0085     m_halfOutputHeight = int((Oscilloscope::self()->probePositioner->probeOutputHeight() - (probeArrowWidth / Oscilloscope::self()->numberOfProbes())) / 2) - 1;
0086 }
0087 
0088 void ScopeViewBase::resizeEvent(QResizeEvent *event)
0089 {
0090     delete m_pixmap;
0091     m_pixmap = new QPixmap(event->size());
0092     b_needRedraw = true;
0093     QFrame::resizeEvent(event);
0094 }
0095 /**
0096  * This is the main drawing loop function.
0097  */
0098 template<typename T> void ScopeViewBase::drawProbeMap(QPainter &p, QMap<int, T *> &map)
0099 {
0100     typedef typename QMap<int, T *>::iterator TheIterator;
0101     const TheIterator end = map.end();
0102     for (TheIterator it = map.begin(); it != end; ++it) {
0103         T *probe = it.value();
0104 
0105         if (probe->isEmpty())
0106             return;
0107 
0108         drawMidLine(p, probe);
0109 
0110         // Set the pen colour according to the colour the user has selected for the probe
0111         p.setPen(probe->color());
0112 
0113         drawProbe(p, probe);
0114     }
0115 }
0116 
0117 #include "moc_scopeviewbase.cpp"