1
0
forked from BRT/arc
arc/views/widget/loader/DesktopSelectionWindow.cpp

77 lines
2.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "DesktopSelectionWindow.h"
#include <QCloseEvent>
#include <QJsonObject>
#include <QLabel>
#include <QPushButton>
#include <QStringList>
#include <QVBoxLayout>
namespace {
// Имена устройств десктопа (поле devices ответа /api/v1/desktop/).
// Отрезаем суффикс после последнего тире: "Infinix X6525D-da7b22..." -> "Infinix X6525D".
QStringList deviceNamesOf(const QJsonObject &d) {
QStringList names;
for (const QJsonValue &devVal : d["devices"].toArray()) {
const QJsonObject dev = devVal.toObject();
QString name = dev["name"].toString();
if (name.isEmpty()) name = dev["unique_name"].toString();
const int dash = name.lastIndexOf('-');
if (dash > 0) name = name.left(dash);
if (!name.isEmpty()) names << name;
}
return names;
}
} // namespace
DesktopSelectionWindow::DesktopSelectionWindow(const QJsonArray &desktops, QWidget *parent)
: QWidget(parent, Qt::Window) {
setWindowTitle("Выбор десктопа");
setMinimumWidth(360);
auto *layout = new QVBoxLayout(this);
layout->addWidget(new QLabel("Выберите десктоп для синхронизации:"));
for (const QJsonValue &dVal : desktops) {
const QJsonObject d = dVal.toObject();
const QString id = d["id"].toString();
QString title = d["unique_name"].toString();
if (title.isEmpty()) title = id;
const QStringList devices = deviceNamesOf(d);
QString text = title;
if (!devices.isEmpty()) {
// При длинном списке показываем только первые 3 устройства,
// а в следующей строке — сколько ещё осталось.
const int kMaxShown = 3;
text += "\n" + devices.mid(0, kMaxShown).join("\n");
if (devices.size() > kMaxShown)
text += QString("\n… ещё %1").arg(devices.size() - kMaxShown);
}
auto *btn = new QPushButton(text);
btn->setStyleSheet("text-align: left; padding: 8px;");
connect(btn, &QPushButton::clicked, this, [this, id]() {
m_chosen = true;
emit desktopChosen(id);
close();
});
layout->addWidget(btn);
}
auto *createBtn = new QPushButton("Создать новый");
createBtn->setStyleSheet("padding: 8px;");
connect(createBtn, &QPushButton::clicked, this, [this]() {
m_chosen = true;
emit desktopChosen(QString());
close();
});
layout->addWidget(createBtn);
}
void DesktopSelectionWindow::closeEvent(QCloseEvent *event) {
// Закрытие окна без выбора (крестик) трактуем как отмену.
if (!m_chosen) emit cancelled();
QWidget::closeEvent(event);
}