25 static const int g_maxMessages = 5; |
25 static const int g_maxMessages = 5; |
26 static const int g_expiry = 5; |
26 static const int g_expiry = 5; |
27 static const int g_fadeTime = 500; // msecs |
27 static const int g_fadeTime = 500; // msecs |
28 |
28 |
29 // ============================================================================= |
29 // ============================================================================= |
30 // ----------------------------------------------------------------------------- |
30 // |
31 MessageManager::MessageManager (QObject* parent) : |
31 MessageManager::MessageManager (QObject* parent) : |
32 QObject (parent) |
32 QObject (parent) |
33 { |
33 { |
34 m_ticker = new QTimer; |
34 m_ticker = new QTimer; |
35 m_ticker->start (100); |
35 m_ticker->start (100); |
36 connect (m_ticker, SIGNAL (timeout()), this, SLOT (tick())); |
36 connect (m_ticker, SIGNAL (timeout()), this, SLOT (tick())); |
37 } |
37 } |
38 |
38 |
39 // ============================================================================= |
39 // ============================================================================= |
40 // ----------------------------------------------------------------------------- |
40 // |
41 MessageManager::Line::Line (QString text) : |
41 MessageManager::Line::Line (QString text) : |
42 text (text), |
42 text (text), |
43 alpha (1.0f), |
43 alpha (1.0f), |
44 expiry (QDateTime::currentDateTime().addSecs (g_expiry)) {} |
44 expiry (QDateTime::currentDateTime().addSecs (g_expiry)) {} |
45 |
45 |
46 // ============================================================================= |
46 // ============================================================================= |
47 // Check this line's expiry and update alpha accordingly. Returns true if the |
47 // Check this line's expiry and update alpha accordingly. Returns true if the |
48 // line is to still stick around, false if it expired. 'changed' is updated to |
48 // line is to still stick around, false if it expired. 'changed' is updated to |
49 // whether the line has somehow changed since the last update. |
49 // whether the line has somehow changed since the last update. |
50 // ----------------------------------------------------------------------------- |
50 // |
51 bool MessageManager::Line::update (bool& changed) |
51 bool MessageManager::Line::update (bool& changed) |
52 { |
52 { |
53 changed = false; |
53 changed = false; |
54 QDateTime now = QDateTime::currentDateTime(); |
54 QDateTime now = QDateTime::currentDateTime(); |
55 int msec = now.msecsTo (expiry); |
55 int msec = now.msecsTo (expiry); |
71 return true; |
71 return true; |
72 } |
72 } |
73 |
73 |
74 // ============================================================================= |
74 // ============================================================================= |
75 // Add a line to the message manager. |
75 // Add a line to the message manager. |
76 // ----------------------------------------------------------------------------- |
76 // |
77 void MessageManager::addLine (QString line) |
77 void MessageManager::addLine (QString line) |
78 { |
78 { |
79 // If there's too many entries, pop the excess out |
79 // If there's too many entries, pop the excess out |
80 while (m_lines.size() >= g_maxMessages) |
80 while (m_lines.size() >= g_maxMessages) |
81 m_lines.removeFirst(); |
81 m_lines.removeFirst(); |
88 } |
88 } |
89 |
89 |
90 // ============================================================================= |
90 // ============================================================================= |
91 // Ticks the message manager. All lines are ticked and the renderer scene is |
91 // Ticks the message manager. All lines are ticked and the renderer scene is |
92 // redrawn if something changed. |
92 // redrawn if something changed. |
93 // ----------------------------------------------------------------------------- |
93 // |
94 void MessageManager::tick() |
94 void MessageManager::tick() |
95 { |
95 { |
96 if (m_lines.isEmpty()) |
96 if (m_lines.isEmpty()) |
97 return; |
97 return; |
98 |
98 |
111 if (changed && getRenderer()) |
111 if (changed && getRenderer()) |
112 getRenderer()->update(); |
112 getRenderer()->update(); |
113 } |
113 } |
114 |
114 |
115 // ============================================================================= |
115 // ============================================================================= |
116 // ----------------------------------------------------------------------------- |
116 // |
117 const QList<MessageManager::Line>& MessageManager::getLines() const |
117 const QList<MessageManager::Line>& MessageManager::getLines() const |
118 { |
118 { |
119 return m_lines; |
119 return m_lines; |
120 } |
120 } |
121 |
121 |
122 // ============================================================================= |
122 // ============================================================================= |
123 // log() interface - format the argument list and add the resulting string to |
123 // log() interface - format the argument list and add the resulting string to |
124 // the main message manager. |
124 // the main message manager. |
125 // ----------------------------------------------------------------------------- |
125 // |
126 void DoLog (std::initializer_list<StringFormatArg> args) |
126 void DoLog (std::initializer_list<StringFormatArg> args) |
127 { |
127 { |
128 const QString msg = DoFormat (args); |
128 const QString msg = DoFormat (args); |
129 |
129 |
130 for (QString& a : msg.split ("\n", QString::SkipEmptyParts)) |
130 for (QString& a : msg.split ("\n", QString::SkipEmptyParts)) |