2010-08-22 14:02:44 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
|
|
|
|
|
*
|
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "GroupView.h"
|
|
|
|
|
|
2013-10-03 13:18:16 +00:00
|
|
|
#include <QDragMoveEvent>
|
|
|
|
|
#include <QMetaObject>
|
2015-07-22 15:03:59 +00:00
|
|
|
#include <QMimeData>
|
2010-10-06 17:40:50 +00:00
|
|
|
|
2010-08-22 14:02:44 +00:00
|
|
|
#include "core/Database.h"
|
|
|
|
|
#include "core/Group.h"
|
2012-05-16 08:16:32 +00:00
|
|
|
#include "gui/group/GroupModel.h"
|
2010-08-22 14:02:44 +00:00
|
|
|
|
2010-08-24 21:12:01 +00:00
|
|
|
GroupView::GroupView(Database* db, QWidget* parent)
|
|
|
|
|
: QTreeView(parent)
|
2012-04-23 17:44:43 +00:00
|
|
|
, m_model(new GroupModel(db, this))
|
2012-06-28 07:21:15 +00:00
|
|
|
, m_updatingExpanded(false)
|
2010-08-22 14:02:44 +00:00
|
|
|
{
|
2010-08-23 18:57:38 +00:00
|
|
|
QTreeView::setModel(m_model);
|
2010-08-22 14:02:44 +00:00
|
|
|
setHeaderHidden(true);
|
2010-10-06 17:40:50 +00:00
|
|
|
setUniformRowHeights(true);
|
2010-08-24 20:26:52 +00:00
|
|
|
|
2012-04-27 16:42:24 +00:00
|
|
|
connect(this, SIGNAL(expanded(QModelIndex)), this, SLOT(expandedChanged(QModelIndex)));
|
2012-04-28 20:50:30 +00:00
|
|
|
connect(this, SIGNAL(collapsed(QModelIndex)), this, SLOT(expandedChanged(QModelIndex)));
|
2012-05-02 13:36:27 +00:00
|
|
|
connect(m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(syncExpandedState(QModelIndex,int,int)));
|
2012-06-28 07:21:15 +00:00
|
|
|
connect(m_model, SIGNAL(modelReset()), SLOT(modelReset()));
|
2010-10-06 17:40:50 +00:00
|
|
|
|
2012-05-14 14:50:28 +00:00
|
|
|
connect(selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(emitGroupChanged()));
|
|
|
|
|
|
2017-08-17 19:02:21 +00:00
|
|
|
connect(this, SIGNAL(clicked(QModelIndex)), SLOT(emitGroupPressed(QModelIndex)));
|
|
|
|
|
|
2012-06-28 07:21:15 +00:00
|
|
|
modelReset();
|
2012-04-24 22:22:55 +00:00
|
|
|
|
|
|
|
|
setDragEnabled(true);
|
|
|
|
|
viewport()->setAcceptDrops(true);
|
|
|
|
|
setDropIndicatorShown(true);
|
2013-04-07 10:36:53 +00:00
|
|
|
setDefaultDropAction(Qt::MoveAction);
|
2010-08-22 14:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-28 07:21:15 +00:00
|
|
|
void GroupView::changeDatabase(Database* newDb)
|
|
|
|
|
{
|
|
|
|
|
m_model->changeDatabase(newDb);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-26 14:35:13 +00:00
|
|
|
void GroupView::dragMoveEvent(QDragMoveEvent* event)
|
|
|
|
|
{
|
2013-04-07 10:36:53 +00:00
|
|
|
if (event->keyboardModifiers() & Qt::ControlModifier) {
|
|
|
|
|
event->setDropAction(Qt::CopyAction);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
event->setDropAction(Qt::MoveAction);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-26 14:35:13 +00:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-27 14:49:06 +00:00
|
|
|
Group* GroupView::currentGroup()
|
|
|
|
|
{
|
2012-05-12 11:22:41 +00:00
|
|
|
if (currentIndex() == QModelIndex()) {
|
2015-07-24 16:28:12 +00:00
|
|
|
return nullptr;
|
2012-05-12 11:22:41 +00:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return m_model->groupFromIndex(currentIndex());
|
|
|
|
|
}
|
2011-12-27 14:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
2010-08-22 14:02:44 +00:00
|
|
|
void GroupView::expandedChanged(const QModelIndex& index)
|
|
|
|
|
{
|
2012-06-28 07:21:15 +00:00
|
|
|
if (m_updatingExpanded) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-23 19:30:20 +00:00
|
|
|
Group* group = m_model->groupFromIndex(index);
|
2010-08-22 14:02:44 +00:00
|
|
|
group->setExpanded(isExpanded(index));
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-23 19:30:20 +00:00
|
|
|
void GroupView::recInitExpanded(Group* group)
|
2010-08-22 14:02:44 +00:00
|
|
|
{
|
2012-06-28 07:21:15 +00:00
|
|
|
m_updatingExpanded = true;
|
2012-04-27 16:45:26 +00:00
|
|
|
expandGroup(group, group->isExpanded());
|
2012-06-28 07:21:15 +00:00
|
|
|
m_updatingExpanded = false;
|
2010-08-22 14:02:44 +00:00
|
|
|
|
2016-09-02 17:51:51 +00:00
|
|
|
const QList<Group*> children = group->children();
|
|
|
|
|
for (Group* child : children) {
|
2010-08-22 14:02:44 +00:00
|
|
|
recInitExpanded(child);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-27 16:45:26 +00:00
|
|
|
void GroupView::expandGroup(Group* group, bool expand)
|
|
|
|
|
{
|
|
|
|
|
QModelIndex index = m_model->index(group);
|
|
|
|
|
setExpanded(index, expand);
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-24 20:26:52 +00:00
|
|
|
void GroupView::emitGroupChanged(const QModelIndex& index)
|
|
|
|
|
{
|
2017-03-10 14:58:42 +00:00
|
|
|
emit groupChanged(m_model->groupFromIndex(index));
|
2010-08-24 20:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
2010-08-22 14:02:44 +00:00
|
|
|
void GroupView::setModel(QAbstractItemModel* model)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(model);
|
|
|
|
|
Q_ASSERT(false);
|
|
|
|
|
}
|
2012-04-21 10:15:21 +00:00
|
|
|
|
|
|
|
|
void GroupView::emitGroupChanged()
|
|
|
|
|
{
|
2017-03-10 14:58:42 +00:00
|
|
|
emit groupChanged(currentGroup());
|
2012-04-21 10:15:21 +00:00
|
|
|
}
|
2012-04-27 16:45:26 +00:00
|
|
|
|
2017-08-17 19:02:21 +00:00
|
|
|
void GroupView::emitGroupPressed(const QModelIndex& index)
|
|
|
|
|
{
|
|
|
|
|
emit groupPressed(m_model->groupFromIndex(index));
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-02 13:36:27 +00:00
|
|
|
void GroupView::syncExpandedState(const QModelIndex& parent, int start, int end)
|
|
|
|
|
{
|
|
|
|
|
for (int row = start; row <= end; row++) {
|
|
|
|
|
Group* group = m_model->groupFromIndex(m_model->index(row, 0, parent));
|
|
|
|
|
recInitExpanded(group);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-27 16:45:26 +00:00
|
|
|
void GroupView::setCurrentGroup(Group* group)
|
|
|
|
|
{
|
2016-11-03 01:01:02 +00:00
|
|
|
if (group == nullptr)
|
|
|
|
|
setCurrentIndex(QModelIndex());
|
|
|
|
|
else
|
|
|
|
|
setCurrentIndex(m_model->index(group));
|
2012-04-27 16:45:26 +00:00
|
|
|
}
|
2012-06-28 07:21:15 +00:00
|
|
|
|
|
|
|
|
void GroupView::modelReset()
|
|
|
|
|
{
|
|
|
|
|
recInitExpanded(m_model->groupFromIndex(m_model->index(0, 0)));
|
|
|
|
|
setCurrentIndex(m_model->index(0, 0));
|
|
|
|
|
}
|