From 227683b0116dce54502b081b8932781a20fe437a Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Fri, 15 Mar 2019 00:56:41 +0100 Subject: More precise program and memory tableviews column width calculation. This should resolve text elide problem experienced on some combination of desktop/fonts setting. Signed-off-by: Pavel Pisa --- qtmips_gui/hinttabledelegate.cpp | 50 +++++++++++++++++++++++++++++++++++++ qtmips_gui/hinttabledelegate.h | 54 ++++++++++++++++++++++++++++++++++++++++ qtmips_gui/memorydock.cpp | 1 + qtmips_gui/memorymodel.cpp | 1 + qtmips_gui/memorymodel.h | 1 + qtmips_gui/memorytableview.cpp | 40 ++++++++++++++++++++--------- qtmips_gui/memorytableview.h | 1 + qtmips_gui/programtableview.cpp | 42 ++++++++++++++++++++++--------- qtmips_gui/qtmips_gui.pro | 6 +++-- 9 files changed, 170 insertions(+), 26 deletions(-) create mode 100644 qtmips_gui/hinttabledelegate.cpp create mode 100644 qtmips_gui/hinttabledelegate.h (limited to 'qtmips_gui') diff --git a/qtmips_gui/hinttabledelegate.cpp b/qtmips_gui/hinttabledelegate.cpp new file mode 100644 index 0000000..a8b0af4 --- /dev/null +++ b/qtmips_gui/hinttabledelegate.cpp @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: GPL-2.0+ +/******************************************************************************* + * QtMips - MIPS 32-bit Architecture Subset Simulator + * + * Implemented to support following courses: + * + * B35APO - Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b35apo + * + * B4M35PAP - Advanced Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b4m35pap/start + * + * Copyright (c) 2017-2019 Karel Koci + * Copyright (c) 2019 Pavel Pisa + * + * Faculty of Electrical Engineering (http://www.fel.cvut.cz) + * Czech Technical University (http://www.cvut.cz/) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + ******************************************************************************/ + +#include +#include "hinttabledelegate.h" + +QSize HintTableDelegate::sizeHintForText(const QStyleOptionViewItem &option, + const QModelIndex &index, const QString &str) const { + QStyleOptionViewItem opt = option; + initStyleOption(&opt, index); + const QWidget *widget = option.widget; + QStyle *style = widget ? widget->style() : QApplication::style(); + + opt.features |= QStyleOptionViewItem::HasDisplay; + opt.text = str; + + return style->sizeFromContents(QStyle::CT_ItemViewItem, &opt, QSize(), widget); +} diff --git a/qtmips_gui/hinttabledelegate.h b/qtmips_gui/hinttabledelegate.h new file mode 100644 index 0000000..629c6de --- /dev/null +++ b/qtmips_gui/hinttabledelegate.h @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: GPL-2.0+ +/******************************************************************************* + * QtMips - MIPS 32-bit Architecture Subset Simulator + * + * Implemented to support following courses: + * + * B35APO - Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b35apo + * + * B4M35PAP - Advanced Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b4m35pap/start + * + * Copyright (c) 2017-2019 Karel Koci + * Copyright (c) 2019 Pavel Pisa + * + * Faculty of Electrical Engineering (http://www.fel.cvut.cz) + * Czech Technical University (http://www.cvut.cz/) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + ******************************************************************************/ + +#ifndef HINTTABLEDELEGATE_H +#define HINTTABLEDELEGATE_H + +#include +#include + +class HintTableDelegate : public QStyledItemDelegate +{ + Q_OBJECT + + using Super = QStyledItemDelegate; + +public: + HintTableDelegate(QWidget *parent = 0) : Super(parent) {} + QSize sizeHintForText(const QStyleOptionViewItem &option, + const QModelIndex &index, const QString &str) const; +}; + +#endif // HINTTABLEDELEGATE_H diff --git a/qtmips_gui/memorydock.cpp b/qtmips_gui/memorydock.cpp index 2e0f780..2c170e9 100644 --- a/qtmips_gui/memorydock.cpp +++ b/qtmips_gui/memorydock.cpp @@ -93,6 +93,7 @@ MemoryDock::MemoryDock(QWidget *parent, QSettings *settings) : Super(parent) { go_edit, SLOT(set_value(std::uint32_t))); connect(this, SIGNAL(focus_addr(std::uint32_t)), memory_content, SLOT(focus_address(std::uint32_t))); + connect(memory_model, SIGNAL(setup_done()), memory_content, SLOT(recompute_columns())); } void MemoryDock::setup(machine::QtMipsMachine *machine) { diff --git a/qtmips_gui/memorymodel.cpp b/qtmips_gui/memorymodel.cpp index b88c5b7..c8a159c 100644 --- a/qtmips_gui/memorymodel.cpp +++ b/qtmips_gui/memorymodel.cpp @@ -156,6 +156,7 @@ void MemoryModel::setup(machine::QtMipsMachine *machine) { if (machine != nullptr) connect(machine, SIGNAL(post_tick()), this, SLOT(check_for_updates())); emit update_all(); + emit setup_done(); } void MemoryModel::setCellsPerRow(unsigned int cells) { diff --git a/qtmips_gui/memorymodel.h b/qtmips_gui/memorymodel.h index 01b5310..bab07fa 100644 --- a/qtmips_gui/memorymodel.h +++ b/qtmips_gui/memorymodel.h @@ -112,6 +112,7 @@ public slots: signals: void cell_size_changed(); + void setup_done(); private: enum MemoryCellSize cell_size; diff --git a/qtmips_gui/memorytableview.cpp b/qtmips_gui/memorytableview.cpp index 74729c5..0ff6bfb 100644 --- a/qtmips_gui/memorytableview.cpp +++ b/qtmips_gui/memorytableview.cpp @@ -41,8 +41,10 @@ #include #include "memorytableview.h" #include "memorymodel.h" +#include "hinttabledelegate.h" MemoryTableView::MemoryTableView(QWidget *parent, QSettings *settings) : Super(parent) { + setItemDelegate(new HintTableDelegate); connect(verticalScrollBar() , SIGNAL(valueChanged(int)), this, SLOT(adjust_scroll_pos_check())); connect(this , SIGNAL(adjust_scroll_pos_queue()), @@ -58,31 +60,41 @@ void MemoryTableView::addr0_save_change(std::uint32_t val) { void MemoryTableView::adjustColumnCount() { MemoryModel *m = dynamic_cast(model()); + if (m == nullptr) + return; + + HintTableDelegate *delegate = dynamic_cast(itemDelegate()); + if (delegate == nullptr) + return; - if (horizontalHeader()->count() >= 2 && m != nullptr) { + if (horizontalHeader()->count() >= 2) { + QModelIndex idx; QFontMetrics fm(*m->getFont()); - int width0 = fm.width("0x00000000"); + idx = m->index(0, 0); + // int width0_dh = itemDelegate(idx)->sizeHint(viewOptions(), idx).width() + 2; + int width0_dh = delegate->sizeHintForText(viewOptions(), idx, + "0x00000000").width() + 2; horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed); - horizontalHeader()->resizeSection(0, width0); + horizontalHeader()->resizeSection(0, width0_dh); + idx = m->index(0, 1); QString t = ""; t.fill(QChar('0'), m->cellSizeBytes() * 2); - /* t + = " C"; */ - int width1 = fm.width(t); - if (width1 < fm.width("+99")) - width1 = fm.width("+99"); + int width1_dh = delegate->sizeHintForText(viewOptions(), idx, t).width() + 2; + if (width1_dh < fm.width("+99")) + width1_dh = fm.width("+99"); horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed); - horizontalHeader()->resizeSection(1, width1); + horizontalHeader()->resizeSection(1, width1_dh); int w = verticalHeader()->width() + 4; unsigned int cells; - width0 = columnWidth(0); - width1 = columnWidth(1); + int width0 = columnWidth(0); + int width1 = columnWidth(1); w = width() - w - width0; - if (w < width1 + 2) { + if (w < width1 + 4) { cells = 1; } else { - cells = w / (width1 + 2); + cells = w / (width1 + 4); } if (cells != m->cellsPerRow()) { m->setCellsPerRow(cells); @@ -98,6 +110,10 @@ void MemoryTableView::adjustColumnCount() { } } +void MemoryTableView::recompute_columns() { + adjustColumnCount(); +} + void MemoryTableView::set_cell_size(int index) { std::uint32_t address; int row; diff --git a/qtmips_gui/memorytableview.h b/qtmips_gui/memorytableview.h index a84b80e..0b71444 100644 --- a/qtmips_gui/memorytableview.h +++ b/qtmips_gui/memorytableview.h @@ -58,6 +58,7 @@ public slots: void set_cell_size(int index); void go_to_address(std::uint32_t address); void focus_address(std::uint32_t address); + void recompute_columns(); protected: void keyPressEvent(QKeyEvent *event); private slots: diff --git a/qtmips_gui/programtableview.cpp b/qtmips_gui/programtableview.cpp index a684692..b6259dc 100644 --- a/qtmips_gui/programtableview.cpp +++ b/qtmips_gui/programtableview.cpp @@ -41,8 +41,10 @@ #include #include "programtableview.h" #include "programmodel.h" +#include "hinttabledelegate.h" ProgramTableView::ProgramTableView(QWidget *parent, QSettings *settings) : Super(parent) { + setItemDelegate(new HintTableDelegate); connect(verticalScrollBar() , SIGNAL(valueChanged(int)), this, SLOT(adjust_scroll_pos_check())); connect(this , SIGNAL(adjust_scroll_pos_queue()), @@ -57,28 +59,44 @@ void ProgramTableView::addr0_save_change(std::uint32_t val) { } void ProgramTableView::adjustColumnCount() { - int cwidth; + QModelIndex idx; + int cwidth_dh; int totwidth; + ProgramModel *m = dynamic_cast(model()); if (m == nullptr) return; - QFontMetrics fm(*m->getFont()); - cwidth = fm.width("Bp"); - totwidth = cwidth; + HintTableDelegate *delegate = dynamic_cast(itemDelegate()); + if (delegate == nullptr) + return; + + idx = m->index(0, 0); + cwidth_dh = delegate->sizeHintForText(viewOptions(), idx, + "Bp").width() + 2; horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed); - horizontalHeader()->resizeSection(0, cwidth); - cwidth = fm.width("0x00000000"); - totwidth += cwidth; + horizontalHeader()->resizeSection(0, cwidth_dh); + totwidth = cwidth_dh; + + idx = m->index(0, 1); + cwidth_dh = delegate->sizeHintForText(viewOptions(), idx, + "0x00000000").width() + 2; horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed); - horizontalHeader()->resizeSection(1, cwidth); - totwidth += cwidth; - cwidth = fm.width("00000000"); + horizontalHeader()->resizeSection(1, cwidth_dh); + totwidth += cwidth_dh; + + idx = m->index(0, 2); + cwidth_dh = delegate->sizeHintForText(viewOptions(), idx, + "00000000").width() + 2; horizontalHeader()->setSectionResizeMode(2, QHeaderView::Fixed); - horizontalHeader()->resizeSection(2, cwidth); + horizontalHeader()->resizeSection(2, cwidth_dh); + totwidth += cwidth_dh; + horizontalHeader()->setSectionResizeMode(3, QHeaderView::Stretch); - totwidth += fm.width("BEQ $18, $17, 0x80020058"); + idx = m->index(0, 3); + totwidth += delegate->sizeHintForText(viewOptions(), idx, + "BEQ $18, $17, 0x80020058").width() + 2; totwidth += verticalHeader()->width(); setColumnHidden(2, totwidth > width()); diff --git a/qtmips_gui/qtmips_gui.pro b/qtmips_gui/qtmips_gui.pro index e10631b..d090b4d 100644 --- a/qtmips_gui/qtmips_gui.pro +++ b/qtmips_gui/qtmips_gui.pro @@ -63,7 +63,8 @@ SOURCES += \ coreview/multitext.cpp \ fontsize.cpp \ gotosymboldialog.cpp \ - cop0dock.cpp + cop0dock.cpp \ + hinttabledelegate.cpp HEADERS += \ mainwindow.h \ @@ -102,7 +103,8 @@ HEADERS += \ coreview/multitext.h \ fontsize.h \ gotosymboldialog.h \ - cop0dock.h + cop0dock.h \ + hinttabledelegate.h FORMS += \ NewDialog.ui \ -- cgit v1.2.3