Qt string requestor

Some enhancements to the widget used to get player input for
getline() and also menu search and text window search.

give caller control of [cancel] and [okay] button names;
give caller a say in how wide the string input box should be
  instead of basing that on the length of the prompt string
  (needs more work...);
use fixed-width font for displaying the user's input;
clean up the widget layout a little bit.

src/Makefile needs a dependency update for Qt (not included).
This commit is contained in:
PatR
2020-11-05 14:36:13 -08:00
parent 702e52b431
commit 24ec7f232c
3 changed files with 41 additions and 30 deletions
+1 -1
View File
@@ -754,7 +754,7 @@ char NetHackQtBind::qt_yn_function(const char *question_,
void NetHackQtBind::qt_getlin(const char *prompt, char *line) void NetHackQtBind::qt_getlin(const char *prompt, char *line)
{ {
NetHackQtStringRequestor requestor(mainWidget(),prompt); NetHackQtStringRequestor requestor(mainWidget(),prompt);
if (!requestor.Get(line)) { if (!requestor.Get(line, BUFSZ, 40)) {
Strcpy(line, "\033"); Strcpy(line, "\033");
// discard any input that Get() might have left pending // discard any input that Get() might have left pending
keybuffer.Drain(); keybuffer.Drain();
+28 -20
View File
@@ -16,6 +16,7 @@ extern "C" {
#include "qt_post.h" #include "qt_post.h"
#include "qt_streq.h" #include "qt_streq.h"
#include "qt_str.h" #include "qt_str.h"
#include "qt_set.h"
namespace nethack_qt_ { namespace nethack_qt_ {
@@ -23,15 +24,19 @@ namespace nethack_qt_ {
void centerOnMain(QWidget *); void centerOnMain(QWidget *);
// end temporary // end temporary
NetHackQtStringRequestor::NetHackQtStringRequestor(QWidget *parent, const char* p, const char* cancelstr) : NetHackQtStringRequestor::NetHackQtStringRequestor(QWidget *parent,
const char *p, const char *cancelstr, const char *okaystr) :
QDialog(parent), QDialog(parent),
prompt(QString::fromLatin1(p),this), prompt(QString::fromLatin1(p),this),
input(this,"input") input(this,"input")
{ {
if (qt_settings)
input.setFont(qt_settings->normalFixedFont());
cancel=new QPushButton(cancelstr,this); cancel=new QPushButton(cancelstr,this);
connect(cancel,SIGNAL(clicked()),this,SLOT(reject())); connect(cancel,SIGNAL(clicked()),this,SLOT(reject()));
okay=new QPushButton("Okay",this); okay = new QPushButton(okaystr, this);
connect(okay,SIGNAL(clicked()),this,SLOT(accept())); connect(okay,SIGNAL(clicked()),this,SLOT(accept()));
connect(&input,SIGNAL(returnPressed()),this,SLOT(accept())); connect(&input,SIGNAL(returnPressed()),this,SLOT(accept()));
okay->setDefault(true); okay->setDefault(true);
@@ -45,23 +50,24 @@ void NetHackQtStringRequestor::resizeEvent(QResizeEvent*)
const int gutter=5; const int gutter=5;
int h = (height() - margin * 2 - gutter); int h = (height() - margin * 2 - gutter);
int w = (width() - margin * 2 - gutter);
int ifw = input.hasFrame() ? 3 : 0; // hack alert for input.frameWidth()
if (prompt.text().size() > 16) { if (prompt.text().size() > 16) {
h /= 3; h /= 3;
prompt.setGeometry(margin,margin,width()-margin*2,h); prompt.setGeometry(margin + ifw * 2 + 1, margin, w + gutter, h);
input.setGeometry(width()*1/5,margin+h+gutter, input.setGeometry(width() * 1 / 5 - ifw, margin + h + gutter,
(width()-margin-2-gutter)*4/5,h); w * 4 / 5, h);
} else { } else {
h /= 2; h /= 2;
prompt.setGeometry(margin,margin,(width()-margin*2-gutter)*2/5,h); prompt.setGeometry(margin + ifw * 2 + 1, margin, w * 2 / 5, h);
input.setGeometry(prompt.geometry().right()+gutter,margin, input.setGeometry(prompt.geometry().right() + gutter
(width()-margin-2-gutter)*3/5,h); - (ifw * 2 + 1) - ifw * 2,
margin, w * 3 / 5, h);
} }
cancel->setGeometry(margin,input.geometry().bottom()+gutter, cancel->setGeometry(margin, input.geometry().bottom() + gutter, w / 2, h);
(width()-margin*2-gutter)/2,h); okay->setGeometry(cancel->geometry().right() + gutter,
okay->setGeometry(cancel->geometry().right()+gutter,cancel->geometry().y(), cancel->geometry().y(), w / 2, h);
cancel->width(),h);
} }
void NetHackQtStringRequestor::SetDefault(const char *d) void NetHackQtStringRequestor::SetDefault(const char *d)
@@ -69,14 +75,16 @@ void NetHackQtStringRequestor::SetDefault(const char* d)
input.setText(d); input.setText(d);
} }
bool NetHackQtStringRequestor::Get(char* buffer, int maxchar) bool NetHackQtStringRequestor::Get(char *buffer, int maxchar, int minchar)
{ {
input.setMaxLength(maxchar); input.setMaxLength(maxchar - 1);
if (prompt.text().size() > 16) {
resize(fontMetrics().width(prompt.text())+50,fontMetrics().height()*6); const QString &txt = prompt.text();
} else { int pw = fontMetrics().width(txt),
resize(fontMetrics().width(prompt.text())*2+50,fontMetrics().height()*4); ww = minchar * input.fontMetrics().width(QChar('X'));
} int heightfactor = ((txt.size() > 16) ? 3 : 2) * 2; // 2 or 3 lines high
int widthfudge = (((txt.size() > 16) ? 1 : 2) * 5) * 2; // 5: margn, guttr
resize(pw + ww + widthfudge, fontMetrics().height() * heightfactor);
#ifdef EDIT_GETLIN #ifdef EDIT_GETLIN
input.setText(buffer); input.setText(buffer);
+5 -2
View File
@@ -19,9 +19,12 @@ private:
QPushButton* cancel; QPushButton* cancel;
public: public:
NetHackQtStringRequestor(QWidget *parent, const char* p,const char* cancelstr="Cancel"); NetHackQtStringRequestor(QWidget *parent, const char *p,
const char *cancelstr = "Cancel",
const char *okaystr = "Okay");
void SetDefault(const char *); void SetDefault(const char *);
bool Get(char* buffer, int maxchar=80); // maxchar is size of buffer[], minchar is size of line edit widget
bool Get(char *buffer, int maxchar = 80, int minchar = 20);
virtual void resizeEvent(QResizeEvent *); virtual void resizeEvent(QResizeEvent *);
}; };