QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
QPainter painter(&p);
painter.setFont(NOKIA_FONT_SMALL);
-
QFontMetrics textMetrics = painter.fontMetrics();
- QString expandedText = TextModifier::splitLongWords(textMetrics, text, m_subItemTextWidth);
+ QString expandedText = TextModifier::splitLongWords(textMetrics, text,
+ m_subItemTextWidth);
ExtendedListItemStore *itemStore = new ExtendedListItemStore(expandedText);
itemStore->setIcon(icon);
- itemStore->setShortenedText(shortenText(expandedText, m_subItemTextWidth,
+ itemStore->setShortenedText(shortenText(text, m_subItemTextWidth,
ListItem::TEXT_SIZE_SMALL));
itemStore->setTextRect(calculateExpandedTextRect(expandedText));
QRect ExtendedListItem::boundingRect(const QString &text)
{
- qWarning() << __PRETTY_FUNCTION__ << text;
+ qDebug() << __PRETTY_FUNCTION__;
QPixmap p = QPixmap(ICON_WIDTH, ICON_HEIGHT);
QPainter painter(&p);
QFontMetrics textMetrics = painter.fontMetrics();
QRect textRect;
- textRect.setSize(QSize(m_subItemTextWidth, textMetrics.height()));
-
- QStringList rows = text.split('\n');
-
-// for (int i = 1; i < rows.count(); ++i) {
-// qWarning() << "jep";
-// QRect textRowRect = textMetrics.boundingRect(rows.at(i));
-// textRect.setHeight(textRect.height() + textRowRect.height());
-// }
-
- QStringList words = text.split(" ");
- QString rowText = words.at(0);
- for (int i = 1; i < words.count(); ++i) {
- if (rowText.contains("\n")) {
- textRect.setHeight(textRect.height() + textMetrics.height());
- //rowText.clear();
- rowText = words.at(i);
+ int rowHeight = textMetrics.ascent() + textMetrics.descent();
+ textRect.setSize(QSize(m_subItemTextWidth, rowHeight));
+
+ QStringList words = text.split(QRegExp("\\b"));
+ QString rowText;
+ foreach (QString word, words) {
+
+ if (word.contains("\n")) {
+ foreach (QChar c, word) {
+ if (c == '\n')
+ textRect.setHeight(textRect.height() + rowHeight);
+ }
+ rowText.clear();
+ } else {
+ rowText.append(word);
+ if (textMetrics.width(rowText) > m_subItemTextWidth) {
+ textRect.setHeight(textRect.height() + rowHeight);
+ rowText = word.simplified();
+ }
}
- else if (textMetrics.width(rowText) > m_subItemTextWidth) {
- textRect.setHeight(textRect.height() + textMetrics.height());
- rowText = words.at(i);
- rowText.remove("\n");
- qWarning() << "hep";
- }
- else
- rowText.append(" " + words.at(i));
}
return textRect;
const int TEXT_BOTTOM_MARGIN = 2;
- QRect textRect = boundingRect(text);
- QRect expandedTextRect = textRect;
-// int textRectFactor = textRect.width() / m_subItemTextWidth;
-// textRectFactor += textRect.height() / SUBITEM_TEXT_ROW_HEIGHT;
-
-// QRect expandedTextRect = QRect(0, 0, m_subItemTextWidth, SUBITEM_TEXT_ROW_HEIGHT
-// * qMax(textRectFactor, 1));
+ QRect expandedTextRect = boundingRect(text);
m_normalHeight += SUBITEM_TEXT_ROW_HEIGHT + TEXT_BOTTOM_MARGIN;
m_expandedHeight += expandedTextRect.height() + TEXT_BOTTOM_MARGIN;
QRect distanceRect = distanceTextFontMetrics.boundingRect(distanceText);
setData(DISTANCE_SIZE_HINT_INDEX, distanceRect);
- QString nameTemp = QString("Pitka nimi pitka nimi pitka nimi");
- setTitle(shortenText(nameTemp, NAME_TEXT_MAX_WIDTH - distanceRect.width() - MARGIN * 2,
+ setTitle(shortenText(user->name(), NAME_TEXT_MAX_WIDTH - distanceRect.width() - MARGIN * 2,
ListItem::TEXT_SIZE_NORMAL));
setCoordinates(user->coordinates());
clearSubItems();
- QString noteTemp = QString("Joillakin lehdillä on tosi tarkat rajat painon kanssa, ja emme "
- "menneet Playboyn pyytämiin \n\nkoekuvauksiinkaan, kun he sanoivat, "
- "että se on ainoa mahdollisuus. Jos on esim. liian ruskettunut, "
- "niin he karsivat saman tien pois. Nain jälkikäteen ajatellen minua "
- "on jäänyt harmittamaan, ettemme menneet koekuvaukseen kun kerran "
- "pyydettiin! Kun Julia pääsi kyseisen lehden Grapevine-sivulle "
- "vuonna 2003, meille molemmille jäi kytemään ajatus jatkosta. "
- "Olemme ihmetelleet, miksi Suomen lehdistö sivuutti asian vain "
- "nopeasti. Vaikka kyseessä ei ollutkaan monen kuvan kuvasarja "
- "Juliasta, hän silti poseerasi varsinaisessa Playboy-lehdessä.");
- QString noteTemp2 = QString("hei\nhei\n\nhei\nhei\nhei\nhei\nhei\nhei\nhei\nhei\nhei\nhei\n"
- "hei\nhei\nhei\nhei\nhei\nhei\nhei\nhei\nhei\nhei\nhei\nhei");
-
- static int index = 0;
-
-// if (index % 2)
- addSubItem(user->note(), QPixmap(":/res/images/envelope.png"));
-// else
-// addSubItem(noteTemp2, QPixmap(":/res/images/envelope.png"));
+ addSubItem(user->note(), QPixmap(":/res/images/envelope.png"));
addSubItem(user->address(), QPixmap(":/res/images/compass.png"));
addSubItem(user->timestamp(), QPixmap(":/res/images/clock.png"));
-
- index++;
}
const int SUBITEM_TEXT_MAX_WIDTH = ITEM_WIDTH - 3 * MARGIN - IMAGE_WIDTH - MARGIN - ICON_WIDTH
- MARGIN * 2;
-const int SUBITEM_TEXT_ROW_HEIGHT = 22; ///< Subitems text row height
+const int SUBITEM_TEXT_ROW_HEIGHT = 21; ///< Subitems text row height
const int TITLE_DISPLAY_INDEX = Qt::DisplayRole;
const int AVATAR_IMAGE_INDEX = Qt::DecorationRole;
public:
TextModifier(QObject *parent = 0);
+ /**
+ * @brief Shortens text to fit.
+ *
+ * Text shortening is defined by text maximum width.
+ *
+ * @param fontMetrics font metrics to use
+ * @param text text to shorten
+ * @param textMaxWidth maximum width for text
+ */
static QString shortenText(const QFontMetrics fontMetrics, const QString &text,
int textMaxWidth);
+ /**
+ * @brief Splits too long word.
+ *
+ * Splits long word to several by adding extra spaces
+ *
+ * @param fontMetrics font metrics to use
+ * @param word long word to be splitted
+ * @param textMaxWidth maximum width for word
+ * @returns splitted word
+ */
static QString splitWord(const QFontMetrics fontMetrics, const QString &word, int textMaxWidth);
+ /**
+ * @brief Splits long words from text.
+ *
+ * Splits long words to several by adding extra spaces
+ *
+ * @param fontMetrics font metrics to use
+ * @param text text to be splitted
+ * @param textMaxWidth maximum width for word
+ * @returns text with long words splitted
+ */
static QString splitLongWords(const QFontMetrics fontMetrics, const QString &text,
int textMaxWidth);
};
#include "common.h"
#include "imagebutton.h"
+#include "textmodifier.h"
#include "user/user.h"
#include "userinfo.h"
const int BACKGROUND_WIDTH = 368;
const int ICON_HEIGHT = 24;
const int ICON_WIDTH = 24;
-const int LABEL_MAX_WIDTH = 300;
const int MARGIN = 5;
+const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - ICON_WIDTH - 5 * MARGIN;
UserInfo::UserInfo(QWidget *parent)
: QWidget(parent),
{
qDebug() << __PRETTY_FUNCTION__;
- QStringList list;
- list = text.split(' ');
-
- for (int i = 0; i < list.count(); i++) {
- if (fontMetrics().width(list.at(i)) > LABEL_MAX_WIDTH)
- list.replace(i, splitWord(list.at(i)));
- }
-
- m_messageText = list.join(" ");
+ m_messageText = TextModifier::splitLongWords(m_statusTextLabel->fontMetrics(), text,
+ LABEL_MAX_WIDTH);
setExpanded(false);
}
{
qDebug() << __PRETTY_FUNCTION__;
- if (expanded)
+ if (expanded) {
m_statusTextLabel->setText(m_messageText);
- else
- m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText, LABEL_MAX_WIDTH));
+ } else {
+ m_statusTextLabel->setText(TextModifier::shortenText(m_statusTextLabel->fontMetrics(),
+ m_messageText, LABEL_MAX_WIDTH));
+ }
}
void UserInfo::setTime(const QString &time)
m_userName = name;
- m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH));
-}
-
-QString UserInfo::shortenText(const QLabel *label, const QString &text, int textMaxWidth)
-{
- qDebug() << __PRETTY_FUNCTION__;
-
- QFontMetrics labelMetrics = label->fontMetrics();
- QString copiedText = text;
- int index = copiedText.indexOf('\n');
-
- if (index >= 0) {
- copiedText.truncate(index);
- copiedText.append("...");
- }
-
- return labelMetrics.elidedText(copiedText, Qt::ElideRight, textMaxWidth);
-}
-
-QString UserInfo::splitWord(const QString &word) const
-{
- qDebug() << __PRETTY_FUNCTION__;
-
- QString result;
- QString temp;
-
- for (int i = 0; i < word.length(); i++) {
- if (fontMetrics().width(temp.append(word.at(i))) > LABEL_MAX_WIDTH) {
- result.append(temp.left(temp.length() - 1));
- result.append(" ");
- temp.remove(0, temp.length() - 1);
- }
- }
-
- result.append(temp);
-
- return result;
+ m_nameLabel->setText(TextModifier::shortenText(m_nameLabel->fontMetrics(), m_userName,
+ LABEL_MAX_WIDTH));
}
void UserInfo::updateLocationDialogFinished(int reason)
*/
void setExpanded(bool expanded);
- /**
- * @brief Elides long text
- *
- * @param label get the fontmetrics from the label
- * @param text long text to be shortened
- * @param textMaxWidth label width
- * @returns shortened text
- */
- QString shortenText(const QLabel *label, const QString &text, int textMaxWidth);
-
- /**
- * @brief Split too long words.
- *
- * Splits long word to several by adding extra spaces
- *
- * @param word long word to be splitted
- * @returns splitted word
- */
- QString splitWord(const QString &word) const;
-
private slots:
/**
* @brief Slot for collapse user info