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

Draw a legend in a GraphicSheet. More...

#include <LegendWidget.h>

Inheritance diagram for SciFigs::LegendWidget:
SciFigs::GraphicObject QGpGuiTools::PropertyContext QGpGuiTools::PropertyItem QGpCoreTools::XMLClass

List of all members.

Public Slots

Q_SCRIPTABLE void load (QString fileName)
Q_SCRIPTABLE void save (QString fileName)

Signals

void changed (Legend leg)

Public Member Functions

virtual void addProperties (PropertyProxy *pp)
bool adjustBox () const
QString fontString () const
const Legendlegend () const
Legendlegend ()
 LegendWidget (QWidget *parent=0, Qt::WFlags f=0)
double lineSize () const
LegendWidgetoperator= (const LegendWidget &o)
virtual void polish ()
virtual void properties (PropertyWidget *w) const
virtual void removeProperties (PropertyProxy *pp)
void resize (int n)
void setAdjustBox (bool t)
void setFontString (QString fs)
void setLineSize (double ls)
virtual void setProperty (uint wid, int pid, QVariant val)
void setTitle (QString t)
QString title () const
virtual void update ()
virtual const QString & xml_tagName () const
 ~LegendWidget ()

Static Public Attributes

static const QString xmlLegendWidgetTag = "LegendWidget"

Protected Member Functions

virtual void paint (QPainter &p, double dotpercm, int w, int h, bool mask)
virtual void setPrintSize (double dotpercm)
XMLMember xml_member (XML_MEMBER_ARGS)
void xml_writeChildren (XML_WRITECHILDREN_ARGS) const

Properties

bool adjustBox
QString font
double lineSize
double lineWidth
QString title

Detailed Description

Draw a legend in a GraphicSheet.

This is graphical legend that lets you map pen and symbols to a string expression


Constructor & Destructor Documentation

SciFigs::LegendWidget::LegendWidget ( QWidget *  parent = 0,
Qt::WFlags  f = 0 
)

Description of constructor still missing

References setTitle(), QGpCoreTools::tr(), and TRACE.

    : GraphicObject(parent, f)
{
  TRACE;
  setTitle(tr( "Title" ));
  _lineSize=1.5;
  _adjustBox=true;
  adjustSize();
}

References TRACE.

{
  TRACE;
}

Member Function Documentation

Setup property editor

Reimplemented from SciFigs::GraphicObject.

References QGpGuiTools::PropertyProxy::addCategory(), QGpGuiTools::PropertyProxy::addReference(), QGpGuiTools::PropertyProxy::addTab(), SciFigs::LegendTable::All, QGpGuiTools::PropertyProxy::setCurrentCategory(), QGpGuiTools::PropertyProxy::setCurrentTab(), SciFigs::LegendProperties::setPropertySections(), QGpCoreTools::tr(), and TRACE.

{
  TRACE;
  GraphicObject::addProperties(pp);
  if( !pp->setCurrentCategory(_category) ) {
    pp->addCategory(_category, tr("Legend"), QIcon( ":/images/LegendWidget.png"));
  }
  if(pp->setCurrentTab(_tabLegend)) {
    pp->addReference(this);
  } else {
    LegendProperties * w=new LegendProperties;
    w->setPropertySections(LegendTable::All);
    pp->addTab(_tabLegend, tr("Legend"), w, this);
  }
  if(pp->setCurrentTab(_tabFormat)) {
    pp->addReference(this);
  } else {
    LegendFormatProperties * w=new LegendFormatProperties;
    pp->addTab(_tabFormat, tr("Format"), w, this);
  }
}
bool SciFigs::LegendWidget::adjustBox ( ) const [inline]

Referenced by properties().

{return _adjustBox;}
void SciFigs::LegendWidget::changed ( Legend  leg) [signal]

Referenced by load(), and setProperty().

QString SciFigs::LegendWidget::fontString ( ) const [inline]
{return font().toString();}
const Legend& SciFigs::LegendWidget::legend ( ) const [inline]
{return _legend;}
double SciFigs::LegendWidget::lineSize ( ) const [inline]

Referenced by paint(), and properties().

{return _lineSize;}
void SciFigs::LegendWidget::load ( QString  fileName) [slot]

References changed(), SciFigs::XMLSciFigs::Data, QGpCoreTools::XMLErrorReport::exec(), legend(), SciFigs::XMLSciFigs::restoreFile(), QGpCoreTools::XMLErrorReport::setFileName(), QGpCoreTools::XMLErrorReport::setTitle(), QGpCoreTools::tr(), and TRACE.

{
  TRACE;
  if(fileName.length()>0) {
    XMLErrorReport xmler(XMLErrorReport::Read);
    xmler.setTitle(tr("Load legend"));
    xmler.setFileName(fileName);
    XMLSciFigs s;
    xmler.exec(s.restoreFile(fileName,&_legend,XMLSciFigs::Data));
    emit changed(legend());
  }
}
LegendWidget & SciFigs::LegendWidget::operator= ( const LegendWidget o)

References TRACE.

{
  TRACE;
  _legend=o._legend;
  GraphicObject::operator=(o);
  _title=o._title;
  _lineSize=o._lineSize;
  _adjustBox=o._adjustBox;
  return *this;
}
void SciFigs::LegendWidget::paint ( QPainter &  p,
double  dotpercm,
int  w,
int  h,
bool  mask 
) [protected, virtual]

Virtual function to be re-implemented for each type of object to paint specific content. It is different than the classical QWidget function of the same type. Actually, the needed resolution is passed as argument as well as the screen width and height of the object. It is called by print().

Nothing is done, calling it in re-implemented function is useless.

Reimplemented from SciFigs::GraphicObject.

References SciFigs::Legend::count(), font, lineSize(), MAX_INT, SciFigs::Symbol::paint(), SciFigs::Symbol::pen(), SciFigs::Legend::pen(), SciFigs::Pen::qpen(), SciFigs::Symbol::size(), SciFigs::Legend::symbol(), SciFigs::Legend::text(), SciFigs::Legend::textColor(), TRACE, and SciFigs::Pen::width().

{
  TRACE;
  p.save();
  // Calculate the height of text
  p.setFont(font());
  int fontHeight=p.fontMetrics().height();
  int y=0;
  int spacing=(int)round(0.2*dotpercm);
  QRect r;
  // Draw title
  if(!_title.isEmpty()) {
    r=p.boundingRect(0, 0, w, MAX_INT, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, _title);
    p.drawText(0, y, w, r.height(), Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, _title);
    y+=r.height()+spacing;
  }
  // Draw items
  int lineSize=(int) floor(_lineSize * dotpercm + 0.5);
  QPen textPen(Qt::black, 1, Qt::SolidLine);
  for(int i=0 ; i<_legend.count(); i++) {
    int lineHeight=(int)round(_legend.pen(i).width()*0.1*dotpercm);
    int symHeight=(int)round((_legend.symbol(i).size()+_legend.symbol(i).pen().width())*0.1*dotpercm);
    int h=lineHeight<symHeight ? symHeight : lineHeight;
    if(!_legend.text(i).isEmpty()) {
      r=p.boundingRect(0, 0, w-lineSize-spacing, MAX_INT, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap,_legend.text(i));
      if(r.height()>h) h=r.height();
      r.translate(lineSize+spacing, y+((h-r.height()) >> 1));
      p.setPen(_legend.textColor(i));
      p.drawText(r, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, _legend.text(i));
    } else {
      if(fontHeight>h) h=fontHeight;
    }
    int yc=y+(h >> 1);
    y+=h+spacing;
    p.save();
    p.setPen(_legend.pen(i).qpen(dotpercm));
    p.drawLine(0, yc, lineSize, yc);
    _legend.symbol(i).paint(p, QPointF(lineSize >> 1, yc), dotpercm);
    p.restore();
  }
  p.restore();
}
void SciFigs::LegendWidget::polish ( ) [virtual]

Ensure that the widget is at least updated once.

Reimplemented from SciFigs::GraphicObject.

References TRACE.

{
  TRACE;
  adjustSize();
  GraphicObject::polish();
}
void SciFigs::LegendWidget::properties ( PropertyWidget w) const [virtual]

Cleanup property editor

Reimplemented from SciFigs::GraphicObject.

References QGpGuiTools::PropertyProxy::removeTab(), QGpGuiTools::PropertyProxy::setCurrentCategory(), and TRACE.

{
  TRACE;
  GraphicObject::removeProperties(pp);
  if(pp->setCurrentCategory(_category) ) {
    pp->removeTab(_tabLegend, this);
    pp->removeTab(_tabFormat, this);
  }
}
void SciFigs::LegendWidget::resize ( int  n) [inline]
{_legend.resize(n);}
void SciFigs::LegendWidget::save ( QString  fileName) [slot]

References SciFigs::XMLSciFigs::Data, QGpCoreTools::XMLErrorReport::exec(), SciFigs::XMLSciFigs::saveFile(), QGpCoreTools::XMLErrorReport::setFileName(), QGpCoreTools::XMLErrorReport::setTitle(), QGpCoreTools::tr(), and TRACE.

{
  TRACE;
  if(fileName.length()>0) {
    XMLErrorReport xmler(XMLErrorReport::Write);
    xmler.setTitle(tr("Save legend"));
    xmler.setFileName(fileName);
    XMLSciFigs s;
    xmler.exec(s.saveFile(fileName,&_legend,XMLSciFigs::Data));
  }
}
void SciFigs::LegendWidget::setAdjustBox ( bool  t) [inline]

Referenced by setProperty().

{_adjustBox=t;}
void SciFigs::LegendWidget::setFontString ( QString  fs) [inline]
{QFont f;f.fromString(fs);setFont(f);}
void SciFigs::LegendWidget::setLineSize ( double  ls) [inline]

Referenced by setProperty().

{_lineSize=ls;}
void SciFigs::LegendWidget::setPrintSize ( double  dotpercm) [protected, virtual]

Called before and after printing to adjust the size of object. Normally what you see is what you get, but there might be some slight modification due to rounding error when scaling fonts. All object that offer options to automatically adjust size and if it depends upon fonts must re-implement this function.

The basic implemetation does nothing.

Reimplemented from SciFigs::GraphicObject.

References SciFigs::GraphicObject::setPrintHeight(), SciFigs::GraphicObject::setPrintWidth(), and w.

{
  if(_adjustBox) {
    int w, h;
    adjustSize(dotpercm, w, h);
    setPrintWidth(( w + 1)/dotpercm);
    setPrintHeight(( h + 1)/dotpercm);
  }
}
void SciFigs::LegendWidget::setProperty ( uint  wid,
int  pid,
QVariant  val 
) [virtual]

Set value val to property id, after a user action in property editor

Reimplemented from SciFigs::GraphicObject.

References SciFigs::LegendFormatProperties::AdjustBox, changed(), SciFigs::LegendFormatProperties::Font, legend(), SciFigs::LegendFormatProperties::LineSize, setAdjustBox(), setLineSize(), setTitle(), SciFigs::LegendFormatProperties::Title, TRACE, and update().

{
  TRACE;
  if(wid==_tabLegend) {
    legend()= val.value<Legend>();
    update();
    emit changed(legend());
  } else if(wid==_tabFormat) {
    switch(pid) {
    case LegendFormatProperties::Title:
      setTitle(val.toString());
      break;
    case LegendFormatProperties::Font: {
        QFont f;
        if(f.fromString(val.toString()) ) {
          setFont(f);
        }
      }
      break;
    case LegendFormatProperties::LineSize:
      setLineSize(val.toDouble());
      break;
    case LegendFormatProperties::AdjustBox:
      setAdjustBox(val.toBool());
      break;
    default:
      break;
    }
    update();
  } else {
    GraphicObject::setProperty(wid, pid, val);
  }
}
void SciFigs::LegendWidget::setTitle ( QString  t) [inline]
QString SciFigs::LegendWidget::title ( ) const [inline]

Referenced by properties().

{return _title;}
void SciFigs::LegendWidget::update ( ) [virtual]

Reimplemented from SciFigs::GraphicObject.

References TRACE.

Referenced by ResultsThread::createLegend(), and setProperty().

{
  TRACE;
  adjustSize();
  GraphicObject::update();
}

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 SciFigs::GraphicObject.

References SciFigs::XMLSciFigs::data(), and TRACE.

{
  TRACE;
  XMLSciFigs * scifigsContext=static_cast<XMLSciFigs *>(context);
  if(scifigsContext->data()) {
    if(tag=="Legend") return XMLMember(&_legend);
  }
  return GraphicObject::xml_member(tag, attributes, context);
}
virtual const QString& SciFigs::LegendWidget::xml_tagName ( ) const [inline, virtual]

Reimplemented from SciFigs::GraphicObject.

Reimplemented from QGpCoreTools::XMLClass.

References SciFigs::XMLSciFigs::data(), TRACE, and QGpCoreTools::XMLClass::xml_save().

{
  TRACE;
  XMLSciFigs * scifigsContext=static_cast<XMLSciFigs *>(context);
  if(scifigsContext->data()) _legend.xml_save(s, context);
}

Member Data Documentation

const QString SciFigs::LegendWidget::xmlLegendWidgetTag = "LegendWidget" [static]

Property Documentation

QString SciFigs::LegendWidget::font [read, write]

Referenced by paint(), and properties().

double SciFigs::LegendWidget::lineSize [read, write]
double SciFigs::LegendWidget::lineWidth [read, write]
QString SciFigs::LegendWidget::title [read, write]

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