Fixed the display problem related to resizing the windows on mac.
[mldemos:auto-amcs-auto-amcs-mldemos.git] / MLDemos / canvas.h
1 /*********************************************************************\r
2 MLDemos: A User-Friendly visualization toolkit for machine learning\r
3 Copyright (C) 2010  Basilio Noris\r
4 Contact: mldemos@b4silio.com\r
5 \r
6 This library is free software; you can redistribute it and/or\r
7 modify it under the terms of the GNU Lesser General Public License,\r
8 version 3 as published by the Free Software Foundation.\r
9 \r
10 This library is distributed in the hope that it will be useful, but\r
11 WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
13 Lesser General Public License for more details.\r
14 \r
15 You should have received a copy of the GNU Lesser General Public\r
16 License along with this library; if not, write to the Free\r
17 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
18 *********************************************************************/\r
19 #ifndef _CANVAS_H_\r
20 #define _CANVAS_H_\r
21 \r
22 #include "datasetManager.h"\r
23 #include "mymaths.h"\r
24 #include <QWidget>\r
25 #include <map>\r
26 #include <QMouseEvent>\r
27 #include <QWheelEvent>\r
28 #include <QPainter>\r
29 #include <QPainterPath>\r
30 \r
31 class Canvas : public QWidget\r
32 {\r
33         Q_OBJECT\r
34 \r
35 public:\r
36         Canvas(QWidget *parent);\r
37         ~Canvas();\r
38 \r
39         bool DeleteData(QPointF center, float radius);\r
40         static bool bCrossesAsDots;\r
41         void DrawSamples();\r
42         void DrawObstacles();\r
43         void DrawTrajectories();\r
44         void DrawLiveTrajectory(QPainter &painter);\r
45         void ResetSamples(){drawnSamples = 0; drawnTrajectories = 0;};\r
46         void FitToData();\r
47         void RedrawAxes();\r
48         bool bDrawing;\r
49         QPainterPath DrawObstacle(Obstacle o);\r
50         fVec center;\r
51         float zoom;\r
52 \r
53 protected:\r
54         void paintEvent(QPaintEvent *event);\r
55         void mousePressEvent(QMouseEvent *event);\r
56         void wheelEvent(QWheelEvent *event);\r
57         void mouseReleaseEvent(QMouseEvent *event);\r
58         void mouseMoveEvent(QMouseEvent *event);\r
59         void enterEvent(QEvent *event);\r
60         void focusOutEvent(QFocusEvent *event);\r
61         void leaveEvent(QEvent *event);\r
62 \r
63 public:\r
64         DatasetManager *data;\r
65         QPixmap confidencePixmap;\r
66         QPixmap modelPixmap;\r
67         QPixmap infoPixmap;\r
68         QPixmap gridPixmap;\r
69         QPainterPath crosshair;\r
70         QPixmap samplesPixmap;\r
71         QPixmap trajectoriesPixmap;\r
72         QPixmap obstaclesPixmap;\r
73         QImage qimg;\r
74         bool bDisplayMap, bDisplayInfo, bDisplaySingle, bDisplaySamples, bDisplayTrajectories, bDisplayLearned, bDisplayGrid;\r
75         bool bShowCrosshair, bNewCrosshair;\r
76         int trajectoryCenterType, trajectoryResampleType, trajectoryResampleCount;\r
77         QPoint mouse, mouseAnchor;\r
78         fVec fromCanvas(QPointF point);\r
79         fVec fromCanvas(float x, float y);\r
80         QPointF toCanvasCoords(float x, float y);\r
81         QPointF toCanvasCoords(fvec sample);\r
82         QPointF toCanvas(fVec sample);\r
83         fvec toSampleCoords(QPointF point);\r
84         fvec toSampleCoords(float x, float y);\r
85         fvec canvasTopLeft();\r
86         fvec canvasBottomRight();\r
87         QRectF canvasRect();\r
88         void SetZoom(float zoom);\r
89         float GetZoom(){return zoom;};\r
90         void SetCenter(fVec center);\r
91         fvec GetCenter(){return center;};\r
92 \r
93         std::map<int,fvec> centers;\r
94         int drawnSamples;\r
95         int drawnTrajectories;\r
96         std::vector<fvec> liveTrajectory;\r
97 \r
98         bool SaveScreenshot(QString filename);\r
99         QPixmap GetScreenshot();\r
100 \r
101 public slots:\r
102         void ResizeEvent();\r
103         void SetConfidenceMap(QImage image);\r
104         void SetModelImage(QImage image);\r
105 \r
106 signals:\r
107         void DrawCrosshair();\r
108         void Drawing(fvec sample, int label);\r
109         void CanvasMoveEvent();\r
110         void Released();\r
111         void Navigation(fvec sample);\r
112 \r
113 public:\r
114         static QPixmap toPixmap(IplImage *src)\r
115         {\r
116                 QPixmap pixmap;\r
117                 if(src->nChannels == 4)\r
118                 {\r
119                         pixmap = QPixmap::fromImage(QImage((const unsigned char *)src->imageData,src->width, src->height, QImage::Format_RGB32)).copy();\r
120                 }\r
121                 else\r
122                 {\r
123                         IplImage *image = cvCreateImage(cvGetSize(src),8,4);\r
124                         cvCvtColor(src, image, src->nChannels==1 ? CV_GRAY2BGRA : CV_BGR2BGRA);\r
125                         QImage qimg = QImage((const unsigned char *)image->imageData, image->width, image->height, QImage::Format_RGB32);\r
126                         pixmap = QPixmap::fromImage(qimg).copy();\r
127                         cvReleaseImage(&image);\r
128                 }\r
129                 return pixmap;\r
130         }\r
131         static void drawCross(QPainter &painter, QPointF point, float radius, int label)\r
132         {\r
133                 float x = point.x();\r
134                 float y = point.y();\r
135                 QColor c1 = Qt::white;\r
136                 QColor c2 = Qt::black;\r
137 \r
138                 switch(label)\r
139                 {\r
140                 case 0:\r
141                         c1 = Qt::white;\r
142                         c2 = Qt::black;\r
143                         break;\r
144                 case 1:\r
145                         c1 = Qt::black;\r
146                         c2 = Qt::white;\r
147                         break;\r
148                 case 2:\r
149                         c1 = Qt::red;\r
150                         c2 = Qt::black;\r
151                         break;\r
152                 case 3:\r
153                         c1 = Qt::blue;\r
154                         c2 = Qt::white;\r
155                         break;\r
156                 default:\r
157                         srand(label);\r
158                         c1 = QColor(rand()%255,rand()%255,rand()%255);\r
159                         c2 = Qt::black;\r
160                         break;\r
161                 }\r
162 \r
163                 QPen pen = painter.pen();\r
164                 pen.setColor(c1);pen.setWidth(3);\r
165                 painter.setPen(pen);\r
166                 painter.drawLine(QPointF(x-radius, y-radius), QPointF(x+radius, y+radius));\r
167                 painter.drawLine(QPointF(x+radius, y-radius), QPointF(x-radius, y+radius));\r
168                 pen.setColor(c2);pen.setWidth(1);\r
169                 painter.setPen(pen);\r
170                 painter.drawLine(QPointF(x-radius, y-radius), QPointF(x+radius, y+radius));\r
171                 painter.drawLine(QPointF(x+radius, y-radius), QPointF(x-radius, y+radius));\r
172         }\r
173 \r
174         static void drawSample(QPainter &painter, QPointF point, float radius, int label)\r
175         {\r
176                 float x = point.x();\r
177                 float y = point.y();\r
178 \r
179                 if(label == 0)\r
180                 {\r
181                         //              radius = 10;\r
182                         painter.setBrush(Qt::white);\r
183                         painter.setPen(Qt::black);\r
184                         painter.drawEllipse(QRectF(x-radius/2.,y-radius/2.,radius,radius));\r
185                 }\r
186                 else if(label == 1)\r
187                 {\r
188                         //              radius = 10;\r
189                         painter.setBrush(Qt::black);\r
190                         painter.setPen(Qt::white);\r
191                         painter.drawEllipse(QRectF(x-radius/2.,y-radius/2.,radius,radius));\r
192                 }\r
193                 else if(label == 2)\r
194                 {\r
195                         //              radius = 10;\r
196                         painter.setBrush(Qt::red);\r
197                         painter.setPen(Qt::black);\r
198                         painter.drawEllipse(QRectF(x-radius/2.,y-radius/2.,radius,radius));\r
199                 }\r
200                 else if(label == 3)\r
201                 {\r
202                         //              radius = 10;\r
203                         painter.setBrush(Qt::blue);\r
204                         painter.setPen(Qt::white);\r
205                         painter.drawEllipse(QRectF(x-radius/2.,y-radius/2.,radius,radius));\r
206                 }\r
207                 else\r
208                 {\r
209                         //              radius = 10;\r
210                         srand(label);\r
211                         painter.setBrush(QColor(rand()%255,rand()%255,rand()%255));\r
212                         painter.setPen(Qt::black);\r
213                         painter.drawEllipse(QRectF(x-radius/2.,y-radius/2.,radius,radius));\r
214                 }\r
215         }\r
216 };\r
217 \r
218 #endif // _CANVAS_H_\r