Added missing comments.
[situare] / src / ui / textmodifier.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Katri Kaikkonen - katri.kaikkonen@ixonos.com
6        Jussi Laitinen - jussi.laitinen@ixonos.com
7
8    Situare is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License
10    version 2 as published by the Free Software Foundation.
11
12    Situare is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Situare; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20    USA.
21 */
22
23 #include <QDebug>
24 #include <QFontMetrics>
25 #include <QStringList>
26
27 #include "textmodifier.h"
28
29 QString TextModifier::shortenText(const QFontMetrics fontMetrics, const QString &text,
30                                   int textMaxWidth)
31 {
32     qDebug() << __PRETTY_FUNCTION__;
33
34     QString copiedText = text;
35     int index = copiedText.indexOf('\n');
36
37     if (index >= 0) {
38         copiedText.truncate(index);
39         copiedText.append("...");
40     }
41
42     return fontMetrics.elidedText(copiedText, Qt::ElideRight, textMaxWidth);
43 }
44
45 QString TextModifier::splitWord(const QFontMetrics fontMetrics, const QString &word,
46                                 int textMaxWidth)
47 {
48     qDebug() << __PRETTY_FUNCTION__;
49
50     QString result;
51     QString temp;
52
53     for (int i = 0; i < word.length(); i++) {
54         if (fontMetrics.width(temp.append(word.at(i))) > textMaxWidth) {
55             result.append(temp.left(temp.length() - 1));
56             result.append(" ");
57             temp.remove(0, temp.length() - 1);
58         }
59     }
60
61     result.append(temp);
62
63     return result;
64 }
65
66 QString TextModifier::splitLongWords(const QFontMetrics fontMetrics, const QString &text,
67                                      int textMaxWidth)
68 {
69     qDebug() << __PRETTY_FUNCTION__;
70
71     QStringList list;
72     list = text.split(' ');
73
74     for (int i = 0; i < list.count(); i++) {
75         if (fontMetrics.width(list.at(i)) > textMaxWidth)
76             list.replace(i, splitWord(fontMetrics, list.at(i), textMaxWidth));
77     }
78
79     return list.join(" ");
80 }