1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

drag&drop support

This commit is contained in:
krepa098 2014-09-25 18:03:25 +02:00
parent 458636e6d6
commit ad5a175f7d
8 changed files with 99 additions and 22 deletions

View File

@ -19,6 +19,9 @@
#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
#include <QPushButton> #include <QPushButton>
#include <QMimeData>
#include <QFileInfo>
#include <QDragEnterEvent>
#include "chatform.h" #include "chatform.h"
#include "friend.h" #include "friend.h"
#include "widget/friendwidget.h" #include "widget/friendwidget.h"
@ -52,6 +55,8 @@ ChatForm::ChatForm(Friend* chatFriend)
connect(micButton, SIGNAL(clicked()), this, SLOT(onMicMuteToggle())); connect(micButton, SIGNAL(clicked()), this, SLOT(onMicMuteToggle()));
connect(chatWidget, &ChatAreaWidget::onFileTranfertInterract, this, &ChatForm::onFileTansBtnClicked); connect(chatWidget, &ChatAreaWidget::onFileTranfertInterract, this, &ChatForm::onFileTansBtnClicked);
connect(Core::getInstance(), &Core::fileSendFailed, this, &ChatForm::onFileSendFailed); connect(Core::getInstance(), &Core::fileSendFailed, this, &ChatForm::onFileSendFailed);
setAcceptDrops(true);
} }
ChatForm::~ChatForm() ChatForm::~ChatForm()
@ -472,3 +477,23 @@ void ChatForm::onAvatarChange(int FriendId, const QPixmap &pic)
avatarLabel->setPixmap(pic); avatarLabel->setPixmap(pic);
} }
void ChatForm::dragEnterEvent(QDragEnterEvent *ev)
{
if (ev->mimeData()->hasUrls())
ev->acceptProposedAction();
}
void ChatForm::dropEvent(QDropEvent *ev)
{
if (ev->mimeData()->hasUrls())
{
for (QUrl url : ev->mimeData()->urls())
{
QFileInfo info(url.path());
if (info.exists())
Core::getInstance()->sendFile(f->friendId, info.fileName(), info.absoluteFilePath(), info.size());
}
}
}

View File

@ -69,6 +69,11 @@ private slots:
void onFileTansBtnClicked(QString widgetName, QString buttonName); void onFileTansBtnClicked(QString widgetName, QString buttonName);
void onFileSendFailed(int FriendId, const QString &fname); void onFileSendFailed(int FriendId, const QString &fname);
protected:
// drag & drop
void dragEnterEvent(QDragEnterEvent* ev);
void dropEvent(QDropEvent* ev);
private: private:
Friend* f; Friend* f;
CroppingLabel *statusMessageLabel; CroppingLabel *statusMessageLabel;

View File

@ -27,12 +27,12 @@
#include "widget/chatareawidget.h" #include "widget/chatareawidget.h"
#include "widget/tool/chattextedit.h" #include "widget/tool/chattextedit.h"
GenericChatForm::GenericChatForm(QObject *parent) : GenericChatForm::GenericChatForm(QWidget *parent) :
QObject(parent) QWidget(parent)
{ {
curRow = 0; curRow = 0;
mainWidget = new QWidget(); headWidget = new QWidget(); headWidget = new QWidget();
nameLabel = new CroppingLabel(); nameLabel = new CroppingLabel();
avatarLabel = new QLabel(); avatarLabel = new QLabel();
@ -84,7 +84,7 @@ GenericChatForm::GenericChatForm(QObject *parent) :
micButton->setObjectName("green"); micButton->setObjectName("green");
micButton->setStyleSheet(micButtonStylesheet); micButton->setStyleSheet(micButtonStylesheet);
mainWidget->setLayout(mainLayout); setLayout(mainLayout);
mainLayout->addWidget(chatWidget); mainLayout->addWidget(chatWidget);
mainLayout->addLayout(mainFootLayout); mainLayout->addLayout(mainFootLayout);
mainLayout->setMargin(0); mainLayout->setMargin(0);
@ -129,10 +129,10 @@ void GenericChatForm::setName(const QString &newName)
void GenericChatForm::show(Ui::MainWindow &ui) void GenericChatForm::show(Ui::MainWindow &ui)
{ {
ui.mainContent->layout()->addWidget(mainWidget); ui.mainContent->layout()->addWidget(this);
ui.mainHead->layout()->addWidget(headWidget); ui.mainHead->layout()->addWidget(headWidget);
mainWidget->show();
headWidget->show(); headWidget->show();
QWidget::show();
} }
void GenericChatForm::onChatContextMenuRequested(QPoint pos) void GenericChatForm::onChatContextMenuRequested(QPoint pos)
@ -172,12 +172,6 @@ void GenericChatForm::addMessage(QString author, QString message, QDateTime date
previousName = author; previousName = author;
} }
GenericChatForm::~GenericChatForm()
{
delete mainWidget;
delete headWidget;
}
void GenericChatForm::onEmoteButtonClicked() void GenericChatForm::onEmoteButtonClicked()
{ {
// don't show the smiley selection widget if there are no smileys available // don't show the smiley selection widget if there are no smileys available

View File

@ -17,7 +17,7 @@
#ifndef GENERICCHATFORM_H #ifndef GENERICCHATFORM_H
#define GENERICCHATFORM_H #define GENERICCHATFORM_H
#include <QObject> #include <QWidget>
#include <QPoint> #include <QPoint>
#include <QDateTime> #include <QDateTime>
@ -35,12 +35,11 @@ namespace Ui {
class MainWindow; class MainWindow;
} }
class GenericChatForm : public QObject class GenericChatForm : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
GenericChatForm(QObject *parent = 0); GenericChatForm(QWidget *parent = 0);
virtual ~GenericChatForm();
virtual void setName(const QString &newName); virtual void setName(const QString &newName);
virtual void show(Ui::MainWindow &ui); virtual void show(Ui::MainWindow &ui);
@ -62,7 +61,7 @@ protected slots:
protected: protected:
CroppingLabel *nameLabel; CroppingLabel *nameLabel;
QLabel *avatarLabel; QLabel *avatarLabel;
QWidget *mainWidget, *headWidget; QWidget *headWidget;
QPushButton *fileButton, *emoteButton, *callButton, *videoButton, *volButton, *micButton; QPushButton *fileButton, *emoteButton, *callButton, *videoButton, *volButton, *micButton;
QVBoxLayout *headTextLayout; QVBoxLayout *headTextLayout;
ChatTextEdit *msgEdit; ChatTextEdit *msgEdit;

View File

@ -19,7 +19,10 @@
#include "widget/groupwidget.h" #include "widget/groupwidget.h"
#include "widget/tool/chattextedit.h" #include "widget/tool/chattextedit.h"
#include "widget/croppinglabel.h" #include "widget/croppinglabel.h"
#include "core.h"
#include <QPushButton> #include <QPushButton>
#include <QMimeData>
#include <QDragEnterEvent>
GroupChatForm::GroupChatForm(Group* chatGroup) GroupChatForm::GroupChatForm(Group* chatGroup)
: group(chatGroup) : group(chatGroup)
@ -58,11 +61,8 @@ GroupChatForm::GroupChatForm(Group* chatGroup)
connect(sendButton, SIGNAL(clicked()), this, SLOT(onSendTriggered())); connect(sendButton, SIGNAL(clicked()), this, SLOT(onSendTriggered()));
connect(msgEdit, SIGNAL(enterPressed()), this, SLOT(onSendTriggered())); connect(msgEdit, SIGNAL(enterPressed()), this, SLOT(onSendTriggered()));
}
GroupChatForm::~GroupChatForm()
{
setAcceptDrops(true);
} }
void GroupChatForm::onSendTriggered() void GroupChatForm::onSendTriggered()
@ -94,3 +94,19 @@ void GroupChatForm::onUserListChanged()
names.chop(2); names.chop(2);
namesList->setText(names); namesList->setText(names);
} }
void GroupChatForm::dragEnterEvent(QDragEnterEvent *ev)
{
if (ev->mimeData()->hasFormat("friend"))
ev->acceptProposedAction();
}
void GroupChatForm::dropEvent(QDropEvent *ev)
{
if (ev->mimeData()->hasFormat("friend"))
{
int friendId = ev->mimeData()->data("friend").toInt();
Core::getInstance()->groupInviteFriend(friendId, group->groupId);
}
}

View File

@ -27,13 +27,18 @@ class GroupChatForm : public GenericChatForm
Q_OBJECT Q_OBJECT
public: public:
GroupChatForm(Group* chatGroup); GroupChatForm(Group* chatGroup);
~GroupChatForm();
void addGroupMessage(QString message, int peerId); void addGroupMessage(QString message, int peerId);
void onUserListChanged(); void onUserListChanged();
private slots: private slots:
void onSendTriggered(); void onSendTriggered();
protected:
// drag & drop
void dragEnterEvent(QDragEnterEvent* ev);
void dropEvent(QDropEvent* ev);
private: private:
Group* group; Group* group;
QLabel *nusersLabel, *namesList; QLabel *nusersLabel, *namesList;

View File

@ -24,6 +24,9 @@
#include "widget/form/chatform.h" #include "widget/form/chatform.h"
#include <QContextMenuEvent> #include <QContextMenuEvent>
#include <QMenu> #include <QMenu>
#include <QDrag>
#include <QMimeData>
#include <QApplication>
FriendWidget::FriendWidget(int FriendId, QString id) FriendWidget::FriendWidget(int FriendId, QString id)
: friendId(FriendId), isDefaultAvatar{true} : friendId(FriendId), isDefaultAvatar{true}
@ -203,3 +206,28 @@ void FriendWidget::onAvatarChange(int FriendId, const QPixmap& pic)
QPixmap scaled = pic.scaled(40,40,Qt::KeepAspectRatio,Qt::SmoothTransformation); QPixmap scaled = pic.scaled(40,40,Qt::KeepAspectRatio,Qt::SmoothTransformation);
avatar.setPixmap(scaled); avatar.setPixmap(scaled);
} }
void FriendWidget::mousePressEvent(QMouseEvent *ev)
{
if (ev->button() == Qt::LeftButton)
dragStartPos = ev->pos();
}
void FriendWidget::mouseMoveEvent(QMouseEvent *ev)
{
if (!(ev->buttons() & Qt::LeftButton))
return;
if ((dragStartPos - ev->pos()).manhattanLength() > QApplication::startDragDistance())
{
QDrag* drag = new QDrag(this);
QMimeData* mdata = new QMimeData;
mdata->setData("friend", QString::number(friendId).toLatin1());
drag->setMimeData(mdata);
if (avatar.pixmap())
drag->setPixmap(*avatar.pixmap());
drag->exec(Qt::CopyAction | Qt::MoveAction);
}
}

View File

@ -44,11 +44,16 @@ signals:
public slots: public slots:
void onAvatarChange(int FriendId, const QPixmap& pic); void onAvatarChange(int FriendId, const QPixmap& pic);
protected:
void mousePressEvent(QMouseEvent* ev);
void mouseMoveEvent(QMouseEvent* ev);
public: public:
int friendId; int friendId;
QLabel avatar, statusPic; QLabel avatar, statusPic;
CroppingLabel name, statusMessage; CroppingLabel name, statusMessage;
bool isDefaultAvatar; bool isDefaultAvatar;
QPoint dragStartPos;
}; };
#endif // FRIENDWIDGET_H #endif // FRIENDWIDGET_H