hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "qtinputdialog.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QVBoxLayout>
 
inputDialog* inputDialog::_instance = nullptr;
inputDialog::inputDialog(QWidget *parent) : QDialog(parent),
  m_eventLoop(nullptr)
{
    const QRect availableGeometry = QApplication::desktop()->availableGeometry();
    QVBoxLayout *mainLayout = new QVBoxLayout;
 
    QFont font;
    QFont fontname;
    QFont fontedit;
    QFont fontbutton;
    font.setPixelSize(availableGeometry.height()/20);
    fontname.setPixelSize(availableGeometry.height()/30);
    fontedit.setPixelSize(availableGeometry.height()/15);
    fontbutton.setPixelSize(availableGeometry.height()/25);
    nameLabel = new QLabel(this);
    nameLabel->setAlignment(Qt::AlignCenter | Qt::AlignVCenter);
    nameLabel->setFont(fontname);
    wordEdit = new QLineEdit(this);
    wordEdit->setFont(fontedit);
    yBtn.setFont(fontbutton);
    nBtn.setFont(fontbutton);
 
    QHBoxLayout *buttonlayout = new QHBoxLayout;
    buttonlayout->addWidget(&yBtn);
    buttonlayout->addWidget(&nBtn);
 
    buttonlayout->setStretchFactor(&yBtn,1);
    buttonlayout->setStretchFactor(&nBtn,1);
 
    mainLayout->addWidget(nameLabel);
    mainLayout->addSpacing(5);
    mainLayout->addWidget(wordEdit);
    mainLayout->addStretch(0);
    mainLayout->addLayout(buttonlayout);
    resize(availableGeometry.width()/3, availableGeometry.height() * 3/20);
    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
//    setWindowFlags(Qt::FramelessWindowHint);
    setLayout(mainLayout);
 
    connect(&yBtn, SIGNAL(clicked()), this, SLOT(slot_onYesClicked()));
    connect(&nBtn, SIGNAL(clicked()), this, SLOT(slot_onNoClicked()));
}
 
inputDialog::~inputDialog()
{
    if(m_eventLoop != nullptr)
        m_eventLoop->exit();
    if(wordEdit != nullptr)
        delete wordEdit;
    if(nameLabel != nullptr)
        delete nameLabel;
    _instance = nullptr;
}
 
void inputDialog::setText(QString yes, QString no, QString text)
{
        yBtn.setText(yes);
        nBtn.setText(no);
        nameLabel->setText(text);
}
 
int inputDialog::exec()
{
    if(m_eventLoop != nullptr)
        m_eventLoop->exit();
    setWindowModality(Qt::WindowModal);
    show();
    m_eventLoop = new QEventLoop(this);
    m_eventLoop->exec();
    return m_chooseResult;
}
 
void inputDialog::exit(bool result)
{
    if(m_eventLoop != nullptr) {
        m_chooseResult = result;
        close();
    }
}
 
bool inputDialog::isRunning(void)
{
    if(m_eventLoop != nullptr)
        return m_eventLoop->isRunning();
    return false;
}
 
void inputDialog::slot_onApplicationFocusChanged(QWidget *, QWidget *nowWidget)
{
    if (nowWidget != nullptr && !isAncestorOf(nowWidget)) {
        if (nowWidget->objectName().compare(parent()->objectName())) {
            setVisible(true);
        } else {
            setVisible(false);
        }
    }
}
 
void inputDialog::slot_onYesClicked()
{
    m_chooseResult = true;
    close();
}
 
void inputDialog::slot_onNoClicked()
{
    m_chooseResult = false;
    close();
}
 
void inputDialog::closeEvent(QCloseEvent*)
{
    if(m_eventLoop != nullptr)
        m_eventLoop->exit();
}