aboutsummaryrefslogtreecommitdiff
path: root/libs/uibase/src/sortabletreewidget.cpp
blob: 5cfbdd4e27db146c9c96fd4e692b184dbf39d27d (plain)
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
/*
Mod Organizer shared UI functionality

Copyright (C) 2013 Sebastian Herbord. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <uibase/sortabletreewidget.h>
#include <QDropEvent>

namespace MOBase
{

SortableTreeWidget::SortableTreeWidget(QWidget* parent)
    : QTreeWidget(parent), m_LocalMoveOnly(false)
{}

void SortableTreeWidget::setLocalMoveOnly(bool localOnly)
{
  m_LocalMoveOnly = localOnly;
}

void SortableTreeWidget::dropEvent(QDropEvent* event)
{
  // only react on internal drag&drop
  if (event->source() == this) {
    QTreeWidget::dropEvent(event);
  }
}

Qt::DropActions SortableTreeWidget::supportedDropActions() const
{
  // we do our own drop handling, this ensures dropMimeData is called.
  return Qt::CopyAction;
}

bool SortableTreeWidget::dropMimeData(QTreeWidgetItem* parent, int index,
                                      const QMimeData*, Qt::DropAction)
{
  // TODO: ignores type of action set up in designer, always moves
  return this->moveSelection(parent, index);
}

bool SortableTreeWidget::moveSelection(QTreeWidgetItem* parent, int idx)
{
  QModelIndex parentIndex = indexFromItem(parent);
  std::vector<QPersistentModelIndex> persistentIndices;
  Q_FOREACH (const QModelIndex& index, selectedIndexes()) {
    if (index == parentIndex)
      return false;
    if (m_LocalMoveOnly && (parentIndex != index.parent()))
      return false;
    if (index.column() != 0)
      continue;
    persistentIndices.push_back(index);
  }

  int targetRow = -1;
  if (itemsExpandable() || !parentIndex.isValid()) {
    targetRow = model()->index(idx, 0, parentIndex).row();

    if (targetRow < 0) {
      targetRow = idx;
    }
  } else {
    // if items aren't expandable, we can't be placing items on sublevels
    targetRow   = parentIndex.row();
    parentIndex = QModelIndex();
  }

  // remove items from the list, store in temporary location
  QList<QTreeWidgetItem*> temp;
  for (auto iter = persistentIndices.rbegin(); iter != persistentIndices.rend();
       ++iter) {
    QTreeWidgetItem* item = itemFromIndex(*iter);
    if (item == nullptr)
      continue;
    if ((item == nullptr) || (item->parent() == nullptr)) {
      temp.append(takeTopLevelItem(iter->row()));
    } else {
      temp.append(item->parent()->takeChild(iter->row()));
    }
  }

  if (idx == -1) {
    // append
    if (parentIndex.isValid()) {
      parent->addChildren(temp);
    } else {
      addTopLevelItems(temp);
    }
  } else {
    if (parentIndex.isValid()) {
      parent->insertChildren(std::min<int>(targetRow, parent->childCount()), temp);
    } else {
      insertTopLevelItems(std::min<int>(targetRow, topLevelItemCount()), temp);
    }
  }
  emit itemsMoved();
  return true;
}

}  // namespace MOBase