1 // Implementation of graphic display
2 // Copyright © 2009 The University of Chicago
3 #include "GraphicView.h"
8 #include <QContextMenuEvent>
11 #include "linepropertiesdialog.h"
12 #include "propertiesdialog.h"
14 #include "StateEmitHMM.h"
18 const double PI
= M_PI
;
20 //----------------------------------------------------------------------------------//
22 //----------------------------------------------------------------------------------//
24 Video::Video(int dimensionality
)
26 m_Dimensionality
= dimensionality
;
27 // m_NumberOfDataPoints = NumberOfDataPoints;
33 //setAutoDelete(TRUE); **** == Fix this -- we don't have auto-delete for the (new) QList. Memory leak will result now.
36 void Video::operator<< ( VideoFrame
* pVideoframe
)
43 //----------------------------------------------------------------------------------//
44 // Labeled Data Point class
45 //----------------------------------------------------------------------------------//
47 LabeledDataPoint::LabeledDataPoint( QString label
, int dimensionality
)
50 m_CurrentLocation
= 0;
51 m_Dimensionality
= dimensionality
;
52 m_Values
= new double [m_Dimensionality
];
54 //----------------------------------------------------------------------------------//
56 LabeledDataPoint::LabeledDataPoint( )
58 m_CurrentLocation
= 0;
63 //----------------------------------------------------------------------------------//
64 LabeledDataPoint::~LabeledDataPoint( )
69 //----------------------------------------------------------------------------------//
71 bool LabeledDataPoint::operator<< (double value
)
73 if ( m_CurrentLocation
< m_Dimensionality
)
75 m_Values
[m_CurrentLocation
++] = value
;
81 //----------------------------------------------------------------------------------//
83 //----------------------------------------------------------------------------------//
84 VideoFrame::VideoFrame (int dimensionality
)
86 m_Dimensionality
= dimensionality
;
89 //----------------------------------------------------------------------------------//
92 VideoFrame::VideoFrame ( )
96 //----------------------------------------------------------------------------------//
99 void VideoFrame::Incorporate(VideoFrame
* OtherOne
)
101 //This int-to-int maps will "translate" from each label index
102 // to the CParse which is the union of the two.
103 IntToInt Translator1
, Translator2
;
104 CParse
BothLabels (m_DimensionLabels
);
105 LabeledDataPoint
* pDataPoint
;
107 double* tempVector
= NULL
;
109 BothLabels
.Append(OtherOne
->m_DimensionLabels
);
111 for ( i
= 1; i
<= m_Dimensionality
; i
++)
113 n
= BothLabels
.Find( m_DimensionLabels
[i
] );
114 Translator1
.insert (i
, n
);
117 for ( i
= 1; i
<= OtherOne
->m_Dimensionality
; i
++)
119 n
= BothLabels
.Find( OtherOne
->m_DimensionLabels
[i
] );
121 Translator2
.insert (i
, n
);
125 int newDimensionality
= BothLabels
.Size();
127 //for ( pDataPoint = first(); pDataPoint; pDataPoint = next() )
128 for ( int z
= 0; z
< size(); z
++)
129 { pDataPoint
= at(z
);
130 tempVector
= new double[newDimensionality
];
131 for (i
= 1; i
<= newDimensionality
; i
++)
135 for (i
= 1; i
<= m_Dimensionality
; i
++)
137 tempVector
[Translator1
[i
]] = pDataPoint
->m_Values
[ i
] ;
138 }// this puts the right value in the new column for the converted data-point-values
140 delete pDataPoint
->m_Values
;
141 pDataPoint
->m_Values
= tempVector
;
142 pDataPoint
->m_Dimensionality
= newDimensionality
;
145 //Second we convert the data points in the OtherOne:
146 //for ( pDataPoint = OtherOne->first(); pDataPoint; pDataPoint= OtherOne->next())
147 for (int z
= 0; z
< OtherOne
->size(); z
++)
148 { pDataPoint
= OtherOne
->at(z
);
149 tempVector
= new double[newDimensionality
];
151 for (i
= 1; i
<= OtherOne
->m_Dimensionality
; i
++)
153 tempVector
[Translator2
[i
]] = pDataPoint
->m_Values
[ i
] ;
155 }// this puts the right value in the new column for the converted data-point-values
157 delete pDataPoint
->m_Values
;
158 pDataPoint
->m_Values
= tempVector
;
159 pDataPoint
->m_Dimensionality
= newDimensionality
;
162 m_DimensionLabels
= BothLabels
;
166 //----------------------------------------------------------------------------------//
168 void VideoFrame::operator<< (LabeledDataPoint
* datapoint
)
170 if (m_Dimensionality
!= datapoint
->m_Dimensionality
) return;
174 //----------------------------------------------------------------------------------//
175 void VideoFrame::AddLabels( CSS string
)
177 m_DimensionLabels
.Append (string
);
181 //----------------------------------------------------------------------------------//
182 // Graphic View class
183 //----------------------------------------------------------------------------------//
185 //////////////////////////////////////////////////////////////////////
186 // Construction/Destruction
187 //////////////////////////////////////////////////////////////////////7
189 GraphicView::GraphicView()
193 GraphicView::GraphicView(Q3Canvas
*canvas
, QMap
<QString
, QString
>* filter
, QWidget
*parent
)
194 : Q3CanvasView(canvas
, parent
)
200 m_IsStateView
= false;
208 GraphicView::~GraphicView()
214 ////////////////////////////////
215 //// Create Action function
217 void GraphicView::createActions()
220 PropertiesAct
= new QAction(tr("&Properties..."), this);
221 connect(PropertiesAct
, SIGNAL(activated()),this, SLOT(Properties()));
223 PlotStatesAct
= new QAction(tr("&Graphic Display State"), this);
224 connect(PlotStatesAct
, SIGNAL(activated()), this, SLOT(PlotStates()));
226 RefreshAct
= new QAction(tr("&Refresh"), this);
227 connect(RefreshAct
, SIGNAL(activated()), this, SLOT(Refresh()));
230 DisplayMeAct
= new QAction(tr("&DisplayMe"), this);
231 connect(DisplayMeAct
, SIGNAL(activated()), this, SLOT(DisplayMe()));
238 //////////////////////////////////
239 /// Three Slot functions
240 // this is used for compounds, among other things.
241 void GraphicView::PlotStates()
243 this->activeItem
= 0;
245 if (m_MyState
!= 0) {
246 // Removed with Yu Hu’s FSA code.
247 struct not_implemented
{ };
249 throw not_implemented();
251 if (m_MyRoots
!= 0) {
253 foreach (Q3CanvasItem
* i
, canvas()->allItems())
256 // Get current parameters of qcanvasview
257 // and resize the qcanvas to fit this view
258 int CanvasView_Height
= height();
259 int CanvasView_Width
= width();
260 canvas()->resize(CanvasView_Width
- 5, CanvasView_Height
- 5);
262 // Display the forest
264 foreach (CEdge
* edge
, *m_MyRoots
) {
265 QRect rectangle
= edge
->m_Daughters
.first()
266 ->drawTree(canvas(), x
, 0, m_filter
);
267 x
= rectangle
.right() + 20;
272 void GraphicView::Refresh()
290 void GraphicView::DisplayMe()
292 if (DiagramBox
* Box
= dynamic_cast<DiagramBox
*>(activeItem
)) {
293 CState
* pState
= Box
->m_MyState
;
299 void GraphicView::Properties()
301 if ( !m_IsStateView
) return;
303 if ((activeItem
) && (activeItem
->rtti() == DiagramBox::RTTI
))
305 PropertiesDialog dialog
;
306 dialog
.exec(activeItem
);
310 if ((activeItem
) && (activeItem
->rtti() == DiagramLine::RTTI
))
312 LinePropertiesDialog dialog
;
313 dialog
.exec(activeItem
);
320 void GraphicView::PlotStems()
323 if ( !m_MyStem
) return;
326 int CanvasView_Height
,Canvas_Height
;
327 int CanvasView_Width
,Canvas_Width
;
328 int CanvasLeftUpperX
;
329 int CanvasLeftUpperY
;
331 Q3CanvasItem
*AddedText
;
332 int XLocOfOneBar
, YLocOfOneBar
;
333 int WidthOfOneBar
= 60;
335 int rightReserve
=60;
337 int bottemReserve
= 20;
338 int nameBarHeight
= 40;
341 int numberOfUnigrams
;
344 double maximumPostive
;
345 double maximumNegative
;
346 double totalValueRange
;
348 int attemptedWidthOfCanvas
;
351 QString oneNameValue
;
356 // Get some parmaters from the CStem
357 numberOfUnigrams
= m_MyStem
->m_countofunigrams
;
358 numberOfMIs
= m_MyStem
->m_countofmis
;
359 totalBars
= numberOfUnigrams
+ numberOfMIs
;
360 maximumPostive
= m_MyStem
->m_maxpositive
;
361 maximumNegative
= -(m_MyStem
->m_maxnegative
);
362 totalValueRange
= maximumPostive
+ maximumNegative
;
365 attemptedWidthOfCanvas
=leftReserve
+ WidthOfOneBar
* (totalBars
+1)+ rightReserve
; // this 1 is for the first "#"
369 Q3CanvasItemList list
= canvas()->allItems();
370 Q3CanvasItemList::Iterator it
= list
.begin();
371 for (; it
!= list
.end(); ++it
)
373 if ( *it
) delete *it
;
380 // Get current parameters of qcanvasview and resize the qcanvas to fit this view
381 CanvasView_Height
= height();
382 CanvasView_Width
= width();
384 if ( CanvasView_Width
>= attemptedWidthOfCanvas
)
386 canvas()->resize(CanvasView_Width
, CanvasView_Height
-20);
390 canvas()->resize(attemptedWidthOfCanvas
, CanvasView_Height
- 20);
393 RectOfCanvas
= canvas()->rect();
394 RectOfCanvas
.rect(&CanvasLeftUpperX
, &CanvasLeftUpperY
, &Canvas_Width
, &Canvas_Height
);
397 // compute the YOfTheNameBars
398 YOfTheNameBars
= Canvas_Height
- bottemReserve
- nameBarHeight
;
401 // compute height for display bars
402 attemptedHeight
= Canvas_Height
- topReserve
- bottemReserve
- nameBarHeight
;
404 // compute the zeroLineHeigt
405 int zeroLineHeight
= static_cast<int>(
406 double(attemptedHeight
) * (maximumPostive
/totalValueRange
));
408 if ( zeroLineHeight
> attemptedHeight
)
410 zeroLineHeight
= attemptedHeight
;
413 // Draw the zero line
414 Q3CanvasLine
* AddedLine
= new Q3CanvasLine(canvas());
415 zeroLineHeight
+= topReserve
;
416 AddedLine
->setPoints(0,zeroLineHeight
, Canvas_Width
, zeroLineHeight
);
417 showNewItem(AddedLine
);
419 //Add the first probbox refer to the first "#" a WidthOfOneBar x 20 positive bar
420 XLocOfOneBar
= leftReserve
;
421 YLocOfOneBar
= zeroLineHeight
- 20;
422 int HeightOfOneBar
= 20;
424 ProbBox
* AddedBar
= new ProbBox(canvas(),
425 WidthOfOneBar
, HeightOfOneBar
, true, 1);
426 DisplayText
= QString(" Unigram");
427 AddedBar
->setText(DisplayText
);
428 AddedBar
->move(XLocOfOneBar
, YLocOfOneBar
);
429 showNewItem(AddedBar
);
431 // Add text for this "#" bar
432 AddedText
= new Q3CanvasText(QString(" # "),canvas() );
433 AddedText
->move(XLocOfOneBar
, YOfTheNameBars
);
434 showNewItem(AddedText
);
437 // Add ProbBox one by one. They might be Unigram prob, MI...
438 for ( i
=0; i
< numberOfUnigrams
; i
++)
441 oneLogValue
= m_MyStem
->m_mis
[i
];
443 if ( oneLogValue
> 0)
445 XLocOfOneBar
= leftReserve
+ WidthOfOneBar
+ i
*2*WidthOfOneBar
;
446 int HeightOfOneBar
= static_cast<int>(
447 double(attemptedHeight
) *
448 (oneLogValue
/totalValueRange
));
450 YLocOfOneBar
= zeroLineHeight
- HeightOfOneBar
;
452 ProbBox
* AddedBar
= new ProbBox(canvas(), WidthOfOneBar
, HeightOfOneBar
, true, 2);
453 DisplayText
= QString(" MI: \n: %1 ").arg(oneLogValue
);
454 AddedBar
->setText(DisplayText
);
455 AddedBar
->move(XLocOfOneBar
, YLocOfOneBar
);
456 showNewItem(AddedBar
);
459 else if ( oneLogValue
< 0)
461 oneLogValue
= -oneLogValue
;
463 XLocOfOneBar
= leftReserve
+ WidthOfOneBar
+ i
*2*WidthOfOneBar
;
465 int HeightOfOneBar
= static_cast<int>(
466 double(attemptedHeight
) *
467 (oneLogValue
/totalValueRange
));
469 YLocOfOneBar
= zeroLineHeight
;
471 ProbBox
* AddedBar
= new ProbBox(canvas(),
472 WidthOfOneBar
, HeightOfOneBar
, false, 2);
473 DisplayText
= QString(" MI: \n: -%1 ").arg(oneLogValue
);
474 AddedBar
->setText(DisplayText
);
475 AddedBar
->move(XLocOfOneBar
, YLocOfOneBar
);
476 showNewItem(AddedBar
);
480 oneLogValue
= m_MyStem
->m_unigrams
[i
]; // oneLogValue can only be > 0
482 XLocOfOneBar
= leftReserve
+ WidthOfOneBar
+ i
*2*WidthOfOneBar
+ WidthOfOneBar
;
484 int HeightOfOneBar
= static_cast<int>(
485 double(attemptedHeight
) *
486 (oneLogValue
/totalValueRange
));
488 YLocOfOneBar
= zeroLineHeight
- HeightOfOneBar
;
490 ProbBox
* AddedBar
= new ProbBox(canvas(),
491 WidthOfOneBar
, HeightOfOneBar
, true, 1);
492 DisplayText
= QString(" Unigram: \n: %1 ").arg(oneLogValue
);
493 AddedBar
->setText(DisplayText
);
494 AddedBar
->move(XLocOfOneBar
, YLocOfOneBar
);
495 showNewItem(AddedBar
);
497 // Put on the Name Bar
498 oneNameValue
= m_MyStem
->m_phonologies
[i
];
499 AddedText
= new Q3CanvasText(QString(" %1").arg(oneNameValue
),canvas() );
500 AddedText
->move(XLocOfOneBar
, YOfTheNameBars
);
501 showNewItem(AddedText
);
507 /////////////////////////////////////////////////////////////////////////
509 /////////////////////////////////////////////////////////////////////////
511 void GraphicView::PlotNVectors(int numberOfDimensions
, int numberOfDataPoints
, double** ptrData
, IntToString listOfSymbols
)
513 // int leftReserve = 60; unused variables: leftReserve, rightReserve, topReserve, bottemReserve,
514 // int rightReserve = 60;
515 // int topReserve = 20;
516 // int bottemReserve = 20;
517 int CanvasView_Height
,Canvas_Height
;
518 int CanvasView_Width
,Canvas_Width
;
519 int CanvasLeftUpperX
;
520 int CanvasLeftUpperY
;
525 int radiusOfTheCircle
;
526 int radiusOfOneAnchor
= 20;
532 double** anchorCoordinates
;
534 double XLocOfOneDataPoint
,
536 Q3CanvasText
* oneDataPointText
;
540 if (numberOfDimensions
< 2)
542 QMessageBox::information ( NULL
, "Warning", "We don't support a dimension less than 2!", "OK" );
546 if (numberOfDataPoints
< 1)
548 QMessageBox::information ( NULL
, "Warning", "Number Of data points is less than 1!", "OK" );
554 QMessageBox::information ( NULL
, "Warning", "Pointer to Data is NULL!", "OK" );
559 // Get data parameters
560 m_numberOfDimension
= numberOfDimensions
;
561 // m_numberOfDataPoints = numberOfDataPoints;
562 m_multiDimensionDataPoints
= ptrData
;
565 Q3CanvasItemList list
= canvas()->allItems();
566 Q3CanvasItemList::Iterator it
= list
.begin();
567 for (; it
!= list
.end(); ++it
)
569 if ( *it
) delete *it
;
575 // Get current parameters of qcanvasview and resize the qcanvas to fit this view
576 CanvasView_Height
= height();
577 CanvasView_Width
= width();
579 canvas() ->resize(CanvasView_Width
-15, CanvasView_Height
-15);
580 RectOfCanvas
= canvas()->rect();
581 RectOfCanvas
.rect(&CanvasLeftUpperX
, &CanvasLeftUpperY
, &Canvas_Width
, &Canvas_Height
);
583 if ( Canvas_Width
> Canvas_Height
)
585 radiusOfTheCircle
= Canvas_Height
/2 - 5;
589 radiusOfTheCircle
= Canvas_Width
/2 - 5;
593 //radiusOfTheCircle = 100;
596 CanvasCenterX
= int ( CanvasLeftUpperX
+ 0.5 * Canvas_Width
);
597 CanvasCenterY
= int ( CanvasLeftUpperY
+ 0.5 * Canvas_Height
);
599 //allocate memory for anchors
600 anchorCoordinates
= new double*[numberOfDimensions
];
601 for ( i
=0; i
<numberOfDimensions
; i
++)
603 anchorCoordinates
[i
] = new double[2];
607 // Next, locate the anchors one by one.
608 // first anchor is as easy as ( CanvasCenterX +radiusOfTheCircle ,CanvasCenterY)
609 oneAnchor
= new Anchor(canvas(), radiusOfOneAnchor
, radiusOfOneAnchor
);
611 displayText
= QString("%1").arg(stateNumber
);
612 oneAnchor
->setText(displayText
);
613 XLocOfOneAnchor
= CanvasCenterX
+radiusOfTheCircle
;
614 YLocOfOneAnchor
= CanvasCenterY
;
615 oneAnchor
->move(XLocOfOneAnchor
, YLocOfOneAnchor
);
616 showNewItem(oneAnchor
);
617 anchorCoordinates
[stateNumber
][0] = XLocOfOneAnchor
;
618 anchorCoordinates
[stateNumber
][1] = YLocOfOneAnchor
;
622 // Now the rest of the anchors:
623 while(stateNumber
< numberOfDimensions
)
625 oneAnchor
= new Anchor(canvas(), radiusOfOneAnchor
, radiusOfOneAnchor
);
626 anchorDegree
= stateNumber
* (360.0 / numberOfDimensions
);
627 XLocOfOneAnchor
= int (CanvasCenterX
+ radiusOfTheCircle
*cos(PI
* anchorDegree
/180.0 ));
628 YLocOfOneAnchor
= int (CanvasCenterY
- radiusOfTheCircle
*sin(PI
* anchorDegree
/180.0 ));
629 oneAnchor
->move(XLocOfOneAnchor
, YLocOfOneAnchor
);
630 displayText
= QString("%1").arg(stateNumber
);
631 oneAnchor
->setText(displayText
);
632 showNewItem(oneAnchor
);
633 anchorCoordinates
[stateNumber
][0] = XLocOfOneAnchor
;
634 anchorCoordinates
[stateNumber
][1] = YLocOfOneAnchor
;
639 // Display the data points
640 for( i
=0; i
<numberOfDataPoints
; i
++)
642 // deal with i datapoint
643 computeProjectedLocForOneDataPoint(
650 oneDataPointText
= new Q3CanvasText(canvas());
652 //to do: get symbol list
653 displayText
= listOfSymbols
[i
];
654 oneDataPointText
->setText(displayText
);
655 oneDataPointText
->move(XLocOfOneDataPoint
, YLocOfOneDataPoint
);
656 showNewItem(oneDataPointText
);
661 for(i
=0; i
<numberOfDimensions
; i
++)
663 delete [] anchorCoordinates
[i
];
666 delete []anchorCoordinates
;
672 // ------------------------------------------------------------------------//
674 void GraphicView::PlotVideo (Video
* pVideo
)
676 // int leftReserve = 60;
677 // int rightReserve = 60; unused variables: leftReserve, rightReserve, topReserve, bottemReserve,
678 // int topReserve = 20;
679 // int bottemReserve = 20;
680 int CanvasView_Height
,Canvas_Height
;
681 int CanvasView_Width
,Canvas_Width
;
682 int CanvasLeftUpperX
;
683 int CanvasLeftUpperY
;
688 int radiusOfTheCircle
;
689 int radiusOfOneAnchor
= 20;
695 double** anchorCoordinates
;
697 double XLocOfOneDataPoint
,
699 Q3CanvasText
* oneDataPointText
;
701 if (pVideo
->GetDimensionality() < 2)
703 QMessageBox::information ( NULL
, "Warning", "We don't support a dimension less than 2!", "OK" );
710 // Get data parameters
711 m_numberOfDimension
= pVideo
->GetDimensionality();
712 // m_numberOfDataPoints = pVideo->GetNumberOfDataPoints();
714 // Get current parameters of qcanvasview and resize the qcanvas to fit this view
715 CanvasView_Height
= height();
716 CanvasView_Width
= width();
718 canvas() ->resize(CanvasView_Width
-15, CanvasView_Height
-15);
719 RectOfCanvas
= canvas()->rect();
720 RectOfCanvas
.rect(&CanvasLeftUpperX
, &CanvasLeftUpperY
, &Canvas_Width
, &Canvas_Height
);
722 if ( Canvas_Width
> Canvas_Height
)
724 radiusOfTheCircle
= Canvas_Height
/2 - 5;
728 radiusOfTheCircle
= Canvas_Width
/2 - 5;
732 //radiusOfTheCircle = 100;
735 CanvasCenterX
= int ( CanvasLeftUpperX
+ 0.5 * Canvas_Width
);
736 CanvasCenterY
= int ( CanvasLeftUpperY
+ 0.5 * Canvas_Height
);
738 //allocate memory for anchors
739 anchorCoordinates
= new double*[m_numberOfDimension
];
740 for ( i
=0; i
<m_numberOfDimension
; i
++)
742 anchorCoordinates
[i
] = new double[2];
746 // Next, locate the anchors one by one.
747 // first anchor is as easy as ( CanvasCenterX +radiusOfTheCircle ,CanvasCenterY)
750 oneAnchor
= new Anchor(canvas(), radiusOfOneAnchor
, radiusOfOneAnchor
);
752 displayText
= QString("%1").arg(stateNumber
);
753 oneAnchor
->setText(displayText
);
754 XLocOfOneAnchor
= CanvasCenterX
+radiusOfTheCircle
;
755 YLocOfOneAnchor
= CanvasCenterY
;
756 oneAnchor
->move(XLocOfOneAnchor
, YLocOfOneAnchor
);
757 showNewItem(oneAnchor
);
758 anchorCoordinates
[stateNumber
][0] = XLocOfOneAnchor
;
759 anchorCoordinates
[stateNumber
][1] = YLocOfOneAnchor
;
763 // Now the rest of the anchors:
764 while(stateNumber
< m_numberOfDimension
)
766 oneAnchor
= new Anchor(canvas(), radiusOfOneAnchor
, radiusOfOneAnchor
);
767 anchorDegree
= stateNumber
* (360.0 / m_numberOfDimension
);
768 XLocOfOneAnchor
= int (CanvasCenterX
+ radiusOfTheCircle
*cos(PI
* anchorDegree
/180.0 ));
769 YLocOfOneAnchor
= int (CanvasCenterY
- radiusOfTheCircle
*sin(PI
* anchorDegree
/180.0 ));
770 oneAnchor
->move(XLocOfOneAnchor
, YLocOfOneAnchor
);
771 displayText
= QString("%1").arg(stateNumber
);
772 oneAnchor
->setText(displayText
);
773 showNewItem(oneAnchor
);
774 anchorCoordinates
[stateNumber
][0] = XLocOfOneAnchor
;
775 anchorCoordinates
[stateNumber
][1] = YLocOfOneAnchor
;
782 VideoFrame
* pVideoFrame
;
783 LabeledDataPoint
* pDataPoint
;
784 // Display the data pointsp
785 //for ( pVideoFrame = pVideo->first(); pVideoFrame; pVideoFrame= pVideo->next() )
786 for (int z
= 0; z
< pVideo
->size(); z
++)
787 { pVideoFrame
= pVideo
->at(z
);
789 // ---------- Clear qcanvas -------------------------------//
790 Q3CanvasItemList list
= canvas()->allItems();
791 Q3CanvasItemList::Iterator it
= list
.begin();
792 for (; it
!= list
.end(); ++it
)
794 if ( *it
) delete *it
;
799 // ---------- End of Clear qcanvas -------------------------------//
803 //for (pDataPoint = pVideoFrame->first(); pDataPoint; pDataPoint = pVideoFrame->next() )
804 for (int y
= 0; y
< pVideoFrame
->size(); y
++)
805 { pDataPoint
= pVideoFrame
->at(y
);
806 computeProjectedLocForOneDataPoint(
812 oneDataPointText
= new Q3CanvasText(canvas());
813 displayText
= pDataPoint
->m_Label
;
814 oneDataPointText
->setText(displayText
);
815 oneDataPointText
->move(XLocOfOneDataPoint
,
817 showNewItem(oneDataPointText
);
819 // Sleep for a short time : Is there any Qt sleep function ?
821 for(j
=0; j
<500000; j
++)
823 dummy
= int (base2log (100));
824 dummy
= int (base2log (100));
832 // for (time = 0; time < pVideo->size(); time++)
834 // pData = &(*pVideo)[time];
835 // VideoFrame::Iterator iter = pData->begin();
836 // for (; iter != pData->end(); ++iter)
838 // /////////////// this is where the main work is done: /////////////////
839 // computeProjectedLocForOneDataPoint(
842 // anchorCoordinates,
843 // XLocOfOneDataPoint,
844 // YLocOfOneDataPoint);
845 // oneDataPointText = new QCanvasText(canvas());
846 // displayText = iter.data().m_Label;
847 // oneDataPointText ->setText(displayText);
848 // oneDataPointText ->move(XLocOfOneDataPoint,
849 // YLocOfOneDataPoint);
850 // showNewItem(oneDataPointText);
851 // ///////////////////////////////////////////////////////////////////////
857 for(i
=0; i
<m_numberOfDimension
; i
++)
859 delete [] anchorCoordinates
[i
];
862 delete []anchorCoordinates
;
880 // ------------------------------------------------------------------------//
881 // John rewrote: 7 2006
882 void GraphicView::PlotNVectors2(VideoFrame
* Data
)
884 // int leftReserve = 60; unused variables: leftReserve, rightReserve, topReserve, bottemReserve,
885 // int rightReserve = 60;
886 // int topReserve = 20;
887 // int bottemReserve = 20;
888 int CanvasView_Height
,Canvas_Height
;
889 int CanvasView_Width
,Canvas_Width
;
890 int CanvasLeftUpperX
;
891 int CanvasLeftUpperY
;
896 int radiusOfTheCircle
;
897 int radiusOfOneAnchor
= 20;
903 double** anchorCoordinates
;
905 double XLocOfOneDataPoint
,
907 Q3CanvasText
* oneDataPointText
;
909 if (Data
->m_Dimensionality
< 2)
911 QMessageBox::information ( NULL
, "Warning", "We don't support a dimension less than 2!", "OK" );
915 // TODO: put this back in (reformulated, though)
916 // if (Data->size() < 1)
918 // QMessageBox::information ( NULL, "Warning", "Number Of data points is less than 1!", "OK" );
924 // Get data parameters
925 m_numberOfDimension
= Data
->m_Dimensionality
;
926 // m_numberOfDataPoints = Data->size();
929 Q3CanvasItemList list
= canvas()->allItems();
930 Q3CanvasItemList::Iterator it
= list
.begin();
931 for (; it
!= list
.end(); ++it
)
933 if ( *it
) delete *it
;
939 // Get current parameters of qcanvasview and resize the qcanvas to fit this view
940 CanvasView_Height
= height();
941 CanvasView_Width
= width();
943 canvas() ->resize(CanvasView_Width
-15, CanvasView_Height
-15);
944 RectOfCanvas
= canvas()->rect();
945 RectOfCanvas
.rect(&CanvasLeftUpperX
, &CanvasLeftUpperY
, &Canvas_Width
, &Canvas_Height
);
947 if ( Canvas_Width
> Canvas_Height
)
949 radiusOfTheCircle
= Canvas_Height
/2 - 5;
953 radiusOfTheCircle
= Canvas_Width
/2 - 5;
957 //radiusOfTheCircle = 100;
960 CanvasCenterX
= int (CanvasLeftUpperX
+ 0.5 * Canvas_Width
);
961 CanvasCenterY
= int (CanvasLeftUpperY
+ 0.5 * Canvas_Height
);
963 //allocate memory for anchors
964 anchorCoordinates
= new double*[m_numberOfDimension
];
965 for ( i
=0; i
<m_numberOfDimension
; i
++)
967 anchorCoordinates
[i
] = new double[2];
971 // Next, locate the anchors one by one.
972 // first anchor is as easy as ( CanvasCenterX +radiusOfTheCircle ,CanvasCenterY)
975 oneAnchor
= new Anchor(canvas(), radiusOfOneAnchor
, radiusOfOneAnchor
);
977 displayText
= QString("%1").arg(stateNumber
);
978 oneAnchor
->setText(displayText
);
979 XLocOfOneAnchor
= CanvasCenterX
+radiusOfTheCircle
;
980 YLocOfOneAnchor
= CanvasCenterY
;
981 oneAnchor
->move(XLocOfOneAnchor
, YLocOfOneAnchor
);
982 showNewItem(oneAnchor
);
983 anchorCoordinates
[stateNumber
][0] = XLocOfOneAnchor
;
984 anchorCoordinates
[stateNumber
][1] = YLocOfOneAnchor
;
988 // Now the rest of the anchors:
989 while(stateNumber
< m_numberOfDimension
)
991 oneAnchor
= new Anchor(canvas(), radiusOfOneAnchor
, radiusOfOneAnchor
);
992 anchorDegree
= stateNumber
* (360.0 / m_numberOfDimension
);
993 XLocOfOneAnchor
= int(CanvasCenterX
+ radiusOfTheCircle
*cos(PI
* anchorDegree
/180.0 ));
994 YLocOfOneAnchor
= int(CanvasCenterY
- radiusOfTheCircle
*sin(PI
* anchorDegree
/180.0 ));
995 oneAnchor
->move(XLocOfOneAnchor
, YLocOfOneAnchor
);
996 displayText
= QString("%1").arg(stateNumber
);
997 oneAnchor
->setText(displayText
);
998 showNewItem(oneAnchor
);
999 anchorCoordinates
[stateNumber
][0] = XLocOfOneAnchor
;
1000 anchorCoordinates
[stateNumber
][1] = YLocOfOneAnchor
;
1006 LabeledDataPoint
* pDataPoint
;
1007 // Display the data pointsp
1008 //for (pDataPoint = Data->first(); pDataPoint; pDataPoint = Data->next() )
1009 for (int z
= 0; z
< Data
->size(); z
++)
1010 { pDataPoint
= Data
->at(z
);
1011 computeProjectedLocForOneDataPoint(
1015 YLocOfOneDataPoint
);
1016 oneDataPointText
= new Q3CanvasText(canvas());
1017 displayText
= pDataPoint
->m_Label
;
1018 oneDataPointText
->setText(displayText
);
1019 oneDataPointText
->move(XLocOfOneDataPoint
,
1020 YLocOfOneDataPoint
);
1021 showNewItem(oneDataPointText
);
1027 for(i
=0; i
<m_numberOfDimension
; i
++)
1029 delete [] anchorCoordinates
[i
];
1032 delete []anchorCoordinates
;
1038 //rewritten JG July 2006
1039 void GraphicView::computeProjectedLocForOneDataPoint( LabeledDataPoint
* Data
,
1040 double** anchorCoordinates
,
1041 double& xCoordinate
,
1042 double& yCoordinate
)
1046 double sumHighDimensionData
= 0.0;
1048 for ( i
=0; i
< Data
->m_Dimensionality
; i
++)
1050 sumHighDimensionData
+= Data
->m_Values
[i
];
1053 weights
= new double[Data
->m_Dimensionality
];
1055 for ( i
=0; i
< Data
->m_Dimensionality
; i
++)
1057 weights
[i
] = Data
->m_Values
[i
] / sumHighDimensionData
;
1063 // Compute xCoordinate and yCoordinate
1064 for ( i
=0; i
< Data
->m_Dimensionality
; i
++)
1066 xCoordinate
+= weights
[i
] * anchorCoordinates
[i
][0]; // anchorCoordinates[i][0] keeps the xCoordinate of anchor i
1067 yCoordinate
+= weights
[i
] * anchorCoordinates
[i
][1]; // anchorCoordinates[i][1] keeps the yCoordinate of anchor i
1073 //: will not be used:
1074 void GraphicView::computeProjectedLocForOneDataPoint(int numberOfStates
,
1075 double** anchorCoordinates
,
1076 double* highDimensionData
,
1077 double& xCoordinate
,
1078 double& yCoordinate
)
1082 double sumHighDimensionData
= 0.0;
1084 for ( i
=0; i
< numberOfStates
; i
++)
1086 sumHighDimensionData
+= highDimensionData
[i
];
1089 weights
= new double[numberOfStates
];
1091 for ( i
=0; i
< numberOfStates
; i
++)
1093 weights
[i
] = highDimensionData
[i
] / sumHighDimensionData
;
1099 // Compute xCoordinate and yCoordinate
1100 for ( i
=0; i
< numberOfStates
; i
++)
1102 xCoordinate
+= weights
[i
] * anchorCoordinates
[i
][0]; // anchorCoordinates[i][0] keeps the xCoordinate of anchor i
1103 yCoordinate
+= weights
[i
] * anchorCoordinates
[i
][1]; // anchorCoordinates[i][1] keeps the yCoordinate of anchor i
1109 /////////////////////////////
1110 //// Mouse event functions
1113 void GraphicView::contentsContextMenuEvent(QContextMenuEvent
*event
)
1115 if ( !m_IsStateView
) return;
1117 Q3PopupMenu
contextMenu(this);
1121 if ( activeItem
->rtti() == DiagramBox::RTTI
)
1123 contextMenu
.insertSeparator();
1124 PropertiesAct
->addTo(&contextMenu
);
1125 contextMenu
.insertSeparator();
1126 DisplayMeAct
->addTo(&contextMenu
);
1130 contextMenu
.insertSeparator();
1131 PropertiesAct
->addTo(&contextMenu
);
1137 RefreshAct
->addTo(&contextMenu
);
1138 contextMenu
.insertSeparator();
1139 PlotStatesAct
->addTo(&contextMenu
);
1141 contextMenu
.exec(event
->globalPos());
1146 void GraphicView::contentsMousePressEvent(QMouseEvent
*event
)
1148 if ( !m_IsStateView
) return;
1150 if ( event
->button() == Qt::LeftButton
)
1152 Q3CanvasItemList items
= canvas()->collisions(event
->pos());
1160 setActiveItem(*items
.begin());
1167 void GraphicView::contentsMouseDoubleClickEvent(QMouseEvent
*event
)
1169 if ( !m_IsStateView
) return;
1171 if (event
->button() == Qt::LeftButton
&& activeItem
)
1179 ///////////////////////////////////////
1180 //// Some other necessary functions
1182 void GraphicView::showNewItem(Q3CanvasItem
*item
)
1184 setActiveItem(item
);
1189 void GraphicView::setActiveItem(Q3CanvasItem
*item
)
1191 if (item
!= activeItem
)
1193 if (activeItem
) activeItem
->setActive(false);
1197 if (activeItem
) activeItem
->setActive(true);
1205 //////////////////////////////////////////////
1206 /////////////////////////////////////////////
1207 /// Next are implementation of DiagramBox
1210 DiagramBox::DiagramBox(Q3Canvas
*canvas
)
1211 : Q3CanvasRectangle(canvas
)
1214 setPen(QPen(Qt::blue
));
1215 setBrush(Qt::white
);
1219 DiagramBox::DiagramBox(Q3Canvas
*canvas
, int Width
, int Height
, CState
* MyState
)
1220 : Q3CanvasRectangle(canvas
)
1222 setSize(Width
, Height
);
1223 setPen(QPen(Qt::blue
));
1224 setBrush(Qt::white
);
1226 m_MyState
= MyState
;
1230 DiagramBox::~DiagramBox()
1235 void DiagramBox::setText(const QString
&newText
)
1242 void DiagramBox::drawShape(QPainter
&painter
)
1244 Q3CanvasRectangle::drawShape(painter
);
1245 painter
.drawText(rect(), Qt::AlignLeft
, text());
1250 //////////////////////////////////////////////
1251 /////////////////////////////////////////////
1252 /// Next are implementation of ProbBox
1255 ProbBox::ProbBox(Q3Canvas
*canvas
)
1256 : Q3CanvasRectangle(canvas
)
1259 setPen(QPen(Qt::blue
));
1260 setBrush(Qt::white
);
1264 ProbBox::ProbBox(Q3Canvas
*canvas
, int Width
, int Height
, bool positive
, int type
)
1265 : Q3CanvasRectangle(canvas
)
1267 // type =1 --> Unigram; type = 2 --> MI
1269 setSize(Width
, Height
);
1274 setPen(QPen(Qt::blue
));
1275 setBrush(Qt::green
);
1277 else if ( type
== 2)
1279 setPen(QPen(Qt::red
));
1284 setPen(QPen(Qt::white
));
1285 setBrush(Qt::white
);
1290 setPen(QPen(Qt::black
));
1303 void ProbBox::setText(const QString
&newText
)
1310 void ProbBox::drawShape(QPainter
&painter
)
1312 Q3CanvasRectangle::drawShape(painter
);
1313 painter
.drawText(rect(), Qt::AlignLeft
, text());
1320 /////////////////////////////////////////////
1321 ///// Implementation of DiagramLine
1323 DiagramLine::DiagramLine(Q3Canvas
*canvas
)
1324 : Q3CanvasLine(canvas
)
1326 setPoints(0, 0, 0, 99);
1329 DiagramLine::DiagramLine(QCanvas *canvas, int SX, int SY, int EX, int EY, int FromState, int ToState, QMap<QString, int>* Morphemes, Morphology* MyMorphology)
1330 : QCanvasLine(canvas)
1332 setPen(QPen(Qt::red));
1333 setPoints(SX, SY, EX, EY);
1334 m_FromWhichState = FromState;
1335 m_ToWhichState = ToState;
1336 m_Morphemes = *Morphemes;
1337 m_MyMorphology = MyMorphology;
1340 DiagramLine::~DiagramLine()
1345 void DiagramLine::drawShape(QPainter
&painter
)
1347 Q3CanvasLine::drawShape(painter
);
1350 //////////////////////////////////////////////
1351 /////////////////////////////////////////////
1352 /// Next are implementation of DiagramBox
1355 Anchor::Anchor(Q3Canvas
*canvas
)
1356 : Q3CanvasEllipse(canvas
)
1359 setPen(QPen(Qt::blue
));
1360 setBrush(Qt::white
);
1364 Anchor::Anchor(Q3Canvas
*canvas
, int Width
, int Height
)
1365 : Q3CanvasEllipse(canvas
)
1367 setSize(Width
, Height
);
1368 setPen(QPen(Qt::white
));
1380 void Anchor::setText(const QString
&newText
)
1387 void Anchor::drawShape(QPainter
&painter
)
1389 QPen
pen(Qt::white
, 2);
1391 Q3CanvasEllipse::drawShape(painter
);
1393 painter
.setPen(pen
);
1394 painter
.drawText(boundingRect(), Qt::AlignCenter
, text());