summaryrefslogtreecommitdiffstats
path: root/src/other/deviceitem.h
blob: e5d98dac301f91bb2dbb0efbc7003b78b684becb (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**************************************************************************
**
** This file is part of Qt Simulator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** No Commercial Usage
**
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/

#ifndef DEVICEITEM_H
#define DEVICEITEM_H

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

#define SIMULATOR_MENU_Z 10000

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

enum Orientation {
    topUp       = 0x0000001,
    topDown     = 0x0000002,
    leftUp      = 0x0000004,
    rightUp     = 0x0000008,
};
Q_DECLARE_FLAGS(SupportedOrientations, Orientation)

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

struct SymbianSoftKeyButtonData {
    enum ButtonType {
        PositiveButton = 0,
        NegativeButton = 1,
        MaxButtons = 5
    };
    Orientation orientation;
    QRectF area;
    int buttonNumber;
};

enum MaemoNavigationMode {
    maemoBack,
    maemoClose
};

struct DeviceData
{
    DeviceData()
        : diagonalInInch(0.)
        , defaultFontSize(12)
        , forceDpi(-1)
        , landscapeOrientation(topUp)
        , portraitOrientation(topUp)
    {}

    QString name;
    QSize resolution;
    QRect availableGeometry;
    qreal diagonalInInch;
    QString mockupPath;
    mutable QSharedPointer<QPixmap> mockup;
    QPoint offset;
    int defaultFontSize;
    int forceDpi;
    QString maemoNavigationBackButton;
    mutable QSharedPointer<QPixmap> maemoBackPixmap;
    QString maemoNavigationCloseButton;
    mutable QSharedPointer<QPixmap> maemoClosePixmap;
    QPointF maemoNavigationButtonPortrait;
    QPointF maemoNavigationButtonLandscape;
    QList<Button> buttons;
    QString style;
    QString styleTheme;
    SupportedOrientations supportedOrientations;
    QHash<Orientation, MenuData> menus;
    QList<SymbianSoftKeyButtonData> symbianSoftKeys;
    Orientation landscapeOrientation;
    Orientation portraitOrientation;
};

class DisplayWidget;
class QState;
class QPropertyAnimation;

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 PixmapButton: public QObject, public QGraphicsPixmapItem
{
    Q_OBJECT
public:
    explicit PixmapButton(QGraphicsItem *parent = 0) : QGraphicsPixmapItem(parent) {};
    virtual ~PixmapButton() {};

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

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 SymbianSoftKeyButton: public QGraphicsObject
{
    Q_OBJECT
public:
    explicit SymbianSoftKeyButton(const SymbianSoftKeyButtonData &button, QGraphicsItem *parent = 0);
    virtual ~SymbianSoftKeyButton();

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

    SymbianSoftKeyButtonData buttonData() const;

    void setText(const QString &text);
    QString text() const;

    void setTextSize(double size);

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

signals:
    void clicked(int buttonNr);

private:
    SymbianSoftKeyButtonData mButton;
    QString mText;
    double mTextSize;
    bool mMouseOver;
};

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

    DisplayWidget *display();

    Orientation deviceOrientation() const;
    Orientation screenOrientation() const;
    QList<Orientation> supportedScreenOrientations() const;

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

public slots:
    void changeDevice(const DeviceData &data);
    void changeOrientation(Orientation newOrientation, bool rotateScreen);
    void updateOrientation(Qt::WidgetAttribute orientation);
    void setInitialRotation(Orientation device, Orientation screen);
    void changeScaleFactor(qreal newScaleFactor);
    void setSymbianSoftKeyText(int buttonNumber, const QString &text);
    void setSymbianSoftKeyTextSize(double);
    void setMaemoNavigationMode(MaemoNavigationMode mode);

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

private:
    void newWindowSize();
    int rotationSideLength();
    void updateMenuPositionAndSize();
    bool needsAnimation(Orientation oldValue, Orientation newValue);
    void applyNewOrientation(Orientation orientation);

    Orientation mDeviceOrientation;
    Orientation mScreenOrientation;
    Qt::WidgetAttribute mFixedOrientation;

    DeviceData mDeviceData;

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

    QPropertyAnimation *mDefaultAnimation;
    QPoint mDragOffset;

    QList<KeyButton*> mButtons;
    PixmapButton *mMaemoNavigationButton;
    MaemoNavigationMode mCurrentMaemoNavigationMode;
    QList<SymbianSoftKeyButton *> mSymbianSoftKeyButtons;
    MenuPixmapItem *mMenu;

private slots:
    void initiateRotate();
    void newViewSize();
    void updateScreenOrientation();

signals:
    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 deviceChanged(bool mainOrientationPortrait);
    void offsetChanged(const QPoint &newOffset);

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

    void orientationChanged(Orientation to);
    void orientationLocked(bool locked);

    friend class InputScriptInterface;
};

#endif //DEVICEITEM_H