Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved scrollindicators #120

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ set(quaternion_SRCS
client/quaternionroom.cpp
client/message.cpp
client/imageprovider.cpp
client/activitydetector.cpp
client/logindialog.cpp
client/mainwindow.cpp
client/roomlistdock.cpp
Expand Down
67 changes: 67 additions & 0 deletions client/activitydetector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**************************************************************************
* *
* Copyright (C) 2016 Malte Brandy <[email protected]> *
* *
* 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 3 *
* of the License, or (at your option) any later version. *
* *
* 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, see <http://www.gnu.org/licenses/>. *
* *
**************************************************************************/

#include "activitydetector.h"
#include "mainwindow.h"
#include "chatroomwidget.h"
#include "models/messageeventmodel.h"
#include <QtCore/QDebug>

ActivityDetector::ActivityDetector(QApplication* a, MainWindow* w): m_app(a), m_mainWindow(w), m_enabled(false), m_messageEventModel(w->getChatRoomWidget()->getMessageEventModel())
{
connect(this, &ActivityDetector::triggered, m_messageEventModel, &MessageEventModel::markShownAsRead);
connect(m_messageEventModel, &MessageEventModel::lastShownIndexChanged, this, &ActivityDetector::updateEnabled);
connect(m_messageEventModel, &MessageEventModel::readMarkerIndexChanged, this, &ActivityDetector::updateEnabled);
}

void ActivityDetector::updateEnabled()
{
setEnabled(m_messageEventModel->awaitingMarkRead());
}

void ActivityDetector::setEnabled(bool enabled)
{
if (enabled && !m_enabled) {
m_app->installEventFilter(this);
m_mainWindow->setMouseTracking(true);
m_enabled = true;
qDebug() << "enabling ActivityDetector";
}
if (!enabled && m_enabled) {
m_mainWindow->setMouseTracking(false);
m_app->removeEventFilter(this);
m_enabled = false;
qDebug() << "disabling ActivityDetector";
}
}

bool ActivityDetector::eventFilter(QObject* obj, QEvent* ev)
{
switch (ev->type())
{
case QEvent::KeyPress:
case QEvent::FocusIn:
case QEvent::MouseMove:
case QEvent::MouseButtonPress:
emit triggered();
setEnabled(false);
default:;
}
return QObject::eventFilter(obj, ev);
}
49 changes: 49 additions & 0 deletions client/activitydetector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**************************************************************************
* *
* Copyright (C) 2016 Malte Brandy <[email protected]> *
* *
* 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 3 *
* of the License, or (at your option) any later version. *
* *
* 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, see <http://www.gnu.org/licenses/>. *
* *
**************************************************************************/

#pragma once

#include <QtWidgets/QApplication>

class MainWindow;
class MessageEventModel;

class ActivityDetector : public QObject
{
Q_OBJECT

public:
ActivityDetector(QApplication* a, MainWindow* c);

public slots:
void updateEnabled();
void setEnabled(bool enabled);

signals:
void triggered();

protected:
bool eventFilter(QObject* obj, QEvent* ev);

private:
QApplication* m_app;
MainWindow* m_mainWindow;
bool m_enabled;
MessageEventModel* m_messageEventModel;
};
5 changes: 2 additions & 3 deletions client/chatroomwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ ChatRoomWidget::ChatRoomWidget(QWidget* parent)
QObject* rootItem = m_quickView->rootObject();
connect( rootItem, SIGNAL(getPreviousContent()), this, SLOT(getPreviousContent()) );


m_chatEdit = new ChatEdit(this);
connect( m_chatEdit, &QLineEdit::returnPressed, this, &ChatRoomWidget::sendLine );

Expand All @@ -116,9 +115,9 @@ ChatRoomWidget::~ChatRoomWidget()
{
}

void ChatRoomWidget::lookAtRoom()
MessageEventModel* ChatRoomWidget::getMessageEventModel()
{
m_messageModel->markShownAsRead();
return m_messageModel;
}

void ChatRoomWidget::enableDebug()
Expand Down
2 changes: 1 addition & 1 deletion client/chatroomwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ChatRoomWidget: public QWidget
void enableDebug();
void triggerCompletion();
void cancelCompletion();
void lookAtRoom();
MessageEventModel* getMessageEventModel();

signals:
void joinRoomNeedsInteraction();
Expand Down
30 changes: 2 additions & 28 deletions client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,7 @@
#include <QtCore/QDebug>

#include "mainwindow.h"

class ActivityDetector : public QObject
{
public:
ActivityDetector(MainWindow* c): m_mainWindow(c)
{
c->setMouseTracking(true);
};
protected:
bool eventFilter(QObject* obj, QEvent* ev)
{
switch (ev->type())
{
case QEvent::KeyPress:
case QEvent::FocusIn:
case QEvent::MouseMove:
case QEvent::MouseButtonPress:
m_mainWindow->activity();
default:;
}
return QObject::eventFilter(obj, ev);
}
private:
MainWindow* m_mainWindow;
};

#include "activitydetector.h"

int main( int argc, char* argv[] )
{
Expand Down Expand Up @@ -80,8 +55,7 @@ int main( int argc, char* argv[] )
MainWindow window;
if( debugEnabled )
window.enableDebug();
ActivityDetector ad(&window);
app.installEventFilter(&ad);
ActivityDetector ad(&app, &window);
window.show();

return app.exec();
Expand Down
4 changes: 2 additions & 2 deletions client/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ MainWindow::~MainWindow()
{
}

void MainWindow::activity()
ChatRoomWidget* MainWindow::getChatRoomWidget()
{
chatRoomWidget->lookAtRoom();
return chatRoomWidget;
}

void MainWindow::createMenu()
Expand Down
2 changes: 1 addition & 1 deletion client/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class MainWindow: public QMainWindow
virtual ~MainWindow();

void enableDebug();
void activity();

void setConnection(QuaternionConnection* newConnection);
ChatRoomWidget* getChatRoomWidget();

protected:
virtual void closeEvent(QCloseEvent* event) override;
Expand Down
44 changes: 39 additions & 5 deletions client/models/messageeventmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ MessageEventModel::MessageEventModel(QObject* parent)
: QAbstractListModel(parent)
, m_currentRoom(nullptr)
, lastShownIndex(-1)
, m_readMarkerIndex(-1)
{ }

MessageEventModel::~MessageEventModel()
Expand All @@ -83,25 +84,34 @@ void MessageEventModel::changeRoom(QuaternionRoom* room)

m_currentRoom = room;
lastShownIndex = -1;
m_readMarkerIndex = -1;
if( room )
{
using namespace QMatrixClient;
connect(m_currentRoom, &Room::aboutToAddNewMessages,
connect(m_currentRoom, &Room::aboutToAddNewMessages, this,
[=](const Events& events)
{
beginInsertRows(QModelIndex(),
rowCount(), rowCount() + events.size() - 1);
});
connect(m_currentRoom, &Room::aboutToAddHistoricalMessages,
connect(m_currentRoom, &Room::aboutToAddHistoricalMessages, this,
[=](const Events& events)
{
beginInsertRows(QModelIndex(), 0, events.size() - 1);
});
connect(m_currentRoom, &Room::addedMessages,
this, &MessageEventModel::endInsertRows);
connect(m_currentRoom, &Room::addedMessages, this,
[=]()
{
endInsertRows();
updateReadMarkerIndex();
});
connect(m_currentRoom, &Room::readMarkerPromoted,
this, &MessageEventModel::updateReadMarkerIndex);
qDebug() << "connected" << room;
}
endResetModel();
emit lastShownIndexChanged(lastShownIndex);
updateReadMarkerIndex();
}

int MessageEventModel::rowCount(const QModelIndex& parent) const
Expand Down Expand Up @@ -329,9 +339,33 @@ QVariant MessageEventModel::data(const QModelIndex& index, int role) const

void MessageEventModel::markShownAsRead()
{
if (m_currentRoom && lastShownIndex > -1)
if (m_currentRoom && lastShownIndex > -1 && lastShownIndex < m_currentRoom->messages().count())
{
auto lastShownMessage = m_currentRoom->messages().at(lastShownIndex);
m_currentRoom->markMessagesAsRead(lastShownMessage->messageEvent()->id());
}
}

void MessageEventModel::updateReadMarkerIndex()
{
if( !m_currentRoom )
return;
for (int newReadMarkerIndex = m_readMarkerIndex; newReadMarkerIndex < m_currentRoom->messages().count() ; ++newReadMarkerIndex)
{
if (newReadMarkerIndex >= 0 && m_currentRoom->readMarkerEventId() == m_currentRoom->messages().at(newReadMarkerIndex)->messageEvent()->id())
{
if (newReadMarkerIndex != m_readMarkerIndex)
{
m_readMarkerIndex = newReadMarkerIndex;
emit readMarkerIndexChanged(newReadMarkerIndex);
}
return;
}
}
}

bool MessageEventModel::awaitingMarkRead()
{
qDebug() << "readMarker: " << m_readMarkerIndex << "lastShown: " << lastShownIndex;
return m_readMarkerIndex < lastShownIndex;
}
6 changes: 6 additions & 0 deletions client/models/messageeventmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class MessageEventModel: public QAbstractListModel
Q_OBJECT
Q_PROPERTY(QuaternionRoom* room MEMBER m_currentRoom CONSTANT)
Q_PROPERTY(int lastShownIndex MEMBER lastShownIndex NOTIFY lastShownIndexChanged)
Q_PROPERTY(int readMarkerIndex MEMBER m_readMarkerIndex NOTIFY readMarkerIndexChanged)
public:
MessageEventModel(QObject* parent = nullptr);
virtual ~MessageEventModel();
Expand All @@ -42,15 +43,20 @@ class MessageEventModel: public QAbstractListModel
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> roleNames() const override;

bool awaitingMarkRead();

signals:
void lastShownIndexChanged(int newValue);
void readMarkerIndexChanged(int newValue);

public slots:
void markShownAsRead();
void updateReadMarkerIndex();

private:
QuaternionRoom* m_currentRoom;
int lastShownIndex;
int m_readMarkerIndex;
};

#endif // LOGMESSAGEMODEL_H
Loading