/
sstQt01PathPaintWidget.cpp
486 lines (440 loc) · 17.4 KB
/
sstQt01PathPaintWidget.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
/**********************************************************************
*
* sstQt01Lib - sst Qt library
* Hosted on github
*
* Copyright (C) 2016 Uli Rehr
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU Lesser General Public Licence as published
* by the Free Software Foundation.
* See the COPYING file for more information.
*
**********************************************************************/
// sstQt01PathPaintWidgetCls.cpp 14.12.18 Re. 14.08.16 Re.
#include <QtWidgets>
#include <stdlib.h>
#include <assert.h>
#include <sstStr01Lib.h>
#include <sstMisc01Lib.h>
#include <sstRec04Lib.h>
#include <sstQt01Lib.h>
//=============================================================================
sstQt01PathPaintWidgetCls::sstQt01PathPaintWidgetCls(sstMisc01PrtFilCls *poTmpPrt,
sstQt01PathStoreViewCls *poTmpPathStorage)
{
Q_INIT_RESOURCE(tooltips);
if (poTmpPathStorage == NULL) assert (0);
if (poTmpPathStorage->countItems() <= 0) assert(0);
this->oPathStorage = poTmpPathStorage;
this->poPrt = poTmpPrt;
setMouseTracking(true);
setBackgroundRole(QPalette::Base);
newCircleButton = createToolButton(tr("New Circle"),
QIcon(":/images/circle.png"),
SLOT(createNewCircle()));
newSquareButton = createToolButton(tr("New Square"),
QIcon(":/images/square.png"),
SLOT(createNewSquare()));
newTriangleButton = createToolButton(tr("New Triangle"),
QIcon(":/images/triangle.png"),
SLOT(createNewTriangle()));
newLineButton = createToolButton(tr("New Line"),
QIcon(":/images/line.png"),
SLOT(createNewLine()));
newPolyLineButton = createToolButton(tr("New PolyLine"),
QIcon(":/images/polyline.png"),
SLOT(createNewPolyLine()));
newArcButton = createToolButton(tr("New Arc"),
QIcon(":/images/arc.png"),
SLOT(createNewArc()));
newTextButton = createToolButton(tr("New Text"),
QIcon(":/images/text.png"),
SLOT(createNewText()));
//=============================================================================
// Create item templates
circlePath.addEllipse(QRect(0, 0, 100, 100));
squarePath.addRect(QRect(0, 0, 100, 100));
qreal x = trianglePath.currentPosition().x();
qreal y = trianglePath.currentPosition().y();
trianglePath.moveTo(x + 120 / 2, y);
trianglePath.lineTo(0, 100);
trianglePath.lineTo(120, 100);
trianglePath.lineTo(x + 120 / 2, y);
linePath.moveTo( 0, 0);
linePath.lineTo(100, 100);
polylinePath.moveTo( 0, 0);
polylinePath.lineTo(25, 75);
polylinePath.lineTo(100, 100);
// arcPath.moveTo( 150, 150);
QRect oRect(100,100,200,200);
arcPath.arcMoveTo(oRect,0);
arcPath.arcTo(oRect,0,45); // Grad, null is east, counterclockwise
QFont oFont;
oFont.setPointSize(50);
// oFont.s
QString oQStr = "A";
textPath.moveTo( 0, 0);
textPath.addText(50,50,oFont,oQStr);
//=============================================================================
setWindowTitle(tr("Tool Tips"));
this->setMinimumSize(350,350); // Get Size for 7 Buttons
iActualItemIndex = 0;
iItemInMotionIndex = 0;
}
//=============================================================================
sstQt01PathPaintWidgetCls::~sstQt01PathPaintWidgetCls()
{
}
//=============================================================================
bool sstQt01PathPaintWidgetCls::event(QEvent *event)
{
if (event->type() == QEvent::ToolTip) {
QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
int index = itemAt(helpEvent->pos());
if (index != -1)
{
QToolTip::showText(helpEvent->globalPos(), (QString) this->oPathStorage->getToolTip(index));
} else {
QToolTip::hideText();
event->ignore();
}
return true;
}
return QWidget::event(event);
}
//=============================================================================
void sstQt01PathPaintWidgetCls::resizeEvent(QResizeEvent * /* event */)
{
int margin = style()->pixelMetric(QStyle::PM_DefaultTopLevelMargin);
int x = width() - margin;
int y = height() - margin;
y = updateButtonGeometry(newCircleButton, x, y);
y = updateButtonGeometry(newSquareButton, x, y);
y = updateButtonGeometry(newTriangleButton, x, y);
y = updateButtonGeometry(newLineButton, x, y);
y = updateButtonGeometry(newPolyLineButton, x, y);
y = updateButtonGeometry(newArcButton, x, y);
y = updateButtonGeometry(newTextButton, x, y);
}
//=============================================================================
void sstQt01PathPaintWidgetCls::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
int iPathCount = (int) this->oPathStorage->countItems();
for (int ii=1; ii <= iPathCount; ii++)
{
sstQt01ShapeItem oShapeItem;
int iStat = this->oPathStorage->getShapeItem2( 0, ii, &oShapeItem);
// Get delete state of record dRecNo
if (iStat >= 0)
{
// Set Area painting attributes
QBrush oBrush;
oBrush.setColor(oShapeItem.getColor());
oBrush.setStyle(Qt::SolidPattern);
// Set Line painting attributes
QPen oPen(oShapeItem.getColor(),Qt::SolidLine);
// QColor oCol = oShapeItem.getColor();
// int iRed = 0;
// int iBlue = 0;
// int iGreen = 0;
// oCol.getRgb(&iRed,&iGreen,&iBlue);
switch (oShapeItem.getShapeType())
{ // Set Area Fill Style
case eSstQt01PathArea: oBrush.setStyle(Qt::SolidPattern);break;
case eSstQt01PathArc: oBrush.setStyle(Qt::NoBrush);break;
case eSstQt01PathCircle: oBrush.setStyle(Qt::NoBrush);break;
case eSstQt01PathLine: oBrush.setStyle(Qt::NoBrush);break;
case eSstQt01PathPLine: oBrush.setStyle(Qt::NoBrush);break;
default: break;
}
painter.translate(this->oPathStorage->getPosition(ii));
painter.setBrush(oBrush);
painter.setPen(oPen);
painter.drawPath(oShapeItem.getPath());
painter.translate(-this->oPathStorage->getPosition(ii));
}
}
}
//=============================================================================
void sstQt01PathPaintWidgetCls::sstPaintEvent(sstQt01ShapeItem oShapeItem)
{
dREC04RECNUMTYP dItemNo = oShapeItem.getInternId();
if(dItemNo <= 0 || dItemNo > this->oPathStorage->countItems())
{
assert(0);
}
this->oPathStorage->replaceShapeItem( 0, dItemNo,oShapeItem);
this->update();
}
//=============================================================================
void sstQt01PathPaintWidgetCls::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
int index = itemAt(event->pos());
if (index != -1)
{
iActualItemIndex = index;
iItemInMotionIndex = index;
oActualItem = this->oPathStorage->getShapeItem(index);
// itemInMotion = &oActualItem;
previousPosition = event->pos();
// Move acual item to end of list. Will be found first with mouse
//shapeItems.move(index, shapeItems.size() - 1);
update();
}
}
}
//=============================================================================
void sstQt01PathPaintWidgetCls::mouseMoveEvent(QMouseEvent *event)
{
if ((event->buttons() & Qt::LeftButton) && iItemInMotionIndex > 0)
moveItemTo(event->pos());
}
//=============================================================================
void sstQt01PathPaintWidgetCls::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton && iItemInMotionIndex > 0)
{
moveItemTo(event->pos());
this->oPathStorage->addPosition(this->iActualItemIndex);
iItemInMotionIndex = 0;
sstQt01ShapeItem oShapeItem;
// dREC04RECNUMTYP dExtId = this->oPathStorage->getExternId(this->iActualItemIndex);
oShapeItem = this->oPathStorage->getShapeItem(this->iActualItemIndex);
oShapeItem.updatePosition();
emit sstPathMoveReleaseSgnl(oShapeItem);
// sstQt01MapSignalCls oMapChangedSignal;
// oMapChangedSignal.setExternTypeStr(oShapeItem.getExternStr());
// oMapChangedSignal.setExternTypeTabRecNo( oShapeItem.getExternId());
// oMapChangedSignal.setShapeItemListRecNo( this->iActualItemIndex);
// emit sstExtPathMoveReleaseSgnl(oMapChangedSignal);
}
}
//=============================================================================
void sstQt01PathPaintWidgetCls::createNewCircle()
{
// static int count = 1;
createShapeItem(circlePath, // tr("Circle <%1>").arg(++count),
randomItemPosition(), randomItemColor(), randomItemPen(),eSstQt01PathCircle);
}
//=============================================================================
void sstQt01PathPaintWidgetCls::createNewSquare()
{
// static int count = 1;
createShapeItem(squarePath, // tr("Square <%1>").arg(++count),
randomItemPosition(), randomItemColor(), randomItemPen(),eSstQt01PathArea);
}
//=============================================================================
void sstQt01PathPaintWidgetCls::createNewTriangle()
{
// static int count = 1;
createShapeItem(trianglePath, // tr("Triangle <%1>").arg(++count),
randomItemPosition(), randomItemColor(), randomItemPen(),eSstQt01PathArea);
}
//=============================================================================
void sstQt01PathPaintWidgetCls::createNewLine()
{
// static int count = 1;
createShapeItem(linePath, // tr("Line <%1>").arg(++count),
randomItemPosition(), randomItemColor(), randomItemPen(),eSstQt01PathLine);
}
//=============================================================================
void sstQt01PathPaintWidgetCls::createNewPolyLine()
{
// static int count = 1;
createShapeItem(polylinePath, // tr("Line <%1>").arg(++count),
randomItemPosition(), randomItemColor(), randomItemPen(),eSstQt01PathPLine);
}
//=============================================================================
void sstQt01PathPaintWidgetCls::createNewArc()
{
// static int count = 1;
createShapeItem(arcPath, // tr("Line <%1>").arg(++count),
randomItemPosition(), randomItemColor(), randomItemPen(),eSstQt01PathArc);
}
//=============================================================================
void sstQt01PathPaintWidgetCls::createNewText()
{
// static int count = 1;
createShapeItem(textPath, // tr("Line <%1>").arg(++count),
randomItemPosition(), randomItemColor(), randomItemPen(),eSstQt01PathText);
}
//=============================================================================
int sstQt01PathPaintWidgetCls::itemAt(const QPoint &pos)
{
for (int i = this->oPathStorage->countItems(); i >= 1; --i)
{
const sstQt01ShapeItem &item = this->oPathStorage->getShapeItem(i);
if (item.getBoundingBox().contains(pos - item.getPosition()))
return i;
}
return -1;
}
//=============================================================================
void sstQt01PathPaintWidgetCls::moveItemTo(const QPoint &pos)
{
QPoint offset = pos - previousPosition;
QPoint oItemPos = this->oPathStorage->getPosition(iItemInMotionIndex);
this->oPathStorage->setPosition(this->iActualItemIndex, oItemPos + offset);
previousPosition = pos;
update();
}
//=============================================================================
int sstQt01PathPaintWidgetCls::updateButtonGeometry(QToolButton *button, int x, int y)
{
QSize size = button->sizeHint();
button->setGeometry(x - size.rwidth(), y - size.rheight(),
size.rwidth(), size.rheight());
return y - size.rheight()
- style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing);
}
//=============================================================================
void sstQt01PathPaintWidgetCls::createShapeItem(const QPainterPath &path,
// const QString &toolTip,
const QPoint &pos,
const QColor &color,
const QPen &oPen,
const sstQt01ShapeType_enum eShapeType)
{
sstQt01ShapeItem shapeItem;
shapeItem.setPath(path);
// shapeItem.setToolTip(toolTip);
shapeItem.setPosition(pos);
shapeItem.setColor(color);
shapeItem.setPen(oPen);
shapeItem.setShapeType(eShapeType);
// Generate tooltip (Type and row number)
std::string oTooltipStr;
sstQt01ShapeTypeCls oShapeType;
oShapeType.Enm2Str( 0, eShapeType, &oTooltipStr);
sstStr01Cls oStrCnvt;
oStrCnvt.Csv_UInt4_2String(0,this->oPathStorage->RecordCount()+1,&oTooltipStr);
QString oQTooltipStr;
oQTooltipStr.append(oTooltipStr.c_str());
shapeItem.setToolTip(oQTooltipStr);
int iBegin=0;
int iEnd=0;
// Insert rows at end of table
iBegin = (int) this->oPathStorage->RecordCount() +1;
iEnd = iBegin + (int) path.elementCount() -1;
emit sstSgnlBeginInsertRows(iBegin,iEnd);
this->oPathStorage->appendShapeItem(shapeItem);
emit sstSgnlEndInsertRows();
update();
}
//=============================================================================
QToolButton *sstQt01PathPaintWidgetCls::createToolButton(const QString &toolTip,
const QIcon &icon, const char *member)
{
QToolButton *button = new QToolButton(this);
button->setToolTip(toolTip);
button->setIcon(icon);
button->setIconSize(QSize(32, 32));
connect(button, SIGNAL(clicked()), this, member);
return button;
}
//=============================================================================
QPoint sstQt01PathPaintWidgetCls::initialItemPosition(const QPainterPath &path)
{
int x = 0;
int y = 0;
y = (height() - (int)path.controlPointRect().height()) / 2;
if (this->oPathStorage->countItems() == 0)
x = ((3 * width()) / 2 - (int)path.controlPointRect().width()) / 2;
else
x = (width() / this->oPathStorage->countItems()
- (int)path.controlPointRect().width()) / 2;
return QPoint(x, y);
}
//=============================================================================
QPoint sstQt01PathPaintWidgetCls::randomItemPosition()
{
return QPoint(qrand() % (width() - 120), qrand() % (height() - 120));
}
//=============================================================================
QColor sstQt01PathPaintWidgetCls::initialItemColor()
{
return QColor::fromHsv(((this->oPathStorage->countItems() + 1) * 85) % 256, 255, 190);
}
//=============================================================================
QColor sstQt01PathPaintWidgetCls::randomItemColor()
{
return QColor::fromHsv(qrand() % 256, 255, 190);
}
//=============================================================================
QPen sstQt01PathPaintWidgetCls::initialItemPen()
{
QPen oPen;
return oPen;
}
//=============================================================================
QPen sstQt01PathPaintWidgetCls::randomItemPen()
{
QPen oPen;
return oPen;
}
//=============================================================================
int sstQt01PathPaintWidgetCls::updateTooltips (int iKey)
//-----------------------------------------------------------------------------
{
// QPoint oPnt(0,0);
QPainterPath *poPath;
QColor oColor;
int iPathNo = 0;
int iRet = 0;
int iStat = 0;
int iStat1 = 0;
//-----------------------------------------------------------------------------
if ( iKey != 0) return -1;
// read next path from shape item table
poPath = new (QPainterPath);
iStat1 = this->oPathStorage->ReadNextPath( 0, poPath, &oColor);
while (iStat1 >= 0)
{
iPathNo++;
int iElements = poPath->elementCount();
assert(iElements > 0);
switch (iElements)
{
case 2:
this->oPathStorage->setToolTip( iPathNo, tr("Line"));
break;
case 4:
this->oPathStorage->setToolTip( iPathNo, tr("Triangle"));
break;
case 5:
this->oPathStorage->setToolTip( iPathNo, tr("Square"));
break;
case 13:
this->oPathStorage->setToolTip( iPathNo, tr("Circle"));
break;
default:
this->oPathStorage->setToolTip( iPathNo, tr("Object"));
// assert(0);
break;
}
delete poPath;
poPath = new (QPainterPath);
iStat1 = this->oPathStorage->ReadNextPath( 0, poPath, &oColor);
}
delete poPath;
assert(iRet >= 0);
// Small Errors will given back
iRet = iStat;
return iRet;
}
//=============================================================================
QSize sstQt01PathPaintWidgetCls::minimumSizeHint() const
{
return QSize(100, 100);
}
//=============================================================================
QSize sstQt01PathPaintWidgetCls::sizeHint() const
{
return QSize(400, 200);
}
//=============================================================================