summaryrefslogtreecommitdiff
path: root/ui/qt5/avatardialog.cc
blob: 02c28644ef3d1d3cd22fcd5565579734c69dd8d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * avatardialog.cc - Implementation of a dialog for selecting a user's avatar
 * horizon-qt5, the Qt 5 user interface for
 * Project Horizon
 *
 * Copyright (c) 2019 Adélie Linux and contributors.  All rights reserved.
 * This code is licensed under the AGPL 3.0 license, as noted in the
 * LICENSE-code file in the root directory of this repository.
 *
 * SPDX-License-Identifier: AGPL-3.0-only
 */

#include "avatardialog.hh"

#include <QDirIterator>
#include <QHBoxLayout>
#include <QListWidget>
#include <QPushButton>
#include <QVBoxLayout>

AvatarDialog::AvatarDialog(QWidget *parent)
    : QDialog(parent, Qt::WindowTitleHint | Qt::WindowCloseButtonHint) {
    setWindowTitle(tr("Choose Avatar"));

    aviList = new QListWidget;
    aviList->setGridSize(QSize(52, 52));
    aviList->setIconSize(QSize(48, 48));
    aviList->setViewMode(QListView::IconMode);
    loadAvatars();
    connect(aviList, &QListWidget::itemSelectionChanged, [=]{
        if(aviList->selectedItems().isEmpty()) return;
        path = aviList->selectedItems()[0]->data(Qt::ToolTipRole).toString();
    });

    QPushButton *ok = new QPushButton(tr("Choose"));
    connect(ok, &QPushButton::clicked, this, &QDialog::accept);
    QPushButton *cancel = new QPushButton(tr("Cancel"));
    connect(cancel, &QPushButton::clicked, this, &QDialog::reject);

    QVBoxLayout *buttonLayout = new QVBoxLayout;
    buttonLayout->setAlignment(Qt::AlignTop);
    buttonLayout->addWidget(ok);
    buttonLayout->addWidget(cancel);

    QHBoxLayout *mainBox = new QHBoxLayout;
    mainBox->addWidget(aviList);
    mainBox->addLayout(buttonLayout);

    setLayout(mainBox);
}

void AvatarDialog::loadAvatars() {
    QDirIterator it("/usr/share/user-manager/avatars", {}, QDir::Files, QDirIterator::Subdirectories);
    while(it.hasNext()) {
        QString path = it.next();
        QListWidgetItem *item = new QListWidgetItem(aviList);
        item->setData(Qt::ToolTipRole, path);
        item->setIcon(QPixmap(path));
    }
}

QString AvatarDialog::avatar() const {
    return this->path;
}

void AvatarDialog::setAvatar(QString path) {
    QList<QListWidgetItem *> items = aviList->findItems(path, Qt::MatchExactly);
    if(items.size() == 0)
        aviList->clearSelection();
    else
        items.at(0)->setSelected(true);
}