File: G_drawstyle.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 (164 lines) | stat: -rw-r--r-- 4,434 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
/*
 *  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 "G_drawstyle.H"
#include "G_refs.H"
#include "G_ref.H"
#include "KSegProperties.H"

QPtrList<G_drawstyle> G_drawstyle::styles;

G_drawstyle *G_drawstyle::match(PointStyle point, const QFont *f,
				const QPen *p, const QBrush *b)
{
  G_drawstyle *newDrawstyle;

  QPtrListIterator<G_drawstyle> iter(styles);

  for(; iter.current() != NULL; ++iter) {
    if(point != ANY && point != iter.current()->pointStyle) continue;
    if(f != 0 && *f != iter.current()->font) continue;
    if(p != 0 && *p != iter.current()->pen) continue;
    if(b != 0 && *b != iter.current()->brush) continue;
    //if we are here, we found a match.  Add a reference to it and return it.

    iter.current()->num_refs++;
    return iter.current();    
  }

  //if we are here, we need to create a new drawstyle.

  newDrawstyle = new G_drawstyle();
  if(point != ANY) newDrawstyle->pointStyle = point; else newDrawstyle->pointStyle = LARGE_CIRCLE;
  if(f != 0) newDrawstyle->font = *f; else newDrawstyle->font = QFont("Arial", 20);
  if(p != 0) newDrawstyle->pen = *p; else newDrawstyle->pen = QPen();
  if(b != 0) newDrawstyle->brush = *b; else newDrawstyle->brush =
					      QBrush(Qt::black, Qt::Dense4Pattern);

  return newDrawstyle;
}

G_drawstyle *G_drawstyle::defaultStyle() {
  QFont f("Arial", 20);
  QPen p;
  QBrush b(Qt::black, Qt::Dense4Pattern);

  return match(LARGE_CIRCLE, &f, &p, &b);
}

void G_drawstyle::deleteReference() // do not use the object after calling this guy.
{
  num_refs--;

  if(num_refs == 0) {
    styles.remove(this);
    delete this;
  }
}

G_drawstyle::G_drawstyle()
{
  num_refs = 1;

  pointStyle = SMALL_CIRCLE;

  styles.append(this);
}


QMap<G_drawstyle *, short> G_drawstyle::saveUsed(QDataStream &stream, G_refs &refs)
{
  QMap<G_drawstyle *, short> toBeReturned;
  std::vector<G_drawstyle *> usedDrawstyles;

  int i;
  int numStyles = 0;

  //make the map first
  for(i = 0; i < (int)refs.count(); i++) {
    if(!(toBeReturned.contains(refs[i]->getDrawstyle()))) {
      toBeReturned.insert(refs[i]->getDrawstyle(), numStyles++);
      usedDrawstyles.push_back(refs[i]->getDrawstyle());
    }
  }

  ASSERT(numStyles < 32767); 

  //now save all the drawstyles in the map:
  stream << (short)numStyles;

  for(i = 0; i < numStyles; ++i) {
    G_drawstyle *tmp = usedDrawstyles[i];

    stream << (Q_INT8)tmp->pointStyle;
    stream << tmp->font;
    stream << tmp->pen;
    stream << tmp->brush;
  }

  return toBeReturned;
}

QMemArray<G_drawstyle *> G_drawstyle::loadUsed(QDataStream &stream)
{
  QMemArray<G_drawstyle *> toBeReturned;

  short numStyles;
  int i;

  stream >> numStyles;

  toBeReturned.resize(numStyles);

  for(i = 0; i < numStyles; i++) {
    Q_INT8 pointStyle;
    QFont font;
    QBrush brush;
    QPen pen;

    stream >> pointStyle >> font >> pen >> brush;

    toBeReturned[i] = match((PointStyle)pointStyle, &font, &pen, &brush);
  }

  return toBeReturned;
}

QColor G_drawstyle::getBorderColor(const QColor &drawColor)
{
  QColor bg = QColor(KSegProperties::getProperty("BackgroundColor"));

  int rbg, gbg, bbg, rdc, gdc, bdc;

  bg.rgb(&rbg, &gbg, &bbg);
  drawColor.rgb(&rdc, &gdc, &bdc);

  int dred = QMIN(SQR(rbg - 255) + SQR(gbg) + SQR(bbg), SQR(rdc - 255) + SQR(gdc) + SQR(bdc));
  int dblack = QMIN(SQR(rbg) + SQR(gbg) + SQR(bbg), SQR(rdc) + SQR(gdc) + SQR(bdc));
  int dwhite = QMIN(SQR(rbg - 255) + SQR(gbg - 255) + SQR(bbg - 255), \
		    SQR(rdc - 255) + SQR(gdc - 255) + SQR(bdc - 255));
  
  if(dred > dblack && dred > dwhite) return Qt::red;
  if(dblack > dwhite) return Qt::black;
  return Qt::white;
}