File indexing completed on 2024-04-14 07:37:13

0001 /*
0002     This file is part of the KDE games kwin4 program
0003     SPDX-FileCopyrightText: 2006 Martin Heni <kde@heni-online.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "reflectiongraphicsscene.h"
0009 
0010 // own
0011 #include "kfourinline_debug.h"
0012 #include "kwin4global.h"
0013 // Qt
0014 #include <QElapsedTimer>
0015 #include <QGraphicsItem>
0016 #include <QPainter>
0017 #include <QRectF>
0018 
0019 // How many time measurements for average
0020 #define MEASUREMENT_LIST_SIZE 50
0021 // How many warnings until reflections are switched off
0022 #define WARNING_MAX_COUNT 10
0023 
0024 // Construct a new scene
0025 ReflectionGraphicsScene::ReflectionGraphicsScene(int updateTime, QObject *parent)
0026     : QGraphicsScene(parent)
0027 {
0028     Q_UNUSED(updateTime)
0029     // Initialize
0030     mBackground = true;
0031 }
0032 
0033 // Destruct scene
0034 ReflectionGraphicsScene::~ReflectionGraphicsScene()
0035 {
0036 }
0037 
0038 // QGV basic function to draw all items of a scene
0039 void ReflectionGraphicsScene::drawItems(QPainter *painter, int numItems, QGraphicsItem *items[], const QStyleOptionGraphicsItem options[], QWidget *widget)
0040 {
0041     QElapsedTimer time;
0042     time.start();
0043 
0044     // No reflections call parent function
0045     QGraphicsScene::drawItems(painter, numItems, items, options, widget);
0046 
0047     /*
0048     // ==========================================================================
0049     // Update time measurement and display
0050     int elapsed = time.elapsed();
0051     mDrawTimes.append(elapsed);
0052     if (mDrawTimes.size() > MEASUREMENT_LIST_SIZE) mDrawTimes.removeFirst();
0053     double avg = 0.0;
0054     for (int i=0; i<mDrawTimes.size(); i++) avg += mDrawTimes[i];
0055     avg /= mDrawTimes.size();
0056 
0057 
0058     if (global_debug > 0)
0059        mFrameSprite->setPlainText(QString("Draw: %1 ms  Average %2 ms  Update: %3 ms").arg(elapsed).arg(int(avg)).arg(mDisplayUpdateTime));
0060 
0061      // Disable reflections on slow computers
0062      if (mDrawTimes.size() >= MEASUREMENT_LIST_SIZE )
0063      {
0064        if (avg > 2*mUpdateTime )
0065        {
0066          mUpdateWarning++;
0067          mDrawTimes.clear();
0068          qCDebug(KFOURINLINE_LOG) << mUpdateWarning << ". slow computer reflection theme warning";
0069        }
0070        else
0071        {
0072          mUpdateWarning = 0;
0073        }
0074      }
0075      if (mUpdateWarning >= WARNING_MAX_COUNT )
0076      {
0077        update();
0078        qCDebug(KFOURINLINE_LOG) << "DISABLING REFLECTIONS DUE TO POOR COMPUTER PERFORMANCE";
0079      }
0080     // ==========================================================================
0081     */
0082 }
0083 
0084 void ReflectionGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)
0085 {
0086     if (mBackground)
0087         QGraphicsScene::drawBackground(painter, rect);
0088 }
0089 
0090 #include "moc_reflectiongraphicsscene.cpp"