// $Revision: 1.9 $ // StandardGraph.C // Copyright (C) 1994, 1995 Taligent, Inc. All rights reserved. #ifndef TaligentSamples_STANDARDGRAPH #include "StandardGraph.h" #endif #ifndef Taligent_GRAFPORT #include // for TLinkedModelMatrixPort #endif //============================================================================== // TStandardGraph TGPoint TStandardGraph_kDefaultAxisLengths(100., 100.); const TGPoint& TStandardGraph::kDefaultAxisLengths = TStandardGraph_kDefaultAxisLengths; TGraphRange TStandardGraph_kDefaultXAxisRange(0., 100.); const TGraphRange& TStandardGraph::kDefaultXAxisRange = TStandardGraph_kDefaultXAxisRange; TGraphRange TStandardGraph_kDefaultYAxisRange(0., 100.); const TGraphRange& TStandardGraph::kDefaultYAxisRange = TStandardGraph_kDefaultYAxisRange; TaligentTypeExtensionMacro(TStandardGraph); TStandardGraph::TStandardGraph() : MGraphic(), fXAxisRange(kDefaultXAxisRange), fYAxisRange(kDefaultYAxisRange), fAxisLengths(kDefaultAxisLengths), fMatrix(), fDrawers(NIL, new TPolymorphicStreamer) { } TStandardGraph::TStandardGraph(const TGraphRange& xRange, const TGraphRange& yRange, const TGPoint& axisLengths) : MGraphic(), fXAxisRange(xRange), fYAxisRange(yRange), fAxisLengths(axisLengths), fMatrix(), fDrawers(NIL, new TPolymorphicStreamer) { } TStandardGraph::TStandardGraph(const TStandardGraph& source) : MGraphic(source), fXAxisRange(source.fXAxisRange), fYAxisRange(source.fYAxisRange), fAxisLengths(source.fAxisLengths), fMatrix(source.fMatrix), fDrawers(NIL, new TPolymorphicStreamer) { CopyDrawers(source.fDrawers); } TStandardGraph::~TStandardGraph() { fDrawers.DeleteAll(); } TStandardGraph& TStandardGraph::operator=(const TStandardGraph& source) { if (&source != this) { MGraphic::operator=(source); fXAxisRange = source.fXAxisRange; fYAxisRange = source.fYAxisRange; fAxisLengths = source.fAxisLengths; fMatrix = source.fMatrix; fDrawers.DeleteAll(); CopyDrawers(source.fDrawers); } return *this; } TStream& TStandardGraph::operator>>=(TStream& toStream) const { ::WriteVersion(toStream, kOriginalVersion); MGraphic::operator>>=(toStream); fXAxisRange >>= toStream; fYAxisRange >>= toStream; fAxisLengths >>= toStream; fMatrix >>= toStream; fDrawers >>= toStream; return toStream; } TStream& TStandardGraph::operator<<=(TStream& fromStream) { ::ReadVersion(fromStream, kOriginalVersion, kOriginalVersion); MGraphic::operator<<=(fromStream); fXAxisRange <<= fromStream; fYAxisRange <<= fromStream; fAxisLengths <<= fromStream; fMatrix <<= fromStream; fDrawers.DeleteAll(); fDrawers <<= fromStream; return fromStream; } void TStandardGraph::Draw(TGrafPort& port) const { TLinkedModelMatrixPort matrixPort(&port, fMatrix); TDeleterFor > iterator = CreateDrawerIterator(); for (TGraphDrawer* drawer = iterator->First(); drawer != NIL; drawer = iterator->Next()) { drawer->DrawIntoGraph(matrixPort); } } TGRect TStandardGraph::GetGeometricBounds() const { TGRect totalBounds(TGRect::kZeroRect); TDeleterFor > iterator = CreateDrawerIterator(); for (TGraphDrawer* drawer = iterator->First(); drawer != NIL; drawer = iterator->Next()) { totalBounds.ExtendTo(drawer->GetBounds()); } totalBounds = fMatrix.TransformBounds(totalBounds); return totalBounds; } void TStandardGraph::TransformBy(const TGrafMatrix& matrix) { fMatrix.ConcatWith(matrix); } const TGPoint& TStandardGraph::GetAxisLengths() const { return fAxisLengths; } const TGraphRange& TStandardGraph::GetXRange() const { return fXAxisRange; } const TGraphRange& TStandardGraph::GetYRange() const { return fYAxisRange; } void TStandardGraph::GetRanges(TGraphRange& xRange, TGraphRange& yRange) const { xRange = fXAxisRange; yRange = fYAxisRange; } void TStandardGraph::SetAxisLengths(const TGPoint& axisLengths, bool prepareDrawers) { fAxisLengths = axisLengths; if (prepareDrawers) { PrepareAllDrawers(); } } void TStandardGraph::SetXRange(const TGraphRange& range, bool prepareDrawers) { fXAxisRange = range; if (prepareDrawers) { PrepareAllDrawers(); } } void TStandardGraph::SetYRange(const TGraphRange& range, bool prepareDrawers) { fYAxisRange = range; if (prepareDrawers) { PrepareAllDrawers(); } } void TStandardGraph::SetRanges(const TGraphRange& xRange, const TGraphRange& yRange, bool prepareDrawers) { fXAxisRange = xRange; fYAxisRange = yRange; if (prepareDrawers) { PrepareAllDrawers(); } } TGPoint TStandardGraph::MapValuesToPoint(GraphValue xValue, GraphValue yValue) const { TGPoint scaledPoint; scaledPoint.fX = (xValue - GetXRange().GetMin()) * (GetAxisLengths().fX / GetXRange().GetLength()); // Note: It's subtracted from the y-axis length because 2-D coordinates are // backward for the y-axis. scaledPoint.fY = GetAxisLengths().fY - (((yValue - GetYRange().GetMin()) / GetYRange().GetLength()) * GetAxisLengths().fY); return scaledPoint; } TGPoint TStandardGraph::MapValuesToPoint(const TGPoint& valuesToBeConverted) const { return MapValuesToPoint(valuesToBeConverted.fX, valuesToBeConverted.fY); } void TStandardGraph::AdoptDrawer(TGraphDrawer* drawer) { fDrawers.Add(drawer); drawer->SetTargetGraph(this); } TGraphDrawer* TStandardGraph::OrphanDrawer(const TGraphDrawer& drawer) { TGraphDrawer* orphan = fDrawers.Remove(drawer); return orphan; } void TStandardGraph::DeleteAllDrawers() { fDrawers.DeleteAll(); } void TStandardGraph::PrepareAllDrawers() const { TDeleterFor > iterator = CreateDrawerIterator(); for (TGraphDrawer* drawer = iterator->First(); drawer != NIL; drawer = iterator->Next()) { drawer->Prepare(*this); } } TIteratorOver* TStandardGraph::CreateDrawerIterator() const { TIteratorOver* iterator = new TDequeOfIterator(&fDrawers); return iterator; } const TGrafMatrix& TStandardGraph::GetMatrix() const { return fMatrix; } void TStandardGraph::CopyDrawers(const TCollectionOf& source) { TDeleterFor > iterator = source.CreateIterator(); for (TGraphDrawer* drawer = iterator->First(); drawer != NIL; drawer = iterator->Next()) { fDrawers.Add(::CopyPointer(drawer)); } }