rssguard/src/gui/messagesview.cpp

412 lines
15 KiB
C++
Raw Normal View History

2013-12-21 21:08:52 +01:00
#include "gui/messagesview.h"
#include "core/messagesproxymodel.h"
#include "core/messagesmodel.h"
#include "core/settings.h"
#include "gui/formmain.h"
2014-01-20 13:59:08 +01:00
#include "gui/messagebox.h"
2013-12-21 21:08:52 +01:00
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-12-10 22:06:45 +01:00
#include <QProcess>
2014-01-20 19:20:31 +01:00
#include <QDesktopServices>
2013-11-17 16:41:44 +01:00
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),
m_columnsAdjusted(false),
m_batchUnreadSwitch(false) {
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
// Forward count changes to the view.
createConnections();
2013-11-15 22:09:38 +01:00
setModel(m_proxyModel);
2013-12-13 16:35:52 +01:00
setupAppearance();
}
MessagesView::~MessagesView() {
qDebug("Destroying MessagesView instance.");
}
void MessagesView::createConnections() {
// Forward feed counts changes.
connect(m_sourceModel, SIGNAL(feedCountsChanged()),
this, SIGNAL(feedCountsChanged()));
// Make sure that source message is opened
// in new tab on double click.
connect(this, SIGNAL(doubleClicked(QModelIndex)),
this, SLOT(openSelectedSourceMessagesInternally()));
// Adjust columns when layout gets changed.
connect(header(), SIGNAL(geometriesChanged()),
this, SLOT(adjustColumns()));
}
2013-12-28 11:22:00 +01:00
void MessagesView::reloadSelections(int mark_current_index_read) {
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);
// Reload the model now.
m_sourceModel->select();
m_sourceModel->fetchAll();
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()));
if (current_index.isValid()) {
if (mark_current_index_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.
m_batchUnreadSwitch = true;
setCurrentIndex(current_index);
m_batchUnreadSwitch = false;
}
else {
setCurrentIndex(current_index);
}
scrollTo(current_index);
reselectIndexes(selected_indexes);
2013-12-28 11:22:00 +01:00
}
else {
// Messages were probably removed from the model, nothing can
// be selected and no message can be displayed.
// TOTO: Check if this is OKAY. If not, then emit this signal
// from FeedsView itself.
2014-01-05 18:43:07 +01:00
emit currentMessagesRemoved();
2013-12-28 11:22:00 +01:00
}
}
2013-12-13 16:35:52 +01:00
void MessagesView::setupAppearance() {
2013-12-24 13:35:58 +01:00
header()->setDefaultSectionSize(MESSAGES_VIEW_DEFAULT_COL);
2013-11-19 21:25:55 +01:00
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-12-28 23:11:28 +01:00
setEditTriggers(QAbstractItemView::NoEditTriggers);
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);
}
2013-11-18 21:45:15 +01:00
void MessagesView::keyPressEvent(QKeyEvent *event) {
QTreeView::keyPressEvent(event);
2013-12-09 16:05:16 +01:00
if (event->key() == Qt::Key_Delete) {
deleteSelectedMessages();
}
2013-11-18 21:45:15 +01:00
}
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*>() <<
2014-01-10 09:18:29 +01:00
FormMain::instance()->m_ui->m_actionOpenSelectedSourceArticlesExternally <<
FormMain::instance()->m_ui->m_actionOpenSelectedSourceArticlesInternally <<
FormMain::instance()->m_ui->m_actionOpenSelectedMessagesInternally <<
FormMain::instance()->m_ui->m_actionMarkSelectedMessagesAsRead <<
FormMain::instance()->m_ui->m_actionMarkSelectedMessagesAsUnread <<
FormMain::instance()->m_ui->m_actionSwitchImportanceOfSelectedMessages <<
FormMain::instance()->m_ui->m_actionDeleteSelectedMessages);
2013-12-07 20:33:41 +01:00
}
2013-12-03 21:39:26 +01:00
void MessagesView::mousePressEvent(QMouseEvent *event) {
QTreeView::mousePressEvent(event);
2013-12-20 14:59:41 +01:00
switch (event->button()) {
case Qt::LeftButton: {
// Make sure that message importance is switched when user
// clicks the "important" column.
QModelIndex clicked_index = indexAt(event->pos());
2013-12-03 21:39:26 +01:00
2013-12-20 14:59:41 +01:00
if (clicked_index.isValid()) {
QModelIndex mapped_index = m_proxyModel->mapToSource(clicked_index);
2013-12-03 21:39:26 +01:00
2013-12-20 14:59:41 +01:00
if (mapped_index.column() == MSG_DB_IMPORTANT_INDEX) {
m_sourceModel->switchMessageImportance(mapped_index.row());
}
}
break;
}
2013-12-03 21:39:26 +01:00
2013-12-20 14:59:41 +01:00
case Qt::MiddleButton: {
// Open selected messages in new tab on mouse middle button click.
openSelectedSourceMessagesInternally();
break;
}
2013-12-03 21:39:26 +01:00
2013-12-20 14:59:41 +01:00
default:
break;
2013-12-03 21:39:26 +01:00
}
}
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-12-20 14:59:41 +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
if (mapped_current_index.isValid()) {
if (!m_batchUnreadSwitch) {
// Set this message as read only if current item
// wasn't changed by "mark selected messages unread" action.
2013-12-08 14:02:28 +01:00
m_sourceModel->setMessageRead(mapped_current_index.row(), 1);
}
2014-01-05 18:43:07 +01:00
emit currentMessagesChanged(QList<Message>() <<
m_sourceModel->messageAt(mapped_current_index.row()));
}
else {
2014-01-05 18:43:07 +01:00
emit currentMessagesRemoved();
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
void MessagesView::loadFeeds(const QList<int> &feed_ids) {
// Load messages.
// TODO: Here we could load user-defined default sorting
// column/order AND possibly hide/show user-defined columns.
m_sourceModel->loadMessages(feed_ids);
// Make sure that initial sorting is that unread messages are visible
// first.
// NOTE: This can be rewritten so that it's changeable.
sortByColumn(MSG_DB_DCREATED_INDEX, Qt::DescendingOrder);
// Messages are loaded, make sure that previously
// active message is not shown in browser.
2014-01-05 18:43:07 +01:00
emit currentMessagesRemoved();
}
2013-12-08 14:02:28 +01:00
void MessagesView::openSelectedSourceArticlesExternally() {
2014-01-10 09:18:29 +01:00
QString browser = Settings::instance()->value(APP_CFG_MESSAGES,
"external_browser_executable").toString();
QString arguments = Settings::instance()->value(APP_CFG_MESSAGES,
"external_browser_arguments",
"%1").toString();
2013-12-10 22:06:45 +01:00
if (browser.isEmpty() || arguments.isEmpty()) {
2014-01-22 16:59:39 +01:00
MessageBox::show(this,
QMessageBox::Critical,
tr("External browser not set"),
2014-01-22 17:32:00 +01:00
tr("External browser is not set, head to application settings and set it up to use this feature."));
2014-01-20 13:59:08 +01:00
2013-12-10 22:06:45 +01:00
return;
}
foreach (const QModelIndex &index, selectionModel()->selectedRows()) {
QString link = m_sourceModel->messageAt(m_proxyModel->mapToSource(index).row()).m_url;
2014-01-20 19:10:29 +01:00
if (!QProcess::startDetached('\"' + browser + "\" \"" + arguments.arg(link) + "\"")) {
2014-01-22 16:59:39 +01:00
MessageBox::show(this,
QMessageBox::Critical,
tr("Problem with starting external web browser"),
2014-01-22 17:32:00 +01:00
tr("External web browser could not be started."));
return;
}
2013-12-10 22:06:45 +01:00
}
// Finally, mark opened messages as read.
markSelectedMessagesRead();
2013-12-07 20:33:41 +01:00
}
void MessagesView::openSelectedSourceMessagesInternally() {
2013-12-10 18:54:55 +01:00
foreach (const QModelIndex &index, selectionModel()->selectedRows()) {
2013-12-31 17:33:42 +01:00
Message message = m_sourceModel->messageAt(m_proxyModel->mapToSource(index).row());
if (message.m_url.isEmpty()) {
2014-01-22 16:59:39 +01:00
MessageBox::show(this,
QMessageBox::Warning,
tr("Meesage without URL"),
2014-01-22 17:32:00 +01:00
tr("Message '%s' does not contain URL.").arg(message.m_title));
2013-12-31 17:33:42 +01:00
}
else {
2014-01-05 18:43:07 +01:00
emit openLinkNewTab(message.m_url);
2013-12-31 17:33:42 +01:00
}
2013-12-10 18:54:55 +01:00
}
// Finally, mark opened messages as read.
markSelectedMessagesRead();
2013-12-07 20:33:41 +01:00
}
2013-12-08 14:02:28 +01:00
void MessagesView::openSelectedMessagesInternally() {
2014-01-05 18:43:07 +01:00
QList<Message> messages;
2013-12-10 13:29:38 +01:00
foreach (const QModelIndex &index, selectionModel()->selectedRows()) {
2014-01-05 18:43:07 +01:00
messages << m_sourceModel->messageAt(m_proxyModel->mapToSource(index).row());
2013-12-10 13:29:38 +01:00
}
2014-01-05 18:43:07 +01:00
emit openMessagesInNewspaperView(messages);
// Finally, mark opened messages as read.
// TODO: It is a question if to mark selected messages as read
// when they get opened externally/in new tab.
markSelectedMessagesRead();
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-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.
m_batchUnreadSwitch = true;
2013-12-08 14:02:28 +01:00
setCurrentIndex(current_index);
m_batchUnreadSwitch = 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();
QModelIndexList selected_indexes = selectionModel()->selectedRows();
QModelIndexList mapped_indexes = m_proxyModel->mapListToSource(selected_indexes);
m_sourceModel->setBatchMessagesDeleted(mapped_indexes, 1);
sortByColumn(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
int row_count = m_sourceModel->rowCount();
if (row_count > 0) {
QModelIndex last_item = current_index.row() < row_count ?
m_proxyModel->index(current_index.row(),
MSG_DB_TITLE_INDEX) :
m_proxyModel->index(row_count - 1,
MSG_DB_TITLE_INDEX);
setCurrentIndex(last_item);
scrollTo(last_item);
reselectIndexes(QModelIndexList() << last_item);
}
else {
2014-01-05 18:43:07 +01:00
emit currentMessagesRemoved();
}
2013-12-08 14:02:28 +01:00
}
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
}
void MessagesView::adjustColumns() {
2014-01-06 21:22:00 +01:00
if (header()->count() > 0 && !m_columnsAdjusted) {
m_columnsAdjusted = true;
#if QT_VERSION >= 0x050000
// 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_CONTENTS_INDEX, QHeaderView::Interactive);
#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_CONTENTS_INDEX, QHeaderView::Interactive);
#endif
// Hide columns.
// TODO: Make this changeable.
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);
qDebug("Adjusting column resize modes for MessagesView.");
}
}