kivio_py_stencil.cpp
1
2#include "kivio_py_stencil.h"
3#include "kivio_view.h"
4
5KivioPage *page;
6KivioView *view;
7#ifdef HAVE_PYTHON
8
9#include "kivioglobal.h"
10#include "kivio_common.h"
11#include "kivio_connector_point.h"
12#include "kivio_connector_target.h"
13#include "kivio_fill_style.h"
14#include "kivio_intra_stencil_data.h"
15#include "kivio_line_style.h"
16#include "kivio_painter.h"
17#include "kivio_point.h"
18#include "kivio_screen_painter.h"
19#include "kivio_stencil_spawner.h"
20#include "kivio_stencil_spawner_info.h"
21#include "kivio_stencil_spawner_set.h"
22
23#include "kivio_py_stencil_spawner.h"
24
25#include "kivio_page.h"
26
27#include <tqpainter.h>
28#include <tqbrush.h>
29#include <tqcolor.h>
30#include <kdebug.h>
31#include <math.h>
32#include <KoZoomHandler.h>
33
34#include "py_kivio.h"
35
36extern "C" {
37 void initkivioc(void);
38}
39
40KivioPyStencil::KivioPyStencil()
41 : KivioStencil()
42{
43 m_pConnectorTargets = new TQPtrList<KivioConnectorTarget>;
44 m_pConnectorTargets->setAutoDelete(true);
45
46 static bool first_time = true;
47 if ( first_time ) {
48 Py_Initialize(); // initialize python only once
49 //kdDebug(43000) << "init kivioc" << endl;
50 initkivioc();
51 first_time = false;
52 }
53
54 PyObject* mainmod = PyImport_AddModule("__main__");
55 globals = PyModule_GetDict(mainmod);
56
57 m_x = old_x = 0.0;
58 m_y = old_y = 0.0;
59 m_w = old_w = 72.0;
60 m_h = old_h = 72.0;
61 double x2 = m_x+m_w;
62 double y2 = m_y+m_h;
63
64
65 vars = Py_BuildValue( "{s:d,s:d,s:d,s:d,s:d,s:d,s:{},s:[],s:[],s:{}}",
66 "x", m_x, "y", m_y, "w", m_w, "h", m_h, "x2", x2, "y2", y2 , "style","connectors","connector_targets","shapes");
67
68 resizeCode = "";
69}
70
71
72KivioPyStencil::~KivioPyStencil()
73{
74}
75
76
77int KivioPyStencil::init( TQString initCode )
78{
79 runPython(kivio_module);
80 if ( !runPython( initCode ) )
81 return 1;
82
83 m_w = getDoubleFromDict( vars, "w");
84 m_h = getDoubleFromDict( vars, "h");
85 m_x = getDoubleFromDict( vars, "x");
86 m_y = getDoubleFromDict( vars, "y");
87
88 old_x = m_x;
89 old_y = m_y;
90 old_w = m_w;
91 old_h = m_h;
92 return 1;
93}
94
95
96bool KivioPyStencil::loadXML( const TQDomElement &e )
97{
98 TQDomNode node;
99 TQDomElement ele;
100
101
102 node = e.firstChild();
103 while( !node.isNull() )
104 {
105 TQString nodeName = node.nodeName();
106
107 ele = node.toElement();
108
109 if( nodeName == "PyData" )
110 {
111 resizeCode = XmlReadString( ele, "resizeCode", "" );
112
113 TQString sVars = XmlReadString( ele, "vars", "" );
114
115 PyObject* mainmod = PyImport_AddModule("__main__");
116 PyObject* gdic = PyModule_GetDict(mainmod);
117 PyObject *ldic = Py_BuildValue("{s:s,s:{}}", "ldic", sVars.latin1() ,"res");
118
119 if ( !PyRun_String("import pickle\nres = pickle.loads(ldic)", Py_file_input, gdic, ldic) ) {
120 PyErr_Print();
121 return false;
122 }
123
124 vars = PyDict_GetItemString( ldic, "res" );
125 Py_INCREF(vars);
126 runPython(kivio_module);
127
128 m_w = getDoubleFromDict( vars, "w");
129 m_h = getDoubleFromDict( vars, "h");
130 m_x = getDoubleFromDict( vars, "x");
131 m_y = getDoubleFromDict( vars, "y");
132
133 old_x = m_x;
134 old_y = m_y;
135 old_w = m_w;
136 old_h = m_h;
137
138 }
139 else if( nodeName == "KivioConnectorTargetList" )
140 {
141 loadConnectorTargetListXML( ele );
142 }
143
144 node = node.nextSibling();
145 }
146 return true;
147}
148
149
153void KivioPyStencil::loadConnectorTargetListXML( const TQDomElement &e )
154{
155 TQDomNode node;
156 TQDomElement ele;
157 TQString nodeName;
158 KivioConnectorTarget *pTarget;
159
160 pTarget = m_pConnectorTargets->first();
161 node = e.firstChild();
162 while( !node.isNull() && pTarget)
163 {
164 nodeName = node.nodeName();
165 ele = node.toElement();
166
167 if( nodeName == "KivioConnectorTarget" )
168 {
169 pTarget->loadXML( ele );
170 }
171
172 pTarget = m_pConnectorTargets->next();
173 node = node.nextSibling();
174 }
175}
176
177
178TQDomElement KivioPyStencil::saveXML( TQDomDocument &doc )
179{
180 TQDomElement e = doc.createElement("KivioPyStencil");
181
182 XmlWriteString( e, "id", m_pSpawner->info()->id() );
183 XmlWriteString( e, "setId", m_pSpawner->set()->id() );
184
185
186 TQDomElement dE = doc.createElement("PyData");
187
188 // The python variables
189 PyObject* mainmod = PyImport_AddModule("__main__");
190 PyObject* gdic = PyModule_GetDict(mainmod);
191 PyObject *ldic = Py_BuildValue("{s:O,s:{}}", "ldic", vars ,"res");
192
193 const char *dump_code = "import copy\n"\
194 "import pickle\n"\
195 "cres = {}\n"\
196 "for key in ldic.keys():\n"\
197 " try:\n"\
198 " cres[key] = copy.deepcopy(ldic[key])\n"\
199 " except:\n"\
200 " ii=0\n"\
201 "res = pickle.dumps(cres)\n";
202
203 if ( !PyRun_String(dump_code, Py_file_input, gdic, ldic) )
204 PyErr_Print();
205
206 TQString sVars = PyBytes_AS_STRING( PyDict_GetItemString( ldic, "res") );
207
208 XmlWriteString( dE, "vars", sVars );
209 XmlWriteString( dE, "resizeCode", resizeCode );
210
211 e.appendChild( dE );
212
213
214 // Save the target list
215 TQDomElement clE = doc.createElement("KivioConnectorTargetList");
216 TQDomElement targetE;
217 KivioConnectorTarget *pTarget = m_pConnectorTargets->first();
218 while( pTarget )
219 {
220 targetE = pTarget->saveXML( doc );
221 clE.appendChild( targetE );
222
223 pTarget = m_pConnectorTargets->next();
224 }
225 e.appendChild( clE );
226
227 return e;
228
229}
230
231void KivioPyStencil::updateGeometry()
232{
233
234 //rescale items :
235
236 rescaleShapes(vars);
237
238 old_x = m_x;
239 old_y = m_y;
240 old_w = m_w;
241 old_h = m_h;
242
243 if ( !resizeCode.isEmpty() )
244 runPython(resizeCode);
245
246 // stuff from KivioSMLStensil
247
248 KivioConnectorTarget *pTarget, *pOriginal;
249
250 TQPtrList<KivioConnectorTarget> *pOriginalTargets = ((KivioPyStencilSpawner*)m_pSpawner)->targets();
251
252 pTarget = m_pConnectorTargets->first();
253 pOriginal = pOriginalTargets->first();
254
255 PyObject *targets = PyDict_GetItemString(vars,"connector_targets");
256 int size = PyList_Size( targets );
257 int i=0;
258
259 while( pTarget && pOriginal && i<size )
260 {
261 PyObject *target = PyList_GetItem( targets, i );
262 double x = getDoubleFromDict( target,"x");
263 double y = getDoubleFromDict( target,"y");
264
265 pTarget ->setPosition( x, y );
266 pOriginal->setPosition( x, y );
267
268 pTarget = m_pConnectorTargets->next();
269 pOriginal = pOriginalTargets->next();
270 i++;
271 }
272
273}
274
275void KivioPyStencil::rescaleShapes( PyObject *o )
276{
277 if ( PyDict_Check(o) ) {
278
279 PyObject *o_x = PyDict_GetItemString(o,"x");
280 if ( o_x ) {
281 double x = getDoubleFromDict(o,"x");
282 x = m_x+(x-old_x)*m_w/old_w;
283 PyDict_SetItemString( o, "x", Py_BuildValue( "d", x ) );
284 }
285
286 PyObject *o_y = PyDict_GetItemString(o,"y");
287 if ( o_y ) {
288 double y = getDoubleFromDict(o,"y");
289 y = m_y+(y-old_y)*m_h/old_h;
290 PyDict_SetItemString( o, "y", Py_BuildValue( "d", y ) );
291 }
292
293 PyObject *o_x2 = PyDict_GetItemString(o,"x2");
294 if ( o_x2 ) {
295 double x = getDoubleFromDict(o,"x2");
296 x = m_x+(x-old_x)*m_w/old_w;
297 PyDict_SetItemString( o, "x2", Py_BuildValue( "d", x ) );
298 }
299
300 //PyObject *o_y2 = PyDict_GetItemString(o,"y2");
301 if ( o_y ) {
302 double y = getDoubleFromDict(o,"y2");
303 y = m_y+(y-old_y)*m_h/old_h;
304 PyDict_SetItemString( o, "y2", Py_BuildValue( "d", y ) );
305 }
306
307 PyObject *o_w = PyDict_GetItemString(o,"w");
308 if ( o_w ) {
309 double w = getDoubleFromDict(o,"w");
310 w = w*m_w/old_w;
311 PyDict_SetItemString( o, "w", Py_BuildValue( "d", w ) );
312 }
313
314 PyObject *o_h = PyDict_GetItemString(o,"h");
315 if ( o_h ) {
316 double h = getDoubleFromDict(o,"h");
317 h = h*m_h/old_h;
318 PyDict_SetItemString( o, "h", Py_BuildValue( "d", h ) );
319 }
320
321 PyObject *childs = PyDict_Values( o );
322 int size = PyList_Size( childs );
323 for ( int i=0; i<size; i++ )
324 rescaleShapes(PyList_GetItem(childs,i) );
325
326 } else
327 if ( PyList_Check(o) ) {
328
329 int size = PyList_Size( o );
330 for ( int i=0; i<size; i++ )
331 rescaleShapes( PyList_GetItem(o,i) );
332 }
333}
334
341KivioStencil *KivioPyStencil::duplicate()
342{
343 KivioPyStencil *pNewStencil = new KivioPyStencil();
344 KivioStencil *pReturn;
345
346 pNewStencil->m_x = m_x;
347 pNewStencil->m_y = m_y;
348 pNewStencil->m_w = m_w;
349 pNewStencil->m_h = m_h;
350
351 pNewStencil->old_x = old_x;
352 pNewStencil->old_y = old_y;
353 pNewStencil->old_w = old_w;
354 pNewStencil->old_h = old_h;
355
356 pNewStencil->m_pSpawner = m_pSpawner;
357 pNewStencil->resizeCode = resizeCode;
358
359 //make deep copy of vars:
360 PyObject* mainmod = PyImport_AddModule("__main__");
361 PyObject* gdic = PyModule_GetDict(mainmod);
362 PyObject *ldic = Py_BuildValue("{s:O,s:{}}", "ldic", vars ,"res");
363
364 const char *copy_code = "import copy\n"\
365 "for key in ldic.keys():\n"\
366 " try:\n"\
367 " res[key] = copy.deepcopy(ldic[key])\n"\
368 " except:\n"\
369 " i=0\n";
370 if ( !PyRun_String(copy_code, Py_file_input, gdic, ldic) )
371 PyErr_Print();
372
373 pNewStencil->vars = PyDict_GetItemString( ldic, "res");
374 pNewStencil->runPython(kivio_module);
375
376 // Copy the Connector Targets
377 KivioConnectorTarget *pTarget = m_pConnectorTargets->first();
378
379 while( pTarget )
380 {
381 pNewStencil->m_pConnectorTargets->append( pTarget->duplicate() );
382 pTarget = m_pConnectorTargets->next();
383 }
384
385 *(pNewStencil->protection()) = *m_pProtection;
386 *(pNewStencil->canProtect()) = *m_pCanProtect;
387
388 pReturn = pNewStencil;
389 return pReturn;
390}
391
392
393
394void KivioPyStencil::paint( KivioIntraStencilData *d )
395{
396 paint(d,false);
397}
398
399void KivioPyStencil::paintOutline( KivioIntraStencilData *d )
400{
401 paint(d,true);
402}
403
404
405void KivioPyStencil::paint( KivioIntraStencilData *d, bool outlined )
406{
407 KoZoomHandler* zoomHandler = d->zoomHandler;
408
409 PyObject *shapes = PyDict_Values( PyDict_GetItemString( vars, "shapes" ) );
410
411 if ( !shapes ) {
412 return;
413 }
414
415 int size = PyList_Size( shapes );
416
417 for ( int i=0; i<size; i++ ) {
418 PyObject *shape = PyList_GetItem( shapes, i );
419 if ( !PyDict_Check(shape) )
420 continue;
421
422 int fill = KivioFillStyle::kcsNone;
423
424 // if style dosn't defined for shape, applay default for stencil
425 setStyle( d, PyDict_GetItemString( vars, "style" ) , fill );
426 setStyle( d, shape, fill );
427
428 if ( isSelected() )
429 setStyle( d, PyDict_GetItemString( shape, "selected" ) , fill );
430
431 if ( outlined )
432 fill = KivioFillStyle::kcsNone;
433
434 TQString stype = getStringFromDict( shape, "type" );
435 stype = stype.lower();
436
437 double x = zoomHandler->zoomItX(getDoubleFromDict(shape,"x"));
438 double y = zoomHandler->zoomItY(getDoubleFromDict(shape,"y"));
439 double w = zoomHandler->zoomItX(getDoubleFromDict(shape,"w"));
440 double h = zoomHandler->zoomItY(getDoubleFromDict(shape,"h"));
441 //double x2 = zoomHandler->zoomItX(getDoubleFromDict(shape,"x2"));
442 //double y2 = zoomHandler->zoomItY(getDoubleFromDict(shape,"y2"));
443
444 // get points list
445 TQPtrList<KivioPoint> points;
446 points.setAutoDelete(true);
447 PyObject *pyPoints = PyDict_GetItemString( shape, "points" );
448 if ( pyPoints && PyList_Check(pyPoints) ) {
449 int size = PyList_Size(pyPoints);
450 for ( int i=0; i<size; i++ ) {
451 PyObject *pyPoint = PyList_GetItem(pyPoints,i);
452 double x = zoomHandler->zoomItX(getDoubleFromDict(pyPoint,"x"));
453 double y = zoomHandler->zoomItY(getDoubleFromDict(pyPoint,"y"));
454 points.append( new KivioPoint( x, y, KivioPoint::kptNormal ) );
455 }
456 }
457
458
459 if ( stype == "rectangle" ) {
460 if (fill)
461 d->painter->fillRect( x, y, w, h );
462 else
463 d->painter->drawRect( x, y, w, h );
464 }
465
466 if ( stype == "textbox" ) {
467 int tf = vTextAlign() | hTextAlign();
468
469 TQFont f = textFont();
470 f.setPointSizeFloat(f.pointSizeFloat() * (((float)zoomHandler->zoom()) / 100.0));
471
472 d->painter->setFont( f );
473 TQString text = getStringFromDict(shape,"text");
474
475 if ( !text.isEmpty() ) {
476 d->painter->drawText( int( x ), int( y ), int( w ), int( h ), tf | TQt::WordBreak, text );
477 }
478 }
479
480 if ( stype == "arc" ) {
481 double a1 = getDoubleFromDict(shape,"a1");
482 double a2 = getDoubleFromDict(shape,"a2");
483 d->painter->drawArc(x,y,w,h,a1,a2);
484 }
485
486 if ( stype == "roundrect" ) {
487 double rx = zoomHandler->zoomItX(getDoubleFromDict(shape,"rx"));
488 double ry = zoomHandler->zoomItY(getDoubleFromDict(shape,"ry"));
489
490 if (fill) {
491 d->painter->fillRoundRect( x, y, w, h, rx, ry );
492 } else {
493 d->painter->drawRoundRect( x, y, w, h, rx, ry );
494 }
495 }
496
497 if ( stype == "linearray" ) {
498 d->painter->drawLineArray(&points);
499 }
500
501 if ( stype == "ellipse" ) {
502 if (fill) {
503 d->painter->fillEllipse( x, y, w, h );
504 } else {
505 d->painter->drawEllipse( x, y, w, h );
506 }
507 }
508
509 if(stype == "polygon") {
510 d->painter->drawPolygon(&points);
511 }
512
513 if(stype == "polyline") {
514 d->painter->drawPolyline(&points);
515 }
516 }
517
518 KivioConnectorTarget *pTarget = m_pConnectorTargets->first();
519
520 while( pTarget )
521 {
522 pTarget->paintOutline( d );
523 pTarget = m_pConnectorTargets->next();
524 }
525}
526
527
528
529int KivioPyStencil::runPython(TQString code)
530{
531
532 view = dynamic_cast<KivioView*>(KoDocument::documentList()->first()->views().getFirst());
533 if ( view ) {
534 page = view->activePage();
535 }
536
537 //const char *ccode = code.local8Bit().data();
538 const char *ccode = code.latin1();
539
540 //kdDebug(43000) << "code to run:" << endl << ccode << endl;
541
542 PyObject *v = PyRun_String( const_cast<char*>(ccode) , Py_file_input, globals, vars );
543
544 if (v == NULL) {
545 PyErr_Print();
546 return 0;
547 }
548
549 PyObject *f = PySys_GetObject("stdout");
550 int ret = 0;
551 if (f)
552 {
553 ret = PyFile_WriteString("\n", f);
554 }
555 if (ret)
556 PyErr_Clear();
557
558 Py_DECREF(v);
559 return 1;
560}
561
562
563double KivioPyStencil::getDoubleFromDict( PyObject *dict, const char *key )
564{
565 if (!PyDict_Check(dict)) return 0.0;
566
567 PyObject *val = PyDict_GetItemString(dict,const_cast<char*>(key));
568 if ( val ) {
569 if ( PyFloat_Check(val) )
570 return PyFloat_AsDouble( val );
571
572 if ( PyLong_Check(val) )
573 return PyLong_AsLong( val );
574
575 if ( PyLong_Check(val) )
576 return PyLong_AsDouble( val );
577 }
578 return 0.0;
579}
580
581TQString KivioPyStencil::getStringFromDict( PyObject *dict, const char *key )
582{
583 PyObject *val = PyDict_GetItemString(dict,const_cast<char*>(key));
584 if ( val && PyBytes_Check(val) )
585 return TQString( PyBytes_AS_STRING(val) );
586
587 return TQString("");
588}
589
590
591KivioCollisionType KivioPyStencil::checkForCollision( KoPoint *pPoint, double )
592{
593 double px = pPoint->x();
594 double py = pPoint->y();
595
596 if( !(px < m_x + m_w &&
597 px >= m_x &&
598 py < m_y + m_h &&
599 py >= m_y ) )
600 {
601 return kctNone;
602 }
603
604 return kctBody;
605
606}
607
608
612int KivioPyStencil::resizeHandlePositions()
613{
614 // Calculate the resize handle positions
615 int mask = KIVIO_RESIZE_HANDLE_POSITION_ALL;
616
617 if( m_pProtection->at( kpWidth ) )
618 {
619 mask &= ~(krhpNE | krhpNW | krhpSW | krhpSE | krhpE | krhpW);
620 }
621
622 if( m_pProtection->at( kpHeight) )
623 {
624 mask &= ~(krhpNE | krhpNW | krhpSW | krhpSE | krhpN | krhpS);
625 }
626
627 return mask;
628}
629
630
634void KivioPyStencil::paintConnectorTargets( KivioIntraStencilData *pData )
635{
636 TQPixmap targetPic;
637 KivioPainter *painter;
638 double x, y;
639
640 // We don't draw these if we are selected!!!
641 if( isSelected() )
642 return;
643
644 // Obtain the graphic used for KivioConnectorTargets
645 targetPic = Kivio::connectorTargetPixmap();
646
647
648 KoZoomHandler* zoomHandler = pData->zoomHandler;
649 painter = pData->painter;
650
651 KivioConnectorTarget *pTarget;
652 pTarget = m_pConnectorTargets->first();
653 while( pTarget )
654 {
655 x = zoomHandler->zoomItX(pTarget->x());
656 y = zoomHandler->zoomItY(pTarget->y());
657
658 painter->drawPixmap( x-3, y-3, targetPic );
659
660 pTarget = m_pConnectorTargets->next();
661 }
662}
663
671KivioConnectorTarget *KivioPyStencil::connectToTarget( KivioConnectorPoint *p, double threshHold )
672{
673 double px = p->x();
674 double py = p->y();
675
676 double tx, ty;
677
678 KivioConnectorTarget *pTarget = m_pConnectorTargets->first();
679 while( pTarget )
680 {
681 tx = pTarget->x();
682 ty = pTarget->y();
683
684
685 if( px >= tx - threshHold &&
686 px <= tx + threshHold &&
687 py >= ty - threshHold &&
688 py <= ty + threshHold )
689 {
690 // setTarget calls pTarget->addConnectorPoint() and removes
691 // any previous connections from p
692 p->setTarget( pTarget );
693 return pTarget;
694 }
695
696 pTarget = m_pConnectorTargets->next();
697 }
698
699 return NULL;
700}
701
702KivioConnectorTarget *KivioPyStencil::connectToTarget( KivioConnectorPoint *p, int /*targetID*/ )
703{
704 int id = p->targetId();
705
706 KivioConnectorTarget *pTarget = m_pConnectorTargets->first();
707 while( pTarget )
708 {
709 if( pTarget->id() == id )
710 {
711 p->setTarget(pTarget);
712
713 return pTarget;
714 }
715
716 pTarget = m_pConnectorTargets->next();
717 }
718
719 return NULL;
720}
721
722int KivioPyStencil::generateIds( int nextAvailable )
723{
724 KivioConnectorTarget *pTarget = m_pConnectorTargets->first();
725
726 // Iterate through all the targets
727 while( pTarget )
728 {
729 // If this target has something connected to it
730 if( pTarget->hasConnections() )
731 {
732 // Set it's id to the next available id
733 pTarget->setId( nextAvailable );
734
735 // Increment the next available id
736 nextAvailable++;
737 }
738 else
739 {
740 // Otherwise mark it as unused (-1)
741 pTarget->setId( -1 );
742 }
743
744 pTarget = m_pConnectorTargets->next();
745 }
746
747 // Return the next availabe id
748 return nextAvailable;
749}
750
751void KivioPyStencil::setStyle( KivioIntraStencilData *d, PyObject *s, int &fillStyle )
752{
753 if ( !s )
754 return;
755
756 if ( !PyDict_Check(s) )
757 return;
758
759 KivioPainter *p = d->painter;
760 KoZoomHandler* zoomHandler = d->zoomHandler;
761
762 PyObject *color = PyDict_GetItemString(s,"color");
763
764 if ( color ) {
765 TQColor c = readColor(color);
766
767 if ( c.isValid() ) {
768 p->setFGColor(c);
769 }
770 }
771
772 color = PyDict_GetItemString(s,"bgcolor");
773
774 if ( color ) {
775 TQColor c = readColor(color);
776
777 if ( c.isValid() ) {
778 p->setBGColor(c);
779 }
780 }
781
782 color = PyDict_GetItemString(s,"textcolor");
783
784 if ( color ) {
785 TQColor c = readColor(color);
786
787 if ( c.isValid() ) {
788 p->setTextColor(c);
789 }
790 }
791
792 PyObject *lineWidth = PyDict_GetItemString(s,"linewidth");
793
794 if ( lineWidth ) {
795 double lw = getDoubleFromDict(s,"linewidth");
796 p->setLineWidth( zoomHandler->zoomItY(lw) );
797 }
798
799 PyObject *o_fillStyle = PyDict_GetItemString(s,"fillstyle");
800
801 if ( o_fillStyle ) {
802 TQString sfill = getStringFromDict(s,"fillstyle");
803
804 if ( sfill == "solid" ) {
805 fillStyle = KivioFillStyle::kcsSolid;
806 }
807
808 if ( sfill == "none" ) {
809 fillStyle = KivioFillStyle::kcsNone;
810 }
811 }
812
813 TQString sfont = getStringFromDict(s,"font");
814 TQFont f;
815 int fontSize = (int)getDoubleFromDict(s,"fontsize");
816
817 if(!fontSize) {
818 fontSize = 12; // FIXME: Should use some kind of global setting!!!
819 }
820
821 f.setPointSize(fontSize);
822 f.setPointSizeFloat(f.pointSizeFloat() * (((float)zoomHandler->zoom()) / 100.0));
823
824 if ( !sfont.isEmpty() ) {
825 f.setFamily(sfont);
826 } else {
827 f.setFamily("times"); // FIXME: Should use some kind of global setting!!!
828 }
829
830 p->setFont(f);
831}
832
833TQColor KivioPyStencil::readColor( PyObject *color )
834{
835 if ( !color )
836 return TQColor();
837
838 if ( PyBytes_Check(color) ) {
839 return TQColor( PyBytes_AsString(color) );
840 }
841
842 if ( PyList_Check(color) ) {
843 if ( PyList_Size(color) == 3 ) {
844 PyObject *ro = PyList_GetItem(color,0);
845 PyObject *go = PyList_GetItem(color,1);
846 PyObject *bo = PyList_GetItem(color,2);
847
848 int r=0, g=0, b=0;
849 if ( PyNumber_Check(ro) )
850 r = PyLong_AsLong( PyNumber_Long(ro));
851 if ( PyNumber_Check(go) )
852 g = PyLong_AsLong( PyNumber_Long(go));
853 if ( PyNumber_Check(bo) )
854 b = PyLong_AsLong( PyNumber_Long(bo));
855
856 return TQColor(r,g,b);
857
858 }
859 }
860
861 return TQColor();
862
863}
864
865void KivioPyStencil::PyDebug( PyObject * o )
866{
867 kdDebug(43000) << "py_debug: " << PyBytes_AsString(PyObject_Str(o)) << endl;
868}
869
870
871TQColor KivioPyStencil::fgColor()
872{
873 TQColor color = readColor( PyDict_GetItemString( PyDict_GetItemString(vars,"style"), "color" ) );
874 if ( color.isValid() )
875 return color;
876 else
877 return TQColor(0,0,0);
878}
879
880void KivioPyStencil::setFGColor( TQColor c )
881{
882 PyDict_SetItemString( PyDict_GetItemString(vars,"style") , "color" , Py_BuildValue("[i,i,i]", c.red(), c.green(), c.blue() ) ) ;
883}
884
885
886TQColor KivioPyStencil::bgColor()
887{
888 TQColor color = readColor( PyDict_GetItemString( PyDict_GetItemString(vars,"style"), "bgcolor" ) );
889 if ( color.isValid() )
890 return color;
891 else
892 return TQColor(0,0,0);
893}
894
895void KivioPyStencil::setBGColor( TQColor c )
896{
897 PyDict_SetItemString( PyDict_GetItemString(vars,"style") , "bgcolor" , Py_BuildValue("[i,i,i]", c.red(), c.green(), c.blue() ) ) ;
898}
899
900
901TQColor KivioPyStencil::textColor()
902{
903 TQColor color = readColor( PyDict_GetItemString( PyDict_GetItemString(vars,"style"), "textcolor" ) );
904 if ( color.isValid() )
905 return color;
906 else
907 return TQColor(0,0,0);
908}
909
910void KivioPyStencil::setTextColor( TQColor c )
911{
912 PyDict_SetItemString( PyDict_GetItemString(vars,"style") , "textcolor" , Py_BuildValue("[i,i,i]", c.red(), c.green(), c.blue() ) ) ;
913}
914
915
916double KivioPyStencil::lineWidth()
917{
918 PyObject *lw = PyDict_GetItemString( PyDict_GetItemString(vars,"style"), "linewidth" );
919 if ( lw )
920 if ( PyNumber_Check(lw) )
921 return ( PyLong_AsLong( PyNumber_Long(lw)) );
922 return 1.0;
923}
924
925void KivioPyStencil::setLineWidth( double w )
926{
927 PyDict_SetItemString( PyDict_GetItemString(vars,"style") , "linewidth" , Py_BuildValue("f",w ) ) ;
928}
929
930
931void KivioPyStencil::setText( const TQString &s )
932{
933 PyObject *to = PyDict_GetItemString( PyDict_GetItemString(vars,"shapes"), "text" );
934 if ( to )
935 PyDict_SetItemString(to, "text", Py_BuildValue("s", s.latin1() ));
936}
937
938
939TQString KivioPyStencil::text()
940{
941 PyObject *to = PyDict_GetItemString( PyDict_GetItemString(vars,"shapes"), "text" );
942 if ( to ) {
943 return getStringFromDict(to, "text");
944 }
945 return TQString("");
946}
947
948void KivioPyStencil::setTextFont( const TQFont &f )
949{
950 double fs = f.pointSizeFloat();
951 TQString family = f.family();
952
953 int bold = f.bold();
954 int italic = f.italic();
955 int underline= f.underline();
956
957 PyDict_SetItemString( PyDict_GetItemString(vars,"style") , "fontsize" , Py_BuildValue("f",fs ) ) ;
958 PyDict_SetItemString( PyDict_GetItemString(vars,"style") , "font" , Py_BuildValue("s",family.latin1() ) ) ;
959 PyDict_SetItemString( PyDict_GetItemString(vars,"style") , "bold" , Py_BuildValue("i",bold ) ) ;
960 PyDict_SetItemString( PyDict_GetItemString(vars,"style") , "italic" , Py_BuildValue("i",italic ) ) ;
961 PyDict_SetItemString( PyDict_GetItemString(vars,"style") , "underline" , Py_BuildValue("i",underline ) ) ;
962}
963
964TQFont KivioPyStencil::textFont()
965{
966 PyObject *fn = PyDict_GetItemString( PyDict_GetItemString(vars,"style"), "font" );
967 PyObject *fs = PyDict_GetItemString( PyDict_GetItemString(vars,"style"), "fontsize" );
968 PyObject *bd = PyDict_GetItemString( PyDict_GetItemString(vars,"style"), "bold" );
969 PyObject *it = PyDict_GetItemString( PyDict_GetItemString(vars,"style"), "italic" );
970 PyObject *ul = PyDict_GetItemString( PyDict_GetItemString(vars,"style"), "underline" );
971
972 TQFont f;
973
974 if ( fs )
975 if ( PyNumber_Check(fs))
976 f.setPointSize( PyLong_AsLong( PyNumber_Long(fs)));
977
978 if ( bd )
979 if ( PyNumber_Check(bd))
980 f.setBold( PyLong_AsLong( PyNumber_Long(bd)));
981
982 if ( it )
983 if ( PyNumber_Check(it))
984 f.setItalic( PyLong_AsLong( PyNumber_Long(it)));
985
986 if ( ul )
987 if ( PyNumber_Check(ul))
988 f.setUnderline( PyLong_AsLong( PyNumber_Long(ul)));
989
990 if ( fn )
991 if ( PyBytes_Check(fn))
992 f.setFamily( PyBytes_AsString(fn));
993
994 return f;
995}
996
997int KivioPyStencil::hTextAlign()
998{
999 PyObject *hta = PyDict_GetItemString( PyDict_GetItemString(vars,"style"), "htextalign" );
1000
1001 if ( hta )
1002 if ( PyNumber_Check(hta) )
1003 return ( PyLong_AsLong( PyNumber_Long(hta)));
1004
1005 return TQt::AlignHCenter;
1006}
1007
1008int KivioPyStencil::vTextAlign()
1009{
1010 PyObject *vta = PyDict_GetItemString( PyDict_GetItemString(vars,"style"), "vtextalign" );
1011
1012 if ( vta )
1013 if ( PyNumber_Check(vta) )
1014 return ( PyLong_AsLong( PyNumber_Long(vta)));
1015
1016 return TQt::AlignVCenter;
1017}
1018
1019void KivioPyStencil::setHTextAlign(int hta)
1020{
1021 PyDict_SetItemString( PyDict_GetItemString(vars,"style") , "htextalign" , Py_BuildValue("i",hta));
1022}
1023
1024void KivioPyStencil::setVTextAlign(int vta)
1025{
1026 PyDict_SetItemString( PyDict_GetItemString(vars,"style") , "vtextalign" , Py_BuildValue("i",vta));
1027}
1028
1029#endif // HAVE_PYTHON
1030