Skip to content

Commit

Permalink
Filter joins/parts/quits instead of just hiding them
Browse files Browse the repository at this point in the history
ListView has troubles handling lots of hidden/collapsed items, so let's
make it easier for ListView and filter out the events from the model.
Furthermore, MessageFilter implements a bit smarter filtering ie. only
join/part/quit message types are filtered out, unless they are our own.
  • Loading branch information
jpnurmi committed Feb 13, 2014
1 parent 4af078c commit ccb5437
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "networksession.h"
#include "messagestorage.h"
#include "messageformatter.h"
#include "messagefilter.h"

#include <IrcCore>
#include <IrcModel>
Expand Down Expand Up @@ -87,6 +88,9 @@ Q_DECL_EXPORT int main(int argc, char* argv[])
qmlRegisterType<QQmlSettings>("Qt.labs.settings", 1, 0, "Settings");
#endif

qRegisterMetaType<QAbstractItemModel*>();
qmlRegisterType<MessageFilter>("Communi", 3, 1, "MessageFilter");

NetworkSession* session = new NetworkSession(app.data());
viewer->rootContext()->setContextProperty("NetworkSession", session);

Expand Down
66 changes: 66 additions & 0 deletions src/messagefilter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright (C) 2013-2014 The Communi Project
You may use this file under the terms of BSD license as follows:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Communi Project nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "messagefilter.h"
#include "messagerole.h"
#include <IrcMessage>

MessageFilter::MessageFilter(QObject* parent) : QSortFilterProxyModel(parent), m_events(true)
{
setDynamicSortFilter(true);
}

bool MessageFilter::showEvents() const
{
return m_events;
}

void MessageFilter::setShowEvents(bool show)
{
if (m_events != show) {
m_events = show;
emit showEventsChanged();
invalidateFilter();
}
}

bool MessageFilter::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
{
if (!m_events) {
const QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
switch (index.data(TypeRole).toInt()) {
case IrcMessage::Join:
case IrcMessage::Part:
case IrcMessage::Quit:
return index.data(OwnRole).toBool();
default:
return true;
}
}
return true;
}
55 changes: 55 additions & 0 deletions src/messagefilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright (C) 2013-2014 The Communi Project
You may use this file under the terms of BSD license as follows:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Communi Project nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef MESSAGEFILTER_H
#define MESSAGEFILTER_H

#include <QSortFilterProxyModel>

class MessageFilter : public QSortFilterProxyModel
{
Q_OBJECT
Q_PROPERTY(bool showEvents READ showEvents WRITE setShowEvents NOTIFY showEventsChanged)

public:
MessageFilter(QObject* parent = 0);

bool showEvents() const;
void setShowEvents(bool show);

signals:
void showEventsChanged();

protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const;

private:
bool m_events;
};

#endif // MESSAGEFILTER_H
18 changes: 10 additions & 8 deletions src/messagemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,14 @@
*/

#include "messagemodel.h"
#include "messagerole.h"
#include "messageformatter.h"
#include <QTextBoundaryFinder>
#include <IrcConnection>
#include <IrcMessage>
#include <IrcBuffer>
#include <QDateTime>

enum DataRole {
SeenRole = Qt::UserRole,
HighlightRole,
TimestampRole,
EventRole
};

MessageModel::MessageModel(IrcBuffer* buffer) : QAbstractListModel(buffer),
m_badge(0), m_current(false), m_visible(false), m_separator(-1),
m_highlights(0), m_buffer(buffer), m_formatter(new MessageFormatter(this))
Expand Down Expand Up @@ -171,6 +165,8 @@ QHash<int, QByteArray> MessageModel::roleNames() const
roles[TimestampRole] = "timestamp";
roles[EventRole] = "event";
roles[SeenRole] = "seen";
roles[TypeRole] = "type";
roles[OwnRole] = "own";
return roles;
}

Expand All @@ -187,6 +183,10 @@ QVariant MessageModel::data(const QModelIndex& index, int role) const
return m_messages.at(row).timestamp;
case EventRole:
return m_messages.at(row).event;
case TypeRole:
return m_messages.at(row).type;
case OwnRole:
return m_messages.at(row).own;
case SeenRole:
return m_seen.at(row);
case Qt::DisplayRole:
Expand All @@ -204,8 +204,10 @@ void MessageModel::receive(IrcMessage* message)
data.plaintext = m_formatter->formatMessage(message, Qt::PlainText);
if (!data.plaintext.isEmpty()) {
data.timestamp = message->timeStamp().toString("hh:mm");
data.type = message->type();
data.own = message->flags() & IrcMessage::Own;
data.event = (message->type() != IrcMessage::Private && message->type() != IrcMessage::Notice);
if (!data.event && !(message->flags() & IrcMessage::Own)) {
if (!data.event && !data.own) {
int pos = 0;
QString nick = message->connection()->nickName();
QString content = message->property("content").toString();
Expand Down
6 changes: 4 additions & 2 deletions src/messagemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
#define MESSAGEMODEL_H

#include <QAbstractListModel>
#include <IrcMessage>
#include <QBitArray>
#include <QVector>

class IrcBuffer;
class IrcMessage;
class MessageFormatter;

class MessageModel : public QAbstractListModel
Expand Down Expand Up @@ -96,7 +96,9 @@ private slots:

private:
struct MessageData {
MessageData() : event(false), hilite(false) { }
MessageData() : type(IrcMessage::Unknown), own(false), event(false), hilite(false) { }
int type;
bool own;
bool event;
bool hilite;
QString richtext;
Expand Down
43 changes: 43 additions & 0 deletions src/messagerole.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright (C) 2013-2014 The Communi Project
You may use this file under the terms of BSD license as follows:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Communi Project nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef MESSAGEROLE_H
#define MESSAGEROLE_H

#include <Qt>

enum MessageRole {
SeenRole = Qt::UserRole,
HighlightRole,
TimestampRole,
EventRole,
TypeRole,
OwnRole
};

#endif // MESSAGEROLE_H
3 changes: 3 additions & 0 deletions src/src.pri
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ INCLUDEPATH += $$PWD $$PWD/3rdparty

HEADERS += $$PWD/activitymodel.h
HEADERS += $$PWD/bufferproxymodel.h
HEADERS += $$PWD/messagefilter.h
HEADERS += $$PWD/messagemodel.h
HEADERS += $$PWD/messagerole.h
HEADERS += $$PWD/messagestorage.h
HEADERS += $$PWD/networksession.h
HEADERS += $$PWD/3rdparty/RowsJoinerProxy.h
HEADERS += $$PWD/3rdparty/simplecrypt.h

SOURCES += $$PWD/activitymodel.cpp
SOURCES += $$PWD/bufferproxymodel.cpp
SOURCES += $$PWD/messagefilter.cpp
SOURCES += $$PWD/messagemodel.cpp
SOURCES += $$PWD/messagestorage.cpp
SOURCES += $$PWD/networksession.cpp
Expand Down
2 changes: 1 addition & 1 deletion ui/settings/settings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Page {
TextSwitch {
width: parent.width
text: qsTrId("Show events")
description: qsTr("Specifies whether joins/parts/quits etc. are shown")
description: qsTr("Specifies whether joins/parts/quits are shown")
checked: eventsConfig.value
onCheckedChanged: eventsConfig.value = checked
}
Expand Down
9 changes: 5 additions & 4 deletions ui/view/BufferPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,13 @@ Page {
}
}

model: storage
model: MessageFilter {
sourceModel: storage
showEvents: !!eventsConfig.value
}

delegate: ListItem {
readonly property bool hidden: event && !eventsConfig.value
contentHeight: hidden ? 0 : label.height + (index > 0 && index < view.count - 1 && ListView.isCurrentItem ? Theme.paddingMedium : 0)
visible: !hidden
contentHeight: label.height + (index > 0 && index < view.count - 1 && ListView.isCurrentItem ? Theme.paddingMedium : 0)
Label {
id: stamp
text: timestamp
Expand Down

0 comments on commit ccb5437

Please sign in to comment.