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
|
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2019-2022 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "RDTreeWidget.h"
#include <QApplication>
#include <QColor>
#include <QDebug>
#include <QHeaderView>
#include <QMouseEvent>
#include <QPainter>
#include <QPen>
#include "Code/Interface/QRDInterface.h"
#include "Code/QRDUtils.h"
#include "Code/Resources.h"
#include "Widgets/Extended/RDTableView.h"
class RDTreeWidgetModel : public QAbstractItemModel
{
public:
RDTreeWidgetModel(RDTreeWidget *parent) : QAbstractItemModel(parent), widget(parent) {}
QModelIndex indexForItem(RDTreeWidgetItem *item, int column) const
{
if(item == NULL || item->m_parent == NULL)
return QModelIndex();
int row = item->m_parent->indexOfChild(item);
return createIndex(row, column, item);
}
RDTreeWidgetItem *itemForIndex(QModelIndex idx) const
{
if(!idx.isValid())
return widget->m_root;
return (RDTreeWidgetItem *)idx.internalPointer();
}
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override
{
emit beginResetModel();
widget->m_root->sort(column, order);
emit endResetModel();
}
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override
{
if(row < 0 || column < 0 || row >= rowCount(parent) || column >= columnCount(parent))
return QModelIndex();
RDTreeWidgetItem *par = itemForIndex(parent);
if(par == NULL)
par = widget->m_root;
return createIndex(row, column, par->m_children[row]);
}
void beginInsertChild(RDTreeWidgetItem *item, int index)
{
beginInsertRows(indexForItem(item, 0), index, index);
}
void endInsertChild(RDTreeWidgetItem *item) { endInsertRows(); }
void beginRemoveChildren(RDTreeWidgetItem *parent, int first, int last)
{
beginRemoveRows(indexForItem(parent, 0), first, last);
}
void endRemoveChildren() { endRemoveRows(); }
void itemChanged(RDTreeWidgetItem *item, const QVector<int> &roles)
{
QModelIndex topLeft = indexForItem(item, 0);
QModelIndex bottomRight = indexForItem(item, columnCount() - 1);
emit dataChanged(topLeft, bottomRight, roles);
}
void refresh()
{
emit beginResetModel();
emit endResetModel();
}
void headerRefresh() { emit headerDataChanged(Qt::Horizontal, 0, columnCount() - 1); }
void itemsChanged(RDTreeWidgetItem *p, QPair<int, int> minRowColumn, QPair<int, int> maxRowColumn,
const QVector<int> &roles)
{
QModelIndex topLeft = createIndex(minRowColumn.first, minRowColumn.second, p);
QModelIndex bottomRight = createIndex(maxRowColumn.first, maxRowColumn.second, p);
emit dataChanged(topLeft, bottomRight, roles);
}
QModelIndex parent(const QModelIndex &index) const override
{
if(index.internalPointer() == NULL)
return QModelIndex();
RDTreeWidgetItem *item = itemForIndex(index);
if(item)
return indexForItem(item->m_parent, 0);
return QModelIndex();
}
int rowCount(const QModelIndex &parent = QModelIndex()) const override
{
if(!parent.isValid())
return widget->m_root->childCount();
RDTreeWidgetItem *parentItem = itemForIndex(parent);
if(parentItem)
return parentItem->childCount();
return 0;
}
int columnCount(const QModelIndex &parent = QModelIndex()) const override
{
return widget->m_headers.count();
}
bool hasChildren(const QModelIndex &parent) const override
{
if(!parent.isValid())
return widget->m_root->childCount() > 0;
RDTreeWidgetItem *parentItem = itemForIndex(parent);
if(parentItem)
return parentItem->childCount() > 0;
return false;
}
Qt::ItemFlags flags(const QModelIndex &index) const override
{
if(!index.isValid())
return 0;
RDTreeWidgetItem *item = itemForIndex(index);
Qt::ItemFlags ret = QAbstractItemModel::flags(index) | Qt::ItemIsUserCheckable;
if(item->editable(index.column()))
ret |= Qt::ItemIsEditable;
return ret;
}
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
{
if(orientation == Qt::Horizontal && role == Qt::DisplayRole && section < widget->m_headers.count())
return widget->m_headers[section];
return QVariant();
}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
{
RDTreeWidgetItem *item = itemForIndex(index);
// invisible root element has no data
if(!item->m_parent || index.column() >= item->m_text.count())
return QVariant();
if(role == Qt::TextAlignmentRole)
{
if(index.column() < widget->m_alignments.count())
{
Qt::Alignment align = widget->m_alignments[index.column()];
if(align != 0)
return QVariant(align);
}
}
else if(role == Qt::DecorationRole)
{
if(widget->m_hoverColumn == index.column())
{
if(itemForIndex(widget->m_currentHoverIndex) == item)
return widget->m_activeHoverIcon;
else
return widget->m_normalHoverIcon;
}
// if not hovering, fall through to the decoration from the item
}
else if(role == Qt::BackgroundRole)
{
if(item->m_back.style() != Qt::NoBrush)
return item->m_back;
// otherwise if we're hover-highlighting, use the highlight color at 20% opacity
if(itemForIndex(widget->m_currentHoverIndex) == item && widget->m_hoverColumn >= 0)
{
QColor col = widget->palette().color(QPalette::Highlight);
col.setAlphaF(0.2f);
return QBrush(col);
}
// otherwise, no special background
return QVariant();
}
else if(role == RDTreeView::TreeLineColorRole)
{
if(item->m_treeCol.isValid())
return QBrush(item->m_treeCol);
}
return item->data(index.column(), role);
}
bool setData(const QModelIndex &index, const QVariant &value, int role) override
{
RDTreeWidgetItem *item = itemForIndex(index);
// invisible root element has no data
if(!item->m_parent)
return false;
bool ret = false;
if(role == Qt::DisplayRole || role == Qt::EditRole)
{
if(index.column() < item->m_text.count())
{
item->m_text[index.column()] = value;
ret = true;
}
}
else if(role == Qt::DecorationRole)
{
if(index.column() < item->m_icons.count())
{
item->m_icons[index.column()] = value.value<QIcon>();
ret = true;
}
}
else if(role == Qt::BackgroundRole)
{
item->m_back = value.value<QBrush>();
ret = true;
}
else if(role == Qt::ForegroundRole)
{
item->m_fore = value.value<QBrush>();
ret = true;
}
else if(role == Qt::ToolTipRole)
{
item->m_tooltip = value.toString();
ret = true;
}
else if(role == Qt::FontRole)
{
ret = false;
}
else
{
item->setData(index.column(), role, value);
ret = true;
}
if(ret)
widget->itemDataChanged(item, index.column(), role);
return ret;
}
private:
RDTreeWidget *widget;
};
RDTreeWidgetItem::RDTreeWidgetItem(const QVariantList &values)
{
m_text.reserve(values.size());
for(const QVariant &v : values)
m_text.push_back(v);
m_icons.resize(m_text.size());
for(int i = 0; i < m_text.count(); i++)
RichResourceTextInitialise(m_text[i]);
}
RDTreeWidgetItem::RDTreeWidgetItem(const std::initializer_list<QVariant> &values)
{
m_text = values;
m_icons.resize(m_text.size());
for(int i = 0; i < m_text.count(); i++)
RichResourceTextInitialise(m_text[i]);
}
void RDTreeWidgetItem::checkForResourceId(int col)
{
RichResourceTextInitialise(m_text[col]);
}
void RDTreeWidgetItem::sort(int column, Qt::SortOrder order)
{
ICaptureContext *ctx = getCaptureContext(m_widget);
std::stable_sort(m_children.begin(), m_children.end(),
[ctx, column, order](const RDTreeWidgetItem *a, const RDTreeWidgetItem *b) {
if(a->treeWidget()->m_SortComparison)
return a->treeWidget()->m_SortComparison(column, order, a, b);
QVariant va = a->data(column, Qt::DisplayRole);
QVariant vb = b->data(column, Qt::DisplayRole);
QString sa, sb;
if(ctx)
{
sa = RichResourceTextFormat(*ctx, va);
sb = RichResourceTextFormat(*ctx, vb);
}
else
{
sa = va.toString();
sb = vb.toString();
}
bool da_ok = false, db_ok = false;
double da = sa.toDouble(&da_ok);
double db = sb.toDouble(&db_ok);
int comp;
if(da_ok && db_ok)
{
if(order == Qt::AscendingOrder)
return da < db;
return da > db;
}
else
{
comp = QString::compare(sa, sb, Qt::CaseInsensitive);
}
if(order == Qt::AscendingOrder)
return comp < 0;
return comp > 0;
});
for(RDTreeWidgetItem *child : m_children)
child->sort(column, order);
}
RDTreeWidgetItem::~RDTreeWidgetItem()
{
if(m_parent)
m_parent->removeChild(this);
clear();
delete m_data;
}
QVariant RDTreeWidgetItem::data(int column, int role) const
{
if(role == Qt::DisplayRole || role == Qt::EditRole)
{
return m_text[column];
}
else if(role == Qt::DecorationRole)
{
return m_icons[column];
}
else if(role == Qt::BackgroundRole)
{
if(m_back != QBrush())
return m_back;
return QVariant();
}
else if(role == Qt::ForegroundRole)
{
if(m_fore != QBrush())
return m_fore;
return QVariant();
}
else if(role == Qt::ToolTipRole)
{
if(!m_tooltip.isEmpty())
return m_tooltip;
}
else if(role == Qt::FontRole)
{
QFont font; // TODO should this come from some default?
font.setItalic(m_italic);
font.setBold(m_bold);
return font;
}
// if we don't have any custom data, and the role wasn't covered above, it's invalid
if(m_data == NULL || column >= m_data->count())
return QVariant();
const QVector<RoleData> &dataVec = (*m_data)[column];
for(const RoleData &d : dataVec)
{
if(d.role == role)
return d.data;
}
return QVariant();
}
void RDTreeWidgetItem::setData(int column, int role, const QVariant &value)
{
// we lazy allocate
if(!m_data)
{
m_data = new QVector<QVector<RoleData>>;
m_data->resize(qMax(m_text.count(), column + 1));
}
// data is allowed to resize above the column count in the widget
if(m_data->count() <= column)
m_data->resize(column + 1);
QVector<RoleData> &dataVec = (*m_data)[column];
for(RoleData &d : dataVec)
{
if(d.role == role)
{
bool different = (d.data != value);
d.data = value;
if(different && role < Qt::UserRole)
m_widget->m_model->itemChanged(this, {role});
return;
}
}
dataVec.push_back(RoleData(role, value));
if(m_widget && role < Qt::UserRole)
m_widget->m_model->itemChanged(this, {role});
}
void RDTreeWidgetItem::addChild(RDTreeWidgetItem *item)
{
insertChild(m_children.count(), item);
}
void RDTreeWidgetItem::insertChild(int index, RDTreeWidgetItem *item)
{
int colCount = item->m_text.count();
if(m_widget && colCount < m_widget->m_headers.count())
qCritical() << "Item added with insufficient column data";
// remove it from any previous parent
if(item->m_parent)
item->m_parent->removeChild(this);
// set up its new parent to us
item->m_parent = this;
// set the widget in case this changed
item->setWidget(m_widget);
// resize per-column vectors to column count
item->m_text.resize(colCount);
item->m_icons.resize(colCount);
// data can resize up, but we don't resize it down.
if(item->m_data)
item->m_data->resize(qMax(item->m_data->count(), colCount));
if(m_widget)
m_widget->beginInsertChild(this, index);
// add to our list of children
m_children.insert(index, item);
if(m_widget)
m_widget->endInsertChild(this, index);
}
void RDTreeWidgetItem::setWidget(RDTreeWidget *widget)
{
if(widget == m_widget)
return;
// if the widget is different, we need to recurse to children
m_widget = widget;
for(RDTreeWidgetItem *item : m_children)
item->setWidget(widget);
}
void RDTreeWidgetItem::dataChanged(int column, int role)
{
if(m_widget)
m_widget->itemDataChanged(this, column, role);
}
RDTreeWidgetItem *RDTreeWidgetItem::takeChild(int index)
{
if(m_widget && !m_widget->m_clearing)
m_widget->m_model->beginRemoveChildren(this, index, index);
m_children[index]->m_parent = NULL;
RDTreeWidgetItem *ret = m_children.takeAt(index);
if(m_widget && !m_widget->m_clearing)
m_widget->m_model->endRemoveChildren();
return ret;
}
void RDTreeWidgetItem::removeChild(RDTreeWidgetItem *child)
{
if(m_widget)
{
int row = m_children.indexOf(child);
m_widget->m_model->beginRemoveChildren(this, row, row);
}
m_children.removeOne(child);
if(m_widget)
m_widget->m_model->endRemoveChildren();
}
void RDTreeWidgetItem::clear()
{
if(!childCount())
return;
if(m_widget && !m_widget->m_clearing)
m_widget->m_model->beginRemoveChildren(this, 0, childCount() - 1);
QVector<RDTreeWidgetItem *> children;
m_children.swap(children);
for(RDTreeWidgetItem *c : children)
{
c->m_parent = NULL;
c->m_widget = NULL;
delete c;
}
if(m_widget && !m_widget->m_clearing)
m_widget->m_model->endRemoveChildren();
}
RDTreeWidgetItemIterator::RDTreeWidgetItemIterator(RDTreeWidget *widget)
{
if(widget->topLevelItemCount() == 0)
m_Current = NULL;
else
m_Current = widget->topLevelItem(0);
}
RDTreeWidgetItemIterator &RDTreeWidgetItemIterator::operator++()
{
// depth first
if(m_Current->childCount() > 0)
{
m_Current = m_Current->child(0);
return *this;
}
// otherwise check if we have siblings, recursively up
RDTreeWidgetItem *parent = m_Current->parent();
RDTreeWidgetItem *child = m_Current;
while(parent)
{
// if there's a sibling at this level, move to it
int idx = parent->indexOfChild(child);
if(idx + 1 < parent->childCount())
{
m_Current = parent->child(idx + 1);
return *this;
}
// if there are no more siblings at this level, move up
child = parent;
parent = parent->parent();
// if we just exhausted siblings at the top-level, parent will now be NULL, so we abort.
}
// no more siblings, stop.
m_Current = NULL;
return *this;
}
RDTreeWidget::RDTreeWidget(QWidget *parent) : RDTreeView(parent)
{
header()->setSectionsMovable(false);
m_root = new RDTreeWidgetItem;
m_root->m_widget = this;
m_model = new RDTreeWidgetModel(this);
RDTreeView::setModel(m_model);
QObject::connect(this, &RDTreeWidget::activated, [this](const QModelIndex &idx) {
emit itemActivated(m_model->itemForIndex(idx), idx.column());
});
QObject::connect(this, &RDTreeWidget::clicked, [this](const QModelIndex &idx) {
emit itemClicked(m_model->itemForIndex(idx), idx.column());
});
QObject::connect(this, &RDTreeWidget::doubleClicked, [this](const QModelIndex &idx) {
emit itemDoubleClicked(m_model->itemForIndex(idx), idx.column());
});
}
RDTreeWidget::~RDTreeWidget()
{
blockSignals(true);
RDTreeView::setModel(NULL);
blockSignals(false);
delete m_root;
delete m_model;
}
void RDTreeWidget::beginUpdate()
{
m_queueUpdates = true;
m_queuedItem = NULL;
m_lowestIndex = m_highestIndex = qMakePair<int, int>(-1, -1);
m_queuedChildren = false;
m_queuedRoles = 0;
setUpdatesEnabled(false);
}
void RDTreeWidget::endUpdate()
{
m_queueUpdates = false;
if(m_queuedRoles || m_queuedChildren)
{
// if we updated multiple different trees we can't issue a single dataChanged for everything
// under a parent. Refresh the whole model.
if(m_queuedItem == NULL)
{
m_model->refresh();
}
else
{
QVector<int> roles;
for(int r = 0; r < 64; r++)
{
if(m_queuedRoles & (1ULL << r))
roles.push_back(r);
}
if(m_queuedChildren)
{
m_model->beginInsertChild(m_queuedItem, m_queuedItem->childCount());
m_model->endInsertChild(m_queuedItem);
}
if(!roles.isEmpty())
m_model->itemsChanged(m_queuedItem, m_lowestIndex, m_highestIndex, roles);
}
}
setUpdatesEnabled(true);
}
void RDTreeWidget::setColumnAlignment(int column, Qt::Alignment align)
{
if(m_alignments.count() <= column)
m_alignments.resize(column + 1);
m_alignments[column] = align;
}
RDTreeWidgetItem *RDTreeWidget::itemForIndex(QModelIndex idx) const
{
if(idx.model() == m_model)
return m_model->itemForIndex(idx);
return NULL;
}
void RDTreeWidget::copyItem(QPoint pos, RDTreeWidgetItem *item)
{
copyIndex(pos, m_model->indexForItem(item, 0));
}
void RDTreeWidget::setColumns(const QStringList &columns)
{
m_headers = columns;
m_model->refresh();
}
void RDTreeWidget::setHeaderText(int column, const QString &text)
{
m_headers[column] = text;
m_model->headerRefresh();
}
RDTreeWidgetItem *RDTreeWidget::selectedItem() const
{
QModelIndexList sel = selectionModel()->selectedIndexes();
if(sel.isEmpty())
return NULL;
return m_model->itemForIndex(sel[0]);
}
RDTreeWidgetItem *RDTreeWidget::currentItem() const
{
return m_model->itemForIndex(currentIndex());
}
void RDTreeWidget::setSelectedItem(RDTreeWidgetItem *node)
{
selectionModel()->select(m_model->indexForItem(node, 0),
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
}
void RDTreeWidget::setCurrentItem(RDTreeWidgetItem *node)
{
setCurrentIndex(m_model->indexForItem(node, 0));
}
RDTreeWidgetItem *RDTreeWidget::itemAt(const QPoint &p) const
{
return m_model->itemForIndex(indexAt(p));
}
void RDTreeWidget::expandItem(RDTreeWidgetItem *item)
{
expand(m_model->indexForItem(item, 0));
}
void RDTreeWidget::expandAllItems(RDTreeWidgetItem *item)
{
expandAll(m_model->indexForItem(item, 0));
}
void RDTreeWidget::collapseItem(RDTreeWidgetItem *item)
{
collapse(m_model->indexForItem(item, 0));
}
void RDTreeWidget::collapseAllItems(RDTreeWidgetItem *item)
{
collapseAll(m_model->indexForItem(item, 0));
}
void RDTreeWidget::scrollToItem(RDTreeWidgetItem *item)
{
scrollTo(m_model->indexForItem(item, 0));
}
void RDTreeWidget::editItem(RDTreeWidgetItem *item)
{
edit(m_model->indexForItem(item, 0));
}
void RDTreeWidget::clear()
{
m_clearing = true;
m_root->clear();
m_clearing = false;
m_currentHoverIndex = QModelIndex();
m_model->refresh();
}
void RDTreeWidget::mouseMoveEvent(QMouseEvent *e)
{
RDTreeWidgetItem *oldHover = m_model->itemForIndex(m_currentHoverIndex);
QModelIndex oldHoverIndex = m_currentHoverIndex;
RDTreeView::mouseMoveEvent(e);
RDTreeWidgetItem *newHover = m_model->itemForIndex(m_currentHoverIndex);
if(m_currentHoverIndex.column() == m_hoverColumn && m_hoverHandCursor)
{
setCursor(QCursor(Qt::PointingHandCursor));
}
else if(oldHoverIndex.column() == m_hoverColumn &&
m_currentHoverIndex.column() != m_hoverColumn && m_hoverHandCursor)
{
unsetCursor();
}
if(oldHover == newHover)
return;
// it's only two items, don't try and make a range but just change them both
QVector<int> roles = {Qt::DecorationRole, Qt::BackgroundRole, Qt::ForegroundRole};
if(oldHover)
m_model->itemChanged(oldHover, roles);
m_model->itemChanged(newHover, roles);
emit hoverItemChanged(newHover);
emit mouseMove(e);
RDTreeView::mouseMoveEvent(e);
}
void RDTreeWidget::mouseReleaseEvent(QMouseEvent *e)
{
QModelIndex idx = indexAt(e->pos());
if(idx.isValid() && idx.column() == m_hoverColumn && m_activateOnClick)
{
emit itemActivated(itemAt(e->pos()), idx.column());
}
RDTreeView::mouseReleaseEvent(e);
}
void RDTreeWidget::leaveEvent(QEvent *e)
{
unsetCursor();
if(m_currentHoverIndex.isValid())
{
RDTreeWidgetItem *item = m_model->itemForIndex(m_currentHoverIndex);
m_model->itemChanged(item, {Qt::DecorationRole, Qt::BackgroundRole, Qt::ForegroundRole});
}
emit hoverItemChanged(NULL);
RDTreeView::leaveEvent(e);
}
void RDTreeWidget::focusOutEvent(QFocusEvent *event)
{
if(m_clearSelectionOnFocusLoss)
{
setCurrentItem(NULL);
clearSelection();
}
RDTreeView::focusOutEvent(event);
}
void RDTreeWidget::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
emit itemSelectionChanged();
RDTreeView::selectionChanged(selected, deselected);
}
void RDTreeWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
{
emit currentItemChanged(m_model->itemForIndex(current), m_model->itemForIndex(previous));
RDTreeView::currentChanged(current, previous);
}
void RDTreeWidget::itemDataChanged(RDTreeWidgetItem *item, int column, int role)
{
if(m_queueUpdates)
{
m_queuedRoles |= (1ULL << role);
// for now we only support updating the whole row, with all columns, even if only one column
// changed.
int row = item->m_parent->indexOfChild(item);
// no queued updates yet, set up this one
if(m_lowestIndex.first == -1)
{
m_queuedItem = item;
m_lowestIndex = qMakePair<int, int>(row, 0);
m_highestIndex = qMakePair<int, int>(m_lowestIndex.first, m_headers.count() - 1);
}
else
{
// there's already an update. Check if we can expand it
if(m_queuedItem == item)
{
m_lowestIndex.first = qMin(m_lowestIndex.first, row);
m_highestIndex.first = qMax(m_highestIndex.first, row);
}
else
{
// can't batch updates across multiple parents, so we just fallback to full model refresh
m_queuedItem = NULL;
}
}
}
else
{
m_model->itemChanged(item, {role});
}
emit itemChanged(item, column);
}
void RDTreeWidget::beginInsertChild(RDTreeWidgetItem *item, int index)
{
if(m_queueUpdates)
{
m_queuedChildren = true;
if(m_lowestIndex.first == -1)
{
m_queuedItem = item;
// make an update of row 0. This will be a bit pessimistic if there are later data changes
// in a later row, but we're generally only changing data *or* adding children, not both,
// and in either case this is primarily about batching updates not providing a minimal update
// set
m_lowestIndex = qMakePair<int, int>(0, 0);
m_highestIndex = qMakePair<int, int>(0, m_headers.count() - 1);
}
else
{
if(m_queuedItem == item)
{
// there's already an update. don't need to expand it, the m_queuedChildren is enough
}
else
{
// can't batch updates across multiple parents, so we just fallback to full model refresh
m_queuedItem = NULL;
}
}
}
else
{
m_model->beginInsertChild(item, index);
}
}
void RDTreeWidget::endInsertChild(RDTreeWidgetItem *item, int index)
{
// work is all done in beginInsertChild
if(!m_queueUpdates)
m_model->endInsertChild(item);
}
|