You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

61 lines
1.9 KiB

#include "accountlist.h"
#include "../db/db_manager.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QPainter>
#include <QPixmap>
static QPixmap makeAvatar(const QColor &color, const QString &letter) {
QPixmap pix(24, 24);
pix.fill(Qt::transparent);
QPainter p(&pix);
p.setRenderHint(QPainter::Antialiasing);
p.setBrush(color);
p.setPen(Qt::NoPen);
p.drawEllipse(0, 0, 24, 24);
p.setPen(Qt::white);
QFont f = p.font();
f.setBold(true);
f.setPointSize(10);
p.setFont(f);
p.drawText(QRect(0, 0, 24, 24), Qt::AlignCenter, letter);
p.end();
return pix;
}
AccountList::AccountList(DbManager* db, QWidget *parent)
: QWidget(parent)
, m_listView(new QListView(this))
, m_model(new QStandardItemModel(this))
, m_db(db)
{
auto *layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
auto *header = new QLabel("Accounts", this);
header->setAlignment(Qt::AlignCenter);
header->setStyleSheet("font-weight: bold; padding: 8px; background: palette(window);");
layout->addWidget(header);
m_listView->setModel(m_model);
m_listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_listView->setSelectionMode(QAbstractItemView::SingleSelection);
m_listView->setIconSize(QSize(24, 24));
m_listView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_listView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
layout->addWidget(m_listView);
}
void AccountList::loadAccounts() {
m_model->clear();
if (!m_db || !m_db->isOpen()) return;
auto accounts = m_db->getAccounts(true);
for (const auto& a : accounts) {
auto *item = new QStandardItem();
item->setText(a.displayName);
item->setIcon(QIcon(makeAvatar(QColor(a.avatarColor), a.avatarLetter)));
m_model->appendRow(item);
}
}