added categories to the accountspage
This commit is contained in:
@@ -6,9 +6,18 @@ namespace chatterino {
|
||||
namespace controllers {
|
||||
namespace accounts {
|
||||
|
||||
Account::Account(const QString &_category)
|
||||
: category(_category)
|
||||
Account::Account(ProviderId _providerId)
|
||||
: providerId(_providerId)
|
||||
{
|
||||
static QString twitch("Twitch");
|
||||
|
||||
this->category = [&]() {
|
||||
switch (_providerId) {
|
||||
case ProviderId::Twitch:
|
||||
return twitch;
|
||||
}
|
||||
return QString("Unknown ProviderId");
|
||||
}();
|
||||
}
|
||||
|
||||
const QString &Account::getCategory() const
|
||||
@@ -16,6 +25,11 @@ const QString &Account::getCategory() const
|
||||
return this->category;
|
||||
}
|
||||
|
||||
ProviderId Account::getProviderId() const
|
||||
{
|
||||
return this->providerId;
|
||||
}
|
||||
|
||||
bool Account::operator<(const Account &other) const
|
||||
{
|
||||
QString a = this->toString();
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "providerid.hpp"
|
||||
|
||||
#include <QString>
|
||||
|
||||
namespace chatterino {
|
||||
@@ -9,14 +11,17 @@ namespace accounts {
|
||||
class Account
|
||||
{
|
||||
public:
|
||||
Account(const QString &category);
|
||||
Account(ProviderId providerId);
|
||||
virtual ~Account() = default;
|
||||
|
||||
virtual QString toString() const = 0;
|
||||
const QString &getCategory() const;
|
||||
ProviderId getProviderId() const;
|
||||
|
||||
bool operator<(const Account &other) const;
|
||||
|
||||
private:
|
||||
ProviderId providerId;
|
||||
QString category;
|
||||
};
|
||||
|
||||
|
||||
@@ -9,7 +9,29 @@ namespace accounts {
|
||||
AccountController::AccountController()
|
||||
{
|
||||
this->twitch.accounts.itemInserted.connect([this](const auto &args) {
|
||||
accounts.insertItem(std::dynamic_pointer_cast<Account>(args.item));
|
||||
this->accounts.insertItem(std::dynamic_pointer_cast<Account>(args.item));
|
||||
});
|
||||
|
||||
this->twitch.accounts.itemRemoved.connect([this](const auto &args) {
|
||||
if (args.caller != this) {
|
||||
auto &accs = this->twitch.accounts.getVector();
|
||||
auto it = std::find(accs.begin(), accs.end(), args.item);
|
||||
assert(it != accs.end());
|
||||
|
||||
this->accounts.removeItem(it - accs.begin());
|
||||
}
|
||||
});
|
||||
|
||||
this->accounts.itemRemoved.connect([this](const auto &args) {
|
||||
switch (args.item->getProviderId()) {
|
||||
case ProviderId::Twitch: {
|
||||
auto &accs = this->twitch.accounts.getVector();
|
||||
auto it = std::find(accs.begin(), accs.end(), args.item);
|
||||
assert(it != accs.end());
|
||||
|
||||
this->twitch.accounts.removeItem(it - accs.begin(), this);
|
||||
} break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -21,8 +43,18 @@ void AccountController::load()
|
||||
AccountModel *AccountController::createModel(QObject *parent)
|
||||
{
|
||||
AccountModel *model = new AccountModel(parent);
|
||||
// model->accountRemoved.connect([this](const auto &account) {
|
||||
// switch (account->getProviderId()) {
|
||||
// case ProviderId::Twitch: {
|
||||
// auto &accs = this->twitch.accounts.getVector();
|
||||
// auto it = std::find(accs.begin(), accs.end(), account);
|
||||
// assert(it != accs.end());
|
||||
|
||||
// this->twitch.accounts.removeItem(it - accs.begin());
|
||||
// } break;
|
||||
// }
|
||||
// });
|
||||
|
||||
//(util::BaseSignalVector<stdAccount> *)
|
||||
model->init(&this->accounts);
|
||||
return model;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "accountmodel.hpp"
|
||||
|
||||
#include "util/standarditemhelper.hpp"
|
||||
|
||||
namespace chatterino {
|
||||
namespace controllers {
|
||||
namespace accounts {
|
||||
@@ -21,6 +23,41 @@ void AccountModel::getRowFromItem(const std::shared_ptr<Account> &item,
|
||||
std::vector<QStandardItem *> &row)
|
||||
{
|
||||
row[0]->setData(item->toString(), Qt::DisplayRole);
|
||||
row[0]->setData(QFont("Segoe UI", 10), Qt::FontRole);
|
||||
// row[0]->setData(QColor(255, 255, 255), Qt::BackgroundRole);
|
||||
}
|
||||
|
||||
int AccountModel::beforeInsert(const std::shared_ptr<Account> &item,
|
||||
std::vector<QStandardItem *> &row, int proposedIndex)
|
||||
{
|
||||
if (this->categoryCount[item->getCategory()]++ == 0) {
|
||||
auto row = this->createRow();
|
||||
|
||||
util::setStringItem(row[0], item->getCategory(), false, false);
|
||||
// row[0]->setData(QColor(142, 36, 170), Qt::ForegroundRole);
|
||||
// row[0]->setData(QColor(255, 255, 255), Qt::BackgroundRole);
|
||||
row[0]->setData(QFont("Segoe UI Light", 16), Qt::FontRole);
|
||||
|
||||
this->insertCustomRow(std::move(row), proposedIndex);
|
||||
|
||||
return proposedIndex + 1;
|
||||
}
|
||||
|
||||
return proposedIndex;
|
||||
}
|
||||
|
||||
void AccountModel::afterRemoved(const std::shared_ptr<Account> &item,
|
||||
std::vector<QStandardItem *> &row, int index)
|
||||
{
|
||||
auto it = this->categoryCount.find(item->getCategory());
|
||||
assert(it != this->categoryCount.end());
|
||||
|
||||
if (it->second <= 1) {
|
||||
this->categoryCount.erase(it);
|
||||
this->removeCustomRow(index - 1);
|
||||
} else {
|
||||
it->second--;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace accounts
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "controllers/accounts/account.hpp"
|
||||
#include "util/qstringhash.hpp"
|
||||
#include "util/signalvectormodel.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace chatterino {
|
||||
namespace controllers {
|
||||
namespace accounts {
|
||||
@@ -23,7 +26,16 @@ protected:
|
||||
virtual void getRowFromItem(const std::shared_ptr<Account> &item,
|
||||
std::vector<QStandardItem *> &row) override;
|
||||
|
||||
virtual int beforeInsert(const std::shared_ptr<Account> &item,
|
||||
std::vector<QStandardItem *> &row, int proposedIndex) override;
|
||||
|
||||
virtual void afterRemoved(const std::shared_ptr<Account> &item,
|
||||
std::vector<QStandardItem *> &row, int index) override;
|
||||
|
||||
friend class AccountController;
|
||||
|
||||
private:
|
||||
std::unordered_map<QString, int> categoryCount;
|
||||
};
|
||||
|
||||
} // namespace accounts
|
||||
|
||||
Reference in New Issue
Block a user