1
/***************************************************************************
2
* Copyright (c) 2018 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"
26
#endif // #ifndef _PreComp_
28
#include <Base/Console.h>
29
#include <Base/Tools.h>
30
#include <Gui/Application.h>
31
#include <Gui/BitmapFactory.h>
32
#include <Gui/Command.h>
33
#include <Gui/Selection.h>
34
#include <Gui/ViewProvider.h>
35
#include <Mod/TechDraw/App/DrawUtil.h>
36
#include <Mod/TechDraw/App/DrawViewPart.h>
37
#include <Mod/TechDraw/App/CenterLine.h>
38
#include <Mod/TechDraw/App/Geometry.h>
39
#include <Mod/TechDraw/App/LineGenerator.h>
42
#include "TaskLineDecor.h"
43
#include "ui_TaskLineDecor.h"
44
#include "ui_TaskRestoreLines.h"
46
#include "ViewProviderViewPart.h"
47
#include "DrawGuiUtil.h"
51
using namespace TechDraw;
52
using namespace TechDrawGui;
54
TaskLineDecor::TaskLineDecor(TechDraw::DrawViewPart* partFeat,
55
std::vector<std::string> edgeNames) :
56
ui(new Ui_TaskLineDecor),
61
initializeRejectArrays();
62
m_lineGenerator = new TechDraw::LineGenerator;
69
connect(ui->cb_Style, qOverload<int>(&QComboBox::currentIndexChanged), this, &TaskLineDecor::onStyleChanged);
70
connect(ui->cc_Color, &ColorButton::changed, this, &TaskLineDecor::onColorChanged);
71
connect(ui->dsb_Weight, qOverload<double>(&QuantitySpinBox::valueChanged), this, &TaskLineDecor::onWeightChanged);
72
connect(ui->cb_Visible, qOverload<int>(&QComboBox::currentIndexChanged), this, &TaskLineDecor::onVisibleChanged);
75
TaskLineDecor::~TaskLineDecor()
77
delete m_lineGenerator;
80
void TaskLineDecor::initUi()
82
std::string viewName = m_partFeat->getNameInDocument();
83
ui->le_View->setText(Base::Tools::fromStdString(viewName));
86
for (auto& e: m_edges) {
87
int num = DrawUtil::getIndexFromName(e);
90
std::string temp = ss.str();
92
temp.resize(temp.length() - 2);
94
ui->le_Lines->setText(Base::Tools::fromStdString(temp));
96
ui->cc_Color->setColor(m_color.asValue<QColor>());
97
ui->dsb_Weight->setValue(m_weight);
98
ui->dsb_Weight->setSingleStep(0.1);
99
ui->cb_Visible->setCurrentIndex(m_visible);
101
// line numbering starts at 1, not 0
102
DrawGuiUtil::loadLineStyleChoices(ui->cb_Style, m_lineGenerator);
103
if (ui->cb_Style->count() >= m_lineNumber ) {
104
ui->cb_Style->setCurrentIndex(m_lineNumber - 1);
108
TechDraw::LineFormat *TaskLineDecor::getFormatAccessPtr(const std::string &edgeName, std::string *newFormatTag)
110
BaseGeomPtr bg = m_partFeat->getEdge(edgeName);
112
if (bg->getCosmetic()) {
113
if (bg->source() == SourceType::COSEDGE) {
114
TechDraw::CosmeticEdge *ce = m_partFeat->getCosmeticEdgeBySelection(edgeName);
116
return &ce->m_format;
119
else if (bg->source() == SourceType::CENTERLINE) {
120
TechDraw::CenterLine *cl = m_partFeat->getCenterLineBySelection(edgeName);
122
return &cl->m_format;
127
TechDraw::GeomFormat *gf = m_partFeat->getGeomFormatBySelection(edgeName);
129
return &gf->m_format;
132
ViewProviderViewPart *viewPart = dynamic_cast<ViewProviderViewPart *>(QGIView::getViewProvider(m_partFeat));
134
TechDraw::LineFormat lineFormat(Qt::SolidLine, viewPart->LineWidth.getValue(), LineFormat::getDefEdgeColor(), true);
135
TechDraw::GeomFormat geomFormat(DrawUtil::getIndexFromName(edgeName), lineFormat);
137
std::string formatTag = m_partFeat->addGeomFormat(&geomFormat);
139
*newFormatTag = formatTag;
142
return &m_partFeat->getGeomFormat(formatTag)->m_format;
150
void TaskLineDecor::initializeRejectArrays()
152
m_originalFormats.resize(m_edges.size());
153
m_createdFormatTags.resize(m_edges.size());
155
for (size_t i = 0; i < m_edges.size(); ++i) {
157
TechDraw::LineFormat *accessPtr = getFormatAccessPtr(m_edges[i], &newTag);
160
m_originalFormats[i] = *accessPtr;
161
if (!newTag.empty()) {
162
m_createdFormatTags[i] = newTag;
168
void TaskLineDecor::getDefaults()
170
// Base::Console().Message("TLD::getDefaults()\n");
171
m_color = LineFormat::getDefEdgeColor();
172
m_weight = LineFormat::getDefEdgeWidth();
176
//set defaults to format of 1st edge
177
if (!m_originalFormats.empty()) {
178
LineFormat &lf = m_originalFormats.front();
179
m_style = lf.m_style;
180
m_color = lf.m_color;
181
m_weight = lf.m_weight;
182
m_visible = lf.m_visible;
183
m_lineNumber = lf.getLineNumber();
187
void TaskLineDecor::onStyleChanged()
189
m_lineNumber = ui->cb_Style->currentIndex() + 1;
191
m_partFeat->requestPaint();
194
void TaskLineDecor::onColorChanged()
196
m_color.setValue<QColor>(ui->cc_Color->color());
198
m_partFeat->requestPaint();
201
void TaskLineDecor::onWeightChanged()
203
m_weight = ui->dsb_Weight->value().getValue();
205
m_partFeat->requestPaint();
208
void TaskLineDecor::onVisibleChanged()
210
m_visible = ui->cb_Visible->currentIndex();
212
m_partFeat->requestPaint();
215
void TaskLineDecor::applyDecorations()
217
// Base::Console().Message("TLD::applyDecorations()\n");
218
for (auto& e: m_edges) {
219
LineFormat *lf = getFormatAccessPtr(e);
221
lf->m_style = m_style;
222
lf->m_color = m_color;
223
lf->m_weight = m_weight;
224
lf->m_visible = m_visible;
225
lf->setLineNumber(m_lineNumber);
230
bool TaskLineDecor::accept()
232
// Base::Console().Message("TLD::accept()\n");
233
Gui::Document* doc = Gui::Application::Instance->getDocument(m_partFeat->getDocument());
241
m_partFeat->requestPaint();
243
//Gui::Command::updateActive(); //no chain of updates here
244
Gui::Command::doCommand(Gui::Command::Gui, "Gui.ActiveDocument.resetEdit()");
249
bool TaskLineDecor::reject()
251
// Base::Console().Message("TLD::reject()\n");
252
Gui::Document* doc = Gui::Application::Instance->getDocument(m_partFeat->getDocument());
256
for (size_t i = 0; i < m_originalFormats.size(); ++i) {
257
std::string &formatTag = m_createdFormatTags[i];
258
if (formatTag.empty()) {
259
LineFormat *lf = getFormatAccessPtr(m_edges[i]);
261
*lf = m_originalFormats[i];
265
m_partFeat->removeGeomFormat(formatTag);
269
m_partFeat->requestPaint();
271
Gui::Command::doCommand(Gui::Command::Gui, "Gui.ActiveDocument.resetEdit()");
275
void TaskLineDecor::changeEvent(QEvent *e)
277
if (e->type() == QEvent::LanguageChange) {
278
ui->retranslateUi(this);
281
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
282
TaskRestoreLines::TaskRestoreLines(TechDraw::DrawViewPart* partFeat,
283
TaskLineDecor* parent) :
284
ui(new Ui_TaskRestoreLines),
285
m_partFeat(partFeat),
290
connect(ui->pb_All, &QPushButton::clicked, this, &TaskRestoreLines::onAllPressed);
291
connect(ui->pb_Geometry, &QPushButton::clicked, this, &TaskRestoreLines::onGeometryPressed);
292
connect(ui->pb_Cosmetic, &QPushButton::clicked, this, &TaskRestoreLines::onCosmeticPressed);
293
connect(ui->pb_Center, &QPushButton::clicked, this, &TaskRestoreLines::onCenterPressed);
298
TaskRestoreLines::~TaskRestoreLines()
302
void TaskRestoreLines::initUi()
304
ui->l_All->setText(QString::number(countInvisibleLines()));
305
ui->l_Geometry->setText(QString::number(countInvisibleGeoms()));
306
ui->l_Cosmetic->setText(QString::number(countInvisibleCosmetics()));
307
ui->l_Center->setText(QString::number(countInvisibleCenters()));
310
void TaskRestoreLines::onAllPressed()
312
// Base::Console().Message("TRL::onAllPressed()\n");
318
void TaskRestoreLines::onGeometryPressed()
320
// Base::Console().Message("TRL::onGeometryPressed()\n");
321
restoreInvisibleGeoms();
322
ui->l_Geometry->setText(QString::number(0));
323
ui->l_All->setText(QString::number(countInvisibleLines()));
326
void TaskRestoreLines::onCosmeticPressed()
328
// Base::Console().Message("TRL::onCosmeticPressed()\n");
329
restoreInvisibleCosmetics();
330
ui->l_Cosmetic->setText(QString::number(0));
331
ui->l_All->setText(QString::number(countInvisibleLines()));
334
void TaskRestoreLines::onCenterPressed()
336
// Base::Console().Message("TRL::onCenterPressed()\n");
337
restoreInvisibleCenters();
338
ui->l_Center->setText(QString::number(0));
339
ui->l_All->setText(QString::number(countInvisibleLines()));
342
int TaskRestoreLines::countInvisibleLines()
345
result += countInvisibleGeoms();
346
result += countInvisibleCosmetics();
347
result += countInvisibleCenters();
351
int TaskRestoreLines::countInvisibleGeoms()
354
const std::vector<TechDraw::GeomFormat*> geoms = m_partFeat->GeomFormats.getValues();
355
for (auto& g : geoms) {
356
if (!g->m_format.m_visible) {
363
int TaskRestoreLines::countInvisibleCosmetics()
366
const std::vector<TechDraw::CosmeticEdge*> cosmos = m_partFeat->CosmeticEdges.getValues();
367
for (auto& c : cosmos) {
368
if (!c->m_format.m_visible) {
375
int TaskRestoreLines::countInvisibleCenters()
378
const std::vector<TechDraw::CenterLine*> centers = m_partFeat->CenterLines.getValues();
379
for (auto& c : centers) {
380
if (!c->m_format.m_visible) {
387
void TaskRestoreLines::restoreInvisibleLines()
389
restoreInvisibleGeoms();
390
restoreInvisibleCosmetics();
391
restoreInvisibleCenters();
394
void TaskRestoreLines::restoreInvisibleGeoms()
396
const std::vector<TechDraw::GeomFormat*> geoms = m_partFeat->GeomFormats.getValues();
397
for (auto& g : geoms) {
398
if (!g->m_format.m_visible) {
399
g->m_format.m_visible = true;
402
m_partFeat->GeomFormats.setValues(geoms);
403
m_parent->apply(false); //don't undo the work we just did
406
void TaskRestoreLines::restoreInvisibleCosmetics()
408
const std::vector<TechDraw::CosmeticEdge*> cosmos = m_partFeat->CosmeticEdges.getValues();
409
for (auto& c : cosmos) {
410
if (!c->m_format.m_visible) {
411
c->m_format.m_visible = true;
414
m_partFeat->CosmeticEdges.setValues(cosmos);
415
m_parent->apply(false); //don't undo the work we just did
418
void TaskRestoreLines::restoreInvisibleCenters()
420
const std::vector<TechDraw::CenterLine*> centers = m_partFeat->CenterLines.getValues();
421
for (auto& c : centers) {
422
if (!c->m_format.m_visible) {
423
c->m_format.m_visible = true;
426
m_partFeat->CenterLines.setValues(centers);
427
m_parent->apply(false); //don't undo the work we just did
431
bool TaskRestoreLines::accept()
433
// Base::Console().Message("TRL::accept()\n");
437
bool TaskRestoreLines::reject()
439
// Base::Console().Message("TRL::reject()\n");
443
void TaskRestoreLines::changeEvent(QEvent *e)
445
if (e->type() == QEvent::LanguageChange) {
446
ui->retranslateUi(this);
450
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
451
TaskDlgLineDecor::TaskDlgLineDecor(TechDraw::DrawViewPart* partFeat,
452
std::vector<std::string> edgeNames) :
455
widget = new TaskLineDecor(partFeat, edgeNames);
456
taskbox = new Gui::TaskView::TaskBox(Gui::BitmapFactory().pixmap("actions/TechDraw_DecorateLine"),
457
widget->windowTitle(), true, nullptr);
458
taskbox->groupLayout()->addWidget(widget);
459
Content.push_back(taskbox);
460
if (edgeNames.empty()) {
461
taskbox->hideGroupBox();
464
TaskLineDecor* parent = dynamic_cast<TaskLineDecor*>(widget);
466
restore = new TaskRestoreLines(partFeat, parent);
467
restoreBox = new Gui::TaskView::TaskBox(Gui::BitmapFactory().pixmap("actions/TechDraw_DecorateLine"),
468
tr("Restore Invisible Lines"), true, nullptr);
469
restoreBox->groupLayout()->addWidget(restore);
470
Content.push_back(restoreBox);
474
TaskDlgLineDecor::~TaskDlgLineDecor()
478
//==== calls from the TaskView ===============================================================
479
void TaskDlgLineDecor::open()
484
void TaskDlgLineDecor::clicked(int i)
489
bool TaskDlgLineDecor::accept()
491
// Base::Console().Message("TDLD::accept()\n");
496
bool TaskDlgLineDecor::reject()
498
// Base::Console().Message("TDLD::reject()\n");
503
#include <Mod/TechDraw/Gui/moc_TaskLineDecor.cpp>