| 1 | /* Copyright (C) 2016 - 2017 Dan Chapman <[email protected]> |
| 2 | |
| 3 | This file is part of Dekko email client for Ubuntu devices |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License as |
| 7 | published by the Free Software Foundation; either version 2 of |
| 8 | the License or (at your option) version 3 |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include "PixelPerfectItem.h" |
| 19 | |
| 20 | PixelPerfectItem::PixelPerfectItem(QQuickItem *parent): QQuickItem(parent), |
| 21 | m_content(Q_NULLPTR), m_timer(this) |
| 22 | { |
| 23 | m_timer.setInterval(50); |
| 24 | m_timer.setSingleShot(true); |
| 25 | connect(&m_timer, &QTimer::timeout, this, &PixelPerfectItem::polish, Qt::UniqueConnection); |
| 26 | } |
| 27 | |
| 28 | void PixelPerfectItem::componentComplete() |
| 29 | { |
| 30 | // grab a pointer to this |
| 31 | QQuickItem *ptr = this; |
| 32 | // Now create a vector of all ancestors |
| 33 | while (ptr != Q_NULLPTR) { |
| 34 | m_ancestors.append(ptr); |
| 35 | connect(ptr, &QQuickItem::xChanged, this, &PixelPerfectItem::triggerUpdate, Qt::UniqueConnection); |
| 36 | connect(ptr, &QQuickItem::yChanged, this, &PixelPerfectItem::triggerUpdate, Qt::UniqueConnection); |
| 37 | ptr = ptr->parentItem(); |
| 38 | } |
| 39 | // repaint |
| 40 | polish(); |
| 41 | } |
| 42 | |
| 43 | void PixelPerfectItem::setContent(QQuickItem *item) |
| 44 | { |
| 45 | if (m_content == item) { |
| 46 | return; |
| 47 | } |
| 48 | m_content = item; |
| 49 | emit contentChanged(item); |
| 50 | } |
| 51 | |
| 52 | void PixelPerfectItem::updatePolish() |
| 53 | { |
| 54 | if (m_content == Q_NULLPTR) { |
| 55 | return; |
| 56 | } |
| 57 | QPointF absolute; |
| 58 | for (QVector<QQuickItem *>::const_iterator it = m_ancestors.constBegin(); it != m_ancestors.constEnd(); it++) { |
| 59 | absolute += (*it)->position(); |
| 60 | } |
| 61 | m_content->setPosition(absolute.toPoint() - absolute); |
| 62 | } |
| 63 | |
| 64 | void PixelPerfectItem::triggerUpdate() |
| 65 | { |
| 66 | m_timer.stop(); |
| 67 | m_timer.start(); |
| 68 | } |
| 69 |