orange / source / orangeqt / point.cpp

  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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
    This file is part of the plot module for Orange
    Copyright (C) 2011  Miha Čančula <miha@noughmad.eu>

    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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include "point.h"
#include "curve.h"

#include <QtGui/QPainter>
#include <QtCore/QDebug>
#include <QtCore/qmath.h>
#include <QtGui/QStyleOptionGraphicsItem>

QHash<PointData, QPixmap> Point::pixmap_cache;

uint qHash(const PointData& data)
{
    // uint has 32 bits:
    uint ret = data.size;
    // size only goes up to 20, so 5 bits is enough
    ret |= data.symbol << 5;
    // symbol is less than 16, so 4 bits will do
    ret |= data.state << 9;
    // state is currently only two bits
    ret |= data.transparent << 11;
    // QRgb takes the full uins, so we just XOR by it
    ret ^= data.color.rgba();
    return ret;
}

bool operator==(const PointData& one, const PointData& other)
{
    return one.symbol == other.symbol && one.size == other.size && one.state == other.state && one.color == other.color;
}

QDebug& operator<<(QDebug& stream, const DataPoint& point)
{
	return stream.maybeSpace() << "DataPoint(" << point.x << ','<< point.y << ')';
}

bool operator==(const DataPoint& one, const DataPoint& other)
{
    return one.x == other.x && one.y == other.y;
}

DataPoint::operator QPointF() const
{
    return QPointF(x, y);
}

LabelItem::LabelItem(QGraphicsItem* parent): QGraphicsTextItem(parent)
{
}

LabelItem::LabelItem(const QString &text, QGraphicsItem *parent): QGraphicsTextItem(text, parent)
{
}

LabelItem::~LabelItem()
{
}

void LabelItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	/*
    Q_UNUSED(widget);
    if (dd->control) {
        painter->save();
        QRectF r = option->exposedRect;
        painter->translate(-dd->controlOffset());
        r.translate(dd->controlOffset());

        QTextDocument *doc = dd->control->document();
        QTextDocumentLayout *layout = qobject_cast<QTextDocumentLayout *>(doc->documentLayout());

        // the layout might need to expand the root frame to
        // the viewport if NoWrap is set
        if (layout)
            layout->setViewport(dd->boundingRect);

        dd->control->drawContents(painter, r);

        if (layout)
            layout->setViewport(QRect());

        painter->restore();
    }

    if (option->state & (QStyle::State_Selected | QStyle::State_HasFocus))
        qt_graphicsItem_highlightSelected(this, painter, option);
    */
	QGraphicsTextItem::paint(painter, option, widget);
}

Point::Point(int symbol, QColor color, int size, QGraphicsItem* parent): QGraphicsObject(parent),
 m_symbol(symbol),
 m_color(color),
 m_size(size)
{
    m_display_mode = DisplayPath;
    m_transparent = true;
    setFlag(ItemIgnoresTransformations);
    label = NULL;
}

Point::Point(QGraphicsItem* parent): QGraphicsObject(parent)
{
    m_symbol = Ellipse;
    m_color = Qt::black;
    m_size = 5;
    m_display_mode = DisplayPath;
    m_transparent = true;
    setFlag(ItemIgnoresTransformations);
    label = NULL;
}
  
void Point::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
    Q_UNUSED(option)
    Q_UNUSED(widget)
    
    const PointData key(m_size, m_symbol, m_color, m_state, m_transparent);
    // We make the pixmap slighly larger because the point outline has non-zero width
    const int ps = m_size + 4;
    if (!pixmap_cache.contains(key))
    {
        if (m_display_mode == DisplayPath)
        {
            QBrush brush(m_color);
            QPixmap pixmap(ps, ps);
            pixmap.fill(Qt::transparent);
            QPainter p;
            
            QPen pen(m_color);
            pen.setWidth(qMin(2, m_size/6));
            
            p.begin(&pixmap);
            p.setRenderHints(painter->renderHints() | QPainter::Antialiasing);
            if (m_state & Selected)
            {
                brush.setColor(m_color);
                //pen.setStyle(Qt::NoPen);
            }
            else if (m_state & Marked)
            {
                /*
            	QRadialGradient g(0.5*ps, 0.5*ps, 0.5*m_size);
                g.setColorAt(0, m_color);
                g.setColorAt(0.5, Qt::transparent);
                g.setColorAt(1, m_color);
                brush = QBrush(g);
                pen.setStyle(Qt::NoPen);
                */
            	//QColor c = Qt::darkGray;
            	//c.setAlpha(150);
            	QColor c = brush.color();
				//c.setAlpha(m_color.alpha()/6);
				brush.setColor(c);
				pen.setColor(Qt::black);
				pen.setWidth(qMin(3, m_size/3));

            }
            else
            {
                QColor c = brush.color();
                c.setAlpha(m_color.alpha()/6);
                brush.setColor(c);
            }
            const QPainterPath path = path_for_symbol(m_symbol, m_size).translated(0.5*ps, 0.5*ps);

            if (!m_transparent)
            {
            	p.setBrush(Qt::white);
            	p.drawPath(path);
            }

            p.setBrush(brush);
            p.setPen(pen);
            p.drawPath(path);
            pixmap_cache.insert(key, pixmap);
        } 
        else if (m_display_mode == DisplayPixmap)
        {
            pixmap_cache.insert(key, pixmap_for_symbol(m_symbol, m_color, m_size));
        }
    }
    painter->drawPixmap(QPointF(-0.5*ps, -0.5*ps), pixmap_cache.value(key));
    /*
    if (!m_label.isEmpty())
    {        
        QFontMetrics metrics = option->fontMetrics;
        int th = metrics.height();
        int tw = metrics.width(m_label);
        QRect r(-tw/2, 0, tw, th);
        //painter->fillRect(r, QBrush(Qt::white));
        painter->drawText(r, Qt::AlignHCenter, m_label);
    }
    */
}

QRectF Point::boundingRect() const
{
    return rect_for_size(m_size);
}

Point::~Point()
{
}

QColor Point::color() const
{
    return m_color;
}

void Point::set_color(const QColor& color)
{
    m_color = color;
    update();
}

Point::DisplayMode Point::display_mode() const
{
    return m_display_mode;
}

void Point::set_display_mode(Point::DisplayMode mode)
{
    m_display_mode = mode;
    update();
}

int Point::size() const
{
    return m_size;
}

void Point::set_size(int size)
{
    m_size = size;
    update();
}

int Point::symbol() const
{
    return m_symbol;
}

void Point::set_symbol(int symbol)
{
    m_symbol = symbol;
    update();
}

QPainterPath Point::path_for_symbol(int symbol, int size)
{
  QPainterPath path;
  qreal d = 0.5 * size;
  switch (symbol)
  {
    case NoSymbol:
      break;
      
    case Ellipse:
      path.addEllipse(-d,-d,2*d,2*d);
      break;
      
    case Rect:
      path.addRect(-d,-d,2*d,2*d);
      break;
      
    case Diamond:
      path.addRect(-d,-d,2*d,2*d);
      path = QTransform().rotate(45).map(path);
      break;
      
    case Triangle:
    case UTriangle:
      path = trianglePath(d, 0);
      break;
      
    case DTriangle:
      path = trianglePath(d, 180);
      break;
      
    case LTriangle:
      path = trianglePath(d, -90);
      break;
    
    case RTriangle:
      path = trianglePath(d, 90);
      break;

    case Cross:
      path = crossPath(d, 0);
      break;
    
    case XCross:
      path = crossPath(d, 45);
      break;
      
    case HLine:
      path.moveTo(-d,0);
      path.lineTo(d,0);
      break;
      
    case VLine:
      path.moveTo(0,-d);
      path.lineTo(0,d);
      break;
      
    case Star1:
      path.addPath(crossPath(d,0));
      path.addPath(crossPath(d,45));
      break;
      
    case Star2:
      path = hexPath(d, true);
      break;
      
    case Hexagon:
      path = hexPath(d, false);
      break;
      
    default:
     // qWarning() << "Unsupported symbol" << symbol;
        break;
  }
  return path;
}


QPainterPath Point::trianglePath(double d, double rot) {
    QPainterPath path;
    path.moveTo(-d, d*sqrt(3.0)/3);
    path.lineTo(d, d*sqrt(3.0)/3);
    path.lineTo(0, -2*d*sqrt(3.0)/3);
    path.closeSubpath();
    return QTransform().rotate(rot).map(path);
}

QPainterPath Point::crossPath(double d, double rot)
{
    QPainterPath path;
    path.lineTo(0,d);
    path.moveTo(0,0);
    path.lineTo(0,-d);
    path.moveTo(0,0); 
    path.lineTo(d,0);
    path.moveTo(0,0);
    path.lineTo(-d,0);
    return QTransform().rotate(rot).map(path);
}

QPainterPath Point::hexPath(double d, bool star) {
    QPainterPath path;
    if (!star)
    {
        path.moveTo(d,0);
    }
    for (int i = 0; i < 6; ++i)
    {
        path.lineTo( d * cos(M_PI/3*i), d*sin(M_PI/3*i) );
        if (star)
        {
            path.lineTo(0,0);
        }
    }
    path.closeSubpath();
    return path;
}

QPixmap Point::pixmap_for_symbol(int symbol, QColor color, int size)
{
    // Indexed8 is the only format with a color table, which means we can replace entire colors
    // and not only indididual pixels
    QPixmap image(QSize(size, size));
    
    // TODO: Create fils with actual images, preferably SVG so they are scalable
    // image.load(filename);
    return image;
}

QRectF Point::rect_for_size(double size)
{
    return QRectF(-size/2, -size/2, size, size);
}

void Point::set_state(Point::State state) 
{
    m_state = state;
}

Point::State Point::state() const 
{
    return m_state;
}

void Point::set_state_flag(Point::StateFlag flag, bool on) 
{
    if (on)
    {
        m_state |= flag;
    }
    else
    {
        m_state &= ~flag;
    }

    if ((flag == Selected || flag == Marked) && label && ((Curve *)parentObject())->labels_on_marked())
    {
		if (is_selected() || is_marked())
		{
			label->show();
		}
		else
		{
			label->hide();
		}
    }

    update();
}

bool Point::state_flag(Point::StateFlag flag) const 
{
    return m_state & flag;
}

void Point::set_selected(bool selected)
{
    set_state_flag(Selected, selected);
}

bool Point::is_selected() const
{
    return state_flag(Selected);
}

void Point::set_marked(bool marked)
{
    set_state_flag(Marked, marked);
}

bool Point::is_marked() const
{
    return state_flag(Marked);
}

bool Point::is_transparent()
{
    return m_transparent;
}
void Point::set_transparent(bool transparent)
{
    m_transparent = transparent;
}

DataPoint Point::coordinates() const
{
    return m_coordinates;
}

void Point::set_coordinates(const DataPoint& data_point)
{
    m_coordinates = data_point;
}

void Point::clear_cache()
{
    pixmap_cache.clear();
}

/*
void Point::set_label(const QString& label)
{
    m_label = label;
}
*/

QString Point::text() const
{
    if (label == NULL)
    {
        return "";
    }
    else
    {
        return label->toPlainText();
    }
}

#include "point.moc"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.