All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines
Public Member Functions | Static Public Attributes | Protected Member Functions
SciFigs::Legend Class Reference

#include <Legend.h>

Inheritance diagram for SciFigs::Legend:
QGpCoreTools::XMLClass

List of all members.

Public Member Functions

void clear ()
void colorHSVInterpole (int imin, int imax)
void colorRGBInterpole (int imin, int imax)
int count () const
void defaultBW (int n=10)
void defaultColors (int n=10)
int indexOf (const QString &text) const
const LegendItemitem (int i)
 Legend ()
 Legend (const Legend &o)
 Legend (int n)
bool operator!= (const Legend &o) const
Legendoperator= (const Legend &o)
bool operator== (const Legend &o) const
const Penpen (int i) const
const Penpen (const QString &text) const
void penColorToSymbolBrush ()
void penColorToSymbolPen ()
void penColorToText ()
void resize (int n)
void setPen (int i, Pen p)
void setPens (const Legend &o)
void setSymbol (int i, Symbol s)
void setSymbols (const Legend &o)
void setText (int i, QString t)
void setTextColor (int i, QColor c)
void setTextColors (const Legend &o)
void setTexts (const QStringList &texts, const Pen &defaultPen, const Symbol &defaultSymbol)
void setTexts (const Legend &o)
const Symbolsymbol (int i) const
const Symbolsymbol (const QString &text) const
const QString & text (int i) const
const QColor & textColor (int i) const
QStringList texts () const
virtual const QString & xml_tagName () const
virtual ~Legend ()

Static Public Attributes

static const QString xmlLegendTag = "Legend"

Protected Member Functions

virtual XMLMember xml_member (XML_MEMBER_ARGS)
virtual void xml_writeChildren (XML_WRITECHILDREN_ARGS) const

Constructor & Destructor Documentation

{_textLookup=0;}
SciFigs::Legend::Legend ( const Legend o) [inline]
: XMLClass() {_textLookup=0; operator=(o);}
SciFigs::Legend::Legend ( int  n) [inline]
{_textLookup=0; resize(n);}
SciFigs::Legend::~Legend ( ) [virtual]

References TRACE.

{
  TRACE;
  delete _textLookup;
}

Member Function Documentation

References TRACE.

{
  TRACE;
  _items.clear();
  resetTextLookup();
}
void SciFigs::Legend::colorHSVInterpole ( int  imin,
int  imax 
)

References TRACE.

{
  TRACE;
  double h, h2, dh, s, s2, ds, v, v2, dv, a, a2, da;
  int n=_items.count();
  if(n <= 2) return ;
  if(imin < 0) imin=0;
  if(imax > n - 1) imax=n - 1;

  const QColor& c1=_items[ imin ].pen().color();
  h=c1.hue();
  s=c1.saturation();
  v=c1.value();
  a=c1.alpha();

  const QColor& c2=_items[ imax ].pen().color();
  h2=c2.hue();
  s2=c2.saturation();
  v2=c2.value();
  a2=c2.alpha();

  dh=(h2 - h)/(double) (imax - imin + 1);
  ds=(s2 - s)/(double) (imax - imin + 1);
  dv=(v2 - v)/(double) (imax - imin + 1);
  da=(a2 - a)/(double) (imax - imin + 1);

  QColor c;
  for(int i=imin + 1; i < imax; i++ ) {
    h += dh;
    s += ds;
    v += dv;
    a += da;
    c.setHsv(( int) h, (int) s, (int) v, (int) a);
    _items[ i ].pen().setColor(c);
  }
}
void SciFigs::Legend::colorRGBInterpole ( int  imin,
int  imax 
)

References TRACE.

{
  TRACE;
  double r, r2, dr, g, g2, dg, b, b2, db, a, a2, da;
  int n=_items.count();
  if(n <= 2) return ;

  const QColor& c1=_items[ imin ].pen().color();
  r=c1.red();
  g=c1.green();
  b=c1.blue();
  a=c1.alpha();

  const QColor& c2=_items[ imax ].pen().color();
  r2=c2.red();
  g2=c2.green();
  b2=c2.blue();
  a2=c2.alpha();

  dr=(r2 - r)/(double) (imax - imin + 1);
  dg=(g2 - g)/(double) (imax - imin + 1);
  db=(b2 - b)/(double) (imax - imin + 1);
  da=(a2 - a)/(double) (imax - imin + 1);

  QColor c;
  for(int i=imin + 1; i < imax; i++ ) {
    r += dr;
    g += dg;
    b += db;
    a += da;
    c.setRgb(( int) r, (int) g, (int) b, (int) a);
    _items[ i ].pen().setColor(c);
  }
}
int SciFigs::Legend::count ( ) const [inline]
void SciFigs::Legend::defaultBW ( int  n = 10)

References resize(), and TRACE.

{
  TRACE;
  resize(n);
  double truc, g;

  if(n>1) {
    for(int i=0; i < n; i++ ) {
      truc=(double) i/(double) (n - 1);
      g=1. - truc * truc;
      _items[ i ].pen().setColor(QColor( (int) (g * 255), (int) (g * 255), (int) (g * 255) ));
    }
  } else if(n==1) {
    _items[ 0 ].pen().setColor(Qt::black);
  }
}
void SciFigs::Legend::defaultColors ( int  n = 10)

References resize(), and TRACE.

Referenced by ProcessStatus::ProcessStatus().

{
  TRACE;
  resize(n);
  int i;
  switch (n) {
  case 1:
    _items[ 0 ].pen().setColor(QColor( 0, 0, 255) );
    break;
  case 2:
    _items[ 0 ].pen().setColor(QColor( 0, 0, 255) );
    _items[ 1 ].pen().setColor(QColor( 255, 0, 0) );
    break;
  case 3:
    _items[ 0 ].pen().setColor(QColor( 0, 0, 255) );
    _items[ 1 ].pen().setColor(QColor( 0, 255, 0) );
    _items[ 2 ].pen().setColor(QColor( 255, 0, 0) );
    break;
  default:
    double truc, r, g, b;

    int n1=n/3;
    int n2=n1 + (n - n1)/2;
    for(i=0; i < n1; i++ ) {
      truc=(double) i/(double) n1;
      r=0.;
      g=pow(truc, 0.3);
      b=1. - truc * truc;
      _items[ i ].pen().setColor(QColor( (int) (r * 255), (int) (g * 255), (int) (b * 255) ));
    }
    for(i=n1; i < n2; i++ ) {
      truc=(double) (i - n1)/(double) (n2 - n1);
      r=pow(truc, 0.3);
      g=1.;
      b=0.;
      _items[ i ].pen().setColor(QColor( (int) (r * 255), (int) (g * 255), (int) (b * 255) ));
    }
    for(i=n2; i < n; i++ ) {
      truc=(double) (i - n2)/(double) (n - n2 - 1);
      r=1.;
      g=1. - truc * truc;
      b=0.;
      _items[ i ].pen().setColor(QColor( (int) (r * 255), (int) (g * 255), (int) (b * 255) ));
    }
    break;
  }
}
int SciFigs::Legend::indexOf ( const QString &  text) const

Returns the index of item with text. This is a fast lookup with a map. The lookup map is initialized at first call. Once called, the lookup map is maintained by other operations. If there are various items with the same name, the returned index may be random (depends upon lookup initialization and maintenance).

References text(), and TRACE.

Referenced by pen(), MonoStation::AbstractSummary::setBubbleValues(), setTexts(), and symbol().

{
  TRACE;  
  if(!_textLookup) {
    _textLookup=new QMap<QString, int>;
    for(int i=_items.count()-1; i >=0; i--) {
      _textLookup->insert(_items[ i ].text(), i);
    }
  }
  QMap<QString, int>::const_iterator it;
  it=_textLookup->find(text);
  if(it!=_textLookup->end()) {
    return it.value();
  } else {
    return -1;
  }
}
const LegendItem & SciFigs::Legend::item ( int  i)

References TRACE.

Referenced by ProcessStatusDelegate::paint(), and ProcessStatusDelegate::sizeHint().

{
  TRACE;
  if(i >= _items.count()) i=_items.count() - 1;
  return _items.at(i);
}
bool SciFigs::Legend::operator!= ( const Legend o) const [inline]
{return !operator==(o);}
Legend & SciFigs::Legend::operator= ( const Legend o)

References TRACE.

{
  TRACE;
  _items=o._items;
  resetTextLookup();
  return *this;
}
bool SciFigs::Legend::operator== ( const Legend o) const

References TRACE.

{
  TRACE;
  int n=_items.count();
  if(n!=o._items.count()) return false;
  for(int i=0;i < n;i++ ) {
    if(_items[i]!=o._items[i]) return false;
  }
  return true;
}
const Pen & SciFigs::Legend::pen ( int  i) const
const Pen & SciFigs::Legend::pen ( const QString &  text) const

References indexOf(), SciFigs::Pen::null, and TRACE.

{
  TRACE;
  int i=indexOf(text);
  if(i > -1) {
    return _items[ i ].pen();
  } else {
    return Pen::null;
  }
}

References pen(), and TRACE.

{
  TRACE;
  for(int i=0;i < _items.count();i++ ) {
    Brush b=_items[ i ].symbol().brush();
    b.setColor(_items[ i ].pen().color());
    _items[ i ].symbol().setBrush(b);
  }
}

References pen(), SciFigs::Pen::setColor(), and TRACE.

{
  TRACE;
  for(int i=0;i < _items.count();i++ ) {
    Pen p=_items[ i ].symbol().pen();
    p.setColor(_items[ i ].pen().color());
    _items[ i ].symbol().setPen(p);
  }
}

References pen(), and TRACE.

{
  TRACE;
  for(int i=0;i < _items.count();i++ ) {
    _items[ i ].setTextColor(_items[ i ].pen().color());
  }
}
void SciFigs::Legend::resize ( int  n)
void SciFigs::Legend::setPen ( int  i,
Pen  p 
)

References resize(), and TRACE.

Referenced by SciFigs::NameLineLayer::legend(), SciFigs::LineLayer::legend(), SciFigs::LegendTableItem::setData(), and setTexts().

{
  TRACE;
  if(i >= _items.count()) {
    resize(i+1);
  }
  _items[i].pen()=p;
}
void SciFigs::Legend::setPens ( const Legend o)

References count(), pen(), and TRACE.

{
  TRACE;
  int n=count();
  if(n > o.count()) n=o.count();
  for(int i=0; i<n; i++ ) {
    _items[ i ].setPen(o.pen(i) );
  }
}
void SciFigs::Legend::setSymbol ( int  i,
Symbol  s 
)

References resize(), and TRACE.

Referenced by SciFigs::NameLineLayer::legend(), SciFigs::LineLayer::legend(), SciFigs::LegendTableItem::setData(), and setTexts().

{
  TRACE;
  if(i >= _items.count()) {
    resize(i+1);
  }
  _items[ i ].symbol()=s;
}
void SciFigs::Legend::setSymbols ( const Legend o)

References count(), symbol(), and TRACE.

{
  TRACE;
  int n=count();
  if(n > o.count()) n=o.count();
  for(int i=0; i<n; i++ ) {
    _items[ i ].setSymbol(o.symbol(i) );
  }
}
void SciFigs::Legend::setText ( int  i,
QString  t 
)

References resize(), and TRACE.

Referenced by SciFigs::NameLineLayer::legend(), SciFigs::LineLayer::legend(), SciFigs::LegendTableItem::setData(), setTexts(), and ProcessStatus::updateMisfitCurve().

{
  TRACE;
  if(i >= _items.count()) {
    resize(i+1);
  }
  _items[i].setText(t);
  resetTextLookup();
}
void SciFigs::Legend::setTextColor ( int  i,
QColor  c 
)

References resize(), and TRACE.

Referenced by SciFigs::NameLineLayer::legend(), and SciFigs::LegendTableItem::setData().

{
  TRACE;
  if(i >= _items.count()) {
    resize(i+1);
  }
  _items[i].setTextColor(c);
}

References count(), textColor(), and TRACE.

{
  TRACE;
  int n=count();
  if(n > o.count()) n=o.count();
  for(int i=0; i<n; i++ ) {
    _items[ i ].setTextColor(o.textColor(i) );
  }
}
void SciFigs::Legend::setTexts ( const QStringList &  texts,
const Pen defaultPen,
const Symbol defaultSymbol 
)

Sets legend items to match tests list. All items not in texts are removed and all texts not available in items are added at the end with the defaultPen and the defaultSymbol.

References count(), indexOf(), resize(), setPen(), setSymbol(), setText(), text(), and TRACE.

Referenced by MonoStation::AbstractSummary::setStations().

{
  TRACE;
  foreach(QString text, texts) {
    int index=indexOf(text);
    if(index==-1) {
      index=count();
      resize(index+1);
      setText(index, text);
      setPen(index, defaultPen);
      setSymbol(index, defaultSymbol);
    }
  }
  // Eventually remove unsused items, do not use erase() which is likely to
  // be unefficient if there are many entries to remove.
  if(texts.count()<count()) {
    QSet<QString> textSet;
    foreach(QString text, texts) {
      textSet.insert(text);
    }
    QVector<LegendItem> newItems;
    int n=count();
    newItems.reserve(n);
    for(int i=0; i<n; i++) {
      if(textSet.contains(text(i))) {
        newItems.append(_items.at(i));
      }
    }
    _items=newItems;
    resetTextLookup();
  }
}
void SciFigs::Legend::setTexts ( const Legend o)

References count(), text(), and TRACE.

{
  TRACE;
  int n=count();
  if(n > o.count()) n=o.count();
  for(int i=0; i<n; i++ ) {
    _items[ i ].setText(o.text(i) );
  }
  resetTextLookup();
}
const Symbol & SciFigs::Legend::symbol ( int  i) const
const Symbol & SciFigs::Legend::symbol ( const QString &  text) const

References indexOf(), SciFigs::Symbol::null, and TRACE.

{
  TRACE;
  int i=indexOf(text);
  if(i > -1) {
    return _items[ i ].symbol();
  } else {
    return Symbol::null;
  }
}
const QString & SciFigs::Legend::text ( int  i) const

References TRACE.

Referenced by SciFigs::LegendTableItem::data(), indexOf(), SciFigs::LegendWidget::paint(), SciFigs::PaletteInterpole::setColors(), and setTexts().

{
  TRACE;
  if(i >= _items.count()) i=_items.count() - 1;
  return _items[ i ].text();
}
const QColor & SciFigs::Legend::textColor ( int  i) const

References TRACE.

Referenced by SciFigs::LegendTableItem::data(), SciFigs::LegendWidget::paint(), MonoStation::AbstractSummary::setBubbleValues(), SciFigs::NameLineLayer::setLegend(), and setTextColors().

{
  TRACE;
  if(i >= _items.count()) i=_items.count() - 1;
  return _items[ i ].textColor();
}
QStringList SciFigs::Legend::texts ( ) const

References TRACE.

{
  TRACE;
  QStringList list;
  for(int i=0;i < _items.count();i++ ) list.append(_items[ i ].text());
  return list;
}

Re-implement this function to offer XML restore (children and properties) support to your class.

From tag and map (with contains the attibute value) return a unique identifier under the format of a XMLMember. XMLMember is initialized with 3 types of contructors:

  • An integer: id number of a property
  • A XMLClass * : a child of this object identified by tag
  • Default constructor: error, unknow child or property

Map of attributes can be inspected in this way (can be achived also in xml_setProperty()):

    static const QString tmp("childrenName");
    XMLRestoreAttributeIterator it=map.find(tmp);
    if(it!=map.end()) {
      // found attribute "childrenName"
    }

If the map of attributes is not used:

    Q_UNUSED(attributes);
    if(tag=="x1") return XMLMember(0);
    else if(tag=="y1") return XMLMember(1);
    else if(tag=="x2") return XMLMember(2);
    else if(tag=="y2") return XMLMember(3);
    else return XMLMember(XMLMember::Unknown);

Arithmetic operations + and - apply to XMLMember to avoid confusion of property id numbers between inherited objects. Offset 3 corresponds to the number of properties defined in this object.

    if(tag=="anInteger") return XMLMember(0);
    else if(tag=="aString") return XMLMember(1);
    else if(tag=="aDouble") return XMLMember(2);
    return AbstractLine::xml_member(tag, attributes, context)+3;

For the arguments of this function use Macro XML_MEMBER_ARGS.

Reimplemented from QGpCoreTools::XMLClass.

References resize(), and TRACE.

{
  TRACE;
  Q_UNUSED(attributes);
  Q_UNUSED(context);
  if(tag=="LegendItem" ) {
    resize(_items.count() + 1);
    return XMLMember(&_items[ _items.count() - 1 ] );
  }
  return XMLMember(XMLMember::Unknown);
}
virtual const QString& SciFigs::Legend::xml_tagName ( ) const [inline, virtual]

Implements QGpCoreTools::XMLClass.

{return xmlLegendTag;}
void SciFigs::Legend::xml_writeChildren ( XML_WRITECHILDREN_ARGS  ) const [protected, virtual]

Reimplemented from QGpCoreTools::XMLClass.

References TRACE, and QGpCoreTools::XMLClass::xml_save().

{
  TRACE;
  for(int i=0;i < _items.count();i++ ) _items[ i ].xml_save(s, context);
}

Member Data Documentation

const QString SciFigs::Legend::xmlLegendTag = "Legend" [static]

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines