Skip to content

Commit 753b853

Browse files
committed
Various Updates
- resizable columns - TCP settings in a tab - wildcard bug fix - timestamp logging bug fix
1 parent cf5c024 commit 753b853

9 files changed

+1652
-1491
lines changed

OSCRouter/LogWidget.cpp

+310
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
2+
// Copyright (c) 2018 Electronic Theatre Controls, Inc., http://www.etcconnect.com
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
#include "LogWidget.h"
23+
24+
// must be last include
25+
#include "LeakWatcher.h"
26+
27+
////////////////////////////////////////////////////////////////////////////////
28+
29+
LogWidget::LogWidget(size_t maxLineCount, QWidget *parent)
30+
: QWidget(parent)
31+
, m_LineHeight(0)
32+
, m_LineWidth(0)
33+
, m_ForwardingWheelEvent(false)
34+
, m_AutoScroll(true)
35+
{
36+
m_Lines.resize(maxLineCount + 1);
37+
38+
QPalette pal(palette());
39+
pal.setColor(QPalette::Base, BG_COLOR);
40+
setPalette(pal);
41+
42+
setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
43+
44+
m_VScrollBar = new QScrollBar(Qt::Vertical, this);
45+
connect(m_VScrollBar, SIGNAL(valueChanged(int)), this, SLOT(onVScrollChanged(int)));
46+
47+
m_HScrollBar = new QScrollBar(Qt::Horizontal, this);
48+
connect(m_HScrollBar, SIGNAL(valueChanged(int)), this, SLOT(onHScrollChanged(int)));
49+
50+
UpdateFont();
51+
}
52+
53+
////////////////////////////////////////////////////////////////////////////////
54+
55+
void LogWidget::Clear()
56+
{
57+
size_t prevNumLines = GetNumLines();
58+
int prevLineWidth = m_LineWidth;
59+
60+
m_Index = sRingBufferIndex();
61+
m_LineWidth = 0;
62+
63+
if (GetNumLines() != prevNumLines)
64+
UpdateVScrollBar();
65+
66+
if (m_LineWidth != prevLineWidth)
67+
UpdateHScrollBar();
68+
69+
update();
70+
}
71+
72+
////////////////////////////////////////////////////////////////////////////////
73+
74+
void LogWidget::Log(EosLog::LOG_Q &logQ)
75+
{
76+
if (logQ.empty() || m_Lines.empty())
77+
return;
78+
79+
size_t prevNumLines = GetNumLines();
80+
81+
for (EosLog::LOG_Q::const_iterator i = logQ.begin(); i != logQ.end(); i++)
82+
{
83+
const EosLog::sLogMsg &msg = *i;
84+
85+
sLine &line = m_Lines[m_Index.tail];
86+
87+
if (msg.text.c_str())
88+
{
89+
line.text = QString::fromUtf8(msg.text.c_str());
90+
91+
switch (msg.type)
92+
{
93+
case EosLog::LOG_MSG_TYPE_DEBUG: line.color = MUTED_COLOR; break;
94+
case EosLog::LOG_MSG_TYPE_WARNING: line.color = WARNING_COLOR; break;
95+
case EosLog::LOG_MSG_TYPE_ERROR: line.color = ERROR_COLOR; break;
96+
case EosLog::LOG_MSG_TYPE_RECV: line.color = RECV_COLOR; break;
97+
case EosLog::LOG_MSG_TYPE_SEND: line.color = SEND_COLOR; break;
98+
default: line.color = palette().color(QPalette::Text); break;
99+
}
100+
}
101+
else
102+
line.text.clear();
103+
104+
if (++m_Index.tail >= m_Lines.size())
105+
m_Index.tail = 0;
106+
107+
if (m_Index.tail == m_Index.head)
108+
{
109+
m_Index.head = (m_Index.tail + 1);
110+
if (m_Index.head >= m_Lines.size())
111+
m_Index.head = 0;
112+
}
113+
}
114+
115+
if (GetNumLines() != prevNumLines)
116+
UpdateVScrollBar();
117+
118+
update();
119+
}
120+
121+
////////////////////////////////////////////////////////////////////////////////
122+
123+
size_t LogWidget::GetNumLines() const
124+
{
125+
if (m_Index.tail > m_Index.head)
126+
return (m_Index.tail - m_Index.head);
127+
else if (m_Index.tail < m_Index.head)
128+
return (m_Lines.size() - 1);
129+
return 0;
130+
}
131+
132+
////////////////////////////////////////////////////////////////////////////////
133+
134+
void LogWidget::GetContentsRect(QRect &r) const
135+
{
136+
r = rect().adjusted(0, 0, -m_VScrollBar->width(), -m_HScrollBar->height());
137+
}
138+
139+
////////////////////////////////////////////////////////////////////////////////
140+
141+
void LogWidget::UpdateFont()
142+
{
143+
m_LineHeight = QFontMetrics(font()).height();
144+
}
145+
146+
////////////////////////////////////////////////////////////////////////////////
147+
148+
void LogWidget::UpdateVScrollBar()
149+
{
150+
bool wasAtBottom = (!m_VScrollBar->isEnabled() || m_VScrollBar->value() == m_VScrollBar->maximum());
151+
152+
if (m_Lines.empty() || m_LineHeight < 1)
153+
{
154+
m_VScrollBar->setEnabled(false);
155+
}
156+
else
157+
{
158+
QRect r;
159+
GetContentsRect(r);
160+
size_t lineCount = GetNumLines();
161+
size_t linesPerPage = r.height() / m_LineHeight;
162+
if (linesPerPage >= lineCount)
163+
{
164+
m_VScrollBar->setEnabled(false);
165+
}
166+
else
167+
{
168+
size_t range = (lineCount - linesPerPage);
169+
m_VScrollBar->setMinimum(0);
170+
m_VScrollBar->setMaximum(static_cast<int>(range));
171+
m_VScrollBar->setEnabled(true);
172+
}
173+
}
174+
175+
if (m_AutoScroll && wasAtBottom && m_VScrollBar->isEnabled())
176+
m_VScrollBar->setValue(m_VScrollBar->maximum());
177+
}
178+
179+
////////////////////////////////////////////////////////////////////////////////
180+
181+
void LogWidget::UpdateHScrollBar()
182+
{
183+
QRect r;
184+
GetContentsRect(r);
185+
if (m_LineWidth > r.width())
186+
{
187+
m_HScrollBar->setMinimum(0);
188+
m_HScrollBar->setMaximum(m_LineWidth - r.width());
189+
m_HScrollBar->setEnabled(true);
190+
}
191+
else
192+
m_HScrollBar->setEnabled(false);
193+
}
194+
195+
////////////////////////////////////////////////////////////////////////////////
196+
197+
bool LogWidget::event(QEvent *event)
198+
{
199+
switch (event->type())
200+
{
201+
case QEvent::FontChange: UpdateFont(); break;
202+
}
203+
204+
return QWidget::event(event);
205+
}
206+
207+
////////////////////////////////////////////////////////////////////////////////
208+
209+
void LogWidget::resizeEvent(QResizeEvent * /*event*/)
210+
{
211+
QSize vsh(m_VScrollBar->sizeHint());
212+
QSize hsh(m_HScrollBar->sizeHint());
213+
m_VScrollBar->setGeometry(width() - vsh.width(), 0, vsh.width(), height() - hsh.height());
214+
m_HScrollBar->setGeometry(0, height() - hsh.height(), width() - vsh.width(), hsh.height());
215+
UpdateVScrollBar();
216+
UpdateHScrollBar();
217+
}
218+
219+
////////////////////////////////////////////////////////////////////////////////
220+
221+
void LogWidget::paintEvent(QPaintEvent * /*event*/)
222+
{
223+
QPainter painter(this);
224+
painter.fillRect(QRect(0, 0, width(), height()), palette().color(QPalette::Window));
225+
226+
size_t lineCount = GetNumLines();
227+
228+
if (lineCount == 0 || m_Lines.empty())
229+
return;
230+
231+
int x = 0;
232+
int y = 0;
233+
int bottom = m_HScrollBar->y();
234+
int maxLineWidth = 0;
235+
QRect bounds;
236+
237+
size_t index = m_Index.head;
238+
239+
if (m_VScrollBar->isEnabled())
240+
{
241+
int scrollOffset = m_VScrollBar->value();
242+
if (scrollOffset >= 0)
243+
{
244+
size_t offset = static_cast<size_t>(scrollOffset);
245+
if (offset < lineCount)
246+
index = ((index + offset) % m_Lines.size());
247+
}
248+
}
249+
250+
if (m_HScrollBar->isEnabled())
251+
x -= m_HScrollBar->value();
252+
253+
QRect r;
254+
GetContentsRect(r);
255+
painter.setClipRect(r);
256+
257+
while (index != m_Index.tail)
258+
{
259+
const sLine &line = m_Lines[index];
260+
QRect textRect(x, y, width() - x, m_LineHeight);
261+
if (y > bottom)
262+
break;
263+
264+
painter.setPen(line.color);
265+
painter.drawText(textRect, Qt::AlignLeft, line.text, &bounds);
266+
y += m_LineHeight;
267+
268+
if (bounds.width() > maxLineWidth)
269+
maxLineWidth = bounds.width();
270+
271+
if (++index >= m_Lines.size())
272+
index = 0;
273+
}
274+
275+
if (m_LineWidth < maxLineWidth)
276+
{
277+
m_LineWidth = maxLineWidth;
278+
UpdateHScrollBar();
279+
}
280+
}
281+
282+
////////////////////////////////////////////////////////////////////////////////
283+
284+
void LogWidget::wheelEvent(QWheelEvent *event)
285+
{
286+
if (!m_ForwardingWheelEvent && m_VScrollBar->isEnabled())
287+
{
288+
m_ForwardingWheelEvent = true;
289+
QApplication::sendEvent(m_VScrollBar, event);
290+
m_ForwardingWheelEvent = false;
291+
}
292+
293+
event->accept();
294+
}
295+
296+
////////////////////////////////////////////////////////////////////////////////
297+
298+
void LogWidget::onVScrollChanged(int /*value*/)
299+
{
300+
update();
301+
}
302+
303+
////////////////////////////////////////////////////////////////////////////////
304+
305+
void LogWidget::onHScrollChanged(int /*value*/)
306+
{
307+
update();
308+
}
309+
310+
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)