summaryrefslogtreecommitdiffstats
path: root/src/libcommuni/examples/quick/qml/ChatPage.qml
blob: 474b950dd03cca5e6dadadb1cb72c4fabb1825fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright (C) 2008-2014 The Communi Project
 *
 * This example is free, and not covered by the BSD license. There is no
 * restriction applied to their modification, redistribution, using and so on.
 * You can study them, modify them, use them in your own program - either
 * completely or partially.
 */

import QtQuick 2.1
import QtQuick.Layouts 1.0
import QtQuick.Controls 1.0
import QtQuick.Controls.Styles 1.0
import Communi 3.0

Item {
    id: page

    property IrcBuffer serverBuffer
    property alias bufferModel: bufferListView.bufferModel
    property alias currentBuffer: bufferListView.currentBuffer
    property IrcChannel currentChannel: currentBuffer ? currentBuffer.toChannel() : null

    Connections {
        target: bufferModel
        onAdded: currentBuffer = buffer
        onAboutToBeRemoved: {
            var idx = bufferModel.indexOf(buffer)
            currentBuffer = bufferModel.get(idx + 1) || bufferModel.get(Math.max(0, idx - 1))
        }
    }

    SplitView {
        anchors.fill: parent

        handleDelegate: Item { }

        BufferListView {
            id: bufferListView
            width: page.width / 6
            onClosed: {
                if (buffer === serverBuffer) {
                    bufferModel.quit()
                } else {
                    if (buffer.channel)
                        buffer.part(qsTr("Communi %1 QtQuick example").arg(irc.version()))
                    bufferModel.remove(buffer)
                }
            }
        }

        Column {
            Layout.fillWidth: true

            TopicLabel {
                id: topicLabel
                width: parent.width
                visible: currentChannel
                channel: currentChannel
            }

            SplitView {
                width: parent.width
                height: parent.height - (currentChannel ? topicLabel.height : 0) - textEntry.height

                handleDelegate: Item { }

                Item {
                    id: stack

                    width: 1; height: 1
                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    Repeater {
                        anchors.fill: parent
                        model: bufferModel
                        delegate: TextBrowser {
                            anchors.fill: parent
                            buffer: model.buffer
                            visible: buffer == currentBuffer
                        }
                    }
                }

                UserListView {
                    width: page.width / 6
                    visible: currentChannel
                    channel: currentChannel
                    onQueried: currentBuffer = currentBuffer.model.add(user.name)
                }
            }

            TextEntry {
                id: textEntry
                width: parent.width
                buffer: currentBuffer
                enabled: currentBuffer
                onMessageSent: currentBuffer.receiveMessage(message)

                Connections {
                    target: page
                    onCurrentBufferChanged: {
                        if (page.visible && currentBuffer)
                            textEntry.forceActiveFocus()
                    }
                }
            }
        }
    }
}