From 9d52e49424afb60c2f28073051c1dbf25f47adec Mon Sep 17 00:00:00 2001 From: Marcus Eggenberger Date: Mon, 11 Aug 2008 13:28:47 +0200 Subject: [PATCH] fixing BR #261 (making fields removable from the chatmonitor). Changes effect currently only new messages as the ChatViews don't treat dataChanged() properly --- src/qtui/CMakeLists.txt | 2 + src/qtui/chatmonitorfilter.cpp | 78 ++++++++++++++++++++++------------ src/qtui/chatmonitorfilter.h | 29 ++++++++++--- src/qtui/chatmonitorview.cpp | 67 +++++++++++++++++++++++++++++ src/qtui/chatmonitorview.h | 44 +++++++++++++++++++ src/qtui/chatscene.cpp | 9 ++++ src/qtui/chatscene.h | 2 + src/qtui/chatview.cpp | 22 ++++------ src/qtui/chatview.h | 39 ++++++++--------- src/qtui/mainwin.cpp | 3 +- 10 files changed, 225 insertions(+), 70 deletions(-) create mode 100644 src/qtui/chatmonitorview.cpp create mode 100644 src/qtui/chatmonitorview.h diff --git a/src/qtui/CMakeLists.txt b/src/qtui/CMakeLists.txt index 670cda30..f1289851 100644 --- a/src/qtui/CMakeLists.txt +++ b/src/qtui/CMakeLists.txt @@ -14,6 +14,7 @@ set(SOURCES chatlinemodel.cpp chatlinemodelitem.cpp chatmonitorfilter.cpp + chatmonitorview.cpp chatscene.cpp chatview.cpp columnhandleitem.cpp @@ -43,6 +44,7 @@ set(MOC_HDRS channellistdlg.h chatlinemodel.h chatmonitorfilter.h + chatmonitorview.h chatscene.h chatview.h columnhandleitem.h diff --git a/src/qtui/chatmonitorfilter.cpp b/src/qtui/chatmonitorfilter.cpp index 9c7082b1..d11378d1 100644 --- a/src/qtui/chatmonitorfilter.cpp +++ b/src/qtui/chatmonitorfilter.cpp @@ -26,10 +26,9 @@ #include "networkmodel.h" ChatMonitorFilter::ChatMonitorFilter(MessageModel *model, QObject *parent) -: MessageFilter(model, QList(), parent) + : MessageFilter(model, QList(), parent), + _initTime(QDateTime::currentDateTime()) { - _initTime = QDateTime::currentDateTime(); - } bool ChatMonitorFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const { @@ -38,37 +37,62 @@ bool ChatMonitorFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourc Message::Flags flags = (Message::Flags)sourceModel()->data(sourceModel()->index(sourceRow, 0), MessageModel::FlagsRole).toInt(); if(!((type & (Message::Plain | Message::Notice | Message::Action)) || flags & Message::Self)) return false; + QDateTime msgTime = sourceModel()->data(sourceModel()->index(sourceRow, 0), MessageModel::TimestampRole).toDateTime(); return msgTime > _initTime; } -QString ChatMonitorFilter::idString() const { - return "ChatMonitor"; -} - // override this to inject display of network and channel QVariant ChatMonitorFilter::data(const QModelIndex &index, int role) const { - if(index.column() != ChatLineModel::SenderColumn) return MessageFilter::data(index, role); - if(role == ChatLineModel::DisplayRole) { - BufferId bufid = data(index, ChatLineModel::BufferIdRole).value(); - if(bufid.isValid()) { - Buffer *buf = Client::buffer(bufid); - if(!buf) { - qDebug() << "invalid buffer!"; - return QVariant(); - } - const Network *net = Client::networkModel()->networkByIndex(Client::networkModel()->bufferIndex(bufid)); - if(!net) { - qDebug() << "invalid net!"; - return QVariant(); - } - QString result = QString("<%1:%2:%3").arg(net->networkName()) - .arg(buf->bufferInfo().bufferName()) - .arg(MessageFilter::data(index, role).toString().mid(1)); - return result; - } + if(index.column() != ChatLineModel::SenderColumn || role != ChatLineModel::DisplayRole) + return MessageFilter::data(index, role); + int showFields_ = showFields(); + + BufferId bufid = data(index, ChatLineModel::BufferIdRole).value(); + if(!bufid.isValid()) { + qDebug() << "ChatMonitorFilter::data(): chatline belongs to an invalid buffer!"; + return QVariant(); } - return MessageFilter::data(index, role); + + QStringList fields; + if(showFields_ & NetworkField) { + fields << Client::networkModel()->networkName(bufid); + } + if(showFields_ & BufferField) { + fields << Client::networkModel()->bufferName(bufid); + } + fields << MessageFilter::data(index, role).toString().mid(1); + return QString("<%1").arg(fields.join(":")); +} + +void ChatMonitorFilter::addShowField(int field) { + QtUiSettings s; + int fields = s.value(showFieldSettingId(), AllFields).toInt(); + if(fields & field) + return; + fields |= field; + s.setValue(showFieldSettingId(), fields); + showFieldSettingsChanged(); } + +void ChatMonitorFilter::removeShowField(int field) { + QtUiSettings s; + int fields = s.value(showFieldSettingId(), AllFields).toInt(); + if(!(fields & field)) + return; + + fields ^= field; + s.setValue(showFieldSettingId(), fields); + showFieldSettingsChanged(); +} + +void ChatMonitorFilter::showFieldSettingsChanged() { + int rows = rowCount(); + if(rows == 0) + return; + + emit dataChanged(index(0, ChatLineModel::SenderColumn), index(rows - 1, ChatLineModel::SenderColumn)); +} + diff --git a/src/qtui/chatmonitorfilter.h b/src/qtui/chatmonitorfilter.h index 782797fb..a27d3e5d 100644 --- a/src/qtui/chatmonitorfilter.h +++ b/src/qtui/chatmonitorfilter.h @@ -24,20 +24,35 @@ #include #include "messagefilter.h" +#include "qtuisettings.h" class ChatMonitorFilter : public MessageFilter { Q_OBJECT - public: - ChatMonitorFilter(MessageModel *model, QObject *parent = 0); +public: + enum SenderFields { + NoField = 0x00, + NetworkField = 0x01, + BufferField = 0x02, + SenderField = 0x04, + AllFields = 0xFF + }; - virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const; - virtual QString idString() const; + ChatMonitorFilter(MessageModel *model, QObject *parent = 0); - virtual QVariant data(const QModelIndex &index, int role) const; + virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const; + virtual QString idString() const { return "ChatMonitor"; } + virtual QVariant data(const QModelIndex &index, int role) const; - private: - QDateTime _initTime; + inline QString showFieldSettingId() const { return QString("ChatView/%1/showFields").arg(idString()); } + inline int showFields() const { return QtUiSettings().value(showFieldSettingId(), AllFields).toInt(); } + void addShowField(int field); + void removeShowField(int field); + +private: + QDateTime _initTime; + + void showFieldSettingsChanged(); }; #endif diff --git a/src/qtui/chatmonitorview.cpp b/src/qtui/chatmonitorview.cpp new file mode 100644 index 00000000..6b9fe601 --- /dev/null +++ b/src/qtui/chatmonitorview.cpp @@ -0,0 +1,67 @@ +/*************************************************************************** + * Copyright (C) 2005-08 by the Quassel Project * + * devel@quassel-irc.org * + * * + * 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 of the License, or * + * (at your option) version 3. * + * * + * 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, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "chatmonitorview.h" + +#include +#include +#include + +#include "chatmonitorfilter.h" +#include "chatlinemodel.h" +#include "chatscene.h" +#include "qtuisettings.h" + +ChatMonitorView::ChatMonitorView(ChatMonitorFilter *filter, QWidget *parent) + : ChatView(filter, parent), + _filter(filter) +{ +} + +void ChatMonitorView::contextMenuEvent(QContextMenuEvent *event) { + if(scene()->sectionByScenePos(event->pos()) != ChatLineModel::SenderColumn) + return; + + int showFields = _filter->showFields(); + + QMenu contextMenu(this); + QAction *showNetworkAction = contextMenu.addAction(tr("Show network name"), this, SLOT(showFieldsChanged(bool))); + showNetworkAction->setCheckable(true); + showNetworkAction->setChecked(showFields & ChatMonitorFilter::NetworkField); + showNetworkAction->setData(ChatMonitorFilter::NetworkField); + + QAction *showBufferAction = contextMenu.addAction(tr("Show buffer name"), this, SLOT(showFieldsChanged(bool))); + showBufferAction->setCheckable(true); + showBufferAction->setChecked(showFields & ChatMonitorFilter::BufferField); + showBufferAction->setData(ChatMonitorFilter::BufferField); + + contextMenu.exec(QCursor::pos()); +} + +void ChatMonitorView::showFieldsChanged(bool checked) { + QAction *showAction = qobject_cast(sender()); + if(!showAction) + return; + + if(checked) + _filter->addShowField(showAction->data().toInt()); + else + _filter->removeShowField(showAction->data().toInt()); +} diff --git a/src/qtui/chatmonitorview.h b/src/qtui/chatmonitorview.h new file mode 100644 index 00000000..bf0b3346 --- /dev/null +++ b/src/qtui/chatmonitorview.h @@ -0,0 +1,44 @@ +/*************************************************************************** + * Copyright (C) 2005-08 by the Quassel Project * + * devel@quassel-irc.org * + * * + * 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 of the License, or * + * (at your option) version 3. * + * * + * 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, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef CHATMONITORVIEW_H +#define CHATMONITORVIEW_H + +#include "chatview.h" + +class ChatMonitorFilter; + +class ChatMonitorView : public ChatView { + Q_OBJECT + +public: + ChatMonitorView(ChatMonitorFilter *filter, QWidget *parent); + +protected: + virtual void contextMenuEvent(QContextMenuEvent *event); + +private slots: + void showFieldsChanged(bool checked); + +private: + ChatMonitorFilter *_filter; +}; + +#endif //CHATMONITORVIEW_H diff --git a/src/qtui/chatscene.cpp b/src/qtui/chatscene.cpp index a6578fa6..aeac420b 100644 --- a/src/qtui/chatscene.cpp +++ b/src/qtui/chatscene.cpp @@ -305,3 +305,12 @@ void ChatScene::requestBacklogIfNeeded() { _lastBacklogSize = model()->rowCount(); } } + +int ChatScene::sectionByScenePos(int x) { + if(x < firstColHandlePos) + return ChatLineModel::TimestampColumn; + if(x < secondColHandlePos) + return ChatLineModel::SenderColumn; + + return ChatLineModel::ContentsColumn; +} diff --git a/src/qtui/chatscene.h b/src/qtui/chatscene.h index e72308e4..c69f0e12 100644 --- a/src/qtui/chatscene.h +++ b/src/qtui/chatscene.h @@ -49,6 +49,8 @@ class ChatScene : public QGraphicsScene { inline bool isFetchingBacklog() const; inline bool isBacklogFetchingEnabled() const; inline BufferId bufferForBacklogFetching() const; + int sectionByScenePos(int x); + inline int sectionByScenePos(const QPoint &pos) { return sectionByScenePos(pos.x()); } public slots: void setWidth(qreal); diff --git a/src/qtui/chatview.cpp b/src/qtui/chatview.cpp index 6d4e5ea2..dbddaaa7 100644 --- a/src/qtui/chatview.cpp +++ b/src/qtui/chatview.cpp @@ -29,7 +29,10 @@ #include "messagefilter.h" #include "quasselui.h" -ChatView::ChatView(Buffer *buf, QWidget *parent) : QGraphicsView(parent), AbstractChatView() { +ChatView::ChatView(Buffer *buf, QWidget *parent) + : QGraphicsView(parent), + AbstractChatView() +{ QList filterList; filterList.append(buf->bufferInfo().bufferId()); MessageFilter *filter = new MessageFilter(Client::messageModel(), filterList, this); @@ -37,7 +40,10 @@ ChatView::ChatView(Buffer *buf, QWidget *parent) : QGraphicsView(parent), Abstra } -ChatView::ChatView(MessageFilter *filter, QWidget *parent) : QGraphicsView(parent), AbstractChatView() { +ChatView::ChatView(MessageFilter *filter, QWidget *parent) + : QGraphicsView(parent), + AbstractChatView() +{ init(filter); } @@ -55,18 +61,6 @@ void ChatView::init(MessageFilter *filter) { connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarChanged(int))); } -ChatView::~ChatView() { - -} - -ChatScene *ChatView::scene() const { - return _scene; -} - -void ChatView::clear() { - -} - void ChatView::resizeEvent(QResizeEvent *event) { scene()->setWidth(event->size().width() - 2); // FIXME figure out why we have to hardcode the -2 here verticalScrollBar()->setValue(verticalScrollBar()->maximum()); diff --git a/src/qtui/chatview.h b/src/qtui/chatview.h index b9c1892a..6da995e8 100644 --- a/src/qtui/chatview.h +++ b/src/qtui/chatview.h @@ -34,32 +34,29 @@ class MessageFilter; class ChatView : public QGraphicsView, public AbstractChatView { Q_OBJECT - public: - ChatView(MessageFilter *, QWidget *parent = 0); - ChatView(Buffer *, QWidget *parent = 0); - ~ChatView(); +public: + ChatView(MessageFilter *, QWidget *parent = 0); + ChatView(Buffer *, QWidget *parent = 0); - ChatScene *scene() const; + inline ChatScene *scene() const { return _scene; } - public slots: +public slots: + inline virtual void clear() {} + void setBufferForBacklogFetching(BufferId buffer); - void clear(); +protected: + virtual void resizeEvent(QResizeEvent *event); - void setBufferForBacklogFetching(BufferId buffer); +protected slots: + virtual void sceneHeightChanged(qreal height); + virtual void verticalScrollbarChanged(int); + virtual void sliderPressed(); + virtual void sliderReleased(); + +private: + void init(MessageFilter *filter); - protected: - virtual void resizeEvent(QResizeEvent *event); - - protected slots: - virtual void sceneHeightChanged(qreal height); - virtual void verticalScrollbarChanged(int); - virtual void sliderPressed(); - virtual void sliderReleased(); - - private: - void init(MessageFilter *filter); - - ChatScene *_scene; + ChatScene *_scene; }; diff --git a/src/qtui/mainwin.cpp b/src/qtui/mainwin.cpp index 6c8628a6..9d0de308 100644 --- a/src/qtui/mainwin.cpp +++ b/src/qtui/mainwin.cpp @@ -27,6 +27,7 @@ #include "channellistdlg.h" #include "chatlinemodel.h" #include "chatmonitorfilter.h" +#include "chatmonitorview.h" #include "chatview.h" #include "client.h" #include "clientbacklogmanager.h" @@ -295,7 +296,7 @@ void MainWin::setupChatMonitor() { dock->setObjectName("ChatMonitorDock"); ChatMonitorFilter *filter = new ChatMonitorFilter(Client::messageModel(), this); - ChatView *chatView = new ChatView(filter, this); + ChatMonitorView *chatView = new ChatMonitorView(filter, this); chatView->show(); dock->setWidget(chatView); dock->show(); -- 2.20.1