Qucs-GUI  0.0.18
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
graphictext.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  graphictext.cpp
3  -----------------
4  begin : Mon Nov 24 2003
5  copyright : (C) 2003 by Michael Margraf
6  email : michael.margraf@alumni.tu-berlin.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #include <QtGui>
18 #include "main.h"
19 #include "mnemo.h"
20 #include "viewpainter.h"
21 #include "graphictext.h"
22 #include "graphictextdialog.h"
23 #include "schematic.h"
24 #include "qucs.h"
25 #include <QWidget>
26 #include <QPainter>
27 #include <QLineEdit>
28 #include <Q3TextEdit>
29 #include <QPushButton>
30 
31 #include <math.h>
32 
33 
35 {
36  Name = "Text ";
37  isSelected = false;
38  Color = QColor(0,0,0);
40  cx = cy = 0;
41  x1 = x2 = 0;
42  y1 = y2 = 0;
43  Angle = 0;
44 }
45 
47 {
48 }
49 
50 // -----------------------------------------------------------------------
52 {
53  // keep track of painter state
54  p->Painter->save();
55 
56  QMatrix wm = p->Painter->worldMatrix();
57  QMatrix Mat(1.0, 0.0, 0.0, 1.0, p->DX + float(cx) * p->Scale,
58  p->DY + float(cy) * p->Scale);
59  p->Painter->setWorldMatrix(Mat);
60  p->Painter->rotate(-Angle); // automatically enables transformation
61 
62  int Size = Font.pointSize();
63  Font.setPointSizeFloat( p->FontScale * float(Size) );
64 
65  QFont f = p->Painter->font();
66  p->Painter->setPen(Color);
67  p->Painter->setFont(Font);
68 
69  // Because of a bug in Qt 3.1, drawing this text is dangerous, if it
70  // contains linefeeds. Qt has problems with linefeeds. It remembers the
71  // last font metrics (within the font ???) and does not calculate it again.
72  // The error often appears at a very different drawText function !!!
73  int w, h;
74  w = p->drawTextMapped(Text, 0, 0, &h);
75 
76  if(isSelected) {
77  p->Painter->setPen(QPen(Qt::darkGray,3));
78  p->Painter->drawRect(-3, -2, w+6, h+5);
79  }
80 
81  Font.setPointSize(Size); // restore real font size
82  p->Painter->setWorldMatrix(wm);
83  p->Painter->setWorldXForm(false);
84 
85  // restore painter state
86  p->Painter->restore();
87 
88  x2 = int(float(w) / p->Scale);
89  y2 = int(float(h) / p->Scale);
90  p->Painter->setFont(f);
91 }
92 
93 // -----------------------------------------------------------------------
95 {
96  // FIXME #warning QMatrix wm = p->worldMatrix();
97  // FIXME #warning QMatrix Mat (wm.m11(), 0.0, 0.0, wm.m22(),
98 // FIXME #warning wm.dx() + double(cx) * wm.m11(),
99 // FIXME #warning wm.dy() + double(cy) * wm.m22());
100  // FIXME #warning p->setWorldMatrix(Mat);
101  // FIXME #warning p->rotate(-Angle);
102  p->PostPaintEvent(_Rect, 0, 0, x2, y2);
103 
104  // FIXME #warning p->setWorldMatrix(wm);
105 }
106 
107 // ------------------------------------------------------------------------
108 void GraphicText::getCenter(int& x, int &y)
109 {
110  x = cx+(x2>>1);
111  y = cy+(y2>>1);
112 }
113 
114 // -----------------------------------------------------------------------
115 // Sets the center of the painting to x/y.
116 void GraphicText::setCenter(int x, int y, bool relative)
117 {
118  if(relative) { cx += x; cy += y; }
119  else { cx = x-(x2>>1); cy = y-(y2>>1); }
120 }
121 
122 // -----------------------------------------------------------------------
124 {
125  return new GraphicText();
126 }
127 
128 // --------------------------------------------------------------------------
129 Element* GraphicText::info(QString& Name, char* &BitmapFile, bool getNewOne)
130 {
131  Name = QObject::tr("Text");
132  BitmapFile = (char *) "text";
133 
134  if(getNewOne) return new GraphicText();
135  return 0;
136 }
137 
138 // -----------------------------------------------------------------------
139 bool GraphicText::load(const QString& s)
140 {
141  bool ok;
142 
143  QString n;
144  n = s.section(' ',1,1); // cx
145  cx = n.toInt(&ok);
146  if(!ok) return false;
147 
148  n = s.section(' ',2,2); // cy
149  cy = n.toInt(&ok);
150  if(!ok) return false;
151 
152  n = s.section(' ',3,3); // Size
153  Font.setPointSize(n.toInt(&ok));
154  if(!ok) return false;
155 
156  n = s.section(' ',4,4); // Color
157  Color.setNamedColor(n);
158  if(!Color.isValid()) return false;
159 
160  n = s.section(' ',5,5); // Angle
161  Angle = n.toInt(&ok);
162  if(!ok) return false;
163 
164  Text = s.mid(s.find('"')+1); // Text (can contain " !!!)
165  Text.truncate(Text.length()-1);
166  if(Text.isEmpty()) return false;
167 
169  QFontMetrics metrics(((Schematic*)QucsMain->DocumentTab->currentPage())->font()); // get size of text
170  QSize r = metrics.size(0, Text); // get size of text
171  x2 = r.width();
172  y2 = r.height();
173 
174  return true;
175 }
176 
177 // -----------------------------------------------------------------------
179 {
180  QString t = Text;
181  convert2ASCII(t);
182 
183  // The 'Text' property has to be the last within the line !
184  QString s = Name+QString::number(cx)+" "+QString::number(cy)+" "
185  + QString::number(Font.pointSize())+" "+Color.name()+" "
186  + QString::number(Angle) + " \""+t+"\"";
187  return s;
188 }
189 
190 // --------------------------------------------------------------------------
192 {
193  QString t = Text;
194  convert2ASCII(t);
195 
196  QString s =
197  QString ("new Text (%1, %2, \"%3\", QColor (\"%4\"), %5, %6, %7)").
198  arg(cx).arg(cy).arg(t).
199  arg(Color.name()).arg(Font.pointSize()).
200  arg(cos(M_PI * Angle / 180.0)).arg(sin(M_PI * Angle / 180.0));
201  s = "Texts.append (" + s + ");";
202  return s;
203 }
204 
206 {
207  QString t = Text;
208  convert2ASCII(t);
209 
210  QString s =
211  QString ("{\"type\" : \"graphictext\", "
212  "\"x\" : %1, \"y\" : %2, \"s\" : \"%3\", "
213  "\"color\" : \"%4\", \"size\" : %5, \"cos\" : %6, \"sin\" : %7},").
214  arg(cx).arg(cy).arg(t).
215  arg(Color.name()).arg(Font.pointSize()).
216  arg(cos(M_PI * Angle / 180.0)).arg(sin(M_PI * Angle / 180.0));
217  return s;
218 }
219 
220 // -----------------------------------------------------------------------
221 // fx/fy are the precise coordinates, gx/gy are the coordinates set on grid.
222 // x/y are coordinates without scaling.
224  Schematic*, int, int, int gx, int gy,
225  Schematic *p, int x, int y, bool drawn)
226 {
227  // FIXME #warning p->setPen(Qt::SolidLine);
228  if(drawn) {
229  p->PostPaintEvent(_Line, x1+15, y1+15, x1+20, y1,0,0,true); // erase old cursor symbol
230  p->PostPaintEvent(_Line, x1+26, y1+15, x1+21, y1,0,0,true);
231  p->PostPaintEvent(_Line, x1+17, y1+8, x1+23, y1+8,0,0,true);
232  }
233  x1 = x;
234  y1 = y;
235  p->PostPaintEvent(_Line, x1+15, y1+15, x1+20, y1,0,0,true); // paint new cursor symbol
236  p->PostPaintEvent(_Line, x1+26, y1+15, x1+21, y1,0,0,true);
237  p->PostPaintEvent(_Line, x1+17, y1+8, x1+23, y1+8,0,0,true);
238 
239  cx = gx;
240  cy = gy;
241 }
242 
243 // ------------------------------------------------------------------------
245 {
246  return Dialog();
247 }
248 
249 // ------------------------------------------------------------------------
250 // Checks if the coordinates x/y point to the painting.
251 // 5 is the precision the user must point onto the painting.
252 bool GraphicText::getSelected(float fX, float fY, float)
253 {
254  double phi = M_PI/180.0*double(Angle);
255  float sine = sin(phi), cosine = cos(phi);
256 
257  fX -= float(cx);
258  fY -= float(cy);
259  int _x = int( fX*cosine - fY*sine );
260  int _y = int( fY*cosine + fX*sine );
261 
262  if(_x >= 0) if(_y >= 0) if(_x <= x2) if(_y <= y2)
263  return true;
264 
265  return false;
266 }
267 
268 // ------------------------------------------------------------------------
269 void GraphicText::Bounding(int& xmin, int& ymin, int& xmax, int& ymax)
270 {
271  double phi = M_PI/180.0*double(Angle);
272  double sine = sin(phi), cosine = cos(phi);
273  int dx = int( double(y2) * sine );
274  int dy = int( double(y2) * cosine );
275  xmin = dx; xmax = cx;
276  ymin = dy; ymax = cy;
277  if(xmin < 0) xmin += cx;
278  else { xmax += xmin; xmin = cx; }
279  if(ymin < 0) ymin += cy;
280  else { ymax += ymin; ymin = cy; }
281 
282  int x = cx + int( double(x2) * cosine );
283  if(xmax < x) xmax = x;
284  else if(xmin > x) xmin = x;
285  x += dx;
286  if(xmax < x) xmax = x;
287  else if(xmin > x) xmin = x;
288 
289  int y = cy - int( double(x2) * sine );
290  if(ymax < y) ymax = y;
291  else if(ymin > y) ymin = y;
292  y += dy;
293  if(ymax < y) ymax = y;
294  else if(ymin > y) ymin = y;
295 }
296 
297 // -----------------------------------------------------------------------
298 // Rotates around the center.
300 {
301  Angle += 90;
302  Angle %= 360;
303  cx -= x2 >> 1;
304  cy -= y2 >> 1;
305 }
306 
307 // -----------------------------------------------------------------------
308 // Mirrors about center line.
310 { // do not mirror, because unreadable
311 }
312 
313 // -----------------------------------------------------------------------
314 // Mirrors about center line.
316 { // do not mirror, because unreadable
317 }
318 
319 // -----------------------------------------------------------------------
320 // Calls the property dialog for the painting and changes them accordingly.
321 // If there were changes, it returns 'true'.
323 {
324  QFont f(QucsSettings.font); // to avoid wrong text width
325  bool changed = false;
326 
328  d->ColorButt->setPaletteBackgroundColor(Color);
329  d->TextSize->setText(QString::number(Font.pointSize()));
330  d->Angle->setText(QString::number(Angle));
331  QString _Text = Text;
332  decode_String(_Text); // replace special characters with LaTeX commands
333  d->text->setText(_Text);
334 
335  if(d->exec() == QDialog::Rejected) {
336  delete d;
337  return false;
338  }
339 
340  if(Color != d->ColorButt->paletteBackgroundColor()) {
341  Color = d->ColorButt->paletteBackgroundColor();
342  changed = true;
343  }
344  f.setPointSize(d->TextSize->text().toInt()); // to avoid wrong text width
345  if(Font.pointSize() != d->TextSize->text().toInt()) {
346  Font.setPointSize(d->TextSize->text().toInt());
347  changed = true;
348  }
349  int tmp = d->Angle->text().toInt();
350  if(Angle != tmp) {
351  Angle = tmp % 360;
352  changed = true;
353  }
354 
355  encode_String(d->text->text(), _Text); // create special characters
356  if(!_Text.isEmpty())
357  if(_Text != Text) {
358  Text = _Text;
359  changed = true;
360  }
361  QFontMetrics m(((Schematic*)QucsMain->DocumentTab->currentPage())->font()); // get size of text
362 // FIXME #warning is this the right way? it was this: QFontMetrics m(f);
363  QSize s = m.size(0, Text); // get size of text
364  x2 = s.width();
365  y2 = s.height();
366 
367  delete d;
368  return changed;
369 }
bool getSelected(float, float, float)
QPainter * Painter
Definition: viewpainter.h:58
void mirrorX()
bool MousePressing()
bool load(const QString &)
int y1
Definition: element.h:153
void PostPaintEvent(PE pe, int x1=0, int y1=0, int x2=0, int y2=0, int a=0, int b=0, bool PaintOnViewport=false)
Definition: schematic.cpp:533
void paintScheme(Schematic *)
Definition: graphictext.cpp:94
QFont font
Definition: main.h:46
float Scale
Definition: viewpainter.h:59
void MouseMoving(Schematic *, int, int, int, int, Schematic *, int, int, bool)
tQucsSettings QucsSettings
Definition: main.cpp:52
int y2
Definition: element.h:153
int x1
Definition: element.h:153
void convert2Unicode(QString &Text)
Definition: main.cpp:416
QString saveCpp()
void mirrorY()
QString Text
Definition: graphictext.h:52
Definitions and declarations for the main application.
void setCenter(int, int, bool relative=false)
QucsApp * QucsMain
Definition: main.cpp:54
int cx
Definition: element.h:153
void Bounding(int &, int &, int &, int &)
QString saveJSON()
void encode_String(const QString &Input, QString &Output)
Definition: mnemo.cpp:58
bool isSelected
Definition: element.h:151
QString Name
Definition: painting.h:55
QFont Font
Definition: graphictext.h:51
Definition: element.h:82
int drawTextMapped(const QString &, int, int, int *Height=0)
void getCenter(int &, int &)
QColor Color
Definition: graphictext.h:50
Superclass of all schematic drawing elements.
Definition: element.h:142
void convert2ASCII(QString &Text)
Definition: main.cpp:433
QTabWidget * DocumentTab
Definition: qucs.h:164
int cy
Definition: element.h:153
QString save()
void decode_String(QString &Output)
Definition: mnemo.cpp:82
QPushButton * ColorButt
#define M_PI
Definition: diagramdialog.h:25
float FontScale
Definition: viewpainter.h:59
void paint(ViewPainter *)
Definition: graphictext.cpp:51
int x2
Definition: element.h:153
Painting * newOne()
static Element * info(QString &, char *&, bool getNewOne=false)