diff -r 2e2dc3d30ca8 -r 341166945d65 homescreenapp/hsutils/src/hswidgetpositioningonwidgetmove.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/homescreenapp/hsutils/src/hswidgetpositioningonwidgetmove.cpp Fri Jun 25 19:19:22 2010 +0300 @@ -0,0 +1,1013 @@ +/* +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of "Eclipse Public License v1.0" +* which accompanies this distribution, and is available +* at the URL "http://www.eclipse.org/legal/epl-v10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: +* +*/ + +#include +#include "hswidgetpositioningonwidgetmove.h" +#include "hsapp_defs.h" + + +/*! + The distance by which the line is extended on each side of reference points +*/ +#define SNAP_LINE_EXTEND_VALUE 10.0 + +/*! + Sets the snap method instance. The existing instance + will be deleted. +*/ +void HsWidgetPositioningOnWidgetMove::setInstance(HsWidgetPositioningOnWidgetMove *instance) +{ + if (mInstance) + delete mInstance; + mInstance = instance; +} + +/*! + Returns the snap method instance. +*/ +HsWidgetPositioningOnWidgetMove *HsWidgetPositioningOnWidgetMove::instance() +{ + return mInstance; +} + +/*! + Points to the snap method instance. +*/ +HsWidgetPositioningOnWidgetMove *HsWidgetPositioningOnWidgetMove::mInstance = 0; + +/*! + Constructor. +*/ +HsSnapToLines::HsSnapToLines() : + mActiveRectWidth(0.0), mActiveRectHeight(0.0), + mHorizontalSnapPosition(0.0), mVerticalSnapPosition(0.0), + mHorizontalSnapFound(false), + mVerticalSnapFound(false), + mRectLieAbove(false), mLeftInRange(false), mRightInRange(false), + mMinVerticalEdgesDistance(0.0), mVerticalDistance(0.0), + mVerticalDistanceFromSelectedRect(0.0), mContainerVerticalEdgeDistance(0.0), + mRectLieLeft(false), mTopInRange(false), mBottomInRange(false), + mMinHorizontalEdgesDistance(0.0), mHorizontalDistance(0.0), + mHorizontalDistanceFromSelectedRect(0.0), mContainerHorizontalEdgeDistance(0.0), + mSnapEnabled(false), mSnapForce(0.0), mSnapGap(0.0), mSnapBorderGap(0.0), + mRectVerticalEdgeLiesInLineWithVerticalLine(false), mRectLieAboveVerticalLine(false), mRectLieBelowVerticalLine(false), + mRectHorizontalEdgeLiesInLineWithHorizontalLine(false), mRectLieLeftOfHorizontalLine(false), mRectLiesRightOfHorizontalLine(false) +{ + +} + +/*! + Configures the snap-to-lines algorithm. +*/ +void HsSnapToLines::setConfiguration(const QVariantHash &configuration) +{ + bool canConvert = false; + mSnapEnabled = configuration[SNAPENABLED].toBool(); + //The following values should be in qreal, so the status received in canConvert is ignored + mSnapForce = configuration[SNAPFORCE].toDouble(&canConvert); + mSnapGap = configuration[SNAPGAP].toDouble(&canConvert); + mSnapBorderGap = configuration[SNAPBORDERGAP].toDouble(&canConvert); +} + +/*! + Set the dimensions of container rect, active rect and position of Inactive Rects + on the page. +*/ +void HsSnapToLines::setPagePresentation(const QRectF &containerRect, + const QList &inactiveRects, + const QRectF &activeRect) +{ + mContainerRect.setLeft(containerRect.left() + mSnapBorderGap); + mContainerRect.setTop(containerRect.top() + mSnapBorderGap); + mContainerRect.setRight(containerRect.right() - mSnapBorderGap); + mContainerRect.setBottom(containerRect.bottom() - mSnapBorderGap); + + mActiveRectWidth = activeRect.width(); + mActiveRectHeight = activeRect.height(); + + createSnappableRectangles(inactiveRects); +} + +/*! + Create the list of rects and flag if their sides are snappable from top or bottom or left or right, + depending on other rects overlapping with the rect. +*/ +void HsSnapToLines::createSnappableRectangles(const QList &inactiveRects) +{ + mInactiveSnapRects.clear(); + + int i; + for (i = 0; i mVerticalLine.y1() && mInactiveRectToCompare.bottom() > mVerticalLine.y2()) { + mRectLieBelowVerticalLine = true; + } + //if the rectangle lies above the vertical line + if (mInactiveRectToCompare.top() < mVerticalLine.y1() && mInactiveRectToCompare.top() < mVerticalLine.y2()) { + mRectLieAboveVerticalLine = true; + } + } +} + +/*! + Increase the Vertical line to include the inactive rect whose vertical edge is inline with vertical line +*/ +void HsSnapToLines::extendVerticalLineToIncludeInactiveRect() +{ + if (mRectLieAboveVerticalLine) { + if (mVerticalLine.y1() < mVerticalLine.y2()) { + mVerticalLine.setP1(QPointF(mVerticalLine.x1(), mInactiveRectToCompare.top())); + } + else { + mVerticalLine.setP2(QPointF(mVerticalLine.x1(), mInactiveRectToCompare.top())); + } + } + if (mRectLieBelowVerticalLine) { + if (mVerticalLine.y1() < mVerticalLine.y2()) { + mVerticalLine.setP2(QPointF(mVerticalLine.x1(), mInactiveRectToCompare.bottom())); + } + else { + mVerticalLine.setP1(QPointF(mVerticalLine.x1(), mInactiveRectToCompare.bottom())); + } + } +} + +/*! + Check if the Horizontal edge of the Rectangle lies inline with the Horizontal line. + Also check if the rectangle's edge lies out of the line. +*/ +void HsSnapToLines::checkInactiveRectPositionToHorizontalLine() +{ + mRectHorizontalEdgeLiesInLineWithHorizontalLine = false; + mRectLieLeftOfHorizontalLine = false; + mRectLiesRightOfHorizontalLine = false; + + //if rectangle horizontal edge lies inline with Horizontal line. + if ((mTopInRange && mInactiveRectToCompare.top() == mHorizontalLine.y1()) + || (mBottomInRange && mInactiveRectToCompare.bottom() == mHorizontalLine.y1())) { + mRectHorizontalEdgeLiesInLineWithHorizontalLine = true; + //if the rectangle lies left of the horizontal line + if (mInactiveRectToCompare.left() < mHorizontalLine.x1() && mInactiveRectToCompare.left() < mHorizontalLine.x2()) { + mRectLieLeftOfHorizontalLine = true; + } + //if the rectangle lies right of the horizontal line + if (mInactiveRectToCompare.right() > mHorizontalLine.x1() && mInactiveRectToCompare.right() > mHorizontalLine.x2()) { + mRectLiesRightOfHorizontalLine = true; + } + } +} + +/*! + Increase the Horizontal line to include the inactive rect whose horizontal edge is inline with horizontal line +*/ +void HsSnapToLines::extendHorizontalLineToIncludeInactiveRect() +{ + if (mRectLieLeftOfHorizontalLine) { + if (mHorizontalLine.x1() < mHorizontalLine.x2()) { + mHorizontalLine.setP1(QPointF(mInactiveRectToCompare.left(), mHorizontalLine.y1())); + } + else { + mHorizontalLine.setP2(QPointF(mInactiveRectToCompare.left(), mHorizontalLine.y1())); + } + } + if (mRectLiesRightOfHorizontalLine) { + if (mHorizontalLine.x1() < mHorizontalLine.x2()) { + mHorizontalLine.setP2(QPointF(mInactiveRectToCompare.right(), mHorizontalLine.y1())); + } + else { + mHorizontalLine.setP1(QPointF(mInactiveRectToCompare.right(), mHorizontalLine.y1())); + } + } +}