X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Fui%2Fuserinfo.cpp;fp=src%2Fui%2Fuserinfo.cpp;h=50dd2cbf63bce0938c44b34e6f034d7e5097a099;hb=4604419a57d599a581dff5d292dddf0076960107;hp=a775b02830c553baf3f8ef6afcdad7a72954c794;hpb=e3196a86eee87ed2c97646c3585d5a79d7bd001e;p=situare diff --git a/src/ui/userinfo.cpp b/src/ui/userinfo.cpp index a775b02..50dd2cb 100644 --- a/src/ui/userinfo.cpp +++ b/src/ui/userinfo.cpp @@ -36,22 +36,13 @@ #include "userinfo.h" -const int BACKGROUND_WIDTH = 368; ///< Width for item -const int BACKGROUND_TOP_HEIGHT = 20; ///< Height for item top -const int BACKGROUND_BOTTOM_HEIGHT = 15; ///< Height for item bottom -const int ICON_HEIGHT = 24; ///< Icon height -const int ICON_WIDTH = 24; ///< Icon width -const int MARGIN = 5; ///< Icon margin -const int LINE_LENGTH = 27; ///< Line length -const int MOUSE_PRESS_AREA_WIDTH = 20; ///< Area width for item height toggling -const int MOUSE_PRESS_AREA_HEIGHT = 20; ///< Area height for item height toggling - -/** -* @var LABEL_MAX_WIDTH -* -* @brief All label's maximum width -*/ -const int LABEL_MAX_WIDTH = BACKGROUND_WIDTH - 3 * MARGIN - ICON_WIDTH + 130; +const int BACKGROUND_BOTTOM_HEIGHT = 15; +const int BACKGROUND_TOP_HEIGHT = 20; +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; UserInfo::UserInfo(QWidget *parent) : QWidget(parent), @@ -86,10 +77,9 @@ UserInfo::UserInfo(QWidget *parent) clockLabel->setContentsMargins(0, 0, MARGIN, 0); clockLabel->setFixedSize(ICON_WIDTH + MARGIN, ICON_HEIGHT); - m_findButton = new ImageButton(); + m_avatar = new ImageButton(); m_nameLabel = new QLabel(); - m_nameLabel->setWordWrap(true); m_statusTextLabel = new QLabel(); m_statusTextLabel->setWordWrap(true); @@ -112,7 +102,7 @@ UserInfo::UserInfo(QWidget *parent) infoLayout->addRow(compassLabel, m_locationLabel); infoLayout->addRow(clockLabel, m_updatedLabel); - verticalLayout->addWidget(m_findButton, 0, Qt::AlignHCenter); + verticalLayout->addWidget(m_avatar, 0, Qt::AlignHCenter); verticalLayout->addWidget(m_nameLabel, 0, Qt::AlignHCenter); verticalLayout->addLayout(infoLayout); verticalLayout->addLayout(buttonLayout); @@ -123,7 +113,7 @@ UserInfo::UserInfo(QWidget *parent) connect(updateFriendsButton,SIGNAL(clicked()), this, SIGNAL(refreshUserData())); - connect(m_findButton, SIGNAL(clicked()), + connect(m_avatar, SIGNAL(clicked()), this, SLOT(findButtonClicked())); setFixedWidth(BACKGROUND_WIDTH); @@ -159,177 +149,179 @@ UserInfo::~UserInfo() } } -void UserInfo::setAddress(const QString &address) +void UserInfo::backupUpdateLocationDialogData(const QString &status, bool publish) { qDebug() << __PRETTY_FUNCTION__; - m_locationLabel->setText(address); + m_backupMessage = status; + m_backupFacebookPublishPolicity = publish; } -void UserInfo::setCoordinates(const GeoCoordinate &coordinates) +void UserInfo::clearUpdateLocationDialogData() { qDebug() << __PRETTY_FUNCTION__; - m_coordinates = coordinates; + m_backupMessage.clear(); + m_backupFacebookPublishPolicity = false; } -void UserInfo::setMessageText(const QString &text) +void UserInfo::collapse() { qDebug() << __PRETTY_FUNCTION__; - m_messageText = text; - m_expandedMessageText.clear(); - QString temp = ""; - for(int i=0;i < text.length();i++) { - if(fontMetrics().width(temp.append(text.at(i))) > 170) { - temp.append("\n"); - if(temp.startsWith(QString(" "))) - temp.remove(0, 1); - - m_expandedMessageText.append(temp); - temp.clear(); - } - } - m_expandedMessageText.append(temp); - setText(false); + setExpanded(false); } -void UserInfo::setProfileImage(const QPixmap &image) +void UserInfo::findButtonClicked() { qDebug() << __PRETTY_FUNCTION__; - if(!image.isNull()) - m_findButton->setButtonIcon(image); + emit findUser(m_coordinates); } -void UserInfo::setTime(const QString &time) +void UserInfo::messageUpdate() { qDebug() << __PRETTY_FUNCTION__; - m_updatedLabel->setText(time); -} + delete m_updateLocation; + m_updateLocation = new UpdateLocationDialog(m_backupMessage, m_backupFacebookPublishPolicity, + this); -void UserInfo::setUserName(const QString &name) -{ - qDebug() << __PRETTY_FUNCTION__; + connect(this, SIGNAL(reverseGeoReady(QString)), + m_updateLocation, SLOT(setAddress(QString))); - m_userName = name; - setText(false); -} + connect(m_updateLocation, SIGNAL(statusUpdate(QString, bool)), + this, SIGNAL(statusUpdate(QString, bool))); -void UserInfo::setText(bool expanded) -{ - qDebug() << __PRETTY_FUNCTION__; + connect(m_updateLocation, SIGNAL(statusUpdate(QString, bool)), + this, SLOT(backupUpdateLocationDialogData(QString, bool))); - if (expanded) { - m_statusTextLabel->setText(m_expandedMessageText); - } - else { - m_nameLabel->setText(shortenText(m_nameLabel, m_userName, LABEL_MAX_WIDTH)); - m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText, - LABEL_MAX_WIDTH)); - } -} - -void UserInfo::backupUpdateLocationDialogData(const QString &status, bool publish) -{ - qDebug() << __PRETTY_FUNCTION__; - - m_backupMessage = status; - m_backupFacebookPublishPolicity = publish; -} + connect(m_updateLocation, SIGNAL(finished(int)), + this, SLOT(updateLocationDialogFinished(int))); -void UserInfo::clearUpdateLocationDialogData() -{ - qDebug() << __PRETTY_FUNCTION__; + m_updateLocation->show(); - m_backupMessage.clear(); - m_backupFacebookPublishPolicity = false; + emit requestReverseGeo(); } -void UserInfo::findButtonClicked() +void UserInfo::mousePressEvent(QMouseEvent *event) { - qDebug() << __PRETTY_FUNCTION__; + qDebug() << __PRETTY_FUNCTION__ << " " << event->pos(); - emit findUser(m_coordinates); + m_mousePosition = event->pos(); } void UserInfo::mouseReleaseEvent(QMouseEvent *event) { qDebug() << __PRETTY_FUNCTION__ << " " << event->pos(); - if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) && - (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) { + const int MOUSE_PRESS_AREA_HEIGHT = 20; + const int MOUSE_PRESS_AREA_WIDTH = 20; + + if ((abs(m_mousePosition.y() - event->pos().y()) <= MOUSE_PRESS_AREA_WIDTH) + && (abs(m_mousePosition.x() - event->pos().x()) <= MOUSE_PRESS_AREA_HEIGHT)) { if (m_expanded) { - setText(false); + setExpanded(false); m_expanded = false; } else { - setText(true); + setExpanded(true); m_expanded = true; } } } -void UserInfo::mousePressEvent(QMouseEvent *event) +void UserInfo::paintEvent(QPaintEvent *event) { - qDebug() << __PRETTY_FUNCTION__ << " " << event->pos(); + qDebug() << __PRETTY_FUNCTION__ << " " << event->rect(); - m_mousePosition = event->pos(); + QPainter painter(this); + + QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT); + + QRect middleRect = QRect(topRect.left(), topRect.bottom() + 1, BACKGROUND_WIDTH, + this->height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT); + + QRect bottomRect = QRect(topRect.left(), middleRect.bottom() + 1, BACKGROUND_WIDTH, + BACKGROUND_BOTTOM_HEIGHT); + + painter.drawPixmap(topRect, m_backgroundTopImage); + painter.drawPixmap(middleRect, m_backgroundMiddleImage); + painter.drawPixmap(bottomRect, m_backgroundBottomImage); } -void UserInfo::messageUpdate() +void UserInfo::restoreUnsendMessage() { qDebug() << __PRETTY_FUNCTION__; - delete m_updateLocation; - m_updateLocation = new UpdateLocationDialog(m_backupMessage, m_backupFacebookPublishPolicity, - this); + QSettings settings(DIRECTORY_NAME, FILE_NAME); + m_backupMessage = settings.value(USER_UNSEND_MESSAGE, EMPTY).toString(); + m_backupFacebookPublishPolicity = settings.value(USER_UNSEND_MESSAGE_PUBLISH, false).toBool(); +} - connect(this, SIGNAL(reverseGeoReady(QString)), - m_updateLocation, SLOT(setAddress(QString))); +void UserInfo::setAddress(const QString &address) +{ + qDebug() << __PRETTY_FUNCTION__; + + m_locationLabel->setText(address); +} - connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)), - this, SIGNAL(statusUpdate(QString,bool))); +void UserInfo::setCoordinates(const GeoCoordinate &coordinates) +{ + qDebug() << __PRETTY_FUNCTION__; - connect(m_updateLocation, SIGNAL(statusUpdate(QString,bool)), - this, SLOT(backupUpdateLocationDialogData(QString,bool))); + m_coordinates = coordinates; +} - connect(m_updateLocation, SIGNAL(finished(int)), - this, SLOT(updateLocationDialogFinished(int))); +void UserInfo::setMessageText(const QString &text) +{ + qDebug() << __PRETTY_FUNCTION__; - m_updateLocation->show(); + QStringList list; + list = text.split(' '); - emit requestReverseGeo(); + 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(" "); + + setExpanded(false); } -void UserInfo::paintEvent(QPaintEvent *aPaintEvent) +void UserInfo::setProfileImage(const QPixmap &image) { - qDebug() << __PRETTY_FUNCTION__ << " " << aPaintEvent->rect(); + qDebug() << __PRETTY_FUNCTION__; - QPainter painter(this); + if(!image.isNull()) + m_avatar->setButtonIcon(image); +} - QRect topRect = QRect(0, MARGIN, BACKGROUND_WIDTH, BACKGROUND_TOP_HEIGHT); -///< @todo Overlaps with topRect? - QRect middleRect = QRect(0, topRect.bottom(), BACKGROUND_WIDTH, - this->height() - BACKGROUND_TOP_HEIGHT - BACKGROUND_BOTTOM_HEIGHT); -///< @todo Overlaps with middleRect - QRect bottomRect = QRect(topRect.left(), middleRect.bottom(), BACKGROUND_WIDTH, - BACKGROUND_BOTTOM_HEIGHT); +void UserInfo::setExpanded(bool expanded) +{ + qDebug() << __PRETTY_FUNCTION__; - painter.drawPixmap(topRect, m_backgroundTopImage); - painter.drawPixmap(middleRect, m_backgroundMiddleImage); - painter.drawPixmap(bottomRect, m_backgroundBottomImage); + if (expanded) + m_statusTextLabel->setText(m_messageText); + else + m_statusTextLabel->setText(shortenText(m_statusTextLabel, m_messageText, LABEL_MAX_WIDTH)); } -void UserInfo::restoreUnsendMessage() +void UserInfo::setTime(const QString &time) { qDebug() << __PRETTY_FUNCTION__; - QSettings settings(DIRECTORY_NAME, FILE_NAME); - m_backupMessage = settings.value(USER_UNSEND_MESSAGE, EMPTY).toString(); - m_backupFacebookPublishPolicity = - settings.value(USER_UNSEND_MESSAGE_PUBLISH, false).toBool(); + m_updatedLabel->setText(time); +} + +void UserInfo::setUserName(const QString &name) +{ + qDebug() << __PRETTY_FUNCTION__; + + 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) @@ -337,18 +329,35 @@ QString UserInfo::shortenText(const QLabel *label, const QString &text, int text qDebug() << __PRETTY_FUNCTION__; QFontMetrics labelMetrics = label->fontMetrics(); - QString textParam = text; - int index = textParam.indexOf('\n'); - int textWidth = fontMetrics().width(textParam); + QString copiedText = text; + int index = copiedText.indexOf('\n'); + + if (index >= 0) { + copiedText.truncate(index); + copiedText.append("..."); + } - if (index > 0) { - textParam.truncate(index); - textParam.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); } - if (textWidth > 250) ///< @todo magic number - textParam.insert(LINE_LENGTH, QString("\n")); + } + + result.append(temp); - return labelMetrics.elidedText(textParam, Qt::ElideRight, textMaxWidth); + return result; } void UserInfo::updateLocationDialogFinished(int reason)