CMiniLexicon::FindMajorSignatures(): use log file routines
[linguistica.git] / GraphicView.cpp
blobd5cef1faed2e219a8a90cfe403347f93a5f9f8d7
1 // Implementation of graphic display
2 // Copyright © 2009 The University of Chicago
3 #include "GraphicView.h"
5 #include <cmath>
6 #include <QMessageBox>
7 #include <Q3PopupMenu>
8 #include <QContextMenuEvent>
9 #include <QMouseEvent>
10 #include <QAction>
11 #include "linepropertiesdialog.h"
12 #include "propertiesdialog.h"
13 #include "Stem.h"
14 #include "StateEmitHMM.h"
15 #include "log2.h"
17 namespace {
18 const double PI = M_PI;
20 //----------------------------------------------------------------------------------//
21 // Video class
22 //----------------------------------------------------------------------------------//
24 Video::Video(int dimensionality )
26 m_Dimensionality = dimensionality;
27 // m_NumberOfDataPoints = NumberOfDataPoints;
28 m_NumberOfFrames = 0;
31 Video::~Video()
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)
38 m_NumberOfFrames++;
39 append (pVideoframe);
43 //----------------------------------------------------------------------------------//
44 // Labeled Data Point class
45 //----------------------------------------------------------------------------------//
47 LabeledDataPoint::LabeledDataPoint( QString label, int dimensionality)
49 m_Label = label;
50 m_CurrentLocation = 0;
51 m_Dimensionality = dimensionality;
52 m_Values = new double [m_Dimensionality];
54 //----------------------------------------------------------------------------------//
56 LabeledDataPoint::LabeledDataPoint( )
58 m_CurrentLocation = 0;
59 m_Dimensionality = 0;
60 m_Values = NULL;
63 //----------------------------------------------------------------------------------//
64 LabeledDataPoint::~LabeledDataPoint( )
66 delete m_Values;
69 //----------------------------------------------------------------------------------//
71 bool LabeledDataPoint::operator<< (double value)
73 if ( m_CurrentLocation < m_Dimensionality )
75 m_Values[m_CurrentLocation++] = value;
77 return TRUE;
81 //----------------------------------------------------------------------------------//
82 // Video Frame class
83 //----------------------------------------------------------------------------------//
84 VideoFrame::VideoFrame (int dimensionality)
86 m_Dimensionality = dimensionality;
89 //----------------------------------------------------------------------------------//
92 VideoFrame::VideoFrame ( )
94 m_Dimensionality = 0;
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;
106 int i, n;
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++)
133 tempVector[i] = 0;
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;
171 append ( datapoint);
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()
191 m_filter = NULL;
193 GraphicView::GraphicView(Q3Canvas *canvas, QMap<QString, QString>* filter, QWidget *parent)
194 : Q3CanvasView(canvas, parent)
196 pendingItem = 0;
197 activeItem = 0;
198 m_MyState = 0;
199 m_MyStem = 0;
200 m_IsStateView = false;
201 m_MyRoots = 0;
202 m_filter = filter;
203 createActions();
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) {
252 // Clear qcanvas
253 foreach (Q3CanvasItem* i, canvas()->allItems())
254 delete i;
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
263 int x = 10;
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()
274 if ( m_IsStateView)
276 if ( m_MyState)
278 PlotStates();
281 else
283 if ( m_MyStem)
285 PlotStems();
290 void GraphicView::DisplayMe()
292 if (DiagramBox* Box = dynamic_cast<DiagramBox*>(activeItem)) {
293 CState* pState = Box->m_MyState;
294 SetMyState(pState);
295 PlotStates();
299 void GraphicView::Properties()
301 if ( !m_IsStateView) return;
303 if ((activeItem) && (activeItem->rtti() == DiagramBox::RTTI ))
305 PropertiesDialog dialog;
306 dialog.exec(activeItem);
307 return;
310 if ((activeItem) && (activeItem->rtti() == DiagramLine::RTTI ))
312 LinePropertiesDialog dialog;
313 dialog.exec(activeItem);
314 return;
320 void GraphicView::PlotStems()
323 if ( !m_MyStem) return;
325 //int DebugInt;
326 int CanvasView_Height,Canvas_Height;
327 int CanvasView_Width,Canvas_Width;
328 int CanvasLeftUpperX;
329 int CanvasLeftUpperY;
330 QRect RectOfCanvas;
331 Q3CanvasItem *AddedText;
332 int XLocOfOneBar, YLocOfOneBar;
333 int WidthOfOneBar = 60;
334 int leftReserve =60;
335 int rightReserve =60;
336 int topReserve = 20;
337 int bottemReserve = 20;
338 int nameBarHeight = 40;
339 int YOfTheNameBars;
340 QString DisplayText;
341 int numberOfUnigrams;
342 int numberOfMIs;
343 int totalBars;
344 double maximumPostive;
345 double maximumNegative;
346 double totalValueRange;
347 int attemptedHeight;
348 int attemptedWidthOfCanvas;
349 int i;
350 double oneLogValue;
351 QString oneNameValue;
354 activeItem = 0;
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 "#"
368 // Clear qcanvas
369 Q3CanvasItemList list = canvas()->allItems();
370 Q3CanvasItemList::Iterator it = list.begin();
371 for (; it != list.end(); ++it)
373 if ( *it ) delete *it;
376 pendingItem = NULL;
377 activeItem = NULL;
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);
388 else
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++)
440 // One MI bar
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);
479 // One Unigram Bar
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 /////////////////////////////////////////////////////////////////////////
508 // will not be used:
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;
521 int CanvasCenterX;
522 int CanvasCenterY;
523 int XLocOfOneAnchor;
524 int YLocOfOneAnchor;
525 int radiusOfTheCircle;
526 int radiusOfOneAnchor = 20;
527 QRect RectOfCanvas;
528 Anchor* oneAnchor;
529 QString displayText;
530 int stateNumber;
531 double anchorDegree;
532 double** anchorCoordinates;
533 int i;
534 double XLocOfOneDataPoint,
535 YLocOfOneDataPoint;
536 Q3CanvasText* oneDataPointText;
540 if (numberOfDimensions < 2)
542 QMessageBox::information ( NULL, "Warning", "We don't support a dimension less than 2!", "OK" );
543 return;
546 if (numberOfDataPoints < 1)
548 QMessageBox::information ( NULL, "Warning", "Number Of data points is less than 1!", "OK" );
549 return;
552 if (ptrData == NULL)
554 QMessageBox::information ( NULL, "Warning", "Pointer to Data is NULL!", "OK" );
555 return;
559 // Get data parameters
560 m_numberOfDimension = numberOfDimensions;
561 // m_numberOfDataPoints = numberOfDataPoints;
562 m_multiDimensionDataPoints = ptrData;
564 // Clear qcanvas
565 Q3CanvasItemList list = canvas()->allItems();
566 Q3CanvasItemList::Iterator it = list.begin();
567 for (; it != list.end(); ++it)
569 if ( *it ) delete *it;
572 pendingItem = NULL;
573 activeItem = NULL;
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;
587 else
589 radiusOfTheCircle = Canvas_Width/2 - 5;
592 //JG:
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);
610 stateNumber = 0;
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;
619 stateNumber++;
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;
635 stateNumber++;
639 // Display the data points
640 for( i=0; i<numberOfDataPoints; i++)
642 // deal with i datapoint
643 computeProjectedLocForOneDataPoint(
644 numberOfDimensions,
645 anchorCoordinates,
646 ptrData[i],
647 XLocOfOneDataPoint,
648 YLocOfOneDataPoint);
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);
660 //release memory
661 for(i=0; i<numberOfDimensions; i++)
663 delete [] anchorCoordinates[i];
666 delete []anchorCoordinates;
668 return;
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;
684 int CanvasCenterX;
685 int CanvasCenterY;
686 int XLocOfOneAnchor;
687 int YLocOfOneAnchor;
688 int radiusOfTheCircle;
689 int radiusOfOneAnchor = 20;
690 QRect RectOfCanvas;
691 Anchor* oneAnchor;
692 QString displayText;
693 int stateNumber;
694 double anchorDegree;
695 double** anchorCoordinates;
696 int i;
697 double XLocOfOneDataPoint,
698 YLocOfOneDataPoint;
699 Q3CanvasText* oneDataPointText;
701 if (pVideo ->GetDimensionality() < 2)
703 QMessageBox::information ( NULL, "Warning", "We don't support a dimension less than 2!", "OK" );
704 return;
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;
726 else
728 radiusOfTheCircle = Canvas_Width/2 - 5;
731 //JG:
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);
751 stateNumber = 0;
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;
760 stateNumber++;
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;
776 stateNumber++;
781 // int time = 0;
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;
797 pendingItem = NULL;
798 activeItem = NULL;
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(
807 // &iter.data(),
808 pDataPoint,
809 anchorCoordinates,
810 XLocOfOneDataPoint,
811 YLocOfOneDataPoint);
812 oneDataPointText = new Q3CanvasText(canvas());
813 displayText = pDataPoint->m_Label;
814 oneDataPointText ->setText(displayText);
815 oneDataPointText ->move(XLocOfOneDataPoint,
816 YLocOfOneDataPoint);
817 showNewItem(oneDataPointText);
819 // Sleep for a short time : Is there any Qt sleep function ?
820 int dummy, j;
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++)
833 // {
834 // pData = &(*pVideo)[time];
835 // VideoFrame::Iterator iter = pData->begin();
836 // for (; iter != pData->end(); ++iter)
837 // {
838 // /////////////// this is where the main work is done: /////////////////
839 // computeProjectedLocForOneDataPoint(
840 // &iter.data(),
841 // pData,
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 // ///////////////////////////////////////////////////////////////////////
853 // }
854 // }
856 //release memory
857 for(i=0; i<m_numberOfDimension; i++)
859 delete [] anchorCoordinates[i];
862 delete []anchorCoordinates;
864 return;
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;
892 int CanvasCenterX;
893 int CanvasCenterY;
894 int XLocOfOneAnchor;
895 int YLocOfOneAnchor;
896 int radiusOfTheCircle;
897 int radiusOfOneAnchor = 20;
898 QRect RectOfCanvas;
899 Anchor* oneAnchor;
900 QString displayText;
901 int stateNumber;
902 double anchorDegree;
903 double** anchorCoordinates;
904 int i;
905 double XLocOfOneDataPoint,
906 YLocOfOneDataPoint;
907 Q3CanvasText* oneDataPointText;
909 if (Data->m_Dimensionality < 2)
911 QMessageBox::information ( NULL, "Warning", "We don't support a dimension less than 2!", "OK" );
912 return;
915 // TODO: put this back in (reformulated, though)
916 // if (Data->size() < 1)
917 // {
918 // QMessageBox::information ( NULL, "Warning", "Number Of data points is less than 1!", "OK" );
919 // return;
920 // }
924 // Get data parameters
925 m_numberOfDimension = Data->m_Dimensionality;
926 // m_numberOfDataPoints = Data->size();
928 // Clear qcanvas
929 Q3CanvasItemList list = canvas()->allItems();
930 Q3CanvasItemList::Iterator it = list.begin();
931 for (; it != list.end(); ++it)
933 if ( *it ) delete *it;
936 pendingItem = NULL;
937 activeItem = NULL;
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;
951 else
953 radiusOfTheCircle = Canvas_Width/2 - 5;
956 //JG:
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);
976 stateNumber = 0;
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;
985 stateNumber++;
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;
1001 stateNumber++;
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(
1012 pDataPoint,
1013 anchorCoordinates,
1014 XLocOfOneDataPoint,
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);
1026 //release memory
1027 for(i=0; i<m_numberOfDimension; i++)
1029 delete [] anchorCoordinates[i];
1032 delete []anchorCoordinates;
1034 return;
1038 //rewritten JG July 2006
1039 void GraphicView::computeProjectedLocForOneDataPoint( LabeledDataPoint* Data,
1040 double** anchorCoordinates,
1041 double& xCoordinate,
1042 double& yCoordinate)
1044 int i;
1045 double* weights;
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;
1060 xCoordinate = 0.0;
1061 yCoordinate = 0.0;
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
1070 delete []weights;
1073 //: will not be used:
1074 void GraphicView::computeProjectedLocForOneDataPoint(int numberOfStates,
1075 double** anchorCoordinates,
1076 double* highDimensionData,
1077 double& xCoordinate,
1078 double& yCoordinate)
1080 int i;
1081 double* weights;
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;
1096 xCoordinate = 0.0;
1097 yCoordinate = 0.0;
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
1106 delete []weights;
1109 /////////////////////////////
1110 //// Mouse event functions
1113 void GraphicView::contentsContextMenuEvent(QContextMenuEvent *event)
1115 if ( !m_IsStateView) return;
1117 Q3PopupMenu contextMenu(this);
1118 if (activeItem )
1121 if ( activeItem ->rtti() == DiagramBox::RTTI)
1123 contextMenu.insertSeparator();
1124 PropertiesAct->addTo(&contextMenu);
1125 contextMenu.insertSeparator();
1126 DisplayMeAct->addTo(&contextMenu);
1128 else
1130 contextMenu.insertSeparator();
1131 PropertiesAct->addTo(&contextMenu);
1135 else
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());
1154 if (items.empty())
1156 setActiveItem(0);
1158 else
1160 setActiveItem(*items.begin());
1167 void GraphicView::contentsMouseDoubleClickEvent(QMouseEvent *event)
1169 if ( !m_IsStateView) return;
1171 if (event->button() == Qt::LeftButton && activeItem)
1173 Properties();
1179 ///////////////////////////////////////
1180 //// Some other necessary functions
1182 void GraphicView::showNewItem(Q3CanvasItem *item)
1184 setActiveItem(item);
1185 item->show();
1186 canvas()->update();
1189 void GraphicView::setActiveItem(Q3CanvasItem *item)
1191 if (item != activeItem)
1193 if (activeItem) activeItem->setActive(false);
1195 activeItem = item;
1197 if (activeItem) activeItem->setActive(true);
1199 canvas()->update();
1205 //////////////////////////////////////////////
1206 /////////////////////////////////////////////
1207 /// Next are implementation of DiagramBox
1210 DiagramBox::DiagramBox(Q3Canvas *canvas)
1211 : Q3CanvasRectangle(canvas)
1213 setSize(100, 60);
1214 setPen(QPen(Qt::blue));
1215 setBrush(Qt::white);
1216 str = "Text";
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);
1225 str = "Text";
1226 m_MyState = MyState;
1230 DiagramBox::~DiagramBox()
1232 hide();
1235 void DiagramBox::setText(const QString &newText)
1237 str = newText;
1238 update();
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)
1258 setSize(100, 60);
1259 setPen(QPen(Qt::blue));
1260 setBrush(Qt::white);
1261 str = "Text";
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);
1270 if ( positive)
1272 if ( type == 1)
1274 setPen(QPen(Qt::blue));
1275 setBrush(Qt::green);
1277 else if ( type == 2)
1279 setPen(QPen(Qt::red));
1280 setBrush(Qt::blue);
1282 else
1284 setPen(QPen(Qt::white));
1285 setBrush(Qt::white);
1288 else
1290 setPen(QPen(Qt::black));
1291 setBrush(Qt::red);
1293 str = "Text";
1298 ProbBox::~ProbBox()
1300 hide();
1303 void ProbBox::setText(const QString &newText)
1305 str = newText;
1306 update();
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()
1342 hide();
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)
1358 setSize(60, 60);
1359 setPen(QPen(Qt::blue));
1360 setBrush(Qt::white);
1361 m_str = "Text";
1364 Anchor::Anchor(Q3Canvas *canvas, int Width, int Height)
1365 : Q3CanvasEllipse(canvas)
1367 setSize(Width, Height);
1368 setPen(QPen(Qt::white));
1369 setBrush(Qt::blue);
1370 m_str = "Text";
1375 Anchor::~Anchor()
1377 hide();
1380 void Anchor::setText(const QString &newText)
1382 m_str = newText;
1383 update();
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());