]> granicus.if.org Git - nethack/commitdiff
Qt string requestor
authorPatR <rankin@nethack.org>
Thu, 5 Nov 2020 22:36:13 +0000 (14:36 -0800)
committerPatR <rankin@nethack.org>
Thu, 5 Nov 2020 22:36:13 +0000 (14:36 -0800)
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
win/Qt/qt_streq.cpp
win/Qt/qt_streq.h

index 512ab06af3a91b335626df8f265e5c1ba7dc48ed..3a64002eebf4dd92abf9ceac1d3063183278a282 100644 (file)
@@ -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();
index 96133edec289526c669ca9d8feb73773a5933a8b..04fed5ca9cd9d9251aaa177a1f635dd2d108d1c4 100644 (file)
@@ -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 chard)
+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;
index 12fd0a776b9d2231ae1735c5bc55b3fc18454a8e..89894dc03871a8aab421d03b3e76c25f27ce207e 100644 (file)
@@ -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_