From 27237e61645622d326633f824772651662334d6a Mon Sep 17 00:00:00 2001 From: Daniel Hrabovcak Date: Sat, 30 May 2015 15:10:43 -0400 Subject: [PATCH] Compact settings and minor circle widget cleanup --- src/persistence/settings.cpp | 46 +++++++++ src/persistence/settings.h | 11 +++ src/widget/circlewidget.cpp | 161 +++++++++++++++++++------------- src/widget/circlewidget.h | 19 ++-- src/widget/friendlistlayout.cpp | 2 +- src/widget/friendlistlayout.h | 2 +- src/widget/friendlistwidget.cpp | 29 +++++- src/widget/friendlistwidget.h | 5 +- src/widget/friendwidget.cpp | 9 +- src/widget/widget.cpp | 9 +- 10 files changed, 210 insertions(+), 83 deletions(-) diff --git a/src/persistence/settings.cpp b/src/persistence/settings.cpp index e573448f7..d42dab499 100644 --- a/src/persistence/settings.cpp +++ b/src/persistence/settings.cpp @@ -287,6 +287,7 @@ void Settings::loadPersonnal(Profile* profile) friendLst.clear(); ps.beginGroup("Friends"); int size = ps.beginReadArray("Friend"); + friendLst.reserve(size); for (int i = 0; i < size; i ++) { ps.setArrayIndex(i); @@ -294,6 +295,7 @@ void Settings::loadPersonnal(Profile* profile) fp.addr = ps.value("addr").toString(); fp.alias = ps.value("alias").toString(); fp.autoAcceptDir = ps.value("autoAcceptDir").toString(); + fp.circleIndex = ps.value("circle", -1).toInt(); friendLst[ToxId(fp.addr).publicKey] = fp; } ps.endArray(); @@ -303,6 +305,17 @@ void Settings::loadPersonnal(Profile* profile) compactLayout = ps.value("compactLayout", false).toBool(); ps.endGroup(); + ps.beginGroup("Circles"); + size = ps.beginReadArray("Circle"); + circleLst.reserve(size); + for (int i = 0; i < size; i ++) + { + ps.setArrayIndex(i); + circleLst.push_back(ps.value("name").toString()); + } + ps.endArray(); + ps.endGroup(); + ps.beginGroup("Privacy"); typingNotification = ps.value("typingNotification", false).toBool(); enableLogging = ps.value("enableLogging", false).toBool(); @@ -450,6 +463,7 @@ void Settings::savePersonal(QString profileName, QString password) ps.setValue("addr", frnd.addr); ps.setValue("alias", frnd.alias); ps.setValue("autoAcceptDir", frnd.autoAcceptDir); + ps.setValue("circle", frnd.circleIndex); index++; } ps.endArray(); @@ -459,6 +473,18 @@ void Settings::savePersonal(QString profileName, QString password) ps.setValue("compactLayout", compactLayout); ps.endGroup(); + ps.beginGroup("Circles"); + ps.beginWriteArray("Circle", circleLst.size()); + index = 0; + for (auto& circle : circleLst) + { + ps.setArrayIndex(index); + ps.setValue("name", circle); + index++; + } + ps.endArray(); + ps.endGroup(); + ps.beginGroup("Privacy"); ps.setValue("typingNotification", typingNotification); ps.setValue("enableLogging", enableLogging); @@ -1255,6 +1281,16 @@ void Settings::setFriendAlias(const ToxId &id, const QString &alias) } } +int Settings::getFriendCircleIndex(const ToxId &id) const +{ + QString key = id.publicKey; + auto it = friendLst.find(key); + if (it != friendLst.end()) + return it->circleIndex; + + return -1; +} + void Settings::removeFriendSettings(const ToxId &id) { QMutexLocker locker{&bigLock}; @@ -1298,6 +1334,16 @@ void Settings::setGroupchatPosition(bool value) groupchatPosition = value; } +int Settings::getCircleCount() const +{ + return circleLst.size(); +} + +QString Settings::getCircleName(int index) const +{ + return circleLst[index]; +} + int Settings::getThemeColor() const { QMutexLocker locker{&bigLock}; diff --git a/src/persistence/settings.h b/src/persistence/settings.h index 1810026cc..ea81ce341 100644 --- a/src/persistence/settings.h +++ b/src/persistence/settings.h @@ -228,6 +228,9 @@ public: QString getFriendAlias(const ToxId &id) const; void setFriendAlias(const ToxId &id, const QString &alias); + int getFriendCircleIndex(const ToxId &id) const; + void setFriendCircleIndex(const ToxId &id, int index); + void removeFriendSettings(const ToxId &id); bool getFauxOfflineMessaging() const; @@ -242,6 +245,11 @@ public: bool getAutoLogin() const; void setAutoLogin(bool state); + int getCircleCount() const; + void addCircle(const QString &name); + QString getCircleName(int index) const; + void setCircleName(int index); + // Assume all widgets have unique names // Don't use it to save every single thing you want to save, use it // for some general purpose widgets, such as MainWindows or Splitters, @@ -357,10 +365,13 @@ private: QString alias; QString addr; QString autoAcceptDir; + int circleIndex; }; QHash friendLst; + QVector circleLst; + int themeColor; static QMutex bigLock; diff --git a/src/widget/circlewidget.cpp b/src/widget/circlewidget.cpp index 603325135..75ac7ae36 100644 --- a/src/widget/circlewidget.cpp +++ b/src/widget/circlewidget.cpp @@ -42,66 +42,44 @@ CircleWidget::CircleWidget(FriendListWidget *parent) container = new QWidget(this); container->setObjectName("circleWidgetContainer"); - container->setProperty("active", false); - mainLayout = new QVBoxLayout(this); - listLayout = new FriendListLayout(); - QHBoxLayout *layout = new QHBoxLayout(); - QVBoxLayout *midLayout = new QVBoxLayout; - QHBoxLayout *topLayout = new QHBoxLayout; + container->setLayoutDirection(Qt::LeftToRight); - this->layout()->setSpacing(0); - this->layout()->setMargin(0); - container->setFixedHeight(55); - setLayoutDirection(Qt::LeftToRight); + // status text + statusLabel = new QLabel("0 / 0", this); + statusLabel->setObjectName("status"); + statusLabel->setTextFormat(Qt::PlainText); - midLayout->addStretch(); - - arrowLabel = new QLabel(">", container); - arrowLabel->setPixmap(QPixmap(":/ui/chatArea/scrollBarRightArrow.svg")); - arrowLabel->setStyleSheet("color: white;"); - topLayout->addWidget(arrowLabel); - topLayout->addSpacing(5); - nameLabel = new QLabel("Circle", container); + // name text + nameLabel = new QLabel(this); nameLabel->setObjectName("name"); - topLayout->addWidget(nameLabel); - QFrame *lineFrame = new QFrame(container); + nameLabel->setTextFormat(Qt::PlainText); + nameLabel->setText("Circle"); + + arrowLabel = new QLabel(this); + arrowLabel->setPixmap(QPixmap(":/ui/chatArea/scrollBarRightArrow.svg")); + + fullLayout = new QVBoxLayout(this); + fullLayout->setSpacing(0); + fullLayout->setMargin(0); + fullLayout->addWidget(container); + + lineFrame = new QFrame(container); lineFrame->setObjectName("line"); lineFrame->setFrameShape(QFrame::HLine); - midLayout->addLayout(topLayout); - midLayout->addWidget(lineFrame); - - midLayout->addStretch(); - - QHBoxLayout *statusLayout = new QHBoxLayout(); - - onlineLabel = new QLabel("0", container); - onlineLabel->setObjectName("status"); - - statusLayout->addWidget(onlineLabel); - - topLayout->addStretch(); - topLayout->addLayout(statusLayout); - - midLayout->addStretch(); - - layout->addSpacing(10); - layout->addLayout(midLayout); - layout->addSpacing(10); - - container->setLayout(layout); - mainLayout->addWidget(container); + listLayout = new FriendListLayout(); setAcceptDrops(true); + + onCompactChanged(isCompact()); + + //renameCircle(); } void CircleWidget::addFriendWidget(FriendWidget *w, Status s) { listLayout->addFriendWidget(w, s); - //if (s == Status::Offline) - updateOffline(); - //else - updateOnline(); + updateStatus(); } void CircleWidget::expand() @@ -116,12 +94,12 @@ void CircleWidget::toggle() expanded = !expanded; if (expanded) { - mainLayout->addLayout(listLayout); + fullLayout->addLayout(listLayout); arrowLabel->setPixmap(QPixmap(":/ui/chatArea/scrollBarDownArrow.svg")); } else { - mainLayout->removeItem(listLayout); + fullLayout->removeItem(listLayout); arrowLabel->setPixmap(QPixmap(":/ui/chatArea/scrollBarRightArrow.svg")); } } @@ -136,29 +114,92 @@ QString CircleWidget::getName() const return nameLabel->text(); } +void CircleWidget::setName(const QString &name) +{ + nameLabel->setText(name); +} + void CircleWidget::renameCircle() { qDebug() << nameLabel->parentWidget()->layout(); QLineEdit *lineEdit = new QLineEdit(nameLabel->text()); - lineEdit->show(); + topLayout->removeWidget(nameLabel); + topLayout->insertWidget(3, lineEdit); + nameLabel->setVisible(false); + //topLayout->replaceWidget(nameLabel, lineEdit); //nameLabel->parentWidget()->layout() //static_cast(nameLabel->parentWidget()->layout())->insertWidget(nameLabel->parentWidget()->layout()->indexOf(nameLabel) - 1, lineEdit); //nameLabel->parentWidget()->layout()->replaceWidget(nameLabel, lineEdit); - nameLabel->setVisible(false); + //nameLabel->setVisible(false); lineEdit->selectAll(); lineEdit->setFocus(); connect(lineEdit, &QLineEdit::editingFinished, [this, lineEdit]() { - nameLabel->setVisible(true); + this->topLayout->removeWidget(lineEdit); + this->topLayout->insertWidget(3, nameLabel); + //this->topLayout->replaceWidget(lineEdit, nameLabel); + //this->nameLabel->setVisible(true); //lineEdit->parentWidget()->layout()->replaceWidget(lineEdit, nameLabel); - nameLabel->setText(lineEdit->text()); + this->nameLabel->setText(lineEdit->text()); + nameLabel->setVisible(true); lineEdit->deleteLater(); }); } -void CircleWidget::onCompactChanged(bool compact) +void CircleWidget::onCompactChanged(bool _compact) { + delete topLayout; + delete mainLayout; + topLayout = new QHBoxLayout; + topLayout->setSpacing(0); + topLayout->setMargin(0); + + setProperty("compact", _compact); + + if (property("compact").toBool()) + { + mainLayout = nullptr; + + container->setFixedHeight(25); + container->setLayout(topLayout); + + topLayout->addSpacing(18); + topLayout->addWidget(arrowLabel); + topLayout->addSpacing(5); + topLayout->addWidget(nameLabel); + topLayout->addSpacing(5); + topLayout->addWidget(lineFrame, 1); + topLayout->addSpacing(5); + topLayout->addWidget(statusLabel); + topLayout->addSpacing(5); + topLayout->activate(); + } + else + { + qDebug() << "LTSE"; + mainLayout = new QVBoxLayout(); + mainLayout->setSpacing(0); + mainLayout->setContentsMargins(20, 0, 20, 0); + + container->setFixedHeight(55); + container->setLayout(mainLayout); + + topLayout->addWidget(arrowLabel); + topLayout->addSpacing(10); + topLayout->addWidget(nameLabel); + topLayout->addStretch(); + topLayout->addWidget(statusLabel); + topLayout->activate(); + + mainLayout->addStretch(); + mainLayout->addLayout(topLayout); + mainLayout->addWidget(lineFrame); + mainLayout->addStretch(); + mainLayout->activate(); + } + + Style::repolish(this); } void CircleWidget::contextMenuEvent(QContextMenuEvent *event) @@ -236,8 +277,7 @@ void CircleWidget::dropEvent(QDropEvent *event) if (circleWidget != nullptr) { // In case the status was changed while moving, update both. - circleWidget->updateOffline(); - circleWidget->updateOnline(); + circleWidget->updateStatus(); } container->setAttribute(Qt::WA_UnderMouse, false); @@ -245,12 +285,7 @@ void CircleWidget::dropEvent(QDropEvent *event) } } -void CircleWidget::updateOnline() +void CircleWidget::updateStatus() { - onlineLabel->setText(QString::number(listLayout->friendOnlineCount()) + QStringLiteral(" / ") + QString::number(listLayout->friendOfflineCount())); -} - -void CircleWidget::updateOffline() -{ - //offlineLabel->setText(QString::number(listLayout->friendOfflineCount())); + statusLabel->setText(QString::number(listLayout->friendOnlineCount()) + QStringLiteral(" / ") + QString::number(listLayout->friendTotalCount())); } diff --git a/src/widget/circlewidget.h b/src/widget/circlewidget.h index f4fa2430d..6f9c77950 100644 --- a/src/widget/circlewidget.h +++ b/src/widget/circlewidget.h @@ -39,10 +39,10 @@ public: void expand(); void toggle(); - void updateOnline(); - void updateOffline(); + void updateStatus(); QString getName() const; + void setName(const QString &name); void renameCircle(); public slots: @@ -65,12 +65,15 @@ private: Offline = 1 }; bool expanded = false; - FriendListLayout *listLayout; - QVBoxLayout *mainLayout; - QLabel *arrowLabel; - QLabel *onlineLabel; - QWidget *container; - QLabel *nameLabel; + FriendListLayout* listLayout; + QVBoxLayout* fullLayout; + QVBoxLayout* mainLayout = nullptr; + QLabel* arrowLabel; + QLabel* nameLabel; + QLabel* statusLabel; + QFrame* lineFrame; + QWidget* container; + QHBoxLayout* topLayout = nullptr; }; #endif // CIRCLEWIDGET_H diff --git a/src/widget/friendlistlayout.cpp b/src/widget/friendlistlayout.cpp index 4fa84d445..d31fac142 100644 --- a/src/widget/friendlistlayout.cpp +++ b/src/widget/friendlistlayout.cpp @@ -88,7 +88,7 @@ int FriendListLayout::friendOnlineCount() const return friendLayouts[Online]->count(); } -int FriendListLayout::friendOfflineCount() const +int FriendListLayout::friendTotalCount() const { return friendLayouts[Offline]->count() + friendOnlineCount(); } diff --git a/src/widget/friendlistlayout.h b/src/widget/friendlistlayout.h index fc2b674a7..ae2dbb862 100644 --- a/src/widget/friendlistlayout.h +++ b/src/widget/friendlistlayout.h @@ -31,7 +31,7 @@ public: void addGroupWidget(GroupWidget *widget); void addFriendWidget(FriendWidget *widget, Status s); int friendOnlineCount() const; - int friendOfflineCount() const; + int friendTotalCount() const; void searchChatrooms(const QString &searchString, bool hideOnline = false, bool hideOffline = false, bool hideGroups = false); bool hasChatrooms() const; diff --git a/src/widget/friendlistwidget.cpp b/src/widget/friendlistwidget.cpp index c14b06f17..92c7f69c8 100644 --- a/src/widget/friendlistwidget.cpp +++ b/src/widget/friendlistwidget.cpp @@ -51,7 +51,30 @@ void FriendListWidget::addGroupWidget(GroupWidget *widget) listLayout->groupLayout->addWidget(widget); } -void FriendListWidget::addCircleWidget(FriendWidget *friendWidget) +void FriendListWidget::addFriendWidget(FriendWidget *w, Status s, int circleIndex) +{ + CircleWidget *circleWidget = nullptr; + qDebug() << circleIndex; + if (circleIndex >= 0 && circleIndex < circleLayout->count()) + circleWidget = dynamic_cast(circleLayout->itemAt(circleIndex)->widget()); + + if (circleWidget == nullptr) + circleIndex = -1; + + if (circleIndex == -1) + moveWidget(w, s, true); + else + circleWidget->addFriendWidget(w, s); +} + +void FriendListWidget::addCircleWidget(const QString &name) +{ + CircleWidget *circleWidget = new CircleWidget(this); + circleWidget->setName(name); + circleLayout->addWidget(circleWidget); +} + +CircleWidget* FriendListWidget::addCircleWidget(FriendWidget *friendWidget) { CircleWidget *circleWidget = new CircleWidget(this); circleLayout->addWidget(circleWidget); @@ -61,6 +84,7 @@ void FriendListWidget::addCircleWidget(FriendWidget *friendWidget) circleWidget->toggle(); } circleWidget->show(); // Avoid flickering. + return circleWidget; } void FriendListWidget::removeCircleWidget(CircleWidget *widget) @@ -168,8 +192,7 @@ void FriendListWidget::dropEvent(QDropEvent *event) if (circleWidget != nullptr) { // In case the status was changed while moving, update both. - circleWidget->updateOffline(); - circleWidget->updateOnline(); + circleWidget->updateStatus(); } } } diff --git a/src/widget/friendlistwidget.h b/src/widget/friendlistwidget.h index d3c1a1f8d..d9ac2dbd8 100644 --- a/src/widget/friendlistwidget.h +++ b/src/widget/friendlistwidget.h @@ -43,8 +43,9 @@ public: explicit FriendListWidget(QWidget *parent = 0, bool groupsOnTop = true); void addGroupWidget(GroupWidget *widget); - - void addCircleWidget(FriendWidget *widget = nullptr); + void addFriendWidget(FriendWidget *w, Status s, int circleIndex); + void addCircleWidget(const QString &name); + CircleWidget *addCircleWidget(FriendWidget *widget = nullptr); void removeCircleWidget(CircleWidget *widget); void searchChatrooms(const QString &searchString, bool hideOnline = false, bool hideOffline = false, bool hideGroups = false); diff --git a/src/widget/friendwidget.cpp b/src/widget/friendwidget.cpp index 64b119d5a..c2f337374 100644 --- a/src/widget/friendwidget.cpp +++ b/src/widget/friendwidget.cpp @@ -62,7 +62,7 @@ void FriendWidget::contextMenuEvent(QContextMenuEvent * event) QString dir = Settings::getInstance().getAutoAcceptDir(id); QMenu menu; QMenu* inviteMenu = menu.addMenu(tr("Invite to group","Menu to invite a friend to a groupchat")); - QMenu* circleMenu = menu.addMenu(tr("Move to circle", "Menu to move a friend into a different circle")); + QMenu* circleMenu = menu.addMenu(tr("Move to circle...", "Menu to move a friend into a different circle")); QAction* copyId = menu.addAction(tr("Copy friend ID","Menu to copy the Tox ID of that friend")); QMap groupActions; @@ -95,8 +95,11 @@ void FriendWidget::contextMenuEvent(QContextMenuEvent * event) QMap circleActions; for (CircleWidget* circle : circleVec) { - QAction* circleAction = circleMenu->addAction(tr("Add to circle \"%1\"").arg(circle->getName())); - circleActions[circleAction] = circle; + if (circle != circleWidget) + { + QAction* circleAction = circleMenu->addAction(tr("Move to circle \"%1\"").arg(circle->getName())); + circleActions[circleAction] = circle; + } } QAction* setAlias = menu.addAction(tr("Set alias...")); diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 2cc4cbe9e..f3186be5a 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -29,6 +29,7 @@ #include "src/group.h" #include "groupwidget.h" #include "form/groupchatform.h" +#include "circlewidget.h" #include "src/widget/style.h" #include "friendlistwidget.h" #include "form/chatform.h" @@ -232,6 +233,9 @@ void Widget::init() if (!Settings::getInstance().getShowSystemTray()) show(); + + for (int i = 0; i < Settings::getInstance().getCircleCount(); ++i) + contactListWidget->addCircleWidget(Settings::getInstance().getCircleName(i)); } bool Widget::eventFilter(QObject *obj, QEvent *event) @@ -577,7 +581,7 @@ void Widget::addFriend(int friendId, const QString &userId) { ToxId userToxId = ToxId(userId); Friend* newfriend = FriendList::addFriend(friendId, userToxId); - contactListWidget->moveWidget(newfriend->getFriendWidget(),Status::Offline); + contactListWidget->addFriendWidget(newfriend->getFriendWidget(),Status::Offline,Settings::getInstance().getFriendCircleIndex(newfriend->getToxId())); Core* core = Nexus::getCore(); connect(newfriend, &Friend::displayedNameChanged, contactListWidget, &FriendListWidget::moveWidget); @@ -1415,7 +1419,8 @@ void Widget::friendListContextMenu(const QPoint &pos) if (chosenAction == addCircleAction) { - contactListWidget->addCircleWidget(); + CircleWidget *newcircle = contactListWidget->addCircleWidget(); + connect(settingsWidget, &SettingsWidget::compactToggled, newcircle, &CircleWidget::onCompactChanged); } }