73 if (index < 0 || index >
children.size()) {
81 : QAbstractProxyModel(parent)
86 connect(
this, &QAbstractProxyModel::sourceModelChanged,
this, &TabTreeModel::init);
100 return createIndex(item->
parent->
children.indexOf(item), 0, item);
106 return it ? it->
tab :
nullptr;
112 if (!it || !it->
tab) {
113 return Qt::ItemIsDropEnabled;
115 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
117 flags |= Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled;
138 if (
parent.column() > 0) {
164 if (!hasIndex(row, column,
parent)) {
168 return createIndex(row, column, parentItem->
children.at(row));
189 if (action != Qt::MoveAction || column > 0 || !m_window) {
192 const auto *mimeData = qobject_cast<const TabModelMimeData*>(
data);
229 if (!it || !parentItem) {
232 if (it->
parent == parentItem && row < 0) {
235 if (!parentItem->
tab) {
240 const QModelIndex fromIdx =
index(it);
241 const int childPos = row > fromIdx.row() ? row - 1 : row;
242 if (!beginMoveRows(fromIdx.parent(), fromIdx.row(), fromIdx.row(), QModelIndex(), row)) {
243 qWarning() <<
"Invalid beginMoveRows" << fromIdx.parent() << fromIdx.row() <<
"root" << row;
255void TabTreeModel::init()
262 for (
int i = 0;
i < sourceModel()->rowCount(); ++
i) {
263 const QModelIndex
index = sourceModel()->index(
i, 0);
268 m_root->
addChild(createItems(item));
273 connectTab(item->
tab);
276 connect(sourceModel(), &QAbstractItemModel::dataChanged,
this, &TabTreeModel::sourceDataChanged, Qt::UniqueConnection);
277 connect(sourceModel(), &QAbstractItemModel::rowsInserted,
this, &TabTreeModel::sourceRowsInserted, Qt::UniqueConnection);
278 connect(sourceModel(), &QAbstractItemModel::rowsAboutToBeRemoved,
this, &TabTreeModel::sourceRowsAboutToBeRemoved, Qt::UniqueConnection);
279 connect(sourceModel(), &QAbstractItemModel::modelReset,
this, &TabTreeModel::sourceReset, Qt::UniqueConnection);
284 if (!item || item == m_root) {
287 return createIndex(item->
parent->
children.indexOf(item), 0, item);
293 return it ? it : m_root;
299 for (
WebTab *child : children) {
300 const QModelIndex
index = sourceModel()->index(child->tabIndex(), 0);
302 m_items[child] = item;
308void TabTreeModel::sourceDataChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomRight,
const QVector<int> &roles)
313void TabTreeModel::sourceRowsInserted(
const QModelIndex &parent,
int start,
int end)
315 for (
int i = start;
i <= end; ++
i) {
320void TabTreeModel::sourceRowsAboutToBeRemoved(
const QModelIndex &parent,
int start,
int end)
322 for (
int i = start;
i <= end; ++
i) {
327void TabTreeModel::sourceReset()
334void TabTreeModel::insertIndex(
const QModelIndex &sourceIndex)
346 const int idx =
parent->children.count();
355void TabTreeModel::removeIndex(
const QModelIndex &sourceIndex)
373 tab->disconnect(
this);
376void TabTreeModel::connectTab(
WebTab *tab)
389 pos = m_root->children.indexOf(item->parent);
391 const QModelIndex fromIdx =
index(item);
392 if (!beginMoveRows(fromIdx.parent(), fromIdx.row(), fromIdx.row(), QModelIndex(), pos)) {
393 qWarning() <<
"Invalid beginMoveRows" << fromIdx.parent() << fromIdx.row() <<
"root" << pos;
405 const QModelIndex fromIdx =
index(from);
406 const QModelIndex toIdx =
index(item);
407 const int childPos = fromIdx.parent() == toIdx && pos > fromIdx.row() ? pos - 1 : pos;
408 if (!beginMoveRows(fromIdx.parent(), fromIdx.row(), fromIdx.row(), toIdx, pos)) {
409 qWarning() <<
"Invalid beginMoveRows" << fromIdx.parent() << fromIdx.row() << toIdx << pos;
417void TabTreeModel::syncTopLevelTabs()
423 const auto items = m_root->
children;
TabWidget * tabWidget() const
int rowCount(const QModelIndex &parent) const override
int columnCount(const QModelIndex &parent) const override
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
WebTab * tab(const QModelIndex &index) const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
QVariant data(const QModelIndex &index, int role) const override
QModelIndex tabIndex(WebTab *tab) const
TabTreeModel(BrowserWindow *window, QObject *parent=nullptr)
QModelIndex parent(const QModelIndex &child) const override
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
bool hasChildren(const QModelIndex &parent) const override
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
TabTreeModelItem * parent
TabTreeModelItem(WebTab *tab=nullptr, const QModelIndex &index=QModelIndex())
void addChild(TabTreeModelItem *item, int index=-1)
QPersistentModelIndex sourceIndex
void setParent(TabTreeModelItem *item)
QVector< TabTreeModelItem * > children
void setParentTab(WebTab *tab)
void parentTabChanged(WebTab *tab)
QVector< WebTab * > childTabs() const
BrowserWindow * browserWindow() const
void addChildTab(WebTab *tab, int index=-1)
void childTabAdded(WebTab *tab, int index)
WebTab * parentTab() const