Skip to content

Commit

Permalink
ui: Add drag and drop functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
yvbbrjdr committed May 17, 2021
1 parent 6f7bb77 commit 5247da3
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 55 deletions.
3 changes: 3 additions & 0 deletions LANDrop/LANDrop.pro
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ SOURCES += \
filetransferserver.cpp \
filetransfersession.cpp \
main.cpp \
selectfilesdialog.cpp \
sendtodialog.cpp \
settings.cpp \
settingsdialog.cpp \
Expand All @@ -26,6 +27,7 @@ HEADERS += \
filetransfersender.h \
filetransferserver.h \
filetransfersession.h \
selectfilesdialog.h \
sendtodialog.h \
settings.h \
settingsdialog.h \
Expand All @@ -34,6 +36,7 @@ HEADERS += \
FORMS += \
aboutdialog.ui \
filetransferdialog.ui \
selectfilesdialog.ui \
sendtodialog.ui \
settingsdialog.ui

Expand Down
Binary file modified LANDrop/locales/LANDrop.zh_CN.qm
Binary file not shown.
63 changes: 41 additions & 22 deletions LANDrop/locales/LANDrop.zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,45 @@ Would you like to receive it?</source>
<translation>你的系统需要支持托盘图标。</translation>
</message>
</context>
<context>
<name>SelectFilesDialog</name>
<message>
<location filename="../selectfilesdialog.ui" line="17"/>
<location filename="../selectfilesdialog.cpp" line="92"/>
<source>Select File(s) to be Sent</source>
<translation>请选择想要发送的文件</translation>
</message>
<message>
<location filename="../selectfilesdialog.ui" line="23"/>
<source>You can drag files to this window:</source>
<translation>你可以拖拽文件至此窗口:</translation>
</message>
<message>
<location filename="../selectfilesdialog.ui" line="42"/>
<source>Add...</source>
<translation>添加...</translation>
</message>
<message>
<location filename="../selectfilesdialog.ui" line="49"/>
<source>Remove</source>
<translation>移除</translation>
</message>
<message>
<location filename="../selectfilesdialog.cpp" line="68"/>
<source>Unable to open file %1. Skipping.</source>
<translation>无法打开文件 %1。已跳过该文件。</translation>
</message>
<message>
<location filename="../selectfilesdialog.cpp" line="74"/>
<source>%1 is not a regular file. Skipping.</source>
<translation>%1 不是一个普通文件。已跳过该文件。</translation>
</message>
<message>
<location filename="../selectfilesdialog.cpp" line="119"/>
<source>No file to be sent.</source>
<translation>无文件可发送。</translation>
</message>
</context>
<context>
<name>SendToDialog</name>
<message>
Expand All @@ -240,12 +279,12 @@ Would you like to receive it?</source>
<translation>端口</translation>
</message>
<message>
<location filename="../sendtodialog.cpp" line="112"/>
<location filename="../sendtodialog.cpp" line="111"/>
<source>Invalid port. Please enter a number between 1 and 65535.</source>
<translation>无效端口。请输入一个 1 到 65535 的数字。</translation>
</message>
<message>
<location filename="../sendtodialog.cpp" line="157"/>
<location filename="../sendtodialog.cpp" line="156"/>
<source>Connection timed out</source>
<translation>连接超时</translation>
</message>
Expand Down Expand Up @@ -320,25 +359,5 @@ Would you like to receive it?</source>
<source> is launched here.</source>
<translation> 已隐藏于托盘。</translation>
</message>
<message>
<location filename="../trayicon.cpp" line="92"/>
<source>Select File(s) to be Sent</source>
<translation>请选择想要发送的文件</translation>
</message>
<message>
<location filename="../trayicon.cpp" line="101"/>
<source>Unable to open file %1. Skipping.</source>
<translation>无法打开文件 %1。已跳过该文件。</translation>
</message>
<message>
<location filename="../trayicon.cpp" line="107"/>
<source>%1 is not a regular file. Skipping.</source>
<translation>%1 不是一个普通文件。已跳过该文件。</translation>
</message>
<message>
<location filename="../trayicon.cpp" line="115"/>
<source>No file to be sent.</source>
<translation>无文件可发送。</translation>
</message>
</context>
</TS>
142 changes: 142 additions & 0 deletions LANDrop/selectfilesdialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2021, LANDrop
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <QDragEnterEvent>
#include <QDropEvent>
#include <QFileDialog>
#include <QMessageBox>
#include <QMimeData>

#include "selectfilesdialog.h"
#include "sendtodialog.h"
#include "ui_selectfilesdialog.h"

SelectFilesDialog::SelectFilesDialog(QWidget *parent, DiscoveryService &discoveryService) :
QDialog(parent), ui(new Ui::SelectFilesDialog), discoveryService(discoveryService)
{
ui->setupUi(this);
setWindowFlag(Qt::WindowStaysOnTopHint);
connect(ui->addButton, &QPushButton::clicked, this, &SelectFilesDialog::addButtonClicked);
connect(ui->removeButton, &QPushButton::clicked, this, &SelectFilesDialog::removeButtonClicked);
ui->filesListView->setModel(&filesStringListModel);
}

SelectFilesDialog::~SelectFilesDialog()
{
delete ui;
}

void SelectFilesDialog::addFile(const QString &filename)
{
foreach (QSharedPointer<QFile> file, files) {
if (file->fileName() == filename)
return;
}

QSharedPointer<QFile> fp = QSharedPointer<QFile>::create(filename);
if (!fp->open(QIODevice::ReadOnly)) {
QMessageBox::critical(this, QApplication::applicationName(),
tr("Unable to open file %1. Skipping.")
.arg(filename));
return;
}
if (fp->isSequential()) {
QMessageBox::critical(this, QApplication::applicationName(),
tr("%1 is not a regular file. Skipping.")
.arg(filename));
return;
}
files.append(fp);
}

void SelectFilesDialog::updateFileStringListModel()
{
QStringList l;
foreach (QSharedPointer<QFile> file, files) {
l.append(QFileInfo(*file).fileName());
}
filesStringListModel.setStringList(l);
}

void SelectFilesDialog::addButtonClicked()
{
QStringList filenames = QFileDialog::getOpenFileNames(nullptr, tr("Select File(s) to be Sent"));
if (filenames.empty())
return;

foreach (const QString &filename, filenames) {
addFile(filename);
}

updateFileStringListModel();
}

void SelectFilesDialog::removeButtonClicked()
{
QModelIndexList indexes = ui->filesListView->selectionModel()->selectedIndexes();
QList<const QSharedPointer<QFile> *> removeList;
foreach (const QModelIndex &i, indexes) {
removeList.append(&files.at(i.row()));
}
foreach (const QSharedPointer<QFile> *fp, removeList) {
files.removeOne(*fp);
}
updateFileStringListModel();
}

void SelectFilesDialog::accept()
{
if (files.empty()) {
QMessageBox::warning(this, QApplication::applicationName(), tr("No file to be sent."));
return;
}

SendToDialog *d = new SendToDialog(nullptr, files, discoveryService);
d->setAttribute(Qt::WA_DeleteOnClose);
d->show();

done(Accepted);
}

void SelectFilesDialog::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasUrls())
event->acceptProposedAction();
}

void SelectFilesDialog::dropEvent(QDropEvent *event)
{
foreach (const QUrl &url, event->mimeData()->urls()) {
addFile(url.toLocalFile());
}
updateFileStringListModel();
}
64 changes: 64 additions & 0 deletions LANDrop/selectfilesdialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2021, LANDrop
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include <QDialog>
#include <QFile>
#include <QStringListModel>

#include "discoveryservice.h"

namespace Ui {
class SelectFilesDialog;
}

class SelectFilesDialog : public QDialog {
Q_OBJECT
public:
explicit SelectFilesDialog(QWidget *parent, DiscoveryService &discoveryService);
~SelectFilesDialog();
private:
Ui::SelectFilesDialog *ui;
DiscoveryService &discoveryService;
QList<QSharedPointer<QFile>> files;
QStringListModel filesStringListModel;
void addFile(const QString &filename);
void updateFileStringListModel();
private slots:
void addButtonClicked();
void removeButtonClicked();
void accept();
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
};
Loading

0 comments on commit 5247da3

Please sign in to comment.