-
Notifications
You must be signed in to change notification settings - Fork 1
/
layer_manager.cpp
214 lines (186 loc) · 4.9 KB
/
layer_manager.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
#include "stdafx.h"
#include "layer_image.h"
#include "layer_manager.h"
namespace psv
{
layer_manager::layer_manager(QWidget* parent) :
QWidget(parent),
layout_(new FlowLayout)
{
QSizePolicy policy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
policy.setHeightForWidth(true);
setSizePolicy(policy);
setLayout(layout_);
setFocusPolicy(Qt::StrongFocus);
auto shortcut = new QShortcut(QKeySequence(tr("Ctrl+A")), this);
connect(shortcut, SIGNAL(activated()), this, SLOT(select_all()));
}
layer_manager::~layer_manager()
{
}
void layer_manager::clear()
{
auto layers = findChildren<layer_image*>();
foreach (auto item, layers)
item->deleteLater();
updateGeometry();
update();
}
void layer_manager::add_layer(const QString& name, const QPixmap& pixmap)
{
auto layer = new layer_image;
layer->set_layer_name(name);
layer->set_pixmap(pixmap);
layout_->addWidget(layer);
updateGeometry();
update();
}
int layer_manager::heightForWidth(int width) const
{
return layout_->heightForWidth(width);
}
QSize layer_manager::sizeHint() const
{
return layout_->sizeHint();
}
void layer_manager::mousePressEvent(QMouseEvent* e)
{
if (e->button() == Qt::LeftButton)
start_drag_ = e->pos();
QWidget::mousePressEvent(e);
}
void layer_manager::mouseReleaseEvent(QMouseEvent* e)
{
if (e->button() == Qt::LeftButton)
{
if ((e->modifiers() & Qt::ControlModifier) == 0)
{
auto layers = findChildren<layer_image*>();
foreach (auto item, layers)
item->set_selected(false);
}
auto layer = qobject_cast<layer_image*>(childAt(e->pos()));
if (layer)
select_layer(layer, !layer->selected());
update();
}
QWidget::mouseReleaseEvent(e);
}
void layer_manager::mouseMoveEvent(QMouseEvent* e)
{
if (e->buttons() & Qt::LeftButton)
{
int distance = (e->pos() - start_drag_).manhattanLength();
if (distance >= QApplication::startDragDistance())
perform_drag();
}
QWidget::mouseMoveEvent(e);
}
void layer_manager::perform_drag()
{
auto layers = selected_layers();
if (layers.empty())
return;
QMimeData* mime_data = new QMimeData;
QDrag* drag = new QDrag(this);
drag->setMimeData(mime_data);
QList<QUrl> urls;
foreach(auto layer, layers)
{
if (layer->pixmap().save(layer->file_name(), "PNG", 0))
urls.append(QUrl::fromLocalFile(layer->file_name()));
else
qCritical() << "Failed to save layer to file '" << layer->file_name() << "'";
}
mime_data->setUrls(urls);
drag->setPixmap(get_layers_icon(layers));
drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction);
// If we remove file some applications can't open it because they try to open after drop was done.
// For example, paint when there are unsaved changes
}
QVector<layer_image*> layer_manager::selected_layers() const
{
QVector<layer_image*> result;
auto layers = findChildren<layer_image*>();
foreach (auto layer, layers)
{
if (layer->selected())
result.append(layer);
}
return result;
}
void layer_manager::set_filenames() const
{
auto layers = findChildren<layer_image*>();
QMap<QString, size_t> cnt;
foreach (auto layer, layers)
++cnt[layer->filesystem_layer_name()];
QMap<QString, size_t> used;
foreach (auto layer, layers)
{
auto name = layer->filesystem_layer_name();
auto path = QDir::tempPath() + QDir::separator() + name;
if (cnt[name] > 1)
path += QString("_%1").arg(++used[name]);
path += ".png";
layer->set_file_name(path);
}
}
QPixmap layer_manager::get_layers_icon(QVector<layer_image*>& layers) const
{
Q_ASSERT(!layers.empty());
switch (layers.size())
{
case 1:
return layers[0]->icon();
case 2:
case 3:
{
QPixmap result(layer_image::icon_size, layer_image::icon_size);
result.fill(Qt::transparent);
QPainter painter(&result);
int h = layer_image::icon_size / 2;
int w = layer_image::icon_size;
painter.drawPixmap(0, 0, w, h, layers[0]->icon());
painter.drawPixmap(0, h, w, h, layers[1]->icon());
return result;
}
default:
{
QPixmap result(layer_image::icon_size, layer_image::icon_size);
result.fill(Qt::transparent);
QPainter painter(&result);
int sz = layer_image::icon_size / 2;
painter.drawPixmap(0, 0, sz, sz, layers[0]->icon());
painter.drawPixmap(sz, 0, sz, sz, layers[1]->icon());
painter.drawPixmap(0, sz, sz, sz, layers[2]->icon());
painter.drawPixmap(sz, sz, sz, sz, layers[3]->icon());
return result;
}
}
}
void layer_manager::select_all()
{
auto layers = findChildren<layer_image*>();
foreach (auto layer, layers)
{
select_layer(layer, true);
}
}
void layer_manager::select_layer(layer_image* layer, bool select)
{
layer->set_selected(select);
auto layers = selected_layers();
if (layers.size() != 1)
return;
QMimeData* mimeData = new QMimeData();
QByteArray data;
QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
layers[0]->pixmap().save(&buffer, "PNG");
buffer.close();
mimeData->setData("PNG", data);
mimeData->setImageData(layers[0]->pixmap());
qApp->clipboard()->setMimeData(mimeData);
}
}