src/messagelog.cpp

Sun, 22 Sep 2013 23:27:07 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sun, 22 Sep 2013 23:27:07 +0300
changeset 491
7d1b5ecd76c0
parent 475
1f5ab7410e4d
child 493
16766ac1bbd9
permissions
-rw-r--r--

it's LDForge's 1st birthday!
- moved the project qmake code into ldforge.pro, removing src/src.pro in the process, with UI_DIR there is no need to have the main qmake in src/
- added moc #include directives to relevant source files, speeding up compile

349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2013 Santeri Piippo
455
c5d14d112034 Code formatting: use same separators everywhere, remove extra spaces from license headers, simplified message manager api a bit
Santeri Piippo <crimsondusk64@gmail.com>
parents: 421
diff changeset
4 *
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
455
c5d14d112034 Code formatting: use same separators everywhere, remove extra spaces from license headers, simplified message manager api a bit
Santeri Piippo <crimsondusk64@gmail.com>
parents: 421
diff changeset
9 *
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
455
c5d14d112034 Code formatting: use same separators everywhere, remove extra spaces from license headers, simplified message manager api a bit
Santeri Piippo <crimsondusk64@gmail.com>
parents: 421
diff changeset
14 *
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
455
c5d14d112034 Code formatting: use same separators everywhere, remove extra spaces from license headers, simplified message manager api a bit
Santeri Piippo <crimsondusk64@gmail.com>
parents: 421
diff changeset
19 #include <QTimer>
c5d14d112034 Code formatting: use same separators everywhere, remove extra spaces from license headers, simplified message manager api a bit
Santeri Piippo <crimsondusk64@gmail.com>
parents: 421
diff changeset
20 #include <QDate>
351
6fdd32c4886c uh, stuff
Santeri Piippo <crimsondusk64@gmail.com>
parents: 349
diff changeset
21 #include "messagelog.h"
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 #include "gldraw.h"
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 #include "gui.h"
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 static const unsigned int g_maxMessages = 5;
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 static const int g_expiry = 5;
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 static const int g_fadeTime = 500; // msecs
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
29 // =============================================================================
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
30 // -----------------------------------------------------------------------------
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
31 MessageManager::MessageManager (QObject* parent) :
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
32 QObject (parent) {
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 m_ticker = new QTimer;
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
34 m_ticker->start (100);
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
35 connect (m_ticker, SIGNAL (timeout()), this, SLOT (tick()));
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 }
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
38 // =============================================================================
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
39 // -----------------------------------------------------------------------------
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
40 MessageManager::Line::Line (str text) :
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
41 text (text),
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
42 alpha (1.0f),
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
43 expiry (QDateTime::currentDateTime().addSecs (g_expiry)) {}
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 // =============================================================================
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 // Check this line's expiry and update alpha accordingly. Returns true if the
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
47 // line is to still stick around, false if it expired. 'changed' is updated to
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
48 // whether the line has somehow changed since the last update.
455
c5d14d112034 Code formatting: use same separators everywhere, remove extra spaces from license headers, simplified message manager api a bit
Santeri Piippo <crimsondusk64@gmail.com>
parents: 421
diff changeset
49 // -----------------------------------------------------------------------------
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
50 bool MessageManager::Line::update (bool& changed) {
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 changed = false;
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
52 QDateTime now = QDateTime::currentDateTime();
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
53 int msec = now.msecsTo (expiry);
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
55 if (now >= expiry) {
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
56 // Message line has expired
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 changed = true;
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 return false;
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 }
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
61 if (msec <= g_fadeTime) {
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
62 // Message line has not expired but is fading out
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
63 alpha = ((float) msec) / g_fadeTime;
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 changed = true;
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 }
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 return true;
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 }
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
70 // =============================================================================
455
c5d14d112034 Code formatting: use same separators everywhere, remove extra spaces from license headers, simplified message manager api a bit
Santeri Piippo <crimsondusk64@gmail.com>
parents: 421
diff changeset
71 // Add a line to the message manager.
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
72 // -----------------------------------------------------------------------------
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
73 void MessageManager::addLine (str line) {
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 // If there's too many entries, pop the excess out
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
75 while (m_lines.size() >= g_maxMessages)
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
76 m_lines.erase (0);
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
78 m_lines << Line (line);
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 // Update the renderer view
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
81 if (renderer())
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 renderer()->update();
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 }
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
85 // =============================================================================
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
86 // Ticks the message manager. All lines are ticked and the renderer scene is
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
87 // redrawn if something changed.
455
c5d14d112034 Code formatting: use same separators everywhere, remove extra spaces from license headers, simplified message manager api a bit
Santeri Piippo <crimsondusk64@gmail.com>
parents: 421
diff changeset
88 // -----------------------------------------------------------------------------
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
89 void MessageManager::tick() {
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
90 if (m_lines.size() == 0)
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 return;
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 bool changed = false;
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
95 for (uint i = 0; i < m_lines.size(); ++i) {
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 bool lineChanged;
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
98 if (!m_lines[i].update (lineChanged))
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
99 m_lines.erase (i--);
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 changed |= lineChanged;
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 }
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
104 if (changed && renderer())
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 renderer()->update();
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 }
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
108 // =============================================================================
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
109 // -----------------------------------------------------------------------------
455
c5d14d112034 Code formatting: use same separators everywhere, remove extra spaces from license headers, simplified message manager api a bit
Santeri Piippo <crimsondusk64@gmail.com>
parents: 421
diff changeset
110 const List<MessageManager::Line>& MessageManager::getLines() const {
c5d14d112034 Code formatting: use same separators everywhere, remove extra spaces from license headers, simplified message manager api a bit
Santeri Piippo <crimsondusk64@gmail.com>
parents: 421
diff changeset
111 return m_lines;
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 }
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
114 // =============================================================================
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
115 // log() interface - format the argument list and add the resulting string to
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
116 // the main message manager.
455
c5d14d112034 Code formatting: use same separators everywhere, remove extra spaces from license headers, simplified message manager api a bit
Santeri Piippo <crimsondusk64@gmail.com>
parents: 421
diff changeset
117 // -----------------------------------------------------------------------------
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
118 void DoLog (std::initializer_list<StringFormatArg> args) {
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
119 const str msg = DoFormat (args);
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
120 g_win->addMessage (msg);
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
121
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
122 // Also print it to stdout
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
123 print ("%1\n", msg);
491
7d1b5ecd76c0 it's LDForge's 1st birthday!
Santeri Piippo <crimsondusk64@gmail.com>
parents: 475
diff changeset
124 }
7d1b5ecd76c0 it's LDForge's 1st birthday!
Santeri Piippo <crimsondusk64@gmail.com>
parents: 475
diff changeset
125 #include "moc_messagelog.cpp"

mercurial