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

refactor: ContentDialog refactoring

This commit is contained in:
Diadlo 2017-02-26 17:22:28 +03:00
parent c7202c8b24
commit add8d51a29
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
6 changed files with 319 additions and 268 deletions

View File

@ -7,7 +7,7 @@
class QFile; class QFile;
class QTimer; class QTimer;
enum class Status : int enum class Status
{ {
Online = 0, Online = 0,
Away, Away,

View File

@ -44,38 +44,50 @@
#include "src/widget/translator.h" #include "src/widget/translator.h"
#include "tool/adjustingscrollarea.h" #include "tool/adjustingscrollarea.h"
QString ContentDialog::username = "";
ContentDialog* ContentDialog::currentDialog = nullptr; ContentDialog* ContentDialog::currentDialog = nullptr;
QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>> ContentDialog::friendList; QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>> ContentDialog::friendList;
QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>> ContentDialog::groupList; QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>> ContentDialog::groupList;
static const int minWidget = 220;
static const int minHeight = 220;
static const QSize minSize(minHeight, minWidget);
static const QSize defaultSize(720, 400);
ContentDialog::ContentDialog(SettingsWidget* settingsWidget, QWidget* parent) ContentDialog::ContentDialog(SettingsWidget* settingsWidget, QWidget* parent)
: ActivateDialog(parent, Qt::Window) : ActivateDialog(parent, Qt::Window)
, splitter{new QSplitter(this)}
, friendLayout{new FriendListLayout(this)}
, activeChatroomWidget(nullptr) , activeChatroomWidget(nullptr)
, settingsWidget(settingsWidget) , settingsWidget(settingsWidget)
, videoSurfaceSize(QSize()) , videoSurfaceSize(QSize())
, videoCount(0) , videoCount(0)
{ {
QVBoxLayout* boxLayout = new QVBoxLayout(this); const Settings& s = Settings::getInstance();
boxLayout->setMargin(0);
boxLayout->setSpacing(0);
splitter = new QSplitter(this);
setStyleSheet(Style::getStylesheet(":/ui/contentDialog/contentDialog.css")); setStyleSheet(Style::getStylesheet(":/ui/contentDialog/contentDialog.css"));
splitter->setHandleWidth(6);
friendLayout->setMargin(0);
friendLayout->setSpacing(0);
layouts = {
friendLayout->getLayoutOnline(),
groupLayout.getLayout(),
friendLayout->getLayoutOffline()
};
if (s.getGroupchatPosition()) {
layouts.swap(0, 1);
}
QWidget* friendWidget = new QWidget(); QWidget* friendWidget = new QWidget();
friendWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); friendWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
friendWidget->setAutoFillBackground(true); friendWidget->setAutoFillBackground(true);
friendLayout = new FriendListLayout();
friendLayout->setMargin(0);
friendLayout->setSpacing(0);
friendWidget->setLayout(friendLayout); friendWidget->setLayout(friendLayout);
onGroupchatPositionChanged(Settings::getInstance().getGroupchatPosition()); onGroupchatPositionChanged(s.getGroupchatPosition());
QScrollArea* friendScroll = new QScrollArea(this); QScrollArea* friendScroll = new QScrollArea(this);
friendScroll->setMinimumWidth(220); friendScroll->setMinimumWidth(minWidget);
friendScroll->setFrameStyle(QFrame::NoFrame); friendScroll->setFrameStyle(QFrame::NoFrame);
friendScroll->setLayoutDirection(Qt::RightToLeft); friendScroll->setLayoutDirection(Qt::RightToLeft);
friendScroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); friendScroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
@ -85,6 +97,7 @@ ContentDialog::ContentDialog(SettingsWidget* settingsWidget, QWidget* parent)
QWidget* contentWidget = new QWidget(this); QWidget* contentWidget = new QWidget(this);
contentWidget->setAutoFillBackground(true); contentWidget->setAutoFillBackground(true);
contentLayout = new ContentLayout(contentWidget); contentLayout = new ContentLayout(contentWidget);
contentLayout->setMargin(0); contentLayout->setMargin(0);
contentLayout->setSpacing(0); contentLayout->setSpacing(0);
@ -93,13 +106,13 @@ ContentDialog::ContentDialog(SettingsWidget* settingsWidget, QWidget* parent)
splitter->addWidget(contentWidget); splitter->addWidget(contentWidget);
splitter->setStretchFactor(1, 1); splitter->setStretchFactor(1, 1);
splitter->setCollapsible(1, false); splitter->setCollapsible(1, false);
QVBoxLayout* boxLayout = new QVBoxLayout(this);
boxLayout->setMargin(0);
boxLayout->setSpacing(0);
boxLayout->addWidget(splitter); boxLayout->addWidget(splitter);
const Settings& s = Settings::getInstance(); setMinimumSize(minSize);
connect(&s, &Settings::groupchatPositionChanged, this, &ContentDialog::onGroupchatPositionChanged);
connect(splitter, &QSplitter::splitterMoved, this, &ContentDialog::saveSplitterState);
setMinimumSize(500, 220);
setAttribute(Qt::WA_DeleteOnClose); setAttribute(Qt::WA_DeleteOnClose);
QByteArray geometry = s.getDialogGeometry(); QByteArray geometry = s.getDialogGeometry();
@ -107,12 +120,11 @@ ContentDialog::ContentDialog(SettingsWidget* settingsWidget, QWidget* parent)
if (!geometry.isNull()) { if (!geometry.isNull()) {
restoreGeometry(geometry); restoreGeometry(geometry);
} else { } else {
resize(720, 400); resize(defaultSize);
} }
QByteArray splitterState = s.getDialogSplitterState();
SplitterRestorer restorer(splitter); SplitterRestorer restorer(splitter);
restorer.restore(splitterState, size()); restorer.restore(s.getDialogSettingsGeometry(), size());
currentDialog = this; currentDialog = this;
setAcceptDrops(true); setAcceptDrops(true);
@ -123,35 +135,30 @@ ContentDialog::ContentDialog(SettingsWidget* settingsWidget, QWidget* parent)
new QShortcut(Qt::CTRL + Qt::Key_PageUp, this, SLOT(previousContact())); new QShortcut(Qt::CTRL + Qt::Key_PageUp, this, SLOT(previousContact()));
new QShortcut(Qt::CTRL + Qt::Key_PageDown, this, SLOT(nextContact())); new QShortcut(Qt::CTRL + Qt::Key_PageDown, this, SLOT(nextContact()));
connect(Core::getInstance(), &Core::usernameSet, this, &ContentDialog::updateTitleAndStatusIcon); connect(&s, &Settings::groupchatPositionChanged, this, &ContentDialog::onGroupchatPositionChanged);
connect(splitter, &QSplitter::splitterMoved, this, &ContentDialog::saveSplitterState);
Translator::registerHandler(std::bind(&ContentDialog::retranslateUi, this), this); Translator::registerHandler(std::bind(&ContentDialog::retranslateUi, this), this);
} }
void ContentDialog::removeCurrent(QHash<int, ContactInfo>& infos) {
for (auto it = infos.begin(); it != infos.end(); ) {
if (std::get<0>(*it) == this) {
it = infos.erase(it);
} else {
++it;
}
}
}
ContentDialog::~ContentDialog() ContentDialog::~ContentDialog()
{ {
if (currentDialog == this) if (currentDialog == this) {
currentDialog = nullptr; currentDialog = nullptr;
auto friendIt = friendList.begin();
while (friendIt != friendList.end()) {
if (std::get<0>(friendIt.value()) == this) {
friendIt = friendList.erase(friendIt);
continue;
}
++friendIt;
} }
auto groupIt = groupList.begin(); removeCurrent(friendList);
removeCurrent(groupList);
while (groupIt != groupList.end()) {
if (std::get<0>(groupIt.value()) == this) {
groupIt = groupList.erase(groupIt);
continue;
}
++groupIt;
}
Translator::unregister(this); Translator::unregister(this);
} }
@ -159,24 +166,19 @@ ContentDialog::~ContentDialog()
FriendWidget* ContentDialog::addFriend(int friendId, QString id) FriendWidget* ContentDialog::addFriend(int friendId, QString id)
{ {
FriendWidget* friendWidget = new FriendWidget(friendId, id); FriendWidget* friendWidget = new FriendWidget(friendId, id);
friendLayout->addFriendWidget(friendWidget, FriendList::findFriend(friendId)->getStatus());
Friend* frnd = friendWidget->getFriend(); Friend* frnd = friendWidget->getFriend();
friendLayout->addFriendWidget(friendWidget, frnd->getStatus());
connect(frnd, &Friend::aliasChanged, this, &ContentDialog::updateFriendWidget); connect(frnd, &Friend::aliasChanged, this, &ContentDialog::updateFriendWidget);
connect(friendWidget, &FriendWidget::chatroomWidgetClicked, this, connect(friendWidget, &FriendWidget::chatroomWidgetClicked, this,
&ContentDialog::onChatroomWidgetClicked); &ContentDialog::onChatroomWidgetClicked);
connect(friendWidget, SIGNAL(chatroomWidgetClicked(GenericChatroomWidget*)), connect(friendWidget, SIGNAL(chatroomWidgetClicked(GenericChatroomWidget*)),
frnd->getChatForm(), SLOT(focusInput())); frnd->getChatForm(), SLOT(focusInput()));
connect(Core::getInstance(), &Core::friendAvatarChanged, friendWidget,
&FriendWidget::onAvatarChange);
connect(Core::getInstance(), &Core::friendAvatarRemoved, friendWidget,
&FriendWidget::onAvatarRemoved);
ContentDialog* lastDialog = getFriendDialog(friendId); ContentDialog* lastDialog = getFriendDialog(friendId);
if (lastDialog) {
if (lastDialog != nullptr)
lastDialog->removeFriend(friendId); lastDialog->removeFriend(friendId);
}
friendList.insert(friendId, std::make_tuple(this, friendWidget)); friendList.insert(friendId, std::make_tuple(this, friendWidget));
// FIXME: emit should be removed // FIXME: emit should be removed
@ -198,8 +200,9 @@ GroupWidget* ContentDialog::addGroup(int groupId, const QString& name)
ContentDialog* lastDialog = getGroupDialog(groupId); ContentDialog* lastDialog = getGroupDialog(groupId);
if (lastDialog != nullptr) if (lastDialog) {
lastDialog->removeGroup(groupId); lastDialog->removeGroup(groupId);
}
groupList.insert(groupId, std::make_tuple(this, groupWidget)); groupList.insert(groupId, std::make_tuple(this, groupWidget));
// FIXME: emit should be removed // FIXME: emit should be removed
@ -211,17 +214,18 @@ GroupWidget* ContentDialog::addGroup(int groupId, const QString& name)
void ContentDialog::removeFriend(int friendId) void ContentDialog::removeFriend(int friendId)
{ {
auto iter = friendList.find(friendId); auto iter = friendList.find(friendId);
if (iter == friendList.end()) {
if (iter == friendList.end())
return; return;
}
FriendWidget* chatroomWidget = static_cast<FriendWidget*>(std::get<1>(iter.value())); FriendWidget* chatroomWidget = static_cast<FriendWidget*>(std::get<1>(iter.value()));
disconnect(chatroomWidget->getFriend(), &Friend::aliasChanged, this, disconnect(chatroomWidget->getFriend(), &Friend::aliasChanged, this,
&ContentDialog::updateFriendWidget); &ContentDialog::updateFriendWidget);
// Need to find replacement to show here instead. // Need to find replacement to show here instead.
if (activeChatroomWidget == chatroomWidget) if (activeChatroomWidget == chatroomWidget) {
cycleContacts(true, false); cycleContacts(true, false);
}
friendLayout->removeFriendWidget(chatroomWidget, Status::Offline); friendLayout->removeFriendWidget(chatroomWidget, Status::Offline);
friendLayout->removeFriendWidget(chatroomWidget, Status::Online); friendLayout->removeFriendWidget(chatroomWidget, Status::Online);
@ -248,15 +252,16 @@ void ContentDialog::removeGroup(int groupId)
} }
auto iter = groupList.find(groupId); auto iter = groupList.find(groupId);
if (iter == groupList.end()) {
if (iter == groupList.end())
return; return;
}
GenericChatroomWidget* chatroomWidget = std::get<1>(iter.value()); GenericChatroomWidget* chatroomWidget = std::get<1>(iter.value());
// Need to find replacement to show here instead. // Need to find replacement to show here instead.
if (activeChatroomWidget == chatroomWidget) if (activeChatroomWidget == chatroomWidget) {
cycleContacts(true, false); cycleContacts(true, false);
}
groupLayout.removeSortedWidget(chatroomWidget); groupLayout.removeSortedWidget(chatroomWidget);
chatroomWidget->deleteLater(); chatroomWidget->deleteLater();
@ -288,75 +293,90 @@ int ContentDialog::chatroomWidgetCount() const
void ContentDialog::ensureSplitterVisible() void ContentDialog::ensureSplitterVisible()
{ {
if (splitter->sizes().at(0) == 0) if (splitter->sizes().at(0) == 0) {
splitter->setSizes({1, 1}); splitter->setSizes({1, 1});
}
update(); update();
} }
void ContentDialog::cycleContacts(bool forward, bool loop) int ContentDialog::getCurrentLayout(QLayout*& layout)
{ {
Settings::getInstance().getGroupchatPosition(); layout = friendLayout->getLayoutOnline();
int index = friendLayout->indexOfFriendWidget(activeChatroomWidget, true);
if (index != -1) {
return index;
}
int index; layout = friendLayout->getLayoutOffline();
QLayout* currentLayout;
if (activeChatroomWidget->getFriend()) {
currentLayout = friendLayout->getLayoutOnline();
index = friendLayout->indexOfFriendWidget(activeChatroomWidget, true);
if (index == -1) {
currentLayout = friendLayout->getLayoutOffline();
index = friendLayout->indexOfFriendWidget(activeChatroomWidget, false); index = friendLayout->indexOfFriendWidget(activeChatroomWidget, false);
} if (index != -1) {
} else { return index;
currentLayout = groupLayout.getLayout();
index = groupLayout.indexOfSortedWidget(activeChatroomWidget);
} }
if (!loop && index == currentLayout->count() - 1) { layout = groupLayout.getLayout();
index = groupLayout.indexOfSortedWidget(activeChatroomWidget);
if (index != -1) {
return index;
}
layout = nullptr;
return -1;
}
void ContentDialog::cycleContacts(bool forward, bool inverse)
{
QLayout* currentLayout;
int index = getCurrentLayout(currentLayout);
if (!currentLayout || index == -1) {
return;
}
if (!inverse && index == currentLayout->count() - 1) {
bool groupsOnTop = Settings::getInstance().getGroupchatPosition(); bool groupsOnTop = Settings::getInstance().getGroupchatPosition();
bool offlineEmpty = friendLayout->getLayoutOffline()->isEmpty(); bool offlineEmpty = friendLayout->getLayoutOffline()->isEmpty();
bool onlineEmpty = bool onlineEmpty = friendLayout->getLayoutOnline()->isEmpty();
offlineEmpty && (friendLayout->getLayoutOnline()->isEmpty() || !groupsOnTop); bool groupsEmpty = groupLayout.getLayout()->isEmpty();
bool groupsEmpty = offlineEmpty && (groupLayout.getLayout()->isEmpty() || groupsOnTop); bool isCurOffline = currentLayout == friendLayout->getLayoutOffline();
bool isCurOnline = currentLayout == friendLayout->getLayoutOnline();
bool isCurGroup = currentLayout == groupLayout.getLayout();
bool nextIsEmpty = isCurOnline && offlineEmpty && (groupsEmpty || groupsOnTop) ||
isCurGroup && offlineEmpty && (onlineEmpty || !groupsOnTop) ||
isCurOffline;
if ((currentLayout == friendLayout->getLayoutOffline()) if (nextIsEmpty) {
|| (currentLayout == friendLayout->getLayoutOnline() && groupsEmpty)
|| (currentLayout == groupLayout.getLayout() && onlineEmpty)) {
forward = !forward; forward = !forward;
} }
} }
index += forward ? 1 : -1; index += forward ? 1 : -1;
// If goes out of the layout, then go to the next and skip empty. This loop goes more
for (;;) { // then 1 time, because for empty layout index will be out of interval (0 < 0 || 0 >= 0)
// Bounds checking. while (index < 0 || index >= currentLayout->count()) {
int oldCount = currentLayout->count();
currentLayout = nextLayout(currentLayout, forward);
int newCount = currentLayout->count();
if (index < 0) { if (index < 0) {
currentLayout = nextLayout(currentLayout, forward); index = newCount - 1;
index = currentLayout->count() - 1; } else if (index >= oldCount) {
continue;
} else if (index >= currentLayout->count()) {
currentLayout = nextLayout(currentLayout, forward);
index = 0; index = 0;
continue; }
} }
GenericChatroomWidget* chatWidget = QWidget* widget = currentLayout->itemAt(index)->widget();
qobject_cast<GenericChatroomWidget*>(currentLayout->itemAt(index)->widget()); GenericChatroomWidget* chatWidget = qobject_cast<GenericChatroomWidget*>(widget);
if (chatWidget && chatWidget != activeChatroomWidget) {
if (chatWidget != nullptr && chatWidget != activeChatroomWidget) {
// FIXME: emit should be removed // FIXME: emit should be removed
emit chatWidget->chatroomWidgetClicked(chatWidget, false); emit chatWidget->chatroomWidgetClicked(chatWidget, false);
} }
return;
}
} }
void ContentDialog::onVideoShow(QSize size) void ContentDialog::onVideoShow(QSize size)
{ {
++videoCount; ++videoCount;
if (videoCount > 1) if (videoCount > 1) {
return; return;
}
videoSurfaceSize = size; videoSurfaceSize = size;
QSize minSize = minimumSize(); QSize minSize = minimumSize();
@ -366,8 +386,9 @@ void ContentDialog::onVideoShow(QSize size)
void ContentDialog::onVideoHide() void ContentDialog::onVideoHide()
{ {
videoCount--; videoCount--;
if (videoCount > 0) if (videoCount > 0) {
return; return;
}
QSize minSize = minimumSize(); QSize minSize = minimumSize();
setMinimumSize(minSize - videoSurfaceSize); setMinimumSize(minSize - videoSurfaceSize);
@ -379,25 +400,37 @@ ContentDialog* ContentDialog::current()
return currentDialog; return currentDialog;
} }
bool ContentDialog::existsFriendWidget(int friendId, bool focus) bool ContentDialog::existsFriendWidget(int friendId)
{ {
return existsWidget(friendId, focus, friendList); return existsWidget(friendId, friendList);
} }
bool ContentDialog::existsGroupWidget(int groupId, bool focus) bool ContentDialog::existsGroupWidget(int groupId)
{ {
return existsWidget(groupId, focus, groupList); return existsWidget(groupId, groupList);
}
void ContentDialog::focusFriend(int friendId)
{
focusDialog(friendId, friendList);
}
void ContentDialog::focusGroup(int groupId)
{
focusDialog(groupId, groupList);
} }
void ContentDialog::updateFriendStatus(int friendId) void ContentDialog::updateFriendStatus(int friendId)
{ {
updateStatus(friendId, friendList); updateStatus(friendId, friendList);
ContentDialog* contentDialog = getFriendDialog(friendId); ContentDialog* contentDialog = getFriendDialog(friendId);
if (contentDialog != nullptr) { if (contentDialog) {
FriendWidget* friendWidget = auto iter = friendList.find(friendId).value();
static_cast<FriendWidget*>(std::get<1>(friendList.find(friendId).value())); GenericChatroomWidget* widget = std::get<1>(iter);
contentDialog->friendLayout->addFriendWidget(friendWidget, FriendWidget* friendWidget = static_cast<FriendWidget*>(widget);
FriendList::findFriend(friendId)->getStatus());
Friend* f = FriendList::findFriend(friendId);
contentDialog->friendLayout->addFriendWidget(friendWidget, f->getStatus());
} }
} }
@ -405,8 +438,9 @@ void ContentDialog::updateFriendStatusMessage(int friendId, const QString& messa
{ {
auto iter = friendList.find(friendId); auto iter = friendList.find(friendId);
if (iter == friendList.end()) if (iter == friendList.end()) {
return; return;
}
std::get<1>(iter.value())->setStatusMsg(message); std::get<1>(iter.value())->setStatusMsg(message);
} }
@ -436,42 +470,43 @@ ContentDialog* ContentDialog::getGroupDialog(int groupId)
return getDialog(groupId, groupList); return getDialog(groupId, groupList);
} }
void ContentDialog::updateTitleAndStatusIcon(const QString& username) void ContentDialog::updateTitleAndStatusIcon()
{ {
if (displayWidget != nullptr) { if (!activeChatroomWidget) {
setWindowTitle(username);
return;
}
setWindowTitle(displayWidget->getTitle() + QStringLiteral(" - ") + username); setWindowTitle(activeChatroomWidget->getTitle() + QStringLiteral(" - ") + username);
// it's null when it's a groupchat bool isGroupchat = activeChatroomWidget->getGroup() != nullptr;
if (displayWidget->getFriend() == nullptr) { if (isGroupchat) {
setWindowIcon(QIcon(":/img/group.svg")); setWindowIcon(QIcon(":/img/group.svg"));
return; return;
} }
Status currentStatus = displayWidget->getFriend()->getStatus(); Status currentStatus = activeChatroomWidget->getFriend()->getStatus();
switch (currentStatus) { QMap<Status, QIcon> icons {
case Status::Online: {Status::Online, QIcon(":/img/status/dot_online.svg")},
setWindowIcon(QIcon(":/img/status/dot_online.svg")); {Status::Away, QIcon(":/img/status/dot_away.svg")},
break; {Status::Busy, QIcon(":/img/status/dot_busy.svg")},
case Status::Away: {Status::Offline, QIcon(":/img/status/dot_offline.svg")}
setWindowIcon(QIcon(":/img/status/dot_away.svg")); };
break;
case Status::Busy: setWindowIcon(icons[currentStatus]);
setWindowIcon(QIcon(":/img/status/dot_busy.svg"));
break;
case Status::Offline:
setWindowIcon(QIcon(":/img/status/dot_offline.svg"));
break;
}
} else
setWindowTitle(username);
} }
void ContentDialog::updateTitle(GenericChatroomWidget* chatroomWidget) /**
* @brief Update layouts order according to settings.
* @param groupOnTop If true move groupchat layout on the top. Move under online otherwise.
*/
void ContentDialog::reorderLayouts(bool newGroupOnTop)
{ {
displayWidget = chatroomWidget; bool oldGroupOnTop = layouts.first() == groupLayout.getLayout();
updateTitleAndStatusIcon(Core::getInstance()->getUsername()); if (newGroupOnTop != oldGroupOnTop) {
layouts.swap(0, 1);
}
} }
void ContentDialog::previousContact() void ContentDialog::previousContact()
@ -484,14 +519,21 @@ void ContentDialog::nextContact()
cycleContacts(true); cycleContacts(true);
} }
void ContentDialog::setUsername(const QString& newName)
{
username = newName;
updateTitleAndStatusIcon();
}
bool ContentDialog::event(QEvent* event) bool ContentDialog::event(QEvent* event)
{ {
switch (event->type()) { switch (event->type()) {
case QEvent::WindowActivate: case QEvent::WindowActivate:
if (activeChatroomWidget != nullptr) { if (activeChatroomWidget) {
activeChatroomWidget->resetEventFlags(); activeChatroomWidget->resetEventFlags();
activeChatroomWidget->updateStatusLight(); activeChatroomWidget->updateStatusLight();
updateTitle(activeChatroomWidget);
updateTitleAndStatusIcon();
Friend* frnd = activeChatroomWidget->getFriend(); Friend* frnd = activeChatroomWidget->getFriend();
Group* group = activeChatroomWidget->getGroup(); Group* group = activeChatroomWidget->getGroup();
@ -523,28 +565,33 @@ void ContentDialog::dragEnterEvent(QDragEnterEvent* event)
if (frnd) { if (frnd) {
ToxId toxId(event->mimeData()->text()); ToxId toxId(event->mimeData()->text());
Friend* contact = FriendList::findFriend(toxId.getPublicKey()); Friend* contact = FriendList::findFriend(toxId.getPublicKey());
if (!contact) if (!contact) {
return; return;
}
int friendId = contact->getFriendId(); int friendId = contact->getFriendId();
auto iter = friendList.find(friendId); auto iter = friendList.find(friendId);
// If friend is already in a dialog then you can't drop friend where it already is. // If friend is already in a dialog then you can't drop friend where it already is.
if (iter == friendList.end() || std::get<0>(iter.value()) != this) if (iter == friendList.end() || std::get<0>(iter.value()) != this) {
event->acceptProposedAction(); event->acceptProposedAction();
}
} else if (group) { } else if (group) {
if (!event->mimeData()->hasFormat("groupId")) if (!event->mimeData()->hasFormat("groupId")) {
return; return;
}
int groupId = event->mimeData()->data("groupId").toInt(); int groupId = event->mimeData()->data("groupId").toInt();
Group* contact = GroupList::findGroup(groupId); Group* contact = GroupList::findGroup(groupId);
if (!contact) if (!contact) {
return; return;
}
auto iter = groupList.find(groupId); auto iter = groupList.find(groupId);
if (iter == groupList.end() || std::get<0>(iter.value()) != this) if (iter == groupList.end() || std::get<0>(iter.value()) != this) {
event->acceptProposedAction(); event->acceptProposedAction();
} }
}
} }
void ContentDialog::dropEvent(QDropEvent* event) void ContentDialog::dropEvent(QDropEvent* event)
@ -555,28 +602,33 @@ void ContentDialog::dropEvent(QDropEvent* event)
if (frnd) { if (frnd) {
ToxId toxId(event->mimeData()->text()); ToxId toxId(event->mimeData()->text());
Friend* contact = FriendList::findFriend(toxId.getPublicKey()); Friend* contact = FriendList::findFriend(toxId.getPublicKey());
if (!contact) if (!contact) {
return; return;
}
int friendId = contact->getFriendId(); int friendId = contact->getFriendId();
auto iter = friendList.find(friendId); auto iter = friendList.find(friendId);
if (iter != friendList.end()) if (iter != friendList.end()) {
std::get<0>(iter.value())->removeFriend(friendId); std::get<0>(iter.value())->removeFriend(friendId);
}
Widget::getInstance()->addFriendDialog(contact, this); Widget::getInstance()->addFriendDialog(contact, this);
ensureSplitterVisible(); ensureSplitterVisible();
} else if (group) { } else if (group) {
if (!event->mimeData()->hasFormat("groupId")) if (!event->mimeData()->hasFormat("groupId")) {
return; return;
}
int groupId = event->mimeData()->data("groupId").toInt(); int groupId = event->mimeData()->data("groupId").toInt();
Group* contact = GroupList::findGroup(groupId); Group* contact = GroupList::findGroup(groupId);
if (!contact) if (!contact) {
return; return;
}
auto iter = friendList.find(groupId); auto iter = friendList.find(groupId);
if (iter != friendList.end()) if (iter != friendList.end()) {
std::get<0>(iter.value())->removeGroup(groupId); std::get<0>(iter.value())->removeGroup(groupId);
}
Widget::getInstance()->addGroupDialog(contact, this); Widget::getInstance()->addGroupDialog(contact, this);
ensureSplitterVisible(); ensureSplitterVisible();
@ -587,9 +639,10 @@ void ContentDialog::changeEvent(QEvent* event)
{ {
QWidget::changeEvent(event); QWidget::changeEvent(event);
if (event->type() == QEvent::ActivationChange) { if (event->type() == QEvent::ActivationChange) {
if (isActiveWindow()) if (isActiveWindow()) {
currentDialog = this; currentDialog = this;
} }
}
} }
void ContentDialog::resizeEvent(QResizeEvent* event) void ContentDialog::resizeEvent(QResizeEvent* event)
@ -606,8 +659,10 @@ void ContentDialog::moveEvent(QMoveEvent* event)
void ContentDialog::keyPressEvent(QKeyEvent* event) void ContentDialog::keyPressEvent(QKeyEvent* event)
{ {
if (event->key() != Qt::Key_Escape) // Ignore escape keyboard shortcut.
QDialog::keyPressEvent(event); // Ignore escape keyboard shortcut. if (event->key() != Qt::Key_Escape) {
QDialog::keyPressEvent(event);
}
} }
void ContentDialog::onChatroomWidgetClicked(GenericChatroomWidget* widget, bool group) void ContentDialog::onChatroomWidgetClicked(GenericChatroomWidget* widget, bool group)
@ -616,7 +671,7 @@ void ContentDialog::onChatroomWidgetClicked(GenericChatroomWidget* widget, bool
ContentDialog* contentDialog = new ContentDialog(settingsWidget); ContentDialog* contentDialog = new ContentDialog(settingsWidget);
contentDialog->show(); contentDialog->show();
if (widget->getFriend() != nullptr) { if (widget->getFriend()) {
removeFriend(widget->getFriend()->getFriendId()); removeFriend(widget->getFriend()->getFriendId());
Widget::getInstance()->addFriendDialog(widget->getFriend(), contentDialog); Widget::getInstance()->addFriendDialog(widget->getFriend(), contentDialog);
} else { } else {
@ -631,13 +686,15 @@ void ContentDialog::onChatroomWidgetClicked(GenericChatroomWidget* widget, bool
} }
// If we clicked on the currently active widget, don't reload and relayout everything // If we clicked on the currently active widget, don't reload and relayout everything
if (activeChatroomWidget == widget) if (activeChatroomWidget == widget) {
return; return;
}
contentLayout->clear(); contentLayout->clear();
if (activeChatroomWidget != nullptr) if (activeChatroomWidget) {
activeChatroomWidget->setAsInactiveChatroom(); activeChatroomWidget->setAsInactiveChatroom();
}
activeChatroomWidget = widget; activeChatroomWidget = widget;
@ -645,7 +702,8 @@ void ContentDialog::onChatroomWidgetClicked(GenericChatroomWidget* widget, bool
widget->setAsActiveChatroom(); widget->setAsActiveChatroom();
widget->resetEventFlags(); widget->resetEventFlags();
widget->updateStatusLight(); widget->updateStatusLight();
updateTitle(widget);
updateTitleAndStatusIcon();
} }
void ContentDialog::updateFriendWidget(uint32_t friendId, QString alias) void ContentDialog::updateFriendWidget(uint32_t friendId, QString alias)
@ -661,23 +719,23 @@ void ContentDialog::updateFriendWidget(uint32_t friendId, QString alias)
void ContentDialog::updateGroupWidget(GroupWidget* w) void ContentDialog::updateGroupWidget(GroupWidget* w)
{ {
std::get<1>(groupList.find(w->groupId).value())->setName(w->getName()); ContactInfo info = groupList.find(w->groupId).value();
static_cast<GroupWidget*>(std::get<1>(groupList.find(w->groupId).value()))->onUserListChanged(); GroupWidget* widget = static_cast<GroupWidget*>(std::get<1>(info));
QString name = w->getName();
widget->setName(name);
widget->onUserListChanged();
} }
void ContentDialog::onGroupchatPositionChanged(bool top) void ContentDialog::onGroupchatPositionChanged(bool top)
{ {
friendLayout->removeItem(groupLayout.getLayout()); friendLayout->removeItem(groupLayout.getLayout());
friendLayout->insertLayout(top ? 0 : 1, groupLayout.getLayout());
if (top)
friendLayout->insertLayout(0, groupLayout.getLayout());
else
friendLayout->insertLayout(1, groupLayout.getLayout());
} }
void ContentDialog::retranslateUi() void ContentDialog::retranslateUi()
{ {
updateTitleAndStatusIcon(Core::getInstance()->getUsername()); updateTitleAndStatusIcon();
} }
void ContentDialog::saveDialogGeometry() void ContentDialog::saveDialogGeometry()
@ -691,111 +749,86 @@ void ContentDialog::saveSplitterState()
} }
bool ContentDialog::hasWidget(int id, GenericChatroomWidget* chatroomWidget, bool ContentDialog::hasWidget(int id, GenericChatroomWidget* chatroomWidget,
const QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>>& list) const QHash<int, ContactInfo>& list)
{ {
auto iter = list.find(id); auto iter = list.find(id);
if (iter == list.end()) {
if (iter == list.end() || std::get<0>(iter.value()) != this)
return false; return false;
return chatroomWidget == std::get<1>(iter.value());
}
bool ContentDialog::existsWidget(int id, bool focus,
const QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>>& list)
{
auto iter = list.find(id);
if (iter == list.end())
return false;
if (focus) {
if (std::get<0>(iter.value())->windowState() & Qt::WindowMinimized)
std::get<0>(iter.value())->showNormal();
std::get<0>(iter.value())->raise();
std::get<0>(iter.value())->activateWindow();
std::get<0>(iter.value())->onChatroomWidgetClicked(std::get<1>(iter.value()), false);
} }
return true; return std::get<0>(*iter) == this &&
std::get<1>(*iter) == chatroomWidget;
} }
void ContentDialog::updateStatus(int id, void ContentDialog::focusDialog(int id, const QHash<int, ContactInfo>& list)
const QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>>& list)
{ {
auto iter = list.find(id); auto iter = list.find(id);
if (iter == list.end()) {
if (iter == list.end())
return; return;
}
GenericChatroomWidget* chatroomWidget = std::get<1>(iter.value()); ContentDialog* dialog = std::get<0>(*iter);
if (dialog->windowState() & Qt::WindowMinimized) {
dialog->showNormal();
}
dialog->raise();
dialog->activateWindow();
dialog->onChatroomWidgetClicked(std::get<1>(iter.value()), false);
}
bool ContentDialog::existsWidget(int id, const QHash<int, ContactInfo>& list)
{
auto iter = list.find(id);
return iter != list.end();
}
void ContentDialog::updateStatus(int id, const QHash<int, ContactInfo>& list)
{
auto iter = list.find(id);
if (iter == list.end()) {
return;
}
GenericChatroomWidget* chatroomWidget = std::get<1>(*iter);
chatroomWidget->updateStatusLight(); chatroomWidget->updateStatusLight();
if (chatroomWidget->isActive()) if (chatroomWidget->isActive()) {
std::get<0>(iter.value())->updateTitle(chatroomWidget); ContentDialog* dialog = std::get<0>(*iter);
dialog->updateTitleAndStatusIcon();
}
} }
bool ContentDialog::isWidgetActive( bool ContentDialog::isWidgetActive(int id, const QHash<int, ContactInfo>& list)
int id, const QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>>& list)
{ {
auto iter = list.find(id); auto iter = list.find(id);
if (iter == list.end()) {
if (iter == list.end())
return false; return false;
}
return std::get<0>(iter.value())->activeChatroomWidget == std::get<1>(iter.value()); return std::get<0>(iter.value())->activeChatroomWidget == std::get<1>(iter.value());
} }
ContentDialog* ContentDialog* ContentDialog::getDialog(int id, const QHash<int, ContactInfo>& list)
ContentDialog::getDialog(int id,
const QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>>& list)
{ {
auto iter = list.find(id); auto iter = list.find(id);
if (iter == list.end()) {
if (iter == list.end())
return nullptr; return nullptr;
}
return std::get<0>(iter.value()); return std::get<0>(iter.value());
} }
QLayout* ContentDialog::nextLayout(QLayout* layout, bool forward) const QLayout* ContentDialog::nextLayout(QLayout* layout, bool forward) const
{ {
if (layout == groupLayout.getLayout()) { int index = layouts.indexOf(layout);
if (forward) { if (index == -1) {
if (Settings::getInstance().getGroupchatPosition())
return friendLayout->getLayoutOnline();
return friendLayout->getLayoutOffline();
} else {
if (Settings::getInstance().getGroupchatPosition())
return friendLayout->getLayoutOffline();
return friendLayout->getLayoutOnline();
}
} else if (layout == friendLayout->getLayoutOnline()) {
if (forward) {
if (Settings::getInstance().getGroupchatPosition())
return friendLayout->getLayoutOffline();
return groupLayout.getLayout();
} else {
if (Settings::getInstance().getGroupchatPosition())
return groupLayout.getLayout();
return friendLayout->getLayoutOffline();
}
} else if (layout == friendLayout->getLayoutOffline()) {
if (forward) {
if (Settings::getInstance().getGroupchatPosition())
return groupLayout.getLayout();
return friendLayout->getLayoutOnline();
} else {
if (Settings::getInstance().getGroupchatPosition())
return friendLayout->getLayoutOnline();
return groupLayout.getLayout();
}
}
return nullptr; return nullptr;
}
int next = forward ? index + 1 : index - 1;
size_t size = layouts.size();
next = (next + size) % size;
return layouts[next];
} }

View File

@ -33,6 +33,7 @@ class QSet;
class QSplitter; class QSplitter;
class QVBoxLayout; class QVBoxLayout;
class ContentDialog;
class ContentLayout; class ContentLayout;
class GenericChatroomWidget; class GenericChatroomWidget;
class FriendWidget; class FriendWidget;
@ -42,6 +43,8 @@ class SettingsWidget;
class Friend; class Friend;
class Group; class Group;
using ContactInfo = std::tuple<ContentDialog*, GenericChatroomWidget*>;
class ContentDialog : public ActivateDialog class ContentDialog : public ActivateDialog
{ {
Q_OBJECT Q_OBJECT
@ -57,14 +60,17 @@ public:
bool hasGroupWidget(int groupId, GenericChatroomWidget* chatroomWidget); bool hasGroupWidget(int groupId, GenericChatroomWidget* chatroomWidget);
int chatroomWidgetCount() const; int chatroomWidgetCount() const;
void ensureSplitterVisible(); void ensureSplitterVisible();
void updateTitleAndStatusIcon();
void cycleContacts(bool forward, bool loop = true); void cycleContacts(bool forward, bool loop = true);
void onVideoShow(QSize size); void onVideoShow(QSize size);
void onVideoHide(); void onVideoHide();
static ContentDialog* current(); static ContentDialog* current();
static bool existsFriendWidget(int friendId, bool focus); static bool existsFriendWidget(int friendId);
static bool existsGroupWidget(int groupId, bool focus); static bool existsGroupWidget(int groupId);
static void focusFriend(int friendId);
static void focusGroup(int groupId);
static void updateFriendStatus(int friendId); static void updateFriendStatus(int friendId);
static void updateFriendStatusMessage(int friendId, const QString& message); static void updateFriendStatusMessage(int friendId, const QString& message);
static void updateGroupStatus(int groupId); static void updateGroupStatus(int groupId);
@ -79,10 +85,10 @@ signals:
void activated(); void activated();
public slots: public slots:
void updateTitleAndStatusIcon(const QString& username); void reorderLayouts(bool newGroupOnTop);
void updateTitle(GenericChatroomWidget* chatroomWidget);
void previousContact(); void previousContact();
void nextContact(); void nextContact();
void setUsername(const QString& newName);
protected: protected:
bool event(QEvent* event) final override; bool event(QEvent* event) final override;
@ -104,31 +110,31 @@ private:
void saveDialogGeometry(); void saveDialogGeometry();
void saveSplitterState(); void saveSplitterState();
QLayout* nextLayout(QLayout* layout, bool forward) const; QLayout* nextLayout(QLayout* layout, bool forward) const;
int getCurrentLayout(QLayout*& layout);
bool hasWidget(int id, GenericChatroomWidget* chatroomWidget, bool hasWidget(int id, GenericChatroomWidget* chatroomWidget,
const QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>>& list); const QHash<int, ContactInfo>& list);
static bool existsWidget(int id, bool focus, void removeCurrent(QHash<int, ContactInfo>& infos);
const QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>>& list); static bool existsWidget(int id, const QHash<int, ContactInfo>& list);
static void static void focusDialog(int id, const QHash<int, ContactInfo>& list);
updateStatus(int id, const QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>>& list); static void updateStatus(int id, const QHash<int, ContactInfo>& list);
static bool static bool isWidgetActive(int id, const QHash<int, ContactInfo>& list);
isWidgetActive(int id, const QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>>& list); static ContentDialog* getDialog(int id, const QHash<int, ContactInfo>& list);
static ContentDialog*
getDialog(int id, const QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>>& list);
QList<QLayout*> layouts;
QSplitter* splitter; QSplitter* splitter;
FriendListLayout* friendLayout; FriendListLayout* friendLayout;
GenericChatItemLayout groupLayout; GenericChatItemLayout groupLayout;
ContentLayout* contentLayout; ContentLayout* contentLayout;
GenericChatroomWidget* activeChatroomWidget; GenericChatroomWidget* activeChatroomWidget;
GenericChatroomWidget* displayWidget = nullptr;
SettingsWidget* settingsWidget; SettingsWidget* settingsWidget;
QSize videoSurfaceSize; QSize videoSurfaceSize;
int videoCount; int videoCount;
static QString username;
static ContentDialog* currentDialog; static ContentDialog* currentDialog;
static QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>> friendList; static QHash<int, ContactInfo> friendList;
static QHash<int, std::tuple<ContentDialog*, GenericChatroomWidget*>> groupList; static QHash<int, ContactInfo> groupList;
}; };
#endif // CONTENTDIALOG_H #endif // CONTENTDIALOG_H

View File

@ -363,7 +363,12 @@ void FriendWidget::search(const QString& searchString, bool hide)
bool FriendWidget::chatFormIsSet(bool focus) const bool FriendWidget::chatFormIsSet(bool focus) const
{ {
Friend* f = FriendList::findFriend(friendId); Friend* f = FriendList::findFriend(friendId);
return ContentDialog::existsFriendWidget(friendId, focus) || f->getChatForm()->isVisible(); if (focus) {
ContentDialog::focusFriend(friendId);
}
bool exist = ContentDialog::existsFriendWidget(friendId);
return exist || f->getChatForm()->isVisible();
} }
void FriendWidget::setChatForm(ContentLayout* contentLayout) void FriendWidget::setChatForm(ContentLayout* contentLayout)

View File

@ -200,9 +200,13 @@ Group* GroupWidget::getGroup() const
bool GroupWidget::chatFormIsSet(bool focus) const bool GroupWidget::chatFormIsSet(bool focus) const
{ {
(void)focus;
Group* g = GroupList::findGroup(groupId); Group* g = GroupList::findGroup(groupId);
return ContentDialog::existsGroupWidget(groupId, focus) || g->getChatForm()->isVisible(); if (focus) {
ContentDialog::focusGroup(groupId);
}
bool exist = ContentDialog::existsGroupWidget(groupId);
return exist || g->getChatForm()->isVisible();
} }
void GroupWidget::setChatForm(ContentLayout* contentLayout) void GroupWidget::setChatForm(ContentLayout* contentLayout)

View File

@ -1457,16 +1457,19 @@ ContentDialog* Widget::createContentDialog() const
ContentDialog* contentDialog = new ContentDialog(settingsWidget); ContentDialog* contentDialog = new ContentDialog(settingsWidget);
connect(contentDialog, &ContentDialog::friendDialogShown, this, &Widget::onFriendDialogShown); connect(contentDialog, &ContentDialog::friendDialogShown, this, &Widget::onFriendDialogShown);
connect(contentDialog, &ContentDialog::groupDialogShown, this, &Widget::onGroupDialogShown); connect(contentDialog, &ContentDialog::groupDialogShown, this, &Widget::onGroupDialogShown);
connect(Core::getInstance(), &Core::usernameSet, contentDialog, &ContentDialog::setUsername);
Settings& s = Settings::getInstance();
connect(&s, &Settings::groupchatPositionChanged, contentDialog, &ContentDialog::reorderLayouts);
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
connect(contentDialog, &ContentDialog::destroyed, &Nexus::getInstance(), Nexus &n = Nexus::getInstance();
&Nexus::updateWindowsClosed); connect(contentDialog, &ContentDialog::destroyed, &n, &Nexus::updateWindowsClosed);
connect(contentDialog, &ContentDialog::windowStateChanged, &Nexus::getInstance(), connect(contentDialog, &ContentDialog::windowStateChanged, &n, &Nexus::onWindowStateChanged);
&Nexus::onWindowStateChanged); connect(contentDialog->windowHandle(), &QWindow::windowTitleChanged, &n, &Nexus::updateWindows);
connect(contentDialog->windowHandle(), &QWindow::windowTitleChanged, &Nexus::getInstance(), n.updateWindows();
&Nexus::updateWindows);
Nexus::getInstance().updateWindows();
#endif #endif
return contentDialog; return contentDialog;
} }