OSDN Git Service

upgrade to 3.6.1
[jnethack/source.git] / win / Qt4 / qt4streq.cpp
1 // Copyright (c) Warwick Allison, 1999.
2 // Qt4 conversion copyright (c) Ray Chason, 2012-2014.
3 // NetHack may be freely redistributed.  See license for details.
4
5 // qt4streq.cpp -- string requestor
6
7 #include "hack.h"
8 #undef Invisible
9 #undef Warning
10 #undef index
11 #undef msleep
12 #undef rindex
13 #undef wizard
14 #undef yn
15 #undef min
16 #undef max
17
18 #include <QtGui/QtGui>
19 #if QT_VERSION >= 0x050000
20 #include <QtWidgets/QtWidgets>
21 #endif
22 #include "qt4streq.h"
23 #include "qt4str.h"
24
25 namespace nethack_qt4 {
26
27 // temporary
28 void centerOnMain(QWidget *);
29 // end temporary
30
31 NetHackQtStringRequestor::NetHackQtStringRequestor(QWidget *parent, const char* p, const char* cancelstr) :
32     QDialog(parent),
33     prompt(QString::fromLatin1(p),this),
34     input(this,"input")
35 {
36     cancel=new QPushButton(cancelstr,this);
37     connect(cancel,SIGNAL(clicked()),this,SLOT(reject()));
38
39     okay=new QPushButton("Okay",this);
40     connect(okay,SIGNAL(clicked()),this,SLOT(accept()));
41     connect(&input,SIGNAL(returnPressed()),this,SLOT(accept()));
42     okay->setDefault(true);
43
44     setFocusPolicy(Qt::StrongFocus);
45 }
46
47 void NetHackQtStringRequestor::resizeEvent(QResizeEvent*)
48 {
49     const int margin=5;
50     const int gutter=5;
51
52     int h=(height()-margin*2-gutter);
53
54     if (prompt.text().size() > 16) {
55         h/=3;
56         prompt.setGeometry(margin,margin,width()-margin*2,h);
57         input.setGeometry(width()*1/5,margin+h+gutter,
58             (width()-margin-2-gutter)*4/5,h);
59     } else {
60         h/=2;
61         prompt.setGeometry(margin,margin,(width()-margin*2-gutter)*2/5,h);
62         input.setGeometry(prompt.geometry().right()+gutter,margin,
63             (width()-margin-2-gutter)*3/5,h);
64     }
65
66     cancel->setGeometry(margin,input.geometry().bottom()+gutter,
67         (width()-margin*2-gutter)/2,h);
68     okay->setGeometry(cancel->geometry().right()+gutter,cancel->geometry().y(),
69         cancel->width(),h);
70 }
71
72 void NetHackQtStringRequestor::SetDefault(const char* d)
73 {
74     input.setText(d);
75 }
76
77 bool NetHackQtStringRequestor::Get(char* buffer, int maxchar)
78 {
79     input.setMaxLength(maxchar);
80     if (prompt.text().size() > 16) {
81         resize(fontMetrics().width(prompt.text())+50,fontMetrics().height()*6);
82     } else {
83         resize(fontMetrics().width(prompt.text())*2+50,fontMetrics().height()*4);
84     }
85
86 #ifdef EDIT_GETLIN
87     input.setText(buffer);
88 #endif
89     centerOnMain(this);
90     show();
91     input.setFocus();
92     exec();
93
94     if (result()) {
95         str_copy(buffer,input.text().toLatin1().constData(),maxchar);
96         return true;
97     } else {
98         return false;
99     }
100 }
101
102 } // namespace nethack_qt4