FreeCAD

Форк
0
/
TaskLineDecor.cpp 
503 строки · 15.2 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2018 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
#endif // #ifndef _PreComp_
27

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

41

42
#include "TaskLineDecor.h"
43
#include "ui_TaskLineDecor.h"
44
#include "ui_TaskRestoreLines.h"
45
#include "QGIView.h"
46
#include "ViewProviderViewPart.h"
47
#include "DrawGuiUtil.h"
48

49

50
using namespace Gui;
51
using namespace TechDraw;
52
using namespace TechDrawGui;
53

54
TaskLineDecor::TaskLineDecor(TechDraw::DrawViewPart* partFeat,
55
                             std::vector<std::string> edgeNames) :
56
    ui(new Ui_TaskLineDecor),
57
    m_partFeat(partFeat),
58
    m_edges(edgeNames),
59
    m_apply(true)
60
{
61
    initializeRejectArrays();
62
    m_lineGenerator = new TechDraw::LineGenerator;
63

64
    ui->setupUi(this);
65

66
    getDefaults();
67
    initUi();
68

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);
73
}
74

75
TaskLineDecor::~TaskLineDecor()
76
{
77
    delete m_lineGenerator;
78
}
79

80
void TaskLineDecor::initUi()
81
{
82
    std::string viewName = m_partFeat->getNameInDocument();
83
    ui->le_View->setText(Base::Tools::fromStdString(viewName));
84

85
    std::stringstream ss;
86
    for (auto& e: m_edges) {
87
        int num = DrawUtil::getIndexFromName(e);
88
        ss << num << ", ";
89
    }
90
    std::string temp = ss.str();
91
    if (!temp.empty()) {
92
        temp.resize(temp.length() - 2);
93
    }
94
    ui->le_Lines->setText(Base::Tools::fromStdString(temp));
95

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

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);
105
    }
106
}
107

108
TechDraw::LineFormat *TaskLineDecor::getFormatAccessPtr(const std::string &edgeName, std::string *newFormatTag)
109
{
110
    BaseGeomPtr bg = m_partFeat->getEdge(edgeName);
111
    if (bg) {
112
        if (bg->getCosmetic()) {
113
            if (bg->source() == SourceType::COSEDGE) {
114
                TechDraw::CosmeticEdge *ce = m_partFeat->getCosmeticEdgeBySelection(edgeName);
115
                if (ce) {
116
                    return &ce->m_format;
117
                }
118
            }
119
            else if (bg->source() == SourceType::CENTERLINE) {
120
                TechDraw::CenterLine *cl = m_partFeat->getCenterLineBySelection(edgeName);
121
                if (cl) {
122
                    return &cl->m_format;
123
                }
124
            }
125
        }
126
        else {
127
            TechDraw::GeomFormat *gf = m_partFeat->getGeomFormatBySelection(edgeName);
128
            if (gf) {
129
                return &gf->m_format;
130
            }
131
            else {
132
                ViewProviderViewPart *viewPart = dynamic_cast<ViewProviderViewPart *>(QGIView::getViewProvider(m_partFeat));
133
                if (viewPart) {
134
                    TechDraw::LineFormat lineFormat(Qt::SolidLine, viewPart->LineWidth.getValue(), LineFormat::getDefEdgeColor(), true);
135
                    TechDraw::GeomFormat geomFormat(DrawUtil::getIndexFromName(edgeName), lineFormat);
136

137
                    std::string formatTag = m_partFeat->addGeomFormat(&geomFormat);
138
                    if (newFormatTag) {
139
                        *newFormatTag = formatTag;
140
                    }
141

142
                    return &m_partFeat->getGeomFormat(formatTag)->m_format;
143
                }
144
            }
145
        }
146
    }
147
    return {};
148
}
149

150
void TaskLineDecor::initializeRejectArrays()
151
{
152
    m_originalFormats.resize(m_edges.size());
153
    m_createdFormatTags.resize(m_edges.size());
154

155
    for (size_t i = 0; i < m_edges.size(); ++i) {
156
        std::string newTag;
157
        TechDraw::LineFormat *accessPtr = getFormatAccessPtr(m_edges[i], &newTag);
158

159
        if (accessPtr) {
160
            m_originalFormats[i] = *accessPtr;
161
            if (!newTag.empty()) {
162
                m_createdFormatTags[i] = newTag;
163
            }
164
        }
165
    }
166
}
167

168
void TaskLineDecor::getDefaults()
169
{
170
//    Base::Console().Message("TLD::getDefaults()\n");
171
    m_color = LineFormat::getDefEdgeColor();
172
    m_weight = LineFormat::getDefEdgeWidth();
173
    m_visible = true;
174
    m_lineNumber = 1;
175

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();
184
    }
185
}
186

187
void TaskLineDecor::onStyleChanged()
188
{
189
    m_lineNumber = ui->cb_Style->currentIndex() + 1;
190
    applyDecorations();
191
    m_partFeat->requestPaint();
192
}
193

194
void TaskLineDecor::onColorChanged()
195
{
196
    m_color.setValue<QColor>(ui->cc_Color->color());
197
    applyDecorations();
198
    m_partFeat->requestPaint();
199
}
200

201
void TaskLineDecor::onWeightChanged()
202
{
203
    m_weight = ui->dsb_Weight->value().getValue();
204
    applyDecorations();
205
    m_partFeat->requestPaint();
206
}
207

208
void TaskLineDecor::onVisibleChanged()
209
{
210
    m_visible = ui->cb_Visible->currentIndex();
211
    applyDecorations();
212
    m_partFeat->requestPaint();
213
}
214

215
void TaskLineDecor::applyDecorations()
216
{
217
//    Base::Console().Message("TLD::applyDecorations()\n");
218
    for (auto& e: m_edges) {
219
        LineFormat *lf = getFormatAccessPtr(e);
220
        if (lf) {
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);
226
        }
227
    }
228
}
229

230
bool TaskLineDecor::accept()
231
{
232
//    Base::Console().Message("TLD::accept()\n");
233
    Gui::Document* doc = Gui::Application::Instance->getDocument(m_partFeat->getDocument());
234
    if (!doc)
235
        return false;
236

237
    if (apply()) {
238
        applyDecorations();
239
    }
240

241
    m_partFeat->requestPaint();
242

243
    //Gui::Command::updateActive();     //no chain of updates here
244
    Gui::Command::doCommand(Gui::Command::Gui, "Gui.ActiveDocument.resetEdit()");
245

246
    return true;
247
}
248

249
bool TaskLineDecor::reject()
250
{
251
//    Base::Console().Message("TLD::reject()\n");
252
    Gui::Document* doc = Gui::Application::Instance->getDocument(m_partFeat->getDocument());
253
    if (!doc)
254
        return false;
255

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]);
260
            if (lf) {
261
                *lf = m_originalFormats[i];
262
            }
263
        }
264
        else {
265
            m_partFeat->removeGeomFormat(formatTag);
266
        }
267
    }
268

269
    m_partFeat->requestPaint();
270

271
    Gui::Command::doCommand(Gui::Command::Gui, "Gui.ActiveDocument.resetEdit()");
272
    return false;
273
}
274

275
void TaskLineDecor::changeEvent(QEvent *e)
276
{
277
    if (e->type() == QEvent::LanguageChange) {
278
        ui->retranslateUi(this);
279
    }
280
}
281
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
282
TaskRestoreLines::TaskRestoreLines(TechDraw::DrawViewPart* partFeat,
283
                                   TaskLineDecor* parent) :
284
    ui(new Ui_TaskRestoreLines),
285
    m_partFeat(partFeat),
286
    m_parent(parent)
287
{
288
    ui->setupUi(this);
289

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

295
    initUi();
296
}
297

298
TaskRestoreLines::~TaskRestoreLines()
299
{
300
}
301

302
void TaskRestoreLines::initUi()
303
{
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()));
308
}
309

310
void TaskRestoreLines::onAllPressed()
311
{
312
//    Base::Console().Message("TRL::onAllPressed()\n");
313
    onGeometryPressed();
314
    onCosmeticPressed();
315
    onCenterPressed();
316
}
317

318
void TaskRestoreLines::onGeometryPressed()
319
{
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()));
324
}
325

326
void TaskRestoreLines::onCosmeticPressed()
327
{
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()));
332
}
333

334
void TaskRestoreLines::onCenterPressed()
335
{
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()));
340
}
341

342
int TaskRestoreLines::countInvisibleLines()
343
{
344
    int result = 0;
345
    result += countInvisibleGeoms();
346
    result += countInvisibleCosmetics();
347
    result += countInvisibleCenters();
348
    return result;
349
}
350

351
int TaskRestoreLines::countInvisibleGeoms()
352
{
353
    int iGeoms = 0;
354
    const std::vector<TechDraw::GeomFormat*> geoms = m_partFeat->GeomFormats.getValues();
355
    for (auto& g : geoms) {
356
        if (!g->m_format.m_visible) {
357
            iGeoms++;
358
        }
359
    }
360
    return iGeoms;
361
}
362

363
int TaskRestoreLines::countInvisibleCosmetics()
364
{
365
    int iCosmos = 0;
366
    const std::vector<TechDraw::CosmeticEdge*> cosmos = m_partFeat->CosmeticEdges.getValues();
367
    for (auto& c : cosmos) {
368
        if (!c->m_format.m_visible) {
369
            iCosmos++;
370
        }
371
    }
372
    return iCosmos++;
373
}
374

375
int TaskRestoreLines::countInvisibleCenters()
376
{
377
    int iCenter = 0;
378
    const std::vector<TechDraw::CenterLine*> centers = m_partFeat->CenterLines.getValues();
379
    for (auto& c : centers) {
380
        if (!c->m_format.m_visible) {
381
            iCenter++;
382
        }
383
    }
384
    return iCenter++;
385
}
386

387
void TaskRestoreLines::restoreInvisibleLines()
388
{
389
    restoreInvisibleGeoms();
390
    restoreInvisibleCosmetics();
391
    restoreInvisibleCenters();
392
}
393

394
void TaskRestoreLines::restoreInvisibleGeoms()
395
{
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;
400
        }
401
    }
402
    m_partFeat->GeomFormats.setValues(geoms);
403
    m_parent->apply(false);                   //don't undo the work we just did
404
}
405

406
void TaskRestoreLines::restoreInvisibleCosmetics()
407
{
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;
412
        }
413
    }
414
    m_partFeat->CosmeticEdges.setValues(cosmos);
415
    m_parent->apply(false);                   //don't undo the work we just did
416
}
417

418
void TaskRestoreLines::restoreInvisibleCenters()
419
{
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;
424
        }
425
    }
426
    m_partFeat->CenterLines.setValues(centers);
427
    m_parent->apply(false);                   //don't undo the work we just did
428
}
429

430

431
bool TaskRestoreLines::accept()
432
{
433
//    Base::Console().Message("TRL::accept()\n");
434
    return true;
435
}
436

437
bool TaskRestoreLines::reject()
438
{
439
//    Base::Console().Message("TRL::reject()\n");
440
    return false;
441
}
442

443
void TaskRestoreLines::changeEvent(QEvent *e)
444
{
445
    if (e->type() == QEvent::LanguageChange) {
446
        ui->retranslateUi(this);
447
    }
448
}
449

450
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
451
TaskDlgLineDecor::TaskDlgLineDecor(TechDraw::DrawViewPart* partFeat,
452
                                   std::vector<std::string> edgeNames) :
453
    TaskDialog()
454
{
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();
462
    }
463

464
    TaskLineDecor* parent = dynamic_cast<TaskLineDecor*>(widget);
465
    if (parent) {
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);
471
    }
472
}
473

474
TaskDlgLineDecor::~TaskDlgLineDecor()
475
{
476
}
477

478
//==== calls from the TaskView ===============================================================
479
void TaskDlgLineDecor::open()
480
{
481

482
}
483

484
void TaskDlgLineDecor::clicked(int i)
485
{
486
    Q_UNUSED(i);
487
}
488

489
bool TaskDlgLineDecor::accept()
490
{
491
//    Base::Console().Message("TDLD::accept()\n");
492
    widget->accept();
493
    return true;
494
}
495

496
bool TaskDlgLineDecor::reject()
497
{
498
//    Base::Console().Message("TDLD::reject()\n");
499
    widget->reject();
500
    return true;
501
}
502

503
#include <Mod/TechDraw/Gui/moc_TaskLineDecor.cpp>
504

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

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

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

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