FreeCAD

Форк
0
/
TaskCosVertex.cpp 
397 строк · 12.4 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2019 WandererFan <wandererfan@gmail.com>                *
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
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
# include <cmath>
26
# include <QStatusBar>
27
#endif // #ifndef _PreComp_
28

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>
44

45
#include "ui_TaskCosVertex.h"
46
#include "TaskCosVertex.h"
47
#include "MDIViewPage.h"
48
#include "QGIView.h"
49
#include "QGSPage.h"
50
#include "QGTracker.h"
51
#include "Rez.h"
52
#include "ViewProviderPage.h"
53

54

55
using namespace Gui;
56
using namespace TechDraw;
57
using namespace TechDrawGui;
58
using DU = DrawUtil;
59

60
TaskCosVertex::TaskCosVertex(TechDraw::DrawViewPart* baseFeat,
61
                               TechDraw::DrawPage* page) :
62
    ui(new Ui_TaskCosVertex),
63
    blockUpdate(false),
64
    m_tracker(nullptr),
65
    m_baseFeat(baseFeat),
66
    m_basePage(page),
67
    m_qgParent(nullptr),
68
    m_trackerMode(QGTracker::None),
69
    m_saveContextPolicy(Qt::DefaultContextMenu),
70
    m_inProgressLock(false),
71
    m_btnOK(nullptr),
72
    m_btnCancel(nullptr),
73
    m_pbTrackerState(TRACKERPICK),
74
    m_savePoint(QPointF(0.0, 0.0))
75
{
76
    //baseFeat and page existence checked in cosmetic vertex command (CommandAnnotate.cpp)
77

78
    ui->setupUi(this);
79

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);
83

84
    setUiPrimary();
85

86
    connect(ui->pbTracker, &QPushButton::clicked,
87
            this, &TaskCosVertex::onTrackerClicked);
88

89
    m_trackerMode = QGTracker::TrackerMode::Point;
90
}
91

92
void TaskCosVertex::updateTask()
93
{
94
    //    blockUpdate = true;
95

96
    //    blockUpdate = false;
97
}
98

99
void TaskCosVertex::changeEvent(QEvent* event)
100
{
101
    if (event->type() == QEvent::LanguageChange) {
102
        ui->retranslateUi(this);
103
    }
104
}
105

106
void TaskCosVertex::setUiPrimary()
107
{
108
//    Base::Console().Message("TCV::setUiPrimary()\n");
109
    setWindowTitle(QObject::tr("New Cosmetic Vertex"));
110

111
    if (m_baseFeat) {
112
        std::string baseName = m_baseFeat->getNameInDocument();
113
        ui->leBaseView->setText(Base::Tools::fromStdString(baseName));
114
    }
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);
124
}
125

126
// set the ui x,y to apparent coords (ie invertY)
127
void TaskCosVertex::updateUi()
128
{
129
    double x = m_savePoint.x();
130
    double y = - m_savePoint.y();
131
    ui->dsbX->setValue(x);
132
    ui->dsbY->setValue(y);
133
}
134

135
//! create the cv at an unscaled, unrotated position
136
void TaskCosVertex::addCosVertex(QPointF qPos)
137
{
138
    Gui::Command::openCommand(QT_TRANSLATE_NOOP("Command", "Add Cosmetic Vertex"));
139

140
    Base::Vector3d pos = DU::invertY(DU::toVector3d(qPos));
141
//    int idx =
142
    (void) m_baseFeat->addCosmeticVertex(pos);
143
    m_baseFeat->requestPaint();
144

145
    Gui::Command::commitCommand();
146
}
147

148

149
//********** Tracker routines *******************************************************************
150
void TaskCosVertex::onTrackerClicked(bool clicked)
151
{
152
    Q_UNUSED(clicked);
153
//    Base::Console().Message("TCV::onTrackerClicked() m_pbTrackerState: %d\n",
154
//                            m_pbTrackerState);
155

156
    removeTracker();
157

158
    if (m_pbTrackerState == TRACKERCANCEL) {
159
        m_pbTrackerState = TRACKERPICK;
160
        ui->pbTracker->setText(tr("Pick Points"));
161
        enableTaskButtons(true);
162

163
        setEditCursor(Qt::ArrowCursor);
164
        return;
165
    }
166

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);
172
    startTracker();
173

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);
181
}
182

183
void TaskCosVertex::startTracker()
184
{
185
//    Base::Console().Message("TCV::startTracker()\n");
186
    if (m_trackerMode == QGTracker::TrackerMode::None) {
187
        return;
188
    }
189

190
    if (!m_tracker) {
191
        m_tracker = new QGTracker(m_vpp->getQGSPage(), m_trackerMode);
192
        QObject::connect(
193
            m_tracker, &QGTracker::drawingFinished,
194
            this, &TaskCosVertex::onTrackerFinished
195
        );
196
    }
197
    else {
198
        //this is too harsh. but need to avoid restarting process
199
        throw Base::RuntimeError("TechDrawNewLeader - tracker already active\n");
200
    }
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);
205
}
206

207
void TaskCosVertex::onTrackerFinished(std::vector<QPointF> pts, QGIView* qgParent)
208
{
209
    //    Base::Console().Message("TCV::onTrackerFinished()\n");
210
    (void)qgParent;
211
    if (pts.empty()) {
212
        Base::Console().Error("TaskCosVertex - no points available\n");
213
        return;
214
    }
215

216
    QPointF dragEnd = pts.front();            //scene pos of mouse click
217

218
    double x = Rez::guiX(m_baseFeat->X.getValue());
219
    double y = Rez::guiX(m_baseFeat->Y.getValue());
220

221
    DrawViewPart* dvp = m_baseFeat;
222
    DrawProjGroupItem* dpgi = dynamic_cast<DrawProjGroupItem*>(dvp);
223
    if (dpgi) {
224
        DrawProjGroup* dpg = dpgi->getPGroup();
225
        if (!dpg) {
226
            Base::Console().Message("TCV:onTrackerFinished - projection group is confused\n");
227
            //TODO::throw something.
228
            return;
229
        }
230
        x += Rez::guiX(dpg->X.getValue());
231
        y += Rez::guiX(dpg->Y.getValue());
232
    }
233
    //x, y are scene pos of dvp/dpgi
234

235
    QPointF basePosScene(x, -y);                 //base position in scene coords
236
    QPointF displace = dragEnd - basePosScene;
237
    QPointF scenePosCV = displace;
238

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));
242

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));
247

248
    m_savePoint = Rez::appX(scenePosCV);
249
    updateUi();
250

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);
259

260
}
261

262
void TaskCosVertex::removeTracker()
263
{
264
//    Base::Console().Message("TCV::removeTracker()\n");
265
    if (m_tracker && m_tracker->scene()) {
266
        m_vpp->getQGSPage()->removeItem(m_tracker);
267
        delete m_tracker;
268
        m_tracker = nullptr;
269
    }
270
}
271

272
void TaskCosVertex::setEditCursor(QCursor cursor)
273
{
274
    if (m_baseFeat) {
275
        QGIView* qgivBase = m_vpp->getQGSPage()->findQViewForDocObj(m_baseFeat);
276
        qgivBase->setCursor(cursor);
277
    }
278
}
279

280
void TaskCosVertex::abandonEditSession()
281
{
282
    QString msg = tr("In progress edit abandoned. Start over.");
283
    getMainWindow()->statusBar()->show();
284
    Gui::getMainWindow()->showMessage(msg, 4000);
285

286
    ui->pbTracker->setEnabled(true);
287

288
    setEditCursor(Qt::ArrowCursor);
289
}
290

291
void TaskCosVertex::saveButtons(QPushButton* btnOK,
292
                             QPushButton* btnCancel)
293
{
294
    m_btnOK = btnOK;
295
    m_btnCancel = btnCancel;
296
}
297

298
void TaskCosVertex::enableTaskButtons(bool button)
299
{
300
    m_btnOK->setEnabled(button);
301
    m_btnCancel->setEnabled(button);
302
}
303

304
//******************************************************************************
305
bool TaskCosVertex::accept()
306
{
307
    Gui::Document* doc = Gui::Application::Instance->getDocument(m_basePage->getDocument());
308
    if (!doc)
309
        return false;
310

311
    removeTracker();
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);
317

318
    addCosVertex(uiPoint);
319

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()");
325

326
    return true;
327
}
328

329
bool TaskCosVertex::reject()
330
{
331
    Gui::Document* doc = Gui::Application::Instance->getDocument(m_basePage->getDocument());
332
    if (!doc)
333
        return false;
334

335
    removeTracker();
336
    m_trackerMode = QGTracker::TrackerMode::None;
337
    if (m_vpp->getMDIViewPage()) {
338
        m_vpp->getMDIViewPage()->setContextMenuPolicy(m_saveContextPolicy);
339
    }
340

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()");
344

345
    return false;
346
}
347

348
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
349
TaskDlgCosVertex::TaskDlgCosVertex(TechDraw::DrawViewPart* baseFeat,
350
                                     TechDraw::DrawPage* page)
351
    : TaskDialog()
352
{
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);
358
}
359

360
TaskDlgCosVertex::~TaskDlgCosVertex()
361
{
362
}
363

364
void TaskDlgCosVertex::update()
365
{
366
//    widget->updateTask();
367
}
368

369
void TaskDlgCosVertex::modifyStandardButtons(QDialogButtonBox* box)
370
{
371
    QPushButton* btnOK = box->button(QDialogButtonBox::Ok);
372
    QPushButton* btnCancel = box->button(QDialogButtonBox::Cancel);
373
    widget->saveButtons(btnOK, btnCancel);
374
}
375

376
//==== calls from the TaskView ===============================================================
377
void TaskDlgCosVertex::open()
378
{
379
}
380

381
void TaskDlgCosVertex::clicked(int)
382
{
383
}
384

385
bool TaskDlgCosVertex::accept()
386
{
387
    widget->accept();
388
    return true;
389
}
390

391
bool TaskDlgCosVertex::reject()
392
{
393
    widget->reject();
394
    return true;
395
}
396

397
#include <Mod/TechDraw/Gui/moc_TaskCosVertex.cpp>
398

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.