-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFrame.cpp
124 lines (108 loc) · 2.82 KB
/
Frame.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
#include "Frame.h"
#include <QPainter>
Frame::Frame(QGraphicsScene* s, QImage i, QColor bgCol, QImage tBg, bool frameBgTransparent)
{
scene = s;
img = i;
frameBgCol = bgCol;
transparentBg = tBg;
x = y = 0;
selected = false;
bgTransparent = frameBgTransparent;
bVisible = true;
//Add image
item = scene->addPixmap(QPixmap::fromImage(img));
//Add bg rect
bg = scene->addRect(0, 0, img.width(), img.height(), QPen(Qt::NoPen), QBrush(frameBgCol));
bg->setZValue(-1); //Behind images
//Add fg rect
fg = scene->addRect(0, 0, img.width(), img.height(), QPen(QColor(0,0,255), FRAME_SELECT_THICKNESS), QBrush(QColor(0,0,255,120)));
fg->setZValue(1); //In front of images
fg->setVisible(false);
}
Frame::~Frame()
{
scene->removeItem(item);
scene->removeItem(fg);
scene->removeItem(bg);
}
void Frame::setPosition(int xPos, int yPos)
{
x = xPos;
y = yPos;
item->setPos(x, y);
bg->setRect(x, y, img.width(), img.height());
fg->setRect(x, y, img.width(), img.height());
}
void Frame::setFrameBgCol(QColor c)
{
frameBgCol = c;
if(!bgTransparent)
{
QBrush brush(c);
bg->setBrush(brush);
}
}
void Frame::resize(int w, int h, BalancePos::Pos vert, BalancePos::Pos horiz)
{
//Use vert/horiz
int xPos, yPos;
if(vert == BalancePos::Up)
yPos = 0;
else if(vert == BalancePos::Mid)
yPos = (h/2)-(img.height()/2);
else
yPos = h - img.height();
if(horiz == BalancePos::Left)
xPos = 0;
else if(horiz == BalancePos::Mid)
xPos = (w/2)-(img.width()/2);
else
xPos = w - img.width();
QImage final = QImage(w, h, QImage::Format_ARGB32);
final.fill(QColor(0,0,0,0));
QPainter painter(&final);
painter.drawImage(xPos, yPos, img);
painter.end();
item->setPixmap(QPixmap::fromImage(final));
img = final;
//Reset frame rects
bg->setRect(x, y, w, h);
fg->setRect(x, y, w, h);
}
void Frame::setFrameBgVisible(bool b)
{
bg->setVisible(b);
}
void Frame::setFrameBgTransparent(bool b)
{
bgTransparent = b;
if(b)
bg->setBrush(QBrush(transparentBg));
else
bg->setBrush(QBrush(frameBgCol));
}
void Frame::selectToggle()
{
selected = !selected;
fg->setVisible(selected);
}
void Frame::render(QPainter& painter)
{
//Fill in bg highlight col if we should
if(!bgTransparent)
{
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
painter.fillRect(x, y, img.width(), img.height(), QBrush(frameBgCol));
}
painter.drawImage(x, y, img);
}
Frame* Frame::copy()
{
Frame* f = new Frame(scene, img.copy(), frameBgCol, transparentBg, bgTransparent);
f->setPosition(x, y);
f->setFrameBgTransparent(bgTransparent);
if(selected)
f->selectToggle();
return f;
}