src/messagelog.cc

changeset 603
47e7773c7841
parent 600
209e3f1f7b2c
child 606
3dd6f343ec06
equal deleted inserted replaced
602:ac1744536b33 603:47e7773c7841
29 29
30 // ============================================================================= 30 // =============================================================================
31 // ----------------------------------------------------------------------------- 31 // -----------------------------------------------------------------------------
32 MessageManager::MessageManager (QObject* parent) : 32 MessageManager::MessageManager (QObject* parent) :
33 QObject (parent) 33 QObject (parent)
34 { m_ticker = new QTimer; 34 {
35 m_ticker = new QTimer;
35 m_ticker->start (100); 36 m_ticker->start (100);
36 connect (m_ticker, SIGNAL (timeout()), this, SLOT (tick())); 37 connect (m_ticker, SIGNAL (timeout()), this, SLOT (tick()));
37 } 38 }
38 39
39 // ============================================================================= 40 // =============================================================================
47 // Check this line's expiry and update alpha accordingly. Returns true if the 48 // 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 49 // 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. 50 // whether the line has somehow changed since the last update.
50 // ----------------------------------------------------------------------------- 51 // -----------------------------------------------------------------------------
51 bool MessageManager::Line::update (bool& changed) 52 bool MessageManager::Line::update (bool& changed)
52 { changed = false; 53 {
54 changed = false;
53 QDateTime now = QDateTime::currentDateTime(); 55 QDateTime now = QDateTime::currentDateTime();
54 int msec = now.msecsTo (expiry); 56 int msec = now.msecsTo (expiry);
55 57
56 if (now >= expiry) 58 if (now >= expiry)
57 { // Message line has expired 59 {
60 // Message line has expired
58 changed = true; 61 changed = true;
59 return false; 62 return false;
60 } 63 }
61 64
62 if (msec <= g_fadeTime) 65 if (msec <= g_fadeTime)
63 { // Message line has not expired but is fading out 66 {
67 // Message line has not expired but is fading out
64 alpha = ( (float) msec) / g_fadeTime; 68 alpha = ( (float) msec) / g_fadeTime;
65 changed = true; 69 changed = true;
66 } 70 }
67 71
68 return true; 72 return true;
70 74
71 // ============================================================================= 75 // =============================================================================
72 // Add a line to the message manager. 76 // Add a line to the message manager.
73 // ----------------------------------------------------------------------------- 77 // -----------------------------------------------------------------------------
74 void MessageManager::addLine (str line) 78 void MessageManager::addLine (str line)
75 { // If there's too many entries, pop the excess out 79 {
80 // If there's too many entries, pop the excess out
76 while (m_lines.size() >= g_maxMessages) 81 while (m_lines.size() >= g_maxMessages)
77 m_lines.removeFirst(); 82 m_lines.removeFirst();
78 83
79 m_lines << Line (line); 84 m_lines << Line (line);
80 85
86 // ============================================================================= 91 // =============================================================================
87 // Ticks the message manager. All lines are ticked and the renderer scene is 92 // Ticks the message manager. All lines are ticked and the renderer scene is
88 // redrawn if something changed. 93 // redrawn if something changed.
89 // ----------------------------------------------------------------------------- 94 // -----------------------------------------------------------------------------
90 void MessageManager::tick() 95 void MessageManager::tick()
91 { if (m_lines.isEmpty()) 96 {
97 if (m_lines.isEmpty())
92 return; 98 return;
93 99
94 bool changed = false; 100 bool changed = false;
95 101
96 for (int i = 0; i < m_lines.size(); ++i) 102 for (int i = 0; i < m_lines.size(); ++i)
97 { bool lineChanged; 103 {
104 bool lineChanged;
98 105
99 if (!m_lines[i].update (lineChanged)) 106 if (!m_lines[i].update (lineChanged))
100 m_lines.removeAt (i--); 107 m_lines.removeAt (i--);
101 108
102 changed |= lineChanged; 109 changed |= lineChanged;
107 } 114 }
108 115
109 // ============================================================================= 116 // =============================================================================
110 // ----------------------------------------------------------------------------- 117 // -----------------------------------------------------------------------------
111 const QList<MessageManager::Line>& MessageManager::getLines() const 118 const QList<MessageManager::Line>& MessageManager::getLines() const
112 { return m_lines; 119 {
120 return m_lines;
113 } 121 }
114 122
115 // ============================================================================= 123 // =============================================================================
116 // log() interface - format the argument list and add the resulting string to 124 // log() interface - format the argument list and add the resulting string to
117 // the main message manager. 125 // the main message manager.
118 // ----------------------------------------------------------------------------- 126 // -----------------------------------------------------------------------------
119 void DoLog (std::initializer_list<StringFormatArg> args) 127 void DoLog (std::initializer_list<StringFormatArg> args)
120 { const str msg = DoFormat (args); 128 {
129 const str msg = DoFormat (args);
121 130
122 for (str& a : msg.split ("\n", QString::SkipEmptyParts)) 131 for (str& a : msg.split ("\n", QString::SkipEmptyParts))
123 { if (g_win) 132 {
133 if (g_win)
124 g_win->addMessage (a); 134 g_win->addMessage (a);
125 135
126 // Also print it to stdout 136 // Also print it to stdout
127 fprint (stdout, "%1\n", a); 137 fprint (stdout, "%1\n", a);
128 } 138 }

mercurial