File indexing completed on 2024-05-12 04:58:28

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2014  David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "treewidget.h"
0019 
0020 #include <QMouseEvent>
0021 
0022 TreeWidget::TreeWidget(QWidget* parent)
0023     : QTreeWidget(parent)
0024     , m_refreshAllItemsNeeded(true)
0025     , m_showMode(ItemsCollapsed)
0026 {
0027     connect(this, &QTreeWidget::itemChanged, this, &TreeWidget::sheduleRefresh);
0028 }
0029 
0030 void TreeWidget::clear()
0031 {
0032     QTreeWidget::clear();
0033     m_allTreeItems.clear();
0034 }
0035 
0036 void TreeWidget::sheduleRefresh()
0037 {
0038     m_refreshAllItemsNeeded = true;
0039 }
0040 
0041 void TreeWidget::addTopLevelItem(QTreeWidgetItem* item)
0042 {
0043     m_allTreeItems.append(item);
0044     QTreeWidget::addTopLevelItem(item);
0045 }
0046 
0047 void TreeWidget::addTopLevelItems(const QList<QTreeWidgetItem*> &items)
0048 {
0049     m_allTreeItems.append(items);
0050     QTreeWidget::addTopLevelItems(items);
0051 }
0052 
0053 void TreeWidget::insertTopLevelItem(int index, QTreeWidgetItem* item)
0054 {
0055     m_allTreeItems.append(item);
0056     QTreeWidget::insertTopLevelItem(index, item);
0057 }
0058 
0059 void TreeWidget::insertTopLevelItems(int index, const QList<QTreeWidgetItem*> &items)
0060 {
0061     m_allTreeItems.append(items);
0062     QTreeWidget::insertTopLevelItems(index, items);
0063 }
0064 
0065 void TreeWidget::mousePressEvent(QMouseEvent* event)
0066 {
0067     if (event->modifiers() == Qt::ControlModifier) {
0068         Q_EMIT itemControlClicked(itemAt(event->position().toPoint()));
0069     }
0070 
0071     if (event->buttons() == Qt::MiddleButton) {
0072         Q_EMIT itemMiddleButtonClicked(itemAt(event->position().toPoint()));
0073     }
0074 
0075     QTreeWidget::mousePressEvent(event);
0076 }
0077 
0078 void TreeWidget::iterateAllItems(QTreeWidgetItem* parent)
0079 {
0080     int count = parent ? parent->childCount() : topLevelItemCount();
0081 
0082     for (int i = 0; i < count; i++) {
0083         QTreeWidgetItem* item = parent ? parent->child(i) : topLevelItem(i);
0084 
0085         if (item->childCount() == 0) {
0086             m_allTreeItems.append(item);
0087         }
0088 
0089         iterateAllItems(item);
0090     }
0091 }
0092 
0093 QList<QTreeWidgetItem*> TreeWidget::allItems()
0094 {
0095     if (m_refreshAllItemsNeeded) {
0096         m_allTreeItems.clear();
0097         iterateAllItems(nullptr);
0098         m_refreshAllItemsNeeded = false;
0099     }
0100 
0101     return m_allTreeItems;
0102 }
0103 
0104 void TreeWidget::filterString(const QString &string)
0105 {
0106     const QList<QTreeWidgetItem*> _allItems = allItems();
0107     QList<QTreeWidgetItem*> parents;
0108     bool stringIsEmpty = string.isEmpty();
0109     for (QTreeWidgetItem* item : _allItems) {
0110         bool containsString = stringIsEmpty || item->text(0).contains(string, Qt::CaseInsensitive);
0111         if (containsString) {
0112             item->setHidden(false);
0113             if (item->parent()) {
0114                 if (!parents.contains(item->parent())) {
0115                     parents << item->parent();
0116                 }
0117             }
0118         }
0119         else {
0120             item->setHidden(true);
0121             if (item->parent()) {
0122                 item->parent()->setHidden(true);
0123             }
0124         }
0125     }
0126 
0127     for (int i = 0; i < parents.size(); ++i) {
0128         QTreeWidgetItem* parentItem = parents.at(i);
0129         parentItem->setHidden(false);
0130         if (stringIsEmpty) {
0131             parentItem->setExpanded(m_showMode == ItemsExpanded);
0132         }
0133         else {
0134             parentItem->setExpanded(true);
0135         }
0136 
0137         if (parentItem->parent() && !parents.contains(parentItem->parent())) {
0138             parents << parentItem->parent();
0139         }
0140     }
0141 }
0142 
0143 bool TreeWidget::appendToParentItem(const QString &parentText, QTreeWidgetItem* item)
0144 {
0145     QList<QTreeWidgetItem*> list = findItems(parentText, Qt::MatchExactly);
0146     if (list.isEmpty()) {
0147         return false;
0148     }
0149     QTreeWidgetItem* parentItem = list.at(0);
0150     if (!parentItem) {
0151         return false;
0152     }
0153 
0154     m_allTreeItems.append(item);
0155     parentItem->addChild(item);
0156     return true;
0157 }
0158 
0159 bool TreeWidget::appendToParentItem(QTreeWidgetItem* parent, QTreeWidgetItem* item)
0160 {
0161     if (!parent || parent->treeWidget() != this) {
0162         return false;
0163     }
0164 
0165     m_allTreeItems.append(item);
0166     parent->addChild(item);
0167     return true;
0168 }
0169 
0170 bool TreeWidget::prependToParentItem(const QString &parentText, QTreeWidgetItem* item)
0171 {
0172     QList<QTreeWidgetItem*> list = findItems(parentText, Qt::MatchExactly);
0173     if (list.isEmpty()) {
0174         return false;
0175     }
0176     QTreeWidgetItem* parentItem = list.at(0);
0177     if (!parentItem) {
0178         return false;
0179     }
0180 
0181     m_allTreeItems.append(item);
0182     parentItem->insertChild(0, item);
0183     return true;
0184 }
0185 
0186 bool TreeWidget::prependToParentItem(QTreeWidgetItem* parent, QTreeWidgetItem* item)
0187 {
0188     if (!parent || parent->treeWidget() != this) {
0189         return false;
0190     }
0191 
0192     m_allTreeItems.append(item);
0193     parent->insertChild(0, item);
0194     return true;
0195 }
0196 
0197 void TreeWidget::deleteItem(QTreeWidgetItem* item)
0198 {
0199 //    if (m_allTreeItems.contains(item)) {
0200 //        m_allTreeItems.removeOne(item);
0201 //    }
0202 
0203     m_refreshAllItemsNeeded = true;
0204 
0205     delete item;
0206 }
0207 
0208 void TreeWidget::deleteItems(const QList<QTreeWidgetItem*> &items)
0209 {
0210     m_refreshAllItemsNeeded = true;
0211 
0212     qDeleteAll(items);
0213 }