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