From a0c28e97daf1287dba726059708da6c697eb4d3b Mon Sep 17 00:00:00 2001 From: Pasi Kallinen Date: Sun, 18 Feb 2024 13:27:47 +0200 Subject: [PATCH] Qt: split menu window color and attr setting --- win/Qt/qt_menu.cpp | 97 ++++++++++++++++++++++++---------------------- win/Qt/qt_menu.h | 1 + 2 files changed, 52 insertions(+), 46 deletions(-) diff --git a/win/Qt/qt_menu.cpp b/win/Qt/qt_menu.cpp index 942fa7187..047ab73c6 100644 --- a/win/Qt/qt_menu.cpp +++ b/win/Qt/qt_menu.cpp @@ -541,6 +541,56 @@ static const char *color_name(const QColor q) } #endif +void NetHackQtMenuWindow::SetTwiAttr(QTableWidgetItem *twi, int color, int attr) +{ + if (color != NO_COLOR) { + twi->setForeground(colors[color].q); + } + + if (attr != ATR_NONE) { + QFont itemfont(table->font()); + switch (attr) { + case ATR_BOLD: + itemfont.setWeight(QFont::Bold); + twi->setFont(itemfont); + break; + case ATR_ITALIC: + itemfont.setItalic(true); + twi->setFont(itemfont); + break; + case ATR_DIM: + twi->setFlags(Qt::NoItemFlags); + break; + case ATR_ULINE: + itemfont.setUnderline(true); + twi->setFont(itemfont); + break; + case ATR_INVERSE: { + QBrush fg = twi->foreground(); + QBrush bg = twi->background(); + if (fg.color() == bg.color()) { + // default foreground and background come up the same for + // some unknown reason + //[pr: both are set to 'Qt::color1' which has same RGB + // value as 'Qt::black'; X11 on OSX behaves similarly] + if (fg.color() == Qt::color1) { + fg = Qt::black; + bg = Qt::white; + } else { + fg = (bg.color() == Qt::white) ? Qt::black : Qt::white; + } + } + twi->setForeground(bg); + twi->setBackground(fg); + break; + } + case ATR_BLINK: + // not supported + break; + } /* switch */ + } /* if attr != ATR_NONE */ +} + void NetHackQtMenuWindow::AddRow(int row, const MenuItem& mi) { QFontMetrics fm(table->font()); @@ -612,52 +662,7 @@ void NetHackQtMenuWindow::AddRow(int row, const MenuItem& mi) table->item(row, 4)->setFlags(Qt::ItemIsEnabled); WidenColumn(4, fm.QFM_WIDTH(text)); - if ((int) mi.color != NO_COLOR) { - twi->setForeground(colors[mi.color].q); - } - - if (mi.attr != ATR_NONE) { - QFont itemfont(table->font()); - switch (mi.attr) { - case ATR_BOLD: - itemfont.setWeight(QFont::Bold); - twi->setFont(itemfont); - break; - case ATR_ITALIC: - itemfont.setItalic(true); - twi->setFont(itemfont); - break; - case ATR_DIM: - twi->setFlags(Qt::NoItemFlags); - break; - case ATR_ULINE: - itemfont.setUnderline(true); - twi->setFont(itemfont); - break; - case ATR_INVERSE: { - QBrush fg = twi->foreground(); - QBrush bg = twi->background(); - if (fg.color() == bg.color()) { - // default foreground and background come up the same for - // some unknown reason - //[pr: both are set to 'Qt::color1' which has same RGB - // value as 'Qt::black'; X11 on OSX behaves similarly] - if (fg.color() == Qt::color1) { - fg = Qt::black; - bg = Qt::white; - } else { - fg = (bg.color() == Qt::white) ? Qt::black : Qt::white; - } - } - twi->setForeground(bg); - twi->setBackground(fg); - break; - } - case ATR_BLINK: - // not supported - break; - } /* switch */ - } /* if mi.attr != ATR_NONE */ + SetTwiAttr(twi, mi.color, mi.attr); } void NetHackQtMenuWindow::WidenColumn(int column, int width) diff --git a/win/Qt/qt_menu.h b/win/Qt/qt_menu.h index b6134c974..844a470f4 100644 --- a/win/Qt/qt_menu.h +++ b/win/Qt/qt_menu.h @@ -128,6 +128,7 @@ private: bool isSelected(int row); long count(int row); + void SetTwiAttr(QTableWidgetItem *twi, int color, int attr); void AddRow(int row, const MenuItem& mi); void WidenColumn(int column, int width); void PadMenuColumns(bool split_descr);