MCPcopy Index your code
hub / github.com/NetHack/NetHack / PadMenuColumns

Method PadMenuColumns

win/Qt/qt_menu.cpp:376–452  ·  view source on GitHub ↗

pad the menu columns so that they all line up

Source from the content-addressed store, hash-verified

374
375// pad the menu columns so that they all line up
376void NetHackQtMenuWindow::PadMenuColumns(bool split_descr)
377{
378 QFontMetrics fm(table->font());
379 QString col0width_str = "";
380 if (biggestcount > 0L)
381 col0width_str = nh_qsprintf("%*ld", std::max(countdigits, 1),
382 biggestcount);
383 int col0width_int = (int) fm.QFM_WIDTH(col0width_str) + MENU_WIDTH_SLOP;
384 if (col0width_int > table->columnWidth(0))
385 WidenColumn(0, col0width_int);
386
387 // If the description (column 4) is a tab separated list, split
388 // it into fields and figure out how wide each field should be.
389 // Needs to be done at most once for any given menu instantiation.
390 std::vector<int> col_widths(1, 0); // start with 1 element init'd to 0
391 if (split_descr) {
392 for (int row = 0; row < itemcount; ++row) {
393 QTableWidgetItem *twi = table->item(row, 4); // description
394 if (twi == NULL)
395 continue;
396 // if a header/footnote/&c with no sub-fields, don't inflate
397 // the size of col_widths[0]
398 if (!itemlist[row].Selectable()
399 && !itemlist[row].str.contains(QChar('\t')))
400 continue;
401 // determine column widths of sub-fields within description
402 QStringList columns = itemlist[row].str.split("\t");
403 for (int fld = 0; fld < (int) columns.size(); ++fld) {
404 bool lastcol = (fld == (int) columns.size() - 1);
405 int w = fm.QFM_WIDTH(columns[fld] + (lastcol ? "" : " "));
406 if (fld >= (int) col_widths.size()) {
407 col_widths.push_back(w); // add another element
408 } else if (col_widths[fld] < w) {
409 col_widths[fld] = w;
410 }
411 }
412 }
413 } // split_descr
414
415 // Reformat all counts so that they line up right justified and
416 // pad each field within description to fill that field's width.
417 int widest4 = 0;
418 for (int row = 0; row < (int) itemlist.size(); ++row) {
419 // column 0 (subset count); format as right-justified number
420 QTableWidgetItem *cnt = table->item(row, 0);
421 if (cnt != NULL) {
422 QString Amt = "";
423 long amt = count(row); // fetch item(row,0) as a number
424 if (amt > 0L)
425 Amt = nh_qsprintf("%*ld", countdigits, amt);
426 cnt->setText(Amt);
427 }
428
429 // column 4 (item description)
430 QTableWidgetItem *twi = table->item(row, 4);
431 if (twi == NULL)
432 continue;
433 QString text = twi->text();

Callers

nothing calls this directly

Calls 2

nh_qsprintfFunction · 0.85
SelectableMethod · 0.45

Tested by

no test coverage detected