Rewrite FileDialog in C++ && add setting to force async load timelime.

Also fixes repeat section label and empty space when message is redacted.

Fixes #27
This commit is contained in:
Black Hat 2018-08-14 14:05:41 +08:00
parent 1e5fda47ab
commit 09fdd52524
11 changed files with 63 additions and 55 deletions

View File

@ -8,6 +8,8 @@ import "form"
Page {
property alias lazyLoad: generalForm.lazyLoad
property alias asyncMessageDelegate: generalForm.asyncMessageDelegate
property alias darkTheme: appearanceForm.darkTheme
property alias miniMode: appearanceForm.miniMode

View File

@ -1,13 +0,0 @@
import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Dialogs 1.2
FileDialog {
id: locationDialog
title: "Please choose a location"
folder: shortcuts.home
selectFolder: true
onAccepted: currentRoom.downloadFile(eventId, folder + "/" + currentRoom.fileNameToDownload(eventId))
onVisibleChanged: visible ? {} : locationDialog.destroy()
}

View File

@ -17,7 +17,7 @@ Item {
onDownloadedChanged: downloaded && openOnFinished ? openSavedFile() : {}
function saveFileAs() {
Qt.createComponent("DownloadDialog.qml").createObject(this).open()
matriqueController.saveFileAs(currentRoom, eventId)
}
function downloadAndOpen()

View File

@ -4,7 +4,7 @@ import QtQuick.Controls.Material 2.2
import Matrique 0.1
Item {
id: messageDelegate
readonly property bool hidden: marks === EventStatus.Redacted || marks === EventStatus.Hidden
readonly property bool darkTheme: Material.theme == Material.Dark
readonly property color background: darkTheme ? "#242424" : "lightgrey"
@ -12,7 +12,7 @@ Item {
readonly property bool sentByMe: author === currentRoom.localUser
readonly property bool isState: eventType === "state" || eventType === "emote"
visible: marks !== EventStatus.Hidden
id: messageDelegate
z: -5
width: delegateLoader.width
@ -29,8 +29,10 @@ Item {
Loader {
id: delegateLoader
asynchronous: setting.asyncMessageDelegate
source: {
if (eventType == "redaction") return ""
if (eventType == "redaction" || hidden) return ""
switch (eventType) {
case "state":
case "emote":

View File

@ -191,12 +191,15 @@ Item {
}
delegate: Column {
readonly property bool hidden: marks === EventStatus.Redacted || marks === EventStatus.Hidden
width: parent.width
height: hidden ? -8 : undefined
spacing: 8
RowLayout {
width: parent.width * 0.8
visible: section !== aboveSection
visible: section !== aboveSection && !hidden
anchors.horizontalCenter: parent.horizontalCenter
spacing: 8
@ -300,30 +303,19 @@ Item {
contentItem: MaterialIcon { icon: "\ue226" }
onClicked: fileDialog.visible = true
FileDialog {
id: fileDialog
title: "Please choose a file"
folder: shortcuts.home
selectMultiple: false
onAccepted: {
currentRoom.uploadFile(fileUrl, fileUrl, matriqueController.getMIME(fileUrl))
var fileTransferProgressCallback = function(id, sent, total) {
if (id == fileUrl) { inputField.progress = sent / total }
}
var completedCallback = function(id, localFile, mxcUrl) {
if (id == fileUrl) {
matriqueController.postFile(currentRoom, localFile, mxcUrl)
inputField.progress = 0
currentRoom.fileTransferCompleted.disconnect(fileTransferProgressCallback)
currentRoom.fileTransferCompleted.disconnect(completedCallback)
}
}
currentRoom.fileTransferProgress.connect(fileTransferProgressCallback)
currentRoom.fileTransferCompleted.connect(completedCallback)
onClicked: {
var fileTransferProgressCallback = function(id, sent, total) {
inputField.progress = sent / total
}
var completedCallback = function(id, localFile, mxcUrl) {
inputField.progress = 0
currentRoom.fileTransferCompleted.disconnect(fileTransferProgressCallback)
currentRoom.fileTransferCompleted.disconnect(completedCallback)
}
currentRoom.fileTransferProgress.connect(fileTransferProgressCallback)
currentRoom.fileTransferCompleted.connect(completedCallback)
matriqueController.uploadFile(currentRoom)
}
}

View File

@ -1,5 +0,0 @@
import QtQuick 2.9
Item {
}

View File

@ -3,12 +3,18 @@ import QtQuick.Controls 2.2
Page {
property alias lazyLoad: lazyLoadSwitch.checked
property alias asyncMessageDelegate: asyncMessageDelegateSwitch.checked
Column {
Switch {
id: lazyLoadSwitch
text: "Lazy Load at Initial Sync"
text: "Lazy load at initial sync"
}
Switch {
id: asyncMessageDelegateSwitch
text: "Force loading message delegates asynchronously"
}
Button {
text: "Invoke GC"
highlighted: true

View File

@ -33,6 +33,7 @@ ApplicationWindow {
property alias token: matriqueController.token
property alias lazyLoad: settingPage.lazyLoad
property alias asyncMessageDelegate: settingPage.asyncMessageDelegate
property alias darkTheme: settingPage.darkTheme
property alias miniMode: settingPage.miniMode

View File

@ -23,7 +23,6 @@
<file>qml/component/AvatarContainer.qml</file>
<file>qml/form/RoomListForm.qml</file>
<file>qml/component/AudioBubble.qml</file>
<file>qml/form/SettingForm.qml</file>
<file>qml/Setting.qml</file>
<file>qml/form/SettingAccountForm.qml</file>
<file>qml/form/SettingAppearanceForm.qml</file>
@ -32,6 +31,5 @@
<file>qml/form/SettingGeneralForm.qml</file>
<file>qml/component/EmojiPicker.qml</file>
<file>qml/component/EmojiButton.qml</file>
<file>qml/component/DownloadDialog.qml</file>
</qresource>
</RCC>

View File

@ -10,8 +10,8 @@
#include <QClipboard>
#include <QFile>
#include <QFileDialog>
#include <QImage>
#include <QMimeDatabase>
Controller::Controller(QObject* parent) : QObject(parent) {
connect(m_connection, &Connection::connected, this, &Controller::connected);
@ -74,6 +74,20 @@ void Controller::reconnect() {
m_connection->connectWithToken(userID, token, "");
}
void Controller::uploadFile(Room* room) {
if (!room) return;
auto localFile = QFileDialog::getOpenFileUrl(Q_NULLPTR, tr("Save File as"));
if (!localFile.isEmpty()) {
room->uploadFile(localFile.toString(), localFile, getMIME(localFile));
QMetaObject::Connection* const connection = new QMetaObject::Connection;
*connection = connect(room, &Room::fileTransferCompleted,
[=](QString id, QUrl localFile, QUrl mxcUrl) {
disconnect(*connection);
postFile(room, localFile, mxcUrl);
});
}
}
void Controller::postFile(Room* room, const QUrl& localFile,
const QUrl& mxcUrl) {
const QString mime = getMIME(localFile);
@ -90,9 +104,7 @@ void Controller::postFile(Room* room, const QUrl& localFile,
}
QString Controller::getMIME(const QUrl& fileUrl) const {
QMimeDatabase* db = new QMimeDatabase();
const QString mime = db->mimeTypeForFile(fileUrl.toLocalFile()).name();
delete db;
const QString mime = m_db.mimeTypeForFile(fileUrl.toLocalFile()).name();
return mime;
}
@ -125,3 +137,11 @@ void Controller::createDirectChat(const QString& userID) {
void Controller::copyToClipboard(const QString& text) {
m_clipboard->setText(text);
}
void Controller::saveFileAs(Room* room, QString eventId) {
if (!room) return;
auto fileName = QFileDialog::getSaveFileName(
Q_NULLPTR, tr("Save File as"), room->fileNameToDownload(eventId));
if (!fileName.isEmpty())
room->downloadFile(eventId, QUrl::fromLocalFile(fileName));
}

View File

@ -7,6 +7,7 @@
#include <QObject>
#include <QApplication>
#include <QMimeDatabase>
using namespace QMatrixClient;
@ -84,11 +85,15 @@ class Controller : public QObject {
private:
QClipboard* m_clipboard = QApplication::clipboard();
QMimeDatabase m_db;
void connected();
void resync();
void reconnect();
QString getMIME(const QUrl& fileUrl) const;
void postFile(Room* room, const QUrl& localFile, const QUrl& mxcUrl);
signals:
void connectionChanged();
void isLoginChanged();
@ -99,13 +104,13 @@ class Controller : public QObject {
void errorOccured();
public slots:
void postFile(Room* room, const QUrl& localFile, const QUrl& mxcUrl);
QString getMIME(const QUrl& fileUrl) const;
void uploadFile(Room* room);
void forgetRoom(const QString& roomID);
void joinRoom(const QString& alias);
void createRoom(const QString& name, const QString& topic);
void createDirectChat(const QString& userID);
void copyToClipboard(const QString& text);
void saveFileAs(Room* room, QString eventId);
};
#endif // CONTROLLER_H