src/messagelog.cpp

changeset 667
31540c1f22ea
parent 666
c595cfb4791c
parent 626
f3c01a033e9e
child 668
6a05c4969074
equal deleted inserted replaced
666:c595cfb4791c 667:31540c1f22ea
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 <QTimer>
20 #include <QDate>
21 #include "messagelog.h"
22 #include "gldraw.h"
23 #include "gui.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 { 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 (str text) :
42 text (text),
43 alpha (1.0f),
44 expiry (QDateTime::currentDateTime().addSecs (g_expiry)) {}
45
46 // =============================================================================
47 // Check this line's expiry and update alpha accordingly. Returns true if the
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 // -----------------------------------------------------------------------------
51 bool MessageManager::Line::update (bool& changed)
52 { changed = false;
53 QDateTime now = QDateTime::currentDateTime();
54 int msec = now.msecsTo (expiry);
55
56 if (now >= expiry)
57 { // Message line has expired
58 changed = true;
59 return false;
60 }
61
62 if (msec <= g_fadeTime)
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 (str line)
75 { // If there's too many entries, pop the excess out
76 while (m_lines.size() >= g_maxMessages)
77 m_lines.removeFirst();
78
79 m_lines << Line (line);
80
81 // Update the renderer view
82 if (renderer())
83 renderer()->update();
84 }
85
86 // =============================================================================
87 // Ticks the message manager. All lines are ticked and the renderer scene is
88 // redrawn if something changed.
89 // -----------------------------------------------------------------------------
90 void MessageManager::tick()
91 { if (m_lines.isEmpty())
92 return;
93
94 bool changed = false;
95
96 for (int i = 0; i < m_lines.size(); ++i)
97 { bool lineChanged;
98
99 if (!m_lines[i].update (lineChanged))
100 m_lines.removeAt (i--);
101
102 changed |= lineChanged;
103 }
104
105 if (changed && renderer())
106 renderer()->update();
107 }
108
109 // =============================================================================
110 // -----------------------------------------------------------------------------
111 const QList<MessageManager::Line>& MessageManager::getLines() const
112 { return m_lines;
113 }
114
115 // =============================================================================
116 // log() interface - format the argument list and add the resulting string to
117 // the main message manager.
118 // -----------------------------------------------------------------------------
119 void DoLog (std::initializer_list<StringFormatArg> args)
120 { const str msg = DoFormat (args);
121
122 for (str& a : msg.split ("\n", QString::SkipEmptyParts))
123 { if (g_win)
124 g_win->addMessage (a);
125
126 // Also print it to stdout
127 fprint (stdout, "%1\n", a);
128 }
129 }

mercurial