7 #include "monitorQt/DumpEditor.hpp" 9 #include <fwCom/Slot.hpp> 10 #include <fwCom/Slot.hxx> 12 #include <fwCore/base.hpp> 14 #include <fwGui/Cursor.hpp> 15 #include <fwGui/dialog/IMessageDialog.hpp> 16 #include <fwGui/dialog/MessageDialog.hpp> 18 #include <fwGuiQt/container/QtContainer.hpp> 20 #include <fwMemory/BufferManager.hpp> 21 #include <fwMemory/ByteSize.hpp> 22 #include <fwMemory/IPolicy.hpp> 23 #include <fwMemory/tools/MemoryMonitorTools.hpp> 25 #include <fwServices/macros.hpp> 26 #include <fwServices/registry/ActiveWorkers.hpp> 28 #include <fwTools/fwID.hpp> 29 #include <fwTools/Stringizer.hpp> 31 #include <boost/lexical_cast.hpp> 35 #include <QHeaderView> 36 #include <QItemDelegate> 37 #include <QStringList> 38 #include <QTableWidgetItem> 39 #include <QtConcurrentRun> 41 #include <QVBoxLayout> 50 ::fwMemory::BufferManager::BufferInfoMapType m_bufferInfos;
55 QString getHumanReadableSize(::fwMemory::ByteSize::SizeType bytes)
71 QWidget* createEditor(QWidget* parent,
const QStyleOptionViewItem& option,
const QModelIndex& index)
const;
73 void setEditorData(QWidget* editor,
const QModelIndex& index)
const;
74 void setModelData(QWidget* editor, QAbstractItemModel* model,
const QModelIndex& index)
const;
76 void updateEditorGeometry(QWidget* editor,
const QStyleOptionViewItem& option,
const QModelIndex& index)
const;
81 QWidget* PolicyComboBoxDelegate::createEditor(QWidget* parent,
82 const QStyleOptionViewItem& option,
83 const QModelIndex& index )
const 85 QComboBox* policyComboBox =
new QComboBox(parent);
87 const std::string value = index.model()->data(index, Qt::DisplayRole).toString().toStdString();
89 const ::fwMemory::policy::registry::Type::KeyVectorType& factories =
90 ::fwMemory::policy::registry::get()->getFactoryKeys();
92 for( const ::fwMemory::policy::registry::KeyType& policy : factories)
94 policyComboBox->addItem(QString::fromStdString(policy));
97 policyComboBox->setCurrentIndex(policyComboBox->count()-1);
100 return policyComboBox;
105 void PolicyComboBoxDelegate::setEditorData(QWidget* editor,
const QModelIndex& index)
const 107 QString value = index.model()->data(index, Qt::DisplayRole).toString();
109 QComboBox* policyComboBox =
static_cast<QComboBox*
>(editor);
111 int idx = policyComboBox->findText(value);
114 policyComboBox->setCurrentIndex(idx);
120 void PolicyComboBoxDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
const QModelIndex& index)
const 122 QComboBox* policyComboBox =
static_cast<QComboBox*
>(editor);
123 QString value = policyComboBox->currentText();
125 model->setData(index, value, Qt::EditRole);
130 void PolicyComboBoxDelegate::updateEditorGeometry(QWidget* editor,
131 const QStyleOptionViewItem& option,
132 const QModelIndex& index )
const 134 editor->setGeometry(option.rect);
143 int rowCount(
const QModelIndex& parent)
const;
144 int columnCount(
const QModelIndex& parent)
const;
145 QVariant data(
const QModelIndex& index,
int role)
const;
146 QVariant headerData(
int section, Qt::Orientation orientation,
int role)
const;
147 Qt::ItemFlags flags(
const QModelIndex& index)
const;
148 bool setData(
const QModelIndex& index,
const QVariant& value,
int role = Qt::EditRole);
150 static const int s_EXTRA_INFO_NB;
153 ::fwMemory::BufferManager::sptr m_buffManager;
156 const int PolicyTableModel::s_EXTRA_INFO_NB = 1;
158 PolicyTableModel::PolicyTableModel(QObject* parent) :
159 QAbstractTableModel(parent)
166 int PolicyTableModel::rowCount(
const QModelIndex& parent)
const 173 ::fwMemory::IPolicy::sptr currentPolicy = m_buffManager->getDumpPolicy();
174 nbParam = currentPolicy->getParamNames().size();
177 return static_cast<int>(nbParam + s_EXTRA_INFO_NB);
182 int PolicyTableModel::columnCount(
const QModelIndex& parent)
const 190 QVariant PolicyTableModel::data(
const QModelIndex& index,
int role)
const 192 if (!m_buffManager && !index.isValid())
197 ::fwMemory::IPolicy::sptr currentPolicy = m_buffManager->getDumpPolicy();
199 if (index.row() > (s_EXTRA_INFO_NB + currentPolicy->getParamNames().size()) || index.row() < 0)
204 if (role == Qt::DisplayRole)
207 if (index.column() == 0)
209 const ::fwMemory::IPolicy::ParamNamesType& names = currentPolicy->getParamNames();
212 return QString::fromStdString(currentPolicy->getLeafClassname());
214 else if( (
unsigned int)index.row() <= names.size())
216 const ::fwMemory::IPolicy::ParamNamesType::value_type& name = names.at(index.row() - 1);
218 return QString::fromStdString(currentPolicy->getParam(name));
228 QVariant PolicyTableModel::headerData(
int section, Qt::Orientation orientation,
int role)
const 230 if (role != Qt::DisplayRole)
235 if (m_buffManager && orientation == Qt::Vertical)
238 ::fwMemory::IPolicy::sptr currentPolicy = m_buffManager->getDumpPolicy();
239 const ::fwMemory::IPolicy::ParamNamesType& names = currentPolicy->getParamNames();
242 return QString(
"Current policy");
244 else if( (
unsigned int)section <= names.size() )
246 const ::fwMemory::IPolicy::ParamNamesType::value_type& name = names.at(section - 1);
247 return QString::fromStdString(name);
255 bool PolicyTableModel::setData(
const QModelIndex& index,
const QVariant& value,
int role)
257 if (m_buffManager && index.isValid() && role == Qt::EditRole)
259 int row = index.row();
260 int col = index.column();
261 const std::string strvalue = value.toString().toStdString();
264 ::fwMemory::IPolicy::sptr currentPolicy = m_buffManager->getDumpPolicy();
265 const ::fwMemory::IPolicy::ParamNamesType& names = currentPolicy->getParamNames();
267 if (col == 0 && (
unsigned int)row <= names.size() )
269 ::fwMemory::IPolicy::sptr dumpPolicy;
273 if(strvalue != currentPolicy->getLeafClassname())
275 dumpPolicy = ::fwMemory::policy::registry::get()->create(strvalue);
279 m_buffManager->setDumpPolicy(dumpPolicy);
281 this->beginResetModel();
282 this->endResetModel();
286 const ::fwMemory::IPolicy::ParamNamesType::value_type& name = names.at(row - 1);
287 currentPolicy->setParam(name, strvalue);
297 Qt::ItemFlags PolicyTableModel::flags(
const QModelIndex& index)
const 299 if (!index.isValid())
301 return Qt::ItemIsEnabled;
304 return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
315 int rowCount(
const QModelIndex& parent)
const;
316 int columnCount(
const QModelIndex& parent)
const;
317 QVariant data(
const QModelIndex& index,
int role)
const;
318 QVariant headerData(
int section, Qt::Orientation orientation,
int role)
const;
322 ::fwMemory::BufferManager::sptr m_buffManager;
325 InfoTableModel::InfoTableModel(QObject* parent) :
326 QAbstractTableModel(parent)
333 int InfoTableModel::rowCount(
const QModelIndex& parent)
const 341 int InfoTableModel::columnCount(
const QModelIndex& parent)
const 349 QVariant InfoTableModel::data(
const QModelIndex& index,
int role)
const 351 if (!m_buffManager || !index.isValid())
356 if (index.row() > this->rowCount(index))
361 if (role == Qt::DisplayRole)
363 if (index.column() == 0)
365 std::uint64_t sysMem;
366 ::fwMemory::BufferManager::SizeType bufferManagerMem;
370 sysMem = ::fwMemory::tools::MemoryMonitorTools::getTotalSystemMemory();
371 return QString(getHumanReadableSize(sysMem));
374 sysMem = ::fwMemory::tools::MemoryMonitorTools::getFreeSystemMemory();
375 return QString(getHumanReadableSize(sysMem));
378 bufferManagerMem = m_bufferStats.totalManaged;
379 return QString(getHumanReadableSize(bufferManagerMem));
382 bufferManagerMem = m_bufferStats.totalDumped;
383 return QString(getHumanReadableSize(bufferManagerMem));
393 QVariant InfoTableModel::headerData(
int section, Qt::Orientation orientation,
int role)
const 395 if (role == Qt::DisplayRole && orientation == Qt::Vertical)
400 return QString(
"Total System Memory");
403 return QString(
"Free System Memory");
406 return QString(
"Managed");
409 return QString(
"Dumped");
436 ::fwGuiQt::container::QtContainer::sptr qtContainer
437 = ::fwGuiQt::container::QtContainer::dynamicCast(this->getContainer() );
439 m_updateTimer =
new QTimer(qtContainer->getQtContainer());
440 m_updateTimer->setInterval(300);
441 m_updateTimer->setSingleShot(
true);
443 m_list =
new QTableWidget();
444 m_mapper =
new QSignalMapper();
446 m_list->setColumnCount(5);
448 header.push_back(
"Size");
449 header.push_back(
"Status");
450 header.push_back(
"Timestamp");
451 header.push_back(
"Locked");
452 header.push_back(
"Action");
453 m_list->setHorizontalHeaderLabels(header);
455 m_refresh =
new QPushButton(tr(
"Refresh"));
456 QVBoxLayout* sizer =
new QVBoxLayout();
458 QHBoxLayout* sizerButton =
new QHBoxLayout();
459 sizerButton->addWidget(m_refresh);
461 sizerButton->addItem(
new QSpacerItem(10, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));
462 QFrame* verticalLine =
new QFrame();
463 verticalLine->setFrameShape(QFrame::VLine);
464 verticalLine->setFrameShadow(QFrame::Sunken);
465 sizerButton->addWidget(verticalLine);
467 sizer->addLayout(sizerButton);
468 sizer->addWidget(m_list, 2);
470 m_policyEditor =
new QTableView();
473 m_policyEditor->setModel(policyTableModel);
474 m_policyEditor->setItemDelegateForRow(0, policyComboBoxDelegate);
475 m_policyEditor->setSortingEnabled(
false);
476 m_policyEditor->horizontalHeader()->hide();
479 m_infoEditor =
new QTableView();
480 m_infoEditor->setModel(infoTableModel);
481 m_infoEditor->horizontalHeader()->hide();
483 QHBoxLayout* tablesLayout =
new QHBoxLayout();
484 tablesLayout->addWidget(m_infoEditor);
485 tablesLayout->addWidget(m_policyEditor);
487 sizer->addLayout(tablesLayout);
489 qtContainer->setLayout( sizer );
491 QObject::connect(m_refresh, SIGNAL(clicked()),
this, SLOT(onRefreshButton()));
492 QObject::connect(m_mapper, SIGNAL(mapped(
int)),
this, SLOT(changeStatus(
int)));
494 QObject::connect(m_updateTimer, SIGNAL(timeout()),
this, SLOT(onRefreshButton()));
495 QObject::connect(&m_watcher, SIGNAL(finished()),
this, SLOT(onBufferInfo()));
503 m_connection = buffManager->getUpdatedSignal()->connect( m_updateSlot );
513 m_connection.disconnect();
514 QObject::disconnect(m_refresh, SIGNAL(clicked()),
this, SLOT(onRefreshButton()));
515 QObject::disconnect(m_mapper, SIGNAL(mapped(
int)),
this, SLOT(changeStatus(
int)));
516 QObject::disconnect(&m_watcher, SIGNAL(finished()),
this, SLOT(onBufferInfo()));
536 QTableWidgetItem(text)
542 virtual bool operator< (
const QTableWidgetItem& other )
const 544 return data(Qt::UserRole).toULongLong() < other.data(Qt::UserRole).toULongLong();
554 ::fwMemory::BufferManager::BufferInfoMapType getInfoMap()
556 ::fwMemory::BufferManager::BufferInfoMapType infoMap;
560 infoMap = buffManager->getBufferInfos().get();
569 m_policyEditor->reset();
570 m_policyEditor->resizeColumnsToContents();
572 QFuture< ::fwMemory::BufferManager::BufferInfoMapType > qFuture = QtConcurrent::run(getInfoMap);
573 m_watcher.setFuture(qFuture);
578 void DumpEditor::onBufferInfo()
580 m_bufferInfos = m_watcher.result();
581 m_bufferStats = ::fwMemory::BufferManager::computeBufferStats(m_bufferInfos);
583 m_mapper->blockSignals(
true);
586 for(
int row = 0; row < m_list->rowCount(); row++)
588 m_mapper->removeMappings( m_list->cellWidget(row, 4) );
590 m_list->clearContents();
591 m_objectsUID.clear();
594 m_list->setSortingEnabled(
false);
595 m_list->setRowCount(static_cast<int>(m_bufferInfos.size()));
596 m_list->setColumnCount(5);
598 for(const ::fwMemory::BufferManager::BufferInfoMapType::value_type& elt : m_bufferInfos)
600 m_objectsUID.push_back(elt.first);
602 std::string status =
"?";
603 std::string date =
"?";
604 std::string lockStatus =
"?";
606 const ::fwMemory::BufferInfo& dumpBuffInfo = elt.second;
607 bool loaded = dumpBuffInfo.loaded;
610 backColor = Qt::darkYellow;
615 backColor = Qt::white;
619 bool isLock = dumpBuffInfo.lockCount() > 0;
622 lockStatus =
"locked(" + ::fwTools::getString(dumpBuffInfo.lockCount()) +
")";
626 lockStatus =
"unlocked";
629 date = ::fwTools::getString(dumpBuffInfo.lastAccess.getLogicStamp());
631 QTableWidgetItem* currentSizeItem =
new SizeTableWidgetItem( getHumanReadableSize(dumpBuffInfo.size) );
632 currentSizeItem->setData(Qt::UserRole, (qulonglong)dumpBuffInfo.size );
633 currentSizeItem->setFlags(Qt::ItemIsEnabled);
634 currentSizeItem->setBackgroundColor(backColor);
635 m_list->setItem(itemCount, 0, currentSizeItem );
637 QTableWidgetItem* statusItem =
new QTableWidgetItem( QString::fromStdString(status));
638 statusItem->setFlags(Qt::ItemIsEnabled);
639 statusItem->setBackgroundColor(backColor);
640 m_list->setItem(itemCount, 1, statusItem );
642 QTableWidgetItem* dateItem =
new QTableWidgetItem( QString::fromStdString(date));
643 dateItem->setFlags(Qt::ItemIsEnabled);
644 dateItem->setBackgroundColor(backColor);
645 m_list->setItem(itemCount, 2, dateItem );
647 QTableWidgetItem* lockStatusItem =
new QTableWidgetItem( QString::fromStdString(lockStatus));
648 lockStatusItem->setFlags(Qt::ItemIsEnabled);
649 lockStatusItem->setBackgroundColor(backColor);
650 m_list->setItem(itemCount, 3, lockStatusItem );
652 QPushButton* actionItem =
new QPushButton(QString::fromStdString((loaded) ?
"Dump" :
"Restore"), m_list);
653 actionItem->setEnabled(!isLock && (dumpBuffInfo.size > 0) );
654 m_list->setCellWidget(itemCount, 4, actionItem );
655 QObject::connect(actionItem, SIGNAL(pressed()), m_mapper, SLOT(map()));
656 m_mapper->setMapping(actionItem, itemCount);
660 m_list->setSortingEnabled(
true);
662 m_mapper->blockSignals(
false);
664 m_infoEditor->reset();
665 m_infoEditor->resizeColumnsToContents();
679 _sstream <<
"Dump Editor";
686 m_updateTimer->start();
703 const ::fwMemory::BufferManager::BufferInfoMapType buffInfoMap = m_bufferInfos;
704 ::fwMemory::BufferManager::ConstBufferPtrType selectedBuffer = m_objectsUID[index];
706 ::fwMemory::BufferManager::BufferInfoMapType::const_iterator iter;
707 iter = buffInfoMap.find(selectedBuffer);
708 if( iter != buffInfoMap.end())
711 cursor.
setCursor(::fwGui::ICursor::BUSY);
712 const ::fwMemory::BufferInfo& dumpBuffInfo = iter->second;
714 bool isLock = dumpBuffInfo.lockCount() > 0;
717 if ( dumpBuffInfo.loaded )
719 buffManager->dumpBuffer(selectedBuffer);
723 buffManager->restoreBuffer(selectedBuffer);
729 "Dump process information",
730 "Dump process is locked. It is impossible to dump or restore this object.",
731 ::fwGui::dialog::IMessageDialog::WARNING);
740 std::stringstream stream;
741 stream <<
"Object " << selectedBuffer <<
" not found, please refresh the grid.";
743 "Dump process information",
745 ::fwGui::dialog::IMessageDialog::WARNING);
void changeStatus(int)
This method is called when an item is pressed.
virtual FWGUI_API void setCursor(::fwGui::ICursor::CursorType cursor) override
Set the cursor.
::boost::upgrade_lock< ReadWriteMutex > ReadToWriteLock
Defines an upgradable lock type for read/write mutex.
Conversion helper for size in bytes Converts string to number of bytes and vice-versa. This class is also able to manage conversions between units standards (IEC, SI)
Class allowing to block a Connection.
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Defines the service interface managing the editor service for object.
static FWGUI_API IMessageDialog::Buttons showMessageDialog(const std::string &title, const std::string &message,::fwGui::dialog::IMessageDialog::Icons icon=INFO)
virtual void configuring() override
Calls classic IAction methods to configure.
static FWMEMORY_API BufferManager::sptr getDefault()
Returns the current BufferManager instance.
virtual void info(std::ostream &_sstream) override
Overrides. Does nothing.
virtual void stopping() override
Stop the layout.
virtual MONITORQT_API ~DumpEditor() noexcept
Destructor. Does nothing.
static FWSERVICES_API const std::string s_DEFAULT_WORKER
Key of default worker in registry, created and registered by initRegistry method. ...
MONITORQT_API DumpEditor() noexcept
Constructor. Does nothing.
virtual void starting() override
Install the layout and call updating() method.
static FWSERVICES_API ActiveWorkers::sptr getDefault()
Returns an instance of ActiveWorkers.
void onUpdate()
Start m_updateTimer, call on buffManager signal emit ( see m_refreshSignal )
void onRefreshButton()
Slot called when user click on button m_refresh, call updating() method.
::boost::shared_lock< ReadWriteMutex > ReadLock
Defines a lock of read type for read/write mutex.
FWGUI_API void create()
Creates view, sub-views and toolbar containers. Manages sub-views and toobar services.
virtual void swapping() override
Call updating() method.
Defines the generic cursor for IHM. Use the Delegate design pattern.
virtual void updating() override
Update the choice selection.
FWGUI_API void initialize()
Initialize managers.
Editor to dump or restore selected buffer.
virtual FWGUI_API void setDefaultCursor() override
Set the default cursor.