--- a/ganeswidgets/src/hgcoverflowcontainer.cpp Fri May 14 16:57:01 2010 +0300
+++ b/ganeswidgets/src/hgcoverflowcontainer.cpp Thu May 27 13:59:05 2010 +0300
@@ -22,33 +22,19 @@
#include "hgcoverflowcontainer.h"
#include "hgmediawallrenderer.h"
#include "hgwidgetitem.h"
+#include "hgcenteritemarea.h"
#include "trace.h"
static const qreal KCameraMaxYAngle(20);
static const qreal KSpringVelocityToCameraYAngleFactor(2);
-static const int KLabelMargin(4);
HgCoverflowContainer::HgCoverflowContainer(
QGraphicsItem* parent) : HgContainer(parent),
- mTitleLabel(0),
- mDescriptionLabel(0),
- mTitlePosition(HgMediawall::PositionAboveImage),
- mDescriptionPosition(HgMediawall::PositionNone),
mPrevPos(-1),
- mAspectRatio(1),
- mAnimationAboutToEndReacted(false)
+ mAnimationAboutToEndReacted(false),
+ mCenterItemArea(0)
{
- mTitleLabel = new HbLabel(this);
- mTitleLabel->setZValue(zValue()+1);
- mTitleLabel->setAlignment(Qt::AlignCenter);
- mTitleLabel->setVisible(false);
-
- mDescriptionLabel = new HbLabel(this);
- mDescriptionLabel->setZValue(zValue()+1);
- mDescriptionLabel->setAlignment(Qt::AlignCenter);
- mDescriptionLabel->setVisible(false);
-
mUserItemSize = QSize(250,250);
mUserItemSpacing = QSize(1,1);
}
@@ -65,15 +51,6 @@
painter->setRenderHint(QPainter::Antialiasing, false);
}
-void HgCoverflowContainer::resizeEvent(QGraphicsSceneResizeEvent *event)
-{
- FUNC_LOG;
-
- HbWidget::resizeEvent(event);
-
- updatePositions();
-}
-
// from HgContainer
HgMediaWallRenderer* HgCoverflowContainer::createRenderer(Qt::Orientation scrollDirection)
{
@@ -125,7 +102,7 @@
if(!mAnimationAboutToEndReacted) {
qreal endPos = mSpring.endPos().x();
qreal abs = qAbs(endPos - mSpring.pos().x());
-
+
if( abs <= 0.5f ) {
HgWidgetItem* item = itemByIndex((int)endPos);
if (item) {
@@ -134,7 +111,7 @@
}
}
}
-
+
qreal ipos = floorf(pos);
qreal frac = pos - ipos;
qreal p = frac > 0.5 ? ipos + 1.0f : ipos;
@@ -158,302 +135,32 @@
mAnimationAboutToEndReacted = false; //reset
}
-void HgCoverflowContainer::handleCurrentChanged(const QModelIndex ¤t)
-{
- FUNC_LOG;
-
- if (current.isValid()) {
- updateLabels(current.row());
- }
-}
-
-void HgCoverflowContainer::itemDataChanged(const int &firstIndex, const int &lastIndex)
-{
- FUNC_LOG;
- HANDLE_ERROR_NULL(mSelectionModel); // If model has been set, also is selection model
-
- HgContainer::itemDataChanged(firstIndex, lastIndex);
-
- if (mSelectionModel->currentIndex().isValid()) {
- int current = mSelectionModel->currentIndex().row();
- if (firstIndex <= current && current <= lastIndex) {
- updateLabels(current);
- }
- }
-
- if (firstIndex == 0) {
- // Take preferred aspect ratio from the first image
- const HgImage *firstImage = image(0);
- if (firstImage && firstImage->height() != 0) {
- mAspectRatio = qMax((qreal)0.1, (qreal)firstImage->width()/firstImage->height()); // Don't let aspect ratio go to 0
- updatePositions();
- }
- }
-}
-
-void HgCoverflowContainer::setTitlePosition(HgMediawall::LabelPosition position)
-{
- FUNC_LOG;
-
- if (mTitlePosition != position) {
- mTitlePosition = position;
- updatePositions();
- }
-}
-
-HgMediawall::LabelPosition HgCoverflowContainer::titlePosition() const
-{
- FUNC_LOG;
-
- return mTitlePosition;
-}
-
-void HgCoverflowContainer::setDescriptionPosition(HgMediawall::LabelPosition position)
-{
- FUNC_LOG;
-
- if (mDescriptionPosition != position) {
- mDescriptionPosition = position;
- updatePositions();
- }
-}
-
-HgMediawall::LabelPosition HgCoverflowContainer::descriptionPosition() const
-{
- FUNC_LOG;
-
- return mDescriptionPosition;
-}
-
-void HgCoverflowContainer::setTitleFontSpec(const HbFontSpec &fontSpec)
-{
- FUNC_LOG;
-
- if (!mTitleLabel) return;
- if (mTitleLabel->fontSpec() != fontSpec) {
- mTitleLabel->setFontSpec(fontSpec);
- updatePositions();
- }
-}
-
-HbFontSpec HgCoverflowContainer::titleFontSpec() const
-{
- FUNC_LOG;
-
- if (!mTitleLabel) return HbFontSpec();
- return mTitleLabel->fontSpec();
-}
-
-void HgCoverflowContainer::setDescriptionFontSpec(const HbFontSpec &fontSpec)
-{
- FUNC_LOG;
-
- if (!mDescriptionLabel) return;
- if (mDescriptionLabel->fontSpec() != fontSpec) {
- mDescriptionLabel->setFontSpec(fontSpec);
- updatePositions();
- }
-}
-
-HbFontSpec HgCoverflowContainer::descriptionFontSpec() const
+void HgCoverflowContainer::calculateItemSize()
{
FUNC_LOG;
- if (!mDescriptionLabel) return HbFontSpec();
- return mDescriptionLabel->fontSpec();
-}
-
-void HgCoverflowContainer::calculatePositions()
-{
- FUNC_LOG;
- HANDLE_ERROR_NULL(mTitleLabel);
- HANDLE_ERROR_NULL(mDescriptionLabel);
+ QRectF containerRect = contentsRect();
+ INFO("Container rect:" << containerRect);
- int height = size().height();
- int width = size().width();
- int titleHeight = QFontMetrics(mTitleLabel->effectiveFontSpec().font()).height();
- int descriptionHeight = QFontMetrics(mDescriptionLabel->effectiveFontSpec().font()).height();
- qreal usableHeight = height-KLabelMargin;
- if (mTitlePosition != HgMediawall::PositionNone) {
- usableHeight -= (titleHeight+KLabelMargin);
- }
- if (mDescriptionPosition != HgMediawall::PositionNone) {
- usableHeight -= (descriptionHeight+KLabelMargin);
- }
-
- if (usableHeight <= 0) return;
-
- qreal usableWidth = width/1.4;
- if (usableWidth <= 0) return;
-
- QRectF imageRect = rect();
- if (usableWidth/usableHeight > mAspectRatio) {
- imageRect.setHeight(usableHeight);
- imageRect.setWidth(mAspectRatio*usableHeight);
- imageRect.moveLeft((width-imageRect.width())/2);
- }
- else {
- imageRect.setWidth(usableWidth);
- imageRect.setHeight(usableWidth/mAspectRatio);
- imageRect.moveTop((usableHeight-imageRect.height())/2);
- imageRect.moveLeft((width-imageRect.width())/2);
- }
-
- QRectF titleGeometry(0, imageRect.top()+KLabelMargin, width, titleHeight);
- QRectF descriptionGeometry(0, imageRect.top()+KLabelMargin, width, descriptionHeight);
+ if (!mCenterItemArea) return;
- if (mTitlePosition == HgMediawall::PositionAboveImage &&
- mDescriptionPosition == HgMediawall::PositionAboveImage) {
- // titleGeometry default is ok
- descriptionGeometry.moveTop(titleGeometry.bottom()+KLabelMargin);
- imageRect.moveTop(descriptionGeometry.bottom()+KLabelMargin);
- }
- else if (mTitlePosition == HgMediawall::PositionBelowImage &&
- mDescriptionPosition == HgMediawall::PositionBelowImage) {
- titleGeometry.moveTop(imageRect.bottom()+KLabelMargin);
- descriptionGeometry.moveTop(titleGeometry.bottom()+KLabelMargin);
- }
- else {
- if (mTitlePosition == HgMediawall::PositionAboveImage) {
- // titleGeometry default is ok
- imageRect.moveTop(titleGeometry.bottom()+KLabelMargin);
- }
- else if (mDescriptionPosition == HgMediawall::PositionAboveImage) {
- // descriptionGeometry default is ok
- imageRect.moveTop(descriptionGeometry.bottom()+KLabelMargin);
- }
+ QRectF centerItemRect = mCenterItemArea->mapRectToParent(mCenterItemArea->contentsRect());
+ INFO("Center item rect:" << centerItemRect);
+ if (centerItemRect.isEmpty()) return;
- if (mTitlePosition == HgMediawall::PositionBelowImage) {
- titleGeometry.moveTop(imageRect.bottom()+KLabelMargin);
- }
- else if (mDescriptionPosition == HgMediawall::PositionBelowImage) {
- descriptionGeometry.moveTop(imageRect.bottom()+KLabelMargin);
- }
- }
-
- INFO("Setting image rect to:" << imageRect << "(total size:" << QSize(width, height)
- << "usable size:" << QSizeF(usableWidth, usableHeight) << ", aspect ratio is:" << mAspectRatio << ")");
-
- mRenderer->setImageSize(imageRect.size());
- qreal diff = rect().center().y()-imageRect.center().y();
-
- INFO("Setting front item position to:" << QPointF(0, -diff) << "(rect:" << rect()
- << "imageRect:" << imageRect << ")");
- mRenderer->setFrontItemPosition(QPointF(0, -diff));
-
- mAutoSize = imageRect.size();
-
- if (mTitlePosition != HgMediawall::PositionNone) {
- INFO("Title geometry:" << titleGeometry);
- mTitleLabel->setGeometry(titleGeometry);
- mTitleLabel->setVisible(true);
- }
- else {
- mTitleLabel->setVisible(false);
- }
- if (mDescriptionPosition != HgMediawall::PositionNone) {
- INFO("Description geometry:" << descriptionGeometry);
- mDescriptionLabel->setGeometry(descriptionGeometry);
- mDescriptionLabel->setVisible(true);
+ QSizeF imageSize(1, 1); // Aspect ratio 1:1
+ if (!mUserItemSize.isEmpty()) {
+ imageSize = mUserItemSize; // Get aspect ration from user item size
}
- else {
- mDescriptionLabel->setVisible(false);
- }
-
- // This may be called before selection model is set.
- if (mSelectionModel && mSelectionModel->currentIndex().isValid()) {
- updateLabels(mSelectionModel->currentIndex().row());
- }
-
- mRenderer->setSpacing(QSizeF(1,1));
-
-}
+ imageSize.scale(centerItemRect.size(), Qt::KeepAspectRatio);
-void HgCoverflowContainer::positionLabels()
-{
- FUNC_LOG;
- HANDLE_ERROR_NULL(mTitleLabel);
- HANDLE_ERROR_NULL(mDescriptionLabel);
-
- int centerIconTop = (size().height() - mRenderer->getImageSize().height()) / 2;
-
- int height = size().height();
- int width = size().width();
- int titleHeight = QFontMetrics(mTitleLabel->effectiveFontSpec().font()).height();
- int descriptionHeight = QFontMetrics(mDescriptionLabel->effectiveFontSpec().font()).height();
+ QPointF delta = centerItemRect.center() - containerRect.center();
+ INFO("Setting image size to:" << imageSize << "delta:" << delta);
- if (mTitlePosition == HgMediawall::PositionAboveImage &&
- mDescriptionPosition == HgMediawall::PositionAboveImage) {
- mTitleLabel->setGeometry(QRectF(
- 0,
- qMax(KLabelMargin, centerIconTop-2*KLabelMargin-titleHeight-descriptionHeight),
- width, titleHeight));
- mDescriptionLabel->setGeometry(QRectF(
- 0,
- mTitleLabel->geometry().bottom()+KLabelMargin,
- width, descriptionHeight));
- }
- else if (mTitlePosition == HgMediawall::PositionBelowImage &&
- mDescriptionPosition == HgMediawall::PositionBelowImage) {
- mDescriptionLabel->setGeometry(QRectF(
- 0,
- height-descriptionHeight-KLabelMargin,
- width, descriptionHeight));
- mTitleLabel->setGeometry(QRectF(
- 0,
- mDescriptionLabel->geometry().top()-titleHeight-KLabelMargin,
- width, titleHeight));
- }
- else {
- if (mTitlePosition == HgMediawall::PositionAboveImage) {
- mTitleLabel->setGeometry(QRectF(
- 0,
- qMax(KLabelMargin, centerIconTop-KLabelMargin-titleHeight),
- width, titleHeight));
- }
- else if (mTitlePosition == HgMediawall::PositionBelowImage) {
- mTitleLabel->setGeometry(QRectF(
- 0,
- height-titleHeight-KLabelMargin,
- width, titleHeight));
- }
-
- if (mDescriptionPosition == HgMediawall::PositionAboveImage) {
- mDescriptionLabel->setGeometry(QRectF(
- 0,
- qMax(KLabelMargin, centerIconTop-KLabelMargin-descriptionHeight),
- width, descriptionHeight));
- }
- else if (mDescriptionPosition == HgMediawall::PositionBelowImage) {
- mDescriptionLabel->setGeometry(QRectF(
- 0,
- height-descriptionHeight-KLabelMargin,
- width, descriptionHeight));
- }
- }
-
- mTitleLabel->setVisible(mTitlePosition != HgMediawall::PositionNone);
- mDescriptionLabel->setVisible(mDescriptionPosition != HgMediawall::PositionNone);
-
- INFO("Title geometry:" << mTitleLabel->geometry() << "visible:" << mTitleLabel->isVisible());
- INFO("Description geometry:" << mDescriptionLabel->geometry() << "visible:" << mDescriptionLabel->isVisible());
-
- if ( mSelectionModel && mSelectionModel->currentIndex().isValid()) {
- updateLabels(mSelectionModel->currentIndex().row());
- }
-}
-
-
-void HgCoverflowContainer::updateLabels(int itemIndex)
-{
- FUNC_LOG;
- HANDLE_ERROR_NULL(mTitleLabel);
- HANDLE_ERROR_NULL(mDescriptionLabel);
-
- if (itemIndex >= 0 && itemIndex < mItems.count()) {
- mTitleLabel->setPlainText(mItems.at(itemIndex)->title());
- mDescriptionLabel->setPlainText(mItems.at(itemIndex)->description());
- }
+ mRenderer->setImageSize(imageSize);
+ mAutoSize = imageSize;
+ setFrontItemPositionDelta(delta);
+ mRenderer->setSpacing(QSizeF(1,1));
}
void HgCoverflowContainer::scrollToPosition(const QPointF& pos, bool animate)
@@ -463,16 +170,6 @@
HgContainer::scrollToPosition(p,animate);
}
-void HgCoverflowContainer::setDefaultImage(QImage defaultImage)
-{
- HgContainer::setDefaultImage(defaultImage);
-
- if (!defaultImage.isNull()) {
- mAspectRatio = qMax((qreal)0.1, (qreal)defaultImage.width()/defaultImage.height()); // Don't let aspect ratio go to 0
- updatePositions();
- }
-}
-
QSizeF HgCoverflowContainer::getAutoItemSize() const
{
return mAutoSize;
@@ -487,17 +184,14 @@
{
HgContainer::updateItemSizeAndSpacing();
- updatePositions();
+ updateItemSize();
}
-void HgCoverflowContainer::updatePositions()
+void HgCoverflowContainer::updateItemSize()
{
if (mItemSizePolicy == HgWidget::ItemSizeAutomatic) {
- calculatePositions();
- }
- else {
- positionLabels();
+ calculateItemSize();
}
}
@@ -525,4 +219,15 @@
return mRenderer ? mRenderer->reflectionsEnabled() : false;
}
+void HgCoverflowContainer::setCenterItemArea(HgCenterItemArea *centerItemArea)
+{
+ FUNC_LOG;
+ if (!mCenterItemArea) {
+ mCenterItemArea = centerItemArea;
+ connect(mCenterItemArea, SIGNAL(sizeChanged()), SLOT(updateItemSize()));
+ }
+}
+
+
+