-
Notifications
You must be signed in to change notification settings - Fork 122
/
CatalogSearch.cpp
1270 lines (1097 loc) · 50.3 KB
/
CatalogSearch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/FacilityInfo.h"
#include "MantidQtMantidWidgets/CatalogSearch.h"
#include <Poco/ActiveResult.h>
#include <Poco/Path.h>
#include <QDesktopServices>
#include <QFileDialog>
#include <QSettings>
#include <QStyle>
#include <QUrl>
#include <QDesktopWidget>
#include <fstream>
namespace MantidQt
{
namespace MantidWidgets
{
/**
* Constructor
*/
CatalogSearch::CatalogSearch(QWidget* parent) : QWidget(parent),
m_icatHelper(new CatalogHelper()), m_catalogSelector(new CatalogSelector()), m_currentPageNumber(1)
{
initLayout();
// Load saved settings from store.
loadSettings();
}
/**
* Destructor
*/
CatalogSearch::~CatalogSearch(){}
/**
* Initialise the default layout.
*/
void CatalogSearch::initLayout()
{
// Draw the GUI from .ui header generated file.
m_icatUiForm.setupUi(this);
// What facilities is the user logged in to?
m_icatUiForm.facilityName->setText(QString::fromStdString("Currently logged into " + Mantid::Kernel::ConfigService::Instance().getFacility().name()));
// Only want to show labels when an error occurs.
hideErrorLabels();
// Hide advanced input fields until "Advanced search" is checked.
advancedSearchChecked();
// Show the search frame by default.
m_icatUiForm.searchCbox->setChecked(true);
showCatalogSearch();
// Prevents a user seeing empty tables.
m_icatUiForm.searchResultsCbox->setEnabled(false);
m_icatUiForm.dataFileCbox->setEnabled(false);
m_icatUiForm.resFrame->hide();
m_icatUiForm.dataFileFrame->hide();
// Disable download and load buttons until a user has selected a datafile.
m_icatUiForm.dataFileDownloadBtn->setEnabled(false);
m_icatUiForm.dataFileLoadBtn->setEnabled(false);
// We create the calendar here to allow only one instance of it to occur.
m_calendar = new QCalendarWidget(qobject_cast<QWidget*>(this->parent()));
// When the user has selected a date from the calendar we want to set the related date input field.
connect(m_calendar,SIGNAL(clicked(QDate)),this,SLOT(dateSelected(QDate)));
// Show related help page when a user clicks on the "Help" button.
connect(m_icatUiForm.helpBtn,SIGNAL(clicked()),this,SLOT(helpClicked()));
// Show "Search" frame when user clicks "Catalog search" check box.
connect(m_icatUiForm.searchCbox,SIGNAL(clicked()),this,SLOT(showCatalogSearch()));
// Show advanced search options if "Advanced search" is checked.
connect(m_icatUiForm.advSearchCbox,SIGNAL(clicked()),this,SLOT(advancedSearchChecked()));
// Open calendar when start or end date is selected
connect(m_icatUiForm.startDatePicker,SIGNAL(clicked()),this, SLOT(openCalendar()));
connect(m_icatUiForm.endDatePicker,SIGNAL(clicked()),this, SLOT(openCalendar()));
// Clear all fields when reset button is pressed.
connect(m_icatUiForm.resetBtn,SIGNAL(clicked()),this,SLOT(onReset()));
// Show "Search results" frame when user tries to "Search".
connect(m_icatUiForm.searchBtn,SIGNAL(clicked()),this,SLOT(searchClicked()));
// Show "Search results" frame when user clicks related check box.
connect(m_icatUiForm.searchResultsCbox,SIGNAL(clicked()),this,SLOT(showSearchResults()));
// When the user has double clicked on an investigation they wish to view datafiles for then load the relevant datafiles.
connect(m_icatUiForm.searchResultsTbl,SIGNAL(itemDoubleClicked(QTableWidgetItem*)),this,SLOT(investigationSelected(QTableWidgetItem*)));
// Show "DataFile frame" when the user selects an investigation.
connect(m_icatUiForm.dataFileCbox,SIGNAL(clicked()),this,SLOT(showDataFileInfo()));
// When the user has selected a filter type then perform the filter for the specified type.
connect(m_icatUiForm.dataFileFilterCombo,SIGNAL(currentIndexChanged(int)),this,SLOT(doFilter(int)));
// When the user clicks "download to..." then open a dialog and download the file(s) to that location.
connect(m_icatUiForm.dataFileDownloadBtn,SIGNAL(clicked()),this,SLOT(downloadDataFiles()));
// When the user clicks the "load" button then load their selected datafiles into a workspace.
connect(m_icatUiForm.dataFileLoadBtn,SIGNAL(clicked()),this,SLOT(loadDataFiles()));
// When a checkbox is selected in a row we want to select (highlight) the entire row.
connect(m_icatUiForm.dataFileResultsTbl,SIGNAL(itemClicked(QTableWidgetItem*)),this,SLOT(dataFileCheckboxSelected(QTableWidgetItem*)));
// When several rows are selected we want to check the related checkboxes.
connect(m_icatUiForm.dataFileResultsTbl,SIGNAL(itemSelectionChanged()),this,SLOT(dataFileRowSelected()));
// When the user clicks "< Prev" populate the results table with the previous 100 results.
connect(m_icatUiForm.resPrevious,SIGNAL(clicked()),this,SLOT(prevPageClicked()));
// When the user clicks "Next >" populate the results table with the next 100 results.
connect(m_icatUiForm.resNext,SIGNAL(clicked()),this,SLOT(nextPageClicked()));
// When the user is done editing & presses enter we retrieve the results for that specific page in paging.
connect(m_icatUiForm.pageStartNum,SIGNAL(editingFinished()),SLOT(goToInputPage()));
// Open the catalog/facility selection widget when 'Select a catalog' is clicked.
connect(m_icatUiForm.catalogSelection,SIGNAL(clicked()),SLOT(openFacilitySelection()));
// No need for error handling as that's dealt with in the algorithm being used.
populateInstrumentBox();
// Although this is an advanced option performing it here allows it to be performed once only.
populateInvestigationTypeBox();
// As the methods have been created, and elements are in GUI I have opted to hide
// these elements for testing purposes as multiple facilities or paging has not yet been implemented.
// They will be implemented in separate tickets in the next release.
m_icatUiForm.facilityLogin->hide();
// Limit input to: A number, 1 hyphen or colon followed by another number. E.g. 444-444, -444, 444-
QRegExp re("[0-9]*(-|:){1}[0-9]*");
m_icatUiForm.RunRange->setValidator(new QRegExpValidator(re, this));
// Limit the page number input field to only digits.
m_icatUiForm.pageStartNum->setValidator(new QIntValidator(0,999,this));
// Resize to minimum width/height to improve UX.
this->resize(minimumSizeHint());
// Centre the GUI on screen.
this->setGeometry(QStyle::alignedRect(Qt::LeftToRight,Qt::AlignCenter,this->window()->size(),QDesktopWidget().availableGeometry()));
}
/**
* Opens the login dialog to allow the user to log into another facility.
*/
void CatalogSearch::onFacilityLogin()
{
}
/**
* Sends the user to relevant search page on the Mantid project site.
*/
void CatalogSearch::helpClicked()
{
QDesktopServices::openUrl(QUrl("http://www.mantidproject.org/Catalog_Search"));
}
/**
* Shows/hides the "Catalog search" frame when search combo box is checked.
*/
void CatalogSearch::showCatalogSearch()
{
if (m_icatUiForm.searchCbox->isChecked())
{
m_icatUiForm.searchFrame->show();
}
else
{
m_icatUiForm.searchFrame->hide();
}
}
/**
* Shows/Hides the "Search results" frame when search results combo box is checked.
*/
void CatalogSearch::showSearchResults()
{
if (m_icatUiForm.searchResultsCbox->isChecked())
{
m_icatUiForm.resFrame->show();
}
else
{
m_icatUiForm.resFrame->hide();
}
}
/**
* Hides "Search results" frame when a result is double clicked.
*/
void CatalogSearch::showDataFileInfo()
{
if (m_icatUiForm.dataFileCbox->isChecked())
{
m_icatUiForm.dataFileFrame->show();
}
else
{
m_icatUiForm.dataFileFrame->hide();
}
}
/**
* Embolden the headers in the provided table.
*/
void CatalogSearch::emboldenTableHeaders(QTableWidget* table)
{
QFont font;
font.setBold(true);
for (int i = 0; i < table->columnCount(); ++i)
{
table->horizontalHeaderItem(i)->setFont(font);
}
}
/**
* Set the table properties prior to adding data to it.
* @param table :: The table we want to setup.
* @param numOfRows :: The number of rows in the workspace.
* @param numOfColumns :: The number of columns in the workspace.
*/
void CatalogSearch::setupTable(QTableWidget* table, const size_t &numOfRows, const size_t &numOfColumns)
{
table->setRowCount(static_cast<int>(numOfRows));
table->setColumnCount(static_cast<int>(numOfColumns));
// Improve the appearance of table to make it easier to read.
table->setAlternatingRowColors(true);
table->setStyleSheet("alternate-background-color: rgb(216, 225, 255)");
table->setSortingEnabled(false);
table->verticalHeader()->setVisible(false);
// Set the height on each row to 20 for UX improvement.
for (size_t i = 0; i < numOfRows; ++i)
{
table->setRowHeight(static_cast<int>(i),20);
}
}
/**
* Populate the provided table with data from the provided workspace.
* @param table :: The table we want to setup.
* @param workspace :: The workspace to obtain data information from.
*/
void CatalogSearch::populateTable(QTableWidget* table, const Mantid::API::ITableWorkspace_sptr &workspace)
{
//NOTE: This method freezes up the ICAT search GUI. We will need to do this adding in another thread.
// This will contain the list of column names.
QStringList columnHeaders;
// Add the data from the workspace to the search results table.
for(size_t col = 0 ; col < workspace->columnCount(); col++)
{
// Get the column name to display as the header of table widget
Mantid::API::Column_sptr column = workspace->getColumn(col);
columnHeaders.push_back(QString::fromStdString(column->name()));
for(size_t row = 0; row < workspace->rowCount(); ++row)
{
// Prints the value from the row to the ostringstream to use later.
std::ostringstream ostr;
column->print(row,ostr);
// Add a result to the table.
QTableWidgetItem *newItem = new QTableWidgetItem(QString::fromStdString(ostr.str()));
table->setItem(static_cast<int>(row),static_cast<int>(col), newItem);
// Allow the row to be selected, and enabled.
newItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
newItem->setToolTip(QString::fromStdString(ostr.str()));
}
}
// Set the table widgets header labels from the table workspace.
table->setHorizontalHeaderLabels(columnHeaders);
// Make the headers of the table bold.
emboldenTableHeaders(table);
}
/**
* Clears data associated with previous search.
* @param table :: The table to modify and remove previous results from.
* @param workspace :: The workspace to remove.
*/
void CatalogSearch::clearSearch(QTableWidget* table, const std::string &workspace)
{
// Remove workspace if it exists.
if(Mantid::API::AnalysisDataService::Instance().doesExist(workspace))
{
Mantid::API::AnalysisDataService::Instance().remove(workspace);
}
// In order to reset fields for the table
setupTable(table, 0, 0);
}
/**
* Clear the "search" frame when an investigation has been selected.
*/
void CatalogSearch::clearSearchFrame()
{
m_icatUiForm.searchCbox->setChecked(false);
m_icatUiForm.searchFrame->hide();
}
/**
* Clear the "search results" frame if no results are returned from search.
*/
void CatalogSearch::clearSearchResultFrame()
{
m_icatUiForm.searchResultsLbl->setText("0 investigations found.");
m_icatUiForm.searchResultsCbox->setEnabled(false);
m_icatUiForm.searchResultsCbox->setChecked(false);
m_icatUiForm.searchResultsTbl->clear();
m_icatUiForm.resFrame->hide();
}
/**
* Clear "dataFileFrame" when the user tries to search again.
*/
void CatalogSearch::clearDataFileFrame()
{
m_icatUiForm.dataFileCbox->setEnabled(false);
m_icatUiForm.dataFileCbox->setChecked(false);
m_icatUiForm.dataFileLbl->clear();
m_icatUiForm.dataFileFrame->hide();
}
/**
* Obtain the index of the column in a table that contains a specified name.
* @param table :: The table to search the headers on.
* @param searchFor :: The header name to search against.
* @return The index of the column with the specified name.
*/
int CatalogSearch::headerIndexByName(QTableWidget* table, const std::string &searchFor)
{
QAbstractItemModel *model = table->model();
// For every column in the table
for (int col = 0; col < table->columnCount(); col++)
{
// Is the column name the same as the searchFor string?
if (searchFor.compare(model->headerData(col, Qt::Horizontal, Qt::DisplayRole).toString()) == 0)
{
// Yes? Return the index of the column.
return (col);
}
}
// This indicates that the column was not found.
return (-1);
}
/**
* Save the current state of ICAT for next time
*/
void CatalogSearch::saveSettings()
{
QSettings settings;
settings.beginGroup("/ICatSettings");
settings.setValue("lastDownloadPath",m_downloadSaveDir);
settings.endGroup();
}
/**
* Read the saved settings from the store.
*/
void CatalogSearch::loadSettings()
{
QSettings settings;
settings.beginGroup("/ICatSettings");
QString lastdir = settings.value("lastDownloadPath").toString();
// The user has not previously selected a directory to save ICAT downloads to.
if (lastdir.isEmpty())
{
lastdir = QString::fromStdString(Mantid::Kernel::ConfigService::Instance().getString("defaultsave.directory"));
}
// Initalise the member variable to the last saved directory.
m_downloadSaveDir = lastdir;
settings.endGroup();
}
///////////////////////////////////////////////////////////////////////////////
/// Methods for "Catalog Search".
///////////////////////////////////////////////////////////////////////////////
/**
void CatalogSearch::dateSelected(const std::string &buttonName)
* Populates the "Instrument" list-box
*/
void CatalogSearch::populateInstrumentBox()
{
// Obtain the list of instruments to display in the drop-box.
std::vector<std::string> instrumentList = m_icatHelper->getInstrumentList(
m_catalogSelector->getSelectedCatalogSessions());
// This option allows the user to select no instruments (thus searching over them all).
m_icatUiForm.Instrument->insertItem(-1,"");
m_icatUiForm.Instrument->setCurrentIndex(0);
QString userInstrument = QString::fromStdString(Mantid::Kernel::ConfigService::Instance().getInstrument().name());
for (unsigned i = 0; i < instrumentList.size(); i++)
{
QString instrument = QString::fromStdString(instrumentList.at(i));
m_icatUiForm.Instrument->addItem(instrument);
if (userInstrument.compare(instrument) == 0)
{
m_icatUiForm.Instrument->setCurrentIndex(i + 1);
}
}
}
/**
* Populates the "Investigation type" list-box.
*/
void CatalogSearch::populateInvestigationTypeBox()
{
// Obtain the list of investigation types to display in the list-box.
std::vector<std::string> invesTypeList = m_icatHelper->getInvestigationTypeList(
m_catalogSelector->getSelectedCatalogSessions());
std::vector<std::string>::const_iterator citr;
for (citr = invesTypeList.begin(); citr != invesTypeList.end(); ++citr)
{
// Add each instrument to the instrument box.
m_icatUiForm.InvestigationType->addItem(QString::fromStdString(*citr));
}
// Sort the list-box by investigation type.
m_icatUiForm.InvestigationType->model()->sort(0);
// Make the default investigation type empty so the user has to select one.
m_icatUiForm.InvestigationType->insertItem(-1,"");
m_icatUiForm.InvestigationType->setCurrentIndex(0);
}
/**
* Get the users' input for each search field.
* @return A map containing all users' search fields - (key => FieldName, value => FieldValue).
*/
const std::map<std::string, std::string> CatalogSearch::getSearchFields()
{
std::map<std::string, std::string> searchFieldInput;
// Left side of form.
searchFieldInput.insert(std::pair<std::string, std::string>("InvestigationName", m_icatUiForm.InvestigationName->text().toStdString()));
searchFieldInput.insert(std::pair<std::string, std::string>("Instrument", m_icatUiForm.Instrument->currentText().toStdString()));
if (m_icatUiForm.RunRange->text().size() > 2)
{
searchFieldInput.insert(std::pair<std::string, std::string>("RunRange", m_icatUiForm.RunRange->text().toStdString()));
}
searchFieldInput.insert(std::pair<std::string, std::string>("InvestigatorSurname", m_icatUiForm.InvestigatorSurname->text().toStdString()));
searchFieldInput.insert(std::pair<std::string, std::string>("DataFileName", m_icatUiForm.DataFileName->text().toStdString()));
searchFieldInput.insert(std::pair<std::string, std::string>("InvestigationId", m_icatUiForm.InvestigationId->text().toStdString()));
// Right side of form.
if (m_icatUiForm.StartDate->text().size() > 2)
{
searchFieldInput.insert(std::pair<std::string, std::string>("StartDate", m_icatUiForm.StartDate->text().toStdString()));
}
if (m_icatUiForm.EndDate->text().size() > 2)
{
searchFieldInput.insert(std::pair<std::string, std::string>("EndDate", m_icatUiForm.EndDate->text().toStdString()));
}
searchFieldInput.insert(std::pair<std::string, std::string>("Keywords", m_icatUiForm.Keywords->text().toStdString()));
searchFieldInput.insert(std::pair<std::string, std::string>("SampleName", m_icatUiForm.SampleName->text().toStdString()));
searchFieldInput.insert(std::pair<std::string, std::string>("InvestigationType", m_icatUiForm.InvestigationType->currentText().toStdString()));
// Since we check if the field is empty in the algorithm, there's no need to check if advanced was clicked.
// If the "My data only" field is checked. We return the state of the checkbox (1 is true, 0 is false).
searchFieldInput.insert(std::pair<std::string, std::string>("MyData", boost::lexical_cast<std::string>(m_icatUiForm.myDataCbox->isChecked())));
return (searchFieldInput);
}
///////////////////////////////////////////////////////////////////////////////
/// SLOTS for "Catalog Search"
///////////////////////////////////////////////////////////////////////////////
/**
* Opens the DateTime m_calendar box when start or end date selected.
*/
void CatalogSearch::openCalendar()
{
// Set min/max dates to prevent user selecting unusual dates.
m_calendar->setMinimumDate(QDate(1950, 1, 1));
m_calendar->setMaximumDate(QDate(2050, 1, 1));
// Centre the calendar on screen.
m_calendar->setGeometry(QStyle::alignedRect(Qt::LeftToRight,Qt::AlignCenter,QSize(445,205),QDesktopWidget().availableGeometry()));
// Improve UX, then display the m_calendar.
m_calendar->setGridVisible(true);
m_calendar->setWindowTitle("Calendar picker");
m_calendar->show();
// Set the name of the date button the user pressed to open the calendar with.
m_dateButtonName = sender()->name();
}
/**
* Update text field when date is selected.
* @param date :: The date the user has selected.
*/
void CatalogSearch::dateSelected(QDate date)
{
// As openCalendar slot is used for both start and end date we need to perform a check
// to see which button was pressed, and then updated the related input field.
if (m_dateButtonName.compare("startDatePicker") == 0)
{
m_icatUiForm.StartDate->setText(date.toString("dd/MM/yyyy"));
}
else
{
m_icatUiForm.EndDate->setText(date.toString("dd/MM/yyyy"));
}
m_calendar->close();
}
/**
* Checks if start date is greater than end date.
* @returns true if start date is greater than end date.
*/
bool CatalogSearch::validateDates()
{
std::string startDateInput = m_icatUiForm.StartDate->text().toStdString();
std::string endDateInput = m_icatUiForm.EndDate->text().toStdString();
// Return false if the user has not input any dates. This prevents any null errors occurring.
if (startDateInput.size() <= 2 || endDateInput.size() <= 2) return false;
// If startDate > endDate we want to throw an error and inform the user (red star(*)).
if (m_icatHelper->getTimevalue(startDateInput) > m_icatHelper->getTimevalue(endDateInput))
{
m_icatUiForm.StartDate_err->setToolTip(QString::fromStdString("<span style=\"color: white;\">Start date cannot be greater than end date.</span>"));
m_icatUiForm.StartDate_err->show();
return true;
}
else
{
m_icatUiForm.StartDate_err->hide();
return false;
}
}
/**
* Show or hide advanced options if "Advanced Search" checked.
*/
void CatalogSearch::advancedSearchChecked()
{
if (m_icatUiForm.advSearchCbox->isChecked())
{
m_icatUiForm.advNameLbl->show();
m_icatUiForm.InvestigatorSurname->show();
m_icatUiForm.advDatafileLbl->show();
m_icatUiForm.DataFileName->show();
m_icatUiForm.advSampleLbl->show();
m_icatUiForm.SampleName->show();
m_icatUiForm.advTypeLbl->show();
m_icatUiForm.InvestigationType->show();
}
else
{
m_icatUiForm.advNameLbl->hide();
m_icatUiForm.InvestigatorSurname->hide();
m_icatUiForm.advDatafileLbl->hide();
m_icatUiForm.DataFileName->hide();
m_icatUiForm.advSampleLbl->hide();
m_icatUiForm.SampleName->hide();
m_icatUiForm.advTypeLbl->hide();
m_icatUiForm.InvestigationType->hide();
}
}
/**
* Hides the search frame, and shows search results frame when "Search" button pressed.
*/
void CatalogSearch::searchClicked()
{
std::string name = sender()->name();
// This allows us to perform paging on each search separately
// as we call this method in three separate SLOTS (two paging & search button).
if (name.compare("searchBtn") == 0) m_currentPageNumber = 1;
clearDataFileFrame();
std::map<std::string, std::string> inputFields = getSearchFields();
// Contains the error label names, and the related error message.
std::map<std::string, std::string> errors = m_icatHelper->validateProperties(inputFields);
// Has any errors occurred?
if (!errors.empty() || validateDates())
{
// Clear form to prevent previous search results showing if an error occurs.
clearSearchResultFrame();
showErrorLabels(errors);
m_icatUiForm.searchResultsLbl->setText("An error has occurred in the search form.");
// Stop here to prevent the search being carried out below.
return;
}
// Performed here to allow the user to login while the search GUI is open,
// and see results of all facilities/catalogs that they are logged in to.
m_catalogSelector->populateFacilitySelection();
// Since there are no longer errors we hide the error labels.
hideErrorLabels();
// We want to disable/hide these as a search is in progress, but no results have been obtained.
m_icatUiForm.resFrame->hide();
m_icatUiForm.searchResultsCbox->setEnabled(false);
m_icatUiForm.searchResultsCbox->setChecked(false);
// Update the label to inform the user that searching is in progress.
m_icatUiForm.searchResultsLbl->setText("searching investigations...");
// Remove previous search results.
std::string searchResults = "searchResults";
clearSearch(m_icatUiForm.searchResultsTbl, searchResults);
auto sessionIDs = m_catalogSelector->getSelectedCatalogSessions();
// Obtain the number of results for paging.
int64_t numrows = m_icatHelper->getNumberOfSearchResults(inputFields,sessionIDs);
// Setup values used for paging.
int limit = 100;
// Have to cast either numrows or limit to double for ceil to work correctly.
double totalNumPages = ceil(static_cast<double>(numrows) / limit);
int offset = (m_currentPageNumber - 1) * limit;
// Set paging labels.
m_icatUiForm.pageStartNum->setText(QString::number(m_currentPageNumber));
m_icatUiForm.resPageEndNumTxt->setText(QString::number(totalNumPages));
// Perform a search using paging (E.g. return only n from m).
m_icatHelper->executeSearch(inputFields,offset,limit,sessionIDs);
// Update the label to inform the user of how many investigations have been returned from the search.
m_icatUiForm.searchResultsLbl->setText(QString::number(numrows) + " investigations found.");
// Populate the result table from the searchResult workspace.
populateResultTable();
}
/**
* Show the error message labels, including the error message on the tooltips.
* @param errors :: A map containing the error label names, and the related error message.
*/
void CatalogSearch::showErrorLabels(std::map<std::string, std::string> &errors)
{
for(auto iter = errors.begin(); iter != errors.end(); ++iter)
{
QLabel* label = m_icatUiForm.searchFrame->findChild<QLabel*>(QString::fromStdString(iter->first));
if (label)
{
// Update the tooltip of the element and then show it.
label->setToolTip(QString::fromStdString("<span style=\"color: white;\">" + iter->second + "</span>"));
label->show();
}
}
}
/**
* Hides the error message labels on the GUI.
*/
void CatalogSearch::hideErrorLabels()
{
// Left side of form.
m_icatUiForm.InvestigationName_err->setVisible(false);
m_icatUiForm.Instrument_err->setVisible(false);
m_icatUiForm.RunRange_err->setVisible(false);
m_icatUiForm.InvestigationId_err->setVisible(false);
m_icatUiForm.InvestigatorSurname_err->setVisible(false);
m_icatUiForm.InvestigationAbstract_err->setVisible(false);
// Right side of form.
m_icatUiForm.StartDate_err->setVisible(false);
m_icatUiForm.EndDate_err->setVisible(false);
m_icatUiForm.Keywords_err->setVisible(false);
m_icatUiForm.SampleName_err->setVisible(false);
m_icatUiForm.InvestigationType_err->setVisible(false);
}
/**
* Reset all fields when the "Reset" button is pressed.
*/
void CatalogSearch::onReset()
{
// Clear the QLineEdit boxes.
foreach(QLineEdit *widget, this->findChildren<QLineEdit*>())
{
widget->clear();
}
// Clear all other elements.
m_icatUiForm.Instrument->setCurrentIndex(0);
m_icatUiForm.InvestigationType->setCurrentIndex(0);
m_icatUiForm.advSearchCbox->setChecked(false);
m_icatUiForm.myDataCbox->setChecked(false);
}
/**
* Allows a user to select specific facilities that they want to search the catalogs of.
*/
void CatalogSearch::openFacilitySelection()
{
m_catalogSelector->populateFacilitySelection();
m_catalogSelector->show();
m_catalogSelector->raise();
}
///////////////////////////////////////////////////////////////////////////////
// Methods for "Search results"
///////////////////////////////////////////////////////////////////////////////
/**
* Outputs the results of the search into the "Search results" table.
*/
void CatalogSearch::populateResultTable()
{
// Obtain a pointer to the "searchResults" workspace where the search results are saved if it exists.
Mantid::API::ITableWorkspace_sptr workspace;
// Check to see if the workspace exists...
if(Mantid::API::AnalysisDataService::Instance().doesExist("__searchResults"))
{
workspace = boost::dynamic_pointer_cast<Mantid::API::ITableWorkspace>(Mantid::API::AnalysisDataService::Instance().retrieve("__searchResults"));
}
else
{
// Otherwise an error will be thrown (in ICat4Catalog). We will reproduce that error on the ICAT form for the user.
m_icatUiForm.searchResultsLbl->setText("You have not input any terms to search for.");
return;
}
// If there are no results then clear search form and don't try to setup table.
if (workspace->rowCount() == 0)
{
clearSearchResultFrame();
return;
}
// Create local variable for convenience and reusability.
QTableWidget* resultsTable = m_icatUiForm.searchResultsTbl;
// Set the result's table properties prior to adding data.
setupTable(resultsTable, workspace->rowCount(), workspace->columnCount());
// Update the label to inform the user of how many investigations have been returned from the search.
// We want to show this now as we are certain that search results exist, and not display a blank frame (bad UX).
m_icatUiForm.resFrame->show();
m_icatUiForm.searchResultsCbox->setEnabled(true);
m_icatUiForm.searchResultsCbox->setChecked(true);
// Add data from the workspace to the results table.
populateTable(resultsTable, workspace);
// Show only a portion of the title as they can be quite long.
resultsTable->setColumnWidth(headerIndexByName(resultsTable, "Title"), 210);
resultsTable->setColumnHidden(headerIndexByName(resultsTable, "SessionID"), true);
// Sort by endDate with the most recent being first.
resultsTable->setSortingEnabled(true);
resultsTable->sortByColumn(headerIndexByName(resultsTable, "Start date"),Qt::DescendingOrder);
}
/**
* Obtain the sessionID for the specific row selected in the search results table.
* (only one row can be selected at a time)
* @return The sessionID from the selected row.
*/
std::string CatalogSearch::selectedInvestigationSession()
{
auto searchResultsTable = m_icatUiForm.searchResultsTbl;
return searchResultsTable->item(
searchResultsTable->selectionModel()->selectedRows().at(0).row(),
headerIndexByName(searchResultsTable, "SessionID"))->text().toStdString();
}
///////////////////////////////////////////////////////////////////////////////
// SLOTS for "Search results"
///////////////////////////////////////////////////////////////////////////////
/**
* Populate the result table, and update the page number.
*/
void CatalogSearch::nextPageClicked()
{
int totalNumPages = m_icatUiForm.resPageEndNumTxt->text().toInt();
// Prevent user from pressing "next" when no more investigations exist.
if (m_currentPageNumber >= totalNumPages)
{
m_currentPageNumber = totalNumPages;
return;
}
// Increment here as we need to validate page number above.
m_currentPageNumber++;
// Perform the search, and update the table with the new results using the current page num.
searchClicked();
}
/**
* Populate the result table, and update the page number.
*/
void CatalogSearch::prevPageClicked()
{
m_currentPageNumber--;
// Prevent user from pressing "Previous" when no investigations exist.
if (m_currentPageNumber <= 0)
{
m_currentPageNumber = 1;
return;
}
searchClicked();
}
/**
* Populate's result table depending page number input by user.
*/
void CatalogSearch::goToInputPage()
{
int pageNum = m_icatUiForm.pageStartNum->text().toInt();
// If the user inputs a page number larger than the total
// amount of page numbers we do not want to do anything.
if (pageNum > m_icatUiForm.resPageEndNumTxt->text().toInt() || pageNum <= 0)
{
m_icatUiForm.pageStartNum->setText(QString::number(m_currentPageNumber));
return;
}
m_currentPageNumber = pageNum;
searchClicked();
}
/**
* Hides the "search results" frame, and shows the "dataFiles" frame when an investigation is selected.
*/
void CatalogSearch::investigationSelected(QTableWidgetItem* item)
{
clearSearchFrame();
//
m_icatUiForm.dataFileCbox->setEnabled(true);
m_icatUiForm.dataFileCbox->setChecked(true);
m_icatUiForm.dataFileFrame->show();
// Have to clear the combo-box in order to prevent the user from seeing the extensions of previous search.
m_icatUiForm.dataFileFilterCombo->clear();
m_icatUiForm.dataFileFilterCombo->addItem("No filter");
// Inform the user that the search is in progress.
m_icatUiForm.dataFileLbl->setText("searching for related datafiles...");
QTableWidget* searchResultsTable = m_icatUiForm.searchResultsTbl;
// Obtain the investigationID from the selected
QTableWidgetItem* investigationId = searchResultsTable->item(
item->row(),headerIndexByName(searchResultsTable,"InvestigationID"));
// Remove previous dataFile search results.
std::string dataFileResults = "dataFileResults";
clearSearch(m_icatUiForm.dataFileResultsTbl, dataFileResults);
// Update the labels in the dataFiles information frame to show relevant info to use.
updateDataFileLabels(item);
// Perform the "search" and obtain the related data files for the selected investigation.
m_icatHelper->executeGetDataFiles(investigationId->text().toStdString(),
searchResultsTable->item(item->row(),headerIndexByName(searchResultsTable, "SessionID"))->text().toStdString());
// Populate the dataFile table from the "dataFileResults" workspace.
populateDataFileTable();
}
///////////////////////////////////////////////////////////////////////////////
// Methods for "DataFile information"
///////////////////////////////////////////////////////////////////////////////
/**
* Outputs related dataFiles (from selected investigation) into the "DataFile information" table.
*/
void CatalogSearch::populateDataFileTable()
{
// Obtain a pointer to the "dataFileResults" workspace where the related datafiles for the user selected investigation exist.
Mantid::API::ITableWorkspace_sptr workspace;
// Check to see if the workspace exists...
if(Mantid::API::AnalysisDataService::Instance().doesExist("__dataFileResults"))
{
workspace = boost::dynamic_pointer_cast<Mantid::API::ITableWorkspace>(Mantid::API::AnalysisDataService::Instance().retrieve("__dataFileResults"));
}
else
{
return;
}
// If there are no results then don't try to setup table.
if (workspace->rowCount() == 0)
{
clearDataFileFrame();
m_icatUiForm.dataFileLbl->setText(QString::number(workspace->rowCount()) + " datafiles found.");
return;
}
// Create local variable for convenience and reusability.
QTableWidget* dataFileTable = m_icatUiForm.dataFileResultsTbl;
// Set the result's table properties prior to adding data.
setupTable(dataFileTable, workspace->rowCount(), workspace->columnCount());
// Update the label to inform the user of how many dataFiles relating to the selected investigation have been found.
m_icatUiForm.dataFileLbl->setText(QString::number(workspace->rowCount()) + " datafiles found.");
// Create the custom header with checkbox ability.
m_customHeader = new CheckboxHeader(Qt::Horizontal, dataFileTable);
// There is no simple way to override default QTableWidget sort.
// Instead, connecting header to obtain column clicked, and sorting by
connect(m_customHeader,SIGNAL(sectionClicked(int)),this,SLOT(sortByFileSize(int)));
// Set it prior to adding labels in populateTable.
dataFileTable->setHorizontalHeader(m_customHeader);
// Add data from the workspace to the results table.
populateTable(dataFileTable, workspace);
// As a new column is being added we do this after populateTable to prevent null errors.
addCheckBoxColumn(dataFileTable);
// Resize the columns to improve the viewing experience.
// Has been called here since we added the checkbox column after populating table.
dataFileTable->resizeColumnsToContents();
// Hide these columns as they're not useful for the user, but are used by the algorithms.
dataFileTable->setColumnHidden(headerIndexByName(dataFileTable, "Id"), true);
dataFileTable->setColumnHidden(headerIndexByName(dataFileTable, "Location"), true);
dataFileTable->setColumnHidden(headerIndexByName(dataFileTable, "File size(bytes)"), true);
// Obtain the list of extensions of all dataFiles for the chosen investigation.
// "File name" is the first column of "dataFileResults" so we make use of it.
std::set<std::string> extensions = getDataFileExtensions(workspace.get()->getColumn(headerIndexByName(dataFileTable, "Name")));
// Populate the "Filter type..." combo-box with all possible file extensions.
populateDataFileType(extensions);
// Sort by create time with the most recent being first.
dataFileTable->setSortingEnabled(true);
dataFileTable->sortByColumn(headerIndexByName(dataFileTable, "Name"),Qt::DescendingOrder);
}
/**
* Add a row of checkboxes to the first column of a table.
* @param table :: The table to add the checkboxes to.
*/
void CatalogSearch::addCheckBoxColumn(QTableWidget* table)
{
// Add a new column checkbox column.
table->insertColumn(0);
// Add a new header item to this column. This allows us to overwrite the default text!
table->setHorizontalHeaderItem(0, new QTableWidgetItem());
// Set this here (rather than on initialisation) as the customer header would be null otherwise.
connect(m_customHeader,SIGNAL(toggled(bool)),this,SLOT(selectAllDataFiles(bool)));
// Add a checkbox to all rows in the first column.
for (int row = 0; row < table->rowCount(); row++)
{
QTableWidgetItem *newItem = new QTableWidgetItem();
// Allow the widget to take on checkbox functionality.
newItem->setCheckState(Qt::Unchecked);
// Allow the user to select and check the box.
newItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
// Add a result to the table.
table->setItem(row,0,newItem);
}
}
/**
* Obtains the names of the selected dataFiles, in preparation for download.
* @return A vector containing the fileID and fileName of the datafile(s) to download.
*/
const std::vector<std::pair<int64_t, std::string>> CatalogSearch::selectedDataFileNames()
{
QTableWidget* table = m_icatUiForm.dataFileResultsTbl;
// Holds the FileID, and fileName in order to perform search to download later.
std::vector<std::pair<int64_t, std::string>> fileInfo;
for (int row = 0; row < table->rowCount(); row++)
{
if (table->item(row, 0)->checkState())
{
fileInfo.push_back(std::make_pair(
table->item(row, headerIndexByName(table, "Id"))->text().toLongLong(),
table->item(row, headerIndexByName(table, "Name"))->text().toStdString())
);
}
}
return (fileInfo);
}
/**
* Updates the dataFile text boxes with relevant info regarding the selected dataFile.
*/
void CatalogSearch::updateDataFileLabels(QTableWidgetItem* item)
{
QTableWidget* searchResultsTable = m_icatUiForm.searchResultsTbl;