src/MessageLog.cc

changeset 629
b75c6cce02e2
child 632
63e6243b880c
equal deleted inserted replaced
628:6b13e4c2e97b 629:b75c6cce02e2
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 #include "moc_MessageLog.cpp"
25
26 static const int g_maxMessages = 5;
27 static const int g_expiry = 5;
28 static const int g_fadeTime = 500; // msecs
29
30 // =============================================================================
31 // -----------------------------------------------------------------------------
32 MessageManager::MessageManager (QObject* parent) :
33 QObject (parent)
34 {
35 m_ticker = new QTimer;
36 m_ticker->start (100);
37 connect (m_ticker, SIGNAL (timeout()), this, SLOT (tick()));
38 }
39
40 // =============================================================================
41 // -----------------------------------------------------------------------------
42 MessageManager::Line::Line (QString text) :
43 text (text),
44 alpha (1.0f),
45 expiry (QDateTime::currentDateTime().addSecs (g_expiry)) {}
46
47 // =============================================================================
48 // Check this line's expiry and update alpha accordingly. Returns true if the
49 // line is to still stick around, false if it expired. 'changed' is updated to
50 // whether the line has somehow changed since the last update.
51 // -----------------------------------------------------------------------------
52 bool MessageManager::Line::update (bool& changed)
53 {
54 changed = false;
55 QDateTime now = QDateTime::currentDateTime();
56 int msec = now.msecsTo (expiry);
57
58 if (now >= expiry)
59 {
60 // Message line has expired
61 changed = true;
62 return false;
63 }
64
65 if (msec <= g_fadeTime)
66 {
67 // Message line has not expired but is fading out
68 alpha = ( (float) msec) / g_fadeTime;
69 changed = true;
70 }
71
72 return true;
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() >= g_maxMessages)
82 m_lines.removeFirst();
83
84 m_lines << Line (line);
85
86 // Update the renderer view
87 if (getRenderer())
88 getRenderer()->update();
89 }
90
91 // =============================================================================
92 // Ticks the message manager. All lines are ticked and the renderer scene is
93 // redrawn if something changed.
94 // -----------------------------------------------------------------------------
95 void MessageManager::tick()
96 {
97 if (m_lines.isEmpty())
98 return;
99
100 bool changed = false;
101
102 for (int i = 0; i < m_lines.size(); ++i)
103 {
104 bool lineChanged;
105
106 if (!m_lines[i].update (lineChanged))
107 m_lines.removeAt (i--);
108
109 changed |= lineChanged;
110 }
111
112 if (changed && getRenderer())
113 getRenderer()->update();
114 }
115
116 // =============================================================================
117 // -----------------------------------------------------------------------------
118 const QList<MessageManager::Line>& MessageManager::getLines() const
119 {
120 return m_lines;
121 }
122
123 // =============================================================================
124 // log() interface - format the argument list and add the resulting string to
125 // the main message manager.
126 // -----------------------------------------------------------------------------
127 void DoLog (std::initializer_list<StringFormatArg> args)
128 {
129 const QString msg = DoFormat (args);
130
131 for (QString& a : msg.split ("\n", QString::SkipEmptyParts))
132 {
133 if (g_win)
134 g_win->addMessage (a);
135
136 // Also print it to stdout
137 fprint (stdout, "%1\n", a);
138 }
139 }

mercurial