Skip to content

A bunch of improvements #18

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

Open
wants to merge 7 commits into
base: master
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
42 changes: 5 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ Tensor is an IM client for the [Matrix](https://matrix.org) protocol in developm
Just install things from "Pre-requisites" using your preferred package manager. If your Qt package base is fine-grained you might want to take a look at CMakeLists.txt to figure out which specific libraries Tensor uses.

### Building
From the root directory of the project sources:
From the root directory of the project sources, first update submodules:

```
mkdir build
cd build
cmake ../
make
git submodule update --init # pull in qmatrixclient library
```

Then open the project in QtCreator and build.

### Installation
From the root directory of the project sources:
```
Expand All @@ -46,39 +46,7 @@ Alternatively, [Download *Qt for Android*](http://www.qt.io/download-open-source

![Screenshot](screen/osx.png)

```
brew install qt5
git submodule update --init # pull in qmatrixclient library
cd lib
git submodule update --init # pull in the KCoreAddons package
cd ..
mkdir build
cd build
cmake ../ -DCMAKE_PREFIX_PATH=/usr/local/Cellar/qt5/5.5.1/ # or whatever version of qt5 brew installed
make
tensor &
```

### Troubleshooting

If `cmake` fails with...
```
CMake Warning at CMakeLists.txt:11 (find_package):
By not providing "FindQt5Widgets.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"Qt5Widgets", but CMake did not find one.
```
...then you need to set the right -DCMAKE_PREFIX_PATH variable, see above.

If `make` fails with...
```
Scanning dependencies of target tensor
make[2]: *** No rule to make target `CMakeFiles/tensor.dir/build'. Stop.
make[1]: *** [CMakeFiles/tensor.dir/all] Error 2
```
...then cmake failed to create a build target for tensor as it couldn't find
an optional dependency - probably KCoreAddons. You probably forgot to do the
`git submodule init && git submodule update` dance.

## iOS

Expand Down
11 changes: 9 additions & 2 deletions client/qml/ChatRoom.qml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import QtQuick 2.0
import QtQuick.Controls 1.0
import Matrix 1.0
import 'jschat.js' as JsChat

Rectangle {
id: root

property Connection currentConnection: null
property var currentRoom: null


function setRoom(room) {
currentRoom = room
messageModel.changeRoom(room)
Expand Down Expand Up @@ -41,17 +43,22 @@ Rectangle {
color: "grey"
}
Label {
width: 64
id: authorlabel
width: 100
elide: Text.ElideRight
text: eventType == "message" ? author : "***"
color: eventType == "message" ? "grey" : "lightgrey"
font.family: JsChat.Theme.nickFont()
color: eventType == "message" ? JsChat.NickColoring.get(author): "lightgrey"
horizontalAlignment: Text.AlignRight
}
Label {
id: contentlabel
text: content
wrapMode: Text.Wrap
width: parent.width - (x - parent.x) - spacing
color: eventType == "message" ? "black" : "lightgrey"
font.family: JsChat.Theme.textFont()
font.pointSize: JsChat.Theme.textSize()
}
}

Expand Down
6 changes: 6 additions & 0 deletions client/qml/RoomList.qml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import QtQuick 2.0
import QtQuick.Controls 1.0
import Matrix 1.0
import 'jschat.js' as JsChat

Rectangle {
color: "#6a1b9a"
Expand Down Expand Up @@ -36,6 +37,11 @@ Rectangle {
roomListView.forceLayout()
}

function changeRoom(dir) {
roomListView.currentIndex = JsChat.posmod(roomListView.currentIndex + dir, roomListView.count);
enterRoom(rooms.roomAt(roomListView.currentIndex))
}

Column {
anchors.fill: parent

Expand Down
44 changes: 43 additions & 1 deletion client/qml/RoomView.qml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import QtQuick 2.0
import QtQuick.Controls 1.0
import QtQuick.Controls.Styles 1.4
import 'jschat.js' as JsChat

Item {
id: room

property var currentRoom
property var completion

signal changeRoom(int dir)

function setRoom(room) {
currentRoom = room
chat.setRoom(room)
}

Expand All @@ -17,6 +25,29 @@ Item {
textEntry.text = ''
}

function onKeyPressed(event) {
if ((event.key === Qt.Key_Tab) || (event.key === Qt.Key_Backtab)) {
if (completion === null) completion = new JsChat.NameCompletion(currentRoom.usernames(), textEntry.text);
event.accepted = true;
textEntry.text = completion.complete(event.key === Qt.Key_Tab);

} else if ((event.key !== Qt.Key_Shift) && (event.key !== Qt.Key_Alt) && (event.key !== Qt.Key_Control)) {
// reset
completion = null;
}

if ((event.modifiers & Qt.ControlModifier) === Qt.ControlModifier) {
if (event.key === Qt.Key_PageUp) {
event.accepted = true;
changeRoom(-1);
}
else if (event.key === Qt.Key_PageDown) {
event.accepted = true;
changeRoom(1);
}
}
}

ChatRoom {
id: chat
anchors.bottom: textEntry.top
Expand All @@ -31,8 +62,19 @@ Item {
anchors.left: parent.left
anchors.bottom: parent.bottom
focus: true
textColor: "black"
/*
style: TextFieldStyle {
textColor: "black"
background: Rectangle {
color: "white"
}
}
*/

placeholderText: qsTr("Say something...")
onAccepted: sendLine(text)

Keys.onBacktabPressed: onKeyPressed(event)
Keys.onPressed: onKeyPressed(event)
}
}
5 changes: 3 additions & 2 deletions client/qml/Tensor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import Matrix 1.0
Rectangle {
id: window
visible: true
width: 800
height: 480
width: 960
height: 600
focus: true
color: "#eee"

Expand Down Expand Up @@ -79,6 +79,7 @@ Rectangle {
height: parent.height
Component.onCompleted: {
setConnection(connection)
roomView.changeRoom.connect(roomListItem.changeRoom)
}
}
}
Expand Down
102 changes: 102 additions & 0 deletions client/qml/jschat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
function posmod(x, m) {
x = x % m;
if (x < 0) x += m;
return x;
}

function NameCompletion(userlist_, prefix_) {
this.userlist = [];
for (var i in userlist_) {
if (userlist_[i].startsWith(prefix_)) {
this.userlist.push(NameCompletion.stripIRC(userlist_[i]));
}
}
this.prefix = prefix_;
this.index = -1;
this.last_dir = 1;
}

NameCompletion.stripIRC = function(username) {
var suff = ' (IRC)';
if (username.endsWith(suff)) {
return username.substr(0, username.length - suff.length);
} else {
return username;
}
};

NameCompletion.prototype.get = function(forward) {
if (this.prefix.length === 0) throw new Error('no_prefix');
if (this.userlist.length === 0) throw new Error('no_completion');

var dir = forward ? 1 : -1;
this.index += dir;

return this.userlist[posmod(this.index, this.userlist.length)];
};

NameCompletion.prototype.complete = function(forward) {
return this.get(forward) + ', ';
};

var NickColoring = {
hashCode: function(str) { // java String#hashCode
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
return hash;
},

intToRGB: function(i){
var c = (i & 0x00FFFFFF)
.toString(16)
.toUpperCase();

return "#" + "00000".substring(0, 6 - c.length) + c;
},

get: function(nick) {
return NickColoring.intToRGB(NickColoring.hashCode(nick));
}
};

var Theme = {
textFont: function() { return Qt.platform.os == 'windows' ? 'Consolas' : 'Monaco' },
nickFont: function() { return Theme.textFont(); },
textSize: function() { return 11; }

};

// ---------

function test() {
function assert_eq(act, exp, msg) {
if (act != exp) throw new Error('Assertion EQ actual == expected :\n' + act + ' == ' + exp + '\nfailed: ' + msg);
}

function assert_throws(fn) {
var t = true;
try { fn(); t = false; }
catch (e) {}
if (!t) throw new Error('throw expected');
}

var n = new NameCompletion(['Albert', 'Alaska', 'Bali', 'Czech'], 'A');
assert_eq(n.get(true), 'Albert');
assert_eq(n.get(true), 'Alaska');
assert_eq(n.get(true), 'Albert');

var n = new NameCompletion(['Albert', 'Alaska'], 'X');
assert_throws(function() { n.get(true); });
assert_throws(function() { n.get(false); });

var n = new NameCompletion(['Abb', 'Acc', 'Add', 'Aee'], 'A');
assert_eq(n.get(true), 'Abb');
assert_eq(n.get(true), 'Acc');
assert_eq(n.get(false), 'Abb');
assert_eq(n.get(false), 'Aee');

}

test();
1 change: 1 addition & 0 deletions client/resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
<file>qml/Tensor.qml</file>
<file>qml/ChatRoom.qml</file>
<file>qml/RoomView.qml</file>
<file>qml/jschat.js</file>
</qresource>
</RCC>
Binary file added tensor.icns
Binary file not shown.
Binary file added tensor.ico
Binary file not shown.
5 changes: 4 additions & 1 deletion tensor.pro
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
TEMPLATE = app

QT += qml quick
CONFIG += c++11
CONFIG += c++11 qml_debug

include(lib/libqmatrixclient.pri)

Expand All @@ -23,6 +23,9 @@ QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)

ICON = tensor.icns
RC_ICONS = tensor.ico

DISTFILES += \
android/AndroidManifest.xml \
android/gradle/wrapper/gradle-wrapper.jar \
Expand Down