From df3fc0452355e7dbbec8ac5032c54b2f049612b4 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Sun, 26 Jul 2026 09:33:31 +0300 Subject: [PATCH] =?UTF-8?q?chatgui:=20RenameDialog=20=E2=80=94=20quick=20r?= =?UTF-8?q?ename=20via=20context=20menu,=20separate=20mini=20dialog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/chatgui/CMakeLists.txt | 1 + tools/chatgui/src/accountlist.cpp | 9 +++ tools/chatgui/src/renamedialog.cpp | 92 ++++++++++++++++++++++++++++++ tools/chatgui/src/renamedialog.h | 24 ++++++++ 4 files changed, 126 insertions(+) create mode 100644 tools/chatgui/src/renamedialog.cpp create mode 100644 tools/chatgui/src/renamedialog.h diff --git a/tools/chatgui/CMakeLists.txt b/tools/chatgui/CMakeLists.txt index 5881f1f2..052f95d1 100644 --- a/tools/chatgui/CMakeLists.txt +++ b/tools/chatgui/CMakeLists.txt @@ -57,6 +57,7 @@ add_executable(chatgui src/accountlist.cpp src/accountdelegate.cpp src/memberpropsdialog.cpp + src/renamedialog.cpp src/memberlistmodel.cpp src/emoji.cpp src/emojipanel.cpp diff --git a/tools/chatgui/src/accountlist.cpp b/tools/chatgui/src/accountlist.cpp index 8efeebee..9af53667 100644 --- a/tools/chatgui/src/accountlist.cpp +++ b/tools/chatgui/src/accountlist.cpp @@ -2,6 +2,7 @@ #include "accountdelegate.h" #include "memberlistmodel.h" #include "memberpropsdialog.h" +#include "renamedialog.h" #include "../db/db_manager.h" #include #include @@ -215,8 +216,16 @@ void AccountList::onMemberContextMenu(const QPoint& pos) { QString name = m_model->data(index, Qt::DisplayRole).toString(); QMenu menu(m_listView); + QAction* renameAction = menu.addAction("Rename"); + if (nodeId == m_db->myNodeId()) renameAction->setEnabled(false); QAction* editAction = menu.addAction("Edit"); QAction* chosen = menu.exec(m_listView->viewport()->mapToGlobal(pos)); + + if (chosen == renameAction) { + RenameDialog dlg(m_db, m_channelId, nodeId, name, this); + if (dlg.exec() == QDialog::Accepted) m_model->refresh(); + return; + } if (chosen != editAction) return; MemberPropsDialog dlg(m_db, m_channelId, nodeId, name, this); diff --git a/tools/chatgui/src/renamedialog.cpp b/tools/chatgui/src/renamedialog.cpp new file mode 100644 index 00000000..fd5dff94 --- /dev/null +++ b/tools/chatgui/src/renamedialog.cpp @@ -0,0 +1,92 @@ +#include "renamedialog.h" +#include "../db/db_manager.h" +#include +#include +#include +#include + +RenameDialog::RenameDialog(DbManager* db, const QString& channelId, + quint64 nodeId, const QString& displayName, + QWidget* parent) + : QDialog(parent) + , m_db(db) + , m_channelId(channelId) + , m_nodeId(nodeId) +{ + setWindowTitle("Rename"); + setMinimumWidth(300); + setModal(true); + + QString curNick; + if (m_db->m_db) { + QString tbl = m_db->peersTableName(channelId); + QByteArray sql = QString( + "SELECT local_nick FROM \"%1\" WHERE node_id=?" + ).arg(tbl).toUtf8(); + sqlite3_stmt* st = nullptr; + if (sqlite3_prepare_v2(m_db->m_db, sql.constData(), -1, &st, nullptr) == SQLITE_OK) { + sqlite3_bind_int64(st, 1, (sqlite3_int64)nodeId); + if (sqlite3_step(st) == SQLITE_ROW) + curNick = DbManager::colText(st, 0); + sqlite3_finalize(st); + } + } + + auto* layout = new QVBoxLayout(this); + layout->setSpacing(12); + layout->setContentsMargins(20, 16, 20, 16); + + auto* title = new QLabel(displayName, this); + title->setStyleSheet("font-weight: bold; font-size: 13px;"); + layout->addWidget(title); + + m_edit = new QLineEdit(this); + m_edit->setText(curNick); + m_edit->setPlaceholderText(displayName); + layout->addWidget(m_edit); + + layout->addStretch(); + + auto* btnLayout = new QHBoxLayout(); + btnLayout->addStretch(); + + auto* cancelBtn = new QPushButton("Cancel", this); + cancelBtn->setFixedWidth(90); + cancelBtn->setStyleSheet( + "QPushButton { border: 1px solid palette(mid); border-radius: 4px;" + " padding: 6px 16px; font-size: 13px; background: palette(button); }" + "QPushButton:hover { background: palette(light); }"); + connect(cancelBtn, &QPushButton::clicked, this, &QDialog::reject); + btnLayout->addWidget(cancelBtn); + + auto* saveBtn = new QPushButton("Save", this); + saveBtn->setFixedWidth(90); + saveBtn->setDefault(true); + saveBtn->setStyleSheet( + "QPushButton { border: none; border-radius: 4px;" + " padding: 6px 16px; font-size: 13px; font-weight: bold;" + " background: #3390EC; color: white; }" + "QPushButton:hover { background: #2B7ED4; }"); + connect(saveBtn, &QPushButton::clicked, this, &RenameDialog::onSave); + btnLayout->addWidget(saveBtn); + + layout->addLayout(btnLayout); +} + +void RenameDialog::onSave() { + if (!m_db->m_db) return; + QString tbl = m_db->peersTableName(m_channelId); + QString nick = m_edit->text().trimmed(); + QByteArray sql = QString( + "UPDATE \"%1\" SET local_nick=? WHERE node_id=?" + ).arg(tbl).toUtf8(); + sqlite3_stmt* st = nullptr; + if (sqlite3_prepare_v2(m_db->m_db, sql.constData(), -1, &st, nullptr) == SQLITE_OK) { + sqlite3_bind_text(st, 1, nick.isEmpty() ? nullptr : nick.toUtf8().constData(), + -1, nick.isEmpty() ? SQLITE_STATIC : SQLITE_TRANSIENT); + sqlite3_bind_int64(st, 2, (sqlite3_int64)m_nodeId); + sqlite3_step(st); + sqlite3_finalize(st); + } + accept(); +} diff --git a/tools/chatgui/src/renamedialog.h b/tools/chatgui/src/renamedialog.h new file mode 100644 index 00000000..c00fd556 --- /dev/null +++ b/tools/chatgui/src/renamedialog.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#include + +class DbManager; + +class RenameDialog : public QDialog { + Q_OBJECT +public: + explicit RenameDialog(DbManager* db, const QString& channelId, + quint64 nodeId, const QString& displayName, + QWidget* parent = nullptr); + +private slots: + void onSave(); + +private: + DbManager* m_db; + QString m_channelId; + quint64 m_nodeId; + QLineEdit* m_edit; +};