File: G_label.cpp

package info (click to toggle)
kseg 0.4.0.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 3,160 kB
  • ctags: 2,052
  • sloc: cpp: 14,632; makefile: 10
file content (233 lines) | stat: -rw-r--r-- 5,600 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 *  KSeg
 *  Copyright (C) 1999-2006 Ilya Baran
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Send comments and/or bug reports to:
 *                 ibaran@mit.edu
 */


#include <qpixmap.h>

#include "G_label.H"
#include "G_drawstyle.H"
#include "G_ref.H"
#include "G_object.H"

void G_label::draw(QPainter &p, const G_drawstyle &d, bool selected)
{
  p.setFont(d.getFont());
  if(!selected) p.setPen(QPen(d.getPen().color()));
  else p.setPen(QPen(QColor(255, 0, 0)));

  G_point pt = getPos();

  if(!pt.isValid()) { output.setPos(0, 0);  output.redraw(p); return; }

  output.makeDirty();

  output.setPos(ROUND(pt.getX()), ROUND(pt.getY()));
  output.redraw(p);
}

void G_label::setString(const QString &inText) //assumes input in ugly form
{ 
  text = KFormula::fromUgly(inText);
  output.parse(text);

  QPixmap tmp(1,1);
  QPainter tmpp(&tmp);
  tmpp.setFont(ref->getDrawstyle()->getFont());
  output.redraw(tmpp);

  setPos(getPos());
}

void G_label::setText(const QString &inText, bool inUpdatePos) //for "internal use"
{ 
  text = inText; 
  output.parse(text); 
  
  QPixmap tmp(1,1);
  QPainter tmpp(&tmp);
  tmpp.setFont(ref->getDrawstyle()->getFont());
  output.redraw(tmpp);
  
  if(inUpdatePos)
    setPos(getPos());
}

QRect G_label::getRect()
{
  QRect tmp;

  tmp.setSize(getSize());
  tmp.moveCenter(getPos().toQPoint());

  return tmp;
}


QSize G_label::getSize()
{
  return output.size();
}


G_point G_label::getPos() const
{
  if(ref->getExists() == false) return G_point::inValid();
  if(ref->getType() & G_CURVE) {
    return relCoord + ref->getObject()->getCurveRef()->getPointOnCurve(relPos);
  }
  else if(ref->getType() == G_POINT) {
    return relCoord + ref->getObject()->getPoint();
  }
  else return G_point::inValid();
}


void G_label::setPos(const G_point &inPoint)
{
  int i;

  relPos = 0.5;
  relCoord = G_point(0, 0);

  if(!inPoint.isValid()) return;

  relCoord = inPoint - getPos();

  QRect r = getRect();

  //if the object is totally inside the label rectangle, we're all set.
  if(r.contains(ref->getObject()->getSelectExtents())) return;

  G_point minPoint, curPoint;
  G_point minOffset, curOffset;
  double minDist = 999999;

  curOffset = G_point(r.topLeft());

  int step = 3;

  //scan top side
  for(i = 0; i <= r.width(); i += step) {
    curPoint = ref->getObject()->getNearestPoint(curOffset);
    if((curPoint - curOffset).length() < minDist) {
      minDist = (curPoint - curOffset).length();
      minPoint = curPoint;
      minOffset = curOffset;
    }

    curOffset = curOffset + G_point(step, 0);
  }

  //scan right side
  for(i = 0; i <= r.height(); i += step) {
    curPoint = ref->getObject()->getNearestPoint(curOffset);
    if((curPoint - curOffset).length() < minDist) {
      minDist = (curPoint - curOffset).length();
      minPoint = curPoint;
      minOffset = curOffset;
    }

    curOffset = curOffset + G_point(0, step);
  }

  //scan bottom side
  for(i = 0; i <= r.width(); i += step) {
    curPoint = ref->getObject()->getNearestPoint(curOffset);
    if((curPoint - curOffset).length() < minDist) {
      minDist = (curPoint - curOffset).length();
      minPoint = curPoint;
      minOffset = curOffset;
    }

    curOffset = curOffset - G_point(step, 0);
  }

  //scan left side
  for(i = 0; i <= r.height(); i += step) {
    curPoint = ref->getObject()->getNearestPoint(curOffset);
    if((curPoint - curOffset).length() < minDist) {
      minDist = (curPoint - curOffset).length();
      minPoint = curPoint;
      minOffset = curOffset;
    }

    curOffset = curOffset - G_point(0, step);
  }

  G_point relc = minOffset - minPoint;

  if(minDist > LABEL_DISTANCE) {
    relc.normalize();
    relc = relc * LABEL_DISTANCE;
  }

  relCoord = relc + (G_point(r.center()) - minOffset);

  if(ref->getType() & G_CURVE) {
    relPos = ref->getObject()->getCurveRef()->getParamFromPoint(minPoint);
  }

  return;
}


QDataStream &operator<<(QDataStream &stream, G_label &label)
{
  QString tmp = label.text;

  //modify tmp so old versions can read it
  for(int i = 0; i < (int)tmp.length(); ++i)
    if(tmp[i].unicode() > UNUSED_OFFSET &&
       tmp[i].unicode() < UNUSED_OFFSET + 300)
      tmp[i] = QChar((int)(tmp[i].unicode()) +
		     (OLD_UNUSED_OFFSET - UNUSED_OFFSET));

  stream << tmp << label.relCoord;

  if(label.ref->getType() & G_CURVE) {
    stream << label.relPos;
  }

  return stream;
}


QDataStream &operator>>(QDataStream &stream, G_label &label)
{
  stream >> label.text >> label.relCoord;

  if(label.ref->getType() & G_CURVE) {
    stream >> label.relPos;
  }

  //modify text from old files
  for(int i = 0; i < (int)label.text.length(); ++i)
    if(label.text[i].unicode() > OLD_UNUSED_OFFSET &&
       label.text[i].unicode() < OLD_UNUSED_OFFSET + 300)
      label.text[i] = QChar(label.text[i].unicode() +
			    (UNUSED_OFFSET - OLD_UNUSED_OFFSET));

  label.output.parse(label.text);

  return stream;
}