1 /***************************************************************************
2  *                                                                         *
3  *   copyright : (C) 2008 The University of Toronto                        *
4  *                   netterfield@astro.utoronto.ca                         *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  ***************************************************************************/
12 
13 #include "sharedaxisboxitem.h"
14 
15 #include "plotitem.h"
16 #include "plotitemmanager.h"
17 
18 #include "viewgridlayout.h"
19 #include "formatgridhelper.h"
20 
21 #include "application.h"
22 
23 #include <debug.h>
24 
25 #include <QDebug>
26 #include <QGraphicsScene>
27 #include <QGraphicsSceneMouseEvent>
28 #include <QMenu>
29 
30 // Zoom Debugging.  0 Off, 1 On.
31 #define DEBUG_ZOOM 0
32 
33 namespace Kst {
34 
SharedAxisBoxItem(View * parent)35 SharedAxisBoxItem::SharedAxisBoxItem(View *parent)
36     : ViewItem(parent),
37       _layout(0),
38       _keyPlot(0),
39       _loaded(false),
40       _firstPaint(true),
41       _dirty(false),
42       _shareX(true),
43       _shareY(true),
44       _serialOfLastChange(0),
45       _xAxisZoomMode(PlotAxis::Auto),
46       _yAxisZoomMode(PlotAxis::Auto),
47       _sharedIsDirty(false) {
48   setTypeName(tr("Shared Axis Box", "plots inside a shared axis box share the same axis"));
49   setBrush(Qt::transparent);
50 
51   _breakAction = new QAction(tr("Break Shared Axis Box"), this);
52   registerShortcut(_breakAction);
53   connect(_breakAction, SIGNAL(triggered()), this, SLOT(breakShare()));
54 
55   connect(this, SIGNAL(breakShareSignal()), this, SLOT(breakShare()));
56 }
57 
58 
~SharedAxisBoxItem()59 SharedAxisBoxItem::~SharedAxisBoxItem() {
60 }
61 
62 
paint(QPainter * painter)63 void SharedAxisBoxItem::paint(QPainter *painter) {
64   if (_dirty) {
65     if (_firstPaint && _loaded) {
66       sharePlots(painter, true);
67       _firstPaint = false;
68     } else if (_sharedIsDirty) {
69       sharePlots(painter, false);
70     }
71     _sharedIsDirty = false;
72     updatePlotTiedZoomSupport();
73     _keyPlot = 0;
74     foreach (PlotItem* plotItem, _sharedPlots) {
75       if (!_keyPlot) {
76         _keyPlot = plotItem;
77       } else {
78         if ((plotItem->pos().x() > _keyPlot->pos().x()) || ((plotItem->pos().y() < _keyPlot->pos().y()))) {
79           _keyPlot = plotItem;
80         }
81       }
82     }
83     _dirty = false;
84 
85   }
86   painter->drawRect(rect());
87 }
88 
89 
updatePlotTiedZoomSupport()90 void SharedAxisBoxItem::updatePlotTiedZoomSupport() {
91   foreach (PlotItem* plot, _sharedPlots) {
92     //plot->setSupportsTiedZoom(!(_shareX && _shareY));
93     plot->setSupportsTiedZoom(true);
94   }
95 }
96 
97 
save(QXmlStreamWriter & xml)98 void SharedAxisBoxItem::save(QXmlStreamWriter &xml) {
99   if (isVisible()) {
100     xml.writeStartElement("sharedaxisbox");
101     xml.writeAttribute("sharex", QVariant(isXAxisShared()).toString());
102     xml.writeAttribute("sharey", QVariant(isYAxisShared()).toString());
103     xml.writeAttribute("xzoommode", QVariant(xAxisZoomMode()).toString());
104     xml.writeAttribute("yzoommode", QVariant(yAxisZoomMode()).toString());
105     ViewItem::save(xml);
106     xml.writeEndElement();
107   }
108 }
109 
110 
setXAxisZoomMode(PlotAxis::ZoomMode mode)111 void SharedAxisBoxItem::setXAxisZoomMode(PlotAxis::ZoomMode mode) {
112   _xAxisZoomMode = mode;
113 }
114 
115 
setYAxisZoomMode(PlotAxis::ZoomMode mode)116 void SharedAxisBoxItem::setYAxisZoomMode(PlotAxis::ZoomMode mode) {
117   _yAxisZoomMode = mode;
118 }
119 
120 
setXAxisShared(const bool shared)121 void SharedAxisBoxItem::setXAxisShared(const bool shared) {
122   _shareX = shared;
123   if (shared) {
124     _xAxisZoomMode = PlotAxis::Auto;
125   } else {
126     _xAxisZoomMode = PlotAxis::FixedExpression;
127   }
128   _sharedIsDirty = true;
129 }
130 
131 
setYAxisShared(const bool shared)132 void SharedAxisBoxItem::setYAxisShared(const bool shared) {
133   _shareY = shared;
134   if (shared) {
135     _yAxisZoomMode = PlotAxis::Auto;
136   } else {
137     _yAxisZoomMode = PlotAxis::FixedExpression;
138   }
139   _sharedIsDirty = true;
140 }
141 
142 
acceptItems()143 bool SharedAxisBoxItem::acceptItems() {
144   bool bReturn = false;
145   if (_loaded) {
146     return true;
147   } else {
148     _loaded = true;
149   }
150 
151   _sharedPlots.clear();
152   QRectF maxSize(mapToParent(viewRect().topLeft()), mapToParent(viewRect().bottomRight()));
153   ViewItem* child = 0;
154   if (view()) {
155     QList<QGraphicsItem*> list = view()->items();
156     foreach (QGraphicsItem *item, list) {
157       ViewItem *viewItem = dynamic_cast<ViewItem*>(item);
158       if (!viewItem || !viewItem->isVisible() || viewItem == this ||
159           viewItem == parentItem() || !collidesWithItem(viewItem, Qt::IntersectsItemBoundingRect)) {
160         continue;
161       }
162 
163       if (PlotItem *plotItem = qobject_cast<PlotItem*>(viewItem)) {
164         if (plotItem->parentItem()) {
165           ViewItem *parent = static_cast<ViewItem*>(plotItem->parentItem());
166           SharedAxisBoxItem *shareBox = qobject_cast<SharedAxisBoxItem*>(parent);
167           if (shareBox) {
168             // share boxes can't contain share boxes.  Bail out now!
169             return false;
170           } else if (parent != parentItem()) {
171             continue;
172           }
173         } else if (parentItem()) {
174           continue;
175         }
176         plotItem->setSharedAxisBox(this);
177 
178         _sharedPlots << plotItem;
179         connect(plotItem, SIGNAL(breakShareTriggered()), this, SLOT(breakShare()));
180         connect(plotItem, SIGNAL(shareXAxisTriggered()), this, SLOT(shareXAxis()));
181         connect(plotItem, SIGNAL(shareYAxisTriggered()), this, SLOT(shareYAxis()));
182 
183         child = plotItem;
184         if (!maxSize.contains(plotItem->mapToParent(plotItem->viewRect().topLeft()))) {
185           maxSize.setTop(qMin(plotItem->mapToParent(plotItem->viewRect().topLeft()).y(), maxSize.top()));
186           maxSize.setLeft(qMin(plotItem->mapToParent(plotItem->viewRect().topLeft()).x(), maxSize.left()));
187         }
188         if (!maxSize.contains(plotItem->mapToParent(plotItem->viewRect().bottomRight()))) {
189           maxSize.setBottom(qMax(plotItem->mapToParent(plotItem->viewRect().bottomRight()).y(), maxSize.bottom()));
190           maxSize.setRight(qMax(plotItem->mapToParent(plotItem->viewRect().bottomRight()).x(), maxSize.right()));
191         }
192       }
193     }
194     if (child) {
195       storePen(QPen(Qt::white));
196       setBrush(Qt::white);
197       ViewGridLayout::updateProjections(this);
198       _dirty = true;
199       bReturn =  true;
200     }
201   }
202   if (maxSize != viewRect()) {
203     setPos(maxSize.topLeft());
204     setViewRect(QRectF(mapFromParent(maxSize.topLeft()), mapFromParent(maxSize.bottomRight())));
205   }
206   return bReturn;
207 }
208 
nRows()209 int SharedAxisBoxItem::nRows() {
210   QList<PlotItem*> plotList = getSharedPlots();
211   QList<ViewItem*> viewItemList;
212   int n_plots = plotList.size();
213   for (int i_plot = 0; i_plot<n_plots; i_plot++) {
214     viewItemList.append(dynamic_cast<ViewItem*>(plotList.at(i_plot)));
215   }
216 
217   FormatGridHelper grid(viewItemList);
218   return grid.n_rows;
219 }
220 
221 
nCols()222 int SharedAxisBoxItem::nCols() {
223   QList<PlotItem*> plotList = getSharedPlots();
224   QList<ViewItem*> viewItemList;
225   int n_plots = plotList.size();
226   for (int i_plot = 0; i_plot<n_plots; i_plot++) {
227     viewItemList.append(dynamic_cast<ViewItem*>(plotList.at(i_plot)));
228   }
229 
230   FormatGridHelper grid(viewItemList);
231   return grid.n_cols;
232 }
233 
234 
breakShare()235 void SharedAxisBoxItem::breakShare() {
236   _loaded = false;
237   QList<PlotItem*> plotList = getSharedPlots();
238   QList<ViewItem*> viewItemList;
239   int n_plots = plotList.size();
240   for (int i_plot = 0; i_plot<n_plots; i_plot++) {
241     viewItemList.append(dynamic_cast<ViewItem*>(plotList.at(i_plot)));
242   }
243 
244   FormatGridHelper grid(viewItemList);
245 
246   double height;
247   double width;
248 
249   if (grid.n_rows>0) {
250     height = double(rect().height())/double(grid.n_rows);
251   } else {
252     height = rect().height();
253   }
254   if (grid.n_cols>0) {
255     width = double(rect().width())/double(grid.n_cols);
256   } else {
257     width = rect().width();
258   }
259   QPointF P0 = rect().topLeft();
260   for (int i_plot = 0; i_plot<n_plots; i_plot++) {
261     PlotItem *plotItem = plotList.at(i_plot);
262 
263     plotItem->setRect(0,0,width, height);
264     plotItem->setPos(mapToParent(P0 + QPointF(grid.rcList.at(i_plot).col*width, grid.rcList.at(i_plot).row*height)));
265     plotItem->setSharedAxisBox(0);
266     plotItem->setTopSuppressed(false);
267     plotItem->setBottomSuppressed(false);
268     plotItem->setLeftSuppressed(false);
269     plotItem->setRightSuppressed(false);
270     plotItem->update();
271   }
272   if (_layout) {
273     _layout->reset();
274   }
275   hide();
276 }
277 
278 
lockItems()279 void SharedAxisBoxItem::lockItems() {
280   _sharedPlots.clear();
281   QList<QGraphicsItem*> list = QGraphicsItem::childItems();
282   foreach (QGraphicsItem *item, list) {
283     ViewItem *viewItem = dynamic_cast<ViewItem*>(item);
284     if (!viewItem)
285       continue;
286 
287     if (PlotItem *plotItem = qobject_cast<PlotItem*>(viewItem)) {
288       plotItem->setAllowedGripModes(0);
289       plotItem->setFlags(0);
290 
291       _sharedPlots << plotItem;
292     }
293   }
294   if (!list.isEmpty()) {
295     storePen(QPen(Qt::white));
296     setBrush(Qt::white);
297   }
298 }
299 
300 
shareXAxis()301 void SharedAxisBoxItem::shareXAxis() {
302   _shareX = !_shareX;
303   _sharedIsDirty = true;
304   updateShare();
305 }
306 
307 
shareYAxis()308 void SharedAxisBoxItem::shareYAxis() {
309   _shareY = !_shareY;
310   _sharedIsDirty = true;
311   updateShare();
312 }
313 
314 
updateShare()315 void SharedAxisBoxItem::updateShare() {
316   ViewGridLayout::updateProjections(this, _shareX, _shareY);
317   view()->setPlotBordersDirty(true);
318   setDirty();
319   update();
320   if (!_shareX && !_shareY) {
321     breakShare();
322   } else {
323   }
324 }
325 
326 
addToMenuForContextEvent(QMenu & menu)327 void SharedAxisBoxItem::addToMenuForContextEvent(QMenu &menu) {
328   menu.addAction(_breakAction);
329 }
330 
331 
triggerContextEvent(QGraphicsSceneContextMenuEvent * event)332 void SharedAxisBoxItem::triggerContextEvent(QGraphicsSceneContextMenuEvent *event) {
333   ViewItem::contextMenuEvent(event);
334 }
335 
336 
creationPolygonChanged(View::CreationEvent event)337 void SharedAxisBoxItem::creationPolygonChanged(View::CreationEvent event) {
338   if (event == View::MousePress) {
339     ViewItem::creationPolygonChanged(event);
340     return;
341   }
342 
343   if (event == View::MouseMove) {
344     ViewItem::creationPolygonChanged(event);
345     if (creationState() == ViewItem::None) {
346       return;
347     }
348 
349     QList<PlotItem*> plots;
350     if (view()) {
351       QList<QGraphicsItem*> list = view()->items();
352       foreach (QGraphicsItem *item, list) {
353         ViewItem *viewItem = dynamic_cast<ViewItem*>(item);
354         if (!viewItem || !viewItem->isVisible() || viewItem == this ||  viewItem == parentItem() || !collidesWithItem(viewItem, Qt::IntersectsItemBoundingRect)) {
355           continue;
356         }
357         if (PlotItem *plotItem = qobject_cast<PlotItem*>(viewItem)) {
358           plots.append(plotItem);
359         }
360       }
361       highlightPlots(plots);
362     }
363     return;
364   }
365 
366   if (event == View::EscapeEvent || event == View::MouseRelease) {
367     ViewItem::creationPolygonChanged(event);
368     highlightPlots(QList<PlotItem*>());
369     return;
370   }
371 }
372 
373 
highlightPlots(QList<PlotItem * > plots)374 void SharedAxisBoxItem::highlightPlots(QList<PlotItem*> plots) {
375   QList<PlotItem*> currentlyHighlighted = _highlightedPlots;
376   _highlightedPlots.clear();
377 
378   foreach(PlotItem *plotItem, plots) {
379     _highlightedPlots.append(plotItem);
380     if (!currentlyHighlighted.contains(plotItem)) {
381       plotItem->setHighlighted(true);
382       plotItem->update();
383     }
384   }
385 
386   foreach(PlotItem* plotItem, currentlyHighlighted) {
387     if (!_highlightedPlots.contains(plotItem)) {
388       plotItem->setHighlighted(false);
389       plotItem->update();
390     }
391   }
392 }
393 
394 
tryMousePressEvent(ViewItem * viewItem,QGraphicsSceneMouseEvent * event)395 bool SharedAxisBoxItem::tryMousePressEvent(ViewItem* viewItem, QGraphicsSceneMouseEvent *event) {
396   if (event->button() == Qt::LeftButton) {
397     if (checkBox().contains(viewItem->mapToParent(event->pos()))) {
398       setTiedZoom(!isTiedZoom(), !isTiedZoom());
399       return true;
400     }
401   }
402   return false;
403 }
404 
405 
getSharedPlots()406 QList<PlotItem*> SharedAxisBoxItem::getSharedPlots() {
407   return _sharedPlots;
408 }
409 
getTiedPlots(PlotItem * originPlotItem)410 QList<PlotItem*> SharedAxisBoxItem::getTiedPlots(PlotItem* originPlotItem) {
411   QList<PlotItem*> plots;
412 
413   if (originPlotItem) {
414     return PlotItemManager::tiedZoomPlotsForView(view());
415   }
416   return plots;
417 }
418 
419 
computeRect(PlotAxis::ZoomMode xZoomMode,PlotAxis::ZoomMode yZoomMode)420 QRectF SharedAxisBoxItem::computeRect(PlotAxis::ZoomMode xZoomMode, PlotAxis::ZoomMode yZoomMode) {
421   QRectF computedRect;
422   foreach(PlotItem* plot, getSharedPlots()) {
423     PlotAxis::ZoomMode existingXMode = plot->xAxis()->axisZoomMode();
424     PlotAxis::ZoomMode existingYMode = plot->yAxis()->axisZoomMode();
425 
426     plot->xAxis()->setAxisZoomMode(xZoomMode);
427     plot->yAxis()->setAxisZoomMode(yZoomMode);
428 
429     if (computedRect.isValid()) {
430       computedRect = computedRect.united(plot->computedProjectionRect());
431     } else {
432       computedRect = plot->computedProjectionRect();
433     }
434     plot->xAxis()->setAxisZoomMode(existingXMode);
435     plot->yAxis()->setAxisZoomMode(existingYMode);
436   }
437   return computedRect;
438 }
439 
440 
applyZoom(const QRectF & projection,PlotItem * originPlotItem,bool applyX,bool applyY)441 void SharedAxisBoxItem::applyZoom(const QRectF &projection, PlotItem* originPlotItem, bool applyX, bool applyY) {
442   QList<PlotItem*> allPlots = getSharedPlots();
443   QList<PlotItem*> plotTied;
444   if (originPlotItem &&
445       originPlotItem->isTiedZoom() &&
446       originPlotItem->isInSharedAxisBox() &&
447       (originPlotItem->sharedAxisBox() == this)) {
448     plotTied = PlotItemManager::tiedZoomPlotsForView(view());
449     foreach (PlotItem* plotItem, plotTied) {
450       // tie only changes the unshared axis for shared plots
451       if ((!_shareX || !plotItem->isInSharedAxisBox()) && applyX) {
452         switch (xAxisZoomMode()) {
453           case PlotAxis::Auto:
454             plotItem->zoomXMaximum(true);
455             break;
456           case PlotAxis::AutoBorder:
457             plotItem->zoomXAutoBorder(true);
458             break;
459           case PlotAxis::SpikeInsensitive:
460             plotItem->zoomXNoSpike(true);
461             break;
462           default:
463             plotItem->zoomXRange(projection,true);
464             break;
465 
466         }
467       }
468 
469       if ((!_shareY || !plotItem->isInSharedAxisBox()) && applyY) {
470         switch (yAxisZoomMode()) {
471           case PlotAxis::Auto:
472             plotItem->zoomYMaximum(true);
473             break;
474           case PlotAxis::AutoBorder:
475             plotItem->zoomYAutoBorder(true);
476             break;
477           case PlotAxis::SpikeInsensitive:
478             plotItem->zoomYNoSpike(true);
479             break;
480           default:
481             plotItem->zoomYRange(projection,true);
482             break;
483         }
484       }
485     }
486   }
487   foreach (PlotItem* plotItem, allPlots) {
488     if ((applyX && applyY) && ((_shareX && _shareY) || (isXTiedZoom() && isYTiedZoom()))) {
489       plotItem->zoomFixedExpression(projection, true);
490     } else if (applyX && (_shareX || isXTiedZoom() || (plotItem == originPlotItem))) {
491       plotItem->zoomXRange(QRectF(projection.x(), plotItem->projectionRect().y(), projection.width(), plotItem->projectionRect().height()), true);
492     } else if (applyY && (_shareY || isYTiedZoom() || (plotItem == originPlotItem))) {
493       plotItem->zoomYRange(QRectF(plotItem->projectionRect().x(), projection.y(), plotItem->projectionRect().width(), projection.height()), true);
494     }
495   }
496 }
497 
498 
zoomFixedExpression(const QRectF & projection,PlotItem * originPlotItem)499 void SharedAxisBoxItem::zoomFixedExpression(const QRectF &projection, PlotItem* originPlotItem) {
500   _xAxisZoomMode = PlotAxis::FixedExpression;
501   _yAxisZoomMode = PlotAxis::FixedExpression;
502   if (originPlotItem) {
503     originPlotItem->zoomFixedExpression(projection, true);
504     if (originPlotItem->isTiedZoom() && originPlotItem->isInSharedAxisBox() && (originPlotItem->sharedAxisBox() == this)) {
505       QList<PlotItem*> plotTied = PlotItemManager::tiedZoomPlotsForView(view());
506 
507       foreach(PlotItem* plotItem, plotTied) {
508         plotItem->zoomFixedExpression(projection, true);
509       }
510     }
511   }
512   applyZoom(projection, originPlotItem, true, true);
513 }
514 
515 
zoomXRange(const QRectF & projection,PlotItem * originPlotItem)516 void SharedAxisBoxItem::zoomXRange(const QRectF &projection, PlotItem* originPlotItem) {
517   if (!_shareX) {
518     if (originPlotItem) {
519       originPlotItem->zoomXRange(projection, true);
520     }
521   } else {
522     _xAxisZoomMode = PlotAxis::FixedExpression;
523     applyZoom(projection, originPlotItem, true, false);
524   }
525 
526   if (originPlotItem) {
527     if (originPlotItem->isXTiedZoom() && originPlotItem->isInSharedAxisBox() && (originPlotItem->sharedAxisBox() == this)) {
528       QList<PlotItem*> plotTied = PlotItemManager::tiedZoomPlotsForView(view());
529 
530       foreach(PlotItem* plotItem, plotTied) {
531         plotItem->zoomXRange(projection, true);
532       }
533     }
534   }
535 
536 }
537 
538 
zoomYRange(const QRectF & projection,PlotItem * originPlotItem)539 void SharedAxisBoxItem::zoomYRange(const QRectF &projection, PlotItem* originPlotItem) {
540   if (!_shareY) {
541     if (originPlotItem) {
542       originPlotItem->zoomYRange(projection, true);
543     }
544   } else {
545     _yAxisZoomMode = PlotAxis::FixedExpression;
546     applyZoom(projection, originPlotItem, false, true);
547   }
548 
549   if (originPlotItem) {
550     if (originPlotItem->isYTiedZoom() && originPlotItem->isInSharedAxisBox() && (originPlotItem->sharedAxisBox() == this)) {
551       QList<PlotItem*> plotTied = PlotItemManager::tiedZoomPlotsForView(view());
552 
553       foreach(PlotItem* plotItem, plotTied) {
554         plotItem->zoomYRange(projection, true);
555       }
556     }
557   }
558 
559 }
560 
561 
zoomMaximum(PlotItem * originPlotItem)562 void SharedAxisBoxItem::zoomMaximum(PlotItem* originPlotItem) {
563   if (!originPlotItem) {
564     originPlotItem = keyPlot();
565   }
566 
567   _xAxisZoomMode = PlotAxis::Auto;
568   _yAxisZoomMode = PlotAxis::AutoBorder;
569   if (originPlotItem) {
570     originPlotItem->zoomMaximum(true);
571     if (originPlotItem->isTiedZoom() && originPlotItem->isInSharedAxisBox() && (originPlotItem->sharedAxisBox() == this)) {
572       QList<PlotItem*> plotTied = PlotItemManager::tiedZoomPlotsForView(view());
573 
574       foreach(PlotItem* plotItem, plotTied) {
575         plotItem->zoomMaximum(true);
576       }
577     }
578   }
579   applyZoom(computeRect(PlotAxis::Auto, PlotAxis::AutoBorder), originPlotItem);
580 }
581 
582 
zoomMaxSpikeInsensitive(PlotItem * originPlotItem)583 void SharedAxisBoxItem::zoomMaxSpikeInsensitive(PlotItem* originPlotItem) {
584   _xAxisZoomMode = PlotAxis::Auto;
585   _yAxisZoomMode = PlotAxis::SpikeInsensitive;
586   if (originPlotItem) {
587     originPlotItem->zoomMaxSpikeInsensitive(true);
588   }
589   applyZoom(computeRect(PlotAxis::Auto, PlotAxis::SpikeInsensitive), originPlotItem);
590 }
591 
592 
zoomMeanCentered(PlotItem * originPlotItem)593 void SharedAxisBoxItem::zoomMeanCentered(PlotItem* originPlotItem) {
594   _yAxisZoomMode = PlotAxis::MeanCentered;
595   applyZoom(computeRect(PlotAxis::Auto, PlotAxis::MeanCentered), originPlotItem, false, true);
596 }
597 
zoomYMeanCentered(qreal dY,PlotItem * originPlotItem)598 void SharedAxisBoxItem::zoomYMeanCentered(qreal dY, PlotItem* originPlotItem) {
599   if (!_shareY) {
600     _yAxisZoomMode = PlotAxis::MeanCentered;
601     if (originPlotItem) {
602       originPlotItem->zoomYMeanCentered(dY, true);
603     }
604   }
605 }
606 
zoomXMeanCentered(PlotItem * originPlotItem)607 void SharedAxisBoxItem::zoomXMeanCentered(PlotItem* originPlotItem) {
608   _xAxisZoomMode = PlotAxis::MeanCentered;
609   if (originPlotItem) {
610     originPlotItem->zoomXMeanCentered(true);
611   }
612   applyZoom(computeRect(PlotAxis::MeanCentered, PlotAxis::MeanCentered), originPlotItem, true, false);
613 }
614 
zoomXMaximum(PlotItem * originPlotItem)615 void SharedAxisBoxItem::zoomXMaximum(PlotItem* originPlotItem) {
616   _xAxisZoomMode = PlotAxis::Auto;
617   if (originPlotItem) {
618     originPlotItem->zoomXMaximum(true);
619   }
620   applyZoom(computeRect(PlotAxis::Auto, PlotAxis::Auto), originPlotItem, true, false);
621 }
622 
623 
zoomXNoSpike(PlotItem * originPlotItem)624 void SharedAxisBoxItem::zoomXNoSpike(PlotItem* originPlotItem) {
625   _xAxisZoomMode = PlotAxis::SpikeInsensitive;
626   if (originPlotItem) {
627     originPlotItem->zoomXNoSpike(true);
628   }
629   applyZoom(computeRect(PlotAxis::SpikeInsensitive, PlotAxis::Auto), originPlotItem, true, false);
630 }
631 
632 
zoomXAutoBorder(PlotItem * originPlotItem)633 void SharedAxisBoxItem::zoomXAutoBorder(PlotItem* originPlotItem) {
634   _xAxisZoomMode = PlotAxis::AutoBorder;
635   if (originPlotItem) {
636     originPlotItem->zoomXAutoBorder(true);
637   }
638   applyZoom(computeRect(PlotAxis::AutoBorder, PlotAxis::Auto), originPlotItem, true, false);
639 }
640 
zoomXRight(PlotItem * originPlotItem,bool scroll_far)641 void SharedAxisBoxItem::zoomXRight(PlotItem* originPlotItem, bool scroll_far) {
642   QList<PlotItem*> allPlots;
643   if (_shareX) {
644     allPlots = getSharedPlots();
645   } else if (originPlotItem) {
646     allPlots = getTiedPlots(originPlotItem);
647   } else {
648     foreach(PlotItem *plot, _sharedPlots) {
649       if (plot->isXTiedZoom()) {
650         allPlots << plot;
651       }
652     }
653   }
654 
655   if (originPlotItem) {
656     bool origin_tied = originPlotItem->isXTiedZoom();
657 
658 
659     if (!(_shareX || origin_tied)) {
660       originPlotItem->zoomXRight(true, scroll_far);
661     } else {
662       _xAxisZoomMode = PlotAxis::FixedExpression;
663       foreach(PlotItem* plotItem, allPlots) {
664         plotItem->zoomXRight(true, scroll_far);
665       }
666     }
667     if (originPlotItem->isXTiedZoom() && originPlotItem->isInSharedAxisBox() && (originPlotItem->sharedAxisBox() == this)) {
668       QList<PlotItem*> plotTied = PlotItemManager::tiedZoomPlotsForView(view());
669 
670       foreach(PlotItem* plotItem, plotTied) {
671         if (!allPlots.contains(plotItem)) {
672           plotItem->zoomXRight(true, scroll_far);
673         }
674       }
675     }
676   } else {
677     foreach (PlotItem *plotItem, allPlots) {
678       plotItem->zoomXRight(true, scroll_far);
679     }
680   }
681 }
682 
zoomXLeft(PlotItem * originPlotItem,bool scroll_far)683 void SharedAxisBoxItem::zoomXLeft(PlotItem* originPlotItem, bool scroll_far) {
684   QList<PlotItem*> allPlots;
685   if (_shareX) {
686     allPlots = getSharedPlots();
687   } else if (originPlotItem) {
688     allPlots = getTiedPlots(originPlotItem);
689   } else {
690     foreach(PlotItem *plot, _sharedPlots) {
691       if (plot->isXTiedZoom()) {
692         allPlots << plot;
693       }
694     }
695   }
696 
697   if (originPlotItem) {
698     bool origin_tied = originPlotItem->isXTiedZoom();
699 
700 
701     if (!(_shareX || origin_tied)) {
702       originPlotItem->zoomXLeft(true, scroll_far);
703     } else {
704       _xAxisZoomMode = PlotAxis::FixedExpression;
705       foreach(PlotItem* plotItem, allPlots) {
706         plotItem->zoomXLeft(true, scroll_far);
707       }
708     }
709     if (originPlotItem->isXTiedZoom() && originPlotItem->isInSharedAxisBox() && (originPlotItem->sharedAxisBox() == this)) {
710       QList<PlotItem*> plotTied = PlotItemManager::tiedZoomPlotsForView(view());
711 
712       foreach(PlotItem* plotItem, plotTied) {
713         if (!allPlots.contains(plotItem)) {
714           plotItem->zoomXLeft(true, scroll_far);
715         }
716       }
717     }
718   } else {
719     foreach (PlotItem *plotItem, allPlots) {
720       plotItem->zoomXLeft(true, scroll_far);
721     }
722   }
723 }
724 
zoomXOut(PlotItem * originPlotItem)725 void SharedAxisBoxItem::zoomXOut(PlotItem* originPlotItem) {
726   QList<PlotItem*> allPlots;
727   if (_shareX) {
728     allPlots = getSharedPlots();
729   } else if (originPlotItem) {
730     allPlots = getTiedPlots(originPlotItem);
731   } else {
732     foreach(PlotItem *plot, _sharedPlots) {
733       if (plot->isXTiedZoom()) {
734         allPlots << plot;
735       }
736     }
737   }
738 
739   if (originPlotItem) {
740     bool origin_tied = originPlotItem->isXTiedZoom();
741 
742 
743     if (!(_shareX || origin_tied)) {
744       originPlotItem->zoomXOut(true);
745     } else {
746       _xAxisZoomMode = PlotAxis::FixedExpression;
747       foreach(PlotItem* plotItem, allPlots) {
748         plotItem->zoomXOut(true);
749       }
750     }
751     if (originPlotItem->isXTiedZoom() && originPlotItem->isInSharedAxisBox() && (originPlotItem->sharedAxisBox() == this)) {
752       QList<PlotItem*> plotTied = PlotItemManager::tiedZoomPlotsForView(view());
753 
754       foreach(PlotItem* plotItem, plotTied) {
755         if (!allPlots.contains(plotItem)) {
756           plotItem->zoomXOut(true);
757         }
758       }
759     }
760   } else {
761     foreach (PlotItem *plotItem, allPlots) {
762       plotItem->zoomXOut(true);
763     }
764   }
765 }
766 
zoomXIn(PlotItem * originPlotItem)767 void SharedAxisBoxItem::zoomXIn(PlotItem* originPlotItem) {
768   QList<PlotItem*> allPlots;
769   if (_shareX) {
770     allPlots = getSharedPlots();
771   } else if (originPlotItem) {
772     allPlots = getTiedPlots(originPlotItem);
773   } else {
774     foreach(PlotItem *plot, _sharedPlots) {
775       if (plot->isXTiedZoom()) {
776         allPlots << plot;
777       }
778     }
779   }
780 
781   if (originPlotItem) {
782     bool origin_tied = originPlotItem->isXTiedZoom();
783 
784 
785     if (!(_shareX || origin_tied)) {
786       originPlotItem->zoomXIn(true);
787     } else {
788       _xAxisZoomMode = PlotAxis::FixedExpression;
789       foreach(PlotItem* plotItem, allPlots) {
790         plotItem->zoomXIn(true);
791       }
792     }
793     if (originPlotItem->isXTiedZoom() && originPlotItem->isInSharedAxisBox() && (originPlotItem->sharedAxisBox() == this)) {
794       QList<PlotItem*> plotTied = PlotItemManager::tiedZoomPlotsForView(view());
795 
796       foreach(PlotItem* plotItem, plotTied) {
797         if (!allPlots.contains(plotItem)) {
798           plotItem->zoomXIn(true);
799         }
800       }
801     }
802   } else {
803     foreach (PlotItem *plotItem, allPlots) {
804       plotItem->zoomXIn(true);
805     }
806   }
807 }
808 
zoomNormalizeXtoY(PlotItem * originPlotItem)809 void SharedAxisBoxItem::zoomNormalizeXtoY(PlotItem* originPlotItem) {
810   if (!(_shareX || isXTiedZoom())) {
811     if (originPlotItem) {
812       originPlotItem->zoomNormalizeXtoY(true);
813     }
814   } else {
815     QRectF computedRect;
816     foreach(PlotItem *plotItem, getSharedPlots()) {
817       QRectF compute = plotItem->projectionRect();
818       qreal mean = compute.center().x();
819       qreal range = plotItem->plotRect().width() * compute.height() / plotItem->plotRect().height();
820 
821       compute.setLeft(mean - (range / 2.0));
822       compute.setRight(mean + (range / 2.0));
823 
824       if (computedRect.isValid()) {
825         computedRect = computedRect.united(compute);
826       } else {
827         computedRect = compute;
828       }
829     }
830     _xAxisZoomMode = PlotAxis::FixedExpression;
831     if (originPlotItem) {
832       originPlotItem->zoomNormalizeXtoY(true);
833     }
834     applyZoom(computedRect, originPlotItem, true, false);
835   }
836 }
837 
838 
zoomLogX(PlotItem * originPlotItem,bool autoEnable,bool enable)839 void SharedAxisBoxItem::zoomLogX(PlotItem* originPlotItem, bool autoEnable, bool enable) {
840   bool enableLog;
841   if (autoEnable && originPlotItem) {
842     enableLog = !originPlotItem->xAxis()->axisLog();
843   } else {
844     enableLog = enable;
845   }
846   QList<PlotItem*> allPlots;
847   if (_shareX) {
848     allPlots = getSharedPlots();
849   } else {
850     allPlots = getTiedPlots(originPlotItem);
851   }
852 
853   bool origin_tied = false;
854   if (originPlotItem) {
855     origin_tied = originPlotItem->isXTiedZoom();
856   }
857 
858   if (!(_shareX || origin_tied)) {
859     if (originPlotItem) {
860       originPlotItem->zoomLogX(true, false, enableLog);
861     }
862   } else {
863     foreach(PlotItem* plotItem, allPlots) {
864       plotItem->zoomLogX(true, false, enableLog);
865     }
866   }
867   if (originPlotItem && originPlotItem->isXTiedZoom() && originPlotItem->isInSharedAxisBox() && (originPlotItem->sharedAxisBox() == this)) {
868     QList<PlotItem*> plotTied = PlotItemManager::tiedZoomPlotsForView(view());
869 
870     foreach(PlotItem* plotItem, plotTied) {
871       if (!allPlots.contains(plotItem)) {
872         plotItem->zoomLogX(true, false, enableLog);
873       }
874     }
875   }
876 }
877 
878 
zoomYLocalMaximum(PlotItem * originPlotItem)879 void SharedAxisBoxItem::zoomYLocalMaximum(PlotItem* originPlotItem) {
880   if (!originPlotItem) {
881     originPlotItem = keyPlot();
882   }
883 
884   _yAxisZoomMode = PlotAxis::FixedExpression;
885   if (originPlotItem) {
886     originPlotItem->zoomYLocalMaximum(true);
887     if (originPlotItem->isYTiedZoom() && originPlotItem->isInSharedAxisBox() && (originPlotItem->sharedAxisBox() == this)) {
888       QList<PlotItem*> plotTied = PlotItemManager::tiedZoomPlotsForView(view());
889 
890       foreach(PlotItem* plotItem, plotTied) {
891         plotItem->zoomYLocalMaximum(true);
892       }
893     }
894   }
895 }
896 
zoomYMaximum(PlotItem * originPlotItem)897 void SharedAxisBoxItem::zoomYMaximum(PlotItem* originPlotItem) {
898   _yAxisZoomMode = PlotAxis::Auto;
899   if (originPlotItem) {
900     originPlotItem->zoomYMaximum(true);
901   }
902   applyZoom(computeRect(PlotAxis::Auto, PlotAxis::Auto), originPlotItem, false, true);
903 }
904 
905 
zoomYNoSpike(PlotItem * originPlotItem)906 void SharedAxisBoxItem::zoomYNoSpike(PlotItem* originPlotItem) {
907   _yAxisZoomMode = PlotAxis::SpikeInsensitive;
908   if (originPlotItem) {
909     originPlotItem->zoomYNoSpike(true);
910   }
911   applyZoom(computeRect(PlotAxis::Auto, PlotAxis::SpikeInsensitive), originPlotItem, false, true);
912 }
913 
914 
zoomYAutoBorder(PlotItem * originPlotItem)915 void SharedAxisBoxItem::zoomYAutoBorder(PlotItem* originPlotItem) {
916   _yAxisZoomMode = PlotAxis::AutoBorder;
917   if (originPlotItem) {
918     originPlotItem->zoomYAutoBorder(true);
919   }
920   applyZoom(computeRect(PlotAxis::Auto, PlotAxis::AutoBorder), originPlotItem, false, true);
921 }
922 
923 
zoomYUp(PlotItem * originPlotItem)924 void SharedAxisBoxItem::zoomYUp(PlotItem* originPlotItem) {
925   QList<PlotItem*> allPlots;
926   if (_shareY) {
927     allPlots = getSharedPlots();
928   } else if (originPlotItem) {
929     allPlots = getTiedPlots(originPlotItem);
930   } else {
931     foreach(PlotItem *plot, _sharedPlots) {
932       if (plot->isYTiedZoom()) {
933         allPlots << plot;
934       }
935     }
936   }
937 
938   if (originPlotItem) {
939     bool origin_tied = originPlotItem->isYTiedZoom();
940 
941 
942     if (!(_shareY || origin_tied)) {
943       originPlotItem->zoomYUp(true);
944     } else {
945       _yAxisZoomMode = PlotAxis::FixedExpression;
946       foreach(PlotItem* plotItem, allPlots) {
947         plotItem->zoomYUp(true);
948       }
949     }
950     if (originPlotItem->isYTiedZoom() && originPlotItem->isInSharedAxisBox() && (originPlotItem->sharedAxisBox() == this)) {
951       QList<PlotItem*> plotTied = PlotItemManager::tiedZoomPlotsForView(view());
952 
953       foreach(PlotItem* plotItem, plotTied) {
954         if (!allPlots.contains(plotItem)) {
955           plotItem->zoomYUp(true);
956         }
957       }
958     }
959   } else {
960     foreach (PlotItem *plotItem, allPlots) {
961       plotItem->zoomYUp(true);
962     }
963   }
964 }
965 
966 
zoomYDown(PlotItem * originPlotItem)967 void SharedAxisBoxItem::zoomYDown(PlotItem* originPlotItem) {
968   QList<PlotItem*> allPlots;
969   if (_shareY) {
970     allPlots = getSharedPlots();
971   } else if (originPlotItem) {
972     allPlots = getTiedPlots(originPlotItem);
973   } else {
974     foreach(PlotItem *plot, _sharedPlots) {
975       if (plot->isYTiedZoom()) {
976         allPlots << plot;
977       }
978     }
979   }
980 
981   if (originPlotItem) {
982     bool origin_tied = originPlotItem->isYTiedZoom();
983 
984 
985     if (!(_shareY || origin_tied)) {
986       originPlotItem->zoomYDown(true);
987     } else {
988       _yAxisZoomMode = PlotAxis::FixedExpression;
989       foreach(PlotItem* plotItem, allPlots) {
990         plotItem->zoomYDown(true);
991       }
992     }
993     if (originPlotItem->isYTiedZoom() && originPlotItem->isInSharedAxisBox() && (originPlotItem->sharedAxisBox() == this)) {
994       QList<PlotItem*> plotTied = PlotItemManager::tiedZoomPlotsForView(view());
995 
996       foreach(PlotItem* plotItem, plotTied) {
997         if (!allPlots.contains(plotItem)) {
998           plotItem->zoomYDown(true);
999         }
1000       }
1001     }
1002   } else {
1003     foreach (PlotItem *plotItem, allPlots) {
1004       plotItem->zoomYDown(true);
1005     }
1006   }
1007 }
1008 
zoomYOut(PlotItem * originPlotItem)1009 void SharedAxisBoxItem::zoomYOut(PlotItem* originPlotItem) {
1010   QList<PlotItem*> allPlots;
1011   if (_shareY) {
1012     allPlots = getSharedPlots();
1013   } else if (originPlotItem) {
1014     allPlots = getTiedPlots(originPlotItem);
1015   } else {
1016     foreach(PlotItem *plot, _sharedPlots) {
1017       if (plot->isYTiedZoom()) {
1018         allPlots << plot;
1019       }
1020     }
1021   }
1022 
1023   if (originPlotItem) {
1024     bool origin_tied = originPlotItem->isYTiedZoom();
1025 
1026 
1027     if (!(_shareY || origin_tied)) {
1028       originPlotItem->zoomYOut(true);
1029     } else {
1030       _yAxisZoomMode = PlotAxis::FixedExpression;
1031       foreach(PlotItem* plotItem, allPlots) {
1032         plotItem->zoomYOut(true);
1033       }
1034     }
1035     if (originPlotItem->isYTiedZoom() && originPlotItem->isInSharedAxisBox() && (originPlotItem->sharedAxisBox() == this)) {
1036       QList<PlotItem*> plotTied = PlotItemManager::tiedZoomPlotsForView(view());
1037 
1038       foreach(PlotItem* plotItem, plotTied) {
1039         if (!allPlots.contains(plotItem)) {
1040           plotItem->zoomYOut(true);
1041         }
1042       }
1043     }
1044   } else {
1045     foreach (PlotItem *plotItem, allPlots) {
1046       plotItem->zoomYOut(true);
1047     }
1048   }
1049 }
1050 
1051 
zoomYIn(PlotItem * originPlotItem)1052 void SharedAxisBoxItem::zoomYIn(PlotItem* originPlotItem) {
1053   QList<PlotItem*> allPlots;
1054   if (_shareY) {
1055     allPlots = getSharedPlots();
1056   } else if (originPlotItem) {
1057     allPlots = getTiedPlots(originPlotItem);
1058   } else {
1059     foreach(PlotItem *plot, _sharedPlots) {
1060       if (plot->isYTiedZoom()) {
1061         allPlots << plot;
1062       }
1063     }
1064   }
1065 
1066   if (originPlotItem) {
1067     bool origin_tied = originPlotItem->isYTiedZoom();
1068 
1069 
1070     if (!(_shareY || origin_tied)) {
1071       originPlotItem->zoomYIn(true);
1072     } else {
1073       _yAxisZoomMode = PlotAxis::FixedExpression;
1074       foreach(PlotItem* plotItem, allPlots) {
1075         plotItem->zoomYIn(true);
1076       }
1077     }
1078     if (originPlotItem->isYTiedZoom() && originPlotItem->isInSharedAxisBox() && (originPlotItem->sharedAxisBox() == this)) {
1079       QList<PlotItem*> plotTied = PlotItemManager::tiedZoomPlotsForView(view());
1080 
1081       foreach(PlotItem* plotItem, plotTied) {
1082         if (!allPlots.contains(plotItem)) {
1083           plotItem->zoomYIn(true);
1084         }
1085       }
1086     }
1087   } else {
1088     foreach (PlotItem *plotItem, allPlots) {
1089       plotItem->zoomYIn(true);
1090     }
1091   }
1092 }
1093 
1094 
zoomNormalizeYtoX(PlotItem * originPlotItem)1095 void SharedAxisBoxItem::zoomNormalizeYtoX(PlotItem* originPlotItem) {
1096   if (!originPlotItem) {
1097     return;
1098   }
1099 
1100   bool origin_tied = false;
1101   if (originPlotItem) {
1102     origin_tied = originPlotItem->isYTiedZoom();
1103   }
1104 
1105   if (!(_shareY || origin_tied)) {
1106 
1107     if (originPlotItem) {
1108       originPlotItem->zoomNormalizeXtoY(true);
1109     }
1110   } else {
1111     QRectF computedRect;
1112     foreach(PlotItem *plotItem, getSharedPlots()) {
1113       QRectF compute = plotItem->projectionRect();
1114 
1115       if (computedRect.isValid()) {
1116         computedRect = computedRect.united(compute);
1117       } else {
1118         computedRect = compute;
1119       }
1120     }
1121     qreal mean = computedRect.center().y();
1122     qreal range = originPlotItem->plotRect().width() * computedRect.height() / originPlotItem->plotRect().height();
1123 
1124     computedRect.setTop(mean - (range / 2.0));
1125     computedRect.setBottom(mean + (range / 2.0));
1126 
1127     _yAxisZoomMode = PlotAxis::FixedExpression;
1128     originPlotItem->zoomNormalizeYtoX(true);
1129     applyZoom(computedRect, originPlotItem, false, true);
1130   }
1131 }
1132 
1133 
zoomLogY(PlotItem * originPlotItem,bool autoEnable,bool enable)1134 void SharedAxisBoxItem::zoomLogY(PlotItem* originPlotItem, bool autoEnable, bool enable) {
1135   QList<PlotItem*> allPlots;
1136   if (_shareY) {
1137     allPlots = getSharedPlots();
1138   } else {
1139     allPlots = getTiedPlots(originPlotItem);
1140   }
1141   bool enableLog;
1142   if (autoEnable && originPlotItem) {
1143     enableLog = !originPlotItem->yAxis()->axisLog();
1144   } else {
1145     enableLog = enable;
1146   }
1147 
1148 
1149   bool origin_tied = false;
1150   if (originPlotItem) {
1151     origin_tied = originPlotItem->isYTiedZoom();
1152   }
1153 
1154   if (!(_shareY || origin_tied)) {
1155 
1156     if (originPlotItem) {
1157       originPlotItem->zoomLogY(true, false, enableLog);
1158     }
1159   } else {
1160     foreach(PlotItem* plotItem, allPlots) {
1161       plotItem->zoomLogY(true, false, enableLog);
1162     }
1163   }
1164   if (originPlotItem && originPlotItem->isYTiedZoom() && originPlotItem->isInSharedAxisBox() && (originPlotItem->sharedAxisBox() == this)) {
1165     QList<PlotItem*> plotTied = PlotItemManager::tiedZoomPlotsForView(view());
1166 
1167     foreach(PlotItem* plotItem, plotTied) {
1168       if (!allPlots.contains(plotItem)) {
1169         plotItem->zoomLogY(true, false, enableLog);
1170       }
1171     }
1172   }
1173 }
1174 
1175 
1176 
updateZoomForDataUpdate(qint64 serial)1177 void SharedAxisBoxItem::updateZoomForDataUpdate(qint64 serial) {
1178   if (serial == _serialOfLastChange) {
1179     return;
1180   }
1181   _serialOfLastChange = serial;
1182   if (_shareX) {
1183     switch (xAxisZoomMode()) {
1184       case PlotAxis::Auto:
1185         zoomXMaximum(0);
1186         break;
1187       case PlotAxis::AutoBorder:
1188         zoomXAutoBorder(0);
1189         break;
1190       case PlotAxis::SpikeInsensitive:
1191         zoomXNoSpike(0);
1192         break;
1193       default:
1194         break;
1195     }
1196   }
1197   if (_shareY) {
1198     switch (yAxisZoomMode()) {
1199       case PlotAxis::Auto:
1200         zoomYMaximum(0);
1201         break;
1202       case PlotAxis::AutoBorder:
1203         zoomYAutoBorder(0);
1204         break;
1205       case PlotAxis::SpikeInsensitive:
1206         zoomYNoSpike(0);
1207         break;
1208       case PlotAxis::MeanCentered:
1209         zoomMeanCentered(0);
1210         break;
1211       default:
1212         break;
1213     }
1214   }
1215 }
1216 
1217 
createItem()1218 void CreateSharedAxisBoxCommand::createItem() {
1219   _item = new SharedAxisBoxItem(_view);
1220   _view->setCursor(Qt::CrossCursor);
1221 
1222   CreateCommand::createItem();
1223 }
1224 
1225 
undo()1226 void CreateSharedAxisBoxCommand::undo() {
1227   Q_ASSERT(_item);
1228   _item->hide();
1229   SharedAxisBoxItem *shareBox = qobject_cast<SharedAxisBoxItem*>(_item);
1230   if (shareBox) {
1231     shareBox->breakShare();
1232   }
1233 }
1234 
1235 
redo()1236 void CreateSharedAxisBoxCommand::redo() {
1237   Q_ASSERT(_item);
1238   _item->show();
1239   SharedAxisBoxItem *shareBox = qobject_cast<SharedAxisBoxItem*>(_item);
1240   if (shareBox) {
1241     if (!shareBox->acceptItems()) {
1242       _item->hide();
1243     }
1244   }
1245 }
1246 
1247 
creationComplete()1248 void CreateSharedAxisBoxCommand::creationComplete() {
1249   Q_ASSERT(_item);
1250   SharedAxisBoxItem *shareBox = qobject_cast<SharedAxisBoxItem*>(_item);
1251   if (shareBox) {
1252     if (shareBox->acceptItems()) {
1253       CreateCommand::creationComplete();
1254     } else {
1255       kstApp->mainWindow()->clearDrawingMarker();
1256       deleteLater();
1257       _item->deleteLater();
1258     }
1259   }
1260 }
1261 
1262 
SharedAxisBoxItemFactory()1263 SharedAxisBoxItemFactory::SharedAxisBoxItemFactory()
1264 : GraphicsFactory() {
1265   registerFactory("sharedaxisbox", this);
1266 }
1267 
1268 
~SharedAxisBoxItemFactory()1269 SharedAxisBoxItemFactory::~SharedAxisBoxItemFactory() {
1270 }
1271 
1272 
generateGraphics(QXmlStreamReader & xml,ObjectStore * store,View * view,ViewItem * parent)1273 ViewItem* SharedAxisBoxItemFactory::generateGraphics(QXmlStreamReader& xml, ObjectStore *store, View *view, ViewItem *parent) {
1274   SharedAxisBoxItem *rc = 0;
1275   while (!xml.atEnd()) {
1276     bool validTag = true;
1277     if (xml.isStartElement()) {
1278       if (!rc && xml.name().toString() == "sharedaxisbox") {
1279         Q_ASSERT(!rc);
1280         rc = new SharedAxisBoxItem(view);
1281         if (parent) {
1282           rc->setParentViewItem(parent);
1283         }
1284         QXmlStreamAttributes attrs = xml.attributes();
1285         QStringRef av = attrs.value("sharex");
1286         if (!av.isNull()) {
1287           rc->setXAxisShared(QVariant(av.toString()).toBool());
1288         }
1289         av = attrs.value("sharey");
1290         if (!av.isNull()) {
1291           rc->setYAxisShared(QVariant(av.toString()).toBool());
1292         }
1293         av = attrs.value("xzoommode");
1294         if (!av.isNull()) {
1295           rc->setXAxisZoomMode((PlotAxis::ZoomMode)av.toString().toInt());
1296         }
1297         av = attrs.value("yzoommode");
1298         if (!av.isNull()) {
1299           rc->setYAxisZoomMode((PlotAxis::ZoomMode)av.toString().toInt());
1300         }
1301         // Add any new specialized SharedAxisBoxItem Properties here.
1302       } else {
1303         Q_ASSERT(rc);
1304         if (!rc->parse(xml, validTag) && validTag) {
1305           ViewItem *i = GraphicsFactory::parse(xml, store, view, rc);
1306           if (PlotItem *plotItem = qobject_cast<PlotItem*>(i)) {
1307             plotItem->setSharedAxisBox(rc);
1308             rc->_sharedPlots << plotItem;
1309             rc->connect(plotItem, SIGNAL(breakShareTriggered()), rc, SLOT(breakShare()));
1310             rc->connect(plotItem, SIGNAL(shareXAxisTriggered()), rc, SLOT(shareXAxis()));
1311             rc->connect(plotItem, SIGNAL(shareYAxisTriggered()), rc, SLOT(shareYAxis()));
1312           }
1313         }
1314       }
1315     } else if (xml.isEndElement()) {
1316       if (xml.name().toString() == "sharedaxisbox") {
1317         break;
1318       } else {
1319         validTag = false;
1320       }
1321     }
1322     if (!validTag) {
1323       Debug::self()->log(QObject::tr("Error creating sharedaxisbox object from Kst file."), Debug::Warning);
1324       delete rc;
1325       return 0;
1326     }
1327     xml.readNext();
1328   }
1329   rc->lockItems();
1330   return rc;
1331 }
1332 
1333 }
1334 
1335 // vim: ts=2 sw=2 et
1336