src/messageLog.cpp

branch
projects
changeset 935
8d98ee0dc917
parent 931
85080f7a1c20
child 941
f895379d7fab
equal deleted inserted replaced
930:ab77deb851fa 935:8d98ee0dc917
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013 - 2015 Teemu 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 <QTimer>
20 #include <QDate>
21 #include "messageLog.h"
22 #include "glRenderer.h"
23 #include "mainWindow.h"
24
25 enum
26 {
27 MaxMessages = 5,
28 ExpireTime = 5000,
29 FadeTime = 500
30 };
31
32 // -------------------------------------------------------------------------------------------------
33 //
34 MessageManager::MessageManager (QObject* parent) :
35 QObject (parent)
36 {
37 m_ticker = new QTimer;
38 m_ticker->start (100);
39 connect (m_ticker, SIGNAL (timeout()), this, SLOT (tick()));
40 }
41
42 // -------------------------------------------------------------------------------------------------
43 //
44 MessageManager::Line::Line (QString text) :
45 text (text),
46 alpha (1.0f),
47 expiry (QDateTime::currentDateTime().addMSecs (ExpireTime)) {}
48
49 // -------------------------------------------------------------------------------------------------
50 //
51 bool MessageManager::Line::update (bool& changed)
52 {
53 changed = false;
54 QDateTime now = QDateTime::currentDateTime();
55 int msec = now.msecsTo (expiry);
56
57 if (now >= expiry)
58 {
59 // Message line has expired
60 changed = true;
61 return false;
62 }
63
64 if (msec <= FadeTime)
65 {
66 // Message line has not expired but is fading out
67 alpha = ( (float) msec) / FadeTime;
68 changed = true;
69 }
70
71 return true;
72 }
73
74 // -------------------------------------------------------------------------------------------------
75 //
76 // Add a line to the message manager.
77 //
78 void MessageManager::addLine (QString line)
79 {
80 // If there's too many entries, pop the excess out
81 while (m_lines.size() >= MaxMessages)
82 m_lines.removeFirst();
83
84 m_lines << Line (line);
85
86 // Update the renderer view
87 if (renderer())
88 renderer()->update();
89 }
90
91 // -------------------------------------------------------------------------------------------------
92 //
93 // Ticks the message manager. All lines are ticked and the renderer scene is redrawn if something
94 // changed.
95 //
96 void MessageManager::tick()
97 {
98 if (m_lines.isEmpty())
99 return;
100
101 bool changed = false;
102
103 for (int i = 0; i < m_lines.size(); ++i)
104 {
105 bool lineChanged;
106
107 if (not m_lines[i].update (lineChanged))
108 m_lines.removeAt (i--);
109
110 changed |= lineChanged;
111 }
112
113 if (changed and renderer())
114 renderer()->update();
115 }
116
117 // =============================================================================
118 //
119 const QList<MessageManager::Line>& MessageManager::getLines() const
120 {
121 return m_lines;
122 }
123
124 // =============================================================================
125 //
126 void PrintToLog (const QString& msg)
127 {
128 for (QString& a : msg.split ("\n", QString::SkipEmptyParts))
129 {
130 if (g_win != null)
131 g_win->addMessage (a);
132
133 // Also print it to stdout
134 fprint (stdout, "%1\n", a);
135 }
136 }

mercurial