BibleTime
csearchanalysisitem.cpp
Go to the documentation of this file.
1/*********
2*
3* In the name of the Father, and of the Son, and of the Holy Spirit.
4*
5* This file is part of BibleTime's source code, https://bibletime.info/
6*
7* Copyright 1999-2025 by the BibleTime developers.
8* The BibleTime source code is licensed under the GNU General Public License
9* version 2.0.
10*
11**********/
12
13#include "csearchanalysisitem.h"
14
15#include <QBrush>
16#include <QFont>
17#include <QPainter>
18#include <QPoint>
19#include <QRect>
20#include <QRectF>
21#include <utility>
23
24
25namespace Search {
26
27const int ITEM_TEXT_SIZE = 8;
28
29//used for the shift between the bars
30const int BAR_DELTAX = 4;
31const int BAR_DELTAY = 2;
32const int BAR_WIDTH = 2 + (2*BAR_DELTAX); //should be equal or bigger than the label font size
33// Used for the text below the bars
34const int BAR_LOWER_BORDER = 100;
35
36CSearchAnalysisItem::CSearchAnalysisItem(QString bookname, int numModules)
37 : m_bookName(std::move(bookname))
38 , m_counts(numModules, 0u)
39{}
40
41/** Reimplementation. Draws the content of this item. */
42void CSearchAnalysisItem::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*) {
43 QFont f = painter->font();
44 f.setPointSize(ITEM_TEXT_SIZE);
45 painter->setFont(f);
46
47 auto const moduleCount = m_counts.size();
48
49 /**
50 * We have to paint so many bars as we have modules available (we use moduleCount)
51 * We paint inside the area which is given by height and width of this rectangle item
52 */
53 int index = 0;
54 int drawn = 0;
55 std::size_t Value = 0;
56
57 //find out the biggest value
58 for (index = 0;index < moduleCount; index++) {
59 if (m_counts[index] > Value) {
60 Value = m_counts[index];
61 }
62 }
63
64 while (drawn < moduleCount) {
65 for (index = 0; index < moduleCount; index++) {
66 if (m_counts[index] == Value) {
67 #define S(...) static_cast<int>(__VA_ARGS__)
68 QPoint p1(S(rect().x()) + (moduleCount - drawn - 1)*BAR_DELTAX,
69 S(rect().height()) + S(y()) - BAR_LOWER_BORDER - (moduleCount - drawn)*BAR_DELTAY);
70 QPoint p2(p1.x() + BAR_WIDTH,
71 p1.y() - S(!m_counts[index] ? 0 : ((m_counts[index])*m_scaleFactor)));
72 #undef S
73 QRect r(p1, p2);
74 painter->fillRect(r, QBrush(CSearchAnalysisScene::getColor(index)) );
75 painter->drawRect(r);
76 drawn++;
77 }
78 }
79 //finds the next smaller value
80 std::size_t newValue = 0u;
81 for (index = 0;index < moduleCount; index++)
82 if (m_counts[index] < Value && m_counts[index] >= newValue)
83 newValue = m_counts[index];
84 Value = newValue;
85 }
86 if (!m_bufferPixmap) {
87 m_bufferPixmap = std::make_unique<QPixmap>(width(), BAR_LOWER_BORDER);
88 //m_bufferPixmap->resize(width(),BAR_LOWER_BORDER);
89 m_bufferPixmap->fill();
90 QPainter p(m_bufferPixmap.get());
91 f = p.font();
92 f.setPointSize(ITEM_TEXT_SIZE);
93 p.setFont(f);
94 p.rotate(90);
95 p.drawText(QPoint(5, 0), m_bookName);
96 }
97 painter->drawPixmap(QPoint(int(rect().x()), int(rect().height() + y() - BAR_LOWER_BORDER)), *m_bufferPixmap);
98}
99
100/** Returns the width of this item. */
102 auto const moduleCount = m_counts.size();
103 return moduleCount * (moduleCount > 1 ? BAR_DELTAX : 0) + BAR_WIDTH;
104}
105
106}
std::unique_ptr< QPixmap > m_bufferPixmap
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override
QVector< std::size_t > m_counts
CSearchAnalysisItem(QString bookname, int numModules)
static QColor getColor(int index)
#define S(...)
QStringList r(content.left(bodyIndex))
const int ITEM_TEXT_SIZE
const int BAR_DELTAY
const int BAR_LOWER_BORDER
const int BAR_DELTAX
const int BAR_WIDTH