-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.cpp
More file actions
151 lines (119 loc) · 3.63 KB
/
background.cpp
File metadata and controls
151 lines (119 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "background.h"
#include "resourcepaths.h"
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QListView>
#include <QDir>
#include <QIcon>
#include <QPainter>
Background::Background(QGraphicsScene *scene, QObject *parent)
: QGraphicsObject()
, m_scene(scene)
, m_average_color(100, 100, 100)
{
setParent(parent); // ← Critical line
m_scene->addItem(this);
setZValue(-1000);
}
QRectF Background::boundingRect() const
{
if (m_scene && !m_scene->views().isEmpty()) {
QGraphicsView *view = m_scene->views().first();
return QRectF(0, 0, view->viewport()->width(), view->viewport()->height());
}
// Fallback for safety (never hit in practice)
return QRectF(0, 0, 800, 600);
}
void Background::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
if (m_pixmap.isNull() || !m_scene || m_scene->views().isEmpty()) {
return;
}
QGraphicsView *view = m_scene->views().first();
QRectF viewRect(0, 0, view->viewport()->width(), view->viewport()->height());
QPixmap scaled = m_pixmap.scaled(viewRect.size().toSize(),
Qt::KeepAspectRatioByExpanding,
Qt::SmoothTransformation);
qreal x = (viewRect.width() - scaled.width()) / 2.0;
qreal y = (viewRect.height() - scaled.height()) / 2.0;
painter->drawPixmap(QPointF(x, y), scaled);
}
void Background::onBackgroundSelected(const QModelIndex &index)
{
QString path = index.data(Qt::UserRole).toString();
if (!path.isEmpty()) {
setBackgroundPixmap(path);
}
}
void Background::setBackgroundPixmap(const QString path)
{
QPixmap newPixmap(path);
if (newPixmap.isNull()) return;
fullpath = path;
m_pixmap = newPixmap;
update(); // Repaint the graphics item
setCredits();
calcAverageColor();
emit backgroundChanged(path); // Notify MainWindow
}
void Background::setCredits()
{
int index = 0;
QString suffix;
for (int b = 1; b <= BACKGROUND_LAST_INDEX; b++) {
if (fullpath.contains(BACKGROUND_FILES[b])) {
suffix = FILES_CREDIT[b];
index = b;
break;
}
}
if (!index) {
credit = "";
return;
}
creditColor = CREDITS_COLOR[index];
if ((index == 11) || (index == 12)) {
credit = tr("Background created using gimp 2.10.18");
} else {
credit = tr("Background image by: ") + suffix;
}
}
// This functions is used to load legacy index INT format in configuration file.
void Background::setBackground(int index)
{
QString filename;
if (index == BACKGROUND_NONE)
return;
filename = BACKGROUND_FILES[index];
creditColor = CREDITS_COLOR[index];
QFile aFile;
fullpath = getResourceFile(QString("backgrounds/") + filename);
if (fullpath.isEmpty()) {
return;
}
setBackgroundPixmap(fullpath);
}
void Background::calcAverageColor()
{
QImage image = m_pixmap.toImage();
if (image.isNull()) return;
// Resize for speed (150x150 is plenty accurate for backgrounds)
image = image.scaled(150, 150, Qt::KeepAspectRatio, Qt::SmoothTransformation);
ulong r = 0, g = 0, b = 0;
int pixelCount = image.width() * image.height();
for (int y = 0; y < image.height(); ++y) {
const QRgb *line = reinterpret_cast<const QRgb*>(image.scanLine(y));
for (int x = 0; x < image.width(); ++x) {
r += qRed(line[x]);
g += qGreen(line[x]);
b += qBlue(line[x]);
}
}
m_average_color.setRgb( r / pixelCount, g / pixelCount, b / pixelCount );
}
QColor Background::getAverageColor()
{
return m_average_color;
}