|
1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013 Santeri Piippo |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 |
|
19 #include "msglog.h" |
|
20 #include "gldraw.h" |
|
21 #include "gui.h" |
|
22 #include <QTimer> |
|
23 #include <QDate> |
|
24 |
|
25 static const unsigned int g_maxMessages = 5; |
|
26 static const int g_expiry = 5; |
|
27 static const int g_fadeTime = 500; // msecs |
|
28 |
|
29 MessageManager::MessageManager( QObject* parent ) : QObject( parent ) |
|
30 { |
|
31 m_ticker = new QTimer; |
|
32 m_ticker->start( 100 ); |
|
33 connect( m_ticker, SIGNAL( timeout() ), this, SLOT( tick() )); |
|
34 } |
|
35 |
|
36 MessageManager::Line::Line( str text ) : text( text ), alpha( 1.0f ) |
|
37 { |
|
38 // Init expiry |
|
39 expiry = QDateTime::currentDateTime().addSecs( g_expiry ); |
|
40 } |
|
41 |
|
42 // ============================================================================= |
|
43 // Check this line's expiry and update alpha accordingly. Returns true if the |
|
44 // line is still around, false if it expired. |
|
45 bool MessageManager::Line::update( bool& changed ) |
|
46 { |
|
47 changed = false; |
|
48 |
|
49 QDateTime now = QDateTime::currentDateTime(); |
|
50 if( now >= expiry ) |
|
51 { |
|
52 changed = true; |
|
53 return false; |
|
54 } |
|
55 |
|
56 int msec = now.msecsTo( expiry ); |
|
57 if( msec <= g_fadeTime ) |
|
58 { |
|
59 alpha = ( (float) msec ) / g_fadeTime; |
|
60 changed = true; |
|
61 } |
|
62 |
|
63 return true; |
|
64 } |
|
65 |
|
66 void MessageManager::addLine( str line ) |
|
67 { |
|
68 // If there's too many entries, pop the excess out |
|
69 while( m_lines.size() >= g_maxMessages ) |
|
70 m_lines.erase( 0 ); |
|
71 |
|
72 m_lines << Line( line ); |
|
73 |
|
74 // Force a tick, we added a new message and it should |
|
75 // show up immediately. |
|
76 tick(); |
|
77 } |
|
78 |
|
79 MessageManager& MessageManager::operator<<( str line ) |
|
80 { |
|
81 addLine( line ); |
|
82 return *this; |
|
83 } |
|
84 |
|
85 void MessageManager::tick() |
|
86 { |
|
87 bool changed = false; |
|
88 |
|
89 for( uint i = 0; i < m_lines.size(); ++i ) |
|
90 { |
|
91 bool lineChanged; |
|
92 |
|
93 if( !m_lines[i].update( lineChanged )) |
|
94 m_lines.erase( i-- ); |
|
95 |
|
96 changed |= lineChanged; |
|
97 } |
|
98 |
|
99 if( changed ) |
|
100 renderer()->update(); |
|
101 } |
|
102 |
|
103 MessageManager::c_it MessageManager::begin() const |
|
104 { |
|
105 return m_lines.begin(); |
|
106 } |
|
107 |
|
108 MessageManager::c_it MessageManager::end() const |
|
109 { |
|
110 return m_lines.end(); |
|
111 } |
|
112 |
|
113 void DoLog( std::initializer_list<StringFormatArg> args ) |
|
114 { |
|
115 g_win->addMessage( DoFormat( args )); |
|
116 } |