2010-08-24 22:26:52 +02: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 "EntryView.h"
|
|
|
|
|
2014-05-17 19:01:43 +02:00
|
|
|
#include <QHeaderView>
|
2013-10-03 15:18:16 +02:00
|
|
|
#include <QKeyEvent>
|
2013-09-29 17:33:45 +02:00
|
|
|
|
2012-07-22 21:56:31 +02:00
|
|
|
#include "gui/SortFilterHideProxyModel.h"
|
2010-08-24 22:26:52 +02:00
|
|
|
|
|
|
|
EntryView::EntryView(QWidget* parent)
|
|
|
|
: QTreeView(parent)
|
2012-04-23 19:44:43 +02:00
|
|
|
, m_model(new EntryModel(this))
|
2012-07-22 21:56:31 +02:00
|
|
|
, m_sortModel(new SortFilterHideProxyModel(this))
|
2012-07-21 18:35:24 +02:00
|
|
|
, m_inEntryListMode(false)
|
2010-08-24 22:26:52 +02:00
|
|
|
{
|
2012-05-11 12:01:01 +02:00
|
|
|
m_sortModel->setSourceModel(m_model);
|
|
|
|
m_sortModel->setDynamicSortFilter(true);
|
|
|
|
m_sortModel->setSortLocaleAware(true);
|
|
|
|
m_sortModel->setSortCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
QTreeView::setModel(m_sortModel);
|
2010-08-24 23:06:35 +02:00
|
|
|
|
|
|
|
setUniformRowHeights(true);
|
|
|
|
setRootIsDecorated(false);
|
2012-05-15 20:04:20 +02:00
|
|
|
setAlternatingRowColors(true);
|
2012-04-26 16:35:13 +02:00
|
|
|
setDragEnabled(true);
|
2012-05-11 12:01:01 +02:00
|
|
|
setSortingEnabled(true);
|
2012-05-25 13:25:46 +02:00
|
|
|
setSelectionMode(QAbstractItemView::ExtendedSelection);
|
2014-05-17 19:01:43 +02:00
|
|
|
header()->setDefaultSectionSize(150);
|
2010-10-06 19:40:50 +02:00
|
|
|
|
2013-04-07 12:36:53 +02:00
|
|
|
// QAbstractItemView::startDrag() uses this property as the default drag action
|
|
|
|
setDefaultDropAction(Qt::MoveAction);
|
|
|
|
|
2013-09-29 17:33:45 +02:00
|
|
|
connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(emitEntryActivated(QModelIndex)));
|
2011-12-29 19:01:58 +01:00
|
|
|
connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SIGNAL(entrySelectionChanged()));
|
2012-07-21 18:35:24 +02:00
|
|
|
connect(m_model, SIGNAL(switchedToEntryListMode()), SLOT(switchToEntryListMode()));
|
|
|
|
connect(m_model, SIGNAL(switchedToGroupMode()), SLOT(switchToGroupMode()));
|
2010-08-24 22:26:52 +02:00
|
|
|
}
|
|
|
|
|
2013-09-29 17:33:45 +02:00
|
|
|
void EntryView::keyPressEvent(QKeyEvent* event)
|
|
|
|
{
|
|
|
|
if ((event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) && currentIndex().isValid()) {
|
2013-10-10 22:47:32 +02:00
|
|
|
emitEntryActivated(currentIndex());
|
2016-11-08 22:13:57 +01:00
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
// Pressing return does not emit the QTreeView::activated signal on mac os
|
2017-03-10 15:58:42 +01:00
|
|
|
emit activated(currentIndex());
|
2016-11-08 22:13:57 +01:00
|
|
|
#endif
|
2013-09-29 17:33:45 +02:00
|
|
|
}
|
2013-10-10 22:47:32 +02:00
|
|
|
|
|
|
|
QTreeView::keyPressEvent(event);
|
2013-09-29 17:33:45 +02:00
|
|
|
}
|
|
|
|
|
2010-08-24 22:26:52 +02:00
|
|
|
void EntryView::setGroup(Group* group)
|
|
|
|
{
|
|
|
|
m_model->setGroup(group);
|
2014-05-16 19:10:30 +02:00
|
|
|
setFirstEntryActive();
|
2010-08-24 22:26:52 +02:00
|
|
|
}
|
|
|
|
|
2012-07-21 18:35:24 +02:00
|
|
|
void EntryView::setEntryList(const QList<Entry*>& entries)
|
2012-05-12 13:22:41 +02:00
|
|
|
{
|
2012-07-21 18:35:24 +02:00
|
|
|
m_model->setEntryList(entries);
|
2014-05-16 19:10:30 +02:00
|
|
|
setFirstEntryActive();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntryView::setFirstEntryActive()
|
|
|
|
{
|
2015-10-15 18:02:31 +02:00
|
|
|
if (m_model->rowCount() > 0) {
|
2014-05-16 19:10:30 +02:00
|
|
|
QModelIndex index = m_sortModel->mapToSource(m_sortModel->index(0, 0));
|
|
|
|
setCurrentEntry(m_model->entryFromIndex(index));
|
|
|
|
}
|
|
|
|
else {
|
2017-03-10 15:58:42 +01:00
|
|
|
emit entrySelectionChanged();
|
2014-05-16 19:10:30 +02:00
|
|
|
}
|
2012-05-12 13:22:41 +02:00
|
|
|
}
|
|
|
|
|
2012-07-21 18:35:24 +02:00
|
|
|
bool EntryView::inEntryListMode()
|
2012-05-13 18:08:22 +02:00
|
|
|
{
|
2012-07-21 18:35:24 +02:00
|
|
|
return m_inEntryListMode;
|
2012-05-13 18:08:22 +02:00
|
|
|
}
|
|
|
|
|
2013-04-14 19:14:06 +02:00
|
|
|
void EntryView::emitEntryActivated(const QModelIndex& index)
|
2010-09-19 19:45:14 +02:00
|
|
|
{
|
2013-04-07 21:05:52 +02:00
|
|
|
Entry* entry = entryFromIndex(index);
|
|
|
|
|
2017-03-10 15:58:42 +01:00
|
|
|
emit entryActivated(entry, static_cast<EntryModel::ModelColumn>(m_sortModel->mapToSource(index).column()));
|
2010-09-19 19:45:14 +02:00
|
|
|
}
|
|
|
|
|
2010-08-24 22:26:52 +02:00
|
|
|
void EntryView::setModel(QAbstractItemModel* model)
|
|
|
|
{
|
|
|
|
Q_UNUSED(model);
|
|
|
|
Q_ASSERT(false);
|
|
|
|
}
|
2011-12-29 19:01:58 +01:00
|
|
|
|
|
|
|
Entry* EntryView::currentEntry()
|
|
|
|
{
|
2012-05-25 13:25:46 +02:00
|
|
|
QModelIndexList list = selectionModel()->selectedRows();
|
|
|
|
if (list.size() == 1) {
|
|
|
|
return m_model->entryFromIndex(m_sortModel->mapToSource(list.first()));
|
|
|
|
}
|
|
|
|
else {
|
2015-07-24 18:28:12 +02:00
|
|
|
return nullptr;
|
2012-05-25 13:25:46 +02:00
|
|
|
}
|
2011-12-29 19:01:58 +01:00
|
|
|
}
|
|
|
|
|
2014-05-15 18:05:58 +02:00
|
|
|
int EntryView::numberOfSelectedEntries()
|
|
|
|
{
|
|
|
|
return selectionModel()->selectedRows().size();
|
|
|
|
}
|
|
|
|
|
2012-05-02 19:33:37 +02:00
|
|
|
void EntryView::setCurrentEntry(Entry* entry)
|
|
|
|
{
|
2012-07-23 22:27:02 +02:00
|
|
|
selectionModel()->setCurrentIndex(m_sortModel->mapFromSource(m_model->indexFromEntry(entry)),
|
|
|
|
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
|
2012-05-11 12:01:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Entry* EntryView::entryFromIndex(const QModelIndex& index)
|
|
|
|
{
|
|
|
|
if (index.isValid()) {
|
|
|
|
return m_model->entryFromIndex(m_sortModel->mapToSource(index));
|
|
|
|
}
|
|
|
|
else {
|
2015-07-24 18:28:12 +02:00
|
|
|
return nullptr;
|
2012-05-11 12:01:01 +02:00
|
|
|
}
|
2012-05-02 19:33:37 +02:00
|
|
|
}
|
2012-05-12 13:22:41 +02:00
|
|
|
|
2012-07-21 18:35:24 +02:00
|
|
|
void EntryView::switchToEntryListMode()
|
2012-05-12 13:22:41 +02:00
|
|
|
{
|
2012-07-22 21:56:31 +02:00
|
|
|
m_sortModel->hideColumn(0, false);
|
2017-02-17 14:18:18 +01:00
|
|
|
|
|
|
|
m_sortModel->sort(1, Qt::AscendingOrder);
|
|
|
|
m_sortModel->sort(0, Qt::AscendingOrder);
|
2012-05-16 10:52:59 +02:00
|
|
|
sortByColumn(0, Qt::AscendingOrder);
|
2017-02-17 14:18:18 +01:00
|
|
|
|
2012-07-21 18:35:24 +02:00
|
|
|
m_inEntryListMode = true;
|
2012-05-12 13:22:41 +02:00
|
|
|
}
|
|
|
|
|
2012-07-21 18:35:24 +02:00
|
|
|
void EntryView::switchToGroupMode()
|
2012-05-12 13:22:41 +02:00
|
|
|
{
|
2012-07-22 21:56:31 +02:00
|
|
|
m_sortModel->hideColumn(0, true);
|
2017-02-17 14:18:18 +01:00
|
|
|
|
|
|
|
m_sortModel->sort(-1, Qt::AscendingOrder);
|
|
|
|
m_sortModel->sort(0, Qt::AscendingOrder);
|
2012-07-22 21:56:31 +02:00
|
|
|
sortByColumn(0, Qt::AscendingOrder);
|
2017-02-17 14:18:18 +01:00
|
|
|
|
2012-07-21 18:35:24 +02:00
|
|
|
m_inEntryListMode = false;
|
2012-05-12 13:22:41 +02:00
|
|
|
}
|