src/MessageLog.cc

Thu, 06 Feb 2014 20:43:22 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Thu, 06 Feb 2014 20:43:22 +0200
changeset 632
63e6243b880c
parent 629
b75c6cce02e2
child 633
34d18b9c2cab
permissions
-rw-r--r--

- migrated to CMake again
- added public domain updaterevision for git information

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

mercurial