Qucs-GUI
0.0.19
|
00001 /*************************************************************************** 00002 ellipse.cpp 00003 ------------- 00004 begin : Sun Nov 23 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 "ellipse.h" 00018 #include "filldialog.h" 00019 #include "schematic.h" 00020 #include "viewpainter.h" 00021 00022 #include <QPainter> 00023 #include <QPushButton> 00024 #include <QLineEdit> 00025 #include <QComboBox> 00026 #include <QCheckBox> 00027 00028 Ellipse::Ellipse(bool _filled) 00029 { 00030 Name = "Ellipse "; 00031 isSelected = false; 00032 Pen = QPen(QColor()); 00033 Brush = QBrush(Qt::lightGray); 00034 filled = _filled; 00035 cx = cy = 0; 00036 x1 = x2 = 0; 00037 y1 = y2 = 0; 00038 } 00039 00040 Ellipse::~Ellipse() 00041 { 00042 } 00043 00044 // -------------------------------------------------------------------------- 00045 void Ellipse::paint(ViewPainter *p) 00046 { 00047 if(isSelected) { 00048 p->Painter->setPen(QPen(Qt::darkGray,Pen.width()+5)); 00049 if(filled) p->Painter->setBrush(Brush); 00050 p->drawEllipse(cx, cy, x2, y2); 00051 p->Painter->setPen(QPen(Qt::white, Pen.width(), Pen.style())); 00052 p->Painter->setBrush(Qt::NoBrush); 00053 p->drawEllipse(cx, cy, x2, y2); 00054 00055 p->Painter->setPen(QPen(Qt::darkRed,2)); 00056 p->drawResizeRect(cx, cy+y2); // markers for changing the size 00057 p->drawResizeRect(cx, cy); 00058 p->drawResizeRect(cx+x2, cy+y2); 00059 p->drawResizeRect(cx+x2, cy); 00060 return; 00061 } 00062 p->Painter->setPen(Pen); 00063 if(filled) p->Painter->setBrush(Brush); 00064 p->drawEllipse(cx, cy, x2, y2); 00065 p->Painter->setBrush(Qt::NoBrush); // no filling for the next paintings 00066 } 00067 00068 // -------------------------------------------------------------------------- 00069 void Ellipse::paintScheme(Schematic *p) 00070 { 00071 p->PostPaintEvent(_Ellipse, cx, cy, x2, y2); 00072 } 00073 00074 // -------------------------------------------------------------------------- 00075 void Ellipse::getCenter(int& x, int &y) 00076 { 00077 x = cx+(x2>>1); 00078 y = cy+(y2>>1); 00079 } 00080 00081 // -------------------------------------------------------------------------- 00082 // Sets the center of the painting to x/y. 00083 void Ellipse::setCenter(int x, int y, bool relative) 00084 { 00085 if(relative) { cx += x; cy += y; } 00086 else { cx = x-(x2>>1); cy = y-(y2>>1); } 00087 } 00088 00089 // -------------------------------------------------------------------------- 00090 Painting* Ellipse::newOne() 00091 { 00092 return new Ellipse(); 00093 } 00094 00095 // -------------------------------------------------------------------------- 00096 Element* Ellipse::info(QString& Name, char* &BitmapFile, bool getNewOne) 00097 { 00098 Name = QObject::tr("Ellipse"); 00099 BitmapFile = (char *) "ellipse"; 00100 00101 if(getNewOne) return new Ellipse(); 00102 return 0; 00103 } 00104 00105 // -------------------------------------------------------------------------- 00106 Element* Ellipse::info_filled(QString& Name, char* &BitmapFile, bool getNewOne) 00107 { 00108 Name = QObject::tr("filled Ellipse"); 00109 BitmapFile = (char *) "filledellipse"; 00110 00111 if(getNewOne) return new Ellipse(true); 00112 return 0; 00113 } 00114 00115 // -------------------------------------------------------------------------- 00116 bool Ellipse::load(const QString& s) 00117 { 00118 bool ok; 00119 00120 QString n; 00121 n = s.section(' ',1,1); // cx 00122 cx = n.toInt(&ok); 00123 if(!ok) return false; 00124 00125 n = s.section(' ',2,2); // cy 00126 cy = n.toInt(&ok); 00127 if(!ok) return false; 00128 00129 n = s.section(' ',3,3); // x2 00130 x2 = n.toInt(&ok); 00131 if(!ok) return false; 00132 00133 n = s.section(' ',4,4); // y2 00134 y2 = n.toInt(&ok); 00135 if(!ok) return false; 00136 00137 n = s.section(' ',5,5); // color 00138 QColor co; 00139 co.setNamedColor(n); 00140 Pen.setColor(co); 00141 if(!Pen.color().isValid()) return false; 00142 00143 n = s.section(' ',6,6); // thickness 00144 Pen.setWidth(n.toInt(&ok)); 00145 if(!ok) return false; 00146 00147 n = s.section(' ',7,7); // line style 00148 Pen.setStyle((Qt::PenStyle)n.toInt(&ok)); 00149 if(!ok) return false; 00150 00151 n = s.section(' ',8,8); // fill color 00152 co.setNamedColor(n); 00153 Brush.setColor(co); 00154 if(!Brush.color().isValid()) return false; 00155 00156 n = s.section(' ',9,9); // fill style 00157 Brush.setStyle((Qt::BrushStyle)n.toInt(&ok)); 00158 if(!ok) return false; 00159 00160 n = s.section(' ',10,10); // filled 00161 if(n.toInt(&ok) == 0) filled = false; 00162 else filled = true; 00163 if(!ok) return false; 00164 00165 return true; 00166 } 00167 00168 // -------------------------------------------------------------------------- 00169 QString Ellipse::save() 00170 { 00171 QString s = Name + 00172 QString::number(cx) + " " + QString::number(cy) + " " + 00173 QString::number(x2) + " " + QString::number(y2) + " " + 00174 Pen.color().name() + " " + QString::number(Pen.width()) + " " + 00175 QString::number(Pen.style()) + " " + 00176 Brush.color().name() + " " + QString::number(Brush.style()); 00177 if(filled) s += " 1"; 00178 else s += " 0"; 00179 return s; 00180 } 00181 00182 // -------------------------------------------------------------------------- 00183 QString Ellipse::saveCpp() 00184 { 00185 QString b = filled ? 00186 QString (", QBrush (QColor (\"%1\"), %2)"). 00187 arg(Brush.color().name()).arg(toBrushString(Brush.style())) : ""; 00188 QString s = 00189 QString ("new Area (%1, %2, %3, %4, " 00190 "QPen (QColor (\"%5\"), %6, %7)%8)"). 00191 arg(cx).arg(cy).arg(x2).arg(y2). 00192 arg(Pen.color().name()).arg(Pen.width()).arg(toPenString(Pen.style())). 00193 arg(b); 00194 s = "Ellips.append (" + s + ");"; 00195 return s; 00196 } 00197 00198 QString Ellipse::saveJSON() 00199 { 00200 QString b = filled ? 00201 QString ("\"colorfill\" : \"%1\", \"stylefill\" : \"%2\""). 00202 arg(Brush.color().name()).arg(toBrushString(Brush.style())) : ""; 00203 QString s = 00204 QString("{\"type\" : \"ellipse\", " 00205 "\"x\" : %1, \"y\" : %2, \"w\" : %3, \"h\" : %4," 00206 "\"color\" : \"%5\", \"thick\" : %6, \"style\" : \"%7\", %8},"). 00207 arg(cx).arg(cy).arg(x2).arg(y2). 00208 arg(Pen.color().name()).arg(Pen.width()).arg(toPenString(Pen.style())). 00209 arg(b); 00210 return s; 00211 } 00212 00213 // -------------------------------------------------------------------------- 00214 // Checks if the resize area was clicked. 00215 bool Ellipse::resizeTouched(float fX, float fY, float len) 00216 { 00217 float fCX = float(cx), fCY = float(cy); 00218 float fX2 = float(cx+x2), fY2 = float(cy+y2); 00219 00220 State = -1; 00221 if(fX < fCX-len) return false; 00222 if(fY < fCY-len) return false; 00223 if(fX > fX2+len) return false; 00224 if(fY > fY2+len) return false; 00225 00226 State = 0; 00227 if(fX < fCX+len) State = 1; 00228 else if(fX <= fX2-len) { State = -1; return false; } 00229 if(fY < fCY+len) State |= 2; 00230 else if(fY <= fY2-len) { State = -1; return false; } 00231 00232 return true; 00233 } 00234 00235 // -------------------------------------------------------------------------- 00236 // Mouse move action during resize. 00237 void Ellipse::MouseResizeMoving(int x, int y, Schematic *p) 00238 { 00239 paintScheme(p); // erase old painting 00240 switch(State) { 00241 case 0: x2 = x-cx; y2 = y-cy; // lower right corner 00242 break; 00243 case 1: x2 -= x-cx; cx = x; y2 = y-cy; // lower left corner 00244 break; 00245 case 2: x2 = x-cx; y2 -= y-cy; cy = y; // upper right corner 00246 break; 00247 case 3: x2 -= x-cx; cx = x; y2 -= y-cy; cy = y; // upper left corner 00248 break; 00249 } 00250 if(x2 < 0) { State ^= 1; x2 *= -1; cx -= x2; } 00251 if(y2 < 0) { State ^= 2; y2 *= -1; cy -= y2; } 00252 00253 paintScheme(p); // paint new painting 00254 } 00255 00256 // -------------------------------------------------------------------------- 00257 // fx/fy are the precise coordinates, gx/gy are the coordinates set on grid. 00258 // x/y are coordinates without scaling. 00259 void Ellipse::MouseMoving( 00260 Schematic *paintScale, int, int, int gx, int gy, 00261 Schematic *p, int x, int y, bool drawn) 00262 { 00263 if(State > 0) { 00264 if(State > 1) 00265 // _Ellipse hang/crash application, using _Arc solved, see bug 141 (closed) 00266 paintScale->PostPaintEvent(_Arc, x1, y1, x2-x1, y2-y1, 0, 16*360); // erase old painting 00267 State++; 00268 x2 = gx; 00269 y2 = gy; 00270 paintScale->PostPaintEvent(_Arc, x1, y1, x2-x1, y2-y1, 0, 16*360); 00271 } 00272 else { x2 = gx; y2 = gy; } 00273 00274 if(drawn) { 00275 p->PostPaintEvent(_Ellipse, cx+13, cy, 18, 12,0,0,true); // erase old cursor symbol 00276 if(filled) { 00277 p->PostPaintEvent(_Line, cx+14, cy+7, cx+20, cy+1,0,0,true); 00278 p->PostPaintEvent(_Line, cx+25, cy+2, cx+18, cy+9,0,0,true); 00279 p->PostPaintEvent(_Line, cx+29, cy+4, cx+23, cy+10,0,0,true); 00280 } 00281 } 00282 cx = x; 00283 cy = y; 00284 p->PostPaintEvent(_Ellipse, cx+13, cy, 18, 12,0,0,true); // paint new cursor symbol 00285 if(filled) { 00286 p->PostPaintEvent(_Line, cx+14, cy+7, cx+20, cy+1,0,0,true); 00287 p->PostPaintEvent(_Line, cx+25, cy+2, cx+18, cy+9,0,0,true); 00288 p->PostPaintEvent(_Line, cx+29, cy+4, cx+23, cy+10,0,0,true); 00289 } 00290 } 00291 00292 // -------------------------------------------------------------------------- 00293 bool Ellipse::MousePressing() 00294 { 00295 State++; 00296 if(State == 1) { 00297 x1 = x2; 00298 y1 = y2; // first corner is determined 00299 } 00300 else { 00301 if(x1 < x2) { cx = x1; x2 = x2-x1; } // cx/cy to upper left corner 00302 else { cx = x2; x2 = x1-x2; } 00303 if(y1 < y2) { cy = y1; y2 = y2-y1; } 00304 else { cy = y2; y2 = y1-y2; } 00305 x1 = y1 = 0; 00306 State = 0; 00307 return true; // painting is ready 00308 } 00309 return false; 00310 } 00311 00312 // -------------------------------------------------------------------------- 00313 // Checks if the coordinates x/y point to the painting. 00314 bool Ellipse::getSelected(float fX, float fY, float w) 00315 { 00316 float fX2 = float(x2); 00317 float fY2 = float(y2); 00318 fX -= float(cx) + fX2/2.0; 00319 fY -= float(cy) + fY2/2.0; 00320 00321 if(filled) { 00322 float a = 2.0 * fX / fX2; a *= a; 00323 float b = 2.0 * fY / fY2; b *= b; 00324 00325 if(a+b > 1.0) 00326 return false; 00327 } 00328 else { 00329 float a1 = fX / (fX2/2.0 - w); a1 *= a1; 00330 float a2 = fX / (fX2/2.0 + w); a2 *= a2; 00331 float b1 = fY / (fY2/2.0 - w); b1 *= b1; 00332 float b2 = fY / (fY2/2.0 + w); b2 *= b2; 00333 00334 if(a1+b1 < 1.0) return false; 00335 if(a2+b2 > 1.0) return false; 00336 } 00337 00338 return true; 00339 } 00340 00341 // -------------------------------------------------------------------------- 00342 // Rotates around the center. 00343 void Ellipse::rotate() 00344 { 00345 cy += (y2-x2) >> 1; 00346 cx += (x2-y2) >> 1; 00347 int tmp = x2; 00348 x2 = y2; 00349 y2 = tmp; 00350 } 00351 00352 // -------------------------------------------------------------------------- 00353 // Mirrors about center line. 00354 void Ellipse::mirrorX() 00355 { 00356 // nothing to do 00357 } 00358 00359 // -------------------------------------------------------------------------- 00360 // Mirrors about center line. 00361 void Ellipse::mirrorY() 00362 { 00363 // nothing to do 00364 } 00365 00366 // -------------------------------------------------------------------------- 00367 // Calls the property dialog for the painting and changes them accordingly. 00368 // If there were changes, it returns 'true'. 00369 bool Ellipse::Dialog() 00370 { 00371 bool changed = false; 00372 00373 FillDialog *d = new FillDialog(QObject::tr("Edit Ellipse Properties")); 00374 d->ColorButt->setPaletteBackgroundColor(Pen.color()); 00375 d->LineWidth->setText(QString::number(Pen.width())); 00376 d->StyleBox->setCurrentItem(Pen.style()-1); 00377 d->FillColorButt->setPaletteBackgroundColor(Brush.color()); 00378 d->FillStyleBox->setCurrentItem(Brush.style()); 00379 d->CheckFilled->setChecked(filled); 00380 d->slotCheckFilled(filled); 00381 00382 if(d->exec() == QDialog::Rejected) { 00383 delete d; 00384 return false; 00385 } 00386 00387 if(Pen.color() != d->ColorButt->paletteBackgroundColor()) { 00388 Pen.setColor(d->ColorButt->paletteBackgroundColor()); 00389 changed = true; 00390 } 00391 if(Pen.width() != d->LineWidth->text().toInt()) { 00392 Pen.setWidth(d->LineWidth->text().toInt()); 00393 changed = true; 00394 } 00395 if(Pen.style() != (Qt::PenStyle)(d->StyleBox->currentItem()+1)) { 00396 Pen.setStyle((Qt::PenStyle)(d->StyleBox->currentItem()+1)); 00397 changed = true; 00398 } 00399 if(filled != d->CheckFilled->isChecked()) { 00400 filled = d->CheckFilled->isChecked(); 00401 changed = true; 00402 } 00403 if(Brush.color() != d->FillColorButt->paletteBackgroundColor()) { 00404 Brush.setColor(d->FillColorButt->paletteBackgroundColor()); 00405 changed = true; 00406 } 00407 if(Brush.style() != d->FillStyleBox->currentItem()) { 00408 Brush.setStyle((Qt::BrushStyle)d->FillStyleBox->currentItem()); 00409 changed = true; 00410 } 00411 00412 delete d; 00413 return changed; 00414 }