| 1 |
/**************************************************************************** |
| 2 |
* Copyright (c) 2018 Zheng Lei (realthunder) <[email protected]> * |
| 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 <QPushButton> |
| 24 |
#include <QTreeWidget> |
| 25 |
#include <QCheckBox> |
| 26 |
|
| 27 |
#include <App/Document.h> |
| 28 |
#include <App/DocumentObject.h> |
| 29 |
|
| 30 |
#include "Dialogs/DlgObjectSelection.h" |
| 31 |
#include "ui_DlgObjectSelection.h" |
| 32 |
#include "Application.h" |
| 33 |
#include "MainWindow.h" |
| 34 |
#include "ViewProviderDocumentObject.h" |
| 35 |
#include "MetaTypes.h" |
| 36 |
#include "ViewParams.h" |
| 37 |
|
| 38 |
FC_LOG_LEVEL_INIT("Gui", true, true) |
| 39 |
|
| 40 |
using namespace Gui; |
| 41 |
|
| 42 |
/* TRANSLATOR Gui::DlgObjectSelection */ |
| 43 |
|
| 44 |
DlgObjectSelection::DlgObjectSelection( |
| 45 |
const std::vector<App::DocumentObject*>& objs, |
| 46 |
QWidget* parent, |
| 47 |
Qt::WindowFlags fl |
| 48 |
) |
| 49 |
: QDialog(parent, fl) |
| 50 |
{ |
| 51 |
init(objs, {}); |
| 52 |
} |
| 53 |
|
| 54 |
DlgObjectSelection::DlgObjectSelection( |
| 55 |
const std::vector<App::DocumentObject*>& objs, |
| 56 |
const std::vector<App::DocumentObject*>& excludes, |
| 57 |
QWidget* parent, |
| 58 |
Qt::WindowFlags fl |
| 59 |
) |
| 60 |
: QDialog(parent, fl) |
| 61 |
{ |
| 62 |
init(objs, excludes); |
| 63 |
} |
| 64 |
|
| 65 |
static bool inline setCheckState(QTreeWidgetItem* item, Qt::CheckState state, bool forced = true) |
| 66 |
{ |
| 67 |
if (!forced) { |
| 68 |
if (item->isSelected()) { |
| 69 |
if (state == Qt::Unchecked || item->checkState(0) == Qt::Unchecked) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
} |
| 73 |
if (item->checkState(0) == state) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
} |
| 77 |
// auto objT = qvariant_cast<App::SubObjectT>(item->data(0, Qt::UserRole)); |
| 78 |
// FC_MSG(objT.getObjectFullName() << (state == Qt::Unchecked ? " unchecked" : |
| 79 |
// (state == Qt::Checked ? " checked" : " partial"))); |
| 80 |
item->setCheckState(0, state); |
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
void DlgObjectSelection::init( |
| 85 |
const std::vector<App::DocumentObject*>& objs, |
| 86 |
const std::vector<App::DocumentObject*>& excludes |
| 87 |
) |
| 88 |
|
| 89 |
{ |
| 90 |
initSels = objs; |
| 91 |
std::sort(initSels.begin(), initSels.end()); |
| 92 |
|
| 93 |
deps = App::Document::getDependencyList(objs, App::Document::DepSort); |
| 94 |
depSet.insert(deps.begin(), deps.end()); |
| 95 |
|
| 96 |
ui = new Ui_DlgObjectSelection; |
| 97 |
ui->setupUi(this); |
| 98 |
|
| 99 |
hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/General"); |
| 100 |
ui->checkBoxAutoDeps->setChecked(hGrp->GetBool("ObjectSelectionAutoDeps", true)); |
| 101 |
connect(ui->checkBoxAutoDeps, &QCheckBox::toggled, this, &DlgObjectSelection::onAutoDeps); |
| 102 |
|
| 103 |
ui->checkBoxShowDeps->setChecked(hGrp->GetBool("ObjectSelectionShowDeps", false)); |
| 104 |
QObject::connect(ui->checkBoxShowDeps, &QCheckBox::toggled, [this](bool checked) { |
| 105 |
hGrp->SetBool("ObjectSelectionShowDeps", checked); |
| 106 |
onShowDeps(); |
| 107 |
}); |
| 108 |
QMetaObject::invokeMethod(this, "onShowDeps", Qt::QueuedConnection); |
| 109 |
|
| 110 |
// make sure to show a horizontal scrollbar if needed |
| 111 |
ui->depList->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents); |
| 112 |
ui->depList->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents); |
| 113 |
ui->depList->header()->setSectionResizeMode(2, QHeaderView::ResizeToContents); |
| 114 |
ui->inList->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents); |
| 115 |
ui->inList->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents); |
| 116 |
ui->inList->header()->setSectionResizeMode(2, QHeaderView::ResizeToContents); |
| 117 |
ui->treeWidget->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents); |
| 118 |
|
| 119 |
ui->depList->headerItem()->setText(0, tr("Depending on")); |
| 120 |
ui->depList->headerItem()->setText(1, tr("Document")); |
| 121 |
ui->depList->headerItem()->setText(2, tr("Name")); |
| 122 |
|
| 123 |
ui->inList->headerItem()->setText(0, tr("Depended by")); |
| 124 |
ui->inList->headerItem()->setText(1, tr("Document")); |
| 125 |
ui->inList->headerItem()->setText(2, tr("Name")); |
| 126 |
|
| 127 |
ui->treeWidget->headerItem()->setText(0, tr("Selections")); |
| 128 |
ui->treeWidget->header()->setStretchLastSection(false); |
| 129 |
|
| 130 |
connect(ui->treeWidget, &QTreeWidget::itemExpanded, this, &DlgObjectSelection::onItemExpanded); |
| 131 |
|
| 132 |
allItem = new QTreeWidgetItem(ui->treeWidget); |
| 133 |
allItem->setText(0, QStringLiteral("<%1>").arg(tr("All"))); |
| 134 |
QFont font = allItem->font(0); |
| 135 |
font.setBold(true); |
| 136 |
allItem->setFont(0, font); |
| 137 |
allItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); |
| 138 |
allItem->setCheckState(0, Qt::Checked); |
| 139 |
|
| 140 |
for (auto obj : initSels) { |
| 141 |
getItem(obj)->setCheckState(0, Qt::Checked); |
| 142 |
} |
| 143 |
|
| 144 |
for (auto obj : deps) { |
| 145 |
getItem(obj)->setCheckState(0, Qt::Checked); |
| 146 |
} |
| 147 |
|
| 148 |
auto filter = excludes; |
| 149 |
std::sort(filter.begin(), filter.end()); |
| 150 |
for (auto obj : deps) { |
| 151 |
auto it = std::lower_bound(filter.begin(), filter.end(), obj); |
| 152 |
if (it != filter.end() && *it == obj) { |
| 153 |
setItemState(obj, Qt::Unchecked); |
| 154 |
} |
| 155 |
} |
| 156 |
onItemSelectionChanged(); |
| 157 |
|
| 158 |
/** |
| 159 |
* create useOriginalsBtn and add to the button box |
| 160 |
* tried adding to .ui file, but could never get the |
| 161 |
* formatting exactly the way I wanted it. -- <TheMarkster> |
| 162 |
*/ |
| 163 |
useOriginalsBtn = new QPushButton(tr("&Use Original Selection")); |
| 164 |
useOriginalsBtn->setToolTip( |
| 165 |
tr("Ignore dependencies and proceed with the objects\noriginally selected prior to opening " |
| 166 |
"this dialog") |
| 167 |
); |
| 168 |
ui->buttonBox->addButton(useOriginalsBtn, QDialogButtonBox::ResetRole); |
| 169 |
|
| 170 |
connect(ui->treeWidget, &QTreeWidget::itemChanged, this, &DlgObjectSelection::onObjItemChanged); |
| 171 |
connect(ui->depList, &QTreeWidget::itemChanged, this, &DlgObjectSelection::onDepItemChanged); |
| 172 |
connect(ui->inList, &QTreeWidget::itemChanged, this, &DlgObjectSelection::onDepItemChanged); |
| 173 |
connect( |
| 174 |
ui->treeWidget, |
| 175 |
&QTreeWidget::itemSelectionChanged, |
| 176 |
this, |
| 177 |
&DlgObjectSelection::onItemSelectionChanged |
| 178 |
); |
| 179 |
connect(useOriginalsBtn, &QPushButton::clicked, this, &DlgObjectSelection::onUseOriginalsBtnClicked); |
| 180 |
|
| 181 |
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &DlgObjectSelection::accept); |
| 182 |
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &DlgObjectSelection::reject); |
| 183 |
|
| 184 |
timer.setSingleShot(true); |
| 185 |
connect(&timer, &QTimer::timeout, this, &DlgObjectSelection::checkItemChanged); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Destroys the object and frees any allocated resources |
| 190 |
*/ |
| 191 |
DlgObjectSelection::~DlgObjectSelection() |
| 192 |
{ |
| 193 |
// no need to delete child widgets, Qt does it all for us |
| 194 |
delete ui; |
| 195 |
} |
| 196 |
|
| 197 |
QTreeWidgetItem* DlgObjectSelection::getItem( |
| 198 |
App::DocumentObject* obj, |
| 199 |
std::vector<QTreeWidgetItem*>** pitems, |
| 200 |
QTreeWidgetItem* parent |
| 201 |
) |
| 202 |
{ |
| 203 |
auto& items = itemMap[App::SubObjectT(obj, "")]; |
| 204 |
if (pitems) { |
| 205 |
*pitems = &items; |
| 206 |
} |
| 207 |
QTreeWidgetItem* item; |
| 208 |
if (!parent) { |
| 209 |
if (!items.empty()) { |
| 210 |
return items[0]; |
| 211 |
} |
| 212 |
item = new QTreeWidgetItem(ui->treeWidget); |
| 213 |
auto vp = freecad_cast<ViewProviderDocumentObject*>( |
| 214 |
Gui::Application::Instance->getViewProvider(obj) |
| 215 |
); |
| 216 |
if (vp) { |
| 217 |
item->setIcon(0, vp->getIcon()); |
| 218 |
} |
| 219 |
App::SubObjectT objT(obj, ""); |
| 220 |
item->setText(0, QString::fromUtf8((obj)->Label.getValue())); |
| 221 |
if (std::binary_search(initSels.begin(), initSels.end(), obj)) { |
| 222 |
QFont font = item->font(0); |
| 223 |
font.setBold(true); |
| 224 |
font.setItalic(true); |
| 225 |
item->setFont(0, font); |
| 226 |
} |
| 227 |
item->setToolTip(0, QString::fromUtf8(objT.getObjectFullName().c_str())); |
| 228 |
item->setData(0, Qt::UserRole, QVariant::fromValue(objT)); |
| 229 |
item->setChildIndicatorPolicy( |
| 230 |
obj->getOutList().empty() ? QTreeWidgetItem::DontShowIndicator |
| 231 |
: QTreeWidgetItem::ShowIndicator |
| 232 |
); |
| 233 |
} |
| 234 |
else if (!items.empty()) { |
| 235 |
item = new QTreeWidgetItem(parent); |
| 236 |
item->setIcon(0, items[0]->icon(0)); |
| 237 |
item->setText(0, items[0]->text(0)); |
| 238 |
item->setFont(0, items[0]->font(0)); |
| 239 |
item->setToolTip(0, items[0]->toolTip(0)); |
| 240 |
item->setData(0, Qt::UserRole, items[0]->data(0, Qt::UserRole)); |
| 241 |
item->setChildIndicatorPolicy(items[0]->childIndicatorPolicy()); |
| 242 |
item->setCheckState(0, items[0]->checkState(0)); |
| 243 |
} |
| 244 |
else { |
| 245 |
return nullptr; |
| 246 |
} |
| 247 |
items.push_back(item); |
| 248 |
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); |
| 249 |
return item; |
| 250 |
} |
| 251 |
|
| 252 |
void DlgObjectSelection::onItemExpanded(QTreeWidgetItem* item) |
| 253 |
{ |
| 254 |
if (item->childCount()) { |
| 255 |
return; |
| 256 |
} |
| 257 |
if (auto obj = qvariant_cast<App::SubObjectT>(item->data(0, Qt::UserRole)).getObject()) { |
| 258 |
QSignalBlocker blocker(ui->treeWidget); |
| 259 |
std::set<App::DocumentObject*> set; |
| 260 |
for (auto child : obj->getOutList()) { |
| 261 |
if (child && set.insert(child).second) { |
| 262 |
getItem(child, nullptr, item); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
void DlgObjectSelection::updateAllItemState() |
| 269 |
{ |
| 270 |
int count = 0; |
| 271 |
for (const auto& v : itemMap) { |
| 272 |
auto state = v.second[0]->checkState(0); |
| 273 |
if (state == Qt::Unchecked) { |
| 274 |
if (count) { |
| 275 |
allItem->setCheckState(0, Qt::PartiallyChecked); |
| 276 |
return; |
| 277 |
} |
| 278 |
} |
| 279 |
else { |
| 280 |
if (state == Qt::PartiallyChecked) { |
| 281 |
allItem->setCheckState(0, Qt::PartiallyChecked); |
| 282 |
return; |
| 283 |
} |
| 284 |
++count; |
| 285 |
} |
| 286 |
} |
| 287 |
if (count && count == (int)itemMap.size()) { |
| 288 |
allItem->setCheckState(0, Qt::Checked); |
| 289 |
} |
| 290 |
else if (!count) { |
| 291 |
allItem->setCheckState(0, Qt::Unchecked); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
void DlgObjectSelection::setItemState(App::DocumentObject* obj, Qt::CheckState state, bool forced) |
| 296 |
{ |
| 297 |
std::vector<QTreeWidgetItem*>* items = nullptr; |
| 298 |
auto item = getItem(obj, &items); |
| 299 |
if (!setCheckState(item, state, forced)) { |
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
for (size_t i = 1; i < items->size(); ++i) { |
| 304 |
setCheckState(items->at(i), state, true); |
| 305 |
} |
| 306 |
|
| 307 |
std::vector<App::DocumentObject*> objs = {obj}; |
| 308 |
|
| 309 |
if (ui->checkBoxAutoDeps->isChecked() && state == Qt::Checked) { |
| 310 |
// If an object is newly checked, check all its dependencies |
| 311 |
for (auto o : obj->getOutListRecursive()) { |
| 312 |
if (!depSet.contains(o) || itemChanged.contains(o)) { |
| 313 |
continue; |
| 314 |
} |
| 315 |
auto itItem = itemMap.find(o); |
| 316 |
if (itItem == itemMap.end() || itItem->second[0]->checkState(0) == state) { |
| 317 |
continue; |
| 318 |
} |
| 319 |
|
| 320 |
for (auto i : itItem->second) { |
| 321 |
setCheckState(i, state, true); |
| 322 |
} |
| 323 |
objs.push_back(o); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
for (auto obj : objs) { |
| 328 |
auto it = inMap.find(obj); |
| 329 |
if (it != inMap.end()) { |
| 330 |
setCheckState(it->second, state); |
| 331 |
} |
| 332 |
|
| 333 |
auto itDep = depMap.find(obj); |
| 334 |
if (itDep != depMap.end()) { |
| 335 |
setCheckState(itDep->second, state); |
| 336 |
} |
| 337 |
|
| 338 |
// If an object toggles state, we need to revisit all its in-list |
| 339 |
// object to update the partial/full checked state. |
| 340 |
for (auto o : obj->getInList()) { |
| 341 |
if (!depSet.contains(o) || itemChanged.contains(o)) { |
| 342 |
continue; |
| 343 |
} |
| 344 |
auto it = itemMap.find(o); |
| 345 |
if (it == itemMap.end() || it->second[0]->checkState(0) == state) { |
| 346 |
continue; |
| 347 |
} |
| 348 |
int count = 0; |
| 349 |
int selcount = 0; |
| 350 |
for (auto sibling : o->getOutList()) { |
| 351 |
if (!depSet.contains(sibling)) { |
| 352 |
continue; |
| 353 |
} |
| 354 |
++count; |
| 355 |
auto it = itemMap.find(sibling); |
| 356 |
if (it == itemMap.end()) { |
| 357 |
continue; |
| 358 |
} |
| 359 |
auto s = it->second[0]->checkState(0); |
| 360 |
if (s == Qt::Unchecked) { |
| 361 |
continue; |
| 362 |
} |
| 363 |
if (it->second[0]->checkState(0) == Qt::PartiallyChecked) { |
| 364 |
selcount = -1; |
| 365 |
break; |
| 366 |
} |
| 367 |
++selcount; |
| 368 |
} |
| 369 |
auto state = it->second[0]->checkState(0); |
| 370 |
if (state == Qt::Checked && selcount != count) { |
| 371 |
setItemState(o, Qt::PartiallyChecked, true); |
| 372 |
} |
| 373 |
else if (state == Qt::PartiallyChecked && selcount == count) { |
| 374 |
setItemState(o, Qt::Checked, true); |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
std::vector<App::DocumentObject*> DlgObjectSelection::getSelections(SelectionOptions options) const |
| 381 |
{ |
| 382 |
|
| 383 |
if (returnOriginals) { |
| 384 |
return initSels; |
| 385 |
} |
| 386 |
|
| 387 |
std::vector<App::DocumentObject*> res; |
| 388 |
Base::Flags<SelectionOptions> flags(options); |
| 389 |
if (!flags.testFlag(SelectionOptions::Invert)) { |
| 390 |
for (const auto& v : itemMap) { |
| 391 |
if (v.second[0]->checkState(0) == Qt::Unchecked) { |
| 392 |
continue; |
| 393 |
} |
| 394 |
if (auto obj = v.first.getObject()) { |
| 395 |
res.push_back(obj); |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
else { |
| 400 |
for (auto obj : deps) { |
| 401 |
auto it = itemMap.find(obj); |
| 402 |
if (it == itemMap.end() || it->second[0]->checkState(0) == Qt::Unchecked) { |
| 403 |
res.push_back(obj); |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
if (flags.testFlag(SelectionOptions::Sort)) { |
| 408 |
std::sort(res.begin(), res.end()); |
| 409 |
} |
| 410 |
return res; |
| 411 |
} |
| 412 |
|
| 413 |
void DlgObjectSelection::onDepItemChanged(QTreeWidgetItem* depItem, int column) |
| 414 |
{ |
| 415 |
if (column) { |
| 416 |
return; |
| 417 |
} |
| 418 |
QSignalBlocker blocker(ui->depList); |
| 419 |
QSignalBlocker blocker2(ui->inList); |
| 420 |
QSignalBlocker blocker3(ui->treeWidget); |
| 421 |
auto state = depItem->checkState(0); |
| 422 |
if (depItem->isSelected()) { |
| 423 |
const auto items = depItem->treeWidget()->selectedItems(); |
| 424 |
for (auto item : items) { |
| 425 |
auto objT = qvariant_cast<App::SubObjectT>(item->data(0, Qt::UserRole)); |
| 426 |
auto it = itemMap.find(objT); |
| 427 |
if (it == itemMap.end()) { |
| 428 |
continue; |
| 429 |
} |
| 430 |
setCheckState(item, state); |
| 431 |
for (auto i : it->second) { |
| 432 |
setCheckState(i, state); |
| 433 |
} |
| 434 |
itemChanged[objT] = state; |
| 435 |
} |
| 436 |
} |
| 437 |
else { |
| 438 |
auto objT = qvariant_cast<App::SubObjectT>(depItem->data(0, Qt::UserRole)); |
| 439 |
auto it = itemMap.find(objT); |
| 440 |
if (it != itemMap.end()) { |
| 441 |
itemChanged[objT] = state; |
| 442 |
for (auto i : it->second) { |
| 443 |
setCheckState(i, state); |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
timer.start(10); |
| 448 |
} |
| 449 |
|
| 450 |
void DlgObjectSelection::onObjItemChanged(QTreeWidgetItem* objItem, int column) |
| 451 |
{ |
| 452 |
if (column != 0) { |
| 453 |
return; |
| 454 |
} |
| 455 |
|
| 456 |
QSignalBlocker blocker3(ui->treeWidget); |
| 457 |
auto state = objItem->checkState(0); |
| 458 |
if (objItem == allItem) { |
| 459 |
if (state == Qt::PartiallyChecked) { |
| 460 |
return; |
| 461 |
} |
| 462 |
ui->treeWidget->selectionModel()->clearSelection(); |
| 463 |
itemChanged.clear(); |
| 464 |
timer.stop(); |
| 465 |
onItemSelectionChanged(); |
| 466 |
if (state == Qt::Unchecked) { |
| 467 |
for (const auto& v : itemMap) { |
| 468 |
for (auto i : v.second) { |
| 469 |
setCheckState(i, Qt::Unchecked); |
| 470 |
} |
| 471 |
auto it = depMap.find(v.first); |
| 472 |
if (it != depMap.end()) { |
| 473 |
setCheckState(it->second, Qt::Unchecked); |
| 474 |
} |
| 475 |
it = inMap.find(v.first); |
| 476 |
if (it != inMap.end()) { |
| 477 |
setCheckState(it->second, Qt::Unchecked); |
| 478 |
} |
| 479 |
} |
| 480 |
} |
| 481 |
else { |
| 482 |
for (auto obj : initSels) { |
| 483 |
setCheckState(getItem(obj), Qt::Checked); |
| 484 |
} |
| 485 |
for (auto obj : deps) { |
| 486 |
setCheckState(getItem(obj), Qt::Checked); |
| 487 |
auto it = depMap.find(obj); |
| 488 |
if (it != depMap.end()) { |
| 489 |
setCheckState(it->second, Qt::Checked); |
| 490 |
} |
| 491 |
it = inMap.find(obj); |
| 492 |
if (it != inMap.end()) { |
| 493 |
setCheckState(it->second, Qt::Checked); |
| 494 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
return; |
| 498 |
} |
| 499 |
|
| 500 |
if (!objItem->isSelected()) { |
| 501 |
ui->treeWidget->selectionModel()->clearSelection(); |
| 502 |
objItem->setSelected(true); |
| 503 |
// We treat selected item in tree widget specially in case of checking |
| 504 |
// items in depList or inList. To simplify logic, we change selection |
| 505 |
// here if an unselected item has been checked. |
| 506 |
itemChanged[qvariant_cast<App::SubObjectT>(objItem->data(0, Qt::UserRole))] = state; |
| 507 |
onItemSelectionChanged(); |
| 508 |
} |
| 509 |
else { |
| 510 |
const auto items = ui->treeWidget->selectedItems(); |
| 511 |
for (auto item : items) { |
| 512 |
setCheckState(item, state); |
| 513 |
itemChanged[qvariant_cast<App::SubObjectT>(item->data(0, Qt::UserRole))] = state; |
| 514 |
} |
| 515 |
} |
| 516 |
timer.start(10); |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
static bool getOutList( |
| 521 |
App::DocumentObject* obj, |
| 522 |
std::set<App::DocumentObject*>& visited, |
| 523 |
std::vector<App::DocumentObject*>& result |
| 524 |
) |
| 525 |
{ |
| 526 |
if (!visited.insert(obj).second) { |
| 527 |
return false; |
| 528 |
} |
| 529 |
|
| 530 |
for (auto o : obj->getOutList()) { |
| 531 |
if (getOutList(o, visited, result)) { |
| 532 |
result.push_back(o); |
| 533 |
} |
| 534 |
} |
| 535 |
return true; |
| 536 |
} |
| 537 |
|
| 538 |
void DlgObjectSelection::checkItemChanged() |
| 539 |
{ |
| 540 |
|
| 541 |
QSignalBlocker blocker(ui->depList); |
| 542 |
QSignalBlocker blocker2(ui->inList); |
| 543 |
QSignalBlocker blocker3(ui->treeWidget); |
| 544 |
|
| 545 |
std::set<App::DocumentObject*> unchecked; |
| 546 |
|
| 547 |
for (const auto& v : itemChanged) { |
| 548 |
const auto& objT = v.first; |
| 549 |
Qt::CheckState state = v.second; |
| 550 |
if (auto obj = objT.getObject()) { |
| 551 |
if (state == Qt::Unchecked) { |
| 552 |
// We'll deal with unchecked item later |
| 553 |
if (ui->checkBoxAutoDeps->isChecked()) { |
| 554 |
unchecked.insert(obj); |
| 555 |
} |
| 556 |
} |
| 557 |
else { |
| 558 |
// For checked item, setItemState will auto select its |
| 559 |
// dependency |
| 560 |
setItemState(obj, state, true); |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
if (!unchecked.empty()) { |
| 566 |
// When some item is unchecked by the user, we need to re-check the |
| 567 |
// recursive outlist of the initially selected object, excluding all |
| 568 |
// currently unchecked object. And then uncheck any item that does not |
| 569 |
// appear in the returned outlist. |
| 570 |
|
| 571 |
for (const auto& v : itemMap) { |
| 572 |
auto item = v.second[0]; |
| 573 |
if (item->checkState(0) == Qt::Unchecked) { |
| 574 |
if (auto obj = qvariant_cast<App::SubObjectT>(item->data(0, Qt::UserRole)).getObject()) { |
| 575 |
unchecked.insert(obj); |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
auto outlist = initSels; |
| 581 |
for (auto obj : initSels) { |
| 582 |
getOutList(obj, unchecked, outlist); |
| 583 |
} |
| 584 |
std::sort(outlist.begin(), outlist.end()); |
| 585 |
|
| 586 |
for (const auto& v : itemMap) { |
| 587 |
if (!itemChanged.contains(v.first) && v.second[0]->checkState(0) == Qt::Unchecked) { |
| 588 |
continue; |
| 589 |
} |
| 590 |
if (auto obj = v.first.getObject()) { |
| 591 |
if (!std::binary_search(outlist.begin(), outlist.end(), obj)) { |
| 592 |
setItemState(obj, Qt::Unchecked, true); |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
itemChanged.clear(); |
| 599 |
updateAllItemState(); |
| 600 |
} |
| 601 |
|
| 602 |
QTreeWidgetItem* DlgObjectSelection::createDepItem(QTreeWidget* parent, App::DocumentObject* obj) |
| 603 |
{ |
| 604 |
auto item = new QTreeWidgetItem(parent); |
| 605 |
if (parent == ui->depList) { |
| 606 |
depMap[obj] = item; |
| 607 |
} |
| 608 |
else { |
| 609 |
inMap[obj] = item; |
| 610 |
} |
| 611 |
App::SubObjectT objT(obj); |
| 612 |
auto vp = Gui::Application::Instance->getViewProvider(obj); |
| 613 |
if (vp) { |
| 614 |
item->setIcon(0, vp->getIcon()); |
| 615 |
} |
| 616 |
item->setData(0, Qt::UserRole, QVariant::fromValue(objT)); |
| 617 |
item->setToolTip(0, QString::fromUtf8(objT.getObjectFullName().c_str())); |
| 618 |
item->setText(0, QString::fromUtf8((obj)->Label.getValue())); |
| 619 |
if (std::binary_search(initSels.begin(), initSels.end(), obj)) { |
| 620 |
QFont font = item->font(0); |
| 621 |
font.setBold(true); |
| 622 |
font.setItalic(true); |
| 623 |
item->setFont(0, font); |
| 624 |
} |
| 625 |
item->setText(1, QString::fromUtf8(obj->getDocument()->getName())); |
| 626 |
item->setText(2, QString::fromUtf8(obj->getNameInDocument())); |
| 627 |
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); |
| 628 |
auto it = itemMap.find(obj); |
| 629 |
if (it != itemMap.end()) { |
| 630 |
setCheckState(item, it->second[0]->checkState(0)); |
| 631 |
} |
| 632 |
return item; |
| 633 |
} |
| 634 |
|
| 635 |
void DlgObjectSelection::onItemSelectionChanged() |
| 636 |
{ |
| 637 |
ui->depList->clear(); |
| 638 |
depMap.clear(); |
| 639 |
ui->inList->clear(); |
| 640 |
inMap.clear(); |
| 641 |
|
| 642 |
std::vector<App::DocumentObject*> sels; |
| 643 |
const auto items = ui->treeWidget->selectedItems(); |
| 644 |
for (auto item : items) { |
| 645 |
if (item == allItem) { |
| 646 |
sels.clear(); |
| 647 |
break; |
| 648 |
} |
| 649 |
auto obj = qvariant_cast<App::SubObjectT>(item->data(0, Qt::UserRole)).getObject(); |
| 650 |
if (obj) { |
| 651 |
sels.push_back(obj); |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
std::vector<App::DocumentObject*> _deps; |
| 656 |
if (!sels.empty()) { |
| 657 |
std::sort(sels.begin(), sels.end()); |
| 658 |
for (auto dep : App::Document::getDependencyList(sels, App::Document::DepSort)) { |
| 659 |
if (!std::binary_search(sels.begin(), sels.end(), dep)) { |
| 660 |
_deps.push_back(dep); |
| 661 |
} |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
bool enabled = ui->depList->isSortingEnabled(); |
| 666 |
if (enabled) { |
| 667 |
ui->depList->setSortingEnabled(false); |
| 668 |
} |
| 669 |
|
| 670 |
bool enabled2 = ui->inList->isSortingEnabled(); |
| 671 |
if (enabled2) { |
| 672 |
ui->inList->setSortingEnabled(false); |
| 673 |
} |
| 674 |
|
| 675 |
{ |
| 676 |
QSignalBlocker blocker(ui->depList); |
| 677 |
auto& objs = !sels.empty() ? _deps : deps; |
| 678 |
for (auto it = objs.rbegin(); it != objs.rend(); ++it) { |
| 679 |
createDepItem(ui->depList, *it); |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
std::set<App::DocumentObject*> inlist; |
| 684 |
for (auto obj : sels) { |
| 685 |
obj->getInListEx(inlist, true); |
| 686 |
} |
| 687 |
for (auto it = inlist.begin(); it != inlist.end();) { |
| 688 |
if (!depSet.contains(*it) || std::binary_search(sels.begin(), sels.end(), *it)) { |
| 689 |
it = inlist.erase(it); |
| 690 |
} |
| 691 |
else { |
| 692 |
++it; |
| 693 |
} |
| 694 |
} |
| 695 |
{ |
| 696 |
QSignalBlocker blocker2(ui->inList); |
| 697 |
for (auto obj : inlist) { |
| 698 |
createDepItem(ui->inList, obj); |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
if (enabled) { |
| 703 |
ui->depList->setSortingEnabled(true); |
| 704 |
} |
| 705 |
if (enabled2) { |
| 706 |
ui->inList->setSortingEnabled(true); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
void DlgObjectSelection::onUseOriginalsBtnClicked() |
| 711 |
{ |
| 712 |
returnOriginals = true; |
| 713 |
QDialog::accept(); |
| 714 |
} |
| 715 |
|
| 716 |
void DlgObjectSelection::accept() |
| 717 |
{ |
| 718 |
QDialog::accept(); |
| 719 |
} |
| 720 |
|
| 721 |
void DlgObjectSelection::reject() |
| 722 |
{ |
| 723 |
QDialog::reject(); |
| 724 |
} |
| 725 |
|
| 726 |
void DlgObjectSelection::addCheckBox(QCheckBox* box) |
| 727 |
{ |
| 728 |
ui->horizontalLayout->insertWidget(0, box); |
| 729 |
} |
| 730 |
|
| 731 |
void DlgObjectSelection::setMessage(const QString& msg) |
| 732 |
{ |
| 733 |
ui->label->setText(msg); |
| 734 |
} |
| 735 |
|
| 736 |
void DlgObjectSelection::onAutoDeps(bool checked) |
| 737 |
{ |
| 738 |
hGrp->SetBool("ObjectSelectionAutoDeps", checked); |
| 739 |
if (!checked) { |
| 740 |
return; |
| 741 |
} |
| 742 |
|
| 743 |
QSignalBlocker blocker(ui->treeWidget); |
| 744 |
for (auto obj : deps) { |
| 745 |
auto it = itemMap.find(obj); |
| 746 |
if (it == itemMap.end()) { |
| 747 |
continue; |
| 748 |
} |
| 749 |
auto item = it->second[0]; |
| 750 |
if (item->checkState(0) == Qt::Unchecked) { |
| 751 |
continue; |
| 752 |
} |
| 753 |
Qt::CheckState state = Qt::Checked; |
| 754 |
for (auto o : obj->getOutList()) { |
| 755 |
auto it = itemMap.find(o); |
| 756 |
if (it == itemMap.end()) { |
| 757 |
continue; |
| 758 |
} |
| 759 |
if (it->second[0]->checkState(0) != Qt::Checked) { |
| 760 |
state = Qt::PartiallyChecked; |
| 761 |
break; |
| 762 |
} |
| 763 |
} |
| 764 |
for (auto i : it->second) { |
| 765 |
setCheckState(i, state); |
| 766 |
} |
| 767 |
} |
| 768 |
onItemSelectionChanged(); |
| 769 |
} |
| 770 |
|
| 771 |
void DlgObjectSelection::onShowDeps() |
| 772 |
{ |
| 773 |
bool checked = ui->checkBoxShowDeps->isChecked(); |
| 774 |
auto sizes = ui->vsplitter->sizes(); |
| 775 |
if (!checked && sizes[1] > 0) { |
| 776 |
sizes[1] = 0; |
| 777 |
} |
| 778 |
else if (checked && (sizes[0] == 0 || sizes[1] == 0)) { |
| 779 |
sizes[0] = sizes[1] = this->width() / 2; |
| 780 |
} |
| 781 |
else { |
| 782 |
return; |
| 783 |
} |
| 784 |
ui->vsplitter->setSizes(sizes); |
| 785 |
} |
| 786 |
|
| 787 |
#include "moc_DlgObjectSelection.cpp" |