1
/***************************************************************************
2
* Copyright (c) 2017 Werner Mayer <wmayer[at]users.sourceforge.net> *
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"
29
#include <App/Document.h>
30
#include <Gui/Application.h>
31
#include <Gui/Command.h>
32
#include <Gui/Document.h>
33
#include <Gui/SelectionObject.h>
34
#include <Gui/Widgets.h>
35
#include <Mod/Part/Gui/ViewProvider.h>
37
#include "TaskFilling.h"
38
#include "TaskFillingVertex.h"
39
#include "ui_TaskFillingVertex.h"
42
using namespace SurfaceGui;
47
class FillingVertexPanel::VertexSelection: public Gui::SelectionFilterGate
50
VertexSelection(FillingVertexPanel::SelectionMode& mode, Surface::Filling* editedObject)
51
: Gui::SelectionFilterGate(nullPointer())
53
, editedObject(editedObject)
55
~VertexSelection() override
57
mode = FillingVertexPanel::None;
60
* Allow the user to pick only edges.
62
bool allow(App::Document*, App::DocumentObject* pObj, const char* sSubName) override
64
// don't allow references to itself
65
if (pObj == editedObject) {
68
if (!pObj->isDerivedFrom(Part::Feature::getClassTypeId())) {
72
if (!sSubName || sSubName[0] == '\0') {
77
case FillingVertexPanel::AppendVertex:
78
return allowVertex(true, pObj, sSubName);
79
case FillingVertexPanel::RemoveVertex:
80
return allowVertex(false, pObj, sSubName);
87
bool allowVertex(bool appendVertex, App::DocumentObject* pObj, const char* sSubName)
89
std::string element(sSubName);
90
if (element.substr(0, 6) != "Vertex") {
94
auto links = editedObject->Points.getSubListValues();
95
for (const auto& it : links) {
96
if (it.first == pObj) {
97
for (const auto& jt : it.second) {
109
FillingVertexPanel::SelectionMode& mode;
110
Surface::Filling* editedObject;
113
// ----------------------------------------------------------------------------
115
FillingVertexPanel::FillingVertexPanel(ViewProviderFilling* vp, Surface::Filling* obj)
117
ui = new Ui_TaskFillingVertex();
121
selectionMode = None;
124
setEditedObject(obj);
126
// Create context menu
127
QAction* action = new QAction(tr("Remove"), this);
128
action->setShortcut(QString::fromLatin1("Del"));
129
action->setShortcutContext(Qt::WidgetShortcut);
130
ui->listFreeVertex->addAction(action);
131
connect(action, &QAction::triggered, this, &FillingVertexPanel::onDeleteVertex);
132
ui->listFreeVertex->setContextMenuPolicy(Qt::ActionsContextMenu);
136
* Destroys the object and frees any allocated resources
138
FillingVertexPanel::~FillingVertexPanel()
140
// no need to delete child widgets, Qt does it all for us
142
Gui::Selection().rmvSelectionGate();
145
void FillingVertexPanel::setupConnections()
147
connect(ui->buttonVertexAdd,
148
&QToolButton::toggled,
150
&FillingVertexPanel::onButtonVertexAddToggled);
151
connect(ui->buttonVertexRemove,
152
&QToolButton::toggled,
154
&FillingVertexPanel::onButtonVertexRemoveToggled);
157
void FillingVertexPanel::appendButtons(Gui::ButtonGroup* buttonGroup)
159
buttonGroup->addButton(ui->buttonVertexAdd, int(SelectionMode::AppendVertex));
160
buttonGroup->addButton(ui->buttonVertexRemove, int(SelectionMode::RemoveVertex));
163
// stores object pointer, its old fill type and adjusts radio buttons according to it.
164
void FillingVertexPanel::setEditedObject(Surface::Filling* obj)
168
auto objects = editedObject->Points.getValues();
169
auto element = editedObject->Points.getSubValues();
170
auto it = objects.begin();
171
auto jt = element.begin();
173
App::Document* doc = editedObject->getDocument();
174
for (; it != objects.end() && jt != element.end(); ++it, ++jt) {
175
QListWidgetItem* item = new QListWidgetItem(ui->listFreeVertex);
176
ui->listFreeVertex->addItem(item);
178
QString text = QString::fromLatin1("%1.%2").arg(QString::fromUtf8((*it)->Label.getValue()),
179
QString::fromStdString(*jt));
182
QList<QVariant> data;
183
data << QByteArray(doc->getName());
184
data << QByteArray((*it)->getNameInDocument());
185
data << QByteArray(jt->c_str());
186
item->setData(Qt::UserRole, data);
188
attachDocument(Gui::Application::Instance->getDocument(doc));
191
void FillingVertexPanel::changeEvent(QEvent* e)
193
if (e->type() == QEvent::LanguageChange) {
194
ui->retranslateUi(this);
197
QWidget::changeEvent(e);
201
void FillingVertexPanel::open()
204
this->vp->highlightReferences(ViewProviderFilling::Vertex,
205
editedObject->Points.getSubListValues(),
207
Gui::Selection().clearSelection();
210
void FillingVertexPanel::reject()
212
this->vp->highlightReferences(ViewProviderFilling::Vertex,
213
editedObject->Points.getSubListValues(),
217
void FillingVertexPanel::clearSelection()
219
Gui::Selection().clearSelection();
222
void FillingVertexPanel::checkOpenCommand()
224
if (checkCommand && !Gui::Command::hasPendingCommand()) {
225
std::string Msg("Edit ");
226
Msg += editedObject->Label.getValue();
227
Gui::Command::openCommand(Msg.c_str());
228
checkCommand = false;
232
void FillingVertexPanel::slotUndoDocument(const Gui::Document&)
237
void FillingVertexPanel::slotRedoDocument(const Gui::Document&)
242
void FillingVertexPanel::slotDeletedObject(const Gui::ViewProviderDocumentObject& Obj)
244
// If this view provider is being deleted then reset the colors of
245
// referenced part objects. The dialog will be deleted later.
246
if (this->vp == &Obj) {
247
this->vp->highlightReferences(ViewProviderFilling::Vertex,
248
editedObject->Points.getSubListValues(),
253
void FillingVertexPanel::onButtonVertexAddToggled(bool checked)
256
// 'selectionMode' is passed by reference and changed when the filter is deleted
257
Gui::Selection().addSelectionGate(new VertexSelection(selectionMode, editedObject));
258
selectionMode = AppendVertex;
260
else if (selectionMode == AppendVertex) {
265
void FillingVertexPanel::onButtonVertexRemoveToggled(bool checked)
268
// 'selectionMode' is passed by reference and changed when the filter is deleted
269
Gui::Selection().addSelectionGate(new VertexSelection(selectionMode, editedObject));
270
selectionMode = RemoveVertex;
272
else if (selectionMode == RemoveVertex) {
277
void FillingVertexPanel::onSelectionChanged(const Gui::SelectionChanges& msg)
279
if (selectionMode == None) {
283
if (msg.Type == Gui::SelectionChanges::AddSelection) {
285
if (selectionMode == AppendVertex) {
286
QListWidgetItem* item = new QListWidgetItem(ui->listFreeVertex);
287
ui->listFreeVertex->addItem(item);
289
Gui::SelectionObject sel(msg);
290
QString text = QString::fromLatin1("%1.%2").arg(
291
QString::fromUtf8(sel.getObject()->Label.getValue()),
292
QString::fromLatin1(msg.pSubName));
295
QList<QVariant> data;
296
data << QByteArray(msg.pDocName);
297
data << QByteArray(msg.pObjectName);
298
data << QByteArray(msg.pSubName);
299
item->setData(Qt::UserRole, data);
301
auto objects = editedObject->Points.getValues();
302
objects.push_back(sel.getObject());
303
auto element = editedObject->Points.getSubValues();
304
element.emplace_back(msg.pSubName);
305
editedObject->Points.setValues(objects, element);
306
this->vp->highlightReferences(ViewProviderFilling::Vertex,
307
editedObject->Points.getSubListValues(),
310
else if (selectionMode == RemoveVertex) {
311
Gui::SelectionObject sel(msg);
312
QList<QVariant> data;
313
data << QByteArray(msg.pDocName);
314
data << QByteArray(msg.pObjectName);
315
data << QByteArray(msg.pSubName);
316
for (int i = 0; i < ui->listFreeVertex->count(); i++) {
317
QListWidgetItem* item = ui->listFreeVertex->item(i);
318
if (item && item->data(Qt::UserRole) == data) {
319
ui->listFreeVertex->takeItem(i);
324
this->vp->highlightReferences(ViewProviderFilling::Vertex,
325
editedObject->Points.getSubListValues(),
327
App::DocumentObject* obj = sel.getObject();
328
std::string sub = msg.pSubName;
329
auto objects = editedObject->Points.getValues();
330
auto element = editedObject->Points.getSubValues();
331
auto it = objects.begin();
332
auto jt = element.begin();
333
for (; it != objects.end() && jt != element.end(); ++it, ++jt) {
334
if (*it == obj && *jt == sub) {
337
editedObject->Points.setValues(objects, element);
341
this->vp->highlightReferences(ViewProviderFilling::Vertex,
342
editedObject->Points.getSubListValues(),
346
editedObject->recomputeFeature();
347
QTimer::singleShot(50, this, &FillingVertexPanel::clearSelection);
351
void FillingVertexPanel::onDeleteVertex()
353
int row = ui->listFreeVertex->currentRow();
354
QListWidgetItem* item = ui->listFreeVertex->item(row);
357
QList<QVariant> data;
358
data = item->data(Qt::UserRole).toList();
359
ui->listFreeVertex->takeItem(row);
362
App::Document* doc = App::GetApplication().getDocument(data[0].toByteArray());
363
App::DocumentObject* obj = doc ? doc->getObject(data[1].toByteArray()) : nullptr;
364
std::string sub = data[2].toByteArray().constData();
365
auto objects = editedObject->Points.getValues();
366
auto element = editedObject->Points.getSubValues();
367
auto it = objects.begin();
368
auto jt = element.begin();
369
this->vp->highlightReferences(ViewProviderFilling::Vertex,
370
editedObject->Points.getSubListValues(),
373
for (; it != objects.end() && jt != element.end(); ++it, ++jt) {
374
if (*it == obj && *jt == sub) {
377
editedObject->Points.setValues(objects, element);
378
editedObject->recomputeFeature();
383
this->vp->highlightReferences(ViewProviderFilling::Vertex,
384
editedObject->Points.getSubListValues(),
389
void FillingVertexPanel::exitSelectionMode()
391
// 'selectionMode' is passed by reference to the filter and changed when the filter is deleted
392
Gui::Selection().clearSelection();
393
Gui::Selection().rmvSelectionGate();
396
} // namespace SurfaceGui
398
#include "moc_TaskFillingVertex.cpp"