2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* 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"
|
2012-09-05 23:52:40 +02:00
|
|
|
#include "bookmarksmodel.h"
|
2011-03-03 15:24:59 +01:00
|
|
|
|
2012-12-20 14:45:35 +01:00
|
|
|
#include <QMimeData>
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QMouseEvent>
|
2012-09-05 23:52:40 +02:00
|
|
|
#include <QSqlDatabase>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QUrl>
|
|
|
|
|
2012-09-06 11:28:43 +02:00
|
|
|
const int ITEM_IS_TOPLEVEL = Qt::UserRole + 20;
|
|
|
|
const int ITEM_PARENT_TITLE = Qt::UserRole + 21;
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2011-12-12 21:14:43 +01:00
|
|
|
TreeWidget::TreeWidget(QWidget* parent)
|
|
|
|
: QTreeWidget(parent)
|
2011-11-06 17:01:23 +01:00
|
|
|
, m_refreshAllItemsNeeded(true)
|
|
|
|
, m_showMode(ItemsCollapsed)
|
2011-03-03 15:24:59 +01:00
|
|
|
{
|
2012-06-24 23:46:32 +02:00
|
|
|
setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
connect(this, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(sheduleRefresh()));
|
2011-07-30 17:57:14 +02:00
|
|
|
}
|
|
|
|
|
2011-10-09 12:11:17 +02:00
|
|
|
void TreeWidget::clear()
|
|
|
|
{
|
|
|
|
QTreeWidget::clear();
|
|
|
|
m_allTreeItems.clear();
|
|
|
|
}
|
|
|
|
|
2011-07-30 17:57:14 +02:00
|
|
|
void TreeWidget::sheduleRefresh()
|
|
|
|
{
|
|
|
|
m_refreshAllItemsNeeded = true;
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void TreeWidget::addTopLevelItem(QTreeWidgetItem* item)
|
2011-07-30 17:57:14 +02:00
|
|
|
{
|
|
|
|
m_allTreeItems.append(item);
|
|
|
|
QTreeWidget::addTopLevelItem(item);
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void TreeWidget::addTopLevelItems(const QList<QTreeWidgetItem*> &items)
|
2011-07-30 17:57:14 +02:00
|
|
|
{
|
|
|
|
m_allTreeItems.append(items);
|
|
|
|
QTreeWidget::addTopLevelItems(items);
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void TreeWidget::insertTopLevelItem(int index, QTreeWidgetItem* item)
|
2011-07-30 17:57:14 +02:00
|
|
|
{
|
|
|
|
m_allTreeItems.append(item);
|
|
|
|
QTreeWidget::insertTopLevelItem(index, item);
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void TreeWidget::insertTopLevelItems(int index, const QList<QTreeWidgetItem*> &items)
|
2011-07-30 17:57:14 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (event->modifiers() == Qt::ControlModifier) {
|
2011-03-03 15:24:59 +01:00
|
|
|
emit itemControlClicked(itemAt(event->pos()));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-03 15:24:59 +01:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (event->buttons() == Qt::MiddleButton) {
|
2011-10-18 21:07:58 +02:00
|
|
|
emit itemMiddleButtonClicked(itemAt(event->pos()));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-18 21:07:58 +02:00
|
|
|
|
2011-03-03 15:24:59 +01:00
|
|
|
QTreeWidget::mousePressEvent(event);
|
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2011-07-30 17:57:14 +02:00
|
|
|
void TreeWidget::iterateAllItems(QTreeWidgetItem* parent)
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
int count = parent ? parent->childCount() : topLevelItemCount();
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
QTreeWidgetItem* item = parent ? parent->child(i) : topLevelItem(i);
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (item->childCount() == 0) {
|
|
|
|
m_allTreeItems.append(item);
|
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
iterateAllItems(item);
|
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
|
2012-09-06 11:28:43 +02:00
|
|
|
void TreeWidget::setMimeType(const QString &mimeType)
|
|
|
|
{
|
|
|
|
m_mimeType = mimeType;
|
|
|
|
}
|
|
|
|
|
2012-09-05 23:52:40 +02:00
|
|
|
Qt::DropActions TreeWidget::supportedDropActions()
|
|
|
|
{
|
|
|
|
return Qt::CopyAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList TreeWidget::mimeTypes() const
|
|
|
|
{
|
|
|
|
QStringList types;
|
2012-09-06 11:28:43 +02:00
|
|
|
types << m_mimeType;
|
2012-09-05 23:52:40 +02:00
|
|
|
return types;
|
|
|
|
}
|
|
|
|
|
2012-09-06 11:28:43 +02:00
|
|
|
QMimeData* TreeWidget::mimeData(const QList<QTreeWidgetItem*> items) const
|
2012-09-05 23:52:40 +02:00
|
|
|
{
|
2012-09-06 11:28:43 +02:00
|
|
|
QMimeData* data = new QMimeData();
|
2012-09-05 23:52:40 +02:00
|
|
|
QByteArray encodedData;
|
|
|
|
|
|
|
|
QDataStream stream(&encodedData, QIODevice::WriteOnly);
|
2012-09-06 11:28:43 +02:00
|
|
|
foreach(const QTreeWidgetItem * item, items) {
|
2012-09-05 23:52:40 +02:00
|
|
|
if (item) {
|
|
|
|
QTreeWidgetItem* clonedItem = item->clone();
|
|
|
|
bool parentIsRoot = false;
|
|
|
|
if (!item->parent() || item->parent() == invisibleRootItem()) {
|
|
|
|
parentIsRoot = true;
|
|
|
|
}
|
2012-09-06 11:28:43 +02:00
|
|
|
clonedItem->setData(0, ITEM_IS_TOPLEVEL, parentIsRoot);
|
|
|
|
clonedItem->setData(0, ITEM_PARENT_TITLE, (parentIsRoot ? QString() : item->parent()->text(0))) ;
|
2012-09-05 23:52:40 +02:00
|
|
|
clonedItem->write(stream);
|
|
|
|
delete clonedItem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-06 11:28:43 +02:00
|
|
|
data->setData(m_mimeType, encodedData);
|
2012-09-05 23:52:40 +02:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2012-09-06 11:28:43 +02:00
|
|
|
bool TreeWidget::dropMimeData(QTreeWidgetItem* parent, int,
|
|
|
|
const QMimeData* data, Qt::DropAction action)
|
2012-09-05 23:52:40 +02:00
|
|
|
{
|
|
|
|
if (action == Qt::IgnoreAction) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent && !parent->text(1).isEmpty()) { // parent is a bookmark, go one level up!
|
|
|
|
parent = parent->parent();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parent) {
|
|
|
|
parent = invisibleRootItem();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ok = false;
|
|
|
|
if (data->hasUrls()) {
|
|
|
|
QString folder = (parent == invisibleRootItem()) ? QLatin1String("unsorted") : parent->text(0);
|
|
|
|
QUrl url = data->urls().at(0);
|
2012-09-06 11:28:43 +02:00
|
|
|
QString title = data->text().isEmpty() ? url.host() + url.path() : data->text();
|
2012-09-05 23:52:40 +02:00
|
|
|
emit linkWasDroped(url, title, data->imageData(), folder, &ok);
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2012-09-06 11:28:43 +02:00
|
|
|
if (!data->hasFormat(m_mimeType)) {
|
2012-09-05 23:52:40 +02:00
|
|
|
return false;
|
2012-09-06 11:28:43 +02:00
|
|
|
}
|
2012-09-05 23:52:40 +02:00
|
|
|
|
|
|
|
setUpdatesEnabled(false);
|
|
|
|
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
|
|
|
|
QSqlDatabase db = QSqlDatabase::database();
|
|
|
|
db.transaction();
|
|
|
|
|
2012-09-06 11:28:43 +02:00
|
|
|
QByteArray ba = data->data(m_mimeType);
|
2012-09-05 23:52:40 +02:00
|
|
|
QDataStream stream(&ba, QIODevice::ReadOnly);
|
2012-09-06 11:28:43 +02:00
|
|
|
if (stream.atEnd()) {
|
2012-09-05 23:52:40 +02:00
|
|
|
return false;
|
2012-09-06 11:28:43 +02:00
|
|
|
}
|
2012-09-05 23:52:40 +02:00
|
|
|
|
|
|
|
while (!stream.atEnd()) {
|
2012-09-06 11:28:43 +02:00
|
|
|
|
|
|
|
QTreeWidgetItem* item = new QTreeWidgetItem;
|
2012-09-05 23:52:40 +02:00
|
|
|
item->read(stream);
|
|
|
|
bool parentIsRoot = item->data(0, ITEM_IS_TOPLEVEL).toBool();
|
|
|
|
QString oldParentTitle = item->data(0, ITEM_PARENT_TITLE).toString();
|
|
|
|
|
|
|
|
bool isFolder = (item && item->text(1).isEmpty());
|
|
|
|
if (isFolder && (item->text(0) == _bookmarksMenu ||
|
2012-09-06 11:28:43 +02:00
|
|
|
item->text(0) == _bookmarksToolbar)) {
|
2012-09-05 23:52:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parentIsOldParent = parentIsRoot ? (parent == invisibleRootItem()) : (oldParentTitle == parent->text(0));
|
2012-09-06 11:28:43 +02:00
|
|
|
if (parentIsOldParent || (isFolder && parent != invisibleRootItem() &&
|
|
|
|
parent->text(0) != _bookmarksToolbar)) {
|
2012-09-05 23:52:40 +02:00
|
|
|
// just 'Bookmarks In ToolBar' folder can have subfolders
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isFolder) {
|
|
|
|
if (parent->text(0) == _bookmarksToolbar) {
|
|
|
|
emit folderParentChanged(item->text(0), true, &ok);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
emit folderParentChanged(item->text(0), false, &ok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
emit bookmarkParentChanged(item->data(0, Qt::UserRole + 10).toInt(),
|
|
|
|
parent->text(0), oldParentTitle, &ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
db.commit();
|
|
|
|
clearSelection();
|
|
|
|
setUpdatesEnabled(true);
|
|
|
|
QApplication::restoreOverrideCursor();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-06 11:28:43 +02:00
|
|
|
void TreeWidget::dragEnterEvent(QDragEnterEvent* event)
|
2012-09-05 23:52:40 +02:00
|
|
|
{
|
2012-09-06 11:28:43 +02:00
|
|
|
const QMimeData* mimeData = event->mimeData();
|
2012-09-05 23:52:40 +02:00
|
|
|
QTreeWidget::dragEnterEvent(event);
|
2012-09-06 11:28:43 +02:00
|
|
|
if (mimeData->hasUrls() || mimeData->hasFormat(m_mimeType)) {
|
2012-09-05 23:52:40 +02:00
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
event->ignore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-06 11:28:43 +02:00
|
|
|
void TreeWidget::dragMoveEvent(QDragMoveEvent* event)
|
2012-09-05 23:52:40 +02:00
|
|
|
{
|
2012-09-06 11:28:43 +02:00
|
|
|
const QMimeData* mimeData = event->mimeData();
|
2012-09-05 23:52:40 +02:00
|
|
|
bool accept = false;
|
|
|
|
if (mimeData->hasUrls()) {
|
|
|
|
accept = true;
|
|
|
|
}
|
2012-09-06 11:28:43 +02:00
|
|
|
else if (mimeData->hasFormat(m_mimeType)) {
|
|
|
|
QTreeWidgetItem* itemUnderMouse = itemAt(event->pos());
|
|
|
|
if (!itemUnderMouse) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-05 23:52:40 +02:00
|
|
|
bool underMouseIsFolder = (itemUnderMouse && itemUnderMouse->text(1).isEmpty());
|
|
|
|
int top = visualItemRect(itemUnderMouse).top();
|
|
|
|
int bottom = visualItemRect(itemUnderMouse).bottom();
|
|
|
|
int y = event->pos().y();
|
2012-09-06 11:28:43 +02:00
|
|
|
bool overEdgeOfItem = (y >= top - 1 && y <= top + 1) || (y <= bottom + 1 && y >= bottom - 1);
|
2012-09-05 23:52:40 +02:00
|
|
|
|
2012-09-06 11:28:43 +02:00
|
|
|
QByteArray ba = mimeData->data(m_mimeType);
|
2012-09-05 23:52:40 +02:00
|
|
|
QDataStream stream(&ba, QIODevice::ReadOnly);
|
|
|
|
|
|
|
|
while (!stream.atEnd()) {
|
2012-09-06 11:28:43 +02:00
|
|
|
QTreeWidgetItem* dragItem = new QTreeWidgetItem;
|
2012-09-05 23:52:40 +02:00
|
|
|
dragItem->read(stream);
|
|
|
|
bool parentIsRoot = dragItem->data(0, ITEM_IS_TOPLEVEL).toBool();
|
|
|
|
QString oldParentTitle = dragItem->data(0, ITEM_PARENT_TITLE).toString();
|
2012-09-06 11:28:43 +02:00
|
|
|
|
2012-09-05 23:52:40 +02:00
|
|
|
bool itemIsFolder = dragItem->text(1).isEmpty();
|
|
|
|
if (dragItem->text(0) != _bookmarksMenu
|
|
|
|
&& dragItem->text(0) != _bookmarksToolbar) {
|
|
|
|
if (!itemUnderMouse->parent() && !parentIsRoot && overEdgeOfItem) {
|
|
|
|
accept = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bool parentsAreDifferent = parentIsRoot
|
2012-09-06 11:28:43 +02:00
|
|
|
? itemUnderMouse->parent() != 0
|
|
|
|
: (!itemUnderMouse->parent() || itemUnderMouse->parent()->text(0) != oldParentTitle);
|
2012-09-05 23:52:40 +02:00
|
|
|
bool canHasSubFolder = !itemUnderMouse->parent()
|
2012-09-06 11:28:43 +02:00
|
|
|
|| itemUnderMouse->parent() == invisibleRootItem()
|
|
|
|
|| itemUnderMouse->parent()->text(0) == _bookmarksToolbar;
|
2012-09-05 23:52:40 +02:00
|
|
|
|
|
|
|
if (!underMouseIsFolder && parentsAreDifferent) {
|
|
|
|
if (!itemIsFolder) {
|
|
|
|
accept = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!itemUnderMouse->parent()
|
2012-09-06 11:28:43 +02:00
|
|
|
|| (dragItem->text(0) != itemUnderMouse->parent()->text(0)
|
|
|
|
&& canHasSubFolder)) {
|
2012-09-05 23:52:40 +02:00
|
|
|
accept = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (underMouseIsFolder) {
|
|
|
|
if (itemIsFolder && itemUnderMouse->text(0) == _bookmarksToolbar
|
|
|
|
&& (parentIsRoot || oldParentTitle != _bookmarksToolbar)) {
|
|
|
|
accept = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!itemIsFolder && oldParentTitle != itemUnderMouse->text(0)) {
|
|
|
|
accept = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QTreeWidget::dragMoveEvent(event);
|
|
|
|
if (accept) {
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
event->ignore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-30 17:57:14 +02:00
|
|
|
QList<QTreeWidgetItem*> TreeWidget::allItems()
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2011-07-30 17:57:14 +02:00
|
|
|
if (m_refreshAllItemsNeeded) {
|
|
|
|
m_allTreeItems.clear();
|
|
|
|
iterateAllItems(0);
|
|
|
|
m_refreshAllItemsNeeded = false;
|
|
|
|
}
|
|
|
|
return m_allTreeItems;
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
2011-12-28 16:31:18 +01:00
|
|
|
|
2011-07-30 17:57:14 +02:00
|
|
|
void TreeWidget::filterString(QString string)
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2011-07-30 17:57:14 +02:00
|
|
|
expandAll();
|
|
|
|
QList<QTreeWidgetItem*> _allItems = allItems();
|
2011-03-27 21:59:40 +02:00
|
|
|
|
|
|
|
if (string.isEmpty()) {
|
2011-11-06 17:01:23 +01:00
|
|
|
foreach(QTreeWidgetItem * item, _allItems)
|
|
|
|
item->setHidden(false);
|
|
|
|
for (int i = 0; i < topLevelItemCount(); i++) {
|
2011-07-30 17:57:14 +02:00
|
|
|
topLevelItem(i)->setHidden(false);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
if (m_showMode == ItemsCollapsed) {
|
2011-07-30 17:57:14 +02:00
|
|
|
collapseAll();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach(QTreeWidgetItem * item, _allItems) {
|
2011-04-15 20:45:22 +02:00
|
|
|
item->setHidden(!item->text(0).contains(string, Qt::CaseInsensitive));
|
2011-07-30 17:57:14 +02:00
|
|
|
item->setExpanded(true);
|
|
|
|
}
|
2011-11-06 17:01:23 +01:00
|
|
|
for (int i = 0; i < topLevelItemCount(); i++) {
|
2011-07-30 17:57:14 +02:00
|
|
|
topLevelItem(i)->setHidden(false);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-07-30 17:57:14 +02:00
|
|
|
|
|
|
|
QTreeWidgetItem* firstItem = topLevelItem(0);
|
|
|
|
QTreeWidgetItem* belowItem = itemBelow(firstItem);
|
2011-12-28 16:31:18 +01:00
|
|
|
|
|
|
|
int topLvlIndex = 0;
|
2011-07-30 17:57:14 +02:00
|
|
|
while (firstItem) {
|
2011-12-28 16:31:18 +01:00
|
|
|
if (firstItem->text(0).contains(string, Qt::CaseInsensitive)) {
|
|
|
|
firstItem->setHidden(false);
|
|
|
|
}
|
|
|
|
else if (!firstItem->parent() && !belowItem) {
|
2011-07-30 17:57:14 +02:00
|
|
|
firstItem->setHidden(true);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else if (!belowItem) {
|
2011-07-30 17:57:14 +02:00
|
|
|
break;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else if (!firstItem->parent() && !belowItem->parent()) {
|
2011-07-30 17:57:14 +02:00
|
|
|
firstItem->setHidden(true);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-12-28 16:31:18 +01:00
|
|
|
|
|
|
|
topLvlIndex++;
|
|
|
|
firstItem = topLevelItem(topLvlIndex);
|
2011-07-30 17:57:14 +02:00
|
|
|
belowItem = itemBelow(firstItem);
|
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-30 17:57:14 +02:00
|
|
|
bool TreeWidget::appendToParentItem(const QString &parentText, QTreeWidgetItem* item)
|
2011-03-27 21:59:40 +02:00
|
|
|
{
|
2011-07-30 17:57:14 +02:00
|
|
|
QList<QTreeWidgetItem*> list = findItems(parentText, Qt::MatchExactly);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (list.count() == 0) {
|
2011-07-30 17:57:14 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-07-30 17:57:14 +02:00
|
|
|
QTreeWidgetItem* parentItem = list.at(0);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!parentItem) {
|
2011-07-30 17:57:14 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-27 21:59:40 +02:00
|
|
|
|
2011-07-30 17:57:14 +02:00
|
|
|
m_allTreeItems.append(item);
|
|
|
|
parentItem->addChild(item);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TreeWidget::appendToParentItem(QTreeWidgetItem* parent, QTreeWidgetItem* item)
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!parent || parent->treeWidget() != this) {
|
2011-07-30 17:57:14 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-07-30 17:57:14 +02:00
|
|
|
|
|
|
|
m_allTreeItems.append(item);
|
|
|
|
parent->addChild(item);
|
|
|
|
return true;
|
2011-03-27 21:59:40 +02:00
|
|
|
}
|
2011-04-15 20:45:22 +02:00
|
|
|
|
2011-07-30 17:57:14 +02:00
|
|
|
bool TreeWidget::prependToParentItem(const QString &parentText, QTreeWidgetItem* item)
|
2011-04-15 20:45:22 +02:00
|
|
|
{
|
2011-07-30 17:57:14 +02:00
|
|
|
QList<QTreeWidgetItem*> list = findItems(parentText, Qt::MatchExactly);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (list.count() == 0) {
|
2011-04-17 13:03:29 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-17 13:03:29 +02:00
|
|
|
QTreeWidgetItem* parentItem = list.at(0);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!parentItem) {
|
2011-04-15 20:45:22 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-15 20:45:22 +02:00
|
|
|
|
2011-07-30 17:57:14 +02:00
|
|
|
m_allTreeItems.append(item);
|
|
|
|
parentItem->insertChild(0, item);
|
2011-04-15 20:45:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
2011-07-30 17:57:14 +02:00
|
|
|
|
|
|
|
bool TreeWidget::prependToParentItem(QTreeWidgetItem* parent, QTreeWidgetItem* item)
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!parent || parent->treeWidget() != this) {
|
2011-07-30 17:57:14 +02:00
|
|
|
return false;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-07-30 17:57:14 +02:00
|
|
|
|
|
|
|
m_allTreeItems.append(item);
|
|
|
|
parent->insertChild(0, item);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void TreeWidget::deleteItem(QTreeWidgetItem* item)
|
2011-07-30 17:57:14 +02:00
|
|
|
{
|
2011-12-10 00:13:37 +01:00
|
|
|
// if (m_allTreeItems.contains(item)) {
|
|
|
|
// m_allTreeItems.removeOne(item);
|
|
|
|
// }
|
|
|
|
|
|
|
|
m_refreshAllItemsNeeded = true;
|
2011-07-30 17:57:14 +02:00
|
|
|
|
|
|
|
delete item;
|
|
|
|
}
|
2012-03-10 12:21:45 +01:00
|
|
|
|
2012-03-10 13:57:50 +01:00
|
|
|
void TreeWidget::deleteItems(const QList<QTreeWidgetItem*> &items)
|
2012-03-10 12:21:45 +01:00
|
|
|
{
|
|
|
|
m_refreshAllItemsNeeded = true;
|
|
|
|
|
|
|
|
qDeleteAll(items);
|
|
|
|
}
|
2012-09-05 23:52:40 +02:00
|
|
|
|
|
|
|
void TreeWidget::setDragDropReceiver(bool enable, QObject* receiver)
|
|
|
|
{
|
|
|
|
if (!receiver) {
|
|
|
|
enable = false;
|
|
|
|
}
|
|
|
|
setDragEnabled(enable);
|
|
|
|
viewport()->setAcceptDrops(enable);
|
|
|
|
setDropIndicatorShown(enable);
|
|
|
|
if (enable) {
|
2012-12-20 14:45:35 +01:00
|
|
|
// TODO: It won't probably work in Qt5
|
|
|
|
#if QT_VERSION < 0x050000
|
2012-09-05 23:52:40 +02:00
|
|
|
model()->setSupportedDragActions(Qt::CopyAction);
|
2012-12-20 14:45:35 +01:00
|
|
|
#endif
|
2012-09-06 11:28:43 +02:00
|
|
|
connect(this, SIGNAL(folderParentChanged(QString, bool, bool*)), receiver, SLOT(changeFolderParent(QString, bool, bool*)));
|
|
|
|
connect(this, SIGNAL(bookmarkParentChanged(int, QString, QString, bool*)), receiver, SLOT(changeBookmarkParent(int, QString, QString, bool*)));
|
|
|
|
connect(this, SIGNAL(linkWasDroped(QUrl, QString, QVariant, QString, bool*)), receiver, SLOT(bookmarkDropedLink(QUrl, QString, QVariant, QString, bool*)));
|
2012-09-05 23:52:40 +02:00
|
|
|
}
|
|
|
|
else {
|
2012-09-06 11:28:43 +02:00
|
|
|
disconnect(this, SIGNAL(folderParentChanged(QString, bool, bool*)), receiver, SLOT(changeFolderParent(QString, bool, bool*)));
|
|
|
|
disconnect(this, SIGNAL(bookmarkParentChanged(int, QString, QString, bool*)), receiver, SLOT(changeBookmarkParent(int, QString, QString, bool*)));
|
|
|
|
disconnect(this, SIGNAL(linkWasDroped(QUrl, QString, QVariant, QString, bool*)), receiver, SLOT(bookmarkDropedLink(QUrl, QString, QVariant, QString, bool*)));
|
2012-09-05 23:52:40 +02:00
|
|
|
}
|
|
|
|
}
|