mirror-chatterino2/src/controllers/accounts/AccountModel.cpp

65 lines
1.8 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "AccountModel.hpp"
2018-05-06 12:52:47 +02:00
#include "controllers/accounts/Account.hpp"
2018-06-26 14:09:39 +02:00
#include "util/StandardItemHelper.hpp"
2018-05-28 08:34:54 +02:00
2018-05-06 12:52:47 +02:00
namespace chatterino {
AccountModel::AccountModel(QObject *parent)
2018-06-26 17:06:17 +02:00
: SignalVectorModel<std::shared_ptr<Account>>(1, parent)
2018-05-06 12:52:47 +02:00
{
}
// turn a vector item into a model row
2018-08-06 21:17:03 +02:00
std::shared_ptr<Account> AccountModel::getItemFromRow(
std::vector<QStandardItem *> &, const std::shared_ptr<Account> &original)
2018-05-06 12:52:47 +02:00
{
return original;
2018-05-06 12:52:47 +02:00
}
// turns a row in the model into a vector item
void AccountModel::getRowFromItem(const std::shared_ptr<Account> &item,
std::vector<QStandardItem *> &row)
{
2018-06-26 17:06:17 +02:00
setStringItem(row[0], item->toString(), false);
2018-05-28 08:34:54 +02:00
row[0]->setData(QFont("Segoe UI", 10), Qt::FontRole);
}
int AccountModel::beforeInsert(const std::shared_ptr<Account> &item,
2018-08-06 21:17:03 +02:00
std::vector<QStandardItem *> &row,
int proposedIndex)
2018-05-28 08:34:54 +02:00
{
2018-10-21 13:43:02 +02:00
if (this->categoryCount_[item->getCategory()]++ == 0)
{
2018-05-28 08:34:54 +02:00
auto row = this->createRow();
2018-06-26 17:06:17 +02:00
setStringItem(row[0], item->getCategory(), false, false);
2018-05-28 08:34:54 +02:00
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());
2018-05-28 08:34:54 +02:00
2018-10-21 13:43:02 +02:00
if (it->second <= 1)
{
this->categoryCount_.erase(it);
2018-05-28 08:34:54 +02:00
this->removeCustomRow(index - 1);
2018-10-21 13:43:02 +02:00
}
else
{
2018-05-28 08:34:54 +02:00
it->second--;
}
2018-05-06 12:52:47 +02:00
}
} // namespace chatterino