/[qspeakers]/branches/qtcharts/portedbox.cpp
ViewVC logotype

Contents of /branches/qtcharts/portedbox.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 267 - (show annotations)
Thu Oct 22 00:53:56 2020 UTC (7 months, 3 weeks ago) by ben
File size: 5351 byte(s)
Avoid deprecated drawing methods, update translation
1 #include <QLocale>
2 #include <QDebug>
3
4 #include <math.h>
5
6 #include "portedbox.h"
7
8 PortedBox::PortedBox(double volume, double resfreq, unsigned int portnum, double portdiam, double portlen) :
9 box(volume)
10 {
11 setResFreq(resfreq);
12 portNum = portnum;
13 setPortDiam(portdiam);
14 setPortLen(portlen);
15 slotActivated = false;
16 }
17
18 void PortedBox::setBoxVolume(double vol)
19 {
20 box.setVolume(vol);
21 }
22
23 void PortedBox::setPortNum(unsigned int value)
24 {
25 portNum = value;
26 }
27
28 void PortedBox::setPortLen(double len)
29 {
30 portLen = double ((int)(len * 100) / 100.0);
31 }
32
33 void PortedBox::setPortDiam(double diam)
34 {
35 portDiam = double ((int)(diam * 100) / 100.0);
36 }
37
38 void PortedBox::setSlotWidth(double width)
39 {
40 slotWidth = double ((int)(width * 100) / 100.0);
41 }
42
43 void PortedBox::setResFreq(double value)
44 {
45 resFreq = double ((int)(value * 100) / 100.0);
46 }
47
48 void PortedBox::setSlotPortActivated(bool enable)
49 {
50 slotActivated = enable;
51 }
52
53 double PortedBox::getBoxVolume() const
54 {
55 return box.getVolume();
56 }
57
58 unsigned int PortedBox::getPortNum() const
59 {
60 return portNum;
61 }
62
63 double PortedBox::getPortLen() const
64 {
65 if (!slotActivated)
66 return portLen;
67
68 double x = getSlotWidth() / getSlotHeight();
69 if (x < 1) {
70 x = getSlotHeight() / getSlotWidth();
71 }
72
73 /* estimated from https://sites.google.com/site/francisaudio69/ */
74 return double ((int)((1.1 - log10(x + 1.1) * 0.35) * portLen * 100) / 100.0);
75 }
76
77 double PortedBox::getPortDiam() const
78 {
79 return portDiam;
80 }
81
82 bool PortedBox::getSlotPortActivated() const
83 {
84 return slotActivated;
85 }
86
87 double PortedBox::getSlotWidth() const
88 {
89 return slotWidth;
90 }
91
92 double PortedBox::getSlotHeight() const
93 {
94 /* width is manual, height is computed from eq. diam. */
95 return double ((int)((PI * pow(portDiam, 2.0)) / (4.0 * slotWidth) * 100) / 100.0);
96 }
97
98 double PortedBox::getResFreq() const
99 {
100 return resFreq;
101 }
102
103 void PortedBox::updatePorts(double sd, double xmax)
104 {
105 /* compute minimum port diameter and optionaly set it */
106 double vol = sd * xmax * 0.001; /* m³ */
107 double dmin = 100 * (20.3 * pow((pow(vol, 2.0) / getResFreq()), 0.25)) / sqrt(getPortNum()); /* cm */
108 setPortDiam(dmin);
109 updateSlots();
110 updatePortsLength();
111 }
112
113 void PortedBox::updateSlots()
114 {
115 /* compute square slot */
116 double diam = getPortDiam();
117 double s = sqrt(PI * pow(diam, 2.0) / 4.0);
118
119 /* consider "width" over "height" and reset if too small */
120 if (slotWidth == 0.0)
121 slotWidth = double ((int)(s * 100) / 100.0);
122 }
123
124 void PortedBox::updatePortsLength()
125 {
126 /* compute resulting length */
127 double diam = getPortDiam();
128 double res = getResFreq();
129 double vol = getBoxVolume();
130 double num = getPortNum();
131 double plen = ((23562.5 * pow(diam, 2.0)) * num / (vol * pow(res, 2.0))) - (K * diam);
132 setPortLen(plen);
133 }
134
135 QDomElement PortedBox::toDomElement(QDomDocument& doc) const
136 {
137 QLocale c(QLocale::C);
138 QDomElement e = Box::toDomElement(doc);
139
140 e.setAttribute("type", "ported");
141 e.setAttribute("res", c.toString(resFreq));
142
143 QDomElement b = box.toDomElement(doc);
144 e.appendChild(b);
145
146 QDomElement p = doc.createElement("port");
147
148 p.setAttribute("num", c.toString(portNum));
149 p.setAttribute("len", c.toString(portLen));
150 p.setAttribute("diam", c.toString(portDiam));
151 p.setAttribute("width", c.toString(slotWidth));
152 p.setAttribute("slot", c.toString(slotActivated ? 1 : 0));
153
154 e.appendChild(p);
155
156 return e;
157 }
158
159 void PortedBox::fromDomElement(const QDomElement &e)
160 {
161 QLocale c(QLocale::C);
162 Box::fromDomElement(e);
163
164 if (e.attribute("type") != "ported") {
165 qWarning() << __func__ << "wrong box type! (not ported, giving up)";
166 return;
167 }
168
169 resFreq = c.toDouble(e.attribute("res"));
170
171 QDomElement b = e.elementsByTagName("box").at(0).toElement();
172 box.fromDomElement(b);
173
174 QDomElement p = e.elementsByTagName("port").at(0).toElement();
175
176 portNum = c.toUInt(p.attribute("num"));
177 portLen = c.toDouble(p.attribute("len"));
178 portDiam = c.toDouble(p.attribute("diam"));
179 slotWidth = c.toDouble(p.attribute("width"));
180 slotActivated = c.toUInt(p.attribute("slot"));
181 }
182
183 void PortedBox::render(QPainter *painter, const QRectF &area) const
184 {
185 if (!painter)
186 return;
187
188 #define PORTED_PARAMS 5
189
190 QString params[PORTED_PARAMS];
191 qreal tab = area.left();
192
193 painter->drawRoundedRect(area.toRect(), 5, 5);
194
195 params[0] = QObject::tr("Vol. %1 L").arg(getBoxVolume());
196 if (!slotActivated) {
197 params[1] = QObject::tr("Port Diam. %1 cm").arg(getPortDiam());
198 } else {
199 params[1] = QObject::tr("Slot port %1 x %2 cm").arg(QString::number(getSlotWidth(), 'f', 2), QString::number(getSlotHeight(), 'f', 2));
200 }
201 params[2] = QObject::tr("Port Len. %1 cm").arg(QString::number(getPortLen(), 'f', 2));
202 params[3] = QObject::tr("Port Num. %1").arg(QString::number(getPortNum()));
203 params[4] = QObject::tr("Fb %1 Hz").arg(QString::number(getResFreq()));
204
205 for (int i = 0; i < PORTED_PARAMS; i++) {
206 QString text = params[i];
207 QRectF where(tab, area.top(), area.width() / PORTED_PARAMS, area.height());
208 QTextOption option(Qt::AlignVCenter|Qt::AlignLeft);
209 painter->drawText(where, text, option);
210 tab += area.width() / PORTED_PARAMS;
211 }
212 }

  ViewVC Help
Powered by ViewVC 1.1.26