1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-14 11:02:19 +01:00
falkonOfficial/src/lib/tools/treewidget.cpp

225 lines
5.9 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
2011-03-03 18:29:20 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
2011-03-03 15:24:59 +01:00
#include "treewidget.h"
#include <QMouseEvent>
TreeWidget::TreeWidget(QWidget* parent)
: QTreeWidget(parent)
, m_refreshAllItemsNeeded(true)
, m_showMode(ItemsCollapsed)
2011-03-03 15:24:59 +01:00
{
setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
connect(this, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(sheduleRefresh()));
}
void TreeWidget::clear()
{
QTreeWidget::clear();
m_allTreeItems.clear();
}
void TreeWidget::sheduleRefresh()
{
m_refreshAllItemsNeeded = true;
}
void TreeWidget::addTopLevelItem(QTreeWidgetItem* item)
{
m_allTreeItems.append(item);
QTreeWidget::addTopLevelItem(item);
}
void TreeWidget::addTopLevelItems(const QList<QTreeWidgetItem*> &items)
{
m_allTreeItems.append(items);
QTreeWidget::addTopLevelItems(items);
}
void TreeWidget::insertTopLevelItem(int index, QTreeWidgetItem* item)
{
m_allTreeItems.append(item);
QTreeWidget::insertTopLevelItem(index, item);
}
void TreeWidget::insertTopLevelItems(int index, const QList<QTreeWidgetItem*> &items)
{
m_allTreeItems.append(items);
QTreeWidget::insertTopLevelItems(index, items);
2011-03-03 15:24:59 +01:00
}
2011-03-17 17:03:04 +01:00
void TreeWidget::mousePressEvent(QMouseEvent* event)
2011-03-03 15:24:59 +01:00
{
if (event->modifiers() == Qt::ControlModifier) {
2011-03-03 15:24:59 +01:00
emit itemControlClicked(itemAt(event->pos()));
}
2011-03-03 15:24:59 +01:00
if (event->buttons() == Qt::MiddleButton) {
emit itemMiddleButtonClicked(itemAt(event->pos()));
}
2011-03-03 15:24:59 +01:00
QTreeWidget::mousePressEvent(event);
}
2011-03-27 21:59:40 +02:00
void TreeWidget::iterateAllItems(QTreeWidgetItem* parent)
2011-03-27 21:59:40 +02:00
{
int count = parent ? parent->childCount() : topLevelItemCount();
2011-03-27 21:59:40 +02:00
for (int i = 0; i < count; i++) {
QTreeWidgetItem* item = parent ? parent->child(i) : topLevelItem(i);
2011-03-27 21:59:40 +02:00
if (item->childCount() == 0) {
m_allTreeItems.append(item);
}
2011-03-27 21:59:40 +02:00
iterateAllItems(item);
}
2011-03-27 21:59:40 +02:00
}
QList<QTreeWidgetItem*> TreeWidget::allItems()
2011-03-27 21:59:40 +02:00
{
if (m_refreshAllItemsNeeded) {
m_allTreeItems.clear();
iterateAllItems(0);
m_refreshAllItemsNeeded = false;
}
return m_allTreeItems;
2011-03-27 21:59:40 +02:00
}
void TreeWidget::filterString(QString string)
2011-03-27 21:59:40 +02:00
{
expandAll();
QList<QTreeWidgetItem*> _allItems = allItems();
2011-03-27 21:59:40 +02:00
if (string.isEmpty()) {
foreach(QTreeWidgetItem * item, _allItems)
item->setHidden(false);
for (int i = 0; i < topLevelItemCount(); i++) {
topLevelItem(i)->setHidden(false);
}
if (m_showMode == ItemsCollapsed) {
collapseAll();
}
}
else {
foreach(QTreeWidgetItem * item, _allItems) {
item->setHidden(!item->text(0).contains(string, Qt::CaseInsensitive));
item->setExpanded(true);
}
for (int i = 0; i < topLevelItemCount(); i++) {
topLevelItem(i)->setHidden(false);
}
QTreeWidgetItem* firstItem = topLevelItem(0);
QTreeWidgetItem* belowItem = itemBelow(firstItem);
int topLvlIndex = 0;
while (firstItem) {
if (firstItem->text(0).contains(string, Qt::CaseInsensitive)) {
firstItem->setHidden(false);
}
else if (!firstItem->parent() && !belowItem) {
firstItem->setHidden(true);
}
else if (!belowItem) {
break;
}
else if (!firstItem->parent() && !belowItem->parent()) {
firstItem->setHidden(true);
}
topLvlIndex++;
firstItem = topLevelItem(topLvlIndex);
belowItem = itemBelow(firstItem);
}
2011-03-27 21:59:40 +02:00
}
}
bool TreeWidget::appendToParentItem(const QString &parentText, QTreeWidgetItem* item)
2011-03-27 21:59:40 +02:00
{
QList<QTreeWidgetItem*> list = findItems(parentText, Qt::MatchExactly);
if (list.count() == 0) {
return false;
}
QTreeWidgetItem* parentItem = list.at(0);
if (!parentItem) {
return false;
}
2011-03-27 21:59:40 +02:00
m_allTreeItems.append(item);
parentItem->addChild(item);
return true;
}
bool TreeWidget::appendToParentItem(QTreeWidgetItem* parent, QTreeWidgetItem* item)
{
if (!parent || parent->treeWidget() != this) {
return false;
}
m_allTreeItems.append(item);
parent->addChild(item);
return true;
2011-03-27 21:59:40 +02:00
}
bool TreeWidget::prependToParentItem(const QString &parentText, QTreeWidgetItem* item)
{
QList<QTreeWidgetItem*> list = findItems(parentText, Qt::MatchExactly);
if (list.count() == 0) {
return false;
}
QTreeWidgetItem* parentItem = list.at(0);
if (!parentItem) {
return false;
}
m_allTreeItems.append(item);
parentItem->insertChild(0, item);
return true;
}
bool TreeWidget::prependToParentItem(QTreeWidgetItem* parent, QTreeWidgetItem* item)
{
if (!parent || parent->treeWidget() != this) {
return false;
}
m_allTreeItems.append(item);
parent->insertChild(0, item);
return true;
}
void TreeWidget::deleteItem(QTreeWidgetItem* item)
{
// if (m_allTreeItems.contains(item)) {
// m_allTreeItems.removeOne(item);
// }
m_refreshAllItemsNeeded = true;
delete item;
}
void TreeWidget::deleteItems(const QList<QTreeWidgetItem*> &items)
{
m_refreshAllItemsNeeded = true;
qDeleteAll(items);
}