tests/benchmarks/uimodels/GraphicsViewBenchmark/widgets/topbar.cpp
changeset 3 41300fa6a67c
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     5 **
       
     6 ** This file is part of the examples of the Qt Toolkit.
       
     7 **
       
     8 ** $QT_BEGIN_LICENSE:LGPL$
       
     9 ** No Commercial Usage
       
    10 ** This file contains pre-release code and may not be distributed.
       
    11 ** You may use this file in accordance with the terms and conditions
       
    12 ** contained in the either Technology Preview License Agreement or the
       
    13 ** Beta Release License Agreement.
       
    14 **
       
    15 ** GNU Lesser General Public License Usage
       
    16 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    17 ** General Public License version 2.1 as published by the Free Software
       
    18 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    19 ** packaging of this file.  Please review the following information to
       
    20 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    22 **
       
    23 ** In addition, as a special exception, Nokia gives you certain
       
    24 ** additional rights. These rights are described in the Nokia Qt LGPL
       
    25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
       
    26 ** package.
       
    27 **
       
    28 ** GNU General Public License Usage
       
    29 ** Alternatively, this file may be used under the terms of the GNU
       
    30 ** General Public License version 3.0 as published by the Free Software
       
    31 ** Foundation and appearing in the file LICENSE.GPL included in the
       
    32 ** packaging of this file.  Please review the following information to
       
    33 ** ensure the GNU General Public License version 3.0 requirements will be
       
    34 ** met: http://www.gnu.org/copyleft/gpl.html.
       
    35 **
       
    36 ** If you are unsure which license is appropriate for your use, please
       
    37 ** contact the sales department at http://qt.nokia.com/contact.
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QGraphicsView>
       
    43 #include <QStyleOptionGraphicsItem>
       
    44 #include <QGraphicsSceneResizeEvent>
       
    45 #include <QPixmap>
       
    46 #include <QFont>
       
    47 
       
    48 #include "themeevent.h"
       
    49 #include "theme.h"
       
    50 #include "topbar.h"
       
    51 #include "mainview.h"
       
    52 
       
    53 TopBar::TopBar(QGraphicsView* mainView, QGraphicsWidget* parent) :
       
    54     GvbWidget(parent), m_mainView(mainView), m_isLimeTheme(false),
       
    55     m_orientation(TopBar::None), m_topBarPixmap(), m_sizesBlue(), m_sizesLime()
       
    56 {
       
    57     setDefaultSizes();
       
    58     
       
    59     m_titleFont = Theme::p()->font(Theme::TitleBar);
       
    60     m_statusFont = Theme::p()->font(Theme::StatusBar);
       
    61 
       
    62     connect(Theme::p(), SIGNAL(themeChanged()), this, SLOT(themeChange()));
       
    63 }
       
    64 
       
    65 TopBar::~TopBar()
       
    66 {
       
    67 }
       
    68 
       
    69 void TopBar::resizeEvent(QGraphicsSceneResizeEvent* /*event*/)
       
    70 {
       
    71     //Check orientation
       
    72     QString topbarName;
       
    73     QSize mainViewSize = m_mainView->size();
       
    74     int rotationAngle = static_cast<MainView*>(m_mainView)->rotationAngle(); 
       
    75     if(rotationAngle == 90 || rotationAngle == 270 ) {
       
    76        int wd = mainViewSize.width();
       
    77        int ht = mainViewSize.height();
       
    78        mainViewSize.setWidth(ht);
       
    79        mainViewSize.setHeight(wd);
       
    80     }
       
    81     bool m_orientationChanged = false;
       
    82     if(mainViewSize.height() >= mainViewSize.width()) {
       
    83         if(m_orientation == TopBar::Landscape)
       
    84             m_orientationChanged = true;
       
    85         m_orientation = TopBar::Portrait;
       
    86         topbarName = "topbar.svg";
       
    87     }
       
    88     else {
       
    89         if(m_orientation == TopBar::Portrait)
       
    90             m_orientationChanged = true;
       
    91         m_orientation = TopBar::Landscape;
       
    92         topbarName = "topbar_horisontal.svg";
       
    93     }
       
    94     
       
    95     //Calculate new size, resize by height, don't make it wider than the screen
       
    96     QHash<QString, QSize>sizes = (Theme::p()->theme() == Theme::Blue) ? 
       
    97         m_sizesBlue : m_sizesLime;
       
    98     
       
    99     //Get current size for topbarpixmap
       
   100     QSize currentSize = !m_topBarPixmap.isNull() && !m_orientationChanged ? 
       
   101             m_topBarPixmap.size() : sizes[topbarName];
       
   102     QSize newSize = !m_orientationChanged ? QSize(currentSize) : sizes[topbarName];
       
   103     
       
   104     //Scale according to aspect ratio
       
   105     newSize.scale(size().toSize(), Qt::KeepAspectRatio);
       
   106     
       
   107     //fix width to window widht if previous scaling produced too narrow image
       
   108     if(newSize.width() < size().width()) {
       
   109         newSize.scale(size().toSize(), Qt::KeepAspectRatioByExpanding);
       
   110     }
       
   111     
       
   112     //Calculate scaling factor for rest of the graphics scaling
       
   113     qreal scaleFactor = (newSize.width() *1.0) / (currentSize.width() * 1.0);
       
   114     
       
   115     //Scale graphics, if the scalefactor applies
       
   116     //This is really heavy since the SVG graphics are read again from the resource
       
   117     if(scaleFactor != 1 || m_topBarPixmap.isNull() )  {
       
   118         m_topBarPixmap = Theme::p()->pixmap(topbarName, newSize );
       
   119         m_topBarUserIcon = Theme::p()->pixmap("user_default_icon.svg", 
       
   120                 !m_topBarUserIcon.isNull() && !m_orientationChanged ? m_topBarUserIcon.size()* scaleFactor : sizes["user_default_icon.svg"] * scaleFactor);
       
   121         
       
   122         m_topBarUserStatus = Theme::p()->pixmap("user_status_online.svg", 
       
   123                 !m_topBarUserStatus.isNull() && !m_orientationChanged ? m_topBarUserStatus.size() * scaleFactor : sizes["user_status_online.svg"] * scaleFactor);
       
   124         
       
   125         m_topBarStatusBarLeft = Theme::p()->pixmap("status_field_left.svg", 
       
   126                 !m_topBarStatusBarLeft.isNull() && !m_orientationChanged ? m_topBarStatusBarLeft.size()* scaleFactor : sizes["status_field_left.svg"] * scaleFactor);
       
   127         
       
   128         m_topBarStatusBarRight = Theme::p()->pixmap("status_field_right.svg",
       
   129                 !m_topBarStatusBarRight.isNull() && !m_orientationChanged ? m_topBarStatusBarRight.size()* scaleFactor : sizes["status_field_right.svg"] * scaleFactor);
       
   130         
       
   131         m_topBarStatusBarMiddle = Theme::p()->pixmap("status_field_middle.svg",
       
   132                 !m_topBarStatusBarMiddle.isNull() && !m_orientationChanged ? m_topBarStatusBarMiddle.size() * scaleFactor : QSize(185, sizes["status_field_middle.svg"].height()) * scaleFactor);
       
   133     
       
   134         //Update the sizeHint to match the size of the scaled m_topBarPixmap
       
   135         updateGeometry();
       
   136         
       
   137         //Point Update - Positions relative to the Top Bar "Backgroud" size.
       
   138         //TODO: consider some layout instead of calculating relative locations
       
   139         QSize topBarPixmapSize = m_topBarPixmap.size();
       
   140         QSize topBarUserIconSize = m_topBarUserIcon.size();
       
   141         QSize topBarUserStatusSize = m_topBarUserStatus.size();
       
   142         QSize topBarStatusBarLeftSize = m_topBarStatusBarLeft.size();
       
   143         QSize topBarStatusBarMiddleSize = m_topBarStatusBarMiddle.size();
       
   144         
       
   145         //Location for Title text 5% width, 35% heigth of the background pixmap
       
   146         m_topBarTitlePoint = QPoint(topBarPixmapSize.width()* 0.05, 
       
   147                 topBarPixmapSize.height() * 0.35);
       
   148         
       
   149         //User Icon location
       
   150         //Placing 70% of the width and 10% of the height of the top bar background
       
   151         m_topBarUserIconPoint = QPoint((topBarPixmapSize.width() * 0.7), (topBarPixmapSize.height() * 0.1));
       
   152         
       
   153         //If Blue theme is in use - position user status icon on the right side of the user icon
       
   154         if(!m_isLimeTheme) {
       
   155             //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
       
   156             m_topBarUserStatusPoint = QPoint( ( (m_topBarUserIconPoint.x()+topBarUserIconSize.width() ) - 
       
   157                     ( topBarUserStatusSize.width()/2 )), 
       
   158                 (m_topBarUserIconPoint.y() + (topBarUserIconSize.height() * 0.35 )));
       
   159         }
       
   160         //If Lime theme is in use - position user status icon on the left side of the user icon
       
   161         else {
       
   162             //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
       
   163             //and move left by 5% of the icon
       
   164             m_topBarUserStatusPoint = QPoint(  m_topBarUserIconPoint.x() + ( topBarUserIconSize.width() * 0.05), 
       
   165                         (m_topBarUserIconPoint.y() + (topBarUserIconSize.height() * 0.5 )));
       
   166         }
       
   167         
       
   168         //Status bar
       
   169         //Placing the left side of the status bar  5% of the width, 50% of the height of the top bar background
       
   170         //Set the text baseline 80% of the height of the status bar
       
   171         m_topBarStatusBarLeftPoint = QPoint( (topBarPixmapSize.width()* 0.05), 
       
   172                                                         (topBarPixmapSize.height() * 0.5));
       
   173         m_topBarStatusBarMiddlePoint = QPoint( (m_topBarStatusBarLeftPoint.x() + topBarStatusBarLeftSize.width()), 
       
   174                                                             (m_topBarStatusBarLeftPoint.y()));
       
   175         m_topBarStatusBarRightPoint = QPoint( (m_topBarStatusBarMiddlePoint.x() + topBarStatusBarMiddleSize.width()), 
       
   176                                                         (m_topBarStatusBarMiddlePoint.y() ) );
       
   177         m_topBarStatusBarTextPoint = QPoint(m_topBarStatusBarMiddlePoint.x(), 
       
   178                                                         m_topBarStatusBarMiddlePoint.y() + (topBarStatusBarMiddleSize.height()*0.8) );
       
   179     } //if scalefactor
       
   180 }
       
   181 
       
   182 void TopBar::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget */*widget*/ )
       
   183 {
       
   184     //Topbar background
       
   185     painter->drawPixmap(option->exposedRect, m_topBarPixmap, option->exposedRect);
       
   186     
       
   187     //User Icon
       
   188     painter->drawPixmap(m_topBarUserIconPoint, m_topBarUserIcon);
       
   189     
       
   190     //User Status
       
   191     painter->drawPixmap(m_topBarUserStatusPoint, m_topBarUserStatus);
       
   192     
       
   193     //Status bar
       
   194     painter->drawPixmap(m_topBarStatusBarLeftPoint, m_topBarStatusBarLeft);
       
   195     painter->drawPixmap(m_topBarStatusBarMiddlePoint, m_topBarStatusBarMiddle);
       
   196     painter->drawPixmap(m_topBarStatusBarRightPoint, m_topBarStatusBarRight);
       
   197     
       
   198     //Title text
       
   199     painter->save();
       
   200     painter->setFont(m_titleFont);
       
   201     painter->setOpacity(0.7);
       
   202     painter->setPen(Qt::white);
       
   203     painter->drawText(m_topBarTitlePoint, QString("Contacts") );
       
   204     //Status text
       
   205     painter->setFont(m_statusFont);
       
   206     painter->setOpacity(1.0);
       
   207     painter->drawText(m_topBarStatusBarTextPoint, QString("My Status (fixed)") );
       
   208     painter->restore();
       
   209 }
       
   210 
       
   211 QRectF TopBar::boundingRect() const
       
   212 {
       
   213     //It's possible that m_topBarPixmap is not allocated yet, 
       
   214     //in this case default size is used for setting boundingRect
       
   215     QHash<QString, QSize>sizes = (Theme::p()->theme() == Theme::Blue) ? 
       
   216         m_sizesBlue : m_sizesLime; 
       
   217     
       
   218     if(!m_topBarPixmap.isNull()) 
       
   219         return QRectF(0, 0, m_topBarPixmap.size().width(), m_topBarPixmap.size().height());
       
   220     else 
       
   221         return QRectF(0, 0, sizes["topbar.svg"].width(), sizes["topbar.svg"].height());
       
   222 }
       
   223 
       
   224 void TopBar::themeChange()
       
   225 {
       
   226     m_titleFont = Theme::p()->font(Theme::TitleBar);
       
   227     m_statusFont = Theme::p()->font(Theme::StatusBar);
       
   228 
       
   229     //Calculate the scaling factor
       
   230     QHash<QString, QSize>sizes = (Theme::p()->theme() == Theme::Blue) ? 
       
   231         m_sizesBlue : m_sizesLime;
       
   232     
       
   233     QString topbarString= m_orientation == TopBar::Portrait ? 
       
   234             "topbar.svg" : "topbar_horisontal.svg";
       
   235     
       
   236     QSize topBarSize = sizes[topbarString];
       
   237     QSize newSize = QSize(topBarSize);
       
   238     
       
   239     //Scale according to aspect ratio
       
   240     newSize.scale(size().toSize(), Qt::KeepAspectRatio);
       
   241     
       
   242     //fix width to window widht if previous scaling produced too narrow image
       
   243     if(newSize.width() < size().width()) {
       
   244         newSize.scale(size().toSize(), Qt::KeepAspectRatioByExpanding);
       
   245     }
       
   246     
       
   247     //Calculate scaling factor for rest of the graphics scaling
       
   248     qreal scaleFactor = (newSize.width() *1.0) / (topBarSize.width() * 1.0);
       
   249 
       
   250     //Background
       
   251     m_topBarPixmap = Theme::p()->pixmap(topbarString, sizes[topbarString] * scaleFactor);
       
   252     
       
   253     //User Icon
       
   254     m_topBarUserIcon = Theme::p()->pixmap("user_default_icon.svg", sizes["user_default_icon.svg"] * scaleFactor);
       
   255     
       
   256     //User Status
       
   257     m_topBarUserStatus = Theme::p()->pixmap("user_status_online.svg", sizes["user_status_online.svg"] * scaleFactor); 
       
   258     
       
   259     //Status Bar 
       
   260     m_topBarStatusBarLeft = Theme::p()->pixmap("status_field_left.svg", sizes["status_field_left.svg"] * scaleFactor);
       
   261     m_topBarStatusBarRight = Theme::p()->pixmap("status_field_right.svg", sizes["status_field_right.svg"] * scaleFactor);
       
   262     m_topBarStatusBarMiddle = Theme::p()->pixmap("status_field_middle.svg", 
       
   263             QSize(185, sizes["status_field_middle.svg"].height())* scaleFactor);
       
   264      
       
   265     //Update Drawing points for Top Bar elements, points are relative to the top bar background size
       
   266     QSize topBarPixmapSize = m_topBarPixmap.size();
       
   267     QSize topBarUserIconSize = m_topBarUserIcon.size();
       
   268     QSize topBarUserStatusSize = m_topBarUserStatus.size();
       
   269     QSize topBarStatusBarLeftSize = m_topBarStatusBarLeft.size();
       
   270     QSize topBarStatusBarMiddleSize = m_topBarStatusBarMiddle.size(); 
       
   271     
       
   272     //Theme Check
       
   273     (Theme::p()->theme() == Theme::Lime) ? m_isLimeTheme = true : m_isLimeTheme = false;
       
   274     
       
   275     //User Icon location
       
   276     //Placing 70% of the width and 10% of the height of the top bar background
       
   277     m_topBarUserIconPoint = QPoint((0.7*topBarPixmapSize.width()), (0.1*topBarPixmapSize.height()));
       
   278     
       
   279     //If Blue theme is in use - position user status icon on the right side of the user icon
       
   280     if(!m_isLimeTheme) {
       
   281         //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
       
   282         m_topBarUserStatusPoint = QPoint( ( (m_topBarUserIconPoint.x()+topBarUserIconSize.width() ) - ( topBarUserStatusSize.width()/2 )), 
       
   283             (m_topBarUserIconPoint.y() + (topBarUserIconSize.height() * 0.35 )));
       
   284     }
       
   285     //If Lime theme is in use - position user status icon on the left side of the user icon
       
   286     else {
       
   287         //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
       
   288         //and move left by 5% of the icon
       
   289         m_topBarUserStatusPoint = QPoint(  m_topBarUserIconPoint.x() + ( topBarUserIconSize.width() * 0.05), 
       
   290                     (m_topBarUserIconPoint.y() + (topBarUserIconSize.height() * 0.5 )));
       
   291     }
       
   292     
       
   293     //Status bar
       
   294     //Placing the left side of the status bar  5% of the width, 50% of the height of the top bar background
       
   295     //Set the text baseline 80% of the height of the status bar
       
   296     m_topBarStatusBarLeftPoint = QPoint( (topBarPixmapSize.width()* 0.05), 
       
   297                                                     (topBarPixmapSize.height() * 0.5));
       
   298     m_topBarStatusBarMiddlePoint = QPoint( (m_topBarStatusBarLeftPoint.x() + topBarStatusBarLeftSize.width()), 
       
   299                                                         (m_topBarStatusBarLeftPoint.y()));
       
   300     m_topBarStatusBarRightPoint = QPoint( (m_topBarStatusBarMiddlePoint.x() + topBarStatusBarMiddleSize.width()), 
       
   301                                                     (m_topBarStatusBarMiddlePoint.y() ) );
       
   302     m_topBarStatusBarTextPoint = QPoint(m_topBarStatusBarMiddlePoint.x(), 
       
   303                                                     m_topBarStatusBarMiddlePoint.y() + (topBarStatusBarMiddleSize.height()*0.8) );
       
   304     
       
   305     update();
       
   306 }
       
   307 
       
   308 QSizeF TopBar::sizeHint(Qt::SizeHint which, 
       
   309         const QSizeF &constraint) const 
       
   310 {
       
   311     //It's possible that m_topBarPixmap is not allocated yet, 
       
   312     //in this case default size is used for setting size hint
       
   313     QHash<QString, QSize>sizes = (Theme::p()->theme() == Theme::Blue) ? 
       
   314         m_sizesBlue : m_sizesLime; 
       
   315    
       
   316     int height = !m_topBarPixmap.isNull() ? 
       
   317             m_topBarPixmap.height() : sizes["topbar.svg"].height(); 
       
   318     
       
   319     switch (which)
       
   320     {
       
   321     case Qt::MinimumSize:
       
   322         return QSizeF(-1, height);
       
   323 
       
   324     case Qt::MaximumSize:
       
   325         return QSizeF(-1, height);
       
   326 
       
   327     default:
       
   328         return QGraphicsWidget::sizeHint(which, constraint);
       
   329     }
       
   330 }
       
   331 
       
   332 void TopBar::setDefaultSizes()
       
   333 {    
       
   334     m_sizesBlue["topbar.svg"] = QSize(356,96);
       
   335     m_sizesBlue["topbar_horisontal.svg"] = QSize(636,96);
       
   336     m_sizesBlue["user_default_icon.svg"] = QSize(68,68);
       
   337     m_sizesBlue["user_status_online.svg"] = QSize(38,38);
       
   338     m_sizesBlue["status_field_left.svg"] = QSize(14,24);
       
   339     m_sizesBlue["status_field_right.svg"] = QSize(10,24);
       
   340     m_sizesBlue["status_field_middle.svg"] = QSize(14,24);
       
   341     
       
   342     m_sizesLime["topbar.svg"] = QSize(356,96);
       
   343     m_sizesLime["topbar_horisontal.svg"] = QSize(636,96);
       
   344     m_sizesLime["user_default_icon.svg"] = QSize(84,68);
       
   345     m_sizesLime["user_status_online.svg"] = QSize(24,24);
       
   346     m_sizesLime["status_field_left.svg"] = QSize(14,24);
       
   347     m_sizesLime["status_field_right.svg"] = QSize(10,24);
       
   348     m_sizesLime["status_field_middle.svg"] = QSize(14,24);
       
   349 }
       
   350 
       
   351 void TopBar::mousePressEvent(QGraphicsSceneMouseEvent *event)
       
   352 {
       
   353     QRect rect = m_topBarStatusBarMiddle.rect();
       
   354     rect.moveTopLeft(m_topBarStatusBarMiddlePoint);
       
   355     QPointF scenePoint = event->scenePos();
       
   356     if(rect.contains(scenePoint.toPoint())) {
       
   357         emit clicked();
       
   358     }
       
   359 }