Debian Codegraph @

src/Gui/TaskView/TaskView.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /***************************************************************************
3 * Copyright (c) 2009 Jürgen Riegel <[email protected]> *
4 * *
5 * This file is part of the FreeCAD CAx development system. *
6 * *
7 * This library is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU Library General Public *
9 * License as published by the Free Software Foundation; either *
10 * version 2 of the License, or (at your option) any later version. *
11 * *
12 * This library is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Library General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Library General Public *
18 * License along with this library; see the file COPYING.LIB. If not, *
19 * write to the Free Software Foundation, Inc., 59 Temple Place, *
20 * Suite 330, Boston, MA 02111-1307, USA *
21 * *
22 ***************************************************************************/
23
24 #include <QAbstractSpinBox>
25 #include <QActionEvent>
26 #include <QApplication>
27 #include <QCursor>
28 #include <QDockWidget>
29 #include <QLineEdit>
30 #include <QPointer>
31 #include <QPushButton>
32 #include <QTimer>
33 #include <QVBoxLayout>
34
35 #include <FCConfig.h>
36
37 #include <App/Document.h>
38 #include <Gui/ActionFunction.h>
39 #include <Gui/Application.h>
40 #include <Gui/Document.h>
41 #include <Gui/MainWindow.h>
42 #include <Gui/ViewProviderDocumentObject.h>
43
44 #include "TaskView.h"
45 #include "TaskDialog.h"
46 #include "TaskEditControl.h"
47 #include <Gui/Control.h>
48
49 #include <Gui/QSint/actionpanel/taskgroup_p.h>
50 #include <Gui/QSint/actionpanel/taskheader_p.h>
51 #include <Gui/QSint/actionpanel/actionpanelscheme.h>
52
53
54 using namespace Gui::TaskView;
55 namespace sp = std::placeholders;
56
57
58 //**************************************************************************
59 //**************************************************************************
60 // TaskWidget
61 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
62
63 TaskWidget::TaskWidget(QWidget* parent)
64 : QWidget(parent)
65 {}
66
67 TaskWidget::~TaskWidget() = default;
68
69 //**************************************************************************
70 //**************************************************************************
71 // TaskGroup
72 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
73
74 TaskGroup::TaskGroup(QWidget* parent)
75 : QSint::ActionBox(parent)
76 {}
77
78 TaskGroup::TaskGroup(const QString& headerText, QWidget* parent)
79 : QSint::ActionBox(headerText, parent)
80 {}
81
82 TaskGroup::TaskGroup(const QPixmap& icon, const QString& headerText, QWidget* parent)
83 : QSint::ActionBox(icon, headerText, parent)
84 {}
85
86 TaskGroup::~TaskGroup() = default;
87
88 void TaskGroup::actionEvent(QActionEvent* e)
89 {
90 QAction* action = e->action();
91 switch (e->type()) {
92 case QEvent::ActionAdded: {
93 this->createItem(action);
94 break;
95 }
96 case QEvent::ActionChanged: {
97 break;
98 }
99 case QEvent::ActionRemoved: {
100 // cannot change anything
101 break;
102 }
103 default:
104 break;
105 }
106 }
107
108 //**************************************************************************
109 //**************************************************************************
110 // TaskBox
111 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
112
113 TaskBox::TaskBox(QWidget* parent)
114 : QSint::ActionGroup(parent)
115 , wasShown(false)
116 {
117 // override vertical size policy because otherwise task dialogs
118 // whose needsFullSpace() returns true won't take full space.
119 myGroup->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
120 }
121
122 TaskBox::TaskBox(const QString& title, bool expandable, QWidget* parent)
123 : QSint::ActionGroup(title, expandable, parent)
124 , wasShown(false)
125 {
126 // override vertical size policy because otherwise task dialogs
127 // whose needsFullSpace() returns true won't take full space.
128 myGroup->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
129 }
130
131 TaskBox::TaskBox(const QPixmap& icon, const QString& title, bool expandable, QWidget* parent)
132 : QSint::ActionGroup(icon, title, expandable, parent)
133 , wasShown(false)
134 {
135 // override vertical size policy because otherwise task dialogs
136 // whose needsFullSpace() returns true won't take full space.
137 myGroup->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
138 }
139
140 QSize TaskBox::minimumSizeHint() const
141 {
142 // ActionGroup returns a size of 200x100 which leads to problems
143 // when there are several task groups in a panel and the first
144 // one is collapsed. In this case the task panel doesn't expand to
145 // the actually required size and all the remaining groups are
146 // squeezed into the available space and thus the widgets in there
147 // often can't be used any more.
148 // To fix this problem minimumSizeHint() is implemented to again
149 // respect the layout's minimum size.
150 QSize s1 = QSint::ActionGroup::minimumSizeHint();
151 QSize s2 = QWidget::minimumSizeHint();
152 return {qMax(s1.width(), s2.width()), qMax(s1.height(), s2.height())};
153 }
154
155 TaskBox::~TaskBox() = default;
156
157 void TaskBox::showEvent(QShowEvent*)
158 {
159 wasShown = true;
160 }
161
162 void TaskBox::hideGroupBox()
163 {
164 if (!wasShown) {
165 // get approximate height
166 int h = 0;
167 int ct = groupLayout()->count();
168 for (int i = 0; i < ct; i++) {
169 QLayoutItem* item = groupLayout()->itemAt(i);
170 if (item && item->widget()) {
171 QWidget* w = item->widget();
172 h += w->height();
173 }
174 }
175
176 m_tempHeight = m_fullHeight = h;
177 // For the very first time the group gets shown
178 // we cannot do the animation because the layouting
179 // is not yet fully done
180 m_foldDelta = 0;
181 }
182 else {
183 m_tempHeight = m_fullHeight = myGroup->height();
184 m_foldDelta = m_fullHeight / myScheme->groupFoldSteps;
185 }
186
187 m_foldStep = 0.0;
188 m_foldDirection = -1;
189
190 // make sure to have the correct icon
191 bool block = myHeader->blockSignals(true);
192 myHeader->fold();
193 myHeader->blockSignals(block);
194
195 myDummy->setFixedHeight(0);
196 myDummy->hide();
197 myGroup->hide();
198
199 m_foldPixmap = QPixmap();
200 setFixedHeight(myHeader->height());
201 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
202 }
203
204 bool TaskBox::isGroupVisible() const
205 {
206 return myGroup->isVisible();
207 }
208
209 void TaskBox::actionEvent(QActionEvent* e)
210 {
211 QAction* action = e->action();
212 switch (e->type()) {
213 case QEvent::ActionAdded: {
214 auto label = new QSint::ActionLabel(action, this);
215 this->addActionLabel(label, true, false);
216 break;
217 }
218 case QEvent::ActionChanged: {
219 break;
220 }
221 case QEvent::ActionRemoved: {
222 // cannot change anything
223 break;
224 }
225 default:
226 break;
227 }
228 }
229
230 //**************************************************************************
231 //**************************************************************************
232 // TaskPanel
233 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
234
235 TaskPanel::TaskPanel(QWidget* parent)
236 : QSint::ActionPanel(parent)
237 {}
238
239 TaskPanel::~TaskPanel() = default;
240
241 QSize TaskPanel::minimumSizeHint() const
242 {
243 // ActionPanel returns a size of 200x150 which leads to problems
244 // when there are several task groups in the panel and the first
245 // one is collapsed. In this case the task panel doesn't expand to
246 // the actually required size and all the remaining groups are
247 // squeezed into the available space and thus the widgets in there
248 // often can't be used any more.
249 // To fix this problem minimumSizeHint() is implemented to again
250 // respect the layout's minimum size.
251 QSize s1 = QSint::ActionPanel::minimumSizeHint();
252 QSize s2 = QWidget::minimumSizeHint();
253 return {qMax(s1.width(), s2.width()), qMax(s1.height(), s2.height())};
254 }
255
256
257 //**************************************************************************
258 //**************************************************************************
259 // TaskView
260 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
261
262 TaskView::TaskView(QWidget* parent)
263 : QWidget(parent)
264 , ActiveDialog(nullptr)
265 , ActiveCtrl(nullptr)
266 , hGrp(Gui::WindowParameter::getDefaultParameter()->GetGroup("General"))
267 {
268 mainLayout = new QVBoxLayout(this);
269 mainLayout->setContentsMargins(0, 0, 0, 0);
270 this->setLayout(mainLayout);
271 scrollArea = new QScrollArea(this);
272
273 contextualPanelsLayout = new QVBoxLayout();
274 contextualPanelsLayout->setContentsMargins(0, 0, 0, 0);
275 mainLayout->addLayout(contextualPanelsLayout);
276
277 dialogLayout = new QVBoxLayout();
278 dialogLayout->setContentsMargins(0, 0, 0, 0);
279 dialogLayout->setSpacing(0);
280 mainLayout->addLayout(dialogLayout, 1);
281
282 taskPanel = new TaskPanel(scrollArea);
283 QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
284 sizePolicy.setHorizontalStretch(0);
285 sizePolicy.setVerticalStretch(0);
286 sizePolicy.setHeightForWidth(taskPanel->sizePolicy().hasHeightForWidth());
287 taskPanel->setSizePolicy(sizePolicy);
288 taskPanel->setScheme(QSint::ActionPanelScheme::defaultScheme());
289
290 scrollArea->setWidget(taskPanel);
291 scrollArea->setWidgetResizable(true);
292 scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
293 scrollArea->setMinimumWidth(200);
294 dialogLayout->addWidget(scrollArea, 1);
295
296 Gui::Selection().Attach(this);
297
298 // NOLINTBEGIN
299 connectApplicationActiveDocument = App::GetApplication().signalActiveDocument.connect(
300 std::bind(&Gui::TaskView::TaskView::slotActiveDocument, this, sp::_1)
301 );
302 connectApplicationDeleteDocument = App::GetApplication().signalDeleteDocument.connect(
303 std::bind(&Gui::TaskView::TaskView::slotDeletedDocument, this, sp::_1)
304 );
305 connectApplicationClosedView = Gui::Application::Instance->signalCloseView.connect(
306 std::bind(&Gui::TaskView::TaskView::slotViewClosed, this, sp::_1)
307 );
308 connectApplicationUndoDocument = App::GetApplication().signalUndoDocument.connect(
309 std::bind(&Gui::TaskView::TaskView::slotUndoDocument, this, sp::_1)
310 );
311 connectApplicationRedoDocument = App::GetApplication().signalRedoDocument.connect(
312 std::bind(&Gui::TaskView::TaskView::slotRedoDocument, this, sp::_1)
313 );
314 connectApplicationInEdit = Gui::Application::Instance->signalInEdit.connect(
315 std::bind(&Gui::TaskView::TaskView::slotInEdit, this, sp::_1)
316 );
317 // NOLINTEND
318
319 setShowTaskWatcher(hGrp->GetBool("ShowTaskWatcher", true));
320 connectShowTaskWatcherSetting = hGrp->Manager()->signalParamChanged.connect(
321 [this](ParameterGrp* Param, ParameterGrp::ParamType Type, const char* name, const char* value) {
322 if (Param == hGrp && Type == ParameterGrp::ParamType::FCBool && name
323 && strcmp(name, "ShowTaskWatcher") == 0) {
324 setShowTaskWatcher(value && *value == '1');
325 }
326 }
327 );
328
329 updateWatcher();
330 }
331
332 TaskView::~TaskView()
333 {
334 connectApplicationActiveDocument.disconnect();
335 connectApplicationDeleteDocument.disconnect();
336 connectApplicationClosedView.disconnect();
337 connectApplicationUndoDocument.disconnect();
338 connectApplicationRedoDocument.disconnect();
339 connectApplicationInEdit.disconnect();
340 connectShowTaskWatcherSetting.disconnect();
341 Gui::Selection().Detach(this);
342
343 for (QWidget* panel : contextualPanels) {
344 delete panel;
345 }
346 }
347
348 bool TaskView::isEmpty(bool includeWatcher) const
349 {
350 if (ActiveCtrl || ActiveDialog) {
351 return false;
352 }
353
354 if (includeWatcher) {
355 for (auto* watcher : ActiveWatcher) {
356 if (watcher->shouldShow()) {
357 return false;
358 }
359 }
360 }
361 return true;
362 }
363
364 bool TaskView::event(QEvent* event)
365 {
366 // Workaround for a limitation in Qt (#0003794)
367 // Line edits and spin boxes don't handle the key combination
368 // Shift+Keypad button (if NumLock is activated)
369 if (event->type() == QEvent::ShortcutOverride) {
370 QWidget* focusWidget = qApp->focusWidget();
371 bool isLineEdit = qobject_cast<QLineEdit*>(focusWidget);
372 bool isSpinBox = qobject_cast<QAbstractSpinBox*>(focusWidget);
373
374 if (isLineEdit || isSpinBox) {
375 auto kevent = static_cast<QKeyEvent*>(event);
376 Qt::KeyboardModifiers ShiftKeypadModifier = Qt::ShiftModifier | Qt::KeypadModifier;
377 if (kevent->modifiers() == Qt::NoModifier || kevent->modifiers() == Qt::ShiftModifier
378 || kevent->modifiers() == Qt::KeypadModifier
379 || kevent->modifiers() == ShiftKeypadModifier) {
380 switch (kevent->key()) {
381 case Qt::Key_Delete:
382 case Qt::Key_Home:
383 case Qt::Key_End:
384 case Qt::Key_Backspace:
385 case Qt::Key_Left:
386 case Qt::Key_Right:
387 kevent->accept();
388 default:
389 break;
390 }
391 }
392 }
393 }
394 return QWidget::event(event);
395 }
396
397 void TaskView::keyPressEvent(QKeyEvent* ke)
398 {
399 if (ActiveCtrl && ActiveDialog) {
400 if (ke->key() == Qt::Key_Return || ke->key() == Qt::Key_Enter) {
401 // get all buttons of the complete task dialog
402 QList<QPushButton*> list = this->findChildren<QPushButton*>();
403 for (auto pb : list) {
404 if (pb->isDefault() && pb->isVisible()) {
405 if (pb->isEnabled()) {
406 #if defined(FC_OS_MACOSX)
407 // #0001354: Crash on using Enter-Key for confirmation of chamfer or fillet
408 // entries
409 QPoint pos = QCursor::pos();
410 QCursor::setPos(pb->parentWidget()->mapToGlobal(pb->pos()));
411 #endif
412 pb->click();
413 #if defined(FC_OS_MACOSX)
414 QCursor::setPos(pos);
415 #endif
416 }
417 return;
418 }
419 }
420 }
421 else if (ke->key() == Qt::Key_Escape && ActiveDialog->isEscapeButtonEnabled()) {
422 // get only the buttons of the button box
423 QDialogButtonBox* box = ActiveCtrl->standardButtons();
424 QList<QAbstractButton*> list = box->buttons();
425 for (auto pb : list) {
426 if (box->buttonRole(pb) == QDialogButtonBox::RejectRole) {
427 if (pb->isEnabled()) {
428 #if defined(FC_OS_MACOSX)
429 // #0001354: Crash on using Enter-Key for confirmation of chamfer or fillet
430 // entries
431 QPoint pos = QCursor::pos();
432 QCursor::setPos(pb->parentWidget()->mapToGlobal(pb->pos()));
433 #endif
434 pb->click();
435 #if defined(FC_OS_MACOSX)
436 QCursor::setPos(pos);
437 #endif
438 }
439 return;
440 }
441 }
442
443 // In case a task panel has no Close or Cancel button
444 // then invoke resetEdit() directly
445 // See also ViewProvider::eventCallback
446 auto func = new Gui::TimerFunction();
447 func->setAutoDelete(true);
448 Gui::Document* doc = Gui::Application::Instance->getDocument(
449 ActiveDialog->getDocumentName().c_str()
450 );
451 if (doc) {
452 func->setFunction([doc]() { doc->resetEdit(); });
453 func->singleShot(0);
454 }
455 }
456 }
457 else {
458 QWidget::keyPressEvent(ke);
459 }
460 }
461
462 void TaskView::triggerMinimumSizeHint()
463 {
464 // NOLINTNEXTLINE
465 QTimer::singleShot(100, this, &TaskView::adjustMinimumSizeHint);
466 }
467
468 void TaskView::adjustMinimumSizeHint()
469 {
470 QSize ms = minimumSizeHint();
471 setMinimumWidth(ms.width());
472 }
473
474 QSize TaskView::minimumSizeHint() const
475 {
476 QSize ms = QWidget::minimumSizeHint();
477 int spacing = 0;
478 if (QLayout* layout = taskPanel->layout()) {
479 spacing = 2 * layout->spacing();
480 }
481
482 ms.setWidth(taskPanel->minimumSizeHint().width() + spacing);
483 return ms;
484 }
485
486 void TaskView::slotActiveDocument(const App::Document& doc)
487 {
488 Q_UNUSED(doc);
489 if (!ActiveDialog) {
490 // at this point, active object of the active view returns None.
491 // which is a problem if shouldShow of a watcher rely on the presence
492 // of an active object (example Assembly).
493 QTimer::singleShot(100, this, &TaskView::updateWatcher);
494 }
495 }
496
497 void TaskView::slotInEdit(const Gui::ViewProviderDocumentObject& vp)
498 {
499 Q_UNUSED(vp);
500 if (!ActiveDialog) {
501 updateWatcher();
502 }
503 }
504
505 void TaskView::slotDeletedDocument(const App::Document& doc)
506 {
507 if (ActiveDialog) {
508 if (ActiveDialog->isAutoCloseOnDeletedDocument()) {
509 std::string name = ActiveDialog->getDocumentName();
510 if (name.empty()) {
511 Base::Console().warning(
512 std::string("TaskView::slotDeletedDocument"),
513 "No document name set\n"
514 );
515 }
516
517 if (name == doc.getName()) {
518 ActiveDialog->autoClosedOnDeletedDocument();
519 removeDialog();
520 }
521 }
522 }
523
524 if (!ActiveDialog) {
525 updateWatcher();
526 }
527 }
528
529 void TaskView::slotViewClosed(const Gui::MDIView* view)
530 {
531 // It can happen that only a view is closed an not the document
532 if (ActiveDialog) {
533 if (ActiveDialog->isAutoCloseOnClosedView()) {
534 const Gui::MDIView* associatedView = ActiveDialog->getAssociatedView();
535 if (!associatedView) {
536 Base::Console().warning(std::string("TaskView::slotViewClosed"), "No view associated\n");
537 }
538
539 if (associatedView == view) {
540 ActiveDialog->autoClosedOnClosedView();
541 removeDialog();
542 }
543 }
544 }
545
546 if (!ActiveDialog) {
547 updateWatcher();
548 }
549 }
550
551 void TaskView::transactionChangeOnDocument(const App::Document& doc, bool undo)
552 {
553 if (ActiveDialog) {
554 std::string name = ActiveDialog->getDocumentName();
555 if (name == doc.getName()) {
556 undo ? ActiveDialog->onUndo() : ActiveDialog->onRedo();
557 }
558
559 if (ActiveDialog->isAutoCloseOnTransactionChange()) {
560 if (name.empty()) {
561 Base::Console().warning(
562 std::string("TaskView::transactionChangeOnDocument"),
563 "No document name set\n"
564 );
565 }
566
567 if (name == doc.getName()) {
568 ActiveDialog->autoClosedOnTransactionChange();
569 removeDialog();
570 }
571 }
572 }
573
574 if (!ActiveDialog) {
575 updateWatcher();
576 }
577 }
578
579 void TaskView::slotUndoDocument(const App::Document& doc)
580 {
581 transactionChangeOnDocument(doc, true);
582 }
583
584 void TaskView::slotRedoDocument(const App::Document& doc)
585 {
586 transactionChangeOnDocument(doc, false);
587 }
588
589 /// @cond DOXERR
590 void TaskView::OnChange(
591 Gui::SelectionSingleton::SubjectType& rCaller,
592 Gui::SelectionSingleton::MessageType Reason
593 )
594 {
595 Q_UNUSED(rCaller);
596 std::string temp;
597
598 if (Reason.Type == SelectionChanges::AddSelection || Reason.Type == SelectionChanges::ClrSelection
599 || Reason.Type == SelectionChanges::SetSelection
600 || Reason.Type == SelectionChanges::RmvSelection) {
601
602 if (!ActiveDialog) {
603 updateWatcher();
604 }
605 }
606 }
607 /// @endcond
608
609 void TaskView::showDialog(TaskDialog* dlg)
610 {
611 // if trying to open the same dialog twice nothing needs to be done
612 if (ActiveDialog == dlg) {
613 return;
614 }
615 assert(!ActiveDialog);
616 assert(!ActiveCtrl);
617
618 // remove the TaskWatcher as long as the Dialog is up
619 removeTaskWatcher();
620
621 // first create the control element, set it up and wire it:
622 ActiveCtrl = new TaskEditControl(this);
623 ActiveCtrl->buttonBox->setStandardButtons(dlg->getStandardButtons());
624 TaskDialogAttorney::setButtonBox(dlg, ActiveCtrl->buttonBox);
625
626 // clang-format off
627 // make connection to the needed signals
628 connect(ActiveCtrl->buttonBox, &QDialogButtonBox::accepted,
629 this, &TaskView::accept);
630 connect(ActiveCtrl->buttonBox, &QDialogButtonBox::rejected,
631 this, &TaskView::reject);
632 connect(ActiveCtrl->buttonBox, &QDialogButtonBox::helpRequested,
633 this, &TaskView::helpRequested);
634 connect(ActiveCtrl->buttonBox, &QDialogButtonBox::clicked,
635 this, &TaskView::clicked);
636 // clang-format on
637
638 const std::vector<QWidget*>& cont = dlg->getDialogContent();
639
640 // give to task dialog to customize the button box
641 dlg->modifyStandardButtons(ActiveCtrl->buttonBox);
642
643 if (dlg->buttonPosition() == TaskDialog::North) {
644 // Add button box to the top of the main layout
645 dialogLayout->insertWidget(0, ActiveCtrl);
646 for (const auto& it : cont) {
647 taskPanel->addWidget(it);
648 }
649 }
650 else {
651 for (const auto& it : cont) {
652 taskPanel->addWidget(it);
653 }
654 // Add button box to the bottom of the main layout
655 dialogLayout->addWidget(ActiveCtrl);
656 }
657
658 taskPanel->setScheme(QSint::ActionPanelScheme::defaultScheme());
659
660 if (!dlg->needsFullSpace()) {
661 taskPanel->addStretch();
662 }
663
664 // set as active Dialog
665 ActiveDialog = dlg;
666
667 ActiveDialog->open();
668
669 saveCurrentWidth();
670 getMainWindow()->updateActions();
671
672 triggerMinimumSizeHint();
673
674 Q_EMIT taskUpdate();
675 }
676
677 void TaskView::removeDialog()
678 {
679 getMainWindow()->updateActions();
680
681 if (ActiveCtrl) {
682 dialogLayout->removeWidget(ActiveCtrl);
683 delete ActiveCtrl;
684 ActiveCtrl = nullptr;
685 }
686
687 TaskDialog* remove = nullptr;
688 if (ActiveDialog) {
689 // See 'accept' and 'reject'
690 if (ActiveDialog->property("taskview_accept_or_reject").isNull()) {
691 const std::vector<QWidget*>& cont = ActiveDialog->getDialogContent();
692 for (const auto& it : cont) {
693 taskPanel->removeWidget(it);
694 }
695 remove = ActiveDialog;
696 ActiveDialog = nullptr;
697 }
698 else {
699 ActiveDialog->setProperty("taskview_remove_dialog", true);
700 }
701 }
702
703 taskPanel->removeStretch();
704
705 // put the watcher back in control
706 addTaskWatcher();
707
708 if (remove) {
709 remove->closed();
710 remove->emitDestructionSignal();
711 delete remove;
712 }
713
714 tryRestoreWidth();
715 triggerMinimumSizeHint();
716 }
717 void TaskView::setShowTaskWatcher(bool show)
718 {
719 if (showTaskWatcher == show) {
720 return;
721 }
722
723 showTaskWatcher = show;
724 if (show) {
725 addTaskWatcher();
726 }
727 else {
728 clearTaskWatcher();
729 }
730 }
731 void TaskView::updateWatcher()
732 {
733 if (!showTaskWatcher) {
734 return;
735 }
736
737 if (ActiveWatcher.empty()) {
738 auto panel = Gui::Control().taskPanel();
739 if (panel && panel->ActiveWatcher.size()) {
740 takeTaskWatcher(panel);
741 }
742 }
743
744 // In case a child of the TaskView has the focus and get hidden we have
745 // to make sure to set the focus on a widget that won't be hidden or
746 // deleted because otherwise Qt may forward the focus via focusNextPrevChild()
747 // to the mdi area which may switch to another mdi view which is not an
748 // acceptable behaviour.
749 QWidget* fw = QApplication::focusWidget();
750 if (!fw) {
751 this->setFocus();
752 }
753 QPointer<QWidget> fwp = fw;
754 while (fw && !fw->isWindow()) {
755 if (fw == this) {
756 this->setFocus();
757 break;
758 }
759 fw = fw->parentWidget();
760 }
761
762 // add all widgets for all watcher to the task view
763 for (const auto& it : ActiveWatcher) {
764 bool match = it->shouldShow();
765 std::vector<QWidget*>& cont = it->getWatcherContent();
766 for (auto& it2 : cont) {
767 if (match) {
768 it2->show();
769 }
770 else {
771 it2->hide();
772 }
773 }
774 }
775
776 // In case the previous widget that had the focus is still visible
777 // give it the focus back.
778 if (fwp && fwp->isVisible()) {
779 fwp->setFocus();
780 }
781
782 triggerMinimumSizeHint();
783
784 Q_EMIT taskUpdate();
785 }
786
787 void TaskView::addTaskWatcher(const std::vector<TaskWatcher*>& Watcher)
788 {
789 // remove and delete the old set of TaskWatcher
790 for (TaskWatcher* tw : ActiveWatcher) {
791 delete tw;
792 }
793
794 ActiveWatcher = Watcher;
795 if (!ActiveCtrl && !ActiveDialog) {
796 addTaskWatcher();
797 }
798 }
799
800 void TaskView::takeTaskWatcher(TaskView* other)
801 {
802 clearTaskWatcher();
803 ActiveWatcher.swap(other->ActiveWatcher);
804 other->clearTaskWatcher();
805 if (isEmpty(false)) {
806 addTaskWatcher();
807 }
808 }
809
810 void TaskView::clearTaskWatcher()
811 {
812 std::vector<TaskWatcher*> watcher;
813 removeTaskWatcher();
814 // make sure to delete the old watchers
815 addTaskWatcher(watcher);
816 }
817
818 void TaskView::addTaskWatcher()
819 {
820 if (!showTaskWatcher) {
821 return;
822 }
823 // add all widgets for all watcher to the task view
824 for (TaskWatcher* tw : ActiveWatcher) {
825 std::vector<QWidget*>& cont = tw->getWatcherContent();
826 for (QWidget* w : cont) {
827 taskPanel->addWidget(w);
828 }
829 }
830
831 if (!ActiveWatcher.empty()) {
832 taskPanel->addStretch();
833 }
834 updateWatcher();
835
836 // Workaround to avoid a crash in Qt. See also
837 // https://forum.freecad.org/viewtopic.php?f=8&t=39187
838 //
839 // Notify the button box about a style change so that it can
840 // safely delete the style animation of its push buttons.
841 auto box = taskPanel->findChild<QDialogButtonBox*>();
842 if (box) {
843 QEvent event(QEvent::StyleChange);
844 QApplication::sendEvent(box, &event);
845 }
846
847 taskPanel->setScheme(QSint::ActionPanelScheme::defaultScheme());
848 }
849
850 void TaskView::saveCurrentWidth()
851 {
852 if (shouldRestoreWidth()) {
853 if (auto parent = qobject_cast<QDockWidget*>(parentWidget())) {
854 currentWidth = parent->width();
855 }
856 }
857 }
858
859 void TaskView::tryRestoreWidth()
860 {
861 if (shouldRestoreWidth()) {
862 if (auto parent = qobject_cast<QDockWidget*>(parentWidget())) {
863 Gui::getMainWindow()->resizeDocks({parent}, {currentWidth}, Qt::Horizontal);
864 }
865 }
866 }
867
868 void TaskView::setRestoreWidth(bool on)
869 {
870 restoreWidth = on;
871 }
872
873 bool TaskView::shouldRestoreWidth() const
874 {
875 return restoreWidth;
876 }
877
878 void TaskView::removeTaskWatcher()
879 {
880 // In case a child of the TaskView has the focus and get hidden we have
881 // to make sure that set the focus on a widget that won't be hidden or
882 // deleted because otherwise Qt may forward the focus via focusNextPrevChild()
883 // to the mdi area which may switch to another mdi view which is not an
884 // acceptable behaviour.
885 QWidget* fw = QApplication::focusWidget();
886 if (!fw) {
887 this->setFocus();
888 }
889 while (fw && !fw->isWindow()) {
890 if (fw == this) {
891 this->setFocus();
892 break;
893 }
894 fw = fw->parentWidget();
895 }
896
897 // remove all widgets
898 for (TaskWatcher* tw : ActiveWatcher) {
899 std::vector<QWidget*>& cont = tw->getWatcherContent();
900 for (QWidget* w : cont) {
901 w->hide();
902 taskPanel->removeWidget(w);
903 }
904 }
905
906 taskPanel->removeStretch();
907 }
908
909 void TaskView::accept()
910 {
911 if (!ActiveDialog) { // Protect against segfaults due to out-of-order deletions
912 Base::Console().warning("ActiveDialog was null in call to TaskView::accept()\n");
913 return;
914 }
915
916 // Make sure that if 'accept' calls 'closeDialog' the deletion is postponed until
917 // the dialog leaves the 'accept' method
918 ActiveDialog->setProperty("taskview_accept_or_reject", true);
919 bool success = ActiveDialog->accept();
920 ActiveDialog->setProperty("taskview_accept_or_reject", QVariant());
921 if (success || ActiveDialog->property("taskview_remove_dialog").isValid()) {
922 removeDialog();
923 }
924 }
925
926 void TaskView::reject()
927 {
928 if (!ActiveDialog) { // Protect against segfaults due to out-of-order deletions
929 Base::Console().warning("ActiveDialog was null in call to TaskView::reject()\n");
930 return;
931 }
932
933 // Make sure that if 'reject' calls 'closeDialog' the deletion is postponed until
934 // the dialog leaves the 'reject' method
935 ActiveDialog->setProperty("taskview_accept_or_reject", true);
936 bool success = ActiveDialog->reject();
937 ActiveDialog->setProperty("taskview_accept_or_reject", QVariant());
938 if (success || ActiveDialog->property("taskview_remove_dialog").isValid()) {
939 removeDialog();
940 }
941 }
942
943 void TaskView::helpRequested()
944 {
945 ActiveDialog->helpRequested();
946 }
947
948 void TaskView::clicked(QAbstractButton* button)
949 {
950 int id = ActiveCtrl->buttonBox->standardButton(button);
951 ActiveDialog->clicked(id);
952 }
953
954 void TaskView::clearActionStyle()
955 {
956 static_cast<QSint::ActionPanelScheme*>(QSint::ActionPanelScheme::defaultScheme())->clearActionStyle();
957 taskPanel->setScheme(QSint::ActionPanelScheme::defaultScheme());
958 }
959
960 void TaskView::restoreActionStyle()
961 {
962 static_cast<QSint::ActionPanelScheme*>(QSint::ActionPanelScheme::defaultScheme())
963 ->restoreActionStyle();
964 taskPanel->setScheme(QSint::ActionPanelScheme::defaultScheme());
965 }
966
967 void TaskView::addContextualPanel(QWidget* panel)
968 {
969 if (!panel || contextualPanels.contains(panel)) {
970 return;
971 }
972
973 contextualPanelsLayout->addWidget(panel);
974 contextualPanels.append(panel);
975 panel->show();
976 triggerMinimumSizeHint();
977 Q_EMIT taskUpdate();
978 }
979
980 void TaskView::removeContextualPanel(QWidget* panel)
981 {
982 if (!panel || !contextualPanels.contains(panel)) {
983 return;
984 }
985
986 contextualPanelsLayout->removeWidget(panel);
987 contextualPanels.removeOne(panel);
988 panel->deleteLater();
989 triggerMinimumSizeHint();
990 Q_EMIT taskUpdate();
991 }
992
993 #include "moc_TaskView.cpp"