src/messagelog.cpp

changeset 665
4355e72ffd47
parent 527
47c068f2e7e3
child 539
72ad83a67165
equal deleted inserted replaced
664:e3a32a79a10a 665:4355e72ffd47
19 #include <QTimer> 19 #include <QTimer>
20 #include <QDate> 20 #include <QDate>
21 #include "messagelog.h" 21 #include "messagelog.h"
22 #include "gldraw.h" 22 #include "gldraw.h"
23 #include "gui.h" 23 #include "gui.h"
24 #include "moc_messagelog.cpp"
24 25
25 static const unsigned int g_maxMessages = 5; 26 static const int g_maxMessages = 5;
26 static const int g_expiry = 5; 27 static const int g_expiry = 5;
27 static const int g_fadeTime = 500; // msecs 28 static const int g_fadeTime = 500; // msecs
28 29
29 // ============================================================================= 30 // =============================================================================
30 // ----------------------------------------------------------------------------- 31 // -----------------------------------------------------------------------------
31 MessageManager::MessageManager (QObject* parent) : 32 MessageManager::MessageManager (QObject* parent) :
32 QObject (parent) { 33 QObject (parent)
33 m_ticker = new QTimer; 34 { m_ticker = new QTimer;
34 m_ticker->start (100); 35 m_ticker->start (100);
35 connect (m_ticker, SIGNAL (timeout()), this, SLOT (tick())); 36 connect (m_ticker, SIGNAL (timeout()), this, SLOT (tick()));
36 } 37 }
37 38
38 // ============================================================================= 39 // =============================================================================
39 // ----------------------------------------------------------------------------- 40 // -----------------------------------------------------------------------------
40 MessageManager::Line::Line (str text) : 41 MessageManager::Line::Line (str text) :
41 text (text), 42 text (text),
42 alpha (1.0f), 43 alpha (1.0f),
43 expiry (QDateTime::currentDateTime().addSecs (g_expiry)) {} 44 expiry (QDateTime::currentDateTime().addSecs (g_expiry)) {}
44 45
45 // ============================================================================= 46 // =============================================================================
46 // 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
47 // 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
48 // whether the line has somehow changed since the last update. 49 // whether the line has somehow changed since the last update.
49 // ----------------------------------------------------------------------------- 50 // -----------------------------------------------------------------------------
50 bool MessageManager::Line::update (bool& changed) { 51 bool MessageManager::Line::update (bool& changed)
51 changed = false; 52 { changed = false;
52 QDateTime now = QDateTime::currentDateTime(); 53 QDateTime now = QDateTime::currentDateTime();
53 int msec = now.msecsTo (expiry); 54 int msec = now.msecsTo (expiry);
54 55
55 if (now >= expiry) { 56 if (now >= expiry)
56 // Message line has expired 57 { // Message line has expired
57 changed = true; 58 changed = true;
58 return false; 59 return false;
59 } 60 }
60 61
61 if (msec <= g_fadeTime) { 62 if (msec <= g_fadeTime)
62 // Message line has not expired but is fading out 63 { // Message line has not expired but is fading out
63 alpha = ((float) msec) / g_fadeTime; 64 alpha = ( (float) msec) / g_fadeTime;
64 changed = true; 65 changed = true;
65 } 66 }
66 67
67 return true; 68 return true;
68 } 69 }
69 70
70 // ============================================================================= 71 // =============================================================================
71 // Add a line to the message manager. 72 // Add a line to the message manager.
72 // ----------------------------------------------------------------------------- 73 // -----------------------------------------------------------------------------
73 void MessageManager::addLine (str line) { 74 void MessageManager::addLine (str line)
74 // If there's too many entries, pop the excess out 75 { // If there's too many entries, pop the excess out
75 while (m_lines.size() >= g_maxMessages) 76 while (m_lines.size() >= g_maxMessages)
76 m_lines.erase (0); 77 m_lines.removeFirst();
77 78
78 m_lines << Line (line); 79 m_lines << Line (line);
79 80
80 // Update the renderer view 81 // Update the renderer view
81 if (renderer()) 82 if (renderer())
82 renderer()->update(); 83 renderer()->update();
83 } 84 }
84 85
85 // ============================================================================= 86 // =============================================================================
86 // Ticks the message manager. All lines are ticked and the renderer scene is 87 // Ticks the message manager. All lines are ticked and the renderer scene is
87 // redrawn if something changed. 88 // redrawn if something changed.
88 // ----------------------------------------------------------------------------- 89 // -----------------------------------------------------------------------------
89 void MessageManager::tick() { 90 void MessageManager::tick()
90 if (m_lines.size() == 0) 91 { if (m_lines.isEmpty())
91 return; 92 return;
92 93
93 bool changed = false; 94 bool changed = false;
94 95
95 for (uint i = 0; i < m_lines.size(); ++i) { 96 for (int i = 0; i < m_lines.size(); ++i)
96 bool lineChanged; 97 { bool lineChanged;
97 98
98 if (!m_lines[i].update (lineChanged)) 99 if (!m_lines[i].update (lineChanged))
99 m_lines.erase (i--); 100 m_lines.removeAt (i--);
100 101
101 changed |= lineChanged; 102 changed |= lineChanged;
102 } 103 }
103 104
104 if (changed && renderer()) 105 if (changed && renderer())
105 renderer()->update(); 106 renderer()->update();
106 } 107 }
107 108
108 // ============================================================================= 109 // =============================================================================
109 // ----------------------------------------------------------------------------- 110 // -----------------------------------------------------------------------------
110 const List<MessageManager::Line>& MessageManager::getLines() const { 111 const QList<MessageManager::Line>& MessageManager::getLines() const
111 return m_lines; 112 { return m_lines;
112 } 113 }
113 114
114 // ============================================================================= 115 // =============================================================================
115 // log() interface - format the argument list and add the resulting string to 116 // log() interface - format the argument list and add the resulting string to
116 // the main message manager. 117 // the main message manager.
117 // ----------------------------------------------------------------------------- 118 // -----------------------------------------------------------------------------
118 void DoLog (std::initializer_list<StringFormatArg> args) { 119 void DoLog (std::initializer_list<StringFormatArg> args)
119 const str msg = DoFormat (args); 120 { const str msg = DoFormat (args);
120 g_win->addMessage (msg); 121
121 122 for (str& a : msg.split ("\n", QString::SkipEmptyParts))
122 // Also print it to stdout 123 { if (g_win)
123 print ("%1\n", msg); 124 g_win->addMessage (a);
125
126 // Also print it to stdout
127 fprint (stdout, "%1\n", a);
128 }
124 } 129 }
125 #include "moc_messagelog.cpp"

mercurial