[Kst] branches/work/kst/portto4/kst/src

Barth Netterfield netterfield at astro.utoronto.ca
Wed Mar 9 04:19:04 CET 2011


SVN commit 1224221 by netterfield:

Significantly improve automatic legend names.
I think I now do a reasonable job with all cases, except the case where
units are not consistent between all relations in a plot.

Please look for troubles.



 M  +3 -3      libkst/datavector.cpp  
 A             libkst/labelinfo.cpp   [License: GPL (v2+)]
 M  +1 -0      libkst/labelinfo.h  
 M  +52 -4     libkstapp/legenditem.cpp  
 M  +1 -1      libkstapp/legenditem.h  
 M  +1 -1      libkstapp/plotitem.cpp  
 M  +7 -36     libkstapp/plotrenderitem.cpp  
 M  +0 -1      libkstapp/plotrenderitem.h  


--- branches/work/kst/portto4/kst/src/libkst/datavector.cpp #1224220:1224221
@@ -328,12 +328,12 @@
     label_info.units = QString();
   }
 
-  label_info.name = _field;
+  label_info.name = descriptiveName();// _field;
 
   // un-escape escaped special characters so they aren't escaped 2x.
-  label_info.name.replace("\\_", "_").replace("\\^","^").replace("\\[", "[").replace("\\]", "]");
+  //label_info.name.replace("\\_", "_").replace("\\^","^").replace("\\[", "[").replace("\\]", "]");
   // now escape the special characters.
-  label_info.name.replace('_', "\\_").replace('^', "\\^").replace('[', "\\[").replace(']', "\\]");
+  //label_info.name.replace('_', "\\_").replace('^', "\\^").replace('[', "\\[").replace(']', "\\]");
 
   return label_info;
 }
--- branches/work/kst/portto4/kst/src/libkst/labelinfo.h #1224220:1224221
@@ -31,6 +31,7 @@
   QString name;
   QString quantity;
   QString units;
+  QString singleRenderItemLabel() const;
 };
 
 }
--- branches/work/kst/portto4/kst/src/libkstapp/legenditem.cpp #1224220:1224221
@@ -84,10 +84,58 @@
   QFont font(_font);
   font.setPointSizeF(view()->defaultFont(_fontScale).pointSizeF());
 
-  foreach(RelationPtr relation, legendItems) {
+  // generate string list of relation names
+  QStringList names;
+  int count = legendItems.count();
+  bool allAuto = true;
+  bool sameX = true;
+  LabelInfo label_info = legendItems.at(0)->xLabelInfo();
+
+  for (int i = 0; i<count; i++) {
+    RelationPtr relation = legendItems.at(i);
+    if (relation->descriptiveNameIsManual()) {
+      allAuto = false;
+    }
+    if (relation->xLabelInfo() != label_info) {
+      sameX = false;
+    }
+  }
+
+  if (!allAuto) {
+    for (int i = 0; i<count; i++) {
+      names.append(legendItems.at(i)->descriptiveName());
+    }
+  } else {
+    for (int i = 0; i<count; i++) {
+      RelationPtr relation = legendItems.at(i);
+      label_info = relation->titleInfo();
+      QString label = label_info.singleRenderItemLabel();
+      if (label.isEmpty()) {
+        label_info = relation->yLabelInfo();
+        if (!label_info.name.isEmpty()) {
+          LabelInfo xlabel_info = relation->xLabelInfo();
+          if (!sameX) {
+            label = i18n("%1 vs %2").arg(label_info.name).arg(xlabel_info.name);
+          } else if (xlabel_info.quantity.isEmpty()) {
+            label = label_info.name;
+          } else if (xlabel_info.quantity != xlabel_info.name) {
+            label = i18n("%1 vs %2").arg(label_info.name).arg(xlabel_info.name);
+          } else {
+            label = label_info.name;
+          }
+        } else {
+          label = relation->descriptiveName();
+        }
+      }
+      names.append(label);
+    }
+  }
+
+  for (int i = 0; i<count; i++) {
+    RelationPtr relation = legendItems.at(i);
     DrawnLegendItem item;
     item.pixmap = QPixmap(LEGENDITEMMAXWIDTH, LEGENDITEMMAXHEIGHT);
-    item.size = paintRelation(relation, &item.pixmap, font);
+    item.size = paintRelation(names.at(i), relation, &item.pixmap, font);
 
     if (_verticalDisplay) {
       legendSize.setWidth(qMax(legendSize.width(), item.size.width()));
@@ -153,8 +201,8 @@
 }
 
 
-QSize LegendItem::paintRelation(RelationPtr relation, QPixmap *pixmap, const QFont &font) {
-  Label::Parsed *parsed = Label::parse(relation->descriptiveName());
+QSize LegendItem::paintRelation(QString name, RelationPtr relation, QPixmap *pixmap, const QFont &font) {
+  Label::Parsed *parsed = Label::parse(name);
 
   pixmap->fill(Qt::transparent);
 
--- branches/work/kst/portto4/kst/src/libkstapp/legenditem.h #1224220:1224221
@@ -71,7 +71,7 @@
     virtual QString _automaticDescriptiveName() const;
     virtual void _initializeShortName();
   private:
-    QSize paintRelation(RelationPtr relation, QPixmap *pixmap, const QFont &font);
+    QSize paintRelation(QString name, RelationPtr relation, QPixmap *pixmap, const QFont &font);
 
     PlotItem *_plotItem;
     bool _auto;
--- branches/work/kst/portto4/kst/src/libkstapp/plotitem.cpp #1224220:1224221
@@ -1920,7 +1920,7 @@
   foreach (PlotRenderItem *renderer, renderItems()) {
     QString label = renderer->bottomLabel();
     if (!label.isEmpty()) {
-      if (_xAxis->axisInterpret()) { // remove units
+      if (_xAxis->axisInterpret()) { // remove units if time interpretation
         QRegExp rx(" \\[*\\]");
         rx.setPatternSyntax(QRegExp::Wildcard);
         return label.remove(rx);
--- branches/work/kst/portto4/kst/src/libkstapp/plotrenderitem.cpp #1224220:1224221
@@ -317,35 +317,6 @@
   }
 }
 
-QString PlotRenderItem::singleRenderItemLabel(const LabelInfo& label_info) const {
-  if (label_info.units.isEmpty() && label_info.quantity.isEmpty()) {           // Nxx xxx
-    return label_info.name;
-  } else if (label_info.quantity.isEmpty()) {
-    if (!label_info.name.isEmpty()) {
-      return QString("%1 \\[%2\\]").arg(label_info.name).arg(label_info.units); // NxU
-    } else {
-      return label_info.units;                                                // xxU
-    }
-  } else if (label_info.units.isEmpty()) {
-    if (label_info.name.isEmpty()) {
-      return label_info.quantity;                                             // xQx
-    } else {
-      if (label_info.name.toLower().contains(label_info.quantity.toLower())) {
-        return label_info.name;                                               // NQx A
-      } else {
-        return QString("%1: %2").arg(label_info.name).arg(label_info.quantity); // NQx B
-      }
-    }
-  } else if (label_info.name.isEmpty()) {
-    return QString("%1 \\[%2\\]").arg(label_info.quantity).arg(label_info.units); // xQU
-  //} else if (label_info.name.toLower().contains(label_info.quantity.toLower())) {
-  //  return QString("%1 \\[%2\\]").arg(label_info.name).arg(label_info.units); // NQU A
-  } else {
-    return QString("%1 \\[%2\\]").arg(label_info.quantity).arg(label_info.units); // NQU B
-  }
-}
-
-
 QString PlotRenderItem::multiRenderItemLabel(bool isX) const {
   QString units;
   QString quantity;
@@ -376,7 +347,7 @@
   }
 
   if (allSame) {
-    return singleRenderItemLabel(label_info);
+    return label_info.singleRenderItemLabel();
   }
 
   // search for the first vector with quantity and units; use it.
@@ -408,8 +379,7 @@
 
 QString PlotRenderItem::leftLabel() const {
   if (relationList().size() == 1) {
-    LabelInfo label_info = relationList().at(0)->yLabelInfo();
-    return singleRenderItemLabel(label_info);
+    return relationList().at(0)->yLabelInfo().singleRenderItemLabel();
   } else {  // multiple curves: quantity [units]
     return multiRenderItemLabel(false);
   }
@@ -418,8 +388,7 @@
 
 QString PlotRenderItem::bottomLabel() const {
   if (relationList().size() == 1) {
-    LabelInfo label_info = relationList().at(0)->xLabelInfo();
-    return singleRenderItemLabel(label_info);
+    return relationList().at(0)->xLabelInfo().singleRenderItemLabel();
   } else {  // multiple curves: quantity [units]
     return multiRenderItemLabel(true);
   }
@@ -436,10 +405,12 @@
 QString PlotRenderItem::topLabel() const {
   if (relationList().size() == 1) {
     LabelInfo label_info = relationList().at(0)->titleInfo();
-    QString label = singleRenderItemLabel(label_info);
+    QString label = label_info.singleRenderItemLabel();
     if (label.isEmpty()) {
       label_info = relationList().at(0)->yLabelInfo();
-      if ((!label_info.name.isEmpty()) && (!label_info.quantity.isEmpty())) {
+      if (label_info.singleRenderItemLabel().isEmpty()) {
+        label = relationList().at(0)->descriptiveName();
+      } else if ((!label_info.name.isEmpty()) && (!label_info.quantity.isEmpty())) {
         LabelInfo xlabel_info = relationList().at(0)->xLabelInfo();
         if ((!xlabel_info.name.isEmpty()) && (!xlabel_info.quantity.isEmpty())) {
           label = i18n("%1 vs %2").arg(label_info.name).arg(xlabel_info.name);
--- branches/work/kst/portto4/kst/src/libkstapp/plotrenderitem.h #1224220:1224221
@@ -62,7 +62,6 @@
     QString bottomLabel() const;
     QString rightLabel() const;
     QString topLabel() const;
-    QString singleRenderItemLabel(const LabelInfo& label_info) const;
     QString multiRenderItemLabel(bool isX) const;
 
     QRectF computedProjectionRect() const;


More information about the Kst mailing list