tests/benchmarks/uimodels/GraphicsViewBenchmark/testmanual/controller.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 <QDebug>
       
    43 #include <QApplication>
       
    44 #include <QDir>
       
    45 #include <QPluginLoader>
       
    46 
       
    47 #include "controller.h"
       
    48 #include "itemrecyclinglist.h"
       
    49 #include "simplelist.h"
       
    50 #include "menu.h"
       
    51 #include "theme.h"
       
    52 #include "button.h"
       
    53 #include "filllists.h"
       
    54 
       
    55 static const int ScrollStep = 36;
       
    56 static const int ScrollDuration = 10000;
       
    57 
       
    58 Controller::Controller(MainView *mw, Settings &settings, QObject *parent)
       
    59     : QObject(parent),
       
    60     m_mainView(mw),
       
    61     m_currentListType(Recycling),
       
    62     m_createListMenuItem(0),
       
    63     m_subTreeMenuItem(0),
       
    64     m_dummyGen(),
       
    65     m_scrollDirection(1),
       
    66     m_testDuration(),
       
    67     m_settings(settings),
       
    68     m_resMon(0)
       
    69 {
       
    70     Menu *menu = m_mainView->menu();
       
    71     if (menu) {
       
    72         QString listText = "Create " + newListName(Simple);
       
    73         m_createListMenuItem = menu->addMenuItem(listText, this, SLOT(toggleListType()));
       
    74         menu->addMenuItem("Change Next Theme", this, SLOT(changeTheme()));
       
    75 #if (QT_VERSION >= 0x040600)
       
    76         m_subTreeMenuItem = menu->addMenuItem("Enable SubtreeCache", this, SLOT(toggleListItemCaching()));
       
    77 #endif
       
    78         menu->addMenuItem("Auto scroll", this, SLOT(startAutoScroll()));
       
    79         menu->addMenuItem("Rotate", this, SLOT(changeRotation()));
       
    80         menu->addMenuItem("One/Two columns", this, SLOT(toggleColumns()));
       
    81         menu->addMenuItem("Quit", this, SLOT(quit()) );
       
    82 
       
    83         if(loadPlugin())
       
    84             m_resMon->Prepare("graphicsviewbenchmark");
       
    85         
       
    86         toggleListType();
       
    87     }
       
    88 
       
    89 #if (QT_VERSION >= 0x040600)
       
    90     if (m_subTreeMenuItem &&
       
    91         settings.options().testFlag(Settings::UseListItemCache))
       
    92         m_subTreeMenuItem->setText("Disable SubtreeCache");
       
    93 #endif
       
    94 
       
    95     connect(this, SIGNAL(doAutoScroll()),
       
    96             this, SLOT(autoScroll()), Qt::QueuedConnection);
       
    97 }
       
    98 
       
    99 
       
   100 Controller::~Controller()
       
   101 {
       
   102     delete m_resMon;
       
   103 }
       
   104 
       
   105 #if (QT_VERSION >= 0x040600)
       
   106 void Controller::toggleListItemCaching()
       
   107 {
       
   108     if (!m_subTreeMenuItem || !m_mainView)
       
   109         return;
       
   110 
       
   111     bool enabled = false;
       
   112 
       
   113     if (m_currentListType == Simple) {
       
   114         SimpleList *list = static_cast<SimpleList *>(
       
   115                 m_mainView->testWidget());
       
   116         list->setListItemCaching(!list->listItemCaching());
       
   117         enabled = list->listItemCaching();
       
   118     } else {
       
   119         ItemRecyclingList *list = static_cast<ItemRecyclingList *>(
       
   120                 m_mainView->testWidget());
       
   121         list->setListItemCaching(!list->listItemCaching());
       
   122         enabled = list->listItemCaching();
       
   123     }
       
   124 
       
   125     if (enabled) 
       
   126         m_subTreeMenuItem->setText(tr("Disable list item caching"));
       
   127     else 
       
   128         m_subTreeMenuItem->setText(tr("Enabled list item caching"));
       
   129 }
       
   130 #endif
       
   131 
       
   132 void Controller::toggleListType()
       
   133 {
       
   134     if (!m_mainView)
       
   135         return;
       
   136 
       
   137     if (m_currentListType == Simple) {
       
   138 #if (QT_VERSION >= 0x040600)
       
   139         bool caching = false;
       
   140 #endif
       
   141         SimpleList *oldList = static_cast<SimpleList *>(
       
   142                 m_mainView->takeTestWidget());
       
   143         if (oldList) {
       
   144 #if (QT_VERSION >= 0x040600)
       
   145             caching = oldList->listItemCaching();
       
   146 #endif
       
   147             delete oldList, oldList = 0;
       
   148         }
       
   149         
       
   150         //CPU/MEM Begin
       
   151         if(m_resMon && !m_settings.options().testFlag(Settings::NoResourceUsage)) {
       
   152             m_resMon->BeginMeasureCPULoad();
       
   153             m_resMon->BeginMeasureMemoryLoad();
       
   154         }
       
   155         
       
   156         ItemRecyclingList *list = new ItemRecyclingList();
       
   157         m_dummyGen.Reset();
       
   158         fillRecyclingList(m_dummyGen, m_settings.listItemCount(), list);
       
   159         
       
   160 #if (QT_VERSION >= 0x040600)
       
   161         list->setListItemCaching(caching);
       
   162 #endif
       
   163         m_currentListType = Recycling;
       
   164         m_mainView->setTestWidget(list);
       
   165         
       
   166         if(m_resMon && !m_settings.options().testFlag(Settings::NoResourceUsage)) {
       
   167             //CPU/MEM End
       
   168             ResourceMonitorInterface::CpuUsage cpuUsage = m_resMon->EndMeasureCPULoad();
       
   169             ResourceMonitorInterface::MemoryAllocation memory = m_resMon->EndMeasureMemoryLoad();
       
   170             QString cpuLoad = QString("List Construction CPU usage: %1% application thread CPU use, %2% total CPU use ")
       
   171             .arg(cpuUsage.appTreadUsage)
       
   172             .arg(cpuUsage.systemUsage);
       
   173             qDebug() << cpuLoad;
       
   174             QString memoryLoad = QString("List Construction Memory Usage: %1 allocated in cells in app heap,"
       
   175                     "%2 cells in use in app heap, %3 available in app heap, %4 RAM available in system, %5 RAM in System")
       
   176                     .arg(memory.allocatedInAppThread)
       
   177                     .arg(memory.numberOfAllocatedCellsInAppThread)
       
   178                     .arg(memory.availableMemoryInAppThreadHeap)
       
   179                     .arg(memory.availableMemoryInSystem)
       
   180                     .arg(memory.totalMemoryInSystem);
       
   181             qDebug() << memoryLoad;
       
   182         }
       
   183     } else {
       
   184 #if (QT_VERSION >= 0x040600)
       
   185         bool caching = false;
       
   186 #endif
       
   187         ItemRecyclingList *oldList = static_cast<ItemRecyclingList *>(
       
   188                 m_mainView->takeTestWidget());
       
   189         if (oldList) {
       
   190 #if (QT_VERSION >= 0x040600)
       
   191             caching = oldList->listItemCaching();
       
   192 #endif
       
   193             delete oldList, oldList = 0;
       
   194         }
       
   195         
       
   196         if(m_resMon && !m_settings.options().testFlag(Settings::NoResourceUsage)) {
       
   197             //CPU/MEM Begin
       
   198             m_resMon->BeginMeasureCPULoad();
       
   199             m_resMon->BeginMeasureMemoryLoad();
       
   200         }
       
   201         
       
   202         SimpleList *list = new SimpleList();
       
   203         m_dummyGen.Reset();
       
   204         fillSimpleList(m_dummyGen, m_settings.listItemCount(), list);
       
   205 #if (QT_VERSION >= 0x040600)
       
   206         list->setListItemCaching(caching);
       
   207 #endif
       
   208         m_currentListType = Simple;
       
   209         m_mainView->setTestWidget(list);
       
   210         
       
   211         if(m_resMon && !m_settings.options().testFlag(Settings::NoResourceUsage)) {
       
   212             //CPU/MEM End
       
   213             ResourceMonitorInterface::CpuUsage cpuUsage = m_resMon->EndMeasureCPULoad();
       
   214             ResourceMonitorInterface::MemoryAllocation memory = m_resMon->EndMeasureMemoryLoad();
       
   215             //For logging
       
   216             QString cpuLoad = QString("List Construction CPU usage: %1% application thread CPU use, %2% total CPU use ")
       
   217             .arg(cpuUsage.appTreadUsage)
       
   218             .arg(cpuUsage.systemUsage);
       
   219             qDebug()<<cpuLoad;
       
   220             QString memoryLoad = QString("List Construction Memory Usage: %1 allocated in cells in app heap,"
       
   221                     "%2 cells in use in app heap, %3 available in app heap, %4 RAM available in system, %5 RAM in System")
       
   222                     .arg(memory.allocatedInAppThread)
       
   223                     .arg(memory.numberOfAllocatedCellsInAppThread)
       
   224                     .arg(memory.availableMemoryInAppThreadHeap)
       
   225                     .arg(memory.availableMemoryInSystem)
       
   226                     .arg(memory.totalMemoryInSystem);
       
   227             qDebug()<<memoryLoad;
       
   228         }
       
   229     }
       
   230 
       
   231     if (m_createListMenuItem) {
       
   232         QString listText = "Create " + newListName(m_currentListType);
       
   233         m_createListMenuItem->setText(listText);
       
   234     }
       
   235 }
       
   236 
       
   237 QString Controller::newListName(ListType currentType)
       
   238 {
       
   239     if(currentType==Simple)
       
   240         return QString("ItemRecyclingList");
       
   241 
       
   242     return QString("SimpleList");
       
   243 }
       
   244 
       
   245 void Controller::startAutoScroll()
       
   246 {
       
   247     if(m_resMon && !m_settings.options().testFlag(Settings::NoResourceUsage)) {
       
   248         //CPU/MEM Begin
       
   249         m_resMon->BeginMeasureCPULoad();
       
   250         m_resMon->BeginMeasureMemoryLoad();
       
   251     }
       
   252     m_testDuration.start();
       
   253     emit doAutoScroll();
       
   254 }
       
   255 
       
   256 void Controller::autoScroll()
       
   257 {
       
   258     ScrollBar *sb = 0;
       
   259     QGraphicsWidget *w = m_mainView->testWidget();
       
   260 
       
   261     ItemRecyclingList *rl = dynamic_cast<ItemRecyclingList *>(w);
       
   262     if (rl)
       
   263         sb = rl->verticalScrollBar();
       
   264 
       
   265     SimpleList *sl = dynamic_cast<SimpleList *>(w);
       
   266     if (sl)
       
   267         sb = sl->verticalScrollBar();
       
   268 
       
   269     if (sb) {
       
   270         qreal currentVal = sb->sliderPosition();
       
   271 
       
   272         if (currentVal+m_scrollDirection*ScrollStep > sb->sliderSize() ||
       
   273             currentVal+m_scrollDirection*ScrollStep < 0.0)
       
   274             m_scrollDirection = -1*m_scrollDirection;
       
   275 
       
   276         sb->setSliderPosition(currentVal+m_scrollDirection*ScrollStep);
       
   277     }
       
   278 
       
   279     if (m_testDuration.elapsed() < ScrollDuration)
       
   280         emit doAutoScroll();
       
   281     else{
       
   282         if(m_resMon && !m_settings.options().testFlag(Settings::NoResourceUsage)) {
       
   283             //CPU/MEM End
       
   284             ResourceMonitorInterface::CpuUsage cpuUsage = m_resMon->EndMeasureCPULoad();
       
   285             ResourceMonitorInterface::MemoryAllocation memory = m_resMon->EndMeasureMemoryLoad();
       
   286             //For logging
       
   287             QString cpuLoad = QString("List Scroll CPU usage: %1% application thread CPU use, %2% total CPU use ")
       
   288             .arg(cpuUsage.appTreadUsage)
       
   289             .arg(cpuUsage.systemUsage);
       
   290             qDebug()<<cpuLoad;
       
   291             QString memoryLoad = QString("List Scroll Memory Usage: %1 allocated in cells in app heap,"
       
   292                     "%2 cells in use in app heap, %3 available in app heap, %4 RAM available in system, %5 RAM in System")
       
   293                     .arg(memory.allocatedInAppThread)
       
   294                     .arg(memory.numberOfAllocatedCellsInAppThread)
       
   295                     .arg(memory.availableMemoryInAppThreadHeap)
       
   296                     .arg(memory.availableMemoryInSystem)
       
   297                     .arg(memory.totalMemoryInSystem);
       
   298             qDebug()<<memoryLoad;
       
   299         }
       
   300     }
       
   301 }
       
   302 
       
   303 void Controller::quit()
       
   304 {
       
   305     QApplication::quit();
       
   306 }
       
   307 
       
   308 void Controller::changeTheme()
       
   309 {
       
   310     if(m_resMon && !m_settings.options().testFlag(Settings::NoResourceUsage)) {
       
   311         //CPU/MEM Begin
       
   312         m_resMon->BeginMeasureCPULoad();
       
   313         m_resMon->BeginMeasureMemoryLoad();
       
   314     }
       
   315 
       
   316     Theme::p()->theme() == Theme::Blue ?  Theme::p()->setTheme(Theme::Lime) : Theme::p()->setTheme(Theme::Blue);
       
   317     
       
   318     if(m_resMon && !m_settings.options().testFlag(Settings::NoResourceUsage)) {
       
   319         //CPU/MEM End
       
   320         ResourceMonitorInterface::CpuUsage cpuUsage = m_resMon->EndMeasureCPULoad();
       
   321         ResourceMonitorInterface::MemoryAllocation memory = m_resMon->EndMeasureMemoryLoad();
       
   322         
       
   323         //For logging
       
   324         QString cpuLoad = QString("Theme Change CPU usage: %1% application thread CPU use, %2% total CPU use ")
       
   325         .arg(cpuUsage.appTreadUsage)
       
   326         .arg(cpuUsage.systemUsage);
       
   327         qDebug()<<cpuLoad;
       
   328         QString memoryLoad = QString("Theme Change Memory Usage: %1 allocated in cells in app heap,"
       
   329                 "%2 cells in use in app heap, %3 available in app heap, %4 RAM available in system, %5 RAM in System")
       
   330                 .arg(memory.allocatedInAppThread)
       
   331                 .arg(memory.numberOfAllocatedCellsInAppThread)
       
   332                 .arg(memory.availableMemoryInAppThreadHeap)
       
   333                 .arg(memory.availableMemoryInSystem)
       
   334                 .arg(memory.totalMemoryInSystem);
       
   335         qDebug()<<memoryLoad;
       
   336     }
       
   337 }
       
   338 
       
   339 void Controller::changeRotation()
       
   340 {  
       
   341     if (m_mainView){
       
   342         if(m_resMon && !m_settings.options().testFlag(Settings::NoResourceUsage)) {
       
   343             //CPU/MEM Begin
       
   344             m_resMon->BeginMeasureCPULoad();
       
   345             m_resMon->BeginMeasureMemoryLoad();
       
   346         }
       
   347         
       
   348         m_mainView->rotateContent(90);
       
   349        
       
   350         if(m_resMon && !m_settings.options().testFlag(Settings::NoResourceUsage)) {
       
   351             //CPU/MEM End
       
   352             ResourceMonitorInterface::CpuUsage cpuUsage = m_resMon->EndMeasureCPULoad();
       
   353             ResourceMonitorInterface::MemoryAllocation memory = m_resMon->EndMeasureMemoryLoad();
       
   354             
       
   355             //For logging
       
   356             QString cpuLoad = QString("Rotate CPU usage: %1% application thread CPU use, %2% total CPU use ")
       
   357                 .arg(cpuUsage.appTreadUsage)
       
   358                 .arg(cpuUsage.systemUsage);
       
   359             qDebug()<<cpuLoad;
       
   360             QString memoryLoad = QString("Rotate Memory Usage: %1 allocated in cells in app heap,"
       
   361                 "%2 cells in use in app heap, %3 available in app heap, %4 RAM available in system, %5 RAM in System")
       
   362                 .arg(memory.allocatedInAppThread)
       
   363                 .arg(memory.numberOfAllocatedCellsInAppThread)
       
   364                 .arg(memory.availableMemoryInAppThreadHeap)
       
   365                 .arg(memory.availableMemoryInSystem)
       
   366                 .arg(memory.totalMemoryInSystem);
       
   367             qDebug()<<memoryLoad;
       
   368         }
       
   369     }
       
   370 }
       
   371 
       
   372 bool Controller::loadPlugin()
       
   373 {
       
   374     QDir pluginsDir(qApp->applicationDirPath());
       
   375     foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
       
   376         QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
       
   377         QObject *plugin = pluginLoader.instance();
       
   378         if (plugin) {
       
   379             m_resMon = qobject_cast<ResourceMonitorInterface *>(plugin);
       
   380             if (m_resMon) 
       
   381                 return true;
       
   382         }
       
   383     }
       
   384     return false;
       
   385 }
       
   386 
       
   387 void Controller::toggleColumns()
       
   388 {
       
   389     if (!m_subTreeMenuItem || !m_mainView)
       
   390         return;
       
   391 
       
   392     if (m_currentListType == Simple) {
       
   393         SimpleList *list = static_cast<SimpleList *>(
       
   394                 m_mainView->testWidget());
       
   395         list->setTwoColumns(!list->twoColumns());
       
   396     } else {
       
   397         ItemRecyclingList *list = static_cast<ItemRecyclingList *>(
       
   398                 m_mainView->testWidget());
       
   399         list->setTwoColumns(!list->twoColumns());
       
   400     }
       
   401 }