1 /* 2 SPDX-FileCopyrightText: 2011-2018 Graeme Gott <graeme@gottcode.org> 3 4 SPDX-License-Identifier: GPL-3.0-or-later 5 */ 6 7 #include "word_counts.h" 8 9 #include <QHBoxLayout> 10 #include <QLabel> 11 #include <QScrollBar> 12 13 #include <algorithm> 14 15 //----------------------------------------------------------------------------- 16 WordCounts(QWidget * parent)17WordCounts::WordCounts(QWidget* parent) 18 : QScrollArea(parent) 19 , m_show_max(false) 20 { 21 setBackgroundRole(QPalette::Mid); 22 setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); 23 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 24 25 QWidget* contents = new QWidget(this); 26 setWidget(contents); 27 setWidgetResizable(true); 28 29 QLabel* label = new QLabel(QString("<small><b>%1<br>%2</b></small>").arg(tr("Letters:"), tr("Found:")), contents); 30 label->setAlignment(Qt::AlignRight | Qt::AlignVCenter); 31 32 QHBoxLayout* layout = new QHBoxLayout(contents); 33 layout->setSpacing(12); 34 layout->addWidget(label); 35 36 for (int i = 0; i < 26; ++i) { 37 Group group; 38 group.length = i; 39 group.count = 0; 40 group.max = 0; 41 group.label = new QLabel(contents); 42 group.label->setAlignment(Qt::AlignRight | Qt::AlignVCenter); 43 group.label->setVisible(false); 44 layout->addWidget(group.label); 45 m_groups.append(group); 46 } 47 layout->addStretch(); 48 49 updateString(); 50 setMinimumHeight(contents->sizeHint().height() + horizontalScrollBar()->sizeHint().height()); 51 } 52 53 //----------------------------------------------------------------------------- 54 findWord(const QString & word)55void WordCounts::findWord(const QString& word) 56 { 57 Q_ASSERT(word.length() < 26); 58 Group& group = m_groups[word.length()]; 59 group.count++; 60 updateString(); 61 } 62 63 //----------------------------------------------------------------------------- 64 setMaximumsVisible(bool visible)65void WordCounts::setMaximumsVisible(bool visible) 66 { 67 m_show_max = visible; 68 updateString(); 69 } 70 71 //----------------------------------------------------------------------------- 72 setWords(const QStringList & words)73void WordCounts::setWords(const QStringList& words) 74 { 75 for (int i = 0; i < 26; ++i) { 76 Group& group = m_groups[i]; 77 group.count = 0; 78 group.max = 0; 79 } 80 81 for (const QString& word : words) { 82 Q_ASSERT(word.length() < 26); 83 Group& group = m_groups[word.length()]; 84 group.max++; 85 } 86 87 updateString(); 88 } 89 90 //----------------------------------------------------------------------------- 91 updateString()92void WordCounts::updateString() 93 { 94 // Set text in columns 95 int max_count = 0; 96 int max = 0; 97 for (int i = 0; i < 26; ++i) { 98 Group& group = m_groups[i]; 99 max = std::max(max, group.max); 100 max_count = std::max(max_count, group.count); 101 102 QString text; 103 if (!m_show_max) { 104 if (group.count > 0) { 105 text = tr("%1<br><b>%2</b>").arg(group.length).arg(group.count); 106 } 107 } else { 108 if (group.max > 0) { 109 if (group.count > 0) { 110 text = tr("%1<br><b>%2/%3</b>").arg(group.length).arg(group.count).arg(group.max); 111 } else { 112 text = tr("%1<br>%2/%3").arg(group.length).arg(group.count).arg(group.max); 113 } 114 } 115 } 116 group.label->setText("<small>" + text + "</small>"); 117 group.label->setVisible(!text.isEmpty()); 118 } 119 int width = fontMetrics().boundingRect(m_show_max ? QString("%1/%2").arg(max_count).arg(max) : QString::number(max)).width(); 120 121 // Resize columns 122 for (int i = 0; i < 26; ++i) { 123 m_groups[i].label->setMinimumWidth(width); 124 } 125 } 126 127 //----------------------------------------------------------------------------- 128