|
bpp-qt
2.1.0
|
00001 // 00002 // File: TreeCanvas.h 00003 // Created by: Julien Dutheil 00004 // Created on: Tue Oct 4 09:20 2006 00005 // 00006 00007 /* 00008 Copyright or © or Copr. Bio++ Development Team, (November 16, 2004) 00009 00010 This software is a computer program whose purpose is to provide 00011 graphic components to develop bioinformatics applications. 00012 00013 This software is governed by the CeCILL license under French law and 00014 abiding by the rules of distribution of free software. You can use, 00015 modify and/ or redistribute the software under the terms of the CeCILL 00016 license as circulated by CEA, CNRS and INRIA at the following URL 00017 "http://www.cecill.info". 00018 00019 As a counterpart to the access to the source code and rights to copy, 00020 modify and redistribute granted by the license, users are provided only 00021 with a limited warranty and the software's author, the holder of the 00022 economic rights, and the successive licensors have only limited 00023 liability. 00024 00025 In this respect, the user's attention is drawn to the risks associated 00026 with loading, using, modifying and/or developing or reproducing the 00027 software by the user in light of its specific status of free software, 00028 that may mean that it is complicated to manipulate, and that also 00029 therefore means that it is reserved for developers and experienced 00030 professionals having in-depth computer knowledge. Users are therefore 00031 encouraged to load and test the software's suitability as regards their 00032 requirements in conditions enabling the security of their systems and/or 00033 data to be ensured and, more generally, to use and operate it in the 00034 same conditions as regards security. 00035 00036 The fact that you are presently reading this means that you have had 00037 knowledge of the CeCILL license and that you accept its terms. 00038 */ 00039 00040 #ifndef _TREECANVAS_H_ 00041 #define _TREECANVAS_H_ 00042 00043 #include "../QtGraphicDevice.h" 00044 #include "../MouseListener.h" 00045 00046 //From the STL: 00047 #include <vector> 00048 #include <map> 00049 #include <string> 00050 #include <algorithm> 00051 00052 //From PhylLib: 00053 #include <Bpp/Phyl/Tree.h> 00054 #include <Bpp/Phyl/Graphics/TreeDrawing.h> 00055 #include <Bpp/Phyl/Graphics/AbstractTreeDrawing.h> 00056 00057 //From Qt: 00058 #include <QGraphicsView> 00059 00060 namespace bpp 00061 { 00062 00063 class TreeCanvas; 00064 00068 class NodeMouseEvent: 00069 public QMouseEvent 00070 { 00071 private: 00072 bool hasNode_; 00073 int nodeId_; 00074 00075 public: 00076 NodeMouseEvent(const TreeCanvas& treeCanvas, const QMouseEvent& event); 00077 00078 bool hasNodeId() const { return hasNode_; } 00079 00080 int getNodeId() const throw (NodeNotFoundException) 00081 { 00082 if (!hasNode_) 00083 throw NodeNotFoundException("NodeMouseEvent::getNodeId().", ""); 00084 else 00085 return nodeId_; 00086 } 00087 00088 }; 00089 00096 class TreeCanvas: 00097 public QGraphicsView 00098 { 00099 Q_OBJECT 00100 00101 private: 00102 const Tree* currentTree_; 00103 TreeDrawing* treeDrawing_; 00104 TreeDrawing* defaultTreeDrawing_; 00105 mutable QtGraphicDevice device_; 00106 unsigned int drawingWidth_; 00107 unsigned int drawingHeight_; 00108 MouseListenerGroup mouseListenerGroup_; 00109 mutable std::map<int, bool> nodeCollapsed_; 00110 00111 public: 00112 TreeCanvas(QWidget* parent = 0); 00113 virtual ~TreeCanvas() 00114 { 00115 delete defaultTreeDrawing_; 00116 } 00117 00118 public: 00119 virtual void setTree(const Tree* tree); 00120 00121 virtual const Tree* getTree() const { return currentTree_; } 00122 00123 virtual void setTreeDrawing(const TreeDrawing& treeDrawing, bool repaint = true); 00124 00125 virtual TreeDrawing* getTreeDrawing() { return treeDrawing_; } 00126 virtual const TreeDrawing* getTreeDrawing() const { return treeDrawing_; } 00127 00128 virtual QtGraphicDevice& getDevice() { return device_; } 00129 virtual const QtGraphicDevice& getDevice() const { return device_; } 00130 00131 virtual void setDrawingSize(unsigned int width, unsigned int height) 00132 { 00133 drawingWidth_ = width; 00134 drawingHeight_ = height; 00135 redraw(); 00136 } 00137 00138 virtual unsigned int drawingWidth() const { return drawingWidth_; } 00139 virtual unsigned int drawingHeight() const { return drawingHeight_; } 00140 00141 void collapseNode(int nodeId, bool tf) throw (NodeNotFoundException) 00142 { 00143 if (!currentTree_) return; 00144 if (!currentTree_->hasNode(nodeId)) 00145 throw NodeNotFoundException("TreeCanvas::collapseNode.", nodeId); 00146 if (treeDrawing_) 00147 treeDrawing_->collapseNode(nodeId, tf); 00148 nodeCollapsed_[nodeId] = tf; 00149 } 00150 00151 bool isNodeCollapsed(int nodeId) const throw (NodeNotFoundException) 00152 { 00153 if (!currentTree_) return false; 00154 if (!currentTree_->hasNode(nodeId)) 00155 throw NodeNotFoundException("TreeCanvas::isNodeCollapsed.", nodeId); 00156 return nodeCollapsed_[nodeId]; 00157 } 00158 00164 QList<QGraphicsTextItem*> searchText(const QString& text); 00165 00171 void addMouseListener(MouseListener* listener) 00172 { 00173 mouseListenerGroup_.addMouseListener(listener); 00174 } 00175 00176 virtual void redraw(); 00177 00178 protected: 00179 00180 void mouseDoubleClickEvent(QMouseEvent* event) 00181 { 00182 std::auto_ptr<NodeMouseEvent> newEvent(new NodeMouseEvent(*this, *event)); 00183 mouseListenerGroup_.processMouseDoubleClickEvent(newEvent.get()); 00184 } 00185 00186 void mouseMoveEvent(QMouseEvent* event) 00187 { 00188 std::auto_ptr<NodeMouseEvent> newEvent(new NodeMouseEvent(*this, *event)); 00189 mouseListenerGroup_.processMouseMoveEvent(newEvent.get()); 00190 } 00191 void mousePressEvent(QMouseEvent* event) 00192 { 00193 std::auto_ptr<NodeMouseEvent> newEvent(new NodeMouseEvent(*this, *event)); 00194 mouseListenerGroup_.processMousePressEvent(newEvent.get()); 00195 } 00196 00197 void mouseReleaseEvent(QMouseEvent* event) 00198 { 00199 std::auto_ptr<NodeMouseEvent> newEvent(new NodeMouseEvent(*this, *event)); 00200 mouseListenerGroup_.processMouseReleaseEvent(newEvent.get()); 00201 } 00204 signals: 00205 void drawingChanged(); 00206 00207 }; 00208 00209 } //end of namespace bpp. 00210 00211 #endif //_TREECANVAS_H_ 00212