summaryrefslogtreecommitdiff
path: root/tizen/src/ui/rotaryview.cpp
blob: fdb6816d7fbd758990a01cb2050da14bfa7c0356 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Qt UI
 *
 * Copyright (C) 2015 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact:
 * GiWoong Kim <giwoong.kim@samsung.com>
 * Sangho Park <sangho1206.park@samsung.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 * Contributors:
 * - S-Core Co., Ltd
 *
 */

#include <QWidget>

#include "rotaryview.h"
#include "mainwindow.h"

extern "C" {
#include "util/ui_operations.h"
}

#define ROTARY_DRAWING_INTERVAL 100

RotaryView::RotaryView(QWidget *parent) :
    QGraphicsView(new QGraphicsScene(parent), parent)
{
    this->win = (MainWindow *)parent;
    this->rotaryImg = NULL;
    this->rotaryItem = NULL;
    this->grabPos = QPoint(-1, -1);
    this->grabAngle = 0;
    this->rotaryDegree = 0;
    this->lastTimestamp = 0;

    timer = new QTimer(this);
    timer->setSingleShot(true);
    connect(timer, SIGNAL(timeout()), this, SLOT(rotateRotaryItem()));

    setStyleSheet("background-color: rgb(189, 189, 189); border-style: none");
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setAlignment(Qt::AlignLeft | Qt::AlignTop);

    updateLayout();
}

void RotaryView::createItems()
{
    rotaryItem = new QGraphicsPixmapItem(*rotaryImg);
    rotaryItem->setTransformOriginPoint(
        rotaryImg->width() / 2, rotaryImg->height() / 2);
    rotaryItem->setRotation(rotaryDegree);
    scene()->addItem(rotaryItem);
}

void RotaryView::updateLayout()
{
    scene()->clear();

    rotaryImg = win->getUiInfo()->getMainForm()->getRotaryImage();
    const QRect displayRect =
        win->getUiInfo()->getMainFormDpyType()->getRect();

    /* repositioning & resizing */
    const int alignOffsetX = (rotaryImg->width() - displayRect.width()) / 2;
    const int alignOffsetY = (rotaryImg->height() - displayRect.height()) / 2;
    const qreal scaleFactor = win->getUiState()->getScaleFactor();

    setGeometry(
        (displayRect.x() - alignOffsetX) * scaleFactor,
        (displayRect.y() - alignOffsetY) * scaleFactor,
        rotaryImg->width() * scaleFactor,
        rotaryImg->height() * scaleFactor);
    scene()->setSceneRect(0, 0, width(), height());

    createItems();
}

void RotaryView::rotateRotaryItem()
{
    rotaryItem->setRotation(rotaryDegree);
}

/* override */
void RotaryView::resizeEvent(QResizeEvent *event)
{
    qDebug() << "resize rotary view:" << size();

    /* scaling */
    const qreal sx = win->getUiState()->getScaleFactor();
    const qreal sy = win->getUiState()->getScaleFactor();
    setTransform(QTransform(sx, 0, 0, 0, sy, 0, 0, 0, 1));

    /* masking */
    QPixmap scaledRotaryImg = rotaryImg->scaled(
        rotaryImg->width() * win->getUiState()->getScaleFactor(),
        rotaryImg->height() * win->getUiState()->getScaleFactor());
    setMask(scaledRotaryImg.mask());

    QGraphicsView::resizeEvent(event);
}

/* override */
void RotaryView::setMask(const QRegion &region)
{
    if (region.isEmpty() == false) {
        QWidget::setMask(region);
    }
}

int RotaryView::getAngleFromVector(QPoint pos)
{
    return (int)(qAtan2(
        pos.y() - (height() / 2), pos.x() - (width() / 2)) *
        float(180 / M_PI) /* radians to degress */);
}

/* override */
void RotaryView::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        grabPos = event->pos();
        grabAngle = getAngleFromVector(grabPos);
    }

    QGraphicsView::mousePressEvent(event);
}

/* override */
void RotaryView::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        grabPos = QPoint(-1, -1);
        grabAngle = 0;
    }

    rotateRotaryItem();

    QGraphicsView::mouseReleaseEvent(event);
}

/* override */
void RotaryView::mouseMoveEvent(QMouseEvent *event)
{
    if (grabPos != QPoint(-1, -1)) {
        const int moveAngle = getAngleFromVector(event->pos());

        int deltaAngle = moveAngle - grabAngle;
        if (deltaAngle < -180) {
            deltaAngle += 360; /* 359 -> 0 */
        } else if (deltaAngle > 180) {
            deltaAngle -= 360; /* 0 -> 359 */
        }

        /* convert to device event */
        do_rotary_event(deltaAngle, 0);

        rotaryDegree += deltaAngle;
        grabAngle = moveAngle;

        /* drawing */
        if ((event->timestamp() - lastTimestamp) > ROTARY_DRAWING_INTERVAL) {
            timer->stop();
            rotateRotaryItem();
            lastTimestamp = event->timestamp();
        } else {
            timer->start(ROTARY_DRAWING_INTERVAL * 3);
        }
    }

    QGraphicsView::mouseMoveEvent(event);
}

RotaryView::~RotaryView()
{
    qDebug("destroy rotary view");

    timer->stop();
}