2015-05-28 01:17:12 +08:00
|
|
|
/*
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
This program is libre software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the COPYING file for more details.
|
|
|
|
*/
|
|
|
|
|
2015-05-28 08:45:23 +08:00
|
|
|
#include "circlewidget.h"
|
2015-05-28 01:17:12 +08:00
|
|
|
#include "src/misc/style.h"
|
|
|
|
#include "src/misc/settings.h"
|
|
|
|
#include "src/friendlist.h"
|
|
|
|
#include "src/friend.h"
|
|
|
|
#include "src/widget/friendwidget.h"
|
|
|
|
#include <QVariant>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QBoxLayout>
|
2015-05-28 22:28:11 +08:00
|
|
|
#include <QMouseEvent>
|
2015-05-29 06:36:21 +08:00
|
|
|
#include <QLineEdit>
|
2015-05-28 01:17:12 +08:00
|
|
|
|
|
|
|
#include <QDragEnterEvent>
|
|
|
|
#include <QMimeData>
|
2015-05-29 06:36:21 +08:00
|
|
|
#include <QMenu>
|
2015-05-28 01:17:12 +08:00
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
2015-05-28 08:45:23 +08:00
|
|
|
#include "friendlistlayout.h"
|
2015-05-29 06:36:21 +08:00
|
|
|
#include "friendlistwidget.h"
|
2015-05-28 08:45:23 +08:00
|
|
|
|
2015-05-29 06:36:21 +08:00
|
|
|
CircleWidget::CircleWidget(FriendListWidget *parent)
|
2015-05-28 08:45:23 +08:00
|
|
|
: GenericChatItemWidget(parent)
|
2015-05-28 01:17:12 +08:00
|
|
|
{
|
|
|
|
setStyleSheet(Style::getStylesheet(":/ui/chatroomWidgets/circleWidget.css"));
|
|
|
|
|
2015-05-28 22:28:11 +08:00
|
|
|
container = new QWidget(this);
|
2015-05-28 01:17:12 +08:00
|
|
|
container->setObjectName("circleWidgetContainer");
|
|
|
|
container->setProperty("active", false);
|
|
|
|
mainLayout = new QVBoxLayout(this);
|
2015-05-29 00:58:44 +08:00
|
|
|
listLayout = new FriendListLayout();
|
2015-05-28 01:17:12 +08:00
|
|
|
QHBoxLayout *layout = new QHBoxLayout();
|
|
|
|
QVBoxLayout *midLayout = new QVBoxLayout;
|
|
|
|
QHBoxLayout *topLayout = new QHBoxLayout;
|
|
|
|
|
|
|
|
this->layout()->setSpacing(0);
|
|
|
|
this->layout()->setMargin(0);
|
|
|
|
container->setFixedHeight(55);
|
|
|
|
setLayoutDirection(Qt::LeftToRight);
|
|
|
|
|
|
|
|
midLayout->addStretch();
|
|
|
|
|
|
|
|
arrowLabel = new QLabel(">", container);
|
|
|
|
arrowLabel->setPixmap(QPixmap(":/ui/chatArea/scrollBarRightArrow.svg"));
|
|
|
|
arrowLabel->setStyleSheet("color: white;");
|
|
|
|
topLayout->addWidget(arrowLabel);
|
|
|
|
topLayout->addSpacing(5);
|
2015-05-29 06:36:21 +08:00
|
|
|
nameLabel = new QLabel("Circle", container);
|
2015-05-28 01:17:12 +08:00
|
|
|
nameLabel->setObjectName("name");
|
|
|
|
topLayout->addWidget(nameLabel);
|
|
|
|
QFrame *lineFrame = new QFrame(container);
|
|
|
|
lineFrame->setObjectName("line");
|
|
|
|
lineFrame->setFrameShape(QFrame::HLine);
|
|
|
|
|
|
|
|
midLayout->addLayout(topLayout);
|
2015-05-29 00:58:44 +08:00
|
|
|
midLayout->addWidget(lineFrame);
|
2015-05-28 01:17:12 +08:00
|
|
|
|
|
|
|
midLayout->addStretch();
|
|
|
|
|
|
|
|
QHBoxLayout *statusLayout = new QHBoxLayout();
|
|
|
|
|
2015-05-29 00:58:44 +08:00
|
|
|
onlineLabel = new QLabel("0", container);
|
2015-05-28 01:17:12 +08:00
|
|
|
onlineLabel->setObjectName("status");
|
|
|
|
|
|
|
|
statusLayout->addWidget(onlineLabel);
|
2015-05-29 21:26:43 +08:00
|
|
|
|
2015-05-29 00:58:44 +08:00
|
|
|
topLayout->addStretch();
|
|
|
|
topLayout->addLayout(statusLayout);
|
2015-05-28 01:17:12 +08:00
|
|
|
|
|
|
|
midLayout->addStretch();
|
|
|
|
|
|
|
|
layout->addSpacing(10);
|
|
|
|
layout->addLayout(midLayout);
|
|
|
|
layout->addSpacing(10);
|
|
|
|
|
|
|
|
container->setLayout(layout);
|
|
|
|
mainLayout->addWidget(container);
|
|
|
|
|
|
|
|
setAcceptDrops(true);
|
|
|
|
}
|
|
|
|
|
2015-05-28 08:45:23 +08:00
|
|
|
void CircleWidget::addFriendWidget(FriendWidget *w, Status s)
|
2015-05-28 01:17:12 +08:00
|
|
|
{
|
2015-05-28 08:45:23 +08:00
|
|
|
listLayout->addFriendWidget(w, s);
|
2015-05-29 00:58:44 +08:00
|
|
|
//if (s == Status::Offline)
|
|
|
|
updateOffline();
|
|
|
|
//else
|
|
|
|
updateOnline();
|
2015-05-28 01:17:12 +08:00
|
|
|
}
|
|
|
|
|
2015-05-29 21:26:43 +08:00
|
|
|
void CircleWidget::expand()
|
|
|
|
{
|
|
|
|
if (expanded)
|
|
|
|
return;
|
|
|
|
toggle();
|
|
|
|
}
|
|
|
|
|
2015-05-28 01:17:12 +08:00
|
|
|
void CircleWidget::toggle()
|
|
|
|
{
|
2015-05-29 00:58:44 +08:00
|
|
|
expanded = !expanded;
|
|
|
|
if (expanded)
|
2015-05-28 01:17:12 +08:00
|
|
|
{
|
2015-05-28 08:45:23 +08:00
|
|
|
mainLayout->addLayout(listLayout);
|
2015-05-28 01:17:12 +08:00
|
|
|
arrowLabel->setPixmap(QPixmap(":/ui/chatArea/scrollBarDownArrow.svg"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-28 08:45:23 +08:00
|
|
|
mainLayout->removeItem(listLayout);
|
2015-05-28 01:17:12 +08:00
|
|
|
arrowLabel->setPixmap(QPixmap(":/ui/chatArea/scrollBarRightArrow.svg"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 22:28:11 +08:00
|
|
|
void CircleWidget::searchChatrooms(const QString &searchString, bool hideOnline, bool hideOffline, bool hideGroups)
|
2015-05-28 01:17:12 +08:00
|
|
|
{
|
2015-05-28 22:28:11 +08:00
|
|
|
listLayout->searchChatrooms(searchString, hideOnline, hideOffline, hideGroups);
|
|
|
|
}
|
|
|
|
|
2015-05-29 21:26:43 +08:00
|
|
|
QString CircleWidget::getName() const
|
|
|
|
{
|
|
|
|
return nameLabel->text();
|
|
|
|
}
|
|
|
|
|
2015-05-29 06:36:21 +08:00
|
|
|
void CircleWidget::renameCircle()
|
|
|
|
{
|
|
|
|
qDebug() << nameLabel->parentWidget()->layout();
|
|
|
|
QLineEdit *lineEdit = new QLineEdit(nameLabel->text());
|
|
|
|
lineEdit->show();
|
|
|
|
//nameLabel->parentWidget()->layout()
|
|
|
|
//static_cast<QBoxLayout*>(nameLabel->parentWidget()->layout())->insertWidget(nameLabel->parentWidget()->layout()->indexOf(nameLabel) - 1, lineEdit);
|
|
|
|
//nameLabel->parentWidget()->layout()->replaceWidget(nameLabel, lineEdit);
|
|
|
|
nameLabel->setVisible(false);
|
|
|
|
lineEdit->selectAll();
|
|
|
|
lineEdit->setFocus();
|
|
|
|
connect(lineEdit, &QLineEdit::editingFinished, [this, lineEdit]()
|
|
|
|
{
|
|
|
|
nameLabel->setVisible(true);
|
|
|
|
//lineEdit->parentWidget()->layout()->replaceWidget(lineEdit, nameLabel);
|
|
|
|
nameLabel->setText(lineEdit->text());
|
|
|
|
lineEdit->deleteLater();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-29 21:26:43 +08:00
|
|
|
void CircleWidget::onCompactChanged(bool compact)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-29 06:36:21 +08:00
|
|
|
void CircleWidget::contextMenuEvent(QContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
QMenu menu;
|
|
|
|
QAction *renameAction = menu.addAction(tr("Rename circle", "Menu for renaming a circle"));
|
|
|
|
QAction *removeAction = menu.addAction(tr("Remove circle", "Menu for removing a circle"));
|
|
|
|
|
|
|
|
QAction *selectedItem = menu.exec(mapToGlobal(event->pos()));
|
|
|
|
if (selectedItem == renameAction)
|
|
|
|
renameCircle();
|
|
|
|
else if (selectedItem == removeAction)
|
|
|
|
{
|
|
|
|
FriendListWidget *friendList = static_cast<FriendListWidget*>(parentWidget());
|
|
|
|
while (listLayout->friendLayouts[Online]->count() != 0)
|
|
|
|
{
|
|
|
|
QWidget *getWidget = listLayout->friendLayouts[Online]->takeAt(0)->widget();
|
|
|
|
assert(getWidget != nullptr);
|
|
|
|
|
|
|
|
FriendWidget *friendWidget = dynamic_cast<FriendWidget*>(getWidget);
|
|
|
|
friendList->moveWidget(friendWidget, FriendList::findFriend(friendWidget->friendId)->getStatus(), true);
|
|
|
|
}
|
|
|
|
while (listLayout->friendLayouts[Offline]->count() != 0)
|
|
|
|
{
|
|
|
|
QWidget *getWidget = listLayout->friendLayouts[Offline]->takeAt(0)->widget();
|
2015-05-29 21:26:43 +08:00
|
|
|
assert(getWidget != nullptr);
|
2015-05-29 06:36:21 +08:00
|
|
|
|
|
|
|
FriendWidget *friendWidget = dynamic_cast<FriendWidget*>(getWidget);
|
|
|
|
friendList->moveWidget(friendWidget, FriendList::findFriend(friendWidget->friendId)->getStatus(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
friendList->removeCircleWidget(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 22:28:11 +08:00
|
|
|
void CircleWidget::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (event->button() == Qt::LeftButton)
|
|
|
|
toggle();
|
2015-05-28 01:17:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CircleWidget::dragEnterEvent(QDragEnterEvent *event)
|
|
|
|
{
|
|
|
|
if (event->mimeData()->hasFormat("friend"))
|
|
|
|
event->acceptProposedAction();
|
2015-05-28 22:28:11 +08:00
|
|
|
container->setAttribute(Qt::WA_UnderMouse, true); // Simulate hover.
|
|
|
|
Style::repolish(container);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CircleWidget::dragLeaveEvent(QDragLeaveEvent *)
|
|
|
|
{
|
|
|
|
container->setAttribute(Qt::WA_UnderMouse, false);
|
|
|
|
Style::repolish(container);
|
2015-05-28 01:17:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CircleWidget::dropEvent(QDropEvent *event)
|
|
|
|
{
|
|
|
|
if (event->mimeData()->hasFormat("friend"))
|
|
|
|
{
|
2015-05-29 00:58:44 +08:00
|
|
|
if (!expanded)
|
2015-05-28 08:45:23 +08:00
|
|
|
toggle();
|
|
|
|
|
2015-05-28 01:17:12 +08:00
|
|
|
int friendId = event->mimeData()->data("friend").toInt();
|
|
|
|
Friend *f = FriendList::findFriend(friendId);
|
|
|
|
assert(f != nullptr);
|
|
|
|
|
|
|
|
FriendWidget *widget = f->getFriendWidget();
|
|
|
|
assert(widget != nullptr);
|
|
|
|
|
2015-05-29 00:58:44 +08:00
|
|
|
// Update old circle after moved.
|
|
|
|
CircleWidget *circleWidget = dynamic_cast<CircleWidget*>(widget->parent());
|
|
|
|
|
|
|
|
addFriendWidget(widget, f->getStatus());
|
|
|
|
|
|
|
|
if (circleWidget != nullptr)
|
|
|
|
{
|
|
|
|
// In case the status was changed while moving, update both.
|
|
|
|
circleWidget->updateOffline();
|
|
|
|
circleWidget->updateOnline();
|
|
|
|
}
|
2015-05-28 22:28:11 +08:00
|
|
|
|
|
|
|
container->setAttribute(Qt::WA_UnderMouse, false);
|
|
|
|
Style::repolish(container);
|
2015-05-28 01:17:12 +08:00
|
|
|
}
|
|
|
|
}
|
2015-05-29 00:58:44 +08:00
|
|
|
|
|
|
|
void CircleWidget::updateOnline()
|
|
|
|
{
|
2015-05-29 06:36:21 +08:00
|
|
|
onlineLabel->setText(QString::number(listLayout->friendOnlineCount()) + QStringLiteral(" / ") + QString::number(listLayout->friendOfflineCount()));
|
2015-05-29 00:58:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CircleWidget::updateOffline()
|
|
|
|
{
|
2015-05-29 06:36:21 +08:00
|
|
|
//offlineLabel->setText(QString::number(listLayout->friendOfflineCount()));
|
2015-05-29 00:58:44 +08:00
|
|
|
}
|