summaryrefslogtreecommitdiffstats
path: root/src/other/deviceitem.h
blob: 2a8d4a2f517c0ca24e0f83eef445a35124771911 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**************************************************************************
**
** This file is part of Qt Simulator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#ifndef DEVICEITEM_H
#define DEVICEITEM_H

#include <QtGui/QGraphicsObject>
#include <QtCore/QStateMachine>

#define SIMULATOR_MENU_Z 10000

struct Button
{
    QRectF area;
    QString text;
    Qt::Key key;
};

enum Orientation {
    notSet,
    portraitOrientation,
    landscapeOrientation,
};

struct MenuData {
    QString pixmapPath;
    mutable QSharedPointer<QPixmap> pixmap;
    QRect availableGeometry;
};

struct DeviceData
{
    DeviceData()
        : diagonalInInch(0.)
        , defaultFontSize(12)
        , forceDpi(-1)
        , nativeOrientation(notSet)
    {}

    QString name;
    QSize resolution;
    QRect availableGeometry;
    qreal diagonalInInch;
    QString mockupPath;
    mutable QSharedPointer<QPixmap> mockup;
    QPoint offset;
    int defaultFontSize;
    int forceDpi;
    QList<Button> buttons;
    QString style;
    Orientation nativeOrientation;
    MenuData landscapeMenu;
    MenuData portraitMenu;
};

class DisplayWidget;
class QState;

class MenuPixmapItem: public QGraphicsPixmapItem
{
public:
    MenuPixmapItem(const QPixmap &pixmap, QGraphicsItem *parent = 0)
        : QGraphicsPixmapItem(pixmap, parent)
    {}
    enum {MenuType = UserType + 2};
    int type() const {return MenuType;};
};

class DeviceButton: public QGraphicsObject
{
    Q_OBJECT
public:
    explicit DeviceButton(QGraphicsItem *parent = 0);
    virtual ~DeviceButton();

    virtual QRectF boundingRect() const;
    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);

    void setSize(const QSizeF &size);
    QSizeF size() const;

protected:
    virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
    virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

private:
    QSizeF mSize;

signals:
    void pressed() const;
    void released() const;
};

class KeyButton: public QGraphicsObject
{
    Q_OBJECT
public:
    explicit KeyButton(const Button &button, QGraphicsItem *parent = 0);
    virtual ~KeyButton();

    virtual QRectF boundingRect() const;
    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);

protected:
    virtual void hoverEnterEvent(QGraphicsSceneHoverEvent  *event);
    virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent  *event);
    virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
    virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

private:
    Button mButton;
    bool mMouseOver;

signals:
    void pressed(Qt::Key, QString) const;
    void released(Qt::Key) const;
};

class DeviceItem: public QGraphicsObject
{
    Q_OBJECT
public:
    DeviceItem(QGraphicsItem *parent = 0);
    ~DeviceItem();

    DisplayWidget *display();

    bool isLandscape() { return mLandscape; }
    virtual QRectF boundingRect() const;
    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);

public slots:
    void changeDevice(const DeviceData &data);
    void toggleOrientation();
    void setInitialRotation(bool rotate);
    void changeScaleFactor(qreal newScaleFactor);

protected:
    virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
    virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);

private:
    void newWindowSize();
    void newViewSize();
    int rotationSideLength();
    void updateMenuPositionAndSize();

    bool mLandscape;

    DeviceData mDeviceData;

    QSizeF mSize;
    QRect mAvailableGeometry;
    DisplayWidget *mDisplay;
    QGraphicsPixmapItem *mMockup;

    QStateMachine mStateMachine;
    QState *mLandscapeState;
    QState *mPortraitState;

    QPoint mDragOffset;

    QList<KeyButton*> mButtons;
    MenuPixmapItem *mMenu;

private slots:
    void startRotate();
    void setLandscape();
    void setPortrait();

signals:
    void rotationSignal();
    void sizeChanged(const QSize &size);
    void drag(const QPoint &to); // to is the new top-left point of the view in screen coordinates
    void viewSizeRequired(const QSize &size);

    void deviceChanged(const QSize &resolution, const DeviceData &device);
    void offsetChanged(const QPoint &newOffset);

    void buttonPressed(Qt::Key key, QString text);
    void buttonReleased(Qt::Key key);
    void closeWindowPressed();

    void orientationChanged(Orientation current, Orientation native);
    void deviceChanged(Orientation current, Orientation currentNative, Orientation targetNative);
};

#endif //DEVICEITEM_H