drawmodule.cpp 40 KB
Newer Older
Victor Yacovlev's avatar
Victor Yacovlev committed
1 2 3 4 5 6 7 8 9
/*
This file is generated, but you can safely change it
until you run "gen_actor_source.py" with "--project" flag.

Generated file is just a skeleton for module contents.
You should change it corresponding to functionality.
*/

// Self include
Denis Khachko's avatar
Denis Khachko committed
10
#include "widgets/draw_turtle_field.h"
Victor Yacovlev's avatar
Victor Yacovlev committed
11 12 13 14
#include "drawmodule.h"

// Kumir includes
#include "extensionsystem/kplugin.h"
Denis Khachko's avatar
Denis Khachko committed
15
#include "extensionsystem/pluginmanager.h"
Denis Khachko's avatar
Denis Khachko committed
16

Victor Yacovlev's avatar
Victor Yacovlev committed
17 18 19 20

// Qt includes
#include <QtCore>
#include <QtGui>
Denis Khachko's avatar
Denis Khachko committed
21

Victor Yacovlev's avatar
Victor Yacovlev committed
22 23

namespace ActorDraw {
Denis Khachko's avatar
Denis Khachko committed
24
#define NET_RESERVE 15
Denis Khachko's avatar
Denis Khachko committed
25 26
#define KUM_MULTI 50
    
Denis Khachko's avatar
Denis Khachko committed
27 28 29 30 31
    ExtensionSystem::SettingsPtr DrawModule::DrawSettings()
    {
        ExtensionSystem::PluginManager * pluginManager = ExtensionSystem::PluginManager::instance();
        ExtensionSystem::KPlugin * plugin = pluginManager->loadedPlugins("ActorDraw")[0];
        return pluginManager->settingsByObject(plugin);
Denis Khachko's avatar
Denis Khachko committed
32
    }//Get  settings
Denis Khachko's avatar
Denis Khachko committed
33
    
Denis Khachko's avatar
Denis Khachko committed
34
    static const qreal MAX_ZOOM = 1000000;
Denis Khachko's avatar
Denis Khachko committed
35
    
36
    
Denis Khachko's avatar
Denis Khachko committed
37
        
Denis Khachko's avatar
Denis Khachko committed
38 39 40 41
    
    
    
    
Denis Khachko's avatar
Denis Khachko committed
42
    QRectF DrawScene::getRect() //User lines bounding rect
Denis Khachko's avatar
Denis Khachko committed
43
    {
Denis Khachko's avatar
Denis Khachko committed
44
        QRectF boundRect=QRectF(QPointF(-5,-5),QPointF(5,5));//default rect
Denis Khachko's avatar
Denis Khachko committed
45 46 47
  
        for(int i=0;i<lines.count();i++)
        {
Denis Khachko's avatar
Denis Khachko committed
48 49
            qreal deb=0;
            if(lines.at(i)->line().dx()>=0)
Denis Khachko's avatar
Denis Khachko committed
50 51 52
            {
                if(lines.at(i)->line().p1().x()<boundRect.left() || (i==0))
                {
Denis Khachko's avatar
Denis Khachko committed
53
                    deb=lines.at(i)->line().p1().x();
Denis Khachko's avatar
Denis Khachko committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
                    boundRect.setLeft(lines.at(i)->line().p1().x()); 
                    
                }
                if(lines.at(i)->line().p2().x()>boundRect.right() || (i==0))
                {
                    boundRect.setRight(lines.at(i)->line().p2().x()); 
                    
                }
         
            }
            if(lines.at(i)->line().dx()<0)
            {
                if(lines.at(i)->line().p2().x()<boundRect.left() || (i==0))
                {
                    boundRect.setLeft(lines.at(i)->line().p2().x()); 
                } 
                if(lines.at(i)->line().p1().x()>boundRect.right() || (i==0))
                {
                    boundRect.setRight(lines.at(i)->line().p1().x()); 
                    
                }
             
            }
Denis Khachko's avatar
Denis Khachko committed
77
            if(lines.at(i)->line().dy()>=0)
Denis Khachko's avatar
Denis Khachko committed
78 79 80 81
            {
                qDebug()<<"Bott"<<boundRect.bottom()<<"Top:"<<boundRect.top()<<"line p1y:"<<lines.at(i)->line().p1().y()<<"line p2y:"<<lines.at(i)->line().p2().y();
                if(-lines.at(i)->line().p1().y()>boundRect.bottom() || (i==0))
                {
Denis Khachko's avatar
Denis Khachko committed
82
                    deb=-lines.at(i)->line().p1().y();
Denis Khachko's avatar
Denis Khachko committed
83 84 85 86 87 88 89 90 91 92 93
                    boundRect.setBottom(-lines.at(i)->line().p1().y()); 
                }
                if(lines.at(i)->line().p2().x()>boundRect.top() || (i==0))
                {
                    boundRect.setTop(-lines.at(i)->line().p2().y()); 
                    
                }
            }
            if(lines.at(i)->line().dy()<0)
            {
                qDebug()<<"Bott"<<boundRect.bottom()<<"Top:"<<boundRect.top()<<"line p1y:"<<lines.at(i)->line().p1().y()<<"line p2y:"<<lines.at(i)->line().p2().y();
Denis Khachko's avatar
Denis Khachko committed
94
                if(lines.at(i)->line().p2().y()<boundRect.top())
Denis Khachko's avatar
Denis Khachko committed
95
                {
Denis Khachko's avatar
Denis Khachko committed
96 97
                    boundRect.setTop(lines.at(i)->line().p2().y());
                    deb=-lines.at(i)->line().p2().y();
Denis Khachko's avatar
Denis Khachko committed
98
                } 
Denis Khachko's avatar
Denis Khachko committed
99
                if(lines.at(i)->line().p1().y()>boundRect.bottom() || (i==0))
Denis Khachko's avatar
Denis Khachko committed
100
                {
Denis Khachko's avatar
Denis Khachko committed
101 102
                    boundRect.setBottom(lines.at(i)->line().p1().y());
                    deb=lines.at(i)->line().p1().y();
Denis Khachko's avatar
Denis Khachko committed
103 104 105 106 107
                    
                }
            }
        }
        
Denis Khachko's avatar
Denis Khachko committed
108 109 110 111 112
        //if(boundRect.width()!=boundRect.height())
       // {
       //     qreal size=qMax(boundRect.width(),boundRect.width());
       //     boundRect.setWidth(size);
       //     boundRect.setHeight(size);
Denis Khachko's avatar
Denis Khachko committed
113
            
Denis Khachko's avatar
Denis Khachko committed
114
      //  }
Denis Khachko's avatar
Denis Khachko committed
115 116 117
        
        return(boundRect);
    };
Denis Khachko's avatar
Denis Khachko committed
118
    
Denis Khachko's avatar
Denis Khachko committed
119 120 121 122 123 124 125 126
    bool DrawScene::isUserLine(QGraphicsItem* obj)
    {
        for(int i=0;i<lines.count();i++)
        {
            if(lines.at(i)==obj)return true;
        }
        return false;
    };
Denis Khachko's avatar
Denis Khachko committed
127
    
Denis Khachko's avatar
Denis Khachko committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    bool DrawScene::isLineAt(const QPointF &pos,qreal radius)
    {
        QGraphicsEllipseItem * testCirc=addEllipse(QRectF(pos.x()-radius,pos.y()-radius,radius*2,radius*2));
        QList<QGraphicsItem *> colItems=collidingItems(testCirc);
        delete testCirc;
     
        for (int i=0;i<colItems.count();i++)
            {
             
             if(isUserLine(colItems.at(i)))
                return true;
                   
            }
        return false;
        
    };
Denis Khachko's avatar
Denis Khachko committed
144
    
Denis Khachko's avatar
Denis Khachko committed
145 146 147
    qreal DrawScene::drawText(const QString &Text,qreal widthChar,QPointF from,QColor color)
    {
        QFont font ( "Courier", 12);
Denis Khachko's avatar
Denis Khachko committed
148
        font.setPointSizeF(KUM_MULTI);
Denis Khachko's avatar
Denis Khachko committed
149
        QFontMetricsF fontMetric(font);
Denis Khachko's avatar
Denis Khachko committed
150 151 152 153 154 155 156 157
        qreal bs=fontMetric.boundingRect(Text).width();
        qreal ch=bs/Text.length();
        qreal psizeF=widthChar/bs;
       
        
   
        //  font.setPointSizeF(psizeF*inc);
          bs=fontMetric.boundingRect("OOOXX").width()/5;
Denis Khachko's avatar
Denis Khachko committed
158 159 160 161 162 163 164
        
       
        fontMetric=QFontMetricsF(font);
        qDebug()<<"Char Size:"<<fontMetric.boundingRect("OOOXX").width()/5000;
        
        
        texts.append(addSimpleText(Text,font));
Victor Yacovlev's avatar
Victor Yacovlev committed
165
//        texts.last()->scale(0.001,0.001);
Denis Khachko's avatar
Denis Khachko committed
166 167
        texts.last()->setScale(psizeF);
        texts.last()->setPos(from.x(), from.y()-(fontMetric.boundingRect(Text).height()*psizeF));
Denis Khachko's avatar
Denis Khachko committed
168
        texts.last()->setPen(QPen(color));
Denis Khachko's avatar
Denis Khachko committed
169
        texts.last()->setZValue(90);
Denis Khachko's avatar
Denis Khachko committed
170
        return widthChar;
Denis Khachko's avatar
Denis Khachko committed
171
    };
Denis Khachko's avatar
Denis Khachko committed
172
    void DrawScene::DestroyNet()
Denis Khachko's avatar
Denis Khachko committed
173
    {
Denis Khachko's avatar
Denis Khachko committed
174
      //  qDebug()<<Netlines.count();
Denis Khachko's avatar
Denis Khachko committed
175 176
        for ( int i = 0; i < Netlines.count(); i++)
        {
Denis Khachko's avatar
Denis Khachko committed
177
       
Denis Khachko's avatar
Denis Khachko committed
178 179
           delete Netlines[i];
        }
Denis Khachko's avatar
Denis Khachko committed
180
        Netlines.clear();
Denis Khachko's avatar
Denis Khachko committed
181 182
        
    }
183
    void DrawScene::drawOnlyAxis(double startx ,double endx,double starty,double endy,qreal aw)
Denis Khachko's avatar
Denis Khachko committed
184 185 186
    {
        
        QPen axisPen=QPen(DRAW->axisColor());
187 188
        axisPen.setWidthF(aw);
        axisPen.setCosmetic(true);
Denis Khachko's avatar
Denis Khachko committed
189 190 191 192 193 194 195 196 197 198 199 200
        Netlines.append(addLine(startx-qAbs(startx-endx),0  , endx+qAbs(startx-endx), 0 ));
        Netlines.last()->setPen(axisPen);
        Netlines.last()->setZValue(1);
        
        Netlines.append(addLine(0,starty-qAbs(starty-endy),0 , endy+qAbs(startx-endx) ));
        Netlines.last()->setPen(axisPen);
        Netlines.last()->setZValue(1);

    }
    
    
    
201
    void DrawScene::drawNet(double startx ,double endx,double starty,double endy,QColor color,double stepX,double stepY,bool net,qreal nw,qreal aw)
Denis Khachko's avatar
Denis Khachko committed
202 203 204 205
    {
        
        QPointF pos,tail;    
        QColor AxisColor=DRAW->axisColor();
206 207 208
        auto lp=QPen(color);
        lp.setWidthF(nw);
        lp.setCosmetic(true);
Denis Khachko's avatar
Denis Khachko committed
209 210 211 212
        
        DestroyNet();
        if(!net)
        {
213
            drawOnlyAxis(startx,endx,starty,endy,aw);
Denis Khachko's avatar
Denis Khachko committed
214 215
            return;
        }
Denis Khachko's avatar
Denis Khachko committed
216
        int lines=qRound(startx/stepX);
Denis Khachko's avatar
Denis Khachko committed
217
        startx=lines*stepX;
Denis Khachko's avatar
Denis Khachko committed
218
        double fx1=startx-NET_RESERVE*stepX,fx2,fy1,fy2; 
Denis Khachko's avatar
Denis Khachko committed
219

Denis Khachko's avatar
Denis Khachko committed
220
       // return;
Denis Khachko's avatar
Denis Khachko committed
221
        while (fx1 < endx+NET_RESERVE*stepX)
Denis Khachko's avatar
Denis Khachko committed
222
		{
Denis Khachko's avatar
Denis Khachko committed
223
			fx1 = fx1 + stepX;
Denis Khachko's avatar
Denis Khachko committed
224
			fx2 = fx1;
Denis Khachko's avatar
Denis Khachko committed
225 226
			fy1 = starty-NET_RESERVE*stepX;
			fy2 = endy+NET_RESERVE*stepX;
Denis Khachko's avatar
Denis Khachko committed
227 228 229
            
			Netlines.append(addLine(fx1, fy1 , fx2, fy2 ));
			Netlines.last()->setZValue(0.5);
230
			Netlines.last()->setPen(lp);
Denis Khachko's avatar
Denis Khachko committed
231
            if(fx1>0-1/DRAW->zoom() && fx1<0+1/DRAW->zoom())
Denis Khachko's avatar
Denis Khachko committed
232
            {
Denis Khachko's avatar
Denis Khachko committed
233
                QPen axisPen=QPen(AxisColor);
234 235
                axisPen.setWidthF(aw);
                axisPen.setCosmetic(true);
Denis Khachko's avatar
Denis Khachko committed
236
               // axisPen.setWidth(3/DRAW->zoom());
Denis Khachko's avatar
Denis Khachko committed
237 238
                Netlines.last()->setPen(axisPen); 
                Netlines.last()->setZValue(1);
239 240 241
              //  Netlines.append(addLine(fx1+1/DRAW->zoom(), fy1 , fx2+1/DRAW->zoom(), fy2 ));
              //  Netlines.last()->setZValue(1);
              //  Netlines.last()->setPen(axisPen);
Denis Khachko's avatar
Denis Khachko committed
242 243
            }
		}
Denis Khachko's avatar
Denis Khachko committed
244 245
      //  Netlines.append(addLine(-1, -1 , 1, 1 ));
       // Netlines.append(addLine(1, -1 , -1, 1 ));
Denis Khachko's avatar
Denis Khachko committed
246 247
        lines=starty/stepY;
        starty=lines*stepY;
Denis Khachko's avatar
Denis Khachko committed
248
        fy1 = starty-NET_RESERVE*stepY;
Denis Khachko's avatar
Denis Khachko committed
249
        
Denis Khachko's avatar
Denis Khachko committed
250
		while (fy1 < endy+NET_RESERVE*stepY)
Denis Khachko's avatar
Denis Khachko committed
251
		{
Denis Khachko's avatar
Denis Khachko committed
252
			fy1 = fy1 + stepY;
Denis Khachko's avatar
Denis Khachko committed
253
			fy2 = fy1;
Denis Khachko's avatar
Denis Khachko committed
254 255
			fx1 = startx-NET_RESERVE*stepY;
			fx2 = endx+NET_RESERVE*stepY;
Denis Khachko's avatar
Denis Khachko committed
256 257 258
            
            Netlines.append(addLine(fx1, fy1 , fx2, fy2 ));
			Netlines.last()->setZValue(0.5);
259
			Netlines.last()->setPen(lp);
Denis Khachko's avatar
Denis Khachko committed
260
            if(fy1>0-1/DRAW->zoom() && fy1<0+1/DRAW->zoom())
Denis Khachko's avatar
Denis Khachko committed
261
            {
Denis Khachko's avatar
Denis Khachko committed
262
                QPen axisPen=QPen(AxisColor);
263 264
                axisPen.setWidthF(aw);
                axisPen.setCosmetic(true);
Denis Khachko's avatar
Denis Khachko committed
265
                Netlines.last()->setPen(axisPen);
Denis Khachko's avatar
Denis Khachko committed
266
                Netlines.last()->setZValue(1);
267 268 269
               //Netlines.append(addLine(fx1, fy1+1/DRAW->zoom() , fx2, fy2+1/DRAW->zoom() ));
                //Netlines.last()->setZValue(1);
               // Netlines.last()->setPen(axisPen);
Denis Khachko's avatar
Denis Khachko committed
270 271
            }
            
Denis Khachko's avatar
Denis Khachko committed
272
		}
Denis Khachko's avatar
Denis Khachko committed
273
    }
Denis Khachko's avatar
Denis Khachko committed
274
    
Denis Khachko's avatar
Denis Khachko committed
275 276 277
int   DrawScene::loadFromFile(const QString& p_FileName)
    {
        QFileInfo fi(p_FileName);
Denis Khachko's avatar
Denis Khachko committed
278
        QString name = fi.fileName();                        
Denis Khachko's avatar
Denis Khachko committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
        QString Title = QString::fromUtf8("Чертежник - ") + name;
        double CurX,CurY;
      //  MV->setWindowTitle ( Title);
        qreal CurrentScale;
        
        QString tmp = "";
        char ctmp[200];
        QString l_String;
        QFile l_File(p_FileName);
        QColor CurColor;
        
        
        int NStrok;
        NStrok = 0;
        
        //	long l_Err;
        //int CurX,CurY;
        //	int SizeX, SizeY;
        qreal x1,y1,x2,y2;
        if  (!l_File.open(QIODevice::ReadOnly))
        {
            QMessageBox::information( NULL, "", QString::fromUtf8("Ошибка открытия файла"), 0,0,0);
            return 1;
        }
        
        //l_String = l_File.readLine();
        //QMessageBox::information( 0, "", tmp, 0,0,0);
        QByteArray ttt;
        ttt = l_File.readLine();
        l_String = QString::fromUtf8(ttt);
        
        //QMessageBox::information( MV, "",l_String , 0,0,0);
        if (l_String.isNull())
        {
            l_File.close();
            QMessageBox::information( NULL, "", QString::fromUtf8("Ошибка чтения строки"), 0,0,0);
            return 1;
        }
        l_String = l_String.simplified();
        QStringList l_List = l_String.split(' ');
        
        
        if (!(l_String == "%!PS-Adobe-1.0 EPSF-1.0"))
        {
            l_File.close();
            QMessageBox::information(NULL, "",QString::fromUtf8("Это не PS - файл") , 0,0,0);
            
            return 1;
        }
        
        // Вторая строка %%Creator: Cherteznik
        //	l_String = l_File.readLine();
        ttt = l_File.readLine();
        l_String = QString::fromUtf8(ttt);
        
        NStrok++;
        
        l_String = l_String.simplified();
        l_List = l_String.split(' ');
        if(!(l_List[1] == "Cherteznik"))
            //if (!(l_String == "%Creator: Cherteznik"))
        {
            
            QMessageBox::information(NULL, "",QString::fromUtf8("Это не файл чережника") , 0,0,0);
            
            l_File.close();
            return 1;
        }
        
        
        for (int i =0; i<15;i++)
        {
            //	l_String = l_File.readLine();
            ttt = l_File.readLine();
            l_String = QString::fromUtf8(ttt);
            NStrok++;
        }
        
        // koordinaty vektorov
       // CurZ = 1.;
        while (!l_File.atEnd())
        {
            //считываем цвет
            
            //	l_String = l_File.readLine();
            ttt = l_File.readLine();
            l_String = QString::fromUtf8(ttt);
            NStrok++;
            
            
            if (l_String.isNull())
            {
                QMessageBox::information( NULL, "",QString::fromUtf8("Ошибка чтения строки") , 0,0,0);
                l_File.close();
                return 1;
            }
            
            l_String = l_String.simplified();
            l_List = l_String.split(' ');
            if (l_List.count() == 0)continue;
            
            
            if(l_List[l_List.count() - 1] == "stroke")break;
            
            if(l_List[l_List.count() - 1] == "setfont")
            {
                CurrentScale = x1 = l_List[2].toFloat();
                continue;
            }
            
            if(l_List[l_List.count() - 1] == "show")
            {
                tmp = "";
                int NWord = l_List.count()-1;
                for (int j = 0; j < NWord; j++)
                {
                    tmp += l_List[j]+" ";
                }
                //		QMessageBox::information( 0, "",  QString::fromUtf8(tmp), 0,0,0);
                int l = tmp.length();
                tmp.chop(2);
                tmp = tmp.right(l-3);
                drawText(tmp,CurrentScale,QPoint(0,0),CurColor);
                continue;
            }
            
            if (l_List[l_List.count() - 1] == "setrgbcolor")
            {
                CurColor.setRed( l_List[0].toInt());
                CurColor.setGreen(l_List[1].toInt());
                CurColor.setBlue ( l_List[2].toInt());
                continue;
            }
            
            
            
            
            if(l_List[l_List.count() - 1] == "moveto")
            {
                x1 = l_List[0].toFloat();
                y1 = -l_List[1].toFloat();
                CurX = x1;
                CurY = -y1;
                continue;
            }
            
            if(l_List[l_List.count() - 1] == "lineto")
            {
                x2 = l_List[0].toFloat();
                y2 = -l_List[1].toFloat();
                lines.append(addLine(x1, y1 , x2, y2 ));
                lines.last()->setZValue(10);
                lines.last()->setPen(QPen(CurColor));
               // CurZ += 0.01;
                CurX = x2;
                CurY = -y2;
                continue;
            }
            
            sprintf(ctmp,"%4i",NStrok);
            tmp = QString::fromUtf8("Ошибка в строке ");
            tmp.append(QString(ctmp));
            
            break;
        }
        
        l_File.close();
        
        
        

        return 0;
        
Denis Khachko's avatar
Denis Khachko committed
452

Denis Khachko's avatar
Denis Khachko committed
453
    }
Denis Khachko's avatar
Denis Khachko committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
int DrawScene::saveToFile(const QString& p_FileName)
    {
      	QFile l_File(p_FileName);
        QChar Bukva;
        char ctmp[200];
        if  (!l_File.open(QIODevice::WriteOnly))
        {
            return 1;
        }
        
        //QString ttt = QString::fromUtf8("Чертежник - Начало");
        
        //l_File.write( ttt.toUtf8());
        l_File.write( "%!PS-Adobe-1.0 EPSF-1.0\n");
        QString l_String;
        l_File.write( "%%Creator: Cherteznik\n");
        l_File.write("%%Pages: 1\n");
        l_File.write("%%Orientation: Portrait\n");
        
        
        // maximum, minimum
        
        qreal MinX,MaxX,MinY,MaxY,VecX1,VecX2,VecY1,VecY2;
        
        QLineF TmpLine;
        MinX = 1000000;
        MinY = 1000000;
        
        MaxX = -1000000;
        MaxY = -1000000;
        
        
        for (int i = 0; i <lines.count(); i++)
        {
            
            TmpLine = lines[i]->line();
            VecX1 = TmpLine.x1();
            VecY1 = -TmpLine.y1();
            VecX2 = TmpLine.x2();
            VecY2 = -TmpLine.y2();
            if (VecX1 < MinX)MinX = VecX1;
            if (VecY1 < MinY)MinY = VecY1;
            if (VecX1 > MaxX)MaxX = VecX1;
            if (VecY1 > MaxY)MaxY = VecY1;
            
            if (VecX2 < MinX)MinX = VecX2;
            if (VecY2 < MinY)MinY = VecY2;
            if (VecX2 > MaxX)MaxX = VecX2;
            if (VecY2 > MaxY)MaxY = VecY2;
            
        }
        double Scale;
        
        if (MaxX - MinX > MaxY - MinY)
        {
            Scale = (596-10)/(MaxX-MinX);
        }
        else
        {
            Scale = (842-10)/(MaxY-MinY);
        }
        Scale = Scale*0.9;
        
        //	QString tmp1 = QString(ctmp)+" scale\n";
        
        
        
        l_File.write("%%BoundingBox: 0 0 596 842\n");
        l_File.write("%%HiResBoundingBox: 0 0 596 842\n");
        l_File.write( "%%EndComments\n");
        l_File.write( "%%Page: 1 1\n");
        sprintf(ctmp,"%f %f translate\n",-MinX,-MinY);
        l_File.write(ctmp);
        sprintf(ctmp,"%f %f scale\n",Scale,Scale);
        l_File.write(ctmp);
        l_File.write("gsave [1 0 0 1 0 0] concat\n");
        l_File.write("0 0 0 setrgbcolor\n");
        l_File.write("[] 0 setdash\n");
        l_File.write("1 setlinewidth\n");
        l_File.write("0 setlinejoin\n");
        l_File.write("0 setlinecap\n");
        l_File.write("newpath\n");
        //QColor TmpColor;
        QPen TmpPen;
        QColor TmpColor;
        for (int i = 0; i <lines.count(); i++)
        {
            
            TmpLine = lines[i]->line();
            TmpPen = lines[i]->pen();
            TmpColor = TmpPen.color();
            sprintf(ctmp,"%i %i %i setrgbcolor\n", TmpColor.red(),  TmpColor.green(), TmpColor.blue());
            l_File.write(ctmp);
            
            VecX1 = TmpLine.x1();
            VecY1 = -TmpLine.y1();
            VecX2 = TmpLine.x2();
            VecY2 = -TmpLine.y2();
            
            sprintf(ctmp,"%f %f moveto\n", VecX1, VecY1);
            l_File.write(ctmp);
            
            sprintf(ctmp,"%f %f lineto\n", VecX2, VecY2);
            
            l_File.write(ctmp);
            
            
        }
        
        //77777777777777777777777777777777777
        QString TmpText;
        QByteArray ccc;
        qreal tmpX,tmpY,FontSize;
        for (int i = 0; i<texts.count(); i++)
        {
            FontSize = texts[i]->font().pointSizeF();
            sprintf(ctmp,"/Curier findfont %f scalefont setfont\n",FontSize);
            l_File.write(ctmp);
            
            tmpX = texts[i]->pos().x();
            tmpY = texts[i]->pos().x();
            sprintf(ctmp,"%f %f moveto\n", tmpX, tmpY);
            l_File.write(ctmp);
            
            
            //TmpPen = texts[i]->pen();
            //TColor = TmpPen.color();
            sprintf(ctmp,"%i %i %i setrgbcolor\n", texts[i]->pen().color().red(), texts[i]->pen().color().green(), texts[i]->pen().color().blue());
            l_File.write(ctmp);
            
            TmpText = "("+texts[i]->text()+") show\n";
            ccc = TmpText.toUtf8();
            l_File.write(ccc);
        }
        
Denis Khachko's avatar
Denis Khachko committed
589
       
Denis Khachko's avatar
Denis Khachko committed
590 591 592 593 594 595 596 597 598
        
        l_File.write("stroke\n");
        l_File.write("grestore\n");
        l_File.write( "showpage\n");
        l_File.close();
        return 0;
        
        
    };    
Denis Khachko's avatar
Denis Khachko committed
599
void DrawView::resizeEvent ( QResizeEvent * event )
Denis Khachko's avatar
Denis Khachko committed
600
    {
Denis Khachko's avatar
Denis Khachko committed
601 602 603 604 605 606 607 608
        if(firstResize)
        {
                  qDebug()<<"FirstresizeEvent";
           QPointF maxRight=mapToScene(geometry().bottomRight());
            
            centerOn(maxRight.x()/2-maxRight.x()/5,-maxRight.y()/2+maxRight.y()/4);
        }
        firstResize=false;
Denis Khachko's avatar
Denis Khachko committed
609 610 611 612 613
        qDebug()<<"resizeEvent";
       // setViewportUpdateMode (QGraphicsView::NoViewportUpdate);
       if(!pressed)  DRAW->drawNet();
        update();
       // setViewportUpdateMode (QGraphicsView::SmartViewportUpdate);
Denis Khachko's avatar
Denis Khachko committed
614
    };
Denis Khachko's avatar
Denis Khachko committed
615 616 617
  
    
    void DrawView::mousePressEvent ( QMouseEvent * event )
Denis Khachko's avatar
Denis Khachko committed
618
    {
Denis Khachko's avatar
Denis Khachko committed
619 620 621
        pressed=true;
        press_pos=event->pos();
        qDebug()<<"Mouse press"<<mapToScene(press_pos);
Denis Khachko's avatar
Denis Khachko committed
622
    
Denis Khachko's avatar
Denis Khachko committed
623 624 625 626 627 628 629 630 631 632 633 634
    };
    void DrawView::mouseReleaseEvent ( QMouseEvent * event )
    {
        pressed=false;
        DRAW->drawNet();
    };
    void DrawView::mouseMoveEvent ( QMouseEvent * event )
    {
        if(pressed)
        {
            setViewportUpdateMode (QGraphicsView::SmartViewportUpdate);
            QPointF delta=mapToScene(press_pos)-mapToScene(event->pos());
Denis Khachko's avatar
Denis Khachko committed
635 636

            
Denis Khachko's avatar
Denis Khachko committed
637
            QPointF center = mapToScene(viewport()->rect().center());
Denis Khachko's avatar
Denis Khachko committed
638 639
            //QPointF center = sceneRect().center();
             qDebug()<<"CenterOn"<<center;
Denis Khachko's avatar
Denis Khachko committed
640

Denis Khachko's avatar
Denis Khachko committed
641 642 643 644 645 646 647 648 649
            verticalScrollBar()->setValue(verticalScrollBar()->value()+(press_pos.y()-event->pos().y()));
            horizontalScrollBar()->setValue(horizontalScrollBar()->value() +(press_pos.x()-event->pos().x()));
            // centerOn(center);
          
           qDebug()<<"CenterOn"<<center<<" realCenter"<<mapToScene(viewport()->rect().center());
            qDebug()<<"SCENERECTCENTER"<<sceneRect().center().x();
            qDebug()<<"DELTA"<<delta<<" xd"<<press_pos.x()-event->pos().x()<<" yd"<<mapToScene(press_pos).y()-mapToScene(event->pos()).y();
            press_pos=event->pos();
            qDebug()<<"Ppos"<<press_pos;
Denis Khachko's avatar
Denis Khachko committed
650 651 652
            update();
        }
    }; 
Denis Khachko's avatar
Denis Khachko committed
653 654
    void DrawView::setZoom(double zoom)
    {
655 656 657 658
        if(zoom>MAX_ZOOM)
        {return;}
        if(zoom<1/MAX_ZOOM)
        {return;}
Denis Khachko's avatar
Denis Khachko committed
659
       this->scale(zoom/c_scale,zoom/c_scale);
Denis Khachko's avatar
Denis Khachko committed
660
        
Denis Khachko's avatar
Denis Khachko committed
661 662
        c_scale=zoom;
    };
Denis Khachko's avatar
Denis Khachko committed
663
 
664 665 666 667
    void DrawView::setNet()
    {
        if(DRAW->isAutoNet())
        {
Denis Khachko's avatar
Denis Khachko committed
668
            net=true;
Denis Khachko's avatar
Denis Khachko committed
669
             smallNetLabel->hide();
670
            double pixel_per_cell=DRAW->NetStepX()/(1/c_scale);
Denis Khachko's avatar
Denis Khachko committed
671 672 673 674 675 676 677 678
            qreal stepX=DRAW->NetStepX();
            qreal stepY=DRAW->NetStepY();
            
            
            if(stepX>5)stepX=(int)(stepX-0.5);
            if(stepY>5)stepY=(int)(stepY-0.5);
            
            if(pixel_per_cell>100)
679
            {
Denis Khachko's avatar
Denis Khachko committed
680 681 682 683 684 685
                stepX=DRAW->NetStepX()*0.5;
                stepY=DRAW->NetStepY()*0.5;
                if(stepX>5)stepX=(int)(stepX-0.5);
                if(stepY>5)stepY=(int)(stepY-0.5);
                DRAW->setNetStepX(stepX);
                DRAW->setNetStepY(stepY);
686
                
Denis Khachko's avatar
Denis Khachko committed
687
               // DRAW->scalePen(0.5);
688 689
                
            }
Denis Khachko's avatar
Denis Khachko committed
690
            if(pixel_per_cell<15)
691
            {
Denis Khachko's avatar
Denis Khachko committed
692 693 694 695 696 697 698
                while(pixel_per_cell<15){
                  pixel_per_cell=stepX/(1/c_scale);
                
                 stepX=stepX*1.5;
                 stepY=stepY*1.5;
                    }
        
Denis Khachko's avatar
Denis Khachko committed
699 700
                DRAW->setNetStepX(stepX);
                DRAW->setNetStepY(stepY);
701
                DRAW->drawNet();
Denis Khachko's avatar
Denis Khachko committed
702
             
Denis Khachko's avatar
Denis Khachko committed
703
               
704
            }
Denis Khachko's avatar
Denis Khachko committed
705 706
            DRAW->setNetStepX(stepX);
            DRAW->setNetStepY(stepY);
Denis Khachko's avatar
Denis Khachko committed
707
            lastStep=stepX;
708
            qDebug()<<"c_scale"<<c_scale<<"NetStep"<<DRAW->NetStepX()<<"PPC"<<pixel_per_cell;
Denis Khachko's avatar
Denis Khachko committed
709
             update();
Denis Khachko's avatar
Denis Khachko committed
710 711 712 713
        }
        else
        {
           double pixel_per_cell=DRAW->NetStepX()/(1/c_scale);
Denis Khachko's avatar
Denis Khachko committed
714
            //if(!net)pixel_per_cell=lastStep/(1/c_scale);
Denis Khachko's avatar
Denis Khachko committed
715 716 717 718 719 720 721
            if(pixel_per_cell<15) //Net step too short
            {
             net=false;
                smallNetLabel->show();
               // lastStep=DRAW->NetStepX();
            }else
            {
722
                if(pixel_per_cell>15 && !net && pixel_per_cell<this->width()*2)
Denis Khachko's avatar
Denis Khachko committed
723 724 725
                {
                 net=true;
                    smallNetLabel->hide();
Denis Khachko's avatar
Denis Khachko committed
726 727
                   // DRAW->setNetStepX(lastStep);
                   // DRAW->setNetStepY(lastStep);
Denis Khachko's avatar
Denis Khachko committed
728
                }
729
                if(pixel_per_cell>this->width()*2)
Denis Khachko's avatar
Denis Khachko committed
730 731 732
                {
                   net=false; 
                }
Denis Khachko's avatar
Denis Khachko committed
733 734 735 736 737
                  
            }
            
                
        }
738 739
    };
    
Denis Khachko's avatar
Denis Khachko committed
740 741 742 743 744 745 746
    void	DrawView::wheelEvent ( QWheelEvent * event )
    {
        float numDegrees = event->delta() / 8;
        qDebug()<<"whell:"<<numDegrees;
        //        c_scale=c_scale*0.8;
        qDebug()<<"Scale"<<c_scale;
        
Denis Khachko's avatar
Denis Khachko committed
747
        
Denis Khachko's avatar
Denis Khachko committed
748 749
        //setRenderHint(QPainter::Antialiasing);
        if(numDegrees>0)
750 751 752
        {
            
 
Denis Khachko's avatar
Denis Khachko committed
753
            setZoom(zoom()*1.189207);
Denis Khachko's avatar
Denis Khachko committed
754
            setNet();
Denis Khachko's avatar
Denis Khachko committed
755
            DRAW->scalePen(DRAW->Pen()->scale()*(1/1.189207));
Denis Khachko's avatar
Denis Khachko committed
756
              DRAW->drawNet();
Denis Khachko's avatar
Denis Khachko committed
757
        }
758
        else
Denis Khachko's avatar
Denis Khachko committed
759 760 761
        {
            if(c_scale<3e-05)return;
            setZoom(zoom()*(1/1.189207));
Denis Khachko's avatar
Denis Khachko committed
762
            setNet();
Denis Khachko's avatar
Denis Khachko committed
763
            DRAW->scalePen(DRAW->Pen()->scale()*((1.189207)));
764 765
            DRAW->drawNet();
            
Denis Khachko's avatar
Denis Khachko committed
766 767 768 769
        }
        
    }    

Victor Yacovlev's avatar
Victor Yacovlev committed
770 771
DrawModule::DrawModule(ExtensionSystem::KPlugin * parent)
    : DrawModuleBase(parent)
772 773 774
{         
    CurView = 0;
}
Denis Khachko's avatar
Denis Khachko committed
775 776 777 778
 void DrawModule::handleGuiReady()
    {
        zoomFullDraw();
    };
779
void DrawModule::createGui()
Victor Yacovlev's avatar
Victor Yacovlev committed
780
{
Denis Khachko's avatar
Denis Khachko committed
781
    CurView=new DrawView();
Denis Khachko's avatar
Denis Khachko committed
782 783
    netStepX=1;
    netStepY=1;
Denis Khachko's avatar
Denis Khachko committed
784
    autoNet=true;
Denis Khachko's avatar
Denis Khachko committed
785
    netColor=QColor("999999");
Denis Khachko's avatar
Denis Khachko committed
786
    penIsDrawing=false;
Denis Khachko's avatar
Denis Khachko committed
787
    CurScene=new DrawScene(CurView);
Denis Khachko's avatar
Denis Khachko committed
788
    navigator=new DrawNavigator(CurView);
Denis Khachko's avatar
Denis Khachko committed
789 790 791
    showToolsBut=new QToolButton(CurView);
    showToolsBut->move(20,20);
    showToolsBut->setCheckable(true);
792 793


Denis Khachko's avatar
Denis Khachko committed
794
    connect(showToolsBut,SIGNAL(toggled (bool)),this,SLOT(showNavigator(bool)));
Denis Khachko's avatar
Denis Khachko committed
795
    showToolsBut->setIcon(QIcon(myResourcesDir().absoluteFilePath("menu-24x24-black.png")));
Denis Khachko's avatar
Denis Khachko committed
796 797
    connect(m_actionDrawSaveDrawing,SIGNAL(triggered()),this,SLOT(saveFile()));
    connect(m_actionDrawLoadDrawing,SIGNAL(triggered()),this,SLOT(openFile()));
Denis Khachko's avatar
Denis Khachko committed
798 799 800 801 802 803 804 805 806 807
    connect(navigator,SIGNAL(redrawNet()),this,SLOT(drawNet()));
    connect(navigator,SIGNAL(autoNetChange(bool)),this,SLOT(autoNetChange(bool)));
     connect(navigator,SIGNAL(netStepChange(double)),this,SLOT(netStepChange(double)));
    
    connect(navigator->zoomUp,SIGNAL(pressed()),this,SLOT(zoomIn()));
    connect(navigator->zoomDown,SIGNAL(pressed()),this,SLOT(zoomOut()));
    connect(navigator->zoomNormal,SIGNAL(pressed()),this,SLOT(zoomNorm()));
    connect(navigator->zoomFullDraw,SIGNAL(pressed()),this,SLOT(zoomFullDraw()));
    
    
Denis Khachko's avatar
Denis Khachko committed
808
    navigator->setParent(CurView);
Denis Khachko's avatar
Denis Khachko committed
809
    navigator->setFixedSize(QSize(120,175));
Denis Khachko's avatar
Denis Khachko committed
810
    
Denis Khachko's avatar
Denis Khachko committed
811 812
    navigator->move(20,showToolsBut->pos().y()+showToolsBut->height());
    navigator->hide();
813

Denis Khachko's avatar
Denis Khachko committed
814
    CurScene->setDraw(this);
Denis Khachko's avatar
Denis Khachko committed
815
    CurView->setScene(CurScene);
816 817
    penColor.r = penColor.g = penColor.b = 0;
    penColor.a = 255;
Denis Khachko's avatar
Denis Khachko committed
818
    CurView->setDraw(this);
Denis Khachko's avatar
Denis Khachko committed
819 820
    CurView->centerOn(5,-5);
    CurView->setViewportUpdateMode (QGraphicsView::NoViewportUpdate);//For better perfomance; Manual Update;
Denis Khachko's avatar
Denis Khachko committed
821
    drawNet();
Denis Khachko's avatar
Denis Khachko committed
822
    CreatePen();
823
    CurView->setZoom(50);
Denis Khachko's avatar
Denis Khachko committed
824
    CurView->setNet();
825 826
    netStepX=1;
    netStepY=1;
827 828 829



Denis Khachko's avatar
Denis Khachko committed
830 831
    CurView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    CurView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
Denis Khachko's avatar
Denis Khachko committed
832
    QBrush curBackground=QBrush(QColor(DrawSettings()->value("Draw/BackColor","lightgreen").toString()));
Denis Khachko's avatar
Denis Khachko committed
833

Denis Khachko's avatar
Denis Khachko committed
834
    CurScene->setBackgroundBrush (curBackground);
Denis Khachko's avatar
Denis Khachko committed
835
    Color Black;
Denis Khachko's avatar
Denis Khachko committed
836
       Black.r=0;Black.g=0;Black.b=0;Black.a=255;
Denis Khachko's avatar
Denis Khachko committed
837 838
    
    penColor=Black;
839
}
Denis Khachko's avatar
Denis Khachko committed
840

841 842 843 844
QString DrawModule::initialize(const QStringList &configurationParameters, const ExtensionSystem::CommandLine &)
{
    if (!configurationParameters.contains("tablesOnly")) {
        createGui();
Denis Khachko's avatar
Denis Khachko committed
845 846 847
        animate=false;
        redrawTimer = new QTimer(this);
        connect(redrawTimer,SIGNAL(timeout()), this, SLOT(redraw()));
848 849
    }
    return "";
Victor Yacovlev's avatar
Victor Yacovlev committed
850
}
Denis Khachko's avatar
Denis Khachko committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
    
void  DrawModule::openFile()
    {
        QString	File=QFileDialog::getOpenFileName(mainWidget(), QString::fromUtf8 ("Открыть файл"), curDir.path(), "(*.ps)");
        
        
        
        QFileInfo info(File);
        QDir dir=info.absoluteDir();
        
        
        if ( File.isEmpty())return;
        // CurrentFileName = RobotFile;
        
        if( CurScene->loadFromFile(File)!=0)//Get troubles when loading env.
        {
            QMessageBox::information(CurView, "", QString::fromUtf8("Ошибка открытия файла! ")+File, 0,0,0); 
            return;
        }
        
    
        
;

    };
void  DrawModule::saveFile()
    {
        QString	File=QFileDialog::getSaveFileName(CurView, QString::fromUtf8 ("Сохранить файл"),curDir.path(), "(*.ps)");
        
        
        //QString	RobotFile=dialog.selectedFiles().first();
        QFileInfo info(File);
        QDir dir=info.absoluteDir();
        curDir=dir.path();
        if (File.contains("*") || File.contains("?"))
        {
            QMessageBox::information( 0, "", QString::fromUtf8("Недопустимый символ в имени файла!"), 0,0,0);
            return;
        }
        //QString	RobotFile =  QFileDialog::getSaveFileName(MV,QString::fromUtf8 ("Сохранить в файл"),"/home", "(*.fil)");
        //if ( RobotFile.isEmpty())return;
        
        if(File.right(4)!=".ps")File+=".ps";
        //CurrentFileName = RobotFile;
        
        CurScene->saveToFile(File);
    };    

Denis Khachko's avatar
Denis Khachko committed
899 900 901 902 903
void DrawModule::showNavigator(bool state)
    {
        navigator->setVisible(state);
    };
    
904 905 906 907 908 909
/* public static */ QList<ExtensionSystem::CommandLineParameter> DrawModule::acceptableCommandLineParameters()
{
    // See "src/shared/extensionsystem/commandlineparameter.h" for constructor details
    return QList<ExtensionSystem::CommandLineParameter>();
}

Victor Yacovlev's avatar
Victor Yacovlev committed
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
/* public slot */ void DrawModule::changeGlobalState(ExtensionSystem::GlobalState old, ExtensionSystem::GlobalState current)
{
    // Called when changed kumir state. The states are defined as enum ExtensionSystem::GlobalState:
    /*
    namespace ExtensionSystem {
        enum GlobalState {
            GS_Unlocked, // Edit mode
            GS_Observe, // Observe mode
            GS_Running, // Running mode
            GS_Input,  // User input required
            GS_Pause  // Running paused
        };
    }
    */
    // TODO implement me
    using namespace ExtensionSystem;  // not to write "ExtensionSystem::" each time in this method scope
    Q_UNUSED(old);  // Remove this line on implementation
    Q_UNUSED(current);  // Remove this line on implementation
Denis Khachko's avatar
Denis Khachko committed
928
  
Denis Khachko's avatar
Denis Khachko committed
929
    CurView->setViewportUpdateMode (QGraphicsView::FullViewportUpdate);
Denis Khachko's avatar
Denis Khachko committed
930
    CurView->forceRedraw();
Denis Khachko's avatar
Denis Khachko committed
931
    CurScene->update(CurScene->sceneRect());
Denis Khachko's avatar
Denis Khachko committed
932
    CurView->repaint();
Denis Khachko's avatar
Denis Khachko committed
933
    CurView->viewport()->update();
Denis Khachko's avatar
Denis Khachko committed
934
    CurView->setViewportUpdateMode (QGraphicsView::NoViewportUpdate);
Denis Khachko's avatar
Denis Khachko committed
935 936 937 938 939
    if(current==GlobalState::GS_Running)
    {
        redrawTimer->start(500);
    }else
        redrawTimer->stop();
Victor Yacovlev's avatar
Victor Yacovlev committed
940 941
}

942 943 944 945 946 947 948 949
/* public slot */ void DrawModule::loadActorData(QIODevice * source)
{
    // Set actor specific data (like environment)
    // The source should be ready-to-read QIODevice like QBuffer or QFile
    Q_UNUSED(source);  // By default do nothing

}

Victor Yacovlev's avatar
Victor Yacovlev committed
950 951
/* public */ QWidget* DrawModule::mainWidget() const
{
Denis Khachko's avatar
Denis Khachko committed
952

Denis Khachko's avatar
Denis Khachko committed
953
    return  CurView;
Denis Khachko's avatar
Denis Khachko committed
954

Victor Yacovlev's avatar
Victor Yacovlev committed
955 956 957 958
}

/* public */ QWidget* DrawModule::pultWidget() const
{
Denis Khachko's avatar
Denis Khachko committed
959
 //No pult for Draw module.
Victor Yacovlev's avatar
Victor Yacovlev committed
960 961 962
    return nullptr;
}

963
/* public slot */ void DrawModule::reloadSettings(ExtensionSystem::SettingsPtr settings, const QStringList & keys)
Victor Yacovlev's avatar
Victor Yacovlev committed
964
{
965 966
    // Updates setting on module load, workspace change or appliyng settings dialog.
    // If @param keys is empty -- should reload all settings, otherwise load only setting specified by @param keys
Victor Yacovlev's avatar
Victor Yacovlev committed
967
    // TODO implement me
Denis Khachko's avatar
Denis Khachko committed
968
    QBrush curBackground=QBrush(QColor(settings->value("BackColor","lightgreen").toString()));
Denis Khachko's avatar
Denis Khachko committed
969 970 971 972
    
    CurScene->setBackgroundBrush (curBackground);
    netColor=QColor(settings->value("LineColor","gray").toString());
    drawNet();
Denis Khachko's avatar
Denis Khachko committed
973
    Q_UNUSED(keys);
Victor Yacovlev's avatar
Victor Yacovlev committed
974 975 976 977 978 979
}

/* public slot */ void DrawModule::reset()
{
    // Resets module to initial state before program execution
    // TODO implement me
Denis Khachko's avatar
Denis Khachko committed
980 981
    mPen->setBrush(QBrush(QColor("white")));
    penIsDrawing=false;
Denis Khachko's avatar
Denis Khachko committed
982
    mPen->setPos(0,0);
Denis Khachko's avatar
Denis Khachko committed
983
    CurScene->reset();
Denis Khachko's avatar
Denis Khachko committed
984
    Color Black;
Denis Khachko's avatar
Denis Khachko committed
985
    Black.r=0;Black.g=0;Black.b=0;Black.a=255;
Denis Khachko's avatar
Denis Khachko committed
986 987
    
    penColor=Black;
Denis Khachko's avatar
Denis Khachko committed
988 989 990
    QList<QRectF> rect;
    rect.append(CurView->sceneRect());
    CurView->updateScene(rect);
Denis Khachko's avatar
Denis Khachko committed
991
    CurView->show();
Denis Khachko's avatar
Denis Khachko committed
992
    CurView->forceRedraw();
993 994
    CurView->setZoom(CurView->zoom()*2);
    CurView->setZoom(CurView->zoom()*0.5);
Victor Yacovlev's avatar
Victor Yacovlev committed
995 996 997 998 999 1000 1001
}

/* public slot */ void DrawModule::setAnimationEnabled(bool enabled)
{
    // Sets GUI animation flag on run
    // NOTE this method just setups a flag and might be called anytime, even module not needed
    // TODO implement me
Denis Khachko's avatar
Denis Khachko committed
1002 1003
   // Q_UNUSED(enabled);  // Remove this line on implementation
    animate=enabled;
Victor Yacovlev's avatar
Victor Yacovlev committed
1004 1005 1006 1007 1008 1009
}

/* public slot */ void DrawModule::runSetupPen()
{
    /* алг опустить перо */
    // TODO implement me
Denis Khachko's avatar
Denis Khachko committed
1010
     mPen->setBrush(QBrush(QColor(penColor.r, penColor.g, penColor.b, penColor.a)));
Denis Khachko's avatar
Denis Khachko committed
1011
    penIsDrawing=true;
Victor Yacovlev's avatar
Victor Yacovlev committed
1012 1013 1014 1015 1016 1017
}

/* public slot */ void DrawModule::runReleasePen()
{
    /* алг поднять перо */
    // TODO implement me
Denis Khachko's avatar
Denis Khachko committed
1018

1019
    mutex.lock();
Denis Khachko's avatar
Denis Khachko committed
1020

Denis Khachko's avatar
Denis Khachko committed
1021
    mPen->setBrush(QBrush(QColor("white")));
Denis Khachko's avatar
Denis Khachko committed
1022

Denis Khachko's avatar
Denis Khachko committed
1023
    penIsDrawing=false;
1024
    mutex.unlock();
Victor Yacovlev's avatar
Victor Yacovlev committed
1025 1026 1027 1028 1029 1030
    
}

/* public slot */ void DrawModule::runSetPenColor(const Color& color)
{
    /* алг установить цвет(цвет color) */
Denis Khachko's avatar
Denis Khachko committed
1031
   
Denis Khachko's avatar
Denis Khachko committed
1032
    penColor=color;
Denis Khachko's avatar
Denis Khachko committed
1033
    mPen->setBrush(QBrush(QColor(penColor.r, penColor.g, penColor.b, penColor.a)));
Victor Yacovlev's avatar
Victor Yacovlev committed
1034 1035
    qDebug()
            << "DrawModule::runSetPenColor( { cssValue = \""
1036
            << QColor(color.r, color.g, color.b, color.a).name()
Denis Khachko's avatar
Denis Khachko committed
1037
            << "\" } )"<<color.a;
Victor Yacovlev's avatar
Victor Yacovlev committed
1038 1039 1040 1041
}

/* public slot */ void DrawModule::runMoveTo(const qreal x, const qreal y)
{
1042
    mutex.lock();
Denis Khachko's avatar
Denis Khachko committed
1043 1044 1045 1046
    QPointF start=mPen->pos();
    mPen->setPos(x, -y);
    if(penIsDrawing)
    {
1047
        CurScene->addDrawLine(QLineF(start,mPen->pos()), QColor(penColor.r, penColor.g, penColor.b, penColor.a),mySettings()->value("LineWidth",1).toFloat());
Denis Khachko's avatar
Denis Khachko committed
1048
    }
Denis Khachko's avatar
Denis Khachko committed
1049
    CurView->resetCachedContent();
1050
    CurView->update();
Denis Khachko's avatar
Denis Khachko committed
1051
    if(animate)redrawPicture();
1052 1053
    mutex.unlock();
}
Victor Yacovlev's avatar
Victor Yacovlev committed
1054 1055 1056 1057 1058

/* public slot */ void DrawModule::runMoveBy(const qreal dX, const qreal dY)
{
    /* алг сместиться на вектор(вещ dX, вещ dY) */
    // TODO implement me
1059
    mutex.lock();
Denis Khachko's avatar
Denis Khachko committed
1060
    QPointF start=mPen->pos();
Denis Khachko's avatar
Denis Khachko committed
1061
    mPen->moveBy(dX, -dY);
Denis Khachko's avatar
Denis Khachko committed
1062 1063
    if(penIsDrawing)
        {
1064
            CurScene->addDrawLine(QLineF(start,mPen->pos()), QColor(penColor.r, penColor.g, penColor.b, penColor.a),mySettings()->value("LineWidth",1).toFloat());
Denis Khachko's avatar
Denis Khachko committed
1065
        }
Denis Khachko's avatar
Denis Khachko committed
1066
    CurView->update();
Denis Khachko's avatar
Denis Khachko committed
1067
     if(animate)redrawPicture();
1068
    mutex.unlock();
Victor Yacovlev's avatar
Victor Yacovlev committed
1069 1070 1071 1072 1073
}

/* public slot */ void DrawModule::runAddCaption(const qreal width, const QString& text)
{
    /* алг надпись(вещ width, лит text) */
Denis Khachko's avatar
Denis Khachko committed
1074
   
Denis Khachko's avatar
Denis Khachko committed
1075 1076 1077 1078 1079
    if (width<=0)
    {
        setError(trUtf8("Отрицательная или нулевая ширина надписи!"));
        return;
    }
Denis Khachko's avatar
Denis Khachko committed
1080 1081
    qreal offset=CurScene->drawText(text, width, mPen->pos(),QColor(penColor.r, penColor.g, penColor.b, penColor.a));
    mPen->moveBy(offset, 0);
Denis Khachko's avatar
Denis Khachko committed
1082
     qDebug()<<"TExt ofset"<<offset;  
Victor Yacovlev's avatar
Victor Yacovlev committed
1083
}
Denis Khachko's avatar
Denis Khachko committed
1084 1085 1086 1087 1088 1089 1090
    
    
bool DrawModule::runIsLineAtCircle(const qreal x, const qreal y, const qreal radius)
    {
        return CurScene->isLineAt(QPointF(x,-y), radius);
    };    
    
Denis Khachko's avatar
Denis Khachko committed
1091 1092
void DrawModule::drawNet()
    {
1093
        mutex.lock();
Denis Khachko's avatar
Denis Khachko committed
1094
       
Denis Khachko's avatar
Denis Khachko committed
1095 1096
        QPointF start_d=CurView->mapToScene(CurView->geometry().topLeft());
        QPointF end_d=CurView->mapToScene(CurView->geometry().bottomRight());
Denis Khachko's avatar
Denis Khachko committed
1097
       // QPointF center=CurView->mapToScene(CurView->geometry().center());
Denis Khachko's avatar
Denis Khachko committed
1098
       // qDebug()<<"StartDeb"<<start_d<<end_d;
Denis Khachko's avatar
Denis Khachko committed
1099
        // QPointF end=CurView->mapToScene(CurView->viewport()->rect().bottomRight().x(),CurView->viewport()->rect().bottomRight().y());
Denis Khachko's avatar
Denis Khachko committed
1100

Denis Khachko's avatar
Denis Khachko committed
1101
        CurView->setSceneRect(QRectF(QPointF(start_d.x()-(CurView->geometry().width())*(1/zoom()),start_d.y()-(CurView->geometry().height()*2)*(1/zoom())),
1102
                                     QPointF(end_d.x()+2000*(1/zoom()),end_d.y()+2000*(1/zoom()))));
Denis Khachko's avatar
Denis Khachko committed
1103 1104
        QPointF start=CurView->sceneRect().topLeft();
        QPointF end=CurView->sceneRect().bottomRight();