image_region.cc 2.37 KB
Newer Older
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
//
// Document layout viewer.
//
// Copyright (C) 2009 Florent D'Halluin.
//
// 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.
//
// The complete GNU General Public Licence Notice can be found as the
// `COPYING' file in the root directory.
//

#include "image_region.hh"

ImageRegion::ImageRegion(region::RegionId id,
			 QString name,
			 QColor color,
			 QModelIndex index,
			 const QVector<QPoint>& points,
			 bool outline,
			 bool fill,
			 bool precise,
			 bool draw)
  : QGraphicsItem(0),
    id_(id),
    name_(name),
    color_(color),
    index_(index),
    shape_(),
    rect_(),
    outline_(outline),
    precise_(precise),
    fill_(fill),
    draw_(draw),
    selected_(false),
    alpha_(30)
{
  setCursor(Qt::ArrowCursor);
  setZValue(1);

  if (points.size() == 0)
    return;

  for (int i = 1; i < points.size(); ++i)
    shape_.lineTo(points[i] - points[0]);
  shape_.lineTo(QPoint(0, 0));

  rect_ = shape_.boundingRect();
Florent D'Halluin's avatar
Florent D'Halluin committed
51
  setPos(points[0] + QPointF(0.5, 0.5));
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

  prepareGeometryChange();
}

ImageRegion::~ImageRegion()
{
}

void
ImageRegion::paint(QPainter* painter,
		   const QStyleOptionGraphicsItem*,
		   QWidget*)
{
  if (!draw_ && !selected_)
    return;

  int width = 0;
  if (precise_)
    width = 1;

  if (outline_)
Florent D'Halluin's avatar
Florent D'Halluin committed
73
74
    painter->setPen(QPen(QBrush(color_), width, Qt::SolidLine,
			 Qt::SquareCap, Qt::MiterJoin));
75
76
77
78
79
80
81
82
83
84
85
  else
    painter->setPen(QColor(0, 0, 0, 0));

  QColor brush = color_;
  if (fill_)
    brush.setAlpha(alpha_);
  else
    brush.setAlpha(0);
  painter->setBrush(brush);

  painter->drawPath(shape_);
Florent D'Halluin's avatar
Florent D'Halluin committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

  if (selected_)
  {
    QPolygonF sceneRect = mapFromScene(scene()->sceneRect());
    QPainterPath path;
    path.addPolygon(sceneRect);
    QColor brush(255, 255, 255);
    brush.setAlpha(120);
    painter->setBrush(brush);
    painter->setPen(QColor(0, 0, 0, 0));
    painter->drawPath(path.subtracted(shape_));
  }
}

void
ImageRegion::select()
{
  if (!selected_)
  {
    selected_ = true;
    setZValue(2);
Florent D'Halluin's avatar
Florent D'Halluin committed
107
    prepareGeometryChange();
Florent D'Halluin's avatar
Florent D'Halluin committed
108
109
110
111
112
113
114
115
116
117
118
119
    scene()->invalidate();
    update();
  }
}

void
ImageRegion::deselect()
{
  if (selected_)
  {
    selected_ = false;
    setZValue(1);
Florent D'Halluin's avatar
Florent D'Halluin committed
120
121
    prepareGeometryChange();
    scene()->invalidate();
Florent D'Halluin's avatar
Florent D'Halluin committed
122
123
    update();
  }
124
}