| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include <QMenu> |
| 25 |
#include <QMouseEvent> |
| 26 |
#include <Inventor/nodes/SoCamera.h> |
| 27 |
|
| 28 |
#include <Inventor/SbVec2s.h> |
| 29 |
|
| 30 |
#include "Flag.h" |
| 31 |
#include "View3DInventorViewer.h" |
| 32 |
|
| 33 |
|
| 34 |
using namespace Gui; |
| 35 |
|
| 36 |
|
| 37 |
/* TRANSLATOR Gui::Flag */ |
| 38 |
|
| 39 |
// TODO: Rename to Annotation |
| 40 |
// Support transparency |
| 41 |
// Embed complete widgets |
| 42 |
|
| 43 |
Flag::Flag(QWidget* parent) |
| 44 |
: QOpenGLWidget(parent) |
| 45 |
, coord(0.0f, 0.0f, 0.0f) |
| 46 |
{ |
| 47 |
this->setFixedHeight(20); |
| 48 |
setAutoFillBackground(true); |
| 49 |
} |
| 50 |
|
| 51 |
Flag::~Flag() = default; |
| 52 |
|
| 53 |
void Flag::initializeGL() |
| 54 |
{ |
| 55 |
const QPalette& p = this->palette(); |
| 56 |
QColor c(p.color(QPalette::Window)); |
| 57 |
glClearColor(c.redF(), c.greenF(), c.blueF(), c.alphaF()); |
| 58 |
} |
| 59 |
|
| 60 |
void Flag::paintGL() |
| 61 |
{ |
| 62 |
QOpenGLWidget::paintGL(); |
| 63 |
} |
| 64 |
|
| 65 |
void Flag::paintEvent(QPaintEvent* e) |
| 66 |
{ |
| 67 |
const QPalette& p = this->palette(); |
| 68 |
QColor c(p.color(QPalette::Text)); |
| 69 |
|
| 70 |
QPainter painter; |
| 71 |
painter.begin(this); |
| 72 |
painter.setRenderHint(QPainter::Antialiasing); |
| 73 |
painter.fillRect(e->rect(), p.color(QPalette::Window)); |
| 74 |
painter.setPen(c); |
| 75 |
painter.drawText(10, 15, this->text); |
| 76 |
painter.end(); |
| 77 |
} |
| 78 |
|
| 79 |
void Flag::resizeGL(int width, int height) |
| 80 |
{ |
| 81 |
Q_UNUSED(width); |
| 82 |
Q_UNUSED(height); |
| 83 |
} |
| 84 |
|
| 85 |
void Flag::setOrigin(const SbVec3f& v) |
| 86 |
{ |
| 87 |
this->coord = v; |
| 88 |
} |
| 89 |
|
| 90 |
const SbVec3f& Flag::getOrigin() const |
| 91 |
{ |
| 92 |
return this->coord; |
| 93 |
} |
| 94 |
|
| 95 |
void Flag::drawLine(View3DInventorViewer* v, int tox, int toy) |
| 96 |
{ |
| 97 |
if (!isVisible()) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
// Get position of line |
| 102 |
QSize s = parentWidget()->size(); |
| 103 |
SbVec2s view(s.width(), s.height()); |
| 104 |
int fromx = pos().x(); |
| 105 |
int fromy = pos().y() + height() / 2; |
| 106 |
if (false) { |
| 107 |
fromx += width(); |
| 108 |
} |
| 109 |
|
| 110 |
GLPainter p; |
| 111 |
p.begin(v->getGLWidget()); |
| 112 |
|
| 113 |
// the line |
| 114 |
p.setLineWidth(1.0f); |
| 115 |
p.drawLine(fromx, fromy, tox, toy); |
| 116 |
// the point |
| 117 |
p.setPointSize(3.0f); |
| 118 |
p.drawPoint(tox, toy); |
| 119 |
p.end(); |
| 120 |
} |
| 121 |
|
| 122 |
void Flag::setText(const QString& t) |
| 123 |
{ |
| 124 |
this->text = t; |
| 125 |
} |
| 126 |
|
| 127 |
void Flag::resizeEvent(QResizeEvent* e) |
| 128 |
{ |
| 129 |
QOpenGLWidget::resizeEvent(e); |
| 130 |
} |
| 131 |
|
| 132 |
void Flag::mouseMoveEvent(QMouseEvent* e) |
| 133 |
{ |
| 134 |
if (e->buttons() & Qt::LeftButton) { |
| 135 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 136 |
move(e->globalPos() - dragPosition); |
| 137 |
#else |
| 138 |
move(e->globalPosition().toPoint() - dragPosition); |
| 139 |
#endif |
| 140 |
e->accept(); |
| 141 |
auto viewer = qobject_cast<View3DInventorViewer*>(parentWidget()); |
| 142 |
if (viewer) { |
| 143 |
viewer->getSoRenderManager()->scheduleRedraw(); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
void Flag::mousePressEvent(QMouseEvent* e) |
| 149 |
{ |
| 150 |
if (e->button() == Qt::LeftButton) { |
| 151 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 152 |
dragPosition = e->globalPos() - frameGeometry().topLeft(); |
| 153 |
#else |
| 154 |
dragPosition = e->globalPosition().toPoint() - frameGeometry().topLeft(); |
| 155 |
#endif |
| 156 |
e->accept(); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
void Flag::contextMenuEvent(QContextMenuEvent* e) |
| 161 |
{ |
| 162 |
QMenu menu(this); |
| 163 |
|
| 164 |
QAction* topleft = menu.addAction(tr("Top Left")); |
| 165 |
topleft->setCheckable(true); |
| 166 |
QAction* botleft = menu.addAction(tr("Bottom Left")); |
| 167 |
botleft->setCheckable(true); |
| 168 |
QAction* topright = menu.addAction(tr("Top Right")); |
| 169 |
topright->setCheckable(true); |
| 170 |
QAction* botright = menu.addAction(tr("Bottom Right")); |
| 171 |
botright->setCheckable(true); |
| 172 |
menu.addSeparator(); |
| 173 |
QAction* remove = menu.addAction(tr("Remove")); |
| 174 |
QAction* select = menu.exec(e->globalPos()); |
| 175 |
if (remove == select) { |
| 176 |
this->deleteLater(); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
QSize Flag::sizeHint() const |
| 181 |
{ |
| 182 |
int w = 100; |
| 183 |
int h = 20; |
| 184 |
QFontMetrics metric(this->font()); |
| 185 |
QRect r = metric.boundingRect(text); |
| 186 |
w = std::max<int>(w, r.width() + 20); |
| 187 |
h = std::max<int>(h, r.height()); |
| 188 |
return {w, h}; |
| 189 |
} |
| 190 |
|
| 191 |
// ------------------------------------------------------------------------ |
| 192 |
|
| 193 |
FlagLayout::FlagLayout(QWidget* parent, int margin, int spacing) |
| 194 |
: QLayout(parent) |
| 195 |
{ |
| 196 |
setContentsMargins(margin, margin, margin, margin); |
| 197 |
setSpacing(spacing); |
| 198 |
} |
| 199 |
|
| 200 |
FlagLayout::FlagLayout(int spacing) |
| 201 |
{ |
| 202 |
setSpacing(spacing); |
| 203 |
} |
| 204 |
|
| 205 |
FlagLayout::~FlagLayout() |
| 206 |
{ |
| 207 |
QLayoutItem* l; |
| 208 |
while ((l = takeAt(0))) { |
| 209 |
delete l; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
void FlagLayout::addItem(QLayoutItem* item) |
| 214 |
{ |
| 215 |
add(item, TopLeft); |
| 216 |
} |
| 217 |
|
| 218 |
void FlagLayout::addWidget(QWidget* widget, Position position) |
| 219 |
{ |
| 220 |
add(new QWidgetItem(widget), position); |
| 221 |
} |
| 222 |
|
| 223 |
Qt::Orientations FlagLayout::expandingDirections() const |
| 224 |
{ |
| 225 |
return Qt::Horizontal | Qt::Vertical; |
| 226 |
} |
| 227 |
|
| 228 |
bool FlagLayout::hasHeightForWidth() const |
| 229 |
{ |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
int FlagLayout::count() const |
| 234 |
{ |
| 235 |
return list.size(); |
| 236 |
} |
| 237 |
|
| 238 |
QLayoutItem* FlagLayout::itemAt(int index) const |
| 239 |
{ |
| 240 |
ItemWrapper* wrapper = list.value(index); |
| 241 |
if (wrapper) { |
| 242 |
return wrapper->item; |
| 243 |
} |
| 244 |
else { |
| 245 |
return nullptr; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
QSize FlagLayout::minimumSize() const |
| 250 |
{ |
| 251 |
return calculateSize(MinimumSize); |
| 252 |
} |
| 253 |
|
| 254 |
void FlagLayout::setGeometry(const QRect& rect) |
| 255 |
{ |
| 256 |
int topHeight = 0; |
| 257 |
int bottomHeight = 0; |
| 258 |
|
| 259 |
QLayout::setGeometry(rect); |
| 260 |
|
| 261 |
// left side |
| 262 |
for (ItemWrapper* wrapper : list) { |
| 263 |
QLayoutItem* item = wrapper->item; |
| 264 |
Position position = wrapper->position; |
| 265 |
|
| 266 |
if (position == TopLeft) { |
| 267 |
topHeight += spacing(); |
| 268 |
item->setGeometry( |
| 269 |
QRect(rect.x() + spacing(), topHeight, item->sizeHint().width(), item->sizeHint().height()) |
| 270 |
); |
| 271 |
|
| 272 |
topHeight += item->geometry().height(); |
| 273 |
} |
| 274 |
else if (position == BottomLeft) { |
| 275 |
bottomHeight += item->geometry().height() + spacing(); |
| 276 |
item->setGeometry(QRect( |
| 277 |
rect.x() + spacing(), |
| 278 |
rect.height() - bottomHeight, |
| 279 |
item->sizeHint().width(), |
| 280 |
item->sizeHint().height() |
| 281 |
)); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
// right side |
| 286 |
topHeight = 0; |
| 287 |
bottomHeight = 0; |
| 288 |
for (ItemWrapper* wrapper : list) { |
| 289 |
QLayoutItem* item = wrapper->item; |
| 290 |
Position position = wrapper->position; |
| 291 |
|
| 292 |
int rightpos = item->sizeHint().width() + spacing(); |
| 293 |
if (position == TopRight) { |
| 294 |
topHeight += spacing(); |
| 295 |
item->setGeometry(QRect( |
| 296 |
rect.x() + rect.width() - rightpos, |
| 297 |
topHeight, |
| 298 |
item->sizeHint().width(), |
| 299 |
item->sizeHint().height() |
| 300 |
)); |
| 301 |
|
| 302 |
topHeight += item->geometry().height(); |
| 303 |
} |
| 304 |
else if (position == BottomRight) { |
| 305 |
bottomHeight += item->geometry().height() + spacing(); |
| 306 |
item->setGeometry(QRect( |
| 307 |
rect.x() + rect.width() - rightpos, |
| 308 |
rect.height() - bottomHeight, |
| 309 |
item->sizeHint().width(), |
| 310 |
item->sizeHint().height() |
| 311 |
)); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
QSize FlagLayout::sizeHint() const |
| 317 |
{ |
| 318 |
return calculateSize(SizeHint); |
| 319 |
} |
| 320 |
|
| 321 |
QLayoutItem* FlagLayout::takeAt(int index) |
| 322 |
{ |
| 323 |
if (index >= 0 && index < list.size()) { |
| 324 |
ItemWrapper* layoutStruct = list.takeAt(index); |
| 325 |
return layoutStruct->item; |
| 326 |
} |
| 327 |
return nullptr; |
| 328 |
} |
| 329 |
|
| 330 |
void FlagLayout::add(QLayoutItem* item, Position position) |
| 331 |
{ |
| 332 |
list.append(new ItemWrapper(item, position)); |
| 333 |
} |
| 334 |
|
| 335 |
QSize FlagLayout::calculateSize(SizeType sizeType) const |
| 336 |
{ |
| 337 |
QSize totalSize; |
| 338 |
|
| 339 |
for (ItemWrapper* wrapper : list) { |
| 340 |
QSize itemSize; |
| 341 |
|
| 342 |
if (sizeType == MinimumSize) { |
| 343 |
itemSize = wrapper->item->minimumSize(); |
| 344 |
} |
| 345 |
else { // (sizeType == SizeHint) |
| 346 |
itemSize = wrapper->item->sizeHint(); |
| 347 |
} |
| 348 |
|
| 349 |
totalSize.rheight() += itemSize.height(); |
| 350 |
totalSize.rwidth() = qMax<int>(totalSize.width(), itemSize.width()); |
| 351 |
} |
| 352 |
return totalSize; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
TYPESYSTEM_SOURCE_ABSTRACT(Gui::GLFlagWindow, Gui::GLGraphicsItem) |
| 357 |
|
| 358 |
GLFlagWindow::GLFlagWindow(View3DInventorViewer* view) |
| 359 |
: _viewer(view) |
| 360 |
, _flagLayout(nullptr) |
| 361 |
{} |
| 362 |
|
| 363 |
GLFlagWindow::~GLFlagWindow() |
| 364 |
{ |
| 365 |
deleteFlags(); |
| 366 |
if (_flagLayout) { |
| 367 |
_flagLayout->deleteLater(); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
void GLFlagWindow::deleteFlags() |
| 372 |
{ |
| 373 |
if (_flagLayout) { |
| 374 |
int ct = _flagLayout->count(); |
| 375 |
for (int i = 0; i < ct; i++) { |
| 376 |
QWidget* flag = _flagLayout->itemAt(0)->widget(); |
| 377 |
if (flag) { |
| 378 |
_flagLayout->removeWidget(flag); |
| 379 |
flag->deleteLater(); |
| 380 |
} |
| 381 |
} |
| 382 |
if (ct > 0) { |
| 383 |
_viewer->getSoRenderManager()->scheduleRedraw(); |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
void GLFlagWindow::addFlag(Flag* item, FlagLayout::Position pos) |
| 389 |
{ |
| 390 |
if (!_flagLayout) { |
| 391 |
_flagLayout = new FlagLayout(3); |
| 392 |
_viewer->setLayout(_flagLayout); |
| 393 |
} |
| 394 |
|
| 395 |
item->setParent(_viewer); |
| 396 |
_flagLayout->addWidget(item, pos); |
| 397 |
item->show(); |
| 398 |
_viewer->getSoRenderManager()->scheduleRedraw(); |
| 399 |
} |
| 400 |
|
| 401 |
void GLFlagWindow::removeFlag(Flag* item) |
| 402 |
{ |
| 403 |
if (_flagLayout) { |
| 404 |
_flagLayout->removeWidget(item); |
| 405 |
_viewer->getSoRenderManager()->scheduleRedraw(); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
Flag* GLFlagWindow::getFlag(int index) const |
| 410 |
{ |
| 411 |
if (_flagLayout) { |
| 412 |
QWidget* flag = _flagLayout->itemAt(index)->widget(); |
| 413 |
return qobject_cast<Flag*>(flag); |
| 414 |
} |
| 415 |
return nullptr; |
| 416 |
} |
| 417 |
|
| 418 |
int GLFlagWindow::countFlags() const |
| 419 |
{ |
| 420 |
if (_flagLayout) { |
| 421 |
return _flagLayout->count(); |
| 422 |
} |
| 423 |
|
| 424 |
return 0; |
| 425 |
} |
| 426 |
|
| 427 |
void GLFlagWindow::paintGL() |
| 428 |
{ |
| 429 |
// draw lines for the flags |
| 430 |
if (_flagLayout) { |
| 431 |
// it can happen that the GL widget gets replaced internally (SoQt only, not with quarter) |
| 432 |
// which causes to destroy the FlagLayout instance |
| 433 |
int ct = _flagLayout->count(); |
| 434 |
const SbViewportRegion vp = _viewer->getSoRenderManager()->getViewportRegion(); |
| 435 |
SbVec2s size = vp.getViewportSizePixels(); |
| 436 |
float aspectratio = float(size[0]) / float(size[1]); |
| 437 |
SbViewVolume vv = _viewer->getSoRenderManager()->getCamera()->getViewVolume(aspectratio); |
| 438 |
for (int i = 0; i < ct; i++) { |
| 439 |
Flag* flag = qobject_cast<Flag*>(_flagLayout->itemAt(i)->widget()); |
| 440 |
if (flag) { |
| 441 |
SbVec3f pt = flag->getOrigin(); |
| 442 |
vv.projectToScreen(pt, pt); |
| 443 |
int tox = (int)(pt[0] * size[0]); |
| 444 |
int toy = (int)((1.0f - pt[1]) * size[1]); |
| 445 |
flag->drawLine(_viewer, tox, toy); |
| 446 |
} |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
#include "moc_Flag.cpp" |