Qucs-GUI  0.0.19
/home/travis/build/Qucs/qucs/qucs/qucs/paintings/graphicline.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                               graphicline.cpp
00003                              -----------------
00004     begin                : Mon Nov 24 2003
00005     copyright            : (C) 2003 by Michael Margraf
00006     email                : michael.margraf@alumni.tu-berlin.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 #include "graphicline.h"
00018 #include "filldialog.h"
00019 #include "schematic.h"
00020 
00021 #include <QPainter>
00022 #include <QPushButton>
00023 #include <QLineEdit>
00024 #include <QComboBox>
00025 
00026 GraphicLine::GraphicLine(int cx_, int cy_, int x2_, int y2_, QPen Pen_)
00027 {
00028   Name = "Line ";
00029   isSelected = false;
00030   Pen = Pen_;
00031   cx = cx_;
00032   cy = cy_;
00033   x1 = y1 = 0;
00034   x2 = x2_;
00035   y2 = y2_;
00036 }
00037 
00038 GraphicLine::~GraphicLine()
00039 {
00040 }
00041 
00042 // --------------------------------------------------------------------------
00043 void GraphicLine::paint(ViewPainter *p)
00044 {
00045   if(isSelected) {
00046     p->Painter->setPen(QPen(Qt::darkGray,Pen.width()+5));
00047     p->drawLine(cx, cy, cx+x2, cy+y2);
00048     p->Painter->setPen(QPen(Qt::white, Pen.width(), Pen.style()));
00049     p->drawLine(cx, cy, cx+x2, cy+y2);
00050 
00051     p->Painter->setPen(QPen(Qt::darkRed,2));
00052     p->drawResizeRect(cx, cy);  // markers for changing the size
00053     p->drawResizeRect(cx+x2, cy+y2);
00054     return;
00055   }
00056   p->Painter->setPen(Pen);
00057   p->drawLine(cx, cy, cx+x2, cy+y2);
00058 }
00059 
00060 // --------------------------------------------------------------------------
00061 void GraphicLine::paintScheme(Schematic *p)
00062 {
00063   p->PostPaintEvent(_Line, cx, cy, cx+x2, cy+y2);
00064 }
00065 
00066 // --------------------------------------------------------------------------
00067 void GraphicLine::getCenter(int& x, int &y)
00068 {
00069   x = cx+(x2>>1);
00070   y = cy+(y2>>1);
00071 }
00072 
00073 // --------------------------------------------------------------------------
00074 // Sets the center of the painting to x/y.
00075 void GraphicLine::setCenter(int x, int y, bool relative)
00076 {
00077   if(relative) { cx += x;  cy += y; }
00078   else { cx = x-(x2>>1);  cy = y-(y2>>1); }
00079 }
00080 
00081 // --------------------------------------------------------------------------
00082 Painting* GraphicLine::newOne()
00083 {
00084   return new GraphicLine();
00085 }
00086 
00087 // --------------------------------------------------------------------------
00088 Element* GraphicLine::info(QString& Name, char* &BitmapFile, bool getNewOne)
00089 {
00090   Name = QObject::tr("Line");
00091   BitmapFile = (char *) "line";
00092 
00093   if(getNewOne)  return new GraphicLine();
00094   return 0;
00095 }
00096 
00097 // --------------------------------------------------------------------------
00098 bool GraphicLine::load(const QString& s)
00099 {
00100   bool ok;
00101 
00102   QString n;
00103   n  = s.section(' ',1,1);    // cx
00104   cx = n.toInt(&ok);
00105   if(!ok) return false;
00106 
00107   n  = s.section(' ',2,2);    // cy
00108   cy = n.toInt(&ok);
00109   if(!ok) return false;
00110 
00111   n  = s.section(' ',3,3);    // x2
00112   x2 = n.toInt(&ok);
00113   if(!ok) return false;
00114 
00115   n  = s.section(' ',4,4);    // y2
00116   y2 = n.toInt(&ok);
00117   if(!ok) return false;
00118 
00119   n  = s.section(' ',5,5);    // color
00120   QColor co;
00121   co.setNamedColor(n);
00122   Pen.setColor(co);
00123   if(!Pen.color().isValid()) return false;
00124 
00125   n  = s.section(' ',6,6);    // thickness
00126   Pen.setWidth(n.toInt(&ok));
00127   if(!ok) return false;
00128 
00129   n  = s.section(' ',7,7);    // line style
00130   Pen.setStyle((Qt::PenStyle)n.toInt(&ok));
00131   if(!ok) return false;
00132 
00133   return true;
00134 }
00135 
00136 // --------------------------------------------------------------------------
00137 QString GraphicLine::save()
00138 {
00139   QString s = Name+QString::number(cx)+" "+QString::number(cy)+" ";
00140   s += QString::number(x2)+" "+QString::number(y2)+" ";
00141   s += Pen.color().name()+" "+QString::number(Pen.width())+" ";
00142   s += QString::number(Pen.style());
00143   return s;
00144 }
00145 
00146 // --------------------------------------------------------------------------
00147 QString GraphicLine::saveCpp()
00148 {
00149   QString s =
00150     QString ("new Line (%1, %2, %3, %4, QPen (QColor (\"%5\"), %6, %7))").
00151     arg(cx+x1).arg(cy+y1).arg(cx+x2).arg(cy+y2).
00152     arg(Pen.color().name()).arg(Pen.width()).arg(toPenString(Pen.style()));
00153   s = "Lines.append (" + s + ");";
00154   return s;
00155 }
00156 
00157 QString GraphicLine::saveJSON()
00158 {
00159   QString s =
00160     QString ("{\"type\" : \"line\", "
00161       "\"x1\" : %1, \"y1\" : %2, \"x2\" : %3, \"y2\" : %4, "
00162       "\"color\" : \"%5\", \"thick\" : %6, \"style\" : \"%7\"},").
00163       arg(cx+x1).arg(cy+y1).arg(cx+x2).arg(cy+y2).
00164       arg(Pen.color().name()).arg(Pen.width()).arg(toPenString(Pen.style()));
00165   return s;
00166 }
00167 
00168 // --------------------------------------------------------------------------
00169 // Checks if the resize area was clicked.
00170 bool GraphicLine::resizeTouched(float fX, float fY, float len)
00171 {
00172   float fCX = float(cx), fCY = float(cy);
00173 
00174   if(fX <= fCX+len) if(fX >= fCX-len) if(fY <= fCY+len) if(fY >= fCY-len) {
00175     State = 1;
00176     return true;
00177   }
00178 
00179   fCX += float(x2);
00180   fCY += float(y2);
00181   if(fX <= fCX+len) if(fX >= fCX-len) if(fY <= fCY+len) if(fY >= fCY-len) {
00182     State = 2;
00183     return true;
00184   }
00185 
00186   State = 0;
00187   return false;
00188 }
00189 
00190 // --------------------------------------------------------------------------
00191 // Mouse move action during resize.
00192 void GraphicLine::MouseResizeMoving(int x, int y, Schematic *p)
00193 {
00194   paintScheme(p);  // erase old painting
00195   if(State == 1) { x2 += cx-x; y2 += cy-y; cx = x; cy = y; } // move beginning
00196   else { x2 = x-cx;  y2 = y-cy; }  // move ending
00197 
00198   paintScheme(p);  // paint new painting
00199 }
00200 
00201 // --------------------------------------------------------------------------
00202 // fx/fy are the precise coordinates, gx/gy are the coordinates set on grid.
00203 // x/y are coordinates without scaling.
00204 void GraphicLine::MouseMoving(
00205   Schematic *paintScale, int, int, int gx, int gy,
00206   Schematic *p, int x, int y, bool drawn)
00207 {
00208   if(State > 0) {
00209     if(State > 1)
00210       paintScale->PostPaintEvent(_Line, cx, cy, cx+x2, cy+y2);  // erase old painting
00211     State++;
00212     x2 = gx-cx;
00213     y2 = gy-cy;
00214     paintScale->PostPaintEvent(_Line, cx, cy, cx+x2, cy+y2);  // paint new painting
00215   }
00216   else { cx = gx; cy = gy; }
00217 
00218 
00219   // FIXME #warning p->setPen(Qt::SolidLine);
00220   if(drawn) {
00221     p->PostPaintEvent(_Line, x1+27, y1, x1+15, y1+12,0,0,true);  // erase old cursor symbol
00222     p->PostPaintEvent(_Line, x1+25, y1-2, x1+29, y1+2,0,0,true);
00223     p->PostPaintEvent(_Line, x1+13, y1+10, x1+17, y1+14,0,0,true);
00224   }
00225   x1 = x;
00226   y1 = y;
00227   p->PostPaintEvent(_Line, x1+27, y1, x1+15, y1+12,0,0,true);  // paint new cursor symbol
00228   p->PostPaintEvent(_Line, x1+25, y1-2, x1+29, y1+2,0,0,true);
00229   p->PostPaintEvent(_Line, x1+13, y1+10, x1+17, y1+14,0,0,true);
00230 }
00231 
00232 // --------------------------------------------------------------------------
00233 bool GraphicLine::MousePressing()
00234 {
00235   State++;
00236   if(State > 2) {
00237     x1 = y1 = 0;
00238     State = 0;
00239     return true;    // painting is ready
00240   }
00241   return false;
00242 }
00243 
00244 // --------------------------------------------------------------------------
00245 // Checks if the coordinates x/y point to the painting.
00246 // 5 is the precision the user must point onto the painting.
00247 bool GraphicLine::getSelected(float fX, float fY, float w)
00248 {
00249   fX -= float(cx);
00250   fY -= float(cy);
00251 
00252   if(fX < -w) {
00253     if(fX < float(x2)-w)  // is point between x coordinates ?
00254       return false;
00255   }
00256   else {
00257     if(fX > w)
00258       if(fX > float(x2)+w)
00259         return false;
00260   }
00261 
00262   if(fY < -w) {
00263     if(fY < float(y2)-w)   // is point between y coordinates ?
00264       return false;
00265   }
00266   else {
00267     if(fY > w)
00268       if(fY > float(y2)+w)
00269         return false;
00270   }
00271 
00272   float A = float(x2)*fY - fX*float(y2); // calculate the rectangle area spanned
00273   A *= A;               // avoid the need for square root
00274 
00275   if(A <= w*w*float(x2*x2 + y2*y2))
00276     return true;     // x/y lies on the graph line
00277 
00278   return false;
00279 }
00280 
00281 // --------------------------------------------------------------------------
00282 void GraphicLine::Bounding(int& _x1, int& _y1, int& _x2, int& _y2)
00283 {
00284   if(x2 < 0) { _x1 = cx+x2; _x2 = cx; }
00285   else { _x1 = cx; _x2 = cx+x2; }
00286 
00287   if(y2 < 0) { _y1 = cy+y2; _y2 = cy; }
00288   else { _y1 = cy; _y2 = cy+y2; }
00289 }
00290 
00291 // --------------------------------------------------------------------------
00292 // Rotates around the center.
00293 void GraphicLine::rotate()
00294 {
00295   cx += (x2>>1) - (y2>>1);
00296   cy += (x2>>1) + (y2>>1);
00297 
00298   int tmp = x2;
00299   x2  =  y2;
00300   y2  = -tmp;
00301 }
00302 
00303 // --------------------------------------------------------------------------
00304 // Mirrors about center line.
00305 void GraphicLine::mirrorX()
00306 {
00307   cy +=  y2;
00308   y2  = -y2;
00309 }
00310 
00311 // --------------------------------------------------------------------------
00312 // Mirrors about center line.
00313 void GraphicLine::mirrorY()
00314 {
00315   cx +=  x2;
00316   x2  = -x2;
00317 }
00318 
00319 // --------------------------------------------------------------------------
00320 // Calls the property dialog for the painting and changes them accordingly.
00321 // If there were changes, it returns 'true'.
00322 bool GraphicLine::Dialog()
00323 {
00324   bool changed = false;
00325 
00326   FillDialog *d = new FillDialog(QObject::tr("Edit Line Properties"), false);
00327   d->ColorButt->setPaletteBackgroundColor(Pen.color());
00328   d->LineWidth->setText(QString::number(Pen.width()));
00329   d->StyleBox->setCurrentItem(Pen.style()-1);
00330 
00331   if(d->exec() == QDialog::Rejected) {
00332     delete d;
00333     return false;
00334   }
00335 
00336   if(Pen.color() != d->ColorButt->paletteBackgroundColor()) {
00337     Pen.setColor(d->ColorButt->paletteBackgroundColor());
00338     changed = true;
00339   }
00340   if(Pen.width()  != d->LineWidth->text().toInt()) {
00341     Pen.setWidth(d->LineWidth->text().toInt());
00342     changed = true;
00343   }
00344   if(Pen.style()  != (d->StyleBox->currentItem()+1)) {
00345     Pen.setStyle((Qt::PenStyle)(d->StyleBox->currentItem()+1));
00346     changed = true;
00347   }
00348 
00349   delete d;
00350   return changed;
00351 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines