src/messagelog.cpp

Thu, 05 Dec 2013 23:20:50 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Thu, 05 Dec 2013 23:20:50 +0200
changeset 539
72ad83a67165
parent 527
47c068f2e7e3
permissions
-rw-r--r--

- upgraded the PROPERTY macro, resulting in a major code refactor

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"
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
24 #include "moc_messagelog.cpp"
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25
498
791c831c8020 further cleansing - removed most uses of unsigned ints, removed list reversers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 493
diff changeset
26 static const int g_maxMessages = 5;
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 static const int g_expiry = 5;
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 static const int g_fadeTime = 500; // msecs
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
30 // =============================================================================
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
31 // -----------------------------------------------------------------------------
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
32 MessageManager::MessageManager (QObject* parent) :
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
33 QObject (parent)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
34 { m_ticker = new QTimer;
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
35 m_ticker->start (100);
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
36 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
37 }
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
39 // =============================================================================
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
40 // -----------------------------------------------------------------------------
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
41 MessageManager::Line::Line (str text) :
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
42 text (text),
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
43 alpha (1.0f),
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
44 expiry (QDateTime::currentDateTime().addSecs (g_expiry)) {}
349
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 // =============================================================================
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 // 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
48 // 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
49 // 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
50 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
51 bool MessageManager::Line::update (bool& changed)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
52 { changed = false;
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
53 QDateTime now = QDateTime::currentDateTime();
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
54 int msec = now.msecsTo (expiry);
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
55
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
56 if (now >= expiry)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
57 { // Message line has expired
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 changed = true;
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 return false;
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
61
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
62 if (msec <= g_fadeTime)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
63 { // Message line has not expired but is fading out
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
64 alpha = ( (float) msec) / g_fadeTime;
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 changed = true;
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
67
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 return true;
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 }
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
71 // =============================================================================
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
72 // Add a line to the message manager.
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
73 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
74 void MessageManager::addLine (str line)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
75 { // 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
76 while (m_lines.size() >= g_maxMessages)
512
adab82ab13a5 fixed: LDForge would crash if message log got full - the older lines would be removed with ::erase, which now expects an iterator because of the recent transition to QList. Why does QList::iterator allow 0 to be passed?
Santeri Piippo <crimsondusk64@gmail.com>
parents: 504
diff changeset
77 m_lines.removeFirst();
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
78
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
79 m_lines << Line (line);
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
80
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 // Update the renderer view
539
72ad83a67165 - upgraded the PROPERTY macro, resulting in a major code refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 527
diff changeset
82 if (getRenderer())
72ad83a67165 - upgraded the PROPERTY macro, resulting in a major code refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 527
diff changeset
83 getRenderer()->update();
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 }
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
86 // =============================================================================
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
87 // Ticks the message manager. All lines are ticked and the renderer scene is
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
88 // 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
89 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
90 void MessageManager::tick()
527
47c068f2e7e3 more minor changes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 514
diff changeset
91 { if (m_lines.isEmpty())
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 return;
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
93
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 bool changed = false;
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
95
498
791c831c8020 further cleansing - removed most uses of unsigned ints, removed list reversers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 493
diff changeset
96 for (int i = 0; i < m_lines.size(); ++i)
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
97 { bool lineChanged;
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
98
421
7d26db0be944 style cleanup - it should be all unified now
Santeri Piippo <crimsondusk64@gmail.com>
parents: 410
diff changeset
99 if (!m_lines[i].update (lineChanged))
503
bebe09014dd6 removed the List class in favor of QList
Santeri Piippo <crimsondusk64@gmail.com>
parents: 498
diff changeset
100 m_lines.removeAt (i--);
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
101
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 changed |= lineChanged;
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
104
539
72ad83a67165 - upgraded the PROPERTY macro, resulting in a major code refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 527
diff changeset
105 if (changed && getRenderer())
72ad83a67165 - upgraded the PROPERTY macro, resulting in a major code refactor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 527
diff changeset
106 getRenderer()->update();
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 }
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
109 // =============================================================================
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
110 // -----------------------------------------------------------------------------
504
6a1fa662bfc1 Removed the List -> QList alias, use QList directly
Santeri Piippo <crimsondusk64@gmail.com>
parents: 503
diff changeset
111 const QList<MessageManager::Line>& MessageManager::getLines() const
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
112 { return m_lines;
349
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 }
8e2630044a48 rename: msglog.cpp/h -> messagelog.cpp/h
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114
404
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
115 // =============================================================================
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
116 // log() interface - format the argument list and add the resulting string to
902f137ea753 cosmetics
Santeri Piippo <crimsondusk64@gmail.com>
parents: 363
diff changeset
117 // 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
118 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
119 void DoLog (std::initializer_list<StringFormatArg> args)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
120 { const str msg = DoFormat (args);
513
29eb671b34f6 Added a crash catcher which trigger under Linux. It calls GDB and tries to get a backtrace. Also integrated assertion failure handling to this new system. Removed the print() function in the process (because the new bomb box uses a text edit and QTextDocuments's print method clashes with the macro and I figured it was a good idea to rid it anyway) and replaced all calls with log().
Santeri Piippo <crimsondusk64@gmail.com>
parents: 512
diff changeset
121
514
d78fea0f664c made messagelog handle messages with newlines
Santeri Piippo <crimsondusk64@gmail.com>
parents: 513
diff changeset
122 for (str& a : msg.split ("\n", QString::SkipEmptyParts))
d78fea0f664c made messagelog handle messages with newlines
Santeri Piippo <crimsondusk64@gmail.com>
parents: 513
diff changeset
123 { if (g_win)
d78fea0f664c made messagelog handle messages with newlines
Santeri Piippo <crimsondusk64@gmail.com>
parents: 513
diff changeset
124 g_win->addMessage (a);
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
125
514
d78fea0f664c made messagelog handle messages with newlines
Santeri Piippo <crimsondusk64@gmail.com>
parents: 513
diff changeset
126 // Also print it to stdout
d78fea0f664c made messagelog handle messages with newlines
Santeri Piippo <crimsondusk64@gmail.com>
parents: 513
diff changeset
127 fprint (stdout, "%1\n", a);
d78fea0f664c made messagelog handle messages with newlines
Santeri Piippo <crimsondusk64@gmail.com>
parents: 513
diff changeset
128 }
491
7d1b5ecd76c0 it's LDForge's 1st birthday!
Santeri Piippo <crimsondusk64@gmail.com>
parents: 475
diff changeset
129 }

mercurial