Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkMemoryUsageIndicatorView.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 /****************************************************************************
18 ** ui.h extension file, included from the uic-generated form implementation.
19 **
20 ** If you want to add, delete, or rename functions or slots, use
21 ** Qt Designer to update this file, preserving your code.
22 **
23 ** You should not define a constructor or destructor in this file.
24 ** Instead, write your code in functions called init() and destroy().
25 ** These will automatically be called by the form's constructor and
26 ** destructor.
27 *****************************************************************************/
28 
30 #include <mitkMemoryUtilities.h>
31 
32 #include <qapplication.h>
33 #include <qeventloop.h>
34 #include <qimage.h>
35 #include <qpixmap.h>
36 #include <qtimer.h>
37 
38 #include <iomanip>
39 #include <iostream>
40 #include <sstream>
41 
42 #include "QmitkMemoryUsageIndicatorImagesGreen.xpm"
43 #include "QmitkMemoryUsageIndicatorImagesOrange.xpm"
44 #include "QmitkMemoryUsageIndicatorImagesRed.xpm"
45 #include "QmitkMemoryUsageIndicatorImagesYellow.xpm"
46 
47 QmitkMemoryUsageIndicatorView::QmitkMemoryUsageIndicatorView(QWidget * /*parent*/, Qt::WindowFlags /*f*/)
48 {
49  this->setupUi(this);
50 
51  auto timer = new QTimer(this);
52  QObject::connect(timer, SIGNAL(timeout()), this, SLOT(UpdateMemoryUsage()));
53  timer->start(1000);
54  m_LEDGreen = QPixmap(QmitkMemoryUsageIndicatorImagesGreen_xpm);
55  m_LEDYellow = QPixmap(QmitkMemoryUsageIndicatorImagesYellow_xpm);
56  m_LEDOrange = QPixmap(QmitkMemoryUsageIndicatorImagesOrange_xpm);
57  m_LEDRed = QPixmap(QmitkMemoryUsageIndicatorImagesRed_xpm);
58  m_LED->setPixmap(m_LEDGreen);
59  m_PreviousState = 0;
60 }
61 
63 {
64 }
65 
67 {
68  size_t processSize = mitk::MemoryUtilities::GetProcessMemoryUsage();
70  float percentage = ((float)processSize / (float)totalSize) * 100.0;
71  m_Label->setText(GetMemoryDescription(processSize, percentage).c_str());
72  if (percentage < 50.0)
73  {
74  if (m_PreviousState != 0)
75  {
76  m_LED->setPixmap(m_LEDGreen);
77  m_PreviousState = 0;
78  m_LED->update();
79  }
80  }
81  else if (percentage < 65.0)
82  {
83  if (m_PreviousState != 1)
84  {
85  m_LED->setPixmap(m_LEDYellow);
86  m_PreviousState = 1;
87  m_LED->update();
88  }
89  }
90  else if (percentage < 80.0)
91  {
92  if (m_PreviousState != 2)
93  {
94  m_LED->setPixmap(m_LEDOrange);
95  m_PreviousState = 2;
96  m_LED->update();
97  }
98  }
99  else
100  {
101  if (m_PreviousState != 3)
102  {
103  m_LED->setPixmap(m_LEDRed);
104  m_PreviousState = 3;
105  m_LED->update();
106  }
107  }
108 }
109 
111 {
112  double val = size;
113  std::string descriptor("B");
114  if (val >= 1000.0)
115  {
116  val /= 1024.0;
117  descriptor = "KB";
118  }
119  if (val >= 1000.0)
120  {
121  val /= 1024.0;
122  descriptor = "MB";
123  }
124  if (val >= 1000.0)
125  {
126  val /= 1024.0;
127  descriptor = "GB";
128  }
129  std::ostringstream str;
130  str.imbue(std::locale::classic());
131  str << std::fixed << std::setprecision(2) << val << " " << descriptor;
132  return str.str();
133 }
134 
136 {
137  std::ostringstream str;
138  str.imbue(std::locale::classic());
139  str << std::fixed << std::setprecision(2) << val << " "
140  << "%";
141  return str.str();
142 }
143 
144 std::string QmitkMemoryUsageIndicatorView::GetMemoryDescription(size_t processSize, float percentage)
145 {
146  std::ostringstream str;
147  str.imbue(std::locale::classic());
148  str << FormatMemorySize(processSize) << " (" << FormatPercentage(percentage) << ")";
149  return str.str();
150 }
static vcl_size_t GetTotalSizeOfPhysicalRam()
std::string FormatMemorySize(vcl_size_t size)
std::string GetMemoryDescription(vcl_size_t processSize, float percentage)
static vcl_size_t GetProcessMemoryUsage()
QmitkMemoryUsageIndicatorView(QWidget *parent=nullptr, Qt::WindowFlags f=nullptr)
constructor