Tue, 30 Jul 2013 17:27:04 +0300
cosmetics
349
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
1 | /* |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
2 | * LDForge: LDraw parts authoring CAD |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
3 | * Copyright (C) 2013 Santeri Piippo |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
4 | * |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
5 | * This program is free software: you can redistribute it and/or modify |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
6 | * it under the terms of the GNU General Public License as published by |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
7 | * the Free Software Foundation, either version 3 of the License, or |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
8 | * (at your option) any later version. |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
9 | * |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
10 | * This program is distributed in the hope that it will be useful, |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
13 | * GNU General Public License for more details. |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
14 | * |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
15 | * You should have received a copy of the GNU General Public License |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
17 | */ |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
18 | |
351 | 19 | #include "messagelog.h" |
349
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
20 | #include "gldraw.h" |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
21 | #include "gui.h" |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | #include <QTimer> |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | #include <QDate> |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
24 | |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
25 | static const unsigned int g_maxMessages = 5; |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
26 | static const int g_expiry = 5; |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
27 | static const int g_fadeTime = 500; // msecs |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
28 | |
404 | 29 | // ============================================================================= |
30 | // ----------------------------------------------------------------------------- | |
31 | MessageManager::MessageManager( QObject* parent ) : | |
32 | QObject( parent ) | |
349
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
33 | { |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | m_ticker = new QTimer; |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
35 | m_ticker->start( 100 ); |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
36 | connect( m_ticker, SIGNAL( timeout() ), this, SLOT( tick() )); |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | } |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
38 | |
404 | 39 | // ============================================================================= |
40 | // ----------------------------------------------------------------------------- | |
41 | MessageManager::Line::Line( str text ) : | |
42 | text( text ), | |
43 | alpha( 1.0f ), | |
44 | expiry( QDateTime::currentDateTime().addSecs( g_expiry )) {} | |
349
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
45 | |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
46 | // ============================================================================= |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
47 | // Check this line's expiry and update alpha accordingly. Returns true if the |
404 | 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. | |
50 | bool MessageManager::Line::update( bool& changed ) { | |
349
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
51 | changed = false; |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
52 | |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
53 | QDateTime now = QDateTime::currentDateTime(); |
404 | 54 | if( now >= expiry ) { |
55 | // Message line has expired | |
349
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
56 | changed = true; |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
57 | return false; |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
58 | } |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
59 | |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
60 | int msec = now.msecsTo( expiry ); |
404 | 61 | if( msec <= g_fadeTime ) { |
62 | // Message line has not expired but is fading out | |
349
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
63 | alpha = ( (float) msec ) / g_fadeTime; |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
64 | changed = true; |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
65 | } |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
66 | |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
67 | return true; |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
68 | } |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
69 | |
404 | 70 | // ============================================================================= |
71 | // ----------------------------------------------------------------------------- | |
72 | // Add a line to the message manager. | |
73 | void MessageManager::addLine( str line ) { | |
349
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
74 | // If there's too many entries, pop the excess out |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
75 | while( m_lines.size() >= g_maxMessages ) |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
76 | m_lines.erase( 0 ); |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
77 | |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
78 | m_lines << Line( line ); |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
79 | |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
80 | // Update the renderer view |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
81 | if( renderer() ) |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
82 | renderer()->update(); |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
83 | } |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
84 | |
404 | 85 | // ============================================================================= |
86 | // ----------------------------------------------------------------------------- | |
87 | // Shortcut | |
88 | MessageManager& MessageManager::operator<<( str line ) { | |
349
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
89 | addLine( line ); |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
90 | return *this; |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
91 | } |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
92 | |
404 | 93 | // ============================================================================= |
94 | // ----------------------------------------------------------------------------- | |
95 | // Ticks the message manager. All lines are ticked and the renderer scene is | |
96 | // redrawn if something changed. | |
97 | void MessageManager::tick() { | |
349
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
98 | if( m_lines.size() == 0 ) |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
99 | return; |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
100 | |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
101 | bool changed = false; |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
102 | |
404 | 103 | for( uint i = 0; i < m_lines.size(); ++i ) { |
349
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
104 | bool lineChanged; |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
105 | |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
106 | if( !m_lines[i].update( lineChanged )) |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
107 | m_lines.erase( i-- ); |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
108 | |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
109 | changed |= lineChanged; |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
110 | } |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
111 | |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
112 | if( changed && renderer() ) |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
113 | renderer()->update(); |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
114 | } |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
115 | |
404 | 116 | // ============================================================================= |
117 | // ----------------------------------------------------------------------------- | |
118 | // C++11-for loop support | |
119 | MessageManager::c_it MessageManager::begin() const { | |
349
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
120 | return m_lines.begin(); |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
121 | } |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
122 | |
404 | 123 | // ============================================================================= |
124 | // ----------------------------------------------------------------------------- | |
125 | MessageManager::c_it MessageManager::end() const { | |
349
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
126 | return m_lines.end(); |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
127 | } |
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
128 | |
404 | 129 | // ============================================================================= |
130 | // ----------------------------------------------------------------------------- | |
131 | // log() interface - format the argument list and add the resulting string to | |
132 | // the main message manager. | |
133 | void DoLog( std::initializer_list<StringFormatArg> args ) { | |
363
75583c9f289d
primitive generator: add conditional lines to cylinders; improved bad color handling
Santeri Piippo <crimsondusk64@gmail.com>
parents:
351
diff
changeset
|
134 | const str msg = DoFormat( args ); |
75583c9f289d
primitive generator: add conditional lines to cylinders; improved bad color handling
Santeri Piippo <crimsondusk64@gmail.com>
parents:
351
diff
changeset
|
135 | g_win->addMessage( msg ); |
404 | 136 | |
137 | // Also print it to stdout | |
363
75583c9f289d
primitive generator: add conditional lines to cylinders; improved bad color handling
Santeri Piippo <crimsondusk64@gmail.com>
parents:
351
diff
changeset
|
138 | print( "%1\n", msg ); |
349
8e2630044a48
rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
139 | } |