Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/8244_icat_date_selector_…
Browse files Browse the repository at this point in the history
…issues'
  • Loading branch information
mantid-roman committed Nov 15, 2013
2 parents 34dffce + 605cac4 commit c79d1dd
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace MantidQt
bool validSession();
/// Open the login dialog if user not logged in.
void openLoginDialog(QWidget* window);
/// Creates a time_t value from an input date ("23/06/2003") for comparison.
time_t getTimevalue(const std::string& inputDate);

private:
/// Creates an algorithm with the name provided.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ namespace MantidQt
// Methods for: "Catalog Search"
///////////////////////////////////////////////////////////////////////////////

/// Ensures the correct text box is updated when the date is selected on the calendar.
void dateSelected(const std::string &buttonName);
/// Populate the instrument list-box.
void populateInstrumentBox();
/// Populate the investigation type list-box.
void populateInvestigationTypeBox();
/// Obtain the users' text input for each search field.
const std::map<std::string, std::string> getSearchFields();
/// Checks if start date is greater than end date.
bool validateDates();

///////////////////////////////////////////////////////////////////////////////
// Methods for: "Search results"
Expand Down Expand Up @@ -134,10 +135,8 @@ namespace MantidQt

/// Open the DateTime Calendar to select date.
void openCalendar();
/// Update startDate text field when startDatePicker is used and date is selected.
void updateStartDate();
/// Update endDate text field when endDatePicker is used and date is selected.
void updateEndDate();
/// Update text field when date is selected.
void dateSelected(QDate date);
/// Show the advanced field when checked.
void advancedSearchChecked();
/// When the "Search" button is clicked, display "Search results" frame.
Expand Down Expand Up @@ -179,6 +178,8 @@ namespace MantidQt
void dataFileRowSelected();

private:
/// The name of the date button the user pressed to open the calendar.
std::string m_dateButtonName;
/// The custom table header with checkbox functionality.
CheckboxHeader * m_customHeader;
/// The form generated by QT Designer
Expand Down
21 changes: 21 additions & 0 deletions Code/Mantid/MantidQt/MantidWidgets/src/CatalogHelper.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "MantidQtMantidWidgets/CatalogHelper.h"
#include "MantidQtAPI/AlgorithmDialog.h"
#include "MantidQtAPI/InterfaceManager.h"
#include "MantidKernel/DateAndTime.h"

#include <boost/algorithm/string/regex.hpp>
#include <QCoreApplication>

namespace MantidQt
Expand Down Expand Up @@ -179,6 +181,25 @@ namespace MantidQt
}
}

/**
* Creates a time_t value from an input date ("23/06/2003") for comparison.
* @param inputDate :: string containing the date.
* @return time_t value of date
*/
time_t CatalogHelper::getTimevalue(const std::string& inputDate)
{
// Prevent any possible errors.
if(inputDate.empty()) return 0;
// A container to hold the segments of the date.
std::vector<std::string> dateSegments;
// Split input by "/" prior to rearranging the date
boost::algorithm::split_regex(dateSegments, inputDate, boost::regex("/"));
// Reorganise the date to be ISO format.
std::string isoDate = dateSegments.at(2) + "-" + dateSegments.at(1) + "-" + dateSegments.at(0) + " 0:00:00.000";
// Return the date as time_t value.
return Mantid::Kernel::DateAndTime(isoDate).to_time_t();
}

/**
* Obtain the algorithm documentation for the given property.
* @param properties :: A list of properties for a provided algorithm.
Expand Down
82 changes: 44 additions & 38 deletions Code/Mantid/MantidQt/MantidWidgets/src/CatalogSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ namespace MantidQt
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.
Expand Down Expand Up @@ -369,26 +374,7 @@ namespace MantidQt
///////////////////////////////////////////////////////////////////////////////

/**
* Updates text field depending on button picker selected.
* @param buttonName :: The name of the text field is derived from the buttonName.
*/
void CatalogSearch::dateSelected(const std::string &buttonName)
{
if (buttonName.compare("startDatePicker") == 0)
{
// Since the user wants to select a startDate we disable the endDate button to prevent any issues.
m_icatUiForm.endDatePicker->setEnabled(false);
// Update the text field and re-enable the button.
connect(m_calendar, SIGNAL(selectionChanged()),this, SLOT(updateStartDate()));
}
else
{
m_icatUiForm.startDatePicker->setEnabled(false);
connect(m_calendar, SIGNAL(selectionChanged()),this, SLOT(updateEndDate()));
}
}

/**
* Populates the "Instrument" list-box
*/
void CatalogSearch::populateInstrumentBox()
Expand Down Expand Up @@ -483,10 +469,6 @@ namespace MantidQt
*/
void CatalogSearch::openCalendar()
{
// Pop the m_calendar out into it's own window.
QWidget* parent = qobject_cast<QWidget*>(this->parent());
m_calendar = new QCalendarWidget(parent);

// Set min/max dates to prevent user selecting unusual dates.
m_calendar->setMinimumDate(QDate(1950, 1, 1));
m_calendar->setMaximumDate(QDate(2050, 1, 1));
Expand All @@ -499,31 +481,53 @@ namespace MantidQt
m_calendar->setWindowTitle("Calendar picker");
m_calendar->show();

// Uses the previously clicked button (startDatePicker or endDatePicker) to determine which
// text field that the opened m_calendar is coordinating with (e.g. the one we want to write date to).
dateSelected(sender()->name());
// Set the name of the date button the user pressed to open the calendar with.
m_dateButtonName = sender()->name();
}

/**
* Update startDate text field when startDatePicker is used and date is selected.
* Update text field when date is selected.
* @param date :: The date the user has selected.
*/
void CatalogSearch::updateStartDate()
void CatalogSearch::dateSelected(QDate date)
{
// Update the text field with the user selected date then close the m_calendar.
m_icatUiForm.StartDate->setText(m_calendar->selectedDate().toString("dd/MM/yyyy"));
// 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();
// Re-enable the button to allow the user to select an endDate if they wish.
m_icatUiForm.endDatePicker->setEnabled(true);
}

/**
* Update endDate text field when endDatePicker is used and date is selected.
* Checks if start date is greater than end date.
* @returns true if start date is greater than end date.
*/
void CatalogSearch::updateEndDate()
bool CatalogSearch::validateDates()
{
m_icatUiForm.EndDate->setText(m_calendar->selectedDate().toString("dd/MM/yyyy"));
m_calendar->close();
m_icatUiForm.startDatePicker->setEnabled(true);
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;
}
}

/**
Expand Down Expand Up @@ -569,8 +573,10 @@ namespace MantidQt
std::map<std::string, std::string> errors = m_icatHelper->validateProperties(inputFields);

// Has any errors occurred?
if (!errors.empty())
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.
Expand Down

0 comments on commit c79d1dd

Please sign in to comment.