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