-
Notifications
You must be signed in to change notification settings - Fork 1
/
rcompositemapobject.cpp
executable file
·298 lines (248 loc) · 9.5 KB
/
rcompositemapobject.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
#include "rcompositemapobject.h"
#include "rmapobjectmovepolicy.h"
#include "rmapobjectresizepolicy.h"
#include "rsection.h"
#include "rtable.h"
#include "rmapobjecttreewidgetitem.h"
#include "rmapobjectresizepolicy.h"
#include "rcompositemapobjectmodel.h"
#include <QPaintEvent>
#include <QPainter>
#include <QDebug>
#include <QAction>
#include <QInputDialog>
namespace {
bool cmpTablesByRows(const RTable *tbl1, const RTable *tbl2)
{
return tbl1->geometry().top() == tbl2->geometry().top()
? tbl1->geometry().left() < tbl2->geometry().left()
: tbl1->geometry().top() < tbl2->geometry().top();
}
}
RCompositeMapObject *RCompositeMapObject::m_instance = 0;
//--------------------------------------------------------------------------------------
void RCompositeMapObject::createInstance(
RMapObject *parent,
RMapObjectMovePolicy *movePolicy,
const QList<RMapObject *> &list)
{
RCompositeMapObject *compositeMapObject = new RCompositeMapObject(parent);
compositeMapObject->setModel(new RCompositeMapObjectModel);
compositeMapObject->setMovePolicy(movePolicy);
compositeMapObject->setResizePolicy(new RMapObjectNoResizePolicy);
compositeMapObject->setMapObjectList(list);
compositeMapObject->show();
compositeMapObject->setFocusMapObject();
compositeMapObject->model()->setModified(false);
m_instance = compositeMapObject;
}
//--------------------------------------------------------------------------------------
void RCompositeMapObject::deleteInstance()
{
if (m_instance) {
m_instance->self_deleted = true;
delete m_instance;
}
}
//--------------------------------------------------------------------------------------
RCompositeMapObject *RCompositeMapObject::instance()
{ return m_instance; }
//--------------------------------------------------------------------------------------
RCompositeMapObject::RCompositeMapObject(QWidget *parent) :
RMapObject(parent),
mouse_pressed(false),
self_deleted(false)
{
treeWidgetItem()->setHidden(true);
QAction *renumberTablesAction = new QAction(tr("Пронумеровать столики"), this);
parentWidget()->parentWidget()->addAction(renumberTablesAction);
connect(renumberTablesAction, SIGNAL(triggered()), this, SLOT(onRenumberTables()));
}
//--------------------------------------------------------------------------------------
RCompositeMapObject::~RCompositeMapObject()
{
if (self_deleted) {
// сами удалились, восстанавливаем сигналы
foreach (RMapObject *obj, map_object_list) {
obj->model()->blockSignals(false);
// если наша модель была обновлена - обновляем и дочерние модели
if (model()->isModified()) {
obj->model()->setModified(true);
}
}
} else {
// нас принудительно удалили, удаляем и объекты
foreach (RMapObject *obj, map_object_list) {
delete obj;
}
}
m_instance = 0;
}
//--------------------------------------------------------------------------------------
void RCompositeMapObject::onRenumberTables()
{
QList<RTable *> tablesList;
foreach (RMapObject *obj, map_object_list) {
if (RTable *table = qobject_cast<RTable *>(obj)) {
tablesList.push_back(table);
}
}
// отсортируем список в нужном нам направлении
qSort(tablesList.begin(), tablesList.end(), cmpTablesByRows);
bool isOk = false;
int number = QInputDialog::getInt(this, tr("Нумерация столиков"), tr("Начальный номер"), 1, 1, 999999, 1, &isOk);
if (isOk) {
foreach (RTable *table, tablesList) {
table->model()->setNumber(number++);
table->model()->setModified(true);
}
}
}
//--------------------------------------------------------------------------------------
RCompositeMapObject *RCompositeMapObject::clone() const
{
RCompositeMapObject *compositeObject = new RCompositeMapObject(parentWidget());
compositeObject->setModel(model()->clone());
compositeObject->setMovePolicy(movePolicy()->clone());
compositeObject->setResizePolicy(resizePolicy()->clone());
foreach (RMapObject *mapObj, map_object_list) {
compositeObject->addMapObject(mapObj->clone());
}
compositeObject->setGeometry(geometry());
compositeObject->setVisible(isVisible());
compositeObject->setFocusMapObject();
compositeObject->model()->setModified(false);
// старый объект удалим
self_deleted = true;
delete m_instance;
m_instance = compositeObject;
// тут уже нельзя производить никаких операций с объектом this!
return compositeObject;
}
// добавить объект в набор
//--------------------------------------------------------------------------------------
void RCompositeMapObject::addMapObject(RMapObject *obj)
{
// заблокируем сигналы модели выбранного объекта
obj->model()->blockSignals(true);
map_object_list.push_back(obj);
QRect newGeom = obj->geometry();
foreach (RMapObject *mapObj, map_object_list) {
newGeom.setLeft(qMin(newGeom.left(), mapObj->geometry().left()));
newGeom.setTop(qMin(newGeom.top(), mapObj->geometry().top()));
newGeom.setRight(qMax(newGeom.right(), mapObj->geometry().right()));
newGeom.setBottom(qMax(newGeom.bottom(), mapObj->geometry().bottom()));
}
setGeometry(newGeom);
}
// извлечь объект из набора
//--------------------------------------------------------------------------------------
void RCompositeMapObject::removeMapObject(RMapObject *obj)
{
if (map_object_list.removeOne(obj)) {
obj->model()->blockSignals(false);
}
}
//--------------------------------------------------------------------------------------
const QList<RMapObject *> RCompositeMapObject::mapObjectList() const
{ return map_object_list; }
//--------------------------------------------------------------------------------------
void RCompositeMapObject::setMapObjectList(const QList<RMapObject *> &list)
{
foreach (RMapObject *mapObj, list) {
addMapObject(mapObj);
}
}
//--------------------------------------------------------------------------------------
void RCompositeMapObject::removeAllMapObject()
{
foreach (RMapObject *obj, map_object_list) {
obj->model()->blockSignals(false);
}
map_object_list.clear();
}
//--------------------------------------------------------------------------------------
bool RCompositeMapObject::isEmpty() const
{ return map_object_list.empty(); }
//--------------------------------------------------------------------------------------
void RCompositeMapObject::paintEvent(QPaintEvent *event)
{
if (!hasFocusMapObject()) {
self_deleted = true;
deleteLater();
return;
}
QPainter painter(this);
QPen pen;
pen.setWidth(4);
pen.setStyle(Qt::DotLine);
painter.setPen(pen);
painter.drawRect(rect());
RMapObject::paintEvent(event);
}
//--------------------------------------------------------------------------------------
void RCompositeMapObject::resizeEvent(QResizeEvent *event)
{
movePolicy()->setMovePolygon(QPolygon(rect().adjusted(5, 5, -5, -5), true));
RMapObject::resizeEvent(event);
}
//--------------------------------------------------------------------------------------
void RCompositeMapObject::mousePressEvent(QMouseEvent *event)
{
foreach (RMapObject *obj, map_object_list) {
if (obj->geometry().contains(mapToParent(event->pos()), true)) {
mouse_pressed = true;
return RMapObject::mousePressEvent(event);
}
}
static_cast<RMapObject *>(parentWidget())->setFocusMapObject();
}
//--------------------------------------------------------------------------------------
void RCompositeMapObject::mouseMoveEvent(QMouseEvent *event)
{
if (mouse_pressed) {
return RMapObject::mouseMoveEvent(event);
}
foreach (RMapObject *obj, map_object_list) {
if (obj->geometry().contains(mapToParent(event->pos()), true)) {
return RMapObject::mouseMoveEvent(event);
}
}
if (cursor().shape() != Qt::ArrowCursor) {
setCursor(Qt::ArrowCursor);
}
}
//--------------------------------------------------------------------------------------
void RCompositeMapObject::mouseReleaseEvent(QMouseEvent *event)
{
mouse_pressed = false;
RMapObject::mouseReleaseEvent(event);
}
//--------------------------------------------------------------------------------------
void RCompositeMapObject::moveEvent(QMoveEvent *event)
{
RMapObject::moveEvent(event);
foreach (RMapObject *obj, map_object_list) {
QRect geom = obj->geometry();
geom.moveTopLeft(geom.topLeft() + event->pos() - event->oldPos());
obj->setGeometry(geom);
}
}
//--------------------------------------------------------------------------------------
void RCompositeMapObject::focusSetEvent()
{
if (QTreeWidget *treeWidget = treeWidgetItem()->treeWidget()) {
treeWidget->selectionModel()->clearSelection();
treeWidget->setCurrentItem(treeWidgetItem());
}
foreach (RMapObject *obj, map_object_list) {
obj->treeWidgetItem()->setSelected(true);
}
}
//--------------------------------------------------------------------------------------
void RCompositeMapObject::focusLostEvent()
{
if (QTreeWidget *treeWidget = treeWidgetItem()->treeWidget()) {
treeWidget->selectionModel()->clearSelection();
}
}