Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 /****************************************************************************
14 ** ui.h extension file, included from the uic-generated form implementation.
15 **
16 ** If you want to add, delete, or rename functions or slots, use
17 ** Qt Designer to update this file, preserving your code.
18 **
19 ** You should not define a constructor or destructor in this file.
20 ** Instead, write your code in functions called init() and destroy().
21 ** These will automatically be called by the form's constructor and
22 ** destructor.
23 *****************************************************************************/
24 
26 #include <mitkMemoryUtilities.h>
27 
28 #include <qapplication.h>
29 #include <qeventloop.h>
30 #include <qimage.h>
31 #include <qpixmap.h>
32 #include <qtimer.h>
33 
34 #include <iomanip>
35 #include <iostream>
36 #include <sstream>
37 
38 #include "QmitkMemoryUsageIndicatorImagesGreen.xpm"
39 #include "QmitkMemoryUsageIndicatorImagesOrange.xpm"
40 #include "QmitkMemoryUsageIndicatorImagesRed.xpm"
41 #include "QmitkMemoryUsageIndicatorImagesYellow.xpm"
42 
43 QmitkMemoryUsageIndicatorView::QmitkMemoryUsageIndicatorView(QWidget * /*parent*/, Qt::WindowFlags /*f*/)
44 {
45  this->setupUi(this);
46 
47  auto timer = new QTimer(this);
48  QObject::connect(timer, SIGNAL(timeout()), this, SLOT(UpdateMemoryUsage()));
49  timer->start(1000);
50  m_LEDGreen = QPixmap(QmitkMemoryUsageIndicatorImagesGreen_xpm);
51  m_LEDYellow = QPixmap(QmitkMemoryUsageIndicatorImagesYellow_xpm);
52  m_LEDOrange = QPixmap(QmitkMemoryUsageIndicatorImagesOrange_xpm);
53  m_LEDRed = QPixmap(QmitkMemoryUsageIndicatorImagesRed_xpm);
54  m_LED->setPixmap(m_LEDGreen);
55  m_PreviousState = 0;
56 }
57 
59 {
60 }
61 
63 {
64  size_t processSize = mitk::MemoryUtilities::GetProcessMemoryUsage();
66  float percentage = ((float)processSize / (float)totalSize) * 100.0;
67  m_Label->setText(GetMemoryDescription(processSize, percentage).c_str());
68  if (percentage < 50.0)
69  {
70  if (m_PreviousState != 0)
71  {
72  m_LED->setPixmap(m_LEDGreen);
73  m_PreviousState = 0;
74  m_LED->update();
75  }
76  }
77  else if (percentage < 65.0)
78  {
79  if (m_PreviousState != 1)
80  {
81  m_LED->setPixmap(m_LEDYellow);
82  m_PreviousState = 1;
83  m_LED->update();
84  }
85  }
86  else if (percentage < 80.0)
87  {
88  if (m_PreviousState != 2)
89  {
90  m_LED->setPixmap(m_LEDOrange);
91  m_PreviousState = 2;
92  m_LED->update();
93  }
94  }
95  else
96  {
97  if (m_PreviousState != 3)
98  {
99  m_LED->setPixmap(m_LEDRed);
100  m_PreviousState = 3;
101  m_LED->update();
102  }
103  }
104 }
105 
107 {
108  double val = size;
109  std::string descriptor("B");
110  if (val >= 1000.0)
111  {
112  val /= 1024.0;
113  descriptor = "KB";
114  }
115  if (val >= 1000.0)
116  {
117  val /= 1024.0;
118  descriptor = "MB";
119  }
120  if (val >= 1000.0)
121  {
122  val /= 1024.0;
123  descriptor = "GB";
124  }
125  std::ostringstream str;
126  str.imbue(std::locale::classic());
127  str << std::fixed << std::setprecision(2) << val << " " << descriptor;
128  return str.str();
129 }
130 
132 {
133  std::ostringstream str;
134  str.imbue(std::locale::classic());
135  str << std::fixed << std::setprecision(2) << val << " "
136  << "%";
137  return str.str();
138 }
139 
140 std::string QmitkMemoryUsageIndicatorView::GetMemoryDescription(size_t processSize, float percentage)
141 {
142  std::ostringstream str;
143  str.imbue(std::locale::classic());
144  str << FormatMemorySize(processSize) << " (" << FormatPercentage(percentage) << ")";
145  return str.str();
146 }
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