1 /***************************************************************************
2  *                                                                         *
3  *   copyright : (C) 2007 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 "plotitemmanager.h"
14 
15 #include "view.h"
16 #include "plotitem.h"
17 
18 #include <QDebug>
19 #include <QCoreApplication>
20 
21 namespace Kst {
22 
23 static PlotItemManager *_self = 0;
cleanup()24 void PlotItemManager::cleanup() {
25   delete _self;
26   _self = 0;
27 }
28 
29 
self()30 PlotItemManager *PlotItemManager::self() {
31   if (!_self) {
32     _self = new PlotItemManager;
33     qAddPostRoutine(cleanup);
34   }
35   return _self;
36 }
37 
38 
PlotItemManager()39 PlotItemManager::PlotItemManager() {
40 }
41 
42 
~PlotItemManager()43 PlotItemManager::~PlotItemManager() {
44 }
45 
46 
addTiedZoomPlot(PlotItem * plotItem,bool checkAll)47 void PlotItemManager::addTiedZoomPlot(PlotItem *plotItem, bool checkAll) {
48   if (!_tiedZoomViewPlotLists.contains(plotItem->view())) {
49     _tiedZoomViewPlotLists.insert(plotItem->view(), QList<PlotItem*>() << plotItem);
50   } else {
51     QList<PlotItem*> list = _tiedZoomViewPlotLists.value(plotItem->view());
52     list << plotItem;
53     _tiedZoomViewPlotLists.insert(plotItem->view(), list);
54   }
55   if (checkAll) {
56     checkAllTied(plotItem->view());
57   }
58 }
59 
60 
checkAllTied(View * view)61 void PlotItemManager::checkAllTied(View* view) {
62   bool bAllTied = true;
63   QList<ViewItem*> items = tieableItemsForView(view);
64   foreach (ViewItem* item, items) {
65     if (!item->isTiedZoom()) {
66       bAllTied = false;
67     }
68   }
69   if (bAllTied) {
70     emit allPlotsTiedZoom();
71   }
72 }
73 
74 
toggleAllTiedZoom(View * view)75 void PlotItemManager::toggleAllTiedZoom(View *view) {
76 
77   bool tiedZoom;
78 
79   // vote on if we should tie all, or untie all
80   int n_plots=0, n_tied=0;
81   QList<ViewItem *> tieable_items;
82   if (view) {
83     tieable_items = tieableItemsForView(view);
84   } else {
85     tieable_items = tieableItems();
86   }
87 
88   foreach(ViewItem* viewItem, tieable_items) {
89     if (viewItem->supportsTiedZoom()) {
90       ++n_plots;
91       if (viewItem->isTiedZoom()) {
92         ++n_tied;
93       }
94     }
95   }
96 
97   if (double(n_tied) > (double)n_plots*0.5) {
98     tiedZoom = false;
99   } else {
100     tiedZoom = true;
101   }
102 
103   foreach(ViewItem* viewItem, tieable_items) {
104     if (viewItem->supportsTiedZoom()) {
105       viewItem->setTiedZoom(tiedZoom, tiedZoom, false);
106     }
107   }
108 }
109 
110 
clearAllTiedZoom(View * view)111 void PlotItemManager::clearAllTiedZoom(View *view) {
112 
113   bool tiedZoom;
114 
115   QList<ViewItem *> tieable_items;
116   if (view) {
117     tieable_items = tieableItemsForView(view);
118   } else {
119     tieable_items = tieableItems();
120   }
121   tiedZoom = false;
122 
123   foreach(ViewItem* viewItem, tieable_items) {
124     if (viewItem->supportsTiedZoom()) {
125       viewItem->setTiedZoom(tiedZoom, tiedZoom, false);
126     }
127   }
128 }
129 
130 
addTiedZoomViewItem(ViewItem * viewItem,bool checkAll)131 void PlotItemManager::addTiedZoomViewItem(ViewItem *viewItem, bool checkAll) {
132   if (!_tiedZoomViewItemLists.contains(viewItem->view())) {
133     _tiedZoomViewItemLists.insert(viewItem->view(), QList<ViewItem*>() << viewItem);
134   } else {
135     QList<ViewItem*> list = _tiedZoomViewItemLists.value(viewItem->view());
136     list << viewItem;
137     _tiedZoomViewItemLists.insert(viewItem->view(), list);
138   }
139   if (checkAll) {
140     checkAllTied(viewItem->view());
141   }
142 }
143 
144 
removeTiedZoomPlot(PlotItem * plotItem)145 void PlotItemManager::removeTiedZoomPlot(PlotItem *plotItem) {
146   if (_tiedZoomViewItemPlotLists.contains(plotItem->parentViewItem())) {
147     QList<PlotItem*> list = _tiedZoomViewItemPlotLists.value(plotItem->parentViewItem());
148     list.removeAll(plotItem);
149     _tiedZoomViewItemPlotLists.insert(plotItem->parentViewItem(), list);
150   } else if (_tiedZoomViewPlotLists.contains(plotItem->view())) {
151     QList<PlotItem*> list = _tiedZoomViewPlotLists.value(plotItem->view());
152     list.removeAll(plotItem);
153     _tiedZoomViewPlotLists.insert(plotItem->view(), list);
154     emit tiedZoomRemoved();
155   }
156 }
157 
158 
removeTiedZoomViewItem(ViewItem * viewItem)159 void PlotItemManager::removeTiedZoomViewItem(ViewItem *viewItem) {
160   if (_tiedZoomViewItemLists.contains(viewItem->view())) {
161     QList<ViewItem*> list = _tiedZoomViewItemLists.value(viewItem->view());
162     list.removeAll(viewItem);
163     _tiedZoomViewItemLists.insert(viewItem->view(), list);
164     emit tiedZoomRemoved();
165   }
166 }
167 
plotsForView(View * view)168 QList<PlotItem*> PlotItemManager::plotsForView(View *view) {
169   QList<QGraphicsItem*> graphics_items = view->scene()->items();
170   QList<PlotItem *> plot_items;
171 
172   foreach(QGraphicsItem* graphics_item, graphics_items) {
173     PlotItem *item = dynamic_cast<PlotItem*>(graphics_item);
174     if (item && item->isVisible()) {
175       plot_items.append(item);
176     }
177   }
178 
179   qSort(plot_items.begin(), plot_items.end(), shortNameLessThan);
180 
181   return plot_items;
182 }
183 
tieableItems()184 QList<ViewItem*> PlotItemManager::tieableItems() {
185   QList<ViewItem*> allViewItems = ViewItem::getItems<ViewItem>();
186   QList<ViewItem*> view_items;
187   foreach (ViewItem *item, allViewItems) {
188     if (item && item->isVisible() && item->supportsTiedZoom()) {
189       view_items.append(item);
190     }
191   }
192   return view_items;
193 }
194 
tieableItemsForView(View * view)195 QList<ViewItem*> PlotItemManager::tieableItemsForView(View *view) {
196   QList<QGraphicsItem*> graphics_items = view->scene()->items();
197   QList<ViewItem *> view_items;
198 
199   foreach(QGraphicsItem* graphics_item, graphics_items) {
200     ViewItem *item = dynamic_cast<ViewItem*>(graphics_item);
201     if (item && item->isVisible() && item->supportsTiedZoom()) {
202       view_items.append(item);
203     }
204   }
205 
206   qSort(view_items.begin(), view_items.end(), shortNameLessThan);
207 
208   return view_items;
209 
210 }
211 
tiedZoomPlotsForView(View * view)212 QList<PlotItem*> PlotItemManager::tiedZoomPlotsForView(View *view) {
213   if (kstApp->mainWindow()->isTiedTabs()) {
214     QList<PlotItem*> plots;
215     QList<View*> keys = PlotItemManager::self()->_tiedZoomViewPlotLists.keys();
216     foreach (View *view, keys) {
217       const QList<PlotItem*> plotlist = PlotItemManager::self()->_tiedZoomViewPlotLists.value(view);
218       foreach(PlotItem* item, plotlist) {
219         plots.append(item);
220       }
221     }
222     return plots;
223   } else {
224     if (PlotItemManager::self()->_tiedZoomViewPlotLists.contains(view)) {
225       QList<PlotItem*> plots = PlotItemManager::self()->_tiedZoomViewPlotLists.value(view);
226       if (PlotItemManager::self()->_tiedZoomViewItemLists.contains(view)) {
227         foreach (ViewItem *viewItem, PlotItemManager::self()->_tiedZoomViewItemLists.value(view)) {
228           plots << tiedZoomPlotsForViewItem(viewItem);
229         }
230       }
231       return plots;
232     }
233   }
234   return QList<PlotItem*>();
235 }
236 
237 
tiedZoomPlotsForViewItem(ViewItem * viewItem)238 QList<PlotItem*> PlotItemManager::tiedZoomPlotsForViewItem(ViewItem *viewItem) {
239   if (PlotItemManager::self()->_tiedZoomViewItemPlotLists.contains(viewItem)) {
240     return PlotItemManager::self()->_tiedZoomViewItemPlotLists.value(viewItem);
241   }
242   return QList<PlotItem*>();
243 }
244 
245 
setFocusPlot(PlotItem * plotItem)246 void PlotItemManager::setFocusPlot(PlotItem *plotItem) {
247   QList<PlotItem*> plots = plotsForView(plotItem->view());
248   foreach (PlotItem* plot, plots) {
249     if (plotItem != plot) {
250       plot->setAllowUpdates(false);
251     }
252   }
253 }
254 
255 
tiedZoomPlots(PlotItem * plotItem)256 QList<PlotItem*> PlotItemManager::tiedZoomPlots(PlotItem* plotItem) {
257   if (kstApp->mainWindow()->isTiedTabs()) {
258     QList<PlotItem*> plots;
259     QList<View *> keys = PlotItemManager::self()->_tiedZoomViewPlotLists.keys();
260     foreach (View *view, keys) {
261       const QList<PlotItem*> plotlist = PlotItemManager::self()->_tiedZoomViewPlotLists.value(view);
262       foreach(PlotItem* item, plotlist) {
263         plots.append(item);
264       }
265     }
266 
267     return plots;
268 
269   } else {
270     if (plotItem->isInSharedAxisBox() && !plotItem->parentViewItem()->isTiedZoom()) {
271       return tiedZoomPlotsForViewItem(plotItem->parentViewItem());
272     } else {
273       return tiedZoomPlotsForView(plotItem->view());
274     }
275   }
276 }
277 
tiedZoomViewItems(PlotItem * plotItem)278 QList<ViewItem*> PlotItemManager::tiedZoomViewItems(PlotItem* plotItem) {
279   return PlotItemManager::self()->_tiedZoomViewItemLists.value(plotItem->view());
280 }
281 
282 
removeFocusPlot(PlotItem * plotItem)283 void PlotItemManager::removeFocusPlot(PlotItem *plotItem) {
284   QList<PlotItem*> plots = plotsForView(plotItem->view());
285   foreach (PlotItem* plot, plots) {
286     if (plotItem != plot) {
287       plot->setAllowUpdates(true);
288     }
289   }
290 }
291 
292 
clearFocusedPlots()293 void PlotItemManager::clearFocusedPlots() {
294   QList<PlotItem*> plotItems = ViewItem::getItems<PlotItem>();
295   foreach (PlotItem* plotItem, plotItems) {
296     if (plotItem->isMaximized()) {
297       plotItem->plotMaximize();
298     }
299   }
300 }
301 
302 }
303 
304 // vim: ts=2 sw=2 et
305