Files
StationManager/userswidget.cpp
2026-04-05 16:14:54 +03:00

99 lines
3.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 "userswidget.h"
#include "database.h"
#include <QTableWidget>
#include <QHeaderView>
#include <QMessageBox>
#include <QInputDialog>
#include <QLabel>
UsersWidget::UsersWidget(QWidget* parent) : QWidget(parent) {
QVBoxLayout* mainLayout = new QVBoxLayout(this);
QLabel* title = new QLabel("Управление пользователями", this);
title->setStyleSheet("font-size: 20px; font-weight: bold;");
mainLayout->addWidget(title);
m_table = new QTableWidget(this);
m_table->setColumnCount(3);
m_table->setHorizontalHeaderLabels({"ID", "Логин", "Роль"});
m_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
m_table->setSelectionBehavior(QAbstractItemView::SelectRows);
m_table->setSelectionMode(QAbstractItemView::SingleSelection);
m_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
mainLayout->addWidget(m_table);
QHBoxLayout* btnLayout = new QHBoxLayout();
m_addBtn = new QPushButton("Добавить пользователя", this);
m_deleteBtn = new QPushButton("Удалить пользователя", this);
btnLayout->addWidget(m_addBtn);
btnLayout->addWidget(m_deleteBtn);
btnLayout->addStretch();
mainLayout->addLayout(btnLayout);
connect(m_addBtn, &QPushButton::clicked, this, &UsersWidget::onAddClicked);
connect(m_deleteBtn, &QPushButton::clicked, this, &UsersWidget::onDeleteClicked);
refreshUsers();
}
void UsersWidget::refreshUsers() {
QVector<User> users = Database::instance().getAllUsers();
m_table->setRowCount(users.size());
for (int i = 0; i < users.size(); ++i) {
const User& user = users[i];
m_table->setItem(i, 0, new QTableWidgetItem(QString::number(user.id)));
m_table->setItem(i, 1, new QTableWidgetItem(user.username));
m_table->setItem(i, 2, new QTableWidgetItem(user.role == "admin" ? "Администратор" : "Кассир"));
}
}
void UsersWidget::onAddClicked() {
QString username = QInputDialog::getText(this, "Новый пользователь", "Логин:");
if (username.isEmpty()) return;
QString password = QInputDialog::getText(this, "Новый пользователь", "Пароль:", QLineEdit::Password);
if (password.isEmpty()) return;
QStringList roles = {"cashier", "admin"};
QString role = QInputDialog::getItem(this, "Новый пользователь", "Роль:", roles, 0, false);
if (role.isEmpty()) return;
if (Database::instance().addUser(username, password, role)) {
QMessageBox::information(this, "Успех", "Пользователь добавлен!");
refreshUsers();
} else {
QMessageBox::critical(this, "Ошибка", "Пользователь с таким логином уже существует!");
}
}
void UsersWidget::onDeleteClicked() {
int row = m_table->currentRow();
if (row < 0) {
QMessageBox::warning(this, "Ошибка", "Выберите пользователя!");
return;
}
QString username = m_table->item(row, 1)->text();
QString role = m_table->item(row, 2)->text();
if (role == "Администратор") {
QMessageBox::warning(this, "Ошибка", "Нельзя удалить администратора!");
return;
}
int id = m_table->item(row, 0)->text().toInt();
QMessageBox::StandardButton reply = QMessageBox::question(
this, "Подтверждение",
QString("Удалить пользователя '%1'?").arg(username),
QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes) {
if (Database::instance().deleteUser(id)) {
QMessageBox::information(this, "Успех", "Пользователь удален!");
refreshUsers();
}
}
}