/* * Copyright (C) 2010 Felix Geyer * * 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 2 or (at your option) * version 3 of the License. * * 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 . */ #include "GroupView.h" #include #include #include "core/Database.h" #include "core/Group.h" #include "gui/group/GroupModel.h" GroupView::GroupView(Database* db, QWidget* parent) : QTreeView(parent) , m_model(new GroupModel(db, this)) { QTreeView::setModel(m_model); m_emitEditFinished = false; setHeaderHidden(true); setUniformRowHeights(true); recInitExpanded(db->rootGroup()); connect(this, SIGNAL(expanded(QModelIndex)), this, SLOT(expandedChanged(QModelIndex))); connect(this, SIGNAL(collapsed(QModelIndex)), this, SLOT(expandedChanged(QModelIndex))); connect(m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(syncExpandedState(QModelIndex,int,int))); connect(selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(emitGroupChanged())); setCurrentIndex(m_model->index(0, 0)); // invoke later so the EntryView is connected QMetaObject::invokeMethod(this, "emitGroupChanged", Qt::QueuedConnection, Q_ARG(QModelIndex, m_model->index(0, 0))); setDragEnabled(true); viewport()->setAcceptDrops(true); setDropIndicatorShown(true); m_emitEditFinished = true; } void GroupView::dragMoveEvent(QDragMoveEvent* event) { QTreeView::dragMoveEvent(event); // entries may only be dropped on groups if (event->isAccepted() && event->mimeData()->hasFormat("application/x-keepassx-entry") && (dropIndicatorPosition() == AboveItem || dropIndicatorPosition() == BelowItem)) { event->ignore(); } } Group* GroupView::currentGroup() { if (currentIndex() == QModelIndex()) { return 0; } else { return m_model->groupFromIndex(currentIndex()); } } void GroupView::expandedChanged(const QModelIndex& index) { Group* group = m_model->groupFromIndex(index); group->setExpanded(isExpanded(index)); emitEditFinished(); } void GroupView::recInitExpanded(Group* group) { expandGroup(group, group->isExpanded()); Q_FOREACH (Group* child, group->children()) { recInitExpanded(child); } } void GroupView::emitEditFinished() { if (m_emitEditFinished) { Q_EMIT editFinished(); } } void GroupView::expandGroup(Group* group, bool expand) { QModelIndex index = m_model->index(group); setExpanded(index, expand); } void GroupView::emitGroupChanged(const QModelIndex& index) { Q_EMIT groupChanged(m_model->groupFromIndex(index)); } void GroupView::setModel(QAbstractItemModel* model) { Q_UNUSED(model); Q_ASSERT(false); } void GroupView::emitGroupChanged() { Q_EMIT groupChanged(currentGroup()); } void GroupView::syncExpandedState(const QModelIndex& parent, int start, int end) { m_emitEditFinished = false; for (int row = start; row <= end; row++) { Group* group = m_model->groupFromIndex(m_model->index(row, 0, parent)); recInitExpanded(group); } m_emitEditFinished = true; emitEditFinished(); } void GroupView::setCurrentGroup(Group* group) { setCurrentIndex(m_model->index(group)); }