| 1 |
/* |
| 2 |
* Copyright (C) 2012-2015 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License version 3 as |
| 6 |
* published by the Free Software Foundation. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Authors: |
| 17 |
* Charles Lindsay <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
import QtQuick 2.9 |
| 21 |
|
| 22 |
// An item that contains an inside area the maximum size of a particular aspect |
| 23 |
// ratio (similar to the Image.PreserveAspectFit fillMode of QtQuick's Image |
| 24 |
// element). |
| 25 |
Item { |
| 26 |
id: aspectArea |
| 27 |
|
| 28 |
/*! |
| 29 |
*/ |
| 30 |
property real aspectWidth |
| 31 |
/*! |
| 32 |
*/ |
| 33 |
property real aspectHeight |
| 34 |
|
| 35 |
// The item to place inside (and reparent to) the aspectItem. |
| 36 |
property Item content |
| 37 |
|
| 38 |
// readonly |
| 39 |
// The item that has the correct aspect ratio inside the AspectArea. |
| 40 |
property Item aspectItem: aspectItem |
| 41 |
|
| 42 |
// internal |
| 43 |
/*! |
| 44 |
*/ |
| 45 |
property real aspectRatio: aspectWidth / aspectHeight |
| 46 |
/*! |
| 47 |
*/ |
| 48 |
property bool isWidthConstrained: (height * aspectRatio > width) |
| 49 |
|
| 50 |
Component.onCompleted: { |
| 51 |
if (content) |
| 52 |
content.parent = aspectItem; |
| 53 |
} |
| 54 |
|
| 55 |
Item { |
| 56 |
id: aspectItem |
| 57 |
|
| 58 |
anchors.centerIn: parent |
| 59 |
width: (isWidthConstrained ? parent.width : parent.height * aspectRatio) |
| 60 |
height: (isWidthConstrained ? parent.width / aspectRatio : parent.height) |
| 61 |
} |
| 62 |
} |