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

Store circle widgets, fixed related bugs

This commit is contained in:
Daniel Hrabovcak 2015-06-03 16:18:40 -04:00 committed by tux3
parent 10c7a745c5
commit 715ef0ce65
14 changed files with 176 additions and 53 deletions

View File

@ -311,7 +311,10 @@ void Settings::loadPersonnal(Profile* profile)
for (int i = 0; i < size; i ++)
{
ps.setArrayIndex(i);
circleLst.push_back(ps.value("name").toString());
circleProp cp;
cp.name = ps.value("name").toString();
cp.expanded = ps.value("expanded", true).toBool();
circleLst.push_back(cp);
}
ps.endArray();
ps.endGroup();
@ -479,7 +482,8 @@ void Settings::savePersonal(QString profileName, QString password)
for (auto& circle : circleLst)
{
ps.setArrayIndex(index);
ps.setValue("name", circle);
ps.setValue("name", circle.name);
ps.setValue("expanded", circle.expanded);
index++;
}
ps.endArray();
@ -1291,6 +1295,14 @@ int Settings::getFriendCircleIndex(const ToxId &id) const
return -1;
}
void Settings::setFriendCircleIndex(const ToxId &id, int index)
{
QString key = id.publicKey;
auto it = friendLst.find(key);
if (it != friendLst.end())
it->circleIndex = index;
}
void Settings::removeFriendSettings(const ToxId &id)
{
QMutexLocker locker{&bigLock};
@ -1341,7 +1353,40 @@ int Settings::getCircleCount() const
QString Settings::getCircleName(int index) const
{
return circleLst[index];
return circleLst[index].name;
}
void Settings::setCircleName(int index, const QString &name)
{
circleLst[index].name = name;
}
int Settings::addCircle(const QString &name)
{
circleProp cp;
cp.name = name;
cp.expanded = false;
circleLst.append(cp);
return circleLst.count() - 1;
}
bool Settings::getCircleExpanded(int index) const
{
return circleLst[index].expanded;
}
void Settings::setCircleExpanded(int index, bool expanded)
{
circleLst[index].expanded = expanded;
}
int Settings::removeCircle(int index)
{
// Replace index with last one and remove last one instead.
// This gives you contiguous ids all the time.
circleLst[index] = circleLst.last();
circleLst.pop_back();
return circleLst.count();
}
int Settings::getThemeColor() const

View File

@ -247,8 +247,11 @@ public:
int getCircleCount() const;
int addCircle(const QString &name);
int removeCircle(int index);
QString getCircleName(int index) const;
void setCircleName(int index);
void setCircleName(int index, const QString &name);
bool getCircleExpanded(int index) const;
void setCircleExpanded(int index, bool expanded);
// Assume all widgets have unique names
// Don't use it to save every single thing you want to save, use it
@ -368,9 +371,15 @@ private:
int circleIndex;
};
struct circleProp
{
QString name;
bool expanded;
};
QHash<QString, friendProp> friendLst;
QVector<QString> circleLst;
QVector<circleProp> circleLst;
int themeColor;

View File

@ -23,6 +23,7 @@
#include <QBoxLayout>
#include <QMouseEvent>
#include <QLineEdit>
#include "src/misc/settings.h"
#include <QDragEnterEvent>
#include <QMimeData>
@ -44,8 +45,11 @@ void maxCropLabel(CroppingLabel* label)
label->setMaximumWidth(metrics.width(label->fullText()) + metrics.width("..."));
}
CircleWidget::CircleWidget(FriendListWidget *parent)
QHash<int, CircleWidget*> CircleWidget::circleList;
CircleWidget::CircleWidget(FriendListWidget *parent, int id_)
: GenericChatItemWidget(parent)
, id(id_)
{
setStyleSheet(Style::getStylesheet(":/ui/chatroomWidgets/circleWidget.css"));
@ -59,9 +63,6 @@ CircleWidget::CircleWidget(FriendListWidget *parent)
statusLabel->setTextFormat(Qt::PlainText);
// name text
nameLabel = new CroppingLabel(this);
nameLabel->setObjectName("name");
nameLabel->setTextFormat(Qt::PlainText);
nameLabel->setText("Circle");
arrowLabel = new QLabel(this);
@ -82,23 +83,52 @@ CircleWidget::CircleWidget(FriendListWidget *parent)
setAcceptDrops(true);
listWidget = new QWidget(this);
fullLayout->addWidget(listWidget);
listWidget->setLayout(listLayout);
listWidget->setVisible(false);
onCompactChanged(isCompact());
if (id != -1)
{
// Set name before connecting text change.
setName(Settings::getInstance().getCircleName(id));
}
connect(nameLabel, &CroppingLabel::textChanged, [this](const QString &newName, const QString &oldName)
{
if (isCompact())
maxCropLabel(nameLabel);
nameLabel->setText(oldName);
emit renameRequested(newName);
Settings::getInstance().setCircleName(id, newName);
});
renameCircle();
bool isNew = false;
auto circleIt = circleList.find(id);
if (circleIt == circleList.end())
{
if (id == -1)
{
isNew = true;
id = Settings::getInstance().addCircle("Circle");
}
}
circleList.insert(id, this);
if (isNew)
renameCircle();
if (Settings::getInstance().getCircleExpanded(id))
expand();
}
void CircleWidget::addFriendWidget(FriendWidget *w, Status s)
{
listLayout->addFriendWidget(w, s);
updateStatus();
Settings::getInstance().setFriendCircleIndex(FriendList::findFriend(w->friendId)->getToxId(), id);
}
void CircleWidget::expand()
@ -111,14 +141,14 @@ void CircleWidget::expand()
void CircleWidget::toggle()
{
expanded = !expanded;
listWidget->setVisible(expanded);
Settings::getInstance().setCircleExpanded(id, expanded);
if (expanded)
{
fullLayout->addLayout(listLayout);
arrowLabel->setPixmap(QPixmap(":/ui/chatArea/scrollBarDownArrow.svg"));
}
else
{
fullLayout->removeItem(listLayout);
arrowLabel->setPixmap(QPixmap(":/ui/chatArea/scrollBarRightArrow.svg"));
}
}
@ -238,12 +268,12 @@ bool CircleWidget::cycleContacts(FriendWidget *activeChatroomWidget, bool forwar
return false;
}
bool CircleWidget::operator<(const CircleWidget& other) const
CircleWidget* CircleWidget::getFromID(int id)
{
int compareValue = nameLabel->text().localeAwareCompare(other.nameLabel->text());
if (compareValue == 0)
return this < &other; // Consistent ordering.
return compareValue > 0;
auto circleIt = circleList.find(id);
if (circleIt != circleList.end())
return circleIt.value();
return nullptr;
}
void CircleWidget::onCompactChanged(bool _compact)
@ -319,6 +349,13 @@ void CircleWidget::contextMenuEvent(QContextMenuEvent *event)
listLayout->moveFriendWidgets(friendList);
friendList->removeCircleWidget(this);
circleList.remove(id);
int replacedCircle = Settings::getInstance().removeCircle(id);
auto circleReplace = circleList.find(replacedCircle);
if (circleReplace != circleList.end())
circleReplace.value()->updateID(id);
}
}
@ -376,3 +413,30 @@ void CircleWidget::updateStatus()
{
statusLabel->setText(QString::number(listLayout->friendOnlineCount()) + QStringLiteral(" / ") + QString::number(listLayout->friendTotalCount()));
}
void CircleWidget::updateID(int index)
{
// For when a circle gets destroyed, another takes its id.
// This function updates all friends widgets.
id = index;
circleList[id] = this;
for (int i = 0; i < listLayout->getLayoutOnline()->count(); ++i)
{
FriendWidget* friendWidget = dynamic_cast<FriendWidget*>(listLayout->getLayoutOnline()->itemAt(i));
if (friendWidget != nullptr)
{
qDebug() << "My yolo slow";
Settings::getInstance().setFriendCircleIndex(FriendList::findFriend(friendWidget->friendId)->getToxId(), id);
}
}
for (int i = 0; i < listLayout->getLayoutOffline()->count(); ++i)
{
FriendWidget* friendWidget = dynamic_cast<FriendWidget*>(listLayout->getLayoutOffline()->itemAt(i));
if (friendWidget != nullptr)
{
qDebug() << "My yolo slow";
Settings::getInstance().setFriendCircleIndex(FriendList::findFriend(friendWidget->friendId)->getToxId(), id);
}
}
}

View File

@ -31,7 +31,7 @@ class CircleWidget : public GenericChatItemWidget
{
Q_OBJECT
public:
CircleWidget(FriendListWidget *parent = 0);
CircleWidget(FriendListWidget *parent = 0, int id = -1);
void addFriendWidget(FriendWidget *w, Status s);
@ -48,7 +48,7 @@ public:
bool cycleContacts(bool forward);
bool cycleContacts(FriendWidget* activeChatroomWidget, bool forward);
bool operator<(const CircleWidget& other) const;
static CircleWidget* getFromID(int id);
signals:
void renameRequested(const QString &newName);
@ -67,11 +67,14 @@ protected:
void dropEvent(QDropEvent* event) override;
private:
void updateID(int index);
static QHash<int, CircleWidget*> circleList;
enum FriendLayoutType
{
Online = 0,
Offline = 1
};
int id;
bool expanded = false;
FriendListLayout* listLayout;
QVBoxLayout* fullLayout;
@ -81,6 +84,7 @@ private:
QFrame* lineFrame;
QWidget* container;
QHBoxLayout* topLayout = nullptr;
QWidget* listWidget;
};
#endif // CIRCLEWIDGET_H

View File

@ -43,8 +43,8 @@ FriendListLayout::FriendListLayout()
void FriendListLayout::addFriendWidget(FriendWidget *w, Status s)
{
// bug somewhere here.
friendOfflineLayout.removeSortedWidget(w);
friendOnlineLayout.removeSortedWidget(w);
friendOfflineLayout.getLayout()->removeWidget(w);
friendOnlineLayout.getLayout()->removeWidget(w);
if (s == Status::Offline)
{
friendOfflineLayout.addSortedWidget(w);

View File

@ -28,6 +28,7 @@
#include "groupwidget.h"
#include "circlewidget.h"
#include "friendlistlayout.h"
#include "src/misc/settings.h"
#include <cassert>
FriendListWidget::FriendListWidget(QWidget *parent, bool groupsOnTop)
@ -59,26 +60,22 @@ void FriendListWidget::addGroupWidget(GroupWidget *widget)
void FriendListWidget::addFriendWidget(FriendWidget *w, Status s, int circleIndex)
{
CircleWidget *circleWidget = nullptr;
if (circleIndex >= 0 && circleIndex < circleLayout2.getLayout()->count())
circleWidget = dynamic_cast<CircleWidget*>(circleLayout2.getLayout()->itemAt(circleIndex)->widget());
if (circleWidget == nullptr)
circleIndex = -1;
if (circleIndex == -1)
moveWidget(w, s, true);
else
circleWidget->addFriendWidget(w, s);
{
CircleWidget::getFromID(circleIndex)->addFriendWidget(w, s);
CircleWidget::getFromID(circleIndex)->show();
}
}
void FriendListWidget::addCircleWidget(const QString &name)
void FriendListWidget::addCircleWidget(int id)
{
CircleWidget *circleWidget = new CircleWidget(this);
circleWidget->setName(name);
CircleWidget *circleWidget = new CircleWidget(this, id);
circleLayout2.addSortedWidget(circleWidget);
connect(this, &FriendListWidget::onCompactChanged, circleWidget, &CircleWidget::onCompactChanged);
connect(circleWidget, &CircleWidget::renameRequested, this, &FriendListWidget::renameCircleWidget);
//ircleWidget->show(); // Avoid flickering.
}
void FriendListWidget::addCircleWidget(FriendWidget *friendWidget)
@ -318,10 +315,11 @@ void FriendListWidget::dropEvent(QDropEvent *event)
void FriendListWidget::moveWidget(FriendWidget *w, Status s, bool add)
{
CircleWidget *circleWidget = dynamic_cast<CircleWidget*>(w->parent());
CircleWidget *circleWidget = dynamic_cast<CircleWidget*>(w->parentWidget());
if (circleWidget == nullptr || add)
{
Settings::getInstance().setFriendCircleIndex(FriendList::findFriend(w->friendId)->getToxId(), -1);
listLayout->addFriendWidget(w, s);
return;
}

View File

@ -49,7 +49,7 @@ public:
void addGroupWidget(GroupWidget *widget);
void addFriendWidget(FriendWidget *w, Status s, int circleIndex);
void addCircleWidget(const QString &name);
void addCircleWidget(int id);
void addCircleWidget(FriendWidget *widget = nullptr);
void removeCircleWidget(CircleWidget *widget);

View File

@ -22,6 +22,10 @@ GenericChatItemWidget::GenericChatItemWidget(QWidget *parent)
: QFrame(parent)
{
setProperty("compact", Settings::getInstance().getCompactLayout());
nameLabel = new CroppingLabel(this);
nameLabel->setObjectName("name");
nameLabel->setTextFormat(Qt::PlainText);
}
bool GenericChatItemWidget::isCompact() const
@ -43,7 +47,7 @@ QString GenericChatItemWidget::getName() const
bool GenericChatItemWidget::operator<(const GenericChatItemWidget& other) const
{
int compareValue = getName().localeAwareCompare(other.getName());
if (compareValue == 0)
return this < &other; // Consistent ordering.
return compareValue > 0;
//if (compareValue == 0)
// return this < &other; // Consistent ordering.
return compareValue < 0;
}

View File

@ -42,7 +42,7 @@ GenericChatroomWidget::GenericChatroomWidget(QWidget *parent)
// name text
nameLabel = new CroppingLabel(this);
nameLabel->setTextFormat(Qt::PlainText);
nameLabel->setForegroundRole(QPalette::WindowText);
nameLabel->setForegroundRole(QPalette::WindowText);*/
setAutoFillBackground(true);
reloadTheme();

View File

@ -68,6 +68,8 @@ GroupWidget::GroupWidget(int GroupId, QString Name)
*/
this->repaint();
});
nameLabel->editStart();
}
void GroupWidget::contextMenuEvent(QContextMenuEvent * event)
@ -138,14 +140,6 @@ QString GroupWidget::getStatusString()
return "New Message";
}
bool GroupWidget::operator<(const GroupWidget& other) const
{
int compareValue = nameLabel->text().localeAwareCompare(other.nameLabel->text());
if (compareValue == 0)
return this < &other; // Consistent ordering.
return compareValue > 0;
}
void GroupWidget::setChatForm(Ui::MainWindow &ui)
{
Group* g = GroupList::findGroup(groupId);

View File

@ -38,8 +38,6 @@ public:
void setName(const QString& name);
void onUserListChanged();
bool operator<(const GroupWidget& other) const;
signals:
void groupWidgetClicked(GroupWidget* widget);
void renameRequested(const QString& newName);

View File

@ -57,8 +57,8 @@ template <typename T, QBoxLayout::Direction Dir>
void SortingBoxLayout<T, Dir>::addSortedWidget(T* widget)
{
int closest = indexOfClosestSortedWidget(widget);
qDebug() << closest;
layout->insertWidget(closest, widget);
qDebug() << "closest: " << closest;
}
template <typename T, QBoxLayout::Direction Dir>
@ -111,16 +111,16 @@ QLayout* SortingBoxLayout<T, Dir>::getLayout() const
template <typename T, QBoxLayout::Direction Dir>
int SortingBoxLayout<T, Dir>::indexOfClosestSortedWidget(T* widget) const
{
qDebug() << "USING 6";
// Binary search: Deferred test of equality.
int min = 0, max = layout->count(), mid;
while (min < max)
{
qDebug() << "min";
mid = (max - min) / 2 + min;
T* atMid = dynamic_cast<T*>(layout->itemAt(mid)->widget());
assert(atMid != nullptr);
if (*widget < *atMid)
if (*atMid < *widget)
min = mid + 1;
else
max = mid;

View File

@ -21,6 +21,8 @@
#include <QResizeEvent>
#include <QLineEdit>
#include <QTimer>
CroppingLabel::CroppingLabel(QWidget* parent)
: QLabel(parent)
, blockPaintEvents(false)
@ -156,8 +158,10 @@ void CroppingLabel::showTextEdit()
{
blockPaintEvents = true;
textEdit->show();
textEdit->setFocus();
//textEdit->setFocus();
textEdit->setText(origText);
// Set focus when event loop is free.
QTimer::singleShot(0, textEdit, SLOT(setFocus()));
}
QString CroppingLabel::fullText()

View File

@ -236,7 +236,10 @@ void Widget::init()
show();
for (int i = 0; i < Settings::getInstance().getCircleCount(); ++i)
contactListWidget->addCircleWidget(Settings::getInstance().getCircleName(i));
{
qDebug() << "HELELEL";
contactListWidget->addCircleWidget(i);
}
}
bool Widget::eventFilter(QObject *obj, QEvent *event)