1
/***************************************************************************
2
* Copyright (c) 2019 WandererFan <wandererfan@gmail.com> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
27
#endif // #ifndef _PreComp_
29
#include <Base/Console.h>
30
#include <Base/Tools.h>
31
#include <Base/UnitsApi.h>
32
#include <Gui/Application.h>
33
#include <Gui/BitmapFactory.h>
34
#include <Gui/Command.h>
35
#include <Gui/Document.h>
36
#include <Gui/MainWindow.h>
37
#include <Gui/ViewProvider.h>
38
#include <Mod/TechDraw/App/DrawPage.h>
39
#include <Mod/TechDraw/App/DrawProjGroup.h>
40
#include <Mod/TechDraw/App/DrawProjGroupItem.h>
41
#include <Mod/TechDraw/App/DrawUtil.h>
42
#include <Mod/TechDraw/App/DrawViewPart.h>
43
#include <Mod/TechDraw/App/Cosmetic.h>
45
#include "ui_TaskCosVertex.h"
46
#include "TaskCosVertex.h"
47
#include "MDIViewPage.h"
52
#include "ViewProviderPage.h"
56
using namespace TechDraw;
57
using namespace TechDrawGui;
60
TaskCosVertex::TaskCosVertex(TechDraw::DrawViewPart* baseFeat,
61
TechDraw::DrawPage* page) :
62
ui(new Ui_TaskCosVertex),
68
m_trackerMode(QGTracker::None),
69
m_saveContextPolicy(Qt::DefaultContextMenu),
70
m_inProgressLock(false),
73
m_pbTrackerState(TRACKERPICK),
74
m_savePoint(QPointF(0.0, 0.0))
76
//baseFeat and page existence checked in cosmetic vertex command (CommandAnnotate.cpp)
80
Gui::Document* activeGui = Gui::Application::Instance->getDocument(m_basePage->getDocument());
81
Gui::ViewProvider* vp = activeGui->getViewProvider(m_basePage);
82
m_vpp = static_cast<ViewProviderPage*>(vp);
86
connect(ui->pbTracker, &QPushButton::clicked,
87
this, &TaskCosVertex::onTrackerClicked);
89
m_trackerMode = QGTracker::TrackerMode::Point;
92
void TaskCosVertex::updateTask()
94
// blockUpdate = true;
96
// blockUpdate = false;
99
void TaskCosVertex::changeEvent(QEvent* event)
101
if (event->type() == QEvent::LanguageChange) {
102
ui->retranslateUi(this);
106
void TaskCosVertex::setUiPrimary()
108
// Base::Console().Message("TCV::setUiPrimary()\n");
109
setWindowTitle(QObject::tr("New Cosmetic Vertex"));
112
std::string baseName = m_baseFeat->getNameInDocument();
113
ui->leBaseView->setText(Base::Tools::fromStdString(baseName));
115
ui->pbTracker->setText(tr("Point Picker"));
116
ui->pbTracker->setEnabled(true);
117
ui->dsbX->setEnabled(true);
118
ui->dsbY->setEnabled(true);
119
int decimals = Base::UnitsApi::getDecimals();
120
ui->dsbX->setDecimals(decimals);
121
ui->dsbY->setDecimals(decimals);
122
ui->dsbX->setUnit(Base::Unit::Length);
123
ui->dsbY->setUnit(Base::Unit::Length);
126
// set the ui x,y to apparent coords (ie invertY)
127
void TaskCosVertex::updateUi()
129
double x = m_savePoint.x();
130
double y = - m_savePoint.y();
131
ui->dsbX->setValue(x);
132
ui->dsbY->setValue(y);
135
//! create the cv at an unscaled, unrotated position
136
void TaskCosVertex::addCosVertex(QPointF qPos)
138
Gui::Command::openCommand(QT_TRANSLATE_NOOP("Command", "Add Cosmetic Vertex"));
140
Base::Vector3d pos = DU::invertY(DU::toVector3d(qPos));
142
(void) m_baseFeat->addCosmeticVertex(pos);
143
m_baseFeat->requestPaint();
145
Gui::Command::commitCommand();
149
//********** Tracker routines *******************************************************************
150
void TaskCosVertex::onTrackerClicked(bool clicked)
153
// Base::Console().Message("TCV::onTrackerClicked() m_pbTrackerState: %d\n",
158
if (m_pbTrackerState == TRACKERCANCEL) {
159
m_pbTrackerState = TRACKERPICK;
160
ui->pbTracker->setText(tr("Pick Points"));
161
enableTaskButtons(true);
163
setEditCursor(Qt::ArrowCursor);
167
m_inProgressLock = true;
168
m_saveContextPolicy = m_vpp->getMDIViewPage()->contextMenuPolicy();
169
m_vpp->getMDIViewPage()->setContextMenuPolicy(Qt::PreventContextMenu);
170
m_trackerMode = QGTracker::TrackerMode::Point;
171
setEditCursor(Qt::CrossCursor);
174
QString msg = tr("Pick a point for cosmetic vertex");
175
getMainWindow()->statusBar()->show();
176
Gui::getMainWindow()->showMessage(msg, 3000);
177
ui->pbTracker->setText(tr("Escape picking"));
178
ui->pbTracker->setEnabled(true);
179
m_pbTrackerState = TRACKERCANCEL;
180
enableTaskButtons(false);
183
void TaskCosVertex::startTracker()
185
// Base::Console().Message("TCV::startTracker()\n");
186
if (m_trackerMode == QGTracker::TrackerMode::None) {
191
m_tracker = new QGTracker(m_vpp->getQGSPage(), m_trackerMode);
193
m_tracker, &QGTracker::drawingFinished,
194
this, &TaskCosVertex::onTrackerFinished
198
//this is too harsh. but need to avoid restarting process
199
throw Base::RuntimeError("TechDrawNewLeader - tracker already active\n");
201
setEditCursor(Qt::CrossCursor);
202
QString msg = tr("Left click to set a point");
203
Gui::getMainWindow()->statusBar()->show();
204
Gui::getMainWindow()->showMessage(msg, 3000);
207
void TaskCosVertex::onTrackerFinished(std::vector<QPointF> pts, QGIView* qgParent)
209
// Base::Console().Message("TCV::onTrackerFinished()\n");
212
Base::Console().Error("TaskCosVertex - no points available\n");
216
QPointF dragEnd = pts.front(); //scene pos of mouse click
218
double x = Rez::guiX(m_baseFeat->X.getValue());
219
double y = Rez::guiX(m_baseFeat->Y.getValue());
221
DrawViewPart* dvp = m_baseFeat;
222
DrawProjGroupItem* dpgi = dynamic_cast<DrawProjGroupItem*>(dvp);
224
DrawProjGroup* dpg = dpgi->getPGroup();
226
Base::Console().Message("TCV:onTrackerFinished - projection group is confused\n");
227
//TODO::throw something.
230
x += Rez::guiX(dpg->X.getValue());
231
y += Rez::guiX(dpg->Y.getValue());
233
//x, y are scene pos of dvp/dpgi
235
QPointF basePosScene(x, -y); //base position in scene coords
236
QPointF displace = dragEnd - basePosScene;
237
QPointF scenePosCV = displace;
239
// Invert Y value so the math works.
240
// scenePosCV is effectively a scaled (and rotated) value
241
Base::Vector3d posToRotate = DU::invertY(DU::toVector3d(scenePosCV));
243
// unscale and rotate the picked point
244
posToRotate = CosmeticVertex::makeCanonicalPoint(m_baseFeat, posToRotate);
245
// now put Y value back to display form
246
scenePosCV = DU::toQPointF(DU::invertY(posToRotate));
248
m_savePoint = Rez::appX(scenePosCV);
251
m_tracker->sleep(true);
252
m_inProgressLock = false;
253
m_pbTrackerState = TRACKERPICK;
254
ui->pbTracker->setText(tr("Pick Points"));
255
ui->pbTracker->setEnabled(true);
256
enableTaskButtons(true);
257
setEditCursor(Qt::ArrowCursor);
258
m_vpp->getMDIViewPage()->setContextMenuPolicy(m_saveContextPolicy);
262
void TaskCosVertex::removeTracker()
264
// Base::Console().Message("TCV::removeTracker()\n");
265
if (m_tracker && m_tracker->scene()) {
266
m_vpp->getQGSPage()->removeItem(m_tracker);
272
void TaskCosVertex::setEditCursor(QCursor cursor)
275
QGIView* qgivBase = m_vpp->getQGSPage()->findQViewForDocObj(m_baseFeat);
276
qgivBase->setCursor(cursor);
280
void TaskCosVertex::abandonEditSession()
282
QString msg = tr("In progress edit abandoned. Start over.");
283
getMainWindow()->statusBar()->show();
284
Gui::getMainWindow()->showMessage(msg, 4000);
286
ui->pbTracker->setEnabled(true);
288
setEditCursor(Qt::ArrowCursor);
291
void TaskCosVertex::saveButtons(QPushButton* btnOK,
292
QPushButton* btnCancel)
295
m_btnCancel = btnCancel;
298
void TaskCosVertex::enableTaskButtons(bool button)
300
m_btnOK->setEnabled(button);
301
m_btnCancel->setEnabled(button);
304
//******************************************************************************
305
bool TaskCosVertex::accept()
307
Gui::Document* doc = Gui::Application::Instance->getDocument(m_basePage->getDocument());
312
// whatever is in the ui for x,y is treated as an unscaled, unrotated, invertedY position.
313
// the position from the tracker is unscaled & unrotated before updating the ui
314
double x = ui->dsbX->value().getValue();
315
double y = ui->dsbY->value().getValue();
316
QPointF uiPoint(x, -y);
318
addCosVertex(uiPoint);
320
m_baseFeat->recomputeFeature();
321
m_baseFeat->requestPaint();
322
m_vpp->getMDIViewPage()->setContextMenuPolicy(m_saveContextPolicy);
323
m_trackerMode = QGTracker::TrackerMode::None;
324
Gui::Command::doCommand(Gui::Command::Gui, "Gui.ActiveDocument.resetEdit()");
329
bool TaskCosVertex::reject()
331
Gui::Document* doc = Gui::Application::Instance->getDocument(m_basePage->getDocument());
336
m_trackerMode = QGTracker::TrackerMode::None;
337
if (m_vpp->getMDIViewPage()) {
338
m_vpp->getMDIViewPage()->setContextMenuPolicy(m_saveContextPolicy);
341
//make sure any dangling objects are cleaned up
342
Gui::Command::doCommand(Gui::Command::Gui, "App.activeDocument().recompute()");
343
Gui::Command::doCommand(Gui::Command::Gui, "Gui.ActiveDocument.resetEdit()");
348
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
349
TaskDlgCosVertex::TaskDlgCosVertex(TechDraw::DrawViewPart* baseFeat,
350
TechDraw::DrawPage* page)
353
widget = new TaskCosVertex(baseFeat, page);
354
taskbox = new Gui::TaskView::TaskBox(Gui::BitmapFactory().pixmap("actions/TechDraw_CosmeticVertex"),
355
widget->windowTitle(), true, nullptr);
356
taskbox->groupLayout()->addWidget(widget);
357
Content.push_back(taskbox);
360
TaskDlgCosVertex::~TaskDlgCosVertex()
364
void TaskDlgCosVertex::update()
366
// widget->updateTask();
369
void TaskDlgCosVertex::modifyStandardButtons(QDialogButtonBox* box)
371
QPushButton* btnOK = box->button(QDialogButtonBox::Ok);
372
QPushButton* btnCancel = box->button(QDialogButtonBox::Cancel);
373
widget->saveButtons(btnOK, btnCancel);
376
//==== calls from the TaskView ===============================================================
377
void TaskDlgCosVertex::open()
381
void TaskDlgCosVertex::clicked(int)
385
bool TaskDlgCosVertex::accept()
391
bool TaskDlgCosVertex::reject()
397
#include <Mod/TechDraw/Gui/moc_TaskCosVertex.cpp>