this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Guido Tack <tack@gecode.org>
5 *
6 * Copyright:
7 * Guido Tack, 2006
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34#ifndef GECODE_GIST_TREECANVAS_HH
35#define GECODE_GIST_TREECANVAS_HH
36
37#include <QtGui>
38#if QT_VERSION >= 0x050000
39#include <QtWidgets>
40#endif
41
42#include <gecode/kernel.hh>
43#include <gecode/gist.hh>
44
45#include <gecode/gist/visualnode.hh>
46
47namespace Gecode { namespace Gist {
48
49 /// \brief Parameters for the tree layout
50 namespace LayoutConfig {
51 /// Minimum scale factor
52 const int minScale = 10;
53 /// Maximum scale factor
54 const int maxScale = 400;
55 /// Default scale factor
56 const int defScale = 100;
57 /// Maximum scale factor for automatic zoom
58 const int maxAutoZoomScale = defScale;
59 }
60
61 class TreeCanvas;
62
63 /// \brief A thread that concurrently explores the tree
64 class SearcherThread : public QThread {
65 Q_OBJECT
66 private:
67 VisualNode* node;
68 int depth;
69 bool a;
70 TreeCanvas* t;
71 void updateCanvas(void);
72 public:
73 void search(VisualNode* n, bool all, TreeCanvas* ti);
74
75 Q_SIGNALS:
76 void update(int w, int h, int scale0);
77 void statusChanged(bool);
78 void scaleChanged(int);
79 void solution(const Space*);
80 void searchFinished(void);
81 void moveToNode(VisualNode* n,bool);
82 protected:
83 void run(void);
84 };
85
86 /// \brief A canvas that displays the search tree
87 class GECODE_GIST_EXPORT TreeCanvas : public QWidget {
88 Q_OBJECT
89
90 friend class SearcherThread;
91 friend class Gist;
92
93 public:
94 /// Constructor
95 TreeCanvas(Space* rootSpace, bool bab, QWidget* parent,
96 const Options& opt);
97 /// Destructor
98 ~TreeCanvas(void);
99
100 /// Add inspector \a i
101 void addDoubleClickInspector(Inspector* i);
102 /// Set active inspector
103 void activateDoubleClickInspector(int i, bool active);
104 /// Add inspector \a i
105 void addSolutionInspector(Inspector* i);
106 /// Set active inspector
107 void activateSolutionInspector(int i, bool active);
108 /// Add inspector \a i
109 void addMoveInspector(Inspector* i);
110 /// Set active inspector
111 void activateMoveInspector(int i, bool active);
112 /// Add comparator \a c
113 void addComparator(Comparator* c);
114 /// Set active comparator
115 void activateComparator(int i, bool active);
116
117 public Q_SLOTS:
118 /// Set scale factor to \a scale0
119 void scaleTree(int scale0, int zoomx=-1, int zoomy=-1);
120
121 /// Explore complete subtree of selected node
122 void searchAll(void);
123 /// Find next solution below selected node
124 void searchOne(void);
125 /// Toggle hidden state of selected node
126 void toggleHidden(void);
127 /// Hide failed subtrees of selected node
128 void hideFailed(void);
129 /// Unhide all nodes below selected node
130 void unhideAll(void);
131 /// Do not stop at selected stop node
132 void toggleStop(void);
133 /// Do not stop at any stop node
134 void unstopAll(void);
135 /// Export pdf of the current subtree
136 void exportPDF(void);
137 /// Export pdf of the whole tree
138 void exportWholeTreePDF(void);
139 /// Print the tree
140 void print(void);
141 /// Zoom the canvas so that the whole tree fits
142 void zoomToFit(void);
143 /// Center the view on the currently selected node
144 void centerCurrentNode(void);
145 /**
146 * \brief Call the double click inspector for the currently selected node
147 *
148 * If \a fix is true, then the node is inspected after fixpoint
149 * computation, otherwise its status after branching but before
150 * fixpoint computation is inspected.
151 */
152 void inspectCurrentNode(bool fix=true, int inspectorNo=-1);
153 /// Calls inspectCurrentNode(false)
154 void inspectBeforeFP(void);
155 /// Label all branches in subtree under current node
156 void labelBranches(void);
157 /// Label all branches on path to root node
158 void labelPath(void);
159
160 /// Stop current search
161 void stopSearch(void);
162
163 /// Reset
164 void reset(void);
165
166 /// Move selection to the parent of the selected node
167 void navUp(void);
168 /// Move selection to the first child of the selected node
169 void navDown(void);
170 /// Move selection to the left sibling of the selected node
171 void navLeft(void);
172 /// Move selection to the right sibling of the selected node
173 void navRight(void);
174 /// Move selection to the root node
175 void navRoot(void);
176 /// Move selection to next solution (in DFS order)
177 void navNextSol(bool back = false);
178 /// Move selection to previous solution (in DFS order)
179 void navPrevSol(void);
180
181 /// Bookmark current node
182 void bookmarkNode(void);
183 /// Set the current node to be the head of the path
184 void setPath(void);
185 /// Call the double click inspector for all nodes on the path from root to head of the path
186 void inspectPath(void);
187 /// Wait for click on node to compare with current node
188 void startCompareNodes(void);
189 /// Wait for click on node to compare with current node before fixpoint
190 void startCompareNodesBeforeFP(void);
191
192 /// Re-emit status change information for current node
193 void emitStatusChanged(void);
194
195 /// Set recomputation distances
196 void setRecompDistances(int c_d, int a_d);
197 /// Set preference whether to automatically hide failed subtrees
198 void setAutoHideFailed(bool b);
199 /// Set preference whether to automatically zoom to fit
200 void setAutoZoom(bool b);
201 /// Return preference whether to automatically hide failed subtrees
202 bool getAutoHideFailed(void);
203 /// Return preference whether to automatically zoom to fit
204 bool getAutoZoom(void);
205 /// Set preference whether to show copies in the tree
206 void setShowCopies(bool b);
207 /// Return preference whether to show copies in the tree
208 bool getShowCopies(void);
209 /// Set refresh rate
210 void setRefresh(int i);
211 /// Set refresh pause in msec
212 void setRefreshPause(int i);
213 /// Return preference whether to use smooth scrolling and zooming
214 bool getSmoothScrollAndZoom(void);
215 /// Set preference whether to use smooth scrolling and zooming
216 void setSmoothScrollAndZoom(bool b);
217 /// Return preference whether to move cursor during search
218 bool getMoveDuringSearch(void);
219 /// Set preference whether to move cursor during search
220 void setMoveDuringSearch(bool b);
221 /// Resize to the outer widget size if auto zoom is enabled
222 void resizeToOuter(void);
223
224 /// Stop search and wait for it to finish
225 bool finish(void);
226
227 Q_SIGNALS:
228 /// The scale factor has changed
229 void scaleChanged(int);
230 /// The auto-zoom state was changed
231 void autoZoomChanged(bool);
232 /// Context menu triggered
233 void contextMenu(QContextMenuEvent*);
234 /// Status bar update
235 void statusChanged(VisualNode*,const Statistics&, bool);
236 /// Signals that a solution has been found
237 void solution(const Space*);
238 /// Signals that %Gist is finished
239 void searchFinished(void);
240 /// Signals that a bookmark has been added
241 void addedBookmark(const QString& id);
242 /// Signals that a bookmark has been removed
243 void removedBookmark(int idx);
244 protected:
245 /// Mutex for synchronizing acccess to the tree
246 QMutex mutex;
247 /// Mutex for synchronizing layout and drawing
248 QMutex layoutMutex;
249 /// Search engine thread
250 SearcherThread searcher;
251 /// Flag signalling the search to stop
252 bool stopSearchFlag;
253 /// Flag signalling that Gist is ready to be closed
254 bool finishedFlag;
255 /// Allocator for nodes
256 Node::NodeAllocator* na;
257 /// The root node of the tree
258 VisualNode* root;
259 /// The currently best solution (for branch-and-bound)
260 BestNode* curBest;
261 /// The currently selected node
262 VisualNode* currentNode;
263 /// The head of the currently selected path
264 VisualNode* pathHead;
265 /// The registered click inspectors, and whether they are active
266 QVector<QPair<Inspector*,bool> > doubleClickInspectors;
267 /// The registered solution inspectors, and whether they are active
268 QVector<QPair<Inspector*,bool> > solutionInspectors;
269 /// The registered move inspectors, and whether they are active
270 QVector<QPair<Inspector*,bool> > moveInspectors;
271 /// The registered comparators, and whether they are active
272 QVector<QPair<Comparator*,bool> > comparators;
273
274 /// The bookmarks map
275 QVector<VisualNode*> bookmarks;
276
277 /// Whether node comparison action is running
278 bool compareNodes;
279 /// Whether node comparison action computes fixpoint
280 bool compareNodesBeforeFP;
281
282 /// The scale bar
283 QSlider* scaleBar;
284
285 /// Statistics about the search tree
286 Statistics stats;
287
288 /// Current scale factor
289 double scale;
290 /// Offset on the x axis so that the tree is centered
291 int xtrans;
292
293 /// Whether to hide failed subtrees automatically
294 bool autoHideFailed;
295 /// Whether to zoom automatically
296 bool autoZoom;
297 /// Whether to show copies in the tree
298 bool showCopies;
299 /// Refresh rate
300 int refresh;
301 /// Time (in msec) to pause after each refresh
302 int refreshPause;
303 /// Whether to use smooth scrolling and zooming
304 bool smoothScrollAndZoom;
305 /// Whether to move cursor during search
306 bool moveDuringSearch;
307
308 /// The recomputation distance
309 int c_d;
310 /// The adaptive recomputation distance
311 int a_d;
312
313 /// Return the node corresponding to the \a event position
314 VisualNode* eventNode(QEvent *event);
315 /// General event handler, used for displaying tool tips
316 bool event(QEvent *event);
317 /// Paint the tree
318 void paintEvent(QPaintEvent* event);
319 /// Handle mouse press event
320 void mousePressEvent(QMouseEvent* event);
321 /// Handle mouse double click event
322 void mouseDoubleClickEvent(QMouseEvent* event);
323 /// Handle context menu event
324 void contextMenuEvent(QContextMenuEvent* event);
325 /// Handle resize event
326 void resizeEvent(QResizeEvent* event);
327 /// Handle mouse wheel events
328 void wheelEvent(QWheelEvent* event);
329
330 /// Timer for smooth zooming
331 QTimeLine zoomTimeLine;
332 /// Timer for smooth scrolling
333 QTimeLine scrollTimeLine;
334 /// Target x coordinate after smooth scrolling
335 int targetX;
336 /// Source x coordinate after smooth scrolling
337 int sourceX;
338 /// Target y coordinate after smooth scrolling
339 int targetY;
340 /// Target y coordinate after smooth scrolling
341 int sourceY;
342
343 /// Target width after layout
344 int targetW;
345 /// Target height after layout
346 int targetH;
347 /// Target scale after layout
348 int targetScale;
349 /// Timer id for delaying the update
350 int layoutDoneTimerId;
351
352 /// Timer invoked for smooth zooming and scrolling
353 virtual void timerEvent(QTimerEvent* e);
354
355 public Q_SLOTS:
356 /// Update display
357 void update(void);
358 /// React to scroll events
359 void scroll(void);
360 /// Layout done
361 void layoutDone(int w, int h, int scale0);
362 /// Set the selected node to \a n
363 void setCurrentNode(VisualNode* n, bool finished=true, bool update=true);
364 private Q_SLOTS:
365 /// Search has finished
366 void statusChanged(bool);
367 /// Export PDF of the subtree of \a n
368 void exportNodePDF(VisualNode* n);
369 /// A new solution \a s has been found
370 void inspectSolution(const Space* s);
371 /// Scroll to \a i percent of the target
372 void scroll(int i);
373 };
374
375}}
376
377#endif
378
379// STATISTICS: gist-any