00001 /*========================================================================= 00002 00003 Program: Medical Imaging & Interaction Toolkit 00004 Language: C++ 00005 Date: $Date$ 00006 Version: $Revision: 13135 $ 00007 00008 Copyright (c) German Cancer Research Center, Division of Medical and 00009 Biological Informatics. All rights reserved. 00010 See MITKCopyright.txt or https://www.mitk.org/copyright.html for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 00018 /**************************************************************************** 00019 ** ui.h extension file, included from the uic-generated form implementation. 00020 ** 00021 ** If you want to add, delete, or rename functions or slots, use 00022 ** Qt Designer to update this file, preserving your code. 00023 ** 00024 ** You should not define a constructor or destructor in this file. 00025 ** Instead, write your code in functions called init() and destroy(). 00026 ** These will automatically be called by the form's constructor and 00027 ** destructor. 00028 *****************************************************************************/ 00029 00030 #include "QmitkMemoryUsageIndicatorView.h" 00031 #include <mitkMemoryUtilities.h> 00032 00033 #include <qtimer.h> 00034 #include <qimage.h> 00035 #include <qpixmap.h> 00036 #include <qapplication.h> 00037 #include <qeventloop.h> 00038 00039 #include <iostream> 00040 #include <sstream> 00041 #include <iomanip> 00042 00043 #include "QmitkMemoryUsageIndicatorImagesGreen.xpm" 00044 #include "QmitkMemoryUsageIndicatorImagesYellow.xpm" 00045 #include "QmitkMemoryUsageIndicatorImagesOrange.xpm" 00046 #include "QmitkMemoryUsageIndicatorImagesRed.xpm" 00047 00048 QmitkMemoryUsageIndicatorView::QmitkMemoryUsageIndicatorView( QWidget * /*parent*/, Qt::WindowFlags /*f*/ ) 00049 { 00050 this->setupUi(this); 00051 00052 QTimer *timer = new QTimer( this ); 00053 QObject::connect( timer, SIGNAL( timeout() ), this, SLOT( UpdateMemoryUsage() ) ); 00054 timer->start(1000); 00055 m_LEDGreen = QmitkMemoryUsageIndicatorImagesGreen_xpm; 00056 m_LEDYellow = QmitkMemoryUsageIndicatorImagesYellow_xpm; 00057 m_LEDOrange = QmitkMemoryUsageIndicatorImagesOrange_xpm; 00058 m_LEDRed = QmitkMemoryUsageIndicatorImagesRed_xpm; 00059 m_LED->setPixmap(m_LEDGreen); 00060 m_PreviousState = 0; 00061 } 00062 00063 QmitkMemoryUsageIndicatorView::~QmitkMemoryUsageIndicatorView() 00064 { 00065 } 00066 00067 void QmitkMemoryUsageIndicatorView::UpdateMemoryUsage() 00068 { 00069 size_t processSize = mitk::MemoryUtilities::GetProcessMemoryUsage(); 00070 size_t totalSize = mitk::MemoryUtilities::GetTotalSizeOfPhysicalRam(); 00071 float percentage = ( (float) processSize / (float) totalSize ) * 100.0; 00072 m_Label->setText( GetMemoryDescription( processSize, percentage ).c_str() ); 00073 if ( percentage < 50.0 ) 00074 { 00075 if(m_PreviousState != 0) 00076 { 00077 m_LED->setPixmap(m_LEDGreen); 00078 m_PreviousState = 0; 00079 m_LED->update(); 00080 } 00081 } 00082 else if ( percentage < 65.0 ) 00083 { 00084 if(m_PreviousState != 1) 00085 { 00086 m_LED->setPixmap(m_LEDYellow); 00087 m_PreviousState = 1; 00088 m_LED->update(); 00089 } 00090 } 00091 else if ( percentage < 80.0 ) 00092 { 00093 if(m_PreviousState != 2) 00094 { 00095 m_LED->setPixmap(m_LEDOrange); 00096 m_PreviousState = 2; 00097 m_LED->update(); 00098 } 00099 } 00100 else 00101 { 00102 if(m_PreviousState != 3) 00103 { 00104 m_LED->setPixmap(m_LEDRed); 00105 m_PreviousState = 3; 00106 m_LED->update(); 00107 } 00108 } 00109 } 00110 00111 00112 std::string QmitkMemoryUsageIndicatorView::FormatMemorySize( size_t size ) 00113 { 00114 double val = size; 00115 std::string descriptor("B"); 00116 if ( val >= 1000.0 ) 00117 { 00118 val /= 1024.0; 00119 descriptor = "KB"; 00120 } 00121 if ( val >= 1000.0 ) 00122 { 00123 val /= 1024.0; 00124 descriptor = "MB"; 00125 } 00126 if ( val >= 1000.0 ) 00127 { 00128 val /= 1024.0; 00129 descriptor = "GB"; 00130 } 00131 std::ostringstream str; 00132 str << std::fixed << std::setprecision(2) << val << " " << descriptor; 00133 return str.str(); 00134 } 00135 00136 std::string QmitkMemoryUsageIndicatorView::FormatPercentage( double val ) 00137 { 00138 std::ostringstream str; 00139 str << std::fixed << std::setprecision(2) << val << " " << "%"; 00140 return str.str(); 00141 } 00142 00143 std::string QmitkMemoryUsageIndicatorView::GetMemoryDescription( size_t processSize, float percentage ) 00144 { 00145 std::ostringstream str; 00146 str << FormatMemorySize(processSize) << " (" << FormatPercentage( percentage ) <<")" ; 00147 return str.str(); 00148 }