~ubuntu-branches/ubuntu/breezy/koffice/breezy

« back to all changes in this revision

Viewing changes to kivio/kiviopart/kiviosdk/kivio_py_stencil.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040509113300-vfrdadqsvjfuhn3b
Tags: 1:1.3.1-1
* New upstream bugfix release.
* Built against newer imagemagick (closes: #246623).
* Made koffice-libs/kformula recommend/depend on latex-xft-fonts, which
  provides mathematical fonts that the formula editor can use.  Also
  patched the kformula part to make these fonts the default.
* Changed kword menu hint from "WordProcessors" to "Word processors"
  (closes: #246209).
* Spellchecker configuration is now fixed (closes: #221256, #227568).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
#include "kivio_py_stencil.h"
 
3
#include "kivio_view.h"
3
4
 
4
5
KivioPage *page;
 
6
KivioView *view;
5
7
#ifdef HAVE_PYTHON
6
8
 
7
9
#include "kivio_common.h"
20
22
#include "kivio_py_stencil_spawner.h"
21
23
 
22
24
#include "kivio_page.h"
23
 
#include "kivio_view.h"
24
25
 
25
26
#include <qpainter.h>
26
27
#include <qbrush.h>
27
28
#include <qcolor.h>
28
29
#include <kdebug.h>
29
30
#include <math.h>
 
31
#include <kozoomhandler.h>
30
32
 
31
33
#include "py_kivio.h"
32
34
 
37
39
KivioPyStencil::KivioPyStencil()
38
40
    : KivioStencil()
39
41
{
40
 
   m_pConnectorTargets = new QList<KivioConnectorTarget>;
 
42
   m_pConnectorTargets = new QPtrList<KivioConnectorTarget>;
41
43
   m_pConnectorTargets->setAutoDelete(true);
42
44
 
43
45
   static bool first_time = true;
243
245
   // stuff from KivioSMLStensil
244
246
 
245
247
    KivioConnectorTarget *pTarget, *pOriginal;
246
 
        
247
 
    QList<KivioConnectorTarget> *pOriginalTargets = ((KivioPyStencilSpawner*)m_pSpawner)->targets();
 
248
 
 
249
    QPtrList<KivioConnectorTarget> *pOriginalTargets = ((KivioPyStencilSpawner*)m_pSpawner)->targets();
248
250
 
249
251
    pTarget = m_pConnectorTargets->first();
250
252
    pOriginal = pOriginalTargets->first();
256
258
    while( pTarget && pOriginal && i<size )
257
259
    {
258
260
        PyObject *target = PyList_GetItem( targets, i );
259
 
        float x = getDoubleFromDict( target,"x");
260
 
        float y = getDoubleFromDict( target,"y");
 
261
        double x = getDoubleFromDict( target,"x");
 
262
        double y = getDoubleFromDict( target,"y");
261
263
 
262
264
        pTarget  ->setPosition( x, y );
263
265
        pOriginal->setPosition( x, y );
401
403
 
402
404
void KivioPyStencil::paint( KivioIntraStencilData *d, bool outlined )
403
405
{
404
 
 
405
 
   float scale = d->scale;
406
 
 
407
 
   PyObject *shapes = PyDict_Values( PyDict_GetItemString( vars, "shapes" ) );
408
 
   if ( !shapes )
409
 
         return;
410
 
 
411
 
   int size = PyList_Size( shapes );
412
 
 
413
 
   for ( int i=0; i<size; i++ ) {
414
 
       PyObject *shape = PyList_GetItem( shapes, i );
415
 
       if ( !PyDict_Check(shape) )
416
 
         continue;
417
 
 
418
 
       int fill = KivioFillStyle::kcsNone;
419
 
 
420
 
       // if style dosn't defined for shape, applay default for stencil
421
 
       setStyle( d, PyDict_GetItemString( vars, "style" ) , fill );
422
 
       setStyle( d, shape, fill );
423
 
 
424
 
       if ( isSelected() )
425
 
         setStyle( d, PyDict_GetItemString( shape, "selected" ) , fill );
426
 
 
427
 
       if ( outlined )
428
 
          fill = KivioFillStyle::kcsNone;
429
 
 
430
 
       QString stype = getStringFromDict( shape, "type" );
431
 
       stype = stype.lower();
432
 
 
433
 
       double x = getDoubleFromDict(shape,"x")*scale;
434
 
       double y = getDoubleFromDict(shape,"y")*scale;
435
 
       double w = getDoubleFromDict(shape,"w")*scale;
436
 
       double h = getDoubleFromDict(shape,"h")*scale;
437
 
       double x2 = getDoubleFromDict(shape,"x2")*scale;
438
 
       double y2 = getDoubleFromDict(shape,"y2")*scale;
439
 
 
440
 
       // get points list
441
 
       QList<KivioPoint> points;
442
 
       points.setAutoDelete(true);
443
 
       PyObject *pyPoints = PyDict_GetItemString( shape, "points" );
444
 
       if ( pyPoints && PyList_Check(pyPoints) ) {
445
 
          int size = PyList_Size(pyPoints);
446
 
          for ( int i=0; i<size; i++ ) {
447
 
             PyObject *pyPoint = PyList_GetItem(pyPoints,i);
448
 
             double x = getDoubleFromDict(pyPoint,"x")*scale;
449
 
             double y = getDoubleFromDict(pyPoint,"y")*scale;
450
 
             points.append( new KivioPoint( x, y, KivioPoint::kptNormal ) );
451
 
          }
452
 
       }
453
 
 
454
 
 
455
 
       if ( stype == "rectangle" ) {
456
 
          if (fill)
457
 
            d->painter->fillRect( x, y, w, h );
458
 
          else
459
 
            d->painter->drawRect( x, y, w, h );
460
 
       }
461
 
 
462
 
       if ( stype == "textbox" ) {
463
 
          int tf = vTextAlign() | hTextAlign();
464
 
 
465
 
          QFont f = textFont();
466
 
          f.setPointSize( f.pointSize() * scale );
467
 
 
468
 
          d->painter->setFont( f );
469
 
          QString text = getStringFromDict(shape,"text");
470
 
          if ( !text.isEmpty() )
471
 
            d->painter->drawText( x, y, w, h, tf | Qt::WordBreak, text );
472
 
       }
473
 
 
474
 
       if ( stype == "arc" ) {
475
 
          double a1 = getDoubleFromDict(shape,"a1");
476
 
          double a2 = getDoubleFromDict(shape,"a2");
477
 
          d->painter->drawArc(x,y,w,h,a1,a2);
478
 
       }
479
 
 
480
 
       if ( stype == "roundrect" ) {
481
 
          double rx = getDoubleFromDict(shape,"rx")*scale;
482
 
          double ry = getDoubleFromDict(shape,"ry")*scale;
483
 
          if (fill)
484
 
            d->painter->fillRoundRect( x, y, w, h, rx, ry );
485
 
          else
486
 
            d->painter->drawRoundRect( x, y, w, h, rx, ry );
487
 
       }
488
 
 
489
 
       if ( stype == "linearray" ) {
490
 
          d->painter->drawLineArray(&points);
491
 
       }
492
 
 
493
 
       if ( stype == "ellipse" ) {
494
 
          if (fill)
495
 
            d->painter->fillEllipse( x, y, w, h );
496
 
          else
497
 
            d->painter->drawEllipse( x, y, w, h );
498
 
       }
499
 
   }
500
 
 
501
 
    KivioConnectorTarget *pTarget = m_pConnectorTargets->first();
502
 
    while( pTarget )
503
 
    {
504
 
        pTarget ->paintOutline( d );
505
 
        pTarget = m_pConnectorTargets->next();
506
 
    }
 
406
  KoZoomHandler* zoomHandler = d->zoomHandler;
 
407
 
 
408
  PyObject *shapes = PyDict_Values( PyDict_GetItemString( vars, "shapes" ) );
 
409
 
 
410
  if ( !shapes ) {
 
411
    return;
 
412
  }
 
413
 
 
414
  int size = PyList_Size( shapes );
 
415
 
 
416
  for ( int i=0; i<size; i++ ) {
 
417
    PyObject *shape = PyList_GetItem( shapes, i );
 
418
    if ( !PyDict_Check(shape) )
 
419
      continue;
 
420
 
 
421
    int fill = KivioFillStyle::kcsNone;
 
422
 
 
423
    // if style dosn't defined for shape, applay default for stencil
 
424
    setStyle( d, PyDict_GetItemString( vars, "style" ) , fill );
 
425
    setStyle( d, shape, fill );
 
426
 
 
427
    if ( isSelected() )
 
428
      setStyle( d, PyDict_GetItemString( shape, "selected" ) , fill );
 
429
 
 
430
    if ( outlined )
 
431
        fill = KivioFillStyle::kcsNone;
 
432
 
 
433
    QString stype = getStringFromDict( shape, "type" );
 
434
    stype = stype.lower();
 
435
 
 
436
    double x = zoomHandler->zoomItX(getDoubleFromDict(shape,"x"));
 
437
    double y = zoomHandler->zoomItY(getDoubleFromDict(shape,"y"));
 
438
    double w = zoomHandler->zoomItX(getDoubleFromDict(shape,"w"));
 
439
    double h = zoomHandler->zoomItY(getDoubleFromDict(shape,"h"));
 
440
    double x2 = zoomHandler->zoomItX(getDoubleFromDict(shape,"x2"));
 
441
    double y2 = zoomHandler->zoomItY(getDoubleFromDict(shape,"y2"));
 
442
 
 
443
    // get points list
 
444
    QPtrList<KivioPoint> points;
 
445
    points.setAutoDelete(true);
 
446
    PyObject *pyPoints = PyDict_GetItemString( shape, "points" );
 
447
    if ( pyPoints && PyList_Check(pyPoints) ) {
 
448
      int size = PyList_Size(pyPoints);
 
449
      for ( int i=0; i<size; i++ ) {
 
450
        PyObject *pyPoint = PyList_GetItem(pyPoints,i);
 
451
        double x = zoomHandler->zoomItX(getDoubleFromDict(pyPoint,"x"));
 
452
        double y = zoomHandler->zoomItY(getDoubleFromDict(pyPoint,"y"));
 
453
        points.append( new KivioPoint( x, y, KivioPoint::kptNormal ) );
 
454
      }
 
455
    }
 
456
 
 
457
 
 
458
    if ( stype == "rectangle" ) {
 
459
      if (fill)
 
460
        d->painter->fillRect( x, y, w, h );
 
461
      else
 
462
        d->painter->drawRect( x, y, w, h );
 
463
    }
 
464
 
 
465
    if ( stype == "textbox" ) {
 
466
      int tf = vTextAlign() | hTextAlign();
 
467
 
 
468
      QFont f = textFont();
 
469
      f.setPointSizeFloat(f.pointSizeFloat() * (((float)zoomHandler->zoom()) / 100.0));
 
470
 
 
471
      d->painter->setFont( f );
 
472
      QString text = getStringFromDict(shape,"text");
 
473
 
 
474
      if ( !text.isEmpty() ) {
 
475
        d->painter->drawText( x, y, w, h, tf | Qt::WordBreak, text );
 
476
      }
 
477
    }
 
478
 
 
479
    if ( stype == "arc" ) {
 
480
      double a1 = getDoubleFromDict(shape,"a1");
 
481
      double a2 = getDoubleFromDict(shape,"a2");
 
482
      d->painter->drawArc(x,y,w,h,a1,a2);
 
483
    }
 
484
 
 
485
    if ( stype == "roundrect" ) {
 
486
      double rx = zoomHandler->zoomItX(getDoubleFromDict(shape,"rx"));
 
487
      double ry = zoomHandler->zoomItY(getDoubleFromDict(shape,"ry"));
 
488
 
 
489
      if (fill) {
 
490
        d->painter->fillRoundRect( x, y, w, h, rx, ry );
 
491
      } else {
 
492
        d->painter->drawRoundRect( x, y, w, h, rx, ry );
 
493
      }
 
494
    }
 
495
 
 
496
    if ( stype == "linearray" ) {
 
497
      d->painter->drawLineArray(&points);
 
498
    }
 
499
 
 
500
    if ( stype == "ellipse" ) {
 
501
      if (fill) {
 
502
        d->painter->fillEllipse( x, y, w, h );
 
503
      } else {
 
504
        d->painter->drawEllipse( x, y, w, h );
 
505
      }
 
506
    }
 
507
 
 
508
    if(stype == "polygon") {
 
509
      d->painter->drawPolygon(&points);
 
510
    }
 
511
 
 
512
    if(stype == "polyline") {
 
513
      d->painter->drawPolyline(&points);
 
514
    }
 
515
  }
 
516
 
 
517
  KivioConnectorTarget *pTarget = m_pConnectorTargets->first();
 
518
 
 
519
  while( pTarget )
 
520
  {
 
521
    pTarget->paintOutline( d );
 
522
    pTarget = m_pConnectorTargets->next();
 
523
  }
507
524
}
508
525
 
509
526
 
511
528
int KivioPyStencil::runPython(QString code)
512
529
{
513
530
 
514
 
    KivioView * view = dynamic_cast<KivioView*>(KoDocument::documentList()->first()->views().getFirst());
 
531
    view = dynamic_cast<KivioView*>(KoDocument::documentList()->first()->views().getFirst());
515
532
    if ( view ) {
516
533
        page = view->activePage();
517
534
    }
564
581
}
565
582
 
566
583
 
567
 
KivioCollisionType KivioPyStencil::checkForCollision( KivioPoint *pPoint, float )
 
584
KivioCollisionType KivioPyStencil::checkForCollision( KivioPoint *pPoint, double )
568
585
{
569
 
    float px = pPoint->x();
570
 
    float py = pPoint->y();
 
586
    double px = pPoint->x();
 
587
    double py = pPoint->y();
571
588
 
572
589
    if( !(px < m_x + m_w &&
573
590
         px >= m_x &&
609
626
 */
610
627
void KivioPyStencil::paintConnectorTargets( KivioIntraStencilData *pData )
611
628
{
612
 
    QPixmap *targetPic;
613
 
    KivioPainter *painter;
614
 
    float x, y;
615
 
 
616
 
    // We don't draw these if we are selected!!!
617
 
    if( isSelected() )
618
 
      return;
619
 
 
620
 
    // Obtain the graphic used for KivioConnectorTargets
621
 
    targetPic = KivioConfig::config()->connectorTargetPixmap();
622
 
 
623
 
 
624
 
    float _scale = pData->scale;
625
 
    painter = pData->painter;
626
 
 
627
 
    KivioConnectorTarget *pTarget;
628
 
    pTarget = m_pConnectorTargets->first();
629
 
    while( pTarget )
630
 
    {
631
 
        x = pTarget->x() * _scale;
632
 
        y = pTarget->y() * _scale;
633
 
 
634
 
        painter->drawPixmap( x-3, y-3, *targetPic );
635
 
 
636
 
        pTarget = m_pConnectorTargets->next();
637
 
    }
 
629
  QPixmap *targetPic;
 
630
  KivioPainter *painter;
 
631
  double x, y;
 
632
 
 
633
  // We don't draw these if we are selected!!!
 
634
  if( isSelected() )
 
635
    return;
 
636
 
 
637
  // Obtain the graphic used for KivioConnectorTargets
 
638
  targetPic = KivioConfig::config()->connectorTargetPixmap();
 
639
 
 
640
 
 
641
  KoZoomHandler* zoomHandler = pData->zoomHandler;
 
642
  painter = pData->painter;
 
643
 
 
644
  KivioConnectorTarget *pTarget;
 
645
  pTarget = m_pConnectorTargets->first();
 
646
  while( pTarget )
 
647
  {
 
648
    x = zoomHandler->zoomItX(pTarget->x());
 
649
    y = zoomHandler->zoomItY(pTarget->y());
 
650
 
 
651
    painter->drawPixmap( x-3, y-3, *targetPic );
 
652
 
 
653
    pTarget = m_pConnectorTargets->next();
 
654
  }
638
655
}
639
656
 
640
657
/**
641
658
 * Attempts to connect a KivioConnectorPoint to this stencil.
642
659
 *
643
660
 * This function will attempt to locate a KivioConnectorTarget in this
644
 
 * stencil with-in a given threshhold.  If it finds it, it will connect
 
661
 * stencil with-in a given threshold.  If it finds it, it will connect
645
662
 * the point to it, and return the target of the connection.
646
663
 */
647
 
KivioConnectorTarget *KivioPyStencil::connectToTarget( KivioConnectorPoint *p, float threshHold )
 
664
KivioConnectorTarget *KivioPyStencil::connectToTarget( KivioConnectorPoint *p, double threshHold )
648
665
{
649
 
    float px = p->x();
650
 
    float py = p->y();
 
666
    double px = p->x();
 
667
    double py = p->y();
651
668
 
652
 
    float tx, ty;
 
669
    double tx, ty;
653
670
 
654
671
    KivioConnectorTarget *pTarget = m_pConnectorTargets->first();
655
672
    while( pTarget )
726
743
 
727
744
void KivioPyStencil::setStyle( KivioIntraStencilData *d, PyObject *s, int &fillStyle )
728
745
{
729
 
    if ( !s )
730
 
        return;
731
 
 
732
 
    if ( !PyDict_Check(s) )
733
 
        return;
734
 
 
735
 
    KivioPainter *p = d->painter;
736
 
    double scale = d->scale;
737
 
 
738
 
 
739
 
    PyObject *color = PyDict_GetItemString(s,"color");
740
 
    if ( color ) {
741
 
        QColor c = readColor(color);
742
 
        if ( c.isValid() )
743
 
            p->setFGColor(c);
744
 
    }
745
 
 
746
 
    color = PyDict_GetItemString(s,"bgcolor");
747
 
    if ( color ) {
748
 
        QColor c = readColor(color);
749
 
        if ( c.isValid() )
750
 
            p->setBGColor(c);
751
 
    }
752
 
 
753
 
    color = PyDict_GetItemString(s,"textcolor");
754
 
    if ( color ) {
755
 
        QColor c = readColor(color);
756
 
        if ( c.isValid() )
757
 
            p->setTextColor(c);
758
 
    }
759
 
 
760
 
    PyObject *lineWidth = PyDict_GetItemString(s,"linewidth");
761
 
    if ( lineWidth ) {
762
 
        float lw = getDoubleFromDict(s,"linewidth");
763
 
        p->setLineWidth( lw*scale );
764
 
    }
765
 
 
766
 
 
767
 
    PyObject *o_fillStyle = PyDict_GetItemString(s,"fillstyle");
768
 
    if ( o_fillStyle ) {
769
 
        QString sfill = getStringFromDict(s,"fillstyle");
770
 
        if ( sfill == "solid" )
771
 
            fillStyle = KivioFillStyle::kcsSolid;
772
 
 
773
 
        if ( sfill == "none" )
774
 
            fillStyle = KivioFillStyle::kcsNone;
775
 
    }
776
 
 
777
 
    QString  sfont = getStringFromDict(s,"font");
778
 
    int      fontSize = int(getDoubleFromDict(s,"fontsize")*scale);
779
 
 
780
 
    if ( !sfont.isEmpty() ) {
781
 
        if ( !fontSize )
782
 
            fontSize = int( 12*scale );
783
 
        p->setFont( QFont( sfont, fontSize ) );
784
 
    }
785
 
    else {
786
 
        if ( fontSize )
787
 
            p->setFont( QFont( "times", fontSize ) );
788
 
    }
789
 
 
 
746
  if ( !s )
 
747
    return;
 
748
 
 
749
  if ( !PyDict_Check(s) )
 
750
    return;
 
751
 
 
752
  KivioPainter *p = d->painter;
 
753
  KoZoomHandler* zoomHandler = d->zoomHandler;
 
754
 
 
755
  PyObject *color = PyDict_GetItemString(s,"color");
 
756
 
 
757
  if ( color ) {
 
758
    QColor c = readColor(color);
 
759
 
 
760
    if ( c.isValid() ) {
 
761
      p->setFGColor(c);
 
762
    }
 
763
  }
 
764
 
 
765
  color = PyDict_GetItemString(s,"bgcolor");
 
766
 
 
767
  if ( color ) {
 
768
    QColor c = readColor(color);
 
769
 
 
770
    if ( c.isValid() ) {
 
771
      p->setBGColor(c);
 
772
    }
 
773
  }
 
774
 
 
775
  color = PyDict_GetItemString(s,"textcolor");
 
776
 
 
777
  if ( color ) {
 
778
    QColor c = readColor(color);
 
779
 
 
780
    if ( c.isValid() ) {
 
781
      p->setTextColor(c);
 
782
    }
 
783
  }
 
784
 
 
785
  PyObject *lineWidth = PyDict_GetItemString(s,"linewidth");
 
786
 
 
787
  if ( lineWidth ) {
 
788
    double lw = getDoubleFromDict(s,"linewidth");
 
789
    p->setLineWidth( zoomHandler->zoomItY(lw) );
 
790
  }
 
791
 
 
792
  PyObject *o_fillStyle = PyDict_GetItemString(s,"fillstyle");
 
793
 
 
794
  if ( o_fillStyle ) {
 
795
    QString sfill = getStringFromDict(s,"fillstyle");
 
796
 
 
797
    if ( sfill == "solid" ) {
 
798
      fillStyle = KivioFillStyle::kcsSolid;
 
799
    }
 
800
 
 
801
    if ( sfill == "none" ) {
 
802
      fillStyle = KivioFillStyle::kcsNone;
 
803
    }
 
804
  }
 
805
 
 
806
  QString  sfont = getStringFromDict(s,"font");
 
807
  QFont f;
 
808
  int fontSize = (int)getDoubleFromDict(s,"fontsize");
 
809
 
 
810
  if(!fontSize) {
 
811
    fontSize = 12; // FIXME: Should use some kind of global setting!!!
 
812
  }
 
813
 
 
814
  f.setPointSize(fontSize);
 
815
  f.setPointSizeFloat(f.pointSizeFloat() * (((float)zoomHandler->zoom()) / 100.0));
 
816
 
 
817
  if ( !sfont.isEmpty() ) {
 
818
    f.setFamily(sfont);
 
819
  } else {
 
820
    f.setFamily("times"); // FIXME: Should use some kind of global setting!!!
 
821
  }
 
822
 
 
823
  p->setFont(f);
790
824
}
791
825
 
792
826
QColor KivioPyStencil::readColor( PyObject *color )
872
906
}
873
907
 
874
908
 
875
 
float KivioPyStencil::lineWidth()
 
909
double KivioPyStencil::lineWidth()
876
910
{
877
911
    PyObject *lw = PyDict_GetItemString( PyDict_GetItemString(vars,"style"), "linewidth" );
878
912
    if ( lw )
881
915
    return 1.0;
882
916
}
883
917
 
884
 
void KivioPyStencil::setLineWidth( float w )
 
918
void KivioPyStencil::setLineWidth( double w )
885
919
{
886
920
    PyDict_SetItemString(  PyDict_GetItemString(vars,"style") , "linewidth"  , Py_BuildValue("f",w ) ) ;
887
921
}
906
940
 
907
941
void KivioPyStencil::setTextFont( const QFont &f )
908
942
{
909
 
    float fs = f.pointSizeFloat();
 
943
    double fs = f.pointSizeFloat();
910
944
    QString family = f.family();
911
945
 
912
946
    int bold     = f.bold();