src/messageLog.cc

changeset 861
83426c5fa732
parent 844
11587d419d2f
child 927
409b82a4765e
equal deleted inserted replaced
860:a496e72af069 861:83426c5fa732
20 #include <QDate> 20 #include <QDate>
21 #include "messageLog.h" 21 #include "messageLog.h"
22 #include "glRenderer.h" 22 #include "glRenderer.h"
23 #include "mainWindow.h" 23 #include "mainWindow.h"
24 24
25 static const int g_maxMessages = 5; 25 enum
26 static const int g_expiry = 5; 26 {
27 static const int g_fadeTime = 500; // msecs 27 MaxMessages = 5,
28 ExpireTime = 5000,
29 FadeTime = 500
30 };
28 31
29 // ============================================================================= 32 // -------------------------------------------------------------------------------------------------
30 // 33 //
31 MessageManager::MessageManager (QObject* parent) : 34 MessageManager::MessageManager (QObject* parent) :
32 QObject (parent) 35 QObject (parent)
33 { 36 {
34 m_ticker = new QTimer; 37 m_ticker = new QTimer;
35 m_ticker->start (100); 38 m_ticker->start (100);
36 connect (m_ticker, SIGNAL (timeout()), this, SLOT (tick())); 39 connect (m_ticker, SIGNAL (timeout()), this, SLOT (tick()));
37 } 40 }
38 41
39 // ============================================================================= 42 // -------------------------------------------------------------------------------------------------
40 // 43 //
41 MessageManager::Line::Line (QString text) : 44 MessageManager::Line::Line (QString text) :
42 text (text), 45 text (text),
43 alpha (1.0f), 46 alpha (1.0f),
44 expiry (QDateTime::currentDateTime().addSecs (g_expiry)) {} 47 expiry (QDateTime::currentDateTime().addMSecs (ExpireTime)) {}
45 48
46 // ============================================================================= 49 // -------------------------------------------------------------------------------------------------
47 // 50 //
48 bool MessageManager::Line::update (bool& changed) 51 bool MessageManager::Line::update (bool& changed)
49 { 52 {
50 changed = false; 53 changed = false;
51 QDateTime now = QDateTime::currentDateTime(); 54 QDateTime now = QDateTime::currentDateTime();
56 // Message line has expired 59 // Message line has expired
57 changed = true; 60 changed = true;
58 return false; 61 return false;
59 } 62 }
60 63
61 if (msec <= g_fadeTime) 64 if (msec <= FadeTime)
62 { 65 {
63 // Message line has not expired but is fading out 66 // Message line has not expired but is fading out
64 alpha = ( (float) msec) / g_fadeTime; 67 alpha = ( (float) msec) / FadeTime;
65 changed = true; 68 changed = true;
66 } 69 }
67 70
68 return true; 71 return true;
69 } 72 }
70 73
71 // ============================================================================= 74 // -------------------------------------------------------------------------------------------------
72 // Add a line to the message manager. 75 //
76 // Add a line to the message manager.
73 // 77 //
74 void MessageManager::addLine (QString line) 78 void MessageManager::addLine (QString line)
75 { 79 {
76 // If there's too many entries, pop the excess out 80 // If there's too many entries, pop the excess out
77 while (m_lines.size() >= g_maxMessages) 81 while (m_lines.size() >= MaxMessages)
78 m_lines.removeFirst(); 82 m_lines.removeFirst();
79 83
80 m_lines << Line (line); 84 m_lines << Line (line);
81 85
82 // Update the renderer view 86 // Update the renderer view
83 if (renderer()) 87 if (renderer())
84 renderer()->update(); 88 renderer()->update();
85 } 89 }
86 90
87 // ============================================================================= 91 // -------------------------------------------------------------------------------------------------
88 // Ticks the message manager. All lines are ticked and the renderer scene is 92 //
89 // redrawn if something changed. 93 // Ticks the message manager. All lines are ticked and the renderer scene is redrawn if something
94 // changed.
90 // 95 //
91 void MessageManager::tick() 96 void MessageManager::tick()
92 { 97 {
93 if (m_lines.isEmpty()) 98 if (m_lines.isEmpty())
94 return; 99 return;
116 return m_lines; 121 return m_lines;
117 } 122 }
118 123
119 // ============================================================================= 124 // =============================================================================
120 // 125 //
121 void printToLog (const QString& msg) 126 void PrintToLog (const QString& msg)
122 { 127 {
123 for (QString& a : msg.split ("\n", QString::SkipEmptyParts)) 128 for (QString& a : msg.split ("\n", QString::SkipEmptyParts))
124 { 129 {
125 if (g_win != null) 130 if (g_win != null)
126 g_win->addMessage (a); 131 g_win->addMessage (a);

mercurial