00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kivio_group_stencil.h"
00021 #include "kivio_intra_stencil_data.h"
00022 #include "kivio_layer.h"
00023
00024 #include <kdebug.h>
00025
00026 #include <KoGlobal.h>
00027
00028 KivioGroupStencil::KivioGroupStencil()
00029 : KivioStencil(),
00030 m_pGroupList(NULL)
00031 {
00032 m_pGroupList = new QPtrList<KivioStencil>;
00033 m_pGroupList->setAutoDelete(true);
00034
00035 m_x = m_y = 1000000000000.0f;
00036 m_w = m_h = -10000000000.0f;
00037 setType(kstGroup);
00038 }
00039
00040 KivioGroupStencil::~KivioGroupStencil()
00041 {
00042 if( m_pGroupList )
00043 {
00044 delete m_pGroupList;
00045 m_pGroupList = NULL;
00046 }
00047 }
00048
00049
00050 void KivioGroupStencil::paint( KivioIntraStencilData *pData )
00051 {
00052
00053 KivioStencil *pStencil = m_pGroupList->first();
00054 while( pStencil )
00055 {
00056 pStencil->paint(pData);
00057
00058 pStencil = m_pGroupList->next();
00059 }
00060 }
00061
00062 void KivioGroupStencil::paintOutline( KivioIntraStencilData *pData )
00063 {
00064
00065 KivioStencil *pStencil = m_pGroupList->first();
00066 while( pStencil )
00067 {
00068 pStencil->paintOutline(pData);
00069
00070 pStencil = m_pGroupList->next();
00071 }
00072 }
00073
00074 void KivioGroupStencil::paintConnectorTargets( KivioIntraStencilData *pData )
00075 {
00076
00077 KivioStencil *pStencil = m_pGroupList->first();
00078 while( pStencil )
00079 {
00080 pStencil->paintConnectorTargets(pData);
00081
00082 pStencil = m_pGroupList->next();
00083 }
00084 }
00085
00086 void KivioGroupStencil::setFGColor( QColor c )
00087 {
00088
00089 KivioStencil *pStencil = m_pGroupList->first();
00090 while( pStencil )
00091 {
00092 pStencil->setFGColor(c);
00093
00094 pStencil = m_pGroupList->next();
00095 }
00096 }
00097
00098 void KivioGroupStencil::setBGColor( QColor c )
00099 {
00100
00101 KivioStencil *pStencil = m_pGroupList->first();
00102 while( pStencil )
00103 {
00104 pStencil->setBGColor(c);
00105
00106 pStencil = m_pGroupList->next();
00107 }
00108 }
00109
00110 void KivioGroupStencil::setLineWidth( double f )
00111 {
00112
00113 KivioStencil *pStencil = m_pGroupList->first();
00114 while( pStencil )
00115 {
00116 pStencil->setLineWidth(f);
00117
00118 pStencil = m_pGroupList->next();
00119 }
00120 }
00121
00122 KivioCollisionType KivioGroupStencil::checkForCollision( KoPoint *p, double threshold )
00123 {
00124 KivioCollisionType colType;
00125
00126
00127 KivioStencil *pStencil = m_pGroupList->last();
00128 while( pStencil )
00129 {
00130 colType = pStencil->checkForCollision( p, threshold );
00131 if( colType != kctNone && pStencil->type()!=kstConnector) {
00132 return colType;
00133 }
00134
00135 pStencil = m_pGroupList->prev();
00136 }
00137
00138 return kctNone;
00139 }
00140
00141 void KivioGroupStencil::addToGroup( KivioStencil *pStencil )
00142 {
00143 double left, right, top, bottom;
00144
00145
00146 m_pGroupList->append(pStencil);
00147
00148
00149 if( m_pGroupList->count() == 1 )
00150 {
00151 m_x = pStencil->x();
00152 m_y = pStencil->y();
00153 m_w = pStencil->w();
00154 m_h = pStencil->h();
00155 }
00156 else
00157 {
00158 right = pStencil->x() + pStencil->w();
00159 left = pStencil->x();
00160 top = pStencil->y();
00161 bottom = pStencil->y() + pStencil->h();
00162
00163
00164 if( left < m_x )
00165 {
00166 m_w = m_w + (m_x - left);
00167 m_x = left;
00168 }
00169 if( right > m_x + m_w )
00170 {
00171 m_w = right - m_x;
00172 }
00173 if( top < m_y )
00174 {
00175 m_h = m_h + (m_y - top);
00176 m_y = top;
00177 }
00178 if( bottom > m_y + m_h )
00179 {
00180 m_h = bottom - m_y;
00181 }
00182 }
00183 }
00184
00185 KivioStencil *KivioGroupStencil::duplicate()
00186 {
00187 KivioGroupStencil *pGroup;
00188 KivioStencil *pStencil;
00189
00190 pGroup = new KivioGroupStencil();
00191
00192 pStencil = m_pGroupList->first();
00193 while( pStencil )
00194 {
00195 pGroup->addToGroup( pStencil->duplicate() );
00196
00197 pStencil = m_pGroupList->next();
00198 }
00199
00200 *(pGroup->protection()) = *m_pProtection;
00201
00202 return pGroup;
00203 }
00204
00205 bool KivioGroupStencil::loadXML( const QDomElement &e, KivioLayer *pLayer )
00206 {
00207 QDomNode node;
00208 KivioStencil *pStencil;
00209
00210 node = e.firstChild();
00211 while( !node.isNull() )
00212 {
00213 QString name = node.nodeName();
00214
00215 if( name == "KivioGroupStencil" )
00216 {
00217 pStencil = pLayer->loadGroupStencil( node.toElement() );
00218 if( pStencil )
00219 {
00220 addToGroup( pStencil );
00221 }
00222 else
00223 {
00224 kdDebug(43000) << "KivioGroupStencil::loadXML() - Error loading group stencil" << endl;
00225 }
00226 }
00227 else if( name == "KivioSMLStencil" || name == "KivioPyStencil" )
00228 {
00229 pStencil = pLayer->loadSMLStencil( node.toElement() );
00230 if( pStencil )
00231 {
00232 addToGroup( pStencil );
00233 }
00234 else
00235 {
00236 kdDebug(43000) << "KivioGroupStencil::loadXML() - Error loading group stencil" << endl;
00237 }
00238 }
00239 else if( name == "KivioPluginStencil" )
00240 {
00241 KivioStencil *pStencil = pLayer->loadPluginStencil( node.toElement() );
00242 if( pStencil )
00243 {
00244 addToGroup( pStencil );
00245 }
00246 else
00247 {
00248 kdDebug(43000) << "KivioGroupStencil::loadXML() - Error loading group stencil" << endl;
00249 }
00250 }
00251
00252 node = node.nextSibling();
00253 }
00254
00255 return true;
00256 }
00257
00258 QDomElement KivioGroupStencil::saveXML( QDomDocument &doc )
00259 {
00260 QDomElement e = doc.createElement("KivioGroupStencil");
00261
00262 QDomElement stencilE;
00263 KivioStencil *pStencil = m_pGroupList->first();
00264 while( pStencil )
00265 {
00266 stencilE = pStencil->saveXML( doc );
00267
00268 e.appendChild( stencilE );
00269
00270 pStencil = m_pGroupList->next();
00271 }
00272
00273 return e;
00274 }
00275
00276 void KivioGroupStencil::setX( double newX )
00277 {
00278 double dx = newX - m_x;
00279
00280 m_x = newX;
00281 KivioStencil *pStencil = m_pGroupList->first();
00282 while( pStencil )
00283 {
00284 if( pStencil->protection()->at(kpX)==false )
00285 {
00286 pStencil->setX( pStencil->x() + dx );
00287 }
00288
00289 pStencil = m_pGroupList->next();
00290 }
00291
00292 }
00293
00294 void KivioGroupStencil::setY( double newY )
00295 {
00296 double dy = newY - m_y;
00297
00298 m_y = newY;
00299 KivioStencil *pStencil = m_pGroupList->first();
00300 while( pStencil )
00301 {
00302 if( pStencil->protection()->at(kpY)==false )
00303 {
00304 pStencil->setY( pStencil->y() + dy );
00305 }
00306
00307 pStencil = m_pGroupList->next();
00308 }
00309
00310 }
00311
00312 void KivioGroupStencil::setPosition( double newX, double newY )
00313 {
00314 double dx = newX - m_x;
00315 double dy = newY - m_y;
00316
00317 double newX2, newY2;
00318
00319 m_x = newX;
00320 m_y = newY;
00321
00322 KivioStencil *pStencil = m_pGroupList->first();
00323 while( pStencil )
00324 {
00325 if(((pStencil->type() == kstConnector) && !pStencil->connected()) ||
00326 pStencil->type() != kstConnector)
00327 {
00328 if( pStencil->protection()->at(kpX)==false ) {
00329 newX2 = pStencil->x() + dx;
00330 } else {
00331 newX2 = pStencil->x();
00332 }
00333
00334 if( pStencil->protection()->at(kpY)==false ) {
00335 newY2 = pStencil->y() + dy;
00336 } else {
00337 newY2 = pStencil->y();
00338 }
00339
00340 pStencil->setPosition( newX2, newY2 );
00341 }
00342
00343 pStencil = m_pGroupList->next();
00344 }
00345
00346 }
00347
00348 void KivioGroupStencil::setW( double newW )
00349 {
00350 double percInc = newW / m_w;
00351
00352 if( newW > 0.0f ) {
00353 m_w = newW;
00354 }
00355
00356 KivioStencil *pStencil = m_pGroupList->first();
00357 while( pStencil )
00358 {
00359 if(((pStencil->type() == kstConnector) && !pStencil->connected()) ||
00360 pStencil->type() != kstConnector)
00361 {
00362 if( pStencil->protection()->at(kpX)==false ) {
00363 pStencil->setX( ((pStencil->x() - m_x) * percInc) + m_x );
00364 }
00365
00366 if( pStencil->protection()->at(kpWidth)==false ) {
00367 pStencil->setW( pStencil->w() * percInc );
00368 }
00369 }
00370
00371 pStencil = m_pGroupList->next();
00372 }
00373 }
00374
00375 void KivioGroupStencil::setH( double newH )
00376 {
00377 double percInc = newH / m_h;
00378
00379 if( newH > 0.0f ) {
00380 m_h = newH;
00381 }
00382
00383 KivioStencil *pStencil = m_pGroupList->first();
00384 while( pStencil )
00385 {
00386 if(((pStencil->type() == kstConnector) && !pStencil->connected()) ||
00387 pStencil->type() != kstConnector)
00388 {
00389 if( pStencil->protection()->at(kpY)==false ) {
00390 pStencil->setY( ((pStencil->y() - m_y) * percInc) + m_y );
00391 }
00392
00393 if( pStencil->protection()->at(kpHeight)==false ) {
00394 pStencil->setH( pStencil->h() * percInc );
00395 }
00396 }
00397
00398 pStencil = m_pGroupList->next();
00399 }
00400 }
00401
00402 void KivioGroupStencil::setDimensions( double newW, double newH )
00403 {
00404 double percIncX = newW / m_w;
00405 double percIncY = newH / m_h;
00406
00407 if( newW > 0.0f ) {
00408 m_w = newW;
00409 }
00410 if( newH > 0.0f ) {
00411 m_h = newH;
00412 }
00413
00414 KivioStencil *pStencil = m_pGroupList->first();
00415 while( pStencil )
00416 {
00417 if(((pStencil->type() == kstConnector) && !pStencil->connected()) ||
00418 pStencil->type() != kstConnector)
00419 {
00420 if( newW > 0.0f ) {
00421 if( pStencil->protection()->at(kpX)==false ) {
00422 pStencil->setX( ((pStencil->x() - m_x) * percIncX) + m_x );
00423 }
00424 if( pStencil->protection()->at(kpWidth)==false ) {
00425 pStencil->setW( pStencil->w() * percIncX );
00426 }
00427 }
00428
00429 if( newH > 0.0f ) {
00430 if( pStencil->protection()->at(kpY)==false ) {
00431 pStencil->setY( ((pStencil->y() - m_y) * percIncY) + m_y );
00432 }
00433 if( pStencil->protection()->at(kpHeight)==false ) {
00434 pStencil->setH( pStencil->h() * percIncY );
00435 }
00436 }
00437 }
00438
00439 pStencil = m_pGroupList->next();
00440 }
00441
00442 }
00443
00444 int KivioGroupStencil::generateIds( int next )
00445 {
00446 KivioStencil *pStencil = m_pGroupList->first();
00447
00448 while( pStencil )
00449 {
00450 next = pStencil->generateIds( next );
00451
00452 pStencil = m_pGroupList->next();
00453 }
00454
00455 return next;
00456 }
00457
00458
00459 KivioConnectorTarget *KivioGroupStencil::connectToTarget( KivioConnectorPoint *p, double thresh)
00460 {
00461 KivioConnectorTarget *pTarget;
00462
00463 KivioStencil *pStencil = m_pGroupList->first();
00464 while( pStencil )
00465 {
00466 pTarget = pStencil->connectToTarget( p, thresh );
00467 if( pTarget )
00468 return pTarget;
00469
00470 pStencil = m_pGroupList->next();
00471 }
00472
00473 return NULL;
00474 }
00475
00476 KivioConnectorTarget *KivioGroupStencil::connectToTarget( KivioConnectorPoint *p, int id )
00477 {
00478 KivioConnectorTarget *pTarget;
00479
00480 KivioStencil *pStencil = m_pGroupList->first();
00481 while( pStencil )
00482 {
00483 pTarget = pStencil->connectToTarget( p, id );
00484 if( pTarget )
00485 return pTarget;
00486
00487 pStencil = m_pGroupList->next();
00488 }
00489
00490 return NULL;
00491 }
00492
00493 void KivioGroupStencil::searchForConnections( KivioPage *p )
00494 {
00495 KivioStencil *pCur = 0;
00496 KivioStencil *pStencil = m_pGroupList->first();
00497
00498 while( pStencil )
00499 {
00500
00501 pCur = pStencil;
00502
00503 pStencil->searchForConnections( p );
00504
00505
00506 m_pGroupList->find( pCur );
00507
00508 pStencil = m_pGroupList->next();
00509 }
00510 }
00511
00512 void KivioGroupStencil::setTextColor( QColor c )
00513 {
00514 KivioStencil *pStencil = m_pGroupList->first();
00515 while( pStencil )
00516 {
00517 pStencil->setTextColor(c);
00518
00519 pStencil = m_pGroupList->next();
00520 }
00521 }
00522
00523 void KivioGroupStencil::setText( const QString &text )
00524 {
00525 KivioStencil *pStencil = m_pGroupList->first();
00526 while( pStencil )
00527 {
00528 pStencil->setText(text);
00529
00530 pStencil = m_pGroupList->next();
00531 }
00532 }
00533
00534 QString KivioGroupStencil::text()
00535 {
00536 KivioStencil *pStencil = m_pGroupList->first();
00537
00538 if( !pStencil )
00539 return QString("");
00540
00541 return pStencil->text();
00542 }
00543
00544 void KivioGroupStencil::setHTextAlign( int a )
00545 {
00546 KivioStencil *pStencil = m_pGroupList->first();
00547 while( pStencil )
00548 {
00549 pStencil->setHTextAlign(a);
00550
00551 pStencil = m_pGroupList->next();
00552 }
00553 }
00554
00555 int KivioGroupStencil::hTextAlign()
00556 {
00557 KivioStencil *pStencil = m_pGroupList->first();
00558
00559 if( !pStencil )
00560 return Qt::AlignHCenter;
00561
00562 return pStencil->hTextAlign();
00563 }
00564
00565 void KivioGroupStencil::setVTextAlign( int a )
00566 {
00567 KivioStencil *pStencil = m_pGroupList->first();
00568 while( pStencil )
00569 {
00570 pStencil->setVTextAlign(a);
00571
00572 pStencil = m_pGroupList->next();
00573 }
00574 }
00575
00576 int KivioGroupStencil::vTextAlign()
00577 {
00578 KivioStencil *pStencil = m_pGroupList->first();
00579
00580 if( !pStencil )
00581 return Qt::AlignVCenter;
00582
00583 return pStencil->vTextAlign();
00584 }
00585
00586 void KivioGroupStencil::setTextFont( const QFont &f )
00587 {
00588 KivioStencil *pStencil = m_pGroupList->first();
00589 while( pStencil )
00590 {
00591 pStencil->setTextFont(f);
00592
00593 pStencil = m_pGroupList->next();
00594 }
00595 }
00596
00597 QFont KivioGroupStencil::textFont()
00598 {
00599 KivioStencil *pStencil = m_pGroupList->first();
00600
00601 if( !pStencil )
00602 return KoGlobal::defaultFont();
00603
00604 return pStencil->textFont();
00605 }
00606
00607 QColor KivioGroupStencil::textColor()
00608 {
00609 KivioStencil *pStencil = m_pGroupList->first();
00610
00611 if( !pStencil )
00612 return QColor(0, 0, 0);
00613
00614 return pStencil->textColor();
00615 }
00616
00617 int KivioGroupStencil::resizeHandlePositions()
00618 {
00619 return KIVIO_RESIZE_HANDLE_POSITION_ALL;
00620 }
00621
00622 QString KivioGroupStencil::getTextBoxName(const KoPoint& p)
00623 {
00624 int id = checkForCollision(p);
00625
00626 if(id < 0) {
00627 return QString::null;
00628 }
00629
00630 KivioStencil* pStencil = m_pGroupList->at(id);
00631 QString name = QString::number(id) + "-" + pStencil->getTextBoxName(p);
00632 return name;
00633 }
00634
00635 void KivioGroupStencil::setText(const QString& text, const QString& name)
00636 {
00637 int id = name.section("-", 0, 0).toInt();
00638 QString n = name.section("-", 1);
00639
00640 m_pGroupList->at(id)->setText(text, n);
00641 }
00642
00643 QString KivioGroupStencil::text(const QString& name)
00644 {
00645 int id = name.section("-", 0, 0).toInt();
00646 QString n = name.section("-", 1);
00647
00648 return m_pGroupList->at(id)->text(n);
00649 }
00650
00651 int KivioGroupStencil::checkForCollision(const KoPoint& p)
00652 {
00653 KivioStencil *pStencil = m_pGroupList->first();
00654 KoPoint pos = p;
00655 int id = 0;
00656
00657 while(pStencil)
00658 {
00659 if(pStencil->checkForCollision(&pos, 4.0) != kctNone) {
00660 return id;
00661 }
00662
00663 pStencil = m_pGroupList->next();
00664 id++;
00665 }
00666
00667 return -1;
00668 }
00669
00670 bool KivioGroupStencil::hasTextBox() const
00671 {
00672 KivioStencil *pStencil = m_pGroupList->first();
00673
00674 while(pStencil)
00675 {
00676 if(pStencil->hasTextBox()) {
00677 return true;
00678 }
00679
00680 pStencil = m_pGroupList->next();
00681 }
00682
00683 return false;
00684 }
00685
00686 QColor KivioGroupStencil::textColor(const QString& textBoxName)
00687 {
00688 int id = textBoxName.section("-", 0, 0).toInt();
00689 QString n = textBoxName.section("-", 1);
00690
00691 return m_pGroupList->at(id)->textColor(n);
00692 }
00693
00694 void KivioGroupStencil::setTextColor(const QString& textBoxName, const QColor& color)
00695 {
00696 int id = textBoxName.section("-", 0, 0).toInt();
00697 QString n = textBoxName.section("-", 1);
00698
00699 m_pGroupList->at(id)->setTextColor(n, color);
00700 }
00701
00702 QFont KivioGroupStencil::textFont(const QString& textBoxName)
00703 {
00704 int id = textBoxName.section("-", 0, 0).toInt();
00705 QString n = textBoxName.section("-", 1);
00706
00707 return m_pGroupList->at(id)->textFont(n);
00708 }
00709
00710 void KivioGroupStencil::setTextFont(const QString& textBoxName, const QFont& font)
00711 {
00712 int id = textBoxName.section("-", 0, 0).toInt();
00713 QString n = textBoxName.section("-", 1);
00714
00715 m_pGroupList->at(id)->setTextFont(n, font);
00716 }
00717
00718 int KivioGroupStencil::hTextAlign(const QString& textBoxName)
00719 {
00720 int id = textBoxName.section("-", 0, 0).toInt();
00721 QString n = textBoxName.section("-", 1);
00722
00723 return m_pGroupList->at(id)->hTextAlign(n);
00724 }
00725
00726 int KivioGroupStencil::vTextAlign(const QString& textBoxName)
00727 {
00728 int id = textBoxName.section("-", 0, 0).toInt();
00729 QString n = textBoxName.section("-", 1);
00730
00731 return m_pGroupList->at(id)->vTextAlign(n);
00732 }
00733
00734 void KivioGroupStencil::setHTextAlign(const QString& textBoxName, int align)
00735 {
00736 int id = textBoxName.section("-", 0, 0).toInt();
00737 QString n = textBoxName.section("-", 1);
00738
00739 m_pGroupList->at(id)->setHTextAlign(n, align);
00740 }
00741
00742 void KivioGroupStencil::setVTextAlign(const QString& textBoxName, int align)
00743 {
00744 int id = textBoxName.section("-", 0, 0).toInt();
00745 QString n = textBoxName.section("-", 1);
00746
00747 m_pGroupList->at(id)->setVTextAlign(n, align);
00748 }