Skip to content

Commit ccb5437

Browse files
committed
Filter joins/parts/quits instead of just hiding them
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.
1 parent 4af078c commit ccb5437

File tree

9 files changed

+191
-15
lines changed

9 files changed

+191
-15
lines changed

src/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "networksession.h"
4242
#include "messagestorage.h"
4343
#include "messageformatter.h"
44+
#include "messagefilter.h"
4445

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

91+
qRegisterMetaType<QAbstractItemModel*>();
92+
qmlRegisterType<MessageFilter>("Communi", 3, 1, "MessageFilter");
93+
9094
NetworkSession* session = new NetworkSession(app.data());
9195
viewer->rootContext()->setContextProperty("NetworkSession", session);
9296

src/messagefilter.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright (C) 2013-2014 The Communi Project
3+
4+
You may use this file under the terms of BSD license as follows:
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
* Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in the
12+
documentation and/or other materials provided with the distribution.
13+
* Neither the name of the Communi Project nor the names of its
14+
contributors may be used to endorse or promote products derived
15+
from this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
21+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
#include "messagefilter.h"
30+
#include "messagerole.h"
31+
#include <IrcMessage>
32+
33+
MessageFilter::MessageFilter(QObject* parent) : QSortFilterProxyModel(parent), m_events(true)
34+
{
35+
setDynamicSortFilter(true);
36+
}
37+
38+
bool MessageFilter::showEvents() const
39+
{
40+
return m_events;
41+
}
42+
43+
void MessageFilter::setShowEvents(bool show)
44+
{
45+
if (m_events != show) {
46+
m_events = show;
47+
emit showEventsChanged();
48+
invalidateFilter();
49+
}
50+
}
51+
52+
bool MessageFilter::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
53+
{
54+
if (!m_events) {
55+
const QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
56+
switch (index.data(TypeRole).toInt()) {
57+
case IrcMessage::Join:
58+
case IrcMessage::Part:
59+
case IrcMessage::Quit:
60+
return index.data(OwnRole).toBool();
61+
default:
62+
return true;
63+
}
64+
}
65+
return true;
66+
}

src/messagefilter.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright (C) 2013-2014 The Communi Project
3+
4+
You may use this file under the terms of BSD license as follows:
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
* Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in the
12+
documentation and/or other materials provided with the distribution.
13+
* Neither the name of the Communi Project nor the names of its
14+
contributors may be used to endorse or promote products derived
15+
from this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
21+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
#ifndef MESSAGEFILTER_H
30+
#define MESSAGEFILTER_H
31+
32+
#include <QSortFilterProxyModel>
33+
34+
class MessageFilter : public QSortFilterProxyModel
35+
{
36+
Q_OBJECT
37+
Q_PROPERTY(bool showEvents READ showEvents WRITE setShowEvents NOTIFY showEventsChanged)
38+
39+
public:
40+
MessageFilter(QObject* parent = 0);
41+
42+
bool showEvents() const;
43+
void setShowEvents(bool show);
44+
45+
signals:
46+
void showEventsChanged();
47+
48+
protected:
49+
bool filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const;
50+
51+
private:
52+
bool m_events;
53+
};
54+
55+
#endif // MESSAGEFILTER_H

src/messagemodel.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,14 @@
2727
*/
2828

2929
#include "messagemodel.h"
30+
#include "messagerole.h"
3031
#include "messageformatter.h"
3132
#include <QTextBoundaryFinder>
3233
#include <IrcConnection>
3334
#include <IrcMessage>
3435
#include <IrcBuffer>
3536
#include <QDateTime>
3637

37-
enum DataRole {
38-
SeenRole = Qt::UserRole,
39-
HighlightRole,
40-
TimestampRole,
41-
EventRole
42-
};
43-
4438
MessageModel::MessageModel(IrcBuffer* buffer) : QAbstractListModel(buffer),
4539
m_badge(0), m_current(false), m_visible(false), m_separator(-1),
4640
m_highlights(0), m_buffer(buffer), m_formatter(new MessageFormatter(this))
@@ -171,6 +165,8 @@ QHash<int, QByteArray> MessageModel::roleNames() const
171165
roles[TimestampRole] = "timestamp";
172166
roles[EventRole] = "event";
173167
roles[SeenRole] = "seen";
168+
roles[TypeRole] = "type";
169+
roles[OwnRole] = "own";
174170
return roles;
175171
}
176172

@@ -187,6 +183,10 @@ QVariant MessageModel::data(const QModelIndex& index, int role) const
187183
return m_messages.at(row).timestamp;
188184
case EventRole:
189185
return m_messages.at(row).event;
186+
case TypeRole:
187+
return m_messages.at(row).type;
188+
case OwnRole:
189+
return m_messages.at(row).own;
190190
case SeenRole:
191191
return m_seen.at(row);
192192
case Qt::DisplayRole:
@@ -204,8 +204,10 @@ void MessageModel::receive(IrcMessage* message)
204204
data.plaintext = m_formatter->formatMessage(message, Qt::PlainText);
205205
if (!data.plaintext.isEmpty()) {
206206
data.timestamp = message->timeStamp().toString("hh:mm");
207+
data.type = message->type();
208+
data.own = message->flags() & IrcMessage::Own;
207209
data.event = (message->type() != IrcMessage::Private && message->type() != IrcMessage::Notice);
208-
if (!data.event && !(message->flags() & IrcMessage::Own)) {
210+
if (!data.event && !data.own) {
209211
int pos = 0;
210212
QString nick = message->connection()->nickName();
211213
QString content = message->property("content").toString();

src/messagemodel.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
#define MESSAGEMODEL_H
3131

3232
#include <QAbstractListModel>
33+
#include <IrcMessage>
3334
#include <QBitArray>
3435
#include <QVector>
3536

3637
class IrcBuffer;
37-
class IrcMessage;
3838
class MessageFormatter;
3939

4040
class MessageModel : public QAbstractListModel
@@ -96,7 +96,9 @@ private slots:
9696

9797
private:
9898
struct MessageData {
99-
MessageData() : event(false), hilite(false) { }
99+
MessageData() : type(IrcMessage::Unknown), own(false), event(false), hilite(false) { }
100+
int type;
101+
bool own;
100102
bool event;
101103
bool hilite;
102104
QString richtext;

src/messagerole.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright (C) 2013-2014 The Communi Project
3+
4+
You may use this file under the terms of BSD license as follows:
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
* Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in the
12+
documentation and/or other materials provided with the distribution.
13+
* Neither the name of the Communi Project nor the names of its
14+
contributors may be used to endorse or promote products derived
15+
from this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
21+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
#ifndef MESSAGEROLE_H
30+
#define MESSAGEROLE_H
31+
32+
#include <Qt>
33+
34+
enum MessageRole {
35+
SeenRole = Qt::UserRole,
36+
HighlightRole,
37+
TimestampRole,
38+
EventRole,
39+
TypeRole,
40+
OwnRole
41+
};
42+
43+
#endif // MESSAGEROLE_H

src/src.pri

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ INCLUDEPATH += $$PWD $$PWD/3rdparty
33

44
HEADERS += $$PWD/activitymodel.h
55
HEADERS += $$PWD/bufferproxymodel.h
6+
HEADERS += $$PWD/messagefilter.h
67
HEADERS += $$PWD/messagemodel.h
8+
HEADERS += $$PWD/messagerole.h
79
HEADERS += $$PWD/messagestorage.h
810
HEADERS += $$PWD/networksession.h
911
HEADERS += $$PWD/3rdparty/RowsJoinerProxy.h
1012
HEADERS += $$PWD/3rdparty/simplecrypt.h
1113

1214
SOURCES += $$PWD/activitymodel.cpp
1315
SOURCES += $$PWD/bufferproxymodel.cpp
16+
SOURCES += $$PWD/messagefilter.cpp
1417
SOURCES += $$PWD/messagemodel.cpp
1518
SOURCES += $$PWD/messagestorage.cpp
1619
SOURCES += $$PWD/networksession.cpp

ui/settings/settings.qml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Page {
7777
TextSwitch {
7878
width: parent.width
7979
text: qsTrId("Show events")
80-
description: qsTr("Specifies whether joins/parts/quits etc. are shown")
80+
description: qsTr("Specifies whether joins/parts/quits are shown")
8181
checked: eventsConfig.value
8282
onCheckedChanged: eventsConfig.value = checked
8383
}

ui/view/BufferPage.qml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,13 @@ Page {
123123
}
124124
}
125125

126-
model: storage
126+
model: MessageFilter {
127+
sourceModel: storage
128+
showEvents: !!eventsConfig.value
129+
}
127130

128131
delegate: ListItem {
129-
readonly property bool hidden: event && !eventsConfig.value
130-
contentHeight: hidden ? 0 : label.height + (index > 0 && index < view.count - 1 && ListView.isCurrentItem ? Theme.paddingMedium : 0)
131-
visible: !hidden
132+
contentHeight: label.height + (index > 0 && index < view.count - 1 && ListView.isCurrentItem ? Theme.paddingMedium : 0)
132133
Label {
133134
id: stamp
134135
text: timestamp

0 commit comments

Comments
 (0)