legend.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:8k
- /*
- * ===========================================================================
- * PRODUCTION $Log: legend.cpp,v $
- * PRODUCTION Revision 1000.1 2004/06/01 20:49:29 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.6
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: legend.cpp,v 1000.1 2004/06/01 20:49:29 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Andrey Yazhuk
- *
- * File Description:
- *
- */
- #include <ncbi_pch.hpp>
- #include <gui/graph/legend.hpp>
- BEGIN_NCBI_SCOPE
- ///////////////////////////////////////////////////////////////////////////////
- /// class CLegend
- CLegend::CLegend()
- : m_Font(CGlBitmapFont::eHelvetica12),
- m_BackColor(0.95f, 0.95f, 0.95f),
- m_BorderColor(0.0f, 0.0f, 0.0f),
- m_bHorz(true),
- m_Space(10)
- {
- }
- CLegend::~CLegend()
- {
- }
- void CLegend::SetBackColor(const CGlColor& Color)
- {
- m_BackColor = Color;
- }
- const CGlColor& CLegend::GetBackColor() const
- {
- return m_BackColor;
- }
- void CLegend::SetBorderColor(const CGlColor& Color)
- {
- m_BorderColor = Color;
- }
- const CGlColor& CLegend::GetBorderColor()
- {
- return m_BorderColor;
- }
- bool CLegend::SetDataSource(IGraphDataSource* pDS)
- {
- ILegendDataSource* pLegendDS = dynamic_cast<ILegendDataSource*>(pDS);
- bool bOk = pLegendDS!= NULL;
- CGraphBase::SetDataSource(bOk ? pDS : NULL);
-
- CalculateLimits();
- return bOk;
- }
- const TModelRect& CLegend::GetLimits() const
- {
- return m_Limits;
- }
- void CLegend::CalculateLimits()
- {
- m_Limits.Init(0, 0, 1, 1);
- }
- void CLegend::Render(CGlPane* pPane)
- {
- if(pPane) {
- pPane->OpenPixels();
- try {
- TVPRect rcVP = pPane->GetViewport();
-
- // fill background
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- glColorC(m_BackColor);
- glRecti(rcVP.Left(), rcVP.Bottom(), rcVP.Right(), rcVP.Top());
-
- // draw Border
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- glColorC(m_BorderColor);
- glRecti(rcVP.Left(), rcVP.Bottom(), rcVP.Right(), rcVP.Top());
- x_RenderItems(pPane);
- }
- catch(...) {
- //need to trace it
- }
- pPane->Close();
- }
- }
- void CLegend::x_RenderItems(CGlPane* pPane)
- {
- ILegendDataSource* pSource = GetLegendDataSource();
- if (pSource) {
- IStringArray* pLabels = pSource->GetLabelArray();
- IColorArray* pColors = pSource->GetColorArray();
- INumericArray* pMarkers = pSource->GetMarkerArray();
-
- int H = max( (int)m_Font.TextHeight(), 10);
- TVPRect rcVP = pPane->GetViewport();
- int LeftLimit = rcVP.Left() + m_Space;
- int RightLimit = rcVP.Right() - m_Space;
- int BottomLimit = rcVP.Bottom() + m_Space;
- int vpX = LeftLimit;
- int StepY = H + max(H / 2, 4);
- int vpY = rcVP.Top() - m_Space - StepY;
- m_BoxW = pSource->ShowMarkers() ? H * 2 : H;
- m_BoxH = H;
-
- int MaxX = LeftLimit;
-
- int N = pLabels->GetSize();
- for ( int i = 0; i < N; i++ ) { // iterating by items
- string sText = pLabels->GetElem(i);
- int W = m_BoxW + m_Space + (int)m_Font.TextWidth(sText.c_str());
- int iMarker = pSource->ShowMarkers() ? (int) pMarkers->GetElem(i) : -1;
- if( m_bHorz) {
- if (vpX + W > RightLimit) { // new line
- vpY -= StepY;
- vpX = LeftLimit;
- }
- x_RenderItem(vpX, vpY, sText, pColors->GetElem(i), iMarker);
-
- vpX += W + m_Space * 2;
- } else {
- x_RenderItem(vpX, vpY, sText, pColors->GetElem(i), iMarker);
-
- MaxX = max(MaxX, vpX + W);
- vpY -= StepY;
- if (vpY < BottomLimit) { // new column
- vpY = rcVP.Top() - m_Space - StepY;
- vpX = MaxX + m_Space;
- }
- }
- }
- }
- }
- void CLegend::x_RenderItem(int X, int Y, const string& sLabel, const CGlColor& Color, int iMarker)
- {
- CGraphDotMarker::EMarkerType Type = static_cast<CGraphDotMarker::EMarkerType>(iMarker);
-
- switch (Type) {
- case CGraphDotMarker::eNone:
- case CGraphDotMarker::eRect:
- case CGraphDotMarker::eDiamond:
- case CGraphDotMarker::eTriangle:
- case CGraphDotMarker::eCross: {
- //draw line
- glColorC(Color);
- glBegin(GL_LINES);
- glVertex2i(X, Y + m_BoxH / 2);
- glVertex2i(X + m_BoxW, Y + m_BoxH / 2);
- glEnd();
- // draw marker
- TModelUnit MarkerSize = (m_BoxH % 2) ? m_BoxH : (m_BoxH - 1);
- CGraphDotMarker::RenderMarker(X + m_BoxW / 2, Y + m_BoxH / 2, MarkerSize, MarkerSize, Type);
- break;
- }
- default: {
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- glColorC(Color);
- glRectd(X, Y, X + m_BoxW, Y + m_BoxH);
-
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- glColorC(m_BorderColor);
- glRectd(X, Y, X + m_BoxW, Y + m_BoxH);
- }
- } //switch
- glColorC(Color);
- m_Font.TextOut(X + m_BoxW + m_Space, Y, sLabel.c_str());
- }
- ///////////////////////////////////////////////////////////////////////////////
- /// class CLegendDataSource
- void CLegendDataSource::CreateArrays()
- {
- CSeriesBase::CreateArrays();
- TStrAdapter* pStrAd = new TStrAdapter(m_Length);
- AddArray(static_cast<IDataArray*>(pStrAd));
-
- TColorAdapter* pCAd = new TColorAdapter(m_Length);
- AddArray(static_cast<IDataArray*>(pCAd));
- TEnumAdapter* pEnAd = new TEnumAdapter(m_Length);
- AddArray(static_cast<IDataArray*>(pEnAd));
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: legend.cpp,v $
- * Revision 1000.1 2004/06/01 20:49:29 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.6
- *
- * Revision 1.6 2004/05/21 22:27:42 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.5 2003/09/17 16:24:16 dicuccio
- * Use CGlBitmapFont instead of CGlutFont
- *
- * Revision 1.4 2003/08/19 00:35:52 dicuccio
- * Fixed compilation errors following API change for IGlFont
- *
- * Revision 1.3 2003/08/11 16:10:57 yazhuk
- * Compilation fixes for GCC
- *
- * Revision 1.2 2003/08/08 15:59:36 yazhuk
- * Comments added
- *
- * ===========================================================================
- */
-