summaryrefslogtreecommitdiff
path: root/ui/qt5/avatardialog.cc
diff options
context:
space:
mode:
authorA. Wilcox <AWilcox@Wilcox-Tech.com>2019-12-14 16:10:16 -0600
committerA. Wilcox <AWilcox@Wilcox-Tech.com>2019-12-14 16:10:16 -0600
commit3a0c398b72f463d09bc87e598fbb6889000b6232 (patch)
treef291fe89a2004e34b3b4bdaf01e34ea18b7ef620 /ui/qt5/avatardialog.cc
parent37d282b7a12c884f8441317c89ee3703bd29210c (diff)
downloadhorizon-3a0c398b72f463d09bc87e598fbb6889000b6232.tar.gz
horizon-3a0c398b72f463d09bc87e598fbb6889000b6232.tar.bz2
horizon-3a0c398b72f463d09bc87e598fbb6889000b6232.tar.xz
horizon-3a0c398b72f463d09bc87e598fbb6889000b6232.zip
Qt UI: Implement user avatar selection
Diffstat (limited to 'ui/qt5/avatardialog.cc')
-rw-r--r--ui/qt5/avatardialog.cc72
1 files changed, 72 insertions, 0 deletions
diff --git a/ui/qt5/avatardialog.cc b/ui/qt5/avatardialog.cc
new file mode 100644
index 0000000..b7662b9
--- /dev/null
+++ b/ui/qt5/avatardialog.cc
@@ -0,0 +1,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
+ aviList->setItemSelected(items.at(0), true);
+}