1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "renamepopup.h"
6
7#include "common/common.h"
8
9#include <QLabel>
10#include <QLineEdit>
11#include <QVBoxLayout>
12#include <QLabel>
13#include <QEventLoop>
14
15class RenamePopupPrivate
16{
17 friend class RenamePopup;
18 QString oldName{""};
19 QLineEdit *renameEdit{nullptr};
20 QLabel *renameLabel{nullptr};
21 QVBoxLayout *vLayout{nullptr};
22 QEventLoop *loop{nullptr};
23};
24
25RenamePopup *RenamePopup::instance()
26{
27 static RenamePopup ins;
28 return &ins;
29}
30
31RenamePopup::RenamePopup(QWidget *parent)
32 : d(new RenamePopupPrivate())
33{
34
35 RenamePopup::setWindowFlag(Qt::Popup, true);
36
37 d->loop = new QEventLoop();
38 d->renameEdit = new QLineEdit();
39 d->renameLabel = new QLabel();
40 d->vLayout = new QVBoxLayout();
41
42 QObject::connect(d->renameEdit, &QLineEdit::returnPressed, [=](){
43 emit this->editingFinished(d->renameEdit->text());
44 this->close();
45 d->loop->quit();
46 });
47
48 d->vLayout->addWidget(d->renameLabel);
49 d->vLayout->addWidget(d->renameEdit);
50 RenamePopup::setLayout(d->vLayout);
51}
52
53RenamePopup::~RenamePopup()
54{
55 if (d) {
56 if (d->loop) {
57 d->loop->quit();
58 delete d->loop;
59 }
60 delete d;
61 }
62}
63
64void RenamePopup::setOldName(const QString &name)
65{
66 d->oldName = name;
67}
68
69QString RenamePopup::oldName()
70{
71 return d->oldName;
72}
73
74int RenamePopup::exec(const QPoint &pos)
75{
76 QEventLoop loop;
77 this->move(pos);
78 this->show();
79 return loop.exec();
80}
81
82int RenamePopup::exec()
83{
84 QEventLoop loop;
85 this->show();
86 return loop.exec();
87}
88
89void RenamePopup::showEvent(QShowEvent *event)
90{
91 Q_UNUSED(event);
92 d->renameLabel->setText(RenamePopup::tr("Rename %0 to:").arg(d->oldName));
93 d->renameEdit->setFocus();
94}
95
96void RenamePopup::hideEvent(QHideEvent *event)
97{
98 Q_UNUSED(event);
99 QWidget *parent = qobject_cast<QWidget*>(this->parent());
100 if (parent) {
101 parent->setFocus();
102 }
103}
104