Mantid
Loading...
Searching...
No Matches
MantidTreeWidgetItem.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
9
10#include "MantidAPI/Workspace.h"
12
13using namespace Mantid::Kernel;
14using namespace Mantid::API;
15using Mantid::Types::Core::DateAndTime;
16
22 : QTreeWidgetItem(parent), m_parent(parent), m_sortPos(0) {}
23
28 : QTreeWidgetItem(list), m_parent(parent), m_sortPos(0) {}
29
33bool MantidTreeWidgetItem::operator<(const QTreeWidgetItem &other) const {
34 // If this and/or other has been set to have a Qt::UserRole, then
35 // it has an accompanying sort order that we must maintain, no matter
36 // what the user has seletected in terms of order or scheme.
37
38 bool thisShouldBeSorted = m_sortPos == 0;
39 const auto *mantidOther = dynamic_cast<const MantidTreeWidgetItem *>(&other);
40 int otherSortPos = mantidOther ? mantidOther->getSortPos() : 0;
41 bool otherShouldBeSorted = otherSortPos == 0;
42
43 // just in case m_parent is NULL. I think I saw this once but cannot
44 // reproduce.
45 if (!m_parent)
46 return false;
47
48 if (!thisShouldBeSorted && !otherShouldBeSorted) {
49 if (m_parent->getSortOrder() == Qt::AscendingOrder)
50 return m_sortPos < otherSortPos;
51 else
52 return m_sortPos >= otherSortPos;
53 } else if (thisShouldBeSorted && !otherShouldBeSorted) {
54 if (m_parent->getSortOrder() == Qt::AscendingOrder)
55 return false;
56 else
57 return true;
58 } else if (!thisShouldBeSorted && otherShouldBeSorted) {
59 if (m_parent->getSortOrder() == Qt::AscendingOrder)
60 return true;
61 else
62 return false;
63 }
64
65 // If both should be sorted, and the scheme is set to ByName ...
67 return QString::compare(text(0), other.text(0), Qt::CaseInsensitive) < 0;
68 }
69 // If both should be sorted and the scheme is set to ByMemorySize ...
71 return this->getMemorySize() < mantidOther->getMemorySize();
72 }
73 // ... else both should be sorted and the scheme is set to ByLastModified.
74 else {
75 try {
76 if (childCount() > 0 && other.childCount() > 0) {
77 const QTreeWidgetItem *other_ptr = &other;
78
79 try {
80 return getLastModified(this) < getLastModified(other_ptr);
81 } catch (std::out_of_range &e) {
82 m_parent->logWarningMessage(e.what());
83 return false;
84 }
85 }
87 ;
88 }
89 return false;
90 }
91}
92
97DateAndTime MantidTreeWidgetItem::getLastModified(const QTreeWidgetItem *item) {
98 QVariant userData = item->data(0, Qt::UserRole);
99 if (userData.isNull())
100 return DateAndTime(); // now
101
102 Workspace_sptr workspace = userData.value<Workspace_sptr>();
103 const Mantid::API::WorkspaceHistory &wsHist = workspace->getHistory();
104 if (wsHist.empty())
105 return DateAndTime(); // now
106
107 const size_t indexOfLast = wsHist.size() - 1;
108 const auto lastAlgHist = wsHist.getAlgorithmHistory(indexOfLast);
109 return lastAlgHist->executionDate();
110}
112 return this->data(0, Qt::UserRole).value<Workspace_sptr>()->getMemorySize();
113}
114} // namespace MantidQt::MantidWidgets
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
A class derived from QTreeWidgetItem, to accomodate sorting on the items in a MantidTreeWidget.
bool operator<(const QTreeWidgetItem &other) const override
Overidden operator.
static Mantid::Types::Core::DateAndTime getLastModified(const QTreeWidgetItem *)
Finds the date and time of the last modification made to the workspace who's details are found in the...
MantidTreeWidgetItem(MantidTreeWidget *)
Constructor.
void logWarningMessage(const std::string &)
Log a warning message.
MantidItemSortScheme getSortScheme() const
This class stores information about the Workspace History used by algorithms on a workspace and the e...
Exception for when an item is not found in a collection.
Definition: Exception.h:145
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20