rssguard/src/gui/messagesview.cpp

272 lines
10 KiB
C++
Raw Normal View History

2013-11-17 16:41:44 +01:00
#include <QHeaderView>
2013-11-18 21:45:15 +01:00
#include <QKeyEvent>
2013-12-04 21:03:09 +01:00
#include <QScrollBar>
2013-12-07 20:33:41 +01:00
#include <QMenu>
2013-11-17 16:41:44 +01:00
2013-10-13 16:12:15 +02:00
#include "gui/messagesview.h"
2013-11-15 22:09:38 +01:00
#include "core/messagesproxymodel.h"
#include "core/messagesmodel.h"
2013-12-07 20:33:41 +01:00
#include "gui/formmain.h"
2013-10-13 16:12:15 +02:00
2013-12-07 20:33:41 +01:00
MessagesView::MessagesView(QWidget *parent)
: QTreeView(parent), m_contextMenu(NULL) {
2013-11-15 22:09:38 +01:00
m_proxyModel = new MessagesProxyModel(this);
2013-11-17 16:41:44 +01:00
m_sourceModel = m_proxyModel->sourceModel();
2013-11-15 22:09:38 +01:00
setModel(m_proxyModel);
2013-11-18 21:45:15 +01:00
2013-11-24 18:49:56 +01:00
// FIXME: Sometimes ASSERT occurs if model provides less columns
// than we set resize mode for.
qDebug("Loading MessagesView with %d columns.",
header()->count());
2013-11-24 15:31:04 +01:00
#if QT_VERSION >= 0x050000
2013-11-19 21:25:55 +01:00
// Setup column resize strategies.
header()->setSectionResizeMode(MSG_DB_ID_INDEX, QHeaderView::Interactive);
header()->setSectionResizeMode(MSG_DB_READ_INDEX, QHeaderView::ResizeToContents);
header()->setSectionResizeMode(MSG_DB_DELETED_INDEX, QHeaderView::Interactive);
header()->setSectionResizeMode(MSG_DB_IMPORTANT_INDEX, QHeaderView::ResizeToContents);
header()->setSectionResizeMode(MSG_DB_FEED_INDEX, QHeaderView::Interactive);
header()->setSectionResizeMode(MSG_DB_TITLE_INDEX, QHeaderView::Stretch);
header()->setSectionResizeMode(MSG_DB_URL_INDEX, QHeaderView::Interactive);
header()->setSectionResizeMode(MSG_DB_AUTHOR_INDEX, QHeaderView::Interactive);
header()->setSectionResizeMode(MSG_DB_DCREATED_INDEX, QHeaderView::Interactive);
header()->setSectionResizeMode(MSG_DB_DUPDATED_INDEX, QHeaderView::Interactive);
header()->setSectionResizeMode(MSG_DB_CONTENTS_INDEX, QHeaderView::Interactive);
2013-11-24 15:31:04 +01:00
#else
// Setup column resize strategies.
header()->setResizeMode(MSG_DB_ID_INDEX, QHeaderView::Interactive);
header()->setResizeMode(MSG_DB_READ_INDEX, QHeaderView::ResizeToContents);
header()->setResizeMode(MSG_DB_DELETED_INDEX, QHeaderView::Interactive);
header()->setResizeMode(MSG_DB_IMPORTANT_INDEX, QHeaderView::ResizeToContents);
header()->setResizeMode(MSG_DB_FEED_INDEX, QHeaderView::Interactive);
header()->setResizeMode(MSG_DB_TITLE_INDEX, QHeaderView::Stretch);
header()->setResizeMode(MSG_DB_URL_INDEX, QHeaderView::Interactive);
header()->setResizeMode(MSG_DB_AUTHOR_INDEX, QHeaderView::Interactive);
header()->setResizeMode(MSG_DB_DCREATED_INDEX, QHeaderView::Interactive);
header()->setResizeMode(MSG_DB_DUPDATED_INDEX, QHeaderView::Interactive);
header()->setResizeMode(MSG_DB_CONTENTS_INDEX, QHeaderView::Interactive);
#endif
2013-11-19 21:25:55 +01:00
// Hide columns.
hideColumn(MSG_DB_ID_INDEX);
hideColumn(MSG_DB_DELETED_INDEX);
hideColumn(MSG_DB_FEED_INDEX);
hideColumn(MSG_DB_URL_INDEX);
hideColumn(MSG_DB_CONTENTS_INDEX);
2013-11-18 21:45:15 +01:00
// NOTE: It is recommended to call this after the model is set
// due to sorting performance.
2013-11-15 22:09:38 +01:00
setupAppearance();
2013-10-13 16:12:15 +02:00
}
MessagesView::~MessagesView() {
qDebug("Destroying MessagesView instance.");
}
2013-11-15 22:09:38 +01:00
2013-11-17 20:42:02 +01:00
MessagesModel *MessagesView::sourceModel() {
return m_sourceModel;
}
MessagesProxyModel *MessagesView::model() {
return m_proxyModel;
}
2013-11-19 21:25:55 +01:00
void MessagesView::setSortingEnabled(bool enable) {
QTreeView::setSortingEnabled(enable);
header()->setSortIndicatorShown(false);
}
2013-11-17 20:42:02 +01:00
2013-11-19 21:25:55 +01:00
void MessagesView::setupAppearance() {
header()->setStretchLastSection(false);
2013-11-18 21:45:15 +01:00
setUniformRowHeights(true);
2013-11-15 22:09:38 +01:00
setAcceptDrops(false);
setDragEnabled(false);
setDragDropMode(QAbstractItemView::NoDragDrop);
setExpandsOnDoubleClick(false);
setRootIsDecorated(false);
2013-11-15 22:24:38 +01:00
setItemsExpandable(false);
2013-11-18 21:45:15 +01:00
setSortingEnabled(true);
2013-11-17 16:41:44 +01:00
setAllColumnsShowFocus(true);
setSelectionMode(QAbstractItemView::ExtendedSelection);
}
void MessagesView::selectionChanged(const QItemSelection &selected,
const QItemSelection &deselected) {
QTreeView::selectionChanged(selected, deselected);
}
2013-11-18 21:45:15 +01:00
void MessagesView::keyPressEvent(QKeyEvent *event) {
QTreeView::keyPressEvent(event);
}
2013-12-07 20:33:41 +01:00
void MessagesView::contextMenuEvent(QContextMenuEvent *event) {
QModelIndex clicked_index = indexAt(event->pos());
if (!clicked_index.isValid()) {
qDebug("Context menu for MessagesView will not be shown because "
"user clicked on invalid item.");
return;
}
if (m_contextMenu == NULL) {
// Context menu is not initialized, initialize.
initializeContextMenu();
}
m_contextMenu->exec(event->globalPos());
}
void MessagesView::initializeContextMenu() {
m_contextMenu = new QMenu(tr("Context menu for messages"), this);
m_contextMenu->addActions(QList<QAction*>() <<
FormMain::getInstance()->m_ui->m_actionOpenSelectedSourceArticlesExternally <<
2013-12-08 14:02:28 +01:00
FormMain::getInstance()->m_ui->m_actionOpenSelectedSourceArticlesInternally <<
2013-12-07 20:33:41 +01:00
FormMain::getInstance()->m_ui->m_actionOpenSelectedMessagesInternally <<
FormMain::getInstance()->m_ui->m_actionMarkSelectedMessagesAsRead <<
FormMain::getInstance()->m_ui->m_actionMarkSelectedMessagesAsUnread <<
FormMain::getInstance()->m_ui->m_actionSwitchImportanceOfSelectedMessages <<
FormMain::getInstance()->m_ui->m_actionDeleteSelectedMessages);
}
2013-12-03 21:39:26 +01:00
void MessagesView::mousePressEvent(QMouseEvent *event) {
QTreeView::mousePressEvent(event);
if (event->button() != Qt::LeftButton) {
// No need for extra actions on right/middle click.
return;
}
QModelIndex clicked_index = indexAt(event->pos());
if (!clicked_index.isValid()) {
qDebug("Clicked on invalid index in MessagesView.");
return;
}
QModelIndex mapped_index = m_proxyModel->mapToSource(clicked_index);
if (mapped_index.column() == MSG_DB_IMPORTANT_INDEX) {
m_sourceModel->switchMessageImportance(mapped_index.row());
}
}
2013-11-17 16:41:44 +01:00
void MessagesView::currentChanged(const QModelIndex &current,
const QModelIndex &previous) {
2013-12-04 21:03:09 +01:00
QModelIndex mapped_current_index = m_proxyModel->mapToSource(current);
2013-11-17 16:41:44 +01:00
2013-11-24 14:44:17 +01:00
qDebug("Current row changed, row [%d,%d] source %d %d",
2013-11-17 16:41:44 +01:00
current.row(), current.column(),
2013-12-04 21:03:09 +01:00
mapped_current_index.row(), mapped_current_index.column());
2013-11-17 16:41:44 +01:00
2013-12-08 14:02:28 +01:00
if (!signalsBlocked()) {
if (mapped_current_index.isValid()) {
m_sourceModel->setMessageRead(mapped_current_index.row(), 1);
emit currentMessageChanged(m_sourceModel->messageAt(mapped_current_index.row()));
}
else {
emit currentMessageRemoved();
}
2013-12-04 21:03:09 +01:00
}
2013-11-19 21:25:55 +01:00
2013-11-17 16:41:44 +01:00
QTreeView::currentChanged(current, previous);
2013-11-15 22:09:38 +01:00
}
2013-11-24 14:44:17 +01:00
2013-12-08 14:02:28 +01:00
void MessagesView::openSelectedSourceArticlesExternally() {
2013-12-07 20:33:41 +01:00
// TODO: otevře vybrane zpravy v externim prohlizeci
}
void MessagesView::openSelectedSourceMessagesInternally() {
2013-12-08 14:02:28 +01:00
// TODO: otevre vybrane zpravy ze zdrojovych webz v internch tabech
2013-12-07 20:33:41 +01:00
}
2013-12-08 14:02:28 +01:00
void MessagesView::openSelectedMessagesInternally() {
// TODO: otevre vybrane nactene zpravy v internich tabech
2013-12-07 20:33:41 +01:00
}
2013-12-08 14:02:28 +01:00
void MessagesView::markSelectedMessagesRead() {
setSelectedMessagesReadStatus(1);
}
void MessagesView::markSelectedMessagesUnread() {
setSelectedMessagesReadStatus(0);
}
void MessagesView::setSelectedMessagesReadStatus(int read) {
2013-12-07 20:33:41 +01:00
QModelIndex current_index = selectionModel()->currentIndex();
QModelIndex mapped_current_index = m_proxyModel->mapToSource(current_index);
QModelIndexList selected_indexes = selectionModel()->selectedRows();
QModelIndexList mapped_indexes = m_proxyModel->mapListToSource(selected_indexes);
2013-12-03 21:39:26 +01:00
2013-12-08 14:02:28 +01:00
m_sourceModel->setBatchMessagesRead(mapped_indexes, read);
2013-12-03 21:39:26 +01:00
2013-12-04 21:03:09 +01:00
sortByColumn(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
2013-12-06 15:12:30 +01:00
2013-12-08 14:02:28 +01:00
selected_indexes = m_proxyModel->mapListFromSource(mapped_indexes, true);
current_index = m_proxyModel->mapFromSource(m_sourceModel->index(mapped_current_index.row(),
mapped_current_index.column()));
if (read == 0) {
// User selected to mark some messages as unread, if one
// of them will be marked as current, then it will be read again.
blockSignals(true);
setCurrentIndex(current_index);
blockSignals(false);
2013-12-03 21:39:26 +01:00
}
2013-12-08 14:02:28 +01:00
else {
setCurrentIndex(current_index);
}
scrollTo(current_index);
reselectIndexes(selected_indexes);
}
void MessagesView::deleteSelectedMessages() {
QModelIndex current_index = selectionModel()->currentIndex();
QModelIndex mapped_current_index = m_proxyModel->mapToSource(current_index);
QModelIndexList selected_indexes = selectionModel()->selectedRows();
QModelIndexList mapped_indexes = m_proxyModel->mapListToSource(selected_indexes);
m_sourceModel->setBatchMessagesDeleted(mapped_indexes, 1);
2013-12-06 15:12:30 +01:00
2013-12-08 14:02:28 +01:00
sortByColumn(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
selected_indexes = m_proxyModel->mapListFromSource(mapped_indexes, true);
current_index = m_proxyModel->mapFromSource(m_sourceModel->index(mapped_current_index.row(),
mapped_current_index.column()));
setCurrentIndex(current_index);
scrollTo(current_index);
reselectIndexes(selected_indexes);
}
void MessagesView::switchSelectedMessagesImportance() {
QModelIndex current_index = selectionModel()->currentIndex();
QModelIndex mapped_current_index = m_proxyModel->mapToSource(current_index);
QModelIndexList selected_indexes = selectionModel()->selectedRows();
QModelIndexList mapped_indexes = m_proxyModel->mapListToSource(selected_indexes);
m_sourceModel->switchBatchMessageImportance(mapped_indexes);
sortByColumn(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
selected_indexes = m_proxyModel->mapListFromSource(mapped_indexes, true);
2013-12-07 20:33:41 +01:00
current_index = m_proxyModel->mapFromSource(m_sourceModel->index(mapped_current_index.row(),
2013-12-08 14:02:28 +01:00
mapped_current_index.column()));
2013-12-03 21:39:26 +01:00
2013-12-07 20:33:41 +01:00
setCurrentIndex(current_index);
scrollTo(current_index);
reselectIndexes(selected_indexes);
2013-12-04 21:03:09 +01:00
}
void MessagesView::reselectIndexes(const QModelIndexList &indexes) {
selectionModel()->clearSelection();
2013-12-07 20:33:41 +01:00
foreach (const QModelIndex &index, indexes) {
selectionModel()->select(index,
QItemSelectionModel::Select |
QItemSelectionModel::Rows);
2013-12-04 21:03:09 +01:00
}
2013-11-24 14:44:17 +01:00
}