From 24ec7f232cf44d20f4f320182115cdea835c7ffe Mon Sep 17 00:00:00 2001 From: PatR Date: Thu, 5 Nov 2020 14:36:13 -0800 Subject: [PATCH] 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). --- win/Qt/qt_bind.cpp | 2 +- win/Qt/qt_streq.cpp | 58 ++++++++++++++++++++++++++------------------- win/Qt/qt_streq.h | 11 +++++---- 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/win/Qt/qt_bind.cpp b/win/Qt/qt_bind.cpp index 512ab06af..3a64002ee 100644 --- a/win/Qt/qt_bind.cpp +++ b/win/Qt/qt_bind.cpp @@ -754,7 +754,7 @@ char NetHackQtBind::qt_yn_function(const char *question_, void NetHackQtBind::qt_getlin(const char *prompt, char *line) { NetHackQtStringRequestor requestor(mainWidget(),prompt); - if (!requestor.Get(line)) { + if (!requestor.Get(line, BUFSZ, 40)) { Strcpy(line, "\033"); // discard any input that Get() might have left pending keybuffer.Drain(); diff --git a/win/Qt/qt_streq.cpp b/win/Qt/qt_streq.cpp index 96133edec..04fed5ca9 100644 --- a/win/Qt/qt_streq.cpp +++ b/win/Qt/qt_streq.cpp @@ -16,6 +16,7 @@ extern "C" { #include "qt_post.h" #include "qt_streq.h" #include "qt_str.h" +#include "qt_set.h" namespace nethack_qt_ { @@ -23,15 +24,19 @@ namespace nethack_qt_ { void centerOnMain(QWidget *); // 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), prompt(QString::fromLatin1(p),this), input(this,"input") { + if (qt_settings) + input.setFont(qt_settings->normalFixedFont()); + cancel=new QPushButton(cancelstr,this); 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(&input,SIGNAL(returnPressed()),this,SLOT(accept())); okay->setDefault(true); @@ -44,39 +49,42 @@ void NetHackQtStringRequestor::resizeEvent(QResizeEvent*) const int margin=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) { - h/=3; - prompt.setGeometry(margin,margin,width()-margin*2,h); - input.setGeometry(width()*1/5,margin+h+gutter, - (width()-margin-2-gutter)*4/5,h); + h /= 3; + prompt.setGeometry(margin + ifw * 2 + 1, margin, w + gutter, h); + input.setGeometry(width() * 1 / 5 - ifw, margin + h + gutter, + w * 4 / 5, h); } else { - h/=2; - prompt.setGeometry(margin,margin,(width()-margin*2-gutter)*2/5,h); - input.setGeometry(prompt.geometry().right()+gutter,margin, - (width()-margin-2-gutter)*3/5,h); + h /= 2; + prompt.setGeometry(margin + ifw * 2 + 1, margin, w * 2 / 5, h); + input.setGeometry(prompt.geometry().right() + gutter + - (ifw * 2 + 1) - ifw * 2, + margin, w * 3 / 5, h); } - cancel->setGeometry(margin,input.geometry().bottom()+gutter, - (width()-margin*2-gutter)/2,h); - okay->setGeometry(cancel->geometry().right()+gutter,cancel->geometry().y(), - cancel->width(),h); + cancel->setGeometry(margin, input.geometry().bottom() + gutter, w / 2, h); + okay->setGeometry(cancel->geometry().right() + gutter, + cancel->geometry().y(), w / 2, h); } -void NetHackQtStringRequestor::SetDefault(const char* d) +void NetHackQtStringRequestor::SetDefault(const char *d) { input.setText(d); } -bool NetHackQtStringRequestor::Get(char* buffer, int maxchar) +bool NetHackQtStringRequestor::Get(char *buffer, int maxchar, int minchar) { - input.setMaxLength(maxchar); - if (prompt.text().size() > 16) { - resize(fontMetrics().width(prompt.text())+50,fontMetrics().height()*6); - } else { - resize(fontMetrics().width(prompt.text())*2+50,fontMetrics().height()*4); - } + input.setMaxLength(maxchar - 1); + + const QString &txt = prompt.text(); + int pw = fontMetrics().width(txt), + 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 input.setText(buffer); @@ -94,7 +102,7 @@ bool NetHackQtStringRequestor::Get(char* buffer, int maxchar) exec(); if (result()) { - str_copy(buffer,input.text().toLatin1().constData(),maxchar); + str_copy(buffer, input.text().toLatin1().constData(), maxchar); return true; } else { return false; diff --git a/win/Qt/qt_streq.h b/win/Qt/qt_streq.h index 12fd0a776..89894dc03 100644 --- a/win/Qt/qt_streq.h +++ b/win/Qt/qt_streq.h @@ -19,10 +19,13 @@ private: QPushButton* cancel; public: - NetHackQtStringRequestor(QWidget *parent, const char* p,const char* cancelstr="Cancel"); - void SetDefault(const char*); - bool Get(char* buffer, int maxchar=80); - virtual void resizeEvent(QResizeEvent*); + NetHackQtStringRequestor(QWidget *parent, const char *p, + const char *cancelstr = "Cancel", + const char *okaystr = "Okay"); + void SetDefault(const char *); + // 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 *); }; } // namespace nethack_qt_ -- 2.50.1