Isis 3 Programmer Reference
HistoryTreeWidget.cpp
1 #include "HistoryTreeWidget.h"
2 
3 #include <QDateTime>
4 #include <QDebug>
5 
6 #include "ProgressBar.h"
7 #include "Project.h"
8 #include "WorkOrder.h"
9 
10 namespace Isis {
18  m_project = project;
19 
20  QStringList headers;
21  headers.append("Operation");
22  headers.append("Progress");
23  headers.append("Time Executed");
24 
25  setHeaderLabels(headers);
26 
27  connect(m_project, SIGNAL(workOrderStarting(WorkOrder *)),
28  this, SLOT(addToHistory(WorkOrder *)));
29  connect(m_project, SIGNAL(projectLoaded(Project *)),
30  this, SLOT(showHistory()));
31  connect(m_project->undoStack(), SIGNAL(indexChanged(int)),
32  this, SLOT(handleUndoIndexChanged(int)));
33 
34  showHistory();
35 
36  refit();
37  }
38 
39 
44  m_project = NULL;
45  }
46 
47 
57  int HistoryTreeWidget::sizeHintForColumn(int column) const {
58  int result = -1;
59 
60  QFontMetrics metrics(invisibleRootItem()->font(1));
61  int dateTimeColumnWidth = qRound(metrics.width(QDateTime::currentDateTime().toString()) * 1.10);
62 
63  int progressBarColumnWidth = 200;
64 
65  // The -12 is a guesstimate for not having a horizontal scroll bar. I'm not sure how to
66  // calculate this number correctly. Consequences of it being wrong are minimal. -SL
67  int operationColumnWidth = width() - dateTimeColumnWidth - progressBarColumnWidth - 12;
68 
69  if (operationColumnWidth > 0) {
70  if (column == 0)
71  result = operationColumnWidth;
72  else if (column == 1)
73  result = progressBarColumnWidth;
74  else if (column == 2)
75  result = dateTimeColumnWidth;
76  }
77 
78  return result;
79  }
80 
81 
87  resizeColumnToContents(0);
88  resizeColumnToContents(1);
89  resizeColumnToContents(2);
90  }
91 
92 
93  void HistoryTreeWidget::updateStatus(QTreeWidgetItem *treeItem) {
94  WorkOrder *workOrder = treeItem->data(0, Qt::UserRole).value<WorkOrder *>();
95 
96  if (workOrder)
97  updateStatus(workOrder);
98  }
99 
100 
108 
109  QString data = workOrder->bestText();
110 
111 
112  connect(workOrder, SIGNAL(destroyed(QObject *)),
113  this, SLOT(removeFromHistory(QObject *)));
114 
115  QStringList columnData;
116  columnData.append(data);
117  columnData.append("");
118  columnData.append(workOrder->executionTime().toString());
119 
120  QTreeWidgetItem *newItem = new QTreeWidgetItem(columnData);
121  newItem->setData(0, Qt::UserRole, qVariantFromValue(workOrder));
122 
123  // Do font for save work orders or work orders not on QUndoStack
124  if (workOrder->createsCleanState() || !workOrder->isUndoable()) {
125  QFont saveFont = newItem->font(0);
126  saveFont.setBold(true);
127  saveFont.setItalic(true);
128  newItem->setFont(0, saveFont);
129  newItem->setForeground(0, Qt::gray);
130  }
131 
132  // Do font for progress text
133  QFont progressFont = newItem->font(1);
134  progressFont.setItalic(true);
135  newItem->setFont(1, progressFont);
136  newItem->setForeground(1, Qt::gray);
137 
138  this->insertTopLevelItem(0, newItem);
139 
140  connect(workOrder, SIGNAL(statusChanged(WorkOrder *)),
141  this, SLOT(updateStatus(WorkOrder *)));
142  connect(workOrder, SIGNAL(creatingProgress(WorkOrder *)),
143  this, SLOT(updateProgressWidgets()));
144  connect(workOrder, SIGNAL(deletingProgress(WorkOrder *)),
145  this, SLOT(updateProgressWidgets()));
146 
147 
148  //Sometimes the pointer returned by this call is 0 (hence the check).
149  //So we are not creating a progress bar for every work order which would
150  //include those that do not need it.
151 
152  if(workOrder->progressBar() ) {
153  this->setItemWidget(newItem, 1, workOrder->progressBar() );
154  }
155  scrollToItem(newItem);
156  refit();
157  }
158 
159 
165  void HistoryTreeWidget::addToHistory(QString historyEntry) {
166 
167  QString data = historyEntry;
168 
169  QStringList columnData;
170  columnData.append(data);
171 
172  QTreeWidgetItem *newItem = new QTreeWidgetItem(columnData);
173 
174 
175  // Do font for progress text
176  QFont progressFont = newItem->font(1);
177  progressFont.setItalic(true);
178  newItem->setFont(1, progressFont);
179  newItem->setForeground(1, Qt::gray);
180 
181  this->insertTopLevelItem(0, newItem);
182 // invisibleRootItem()->addChild(newItem);
183 
184  //Sometimes the pointer returned by this call is 0 (hence the check).
185  //So we are not creating a progress bar for every work order which would
186  //include those that do not need it.
187 
188 // if(workOrder->progressBar() ) {
189 // setItemWidget(newItem, 1, new ProgressBar);
191 // }
192  scrollToItem(newItem);
193  refit();
194  }
195 
196 
206  if( !m_project->clearing() ){
207  for (int i = 0; i < invisibleRootItem()->childCount(); i++) {
208  QTreeWidgetItem *item = invisibleRootItem()->child(i);
209  if (item->data(0, Qt::UserRole).toString() != "") {
210  WorkOrder *workOrder = item->data(0, Qt::UserRole).value<WorkOrder *>();
211  if (workOrder && itemWidget(item, 1) != workOrder->progressBar()) {
212  setItemWidget(item, 1, workOrder->progressBar());
213  }
214  }
215  }
216  }
217  }
218 
219 
227  if (treeItem) {
228  treeItem->setForeground(0, Qt::black);
229  updateStatus(treeItem);
230  }
231  }
232 
233 
241  if (treeItem) {
242  treeItem->setForeground(0, Qt::gray);
243  updateStatus(treeItem);
244  }
245  }
246 
247 
253  m_project->undoStack()->command(newIndex - 1));
254  markNotUndone(prevItem);
255 
256  QTreeWidgetItem *curItem = undoCommandToTreeItem(m_project->undoStack()->command(newIndex));
257  markUndone(curItem);
258 
259  markUndone(undoCommandToTreeItem(m_project->undoStack()->command(newIndex + 1)));
260 
261  scrollToItem(prevItem);
262  scrollToItem(curItem);
263  }
264 
265 
270  QTreeWidgetItem *itemToRemove = undoCommandToTreeItem( (QUndoCommand *)((WorkOrder *)deletedObject));
271 
272  if (itemToRemove) {
273 
274  int indexToDelete = invisibleRootItem()->indexOfChild(itemToRemove);
275  if (indexToDelete < invisibleRootItem()->childCount()) {
276 
277  // Clear progress bar widget
278  setItemWidget(invisibleRootItem()->child(indexToDelete), 1, NULL);
279 
280  // Take & delete the tree item
281  delete invisibleRootItem()->takeChild(indexToDelete);
282  }
283  }
284  }
285 
286 
295  QTreeWidgetItem *result = NULL;
296  if (undoCommand) {
297  for (int i = invisibleRootItem()->childCount() - 1; !result && i >= 0; i--) {
298  QTreeWidgetItem *item = invisibleRootItem()->child(i);
299  if (item->data(0, Qt::UserRole).toString() != "" ) {
300  WorkOrder *workOrder = item->data(0, Qt::UserRole).value<WorkOrder *>();
301 
302  if (undoCommand == workOrder)
303  result = item;
304  }
305  }
306  }
307  return result;
308  }
309 
310 
318  foreach (QTreeWidgetItem *item, invisibleRootItem()->takeChildren()) {
319  delete item;
320  }
321 
322  foreach (WorkOrder *workOrder, m_project->workOrderHistory()) {
323  addToHistory(workOrder);
324  }
325  }
326 
327 
328  void HistoryTreeWidget::updateStatus(WorkOrder *workOrder) {
329  if (undoCommandToTreeItem(workOrder)) {
330  undoCommandToTreeItem(workOrder)->setText(1, workOrder->statusText());
331  }
332 
333  }
334 }
QString bestText() const
Generate unique action names We don&#39;t use action text anymore because Directory likes to rename our a...
Definition: WorkOrder.cpp:809
void addToHistory(QString historyEntry)
Add a non-workorder history to the display.
The main project for ipce.
Definition: Project.h:289
void markUndone(QTreeWidgetItem *)
Display the item as an item that has been undone.
QList< WorkOrder * > workOrderHistory()
Get the entire list of work orders that have executed.
Definition: Project.cpp:1748
void markNotUndone(QTreeWidgetItem *)
Display the item as not an item that has been undone - it&#39;s working or done.
QUndoStack * undoStack()
Returns the Projects stack of QUndoCommands.
Definition: Project.cpp:1694
HistoryTreeWidget(Project *project, QWidget *parent=0)
Construct a history tree widget.
virtual ~HistoryTreeWidget()
Clean up allocated memory.
int sizeHintForColumn(int column) const
Get the preferred size of a given column.
void handleUndoIndexChanged(int)
The project&#39;s undo stack has changed.
Provide Undo/redo abilities, serialization, and history for an operation.
Definition: WorkOrder.h:322
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
void removeFromHistory(QObject *)
A work order was lost...
bool createsCleanState() const
Returns the CleanState status (whether the Project has been saved to disk or not).
Definition: WorkOrder.cpp:868
void updateProgressWidgets()
We need to manually manage these progress widgets because QTreeWidget does a poor job of it...
void showHistory()
This resets the tree widget and re-initializes.
void refit()
This resizes the columns to an okay width for viewing all of the data cleanly.
QTreeWidgetItem * undoCommandToTreeItem(const QUndoCommand *)
Get the QTreeWidgetItem associated with the given undo command (work order).
QString statusText() const
WorkOrder::statusText.
Definition: WorkOrder.cpp:968
QDateTime executionTime() const
Gets the execution time of this WorkOrder.
Definition: WorkOrder.cpp:878
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
ProgressBar * progressBar()
Returns the ProgressBar.
Definition: WorkOrder.cpp:989
bool isUndoable() const
Returns true if this work order is undoable, otherwise false.
Definition: WorkOrder.cpp:833