src/messageLog.cc

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

mercurial