summaryrefslogtreecommitdiffstats
path: root/tests/benchmarks/gui/graphicsview/functional/GraphicsViewBenchmark/widgets/topbar.cpp
blob: 09ff5d139492c280e805e25be6e1a562fc92a723 (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QGraphicsView>
#include <QStyleOptionGraphicsItem>
#include <QGraphicsSceneResizeEvent>
#include <QPixmap>
#include <QFont>

#include "themeevent.h"
#include "theme.h"
#include "topbar.h"
#include "mainview.h"

TopBar::TopBar(QGraphicsView* mainView, QGraphicsWidget* parent) :
    GvbWidget(parent), m_mainView(mainView), m_isLimeTheme(false),
    m_orientation(TopBar::None), m_topBarPixmap(), m_sizesBlue(), m_sizesLime()
{
    setDefaultSizes();
    
    m_titleFont = Theme::p()->font(Theme::TitleBar);
    m_statusFont = Theme::p()->font(Theme::StatusBar);

    connect(Theme::p(), SIGNAL(themeChanged()), this, SLOT(themeChange()));
}

TopBar::~TopBar()
{
}

void TopBar::resizeEvent(QGraphicsSceneResizeEvent* /*event*/)
{
    //Check orientation
    QString topbarName;
    QSize mainViewSize = m_mainView->size();
    int rotationAngle = static_cast<MainView*>(m_mainView)->rotationAngle(); 
    if(rotationAngle == 90 || rotationAngle == 270 ) {
       int wd = mainViewSize.width();
       int ht = mainViewSize.height();
       mainViewSize.setWidth(ht);
       mainViewSize.setHeight(wd);
    }
    bool m_orientationChanged = false;
    if(mainViewSize.height() >= mainViewSize.width()) {
        if(m_orientation == TopBar::Landscape)
            m_orientationChanged = true;
        m_orientation = TopBar::Portrait;
        topbarName = "topbar.svg";
    }
    else {
        if(m_orientation == TopBar::Portrait)
            m_orientationChanged = true;
        m_orientation = TopBar::Landscape;
        topbarName = "topbar_horisontal.svg";
    }
    
    //Calculate new size, resize by height, don't make it wider than the screen
    QHash<QString, QSize>sizes = (Theme::p()->theme() == Theme::Blue) ? 
        m_sizesBlue : m_sizesLime;
    
    //Get current size for topbarpixmap
    QSize currentSize = !m_topBarPixmap.isNull() && !m_orientationChanged ? 
            m_topBarPixmap.size() : sizes[topbarName];
    QSize newSize = !m_orientationChanged ? QSize(currentSize) : sizes[topbarName];
    
    //Scale according to aspect ratio
    newSize.scale(size().toSize(), Qt::KeepAspectRatio);
    
    //fix width to window widht if previous scaling produced too narrow image
    if(newSize.width() < size().width()) {
        newSize.scale(size().toSize(), Qt::KeepAspectRatioByExpanding);
    }
    
    //Calculate scaling factor for rest of the graphics scaling
    qreal scaleFactor = (newSize.width() *1.0) / (currentSize.width() * 1.0);
    
    //Scale graphics, if the scalefactor applies
    //This is really heavy since the SVG graphics are read again from the resource
    if(scaleFactor != 1 || m_topBarPixmap.isNull() )  {
        m_topBarPixmap = Theme::p()->pixmap(topbarName, newSize );
        m_topBarUserIcon = Theme::p()->pixmap("user_default_icon.svg", 
                !m_topBarUserIcon.isNull() && !m_orientationChanged ? m_topBarUserIcon.size()* scaleFactor : sizes["user_default_icon.svg"] * scaleFactor);
        
        m_topBarUserStatus = Theme::p()->pixmap("user_status_online.svg", 
                !m_topBarUserStatus.isNull() && !m_orientationChanged ? m_topBarUserStatus.size() * scaleFactor : sizes["user_status_online.svg"] * scaleFactor);
        
        m_topBarStatusBarLeft = Theme::p()->pixmap("status_field_left.svg", 
                !m_topBarStatusBarLeft.isNull() && !m_orientationChanged ? m_topBarStatusBarLeft.size()* scaleFactor : sizes["status_field_left.svg"] * scaleFactor);
        
        m_topBarStatusBarRight = Theme::p()->pixmap("status_field_right.svg",
                !m_topBarStatusBarRight.isNull() && !m_orientationChanged ? m_topBarStatusBarRight.size()* scaleFactor : sizes["status_field_right.svg"] * scaleFactor);
        
        m_topBarStatusBarMiddle = Theme::p()->pixmap("status_field_middle.svg",
                !m_topBarStatusBarMiddle.isNull() && !m_orientationChanged ? m_topBarStatusBarMiddle.size() * scaleFactor : QSize(185, sizes["status_field_middle.svg"].height()) * scaleFactor);
    
        //Update the sizeHint to match the size of the scaled m_topBarPixmap
        updateGeometry();
        
        //Point Update - Positions relative to the Top Bar "Backgroud" size.
        //TODO: consider some layout instead of calculating relative locations
        QSize topBarPixmapSize = m_topBarPixmap.size();
        QSize topBarUserIconSize = m_topBarUserIcon.size();
        QSize topBarUserStatusSize = m_topBarUserStatus.size();
        QSize topBarStatusBarLeftSize = m_topBarStatusBarLeft.size();
        QSize topBarStatusBarMiddleSize = m_topBarStatusBarMiddle.size();
        
        //Location for Title text 5% width, 35% heigth of the background pixmap
        m_topBarTitlePoint = QPoint(topBarPixmapSize.width()* 0.05, 
                topBarPixmapSize.height() * 0.35);
        
        //User Icon location
        //Placing 70% of the width and 10% of the height of the top bar background
        m_topBarUserIconPoint = QPoint((topBarPixmapSize.width() * 0.7), (topBarPixmapSize.height() * 0.1));
        
        //If Blue theme is in use - position user status icon on the right side of the user icon
        if(!m_isLimeTheme) {
            //Place the status icon on top of the right edge of the user icon, lower it by 35% of the height of the user icon
            m_topBarUserStatusPoint = QPoint( ( (m_topBarUserIconPoint.x()+topBarUserIconSize.width() ) - 
                    ( topBarUserStatusSize.width()/2 )), 
                (m_topBarUserIconPoint.y() + (topBarUserIconSize.height() * 0.35 )));
        }
        //If Lime theme is in use - position user status icon on the left side of the user icon
        else {
            //Place the status icon on top of the left side of the user icon, lower it by 50% of the height of the user icon
            //and move left by 5% of the icon
            m_topBarUserStatusPoint = QPoint(  m_topBarUserIconPoint.x() + ( topBarUserIconSize.width() * 0.05), 
                        (m_topBarUserIconPoint.y() + (topBarUserIconSize.height() * 0.5 )));
        }
        
        //Status bar
        //Placing the left side of the status bar  5% of the width, 50% of the height of the top bar background
        //Set the text baseline 80% of the height of the status bar
        m_topBarStatusBarLeftPoint = QPoint( (topBarPixmapSize.width()* 0.05), 
                                                        (topBarPixmapSize.height() * 0.5));
        m_topBarStatusBarMiddlePoint = QPoint( (m_topBarStatusBarLeftPoint.x() + topBarStatusBarLeftSize.width()), 
                                                            (m_topBarStatusBarLeftPoint.y()));
        m_topBarStatusBarRightPoint = QPoint( (m_topBarStatusBarMiddlePoint.x() + topBarStatusBarMiddleSize.width()), 
                                                        (m_topBarStatusBarMiddlePoint.y() ) );
        m_topBarStatusBarTextPoint = QPoint(m_topBarStatusBarMiddlePoint.x(), 
                                                        m_topBarStatusBarMiddlePoint.y() + (topBarStatusBarMiddleSize.height()*0.8) );
    } //if scalefactor
}

void TopBar::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget */*widget*/ )
{
    //Topbar background
    painter->drawPixmap(option->exposedRect, m_topBarPixmap, option->exposedRect);
    
    //User Icon
    painter->drawPixmap(m_topBarUserIconPoint, m_topBarUserIcon);
    
    //User Status
    painter->drawPixmap(m_topBarUserStatusPoint, m_topBarUserStatus);
    
    //Status bar
    painter->drawPixmap(m_topBarStatusBarLeftPoint, m_topBarStatusBarLeft);
    painter->drawPixmap(m_topBarStatusBarMiddlePoint, m_topBarStatusBarMiddle);
    painter->drawPixmap(m_topBarStatusBarRightPoint, m_topBarStatusBarRight);
    
    //Title text
    painter->save();
    painter->setFont(m_titleFont);
    painter->setOpacity(0.7);
    painter->setPen(Qt::white);
    painter->drawText(m_topBarTitlePoint, QString("Contacts") );
    //Status text
    painter->setFont(m_statusFont);
    painter->setOpacity(1.0);
    painter->drawText(m_topBarStatusBarTextPoint, QString("My Status (fixed)") );
    painter->restore();
}

QRectF TopBar::boundingRect() const
{
    //It's possible that m_topBarPixmap is not allocated yet, 
    //in this case default size is used for setting boundingRect
    QHash<QString, QSize>sizes = (Theme::p()->theme() == Theme::Blue) ? 
        m_sizesBlue : m_sizesLime; 
    
    if(!m_topBarPixmap.isNull()) 
        return QRectF(0, 0, m_topBarPixmap.size().width(), m_topBarPixmap.size().height());
    else 
        return QRectF(0, 0, sizes["topbar.svg"].width(), sizes["topbar.svg"].height());
}

void TopBar::themeChange()
{
    m_titleFont = Theme::p()->font(Theme::TitleBar);
    m_statusFont = Theme::p()->font(Theme::StatusBar);

    //Calculate the scaling factor
    QHash<QString, QSize>sizes = (Theme::p()->theme() == Theme::Blue) ? 
        m_sizesBlue : m_sizesLime;
    
    QString topbarString= m_orientation == TopBar::Portrait ? 
            "topbar.svg" : "topbar_horisontal.svg";
    
    QSize topBarSize = sizes[topbarString];
    QSize newSize = QSize(topBarSize);
    
    //Scale according to aspect ratio
    newSize.scale(size().toSize(), Qt::KeepAspectRatio);
    
    //fix width to window widht if previous scaling produced too narrow image
    if(newSize.width() < size().width()) {
        newSize.scale(size().toSize(), Qt::KeepAspectRatioByExpanding);
    }
    
    //Calculate scaling factor for rest of the graphics scaling
    qreal scaleFactor = (newSize.width() *1.0) / (topBarSize.width() * 1.0);

    //Background
    m_topBarPixmap = Theme::p()->pixmap(topbarString, sizes[topbarString] * scaleFactor);
    
    //User Icon
    m_topBarUserIcon = Theme::p()->pixmap("user_default_icon.svg", sizes["user_default_icon.svg"] * scaleFactor);
    
    //User Status
    m_topBarUserStatus = Theme::p()->pixmap("user_status_online.svg", sizes["user_status_online.svg"] * scaleFactor); 
    
    //Status Bar 
    m_topBarStatusBarLeft = Theme::p()->pixmap("status_field_left.svg", sizes["status_field_left.svg"] * scaleFactor);
    m_topBarStatusBarRight = Theme::p()->pixmap("status_field_right.svg", sizes["status_field_right.svg"] * scaleFactor);
    m_topBarStatusBarMiddle = Theme::p()->pixmap("status_field_middle.svg", 
            QSize(185, sizes["status_field_middle.svg"].height())* scaleFactor);
     
    //Update Drawing points for Top Bar elements, points are relative to the top bar background size
    QSize topBarPixmapSize = m_topBarPixmap.size();
    QSize topBarUserIconSize = m_topBarUserIcon.size();
    QSize topBarUserStatusSize = m_topBarUserStatus.size();
    QSize topBarStatusBarLeftSize = m_topBarStatusBarLeft.size();
    QSize topBarStatusBarMiddleSize = m_topBarStatusBarMiddle.size(); 
    
    //Theme Check
    (Theme::p()->theme() == Theme::Lime) ? m_isLimeTheme = true : m_isLimeTheme = false;
    
    //User Icon location
    //Placing 70% of the width and 10% of the height of the top bar background
    m_topBarUserIconPoint = QPoint((0.7*topBarPixmapSize.width()), (0.1*topBarPixmapSize.height()));
    
    //If Blue theme is in use - position user status icon on the right side of the user icon
    if(!m_isLimeTheme) {
        //Place the status icon on top of the right edge of the user icon, lower it by 35% of the height of the user icon
        m_topBarUserStatusPoint = QPoint( ( (m_topBarUserIconPoint.x()+topBarUserIconSize.width() ) - ( topBarUserStatusSize.width()/2 )), 
            (m_topBarUserIconPoint.y() + (topBarUserIconSize.height() * 0.35 )));
    }
    //If Lime theme is in use - position user status icon on the left side of the user icon
    else {
        //Place the status icon on top of the left side of the user icon, lower it by 50% of the height of the user icon
        //and move left by 5% of the icon
        m_topBarUserStatusPoint = QPoint(  m_topBarUserIconPoint.x() + ( topBarUserIconSize.width() * 0.05), 
                    (m_topBarUserIconPoint.y() + (topBarUserIconSize.height() * 0.5 )));
    }
    
    //Status bar
    //Placing the left side of the status bar  5% of the width, 50% of the height of the top bar background
    //Set the text baseline 80% of the height of the status bar
    m_topBarStatusBarLeftPoint = QPoint( (topBarPixmapSize.width()* 0.05), 
                                                    (topBarPixmapSize.height() * 0.5));
    m_topBarStatusBarMiddlePoint = QPoint( (m_topBarStatusBarLeftPoint.x() + topBarStatusBarLeftSize.width()), 
                                                        (m_topBarStatusBarLeftPoint.y()));
    m_topBarStatusBarRightPoint = QPoint( (m_topBarStatusBarMiddlePoint.x() + topBarStatusBarMiddleSize.width()), 
                                                    (m_topBarStatusBarMiddlePoint.y() ) );
    m_topBarStatusBarTextPoint = QPoint(m_topBarStatusBarMiddlePoint.x(), 
                                                    m_topBarStatusBarMiddlePoint.y() + (topBarStatusBarMiddleSize.height()*0.8) );
    
    update();
}

QSizeF TopBar::sizeHint(Qt::SizeHint which, 
        const QSizeF &constraint) const 
{
    //It's possible that m_topBarPixmap is not allocated yet, 
    //in this case default size is used for setting size hint
    QHash<QString, QSize>sizes = (Theme::p()->theme() == Theme::Blue) ? 
        m_sizesBlue : m_sizesLime; 
   
    int height = !m_topBarPixmap.isNull() ? 
            m_topBarPixmap.height() : sizes["topbar.svg"].height(); 
    
    switch (which)
    {
    case Qt::MinimumSize:
        return QSizeF(-1, height);

    case Qt::MaximumSize:
        return QSizeF(-1, height);

    default:
        return QGraphicsWidget::sizeHint(which, constraint);
    }
}

void TopBar::setDefaultSizes()
{    
    m_sizesBlue["topbar.svg"] = QSize(356,96);
    m_sizesBlue["topbar_horisontal.svg"] = QSize(636,96);
    m_sizesBlue["user_default_icon.svg"] = QSize(68,68);
    m_sizesBlue["user_status_online.svg"] = QSize(38,38);
    m_sizesBlue["status_field_left.svg"] = QSize(14,24);
    m_sizesBlue["status_field_right.svg"] = QSize(10,24);
    m_sizesBlue["status_field_middle.svg"] = QSize(14,24);
    
    m_sizesLime["topbar.svg"] = QSize(356,96);
    m_sizesLime["topbar_horisontal.svg"] = QSize(636,96);
    m_sizesLime["user_default_icon.svg"] = QSize(84,68);
    m_sizesLime["user_status_online.svg"] = QSize(24,24);
    m_sizesLime["status_field_left.svg"] = QSize(14,24);
    m_sizesLime["status_field_right.svg"] = QSize(10,24);
    m_sizesLime["status_field_middle.svg"] = QSize(14,24);
}

void TopBar::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QRect rect = m_topBarStatusBarMiddle.rect();
    rect.moveTopLeft(m_topBarStatusBarMiddlePoint);
    QPointF scenePoint = event->scenePos();
    if(rect.contains(scenePoint.toPoint())) {
        emit clicked();
    }
}