Skip to content

Commit

Permalink
refactor icon state changing code
Browse files Browse the repository at this point in the history
This answers point 1 from the issue

Point 2 I have discussed with Martyn for a second opinion.
The way it is currently implemented fits best with how Qt dialogs work
with return values, as they just return done(int retVal).  Qt has set
return values for Ok and Cancel, Yes and No, but no option for a custom
response.
The static constant is well away from the existing values and allows this
extension cleanly.

re #12324
  • Loading branch information
NickDraper committed Jun 30, 2015
1 parent 2c085bc commit ef4def8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 54 deletions.
Expand Up @@ -48,7 +48,7 @@ class EXPORT_OPT_MANTIDQT_MANTIDWIDGETS SelectWorkspacesDialog : public QDialog

public:
///return value of the Custom button
static const int CustomButton = 45654;
static const int CustomButton = 45654; //do not use this number direct, just refer to this static constant

/// Constructor
SelectWorkspacesDialog (QWidget* parent = NULL, const std::string& typeFilter = "", const std::string& customButtonLabel = "");
Expand Down
Expand Up @@ -209,6 +209,10 @@ public slots:
private:
void loadSettings();
void saveSettings();
void setIconFromString(QAction* action, const std::string& iconName,
QIcon::Mode mode, QIcon::State state);
void setIconFromString(QWidget* btn, const std::string& iconName,
QIcon::Mode mode, QIcon::State state);
void initMenus();
void initZoomer();

Expand Down
96 changes: 43 additions & 53 deletions Code/Mantid/MantidQt/SliceViewer/src/SliceViewer.cpp
Expand Up @@ -231,8 +231,35 @@ void SliceViewer::saveSettings() {
static_cast<int>(this->getNormalization()));
settings.endGroup();
}

//------------------------------------------------------------------------------
/** set an icon given the control and a string.
* @param btn the widget to give the new icon
* @param iconName the path of the new icon
* @param mode the mode of the icon
* @param state on or off state of the icon
*/
void SliceViewer::setIconFromString(QWidget* btn, const std::string& iconName,
QIcon::Mode mode = QIcon::Mode::Normal, QIcon::State state = QIcon::State::Off)
{
QIcon icon;
icon.addFile(QString::fromStdString(iconName), QSize(), mode,state);
btn->setIcon(icon.pixmap());
}
/** set an icon given the control and a string.
* @param action the menu action to give the new icon
* @param iconName the path of the new icon
* @param mode the mode of the icon
* @param state on or off state of the icon
*/
void SliceViewer::setIconFromString(QAction* action,const std::string& iconName,
QIcon::Mode mode = QIcon::Mode::Normal, QIcon::State state= QIcon::State::Off)
{
QIcon icon;
icon.addFile(QString::fromStdString(iconName), QSize(), mode,state);
action->setIcon(icon);
}

//------------------------------------------------------------------------------
/** Create the menus */
void SliceViewer::initMenus() {
// ---------------------- Build the menu bar -------------------------
Expand Down Expand Up @@ -277,10 +304,7 @@ void SliceViewer::initMenus() {
action = new QAction(QPixmap(), "&Reset Zoom", this);
connect(action, SIGNAL(triggered()), this, SLOT(resetZoom()));
{
QIcon icon;
icon.addFile(QString::fromStdString(g_iconViewFull), QSize(), QIcon::Normal,
QIcon::Off);
action->setIcon(icon);
setIconFromString(action,g_iconViewFull);
}
m_menuView->addAction(action);

Expand Down Expand Up @@ -383,20 +407,14 @@ void SliceViewer::initMenus() {
connect(action, SIGNAL(triggered()), this, SLOT(setColorScaleAutoSlice()));
action->setIconVisibleInMenu(true);
{
QIcon icon;
icon.addFile(QString::fromStdString(g_iconZoomPlus), QSize(), QIcon::Normal,
QIcon::Off);
action->setIcon(icon);
setIconFromString(action,g_iconZoomPlus);
}
m_menuColorOptions->addAction(action);

action = new QAction(QPixmap(), "&Full range", this);
connect(action, SIGNAL(triggered()), this, SLOT(setColorScaleAutoFull()));
{
QIcon icon;
icon.addFile(QString::fromStdString(g_iconZoomMinus), QSize(),
QIcon::Normal, QIcon::Off);
action->setIcon(icon);
setIconFromString(action,g_iconZoomMinus);
}
m_menuColorOptions->addAction(action);

Expand Down Expand Up @@ -922,11 +940,8 @@ void SliceViewer::refreshRebin() { this->rebinParamsChanged(); }
void SliceViewer::LineMode_toggled(bool checked) {
m_lineOverlay->setShown(checked);

QIcon icon;
if (checked) {
icon.addFile(QString::fromStdString(g_iconCutOn), QSize(), QIcon::Normal,
QIcon::On);
ui.btnDoLine->setIcon(icon);
setIconFromString(ui.btnDoLine,g_iconCutOn,QIcon::Mode::Normal,QIcon::State::On);
QString text;
if (m_lineOverlay->getCreationMode())
text = "Click and drag to draw an cut line.\n"
Expand All @@ -938,9 +953,7 @@ void SliceViewer::LineMode_toggled(bool checked) {
if (!checked) {
// clear the old line
clearLine();
icon.addFile(QString::fromStdString(g_iconCut), QSize(), QIcon::Normal,
QIcon::Off);
ui.btnDoLine->setIcon(icon);
setIconFromString(ui.btnDoLine,g_iconCut);
}
emit showLineViewer(checked);
}
Expand All @@ -966,30 +979,24 @@ void SliceViewer::clearLine() {
//------------------------------------------------------------------------------
/// Slot called when the snap to grid is checked
void SliceViewer::SnapToGrid_toggled(bool checked) {

QIcon icon;
if (checked) {
SnapToGridDialog *dlg = new SnapToGridDialog(this);
dlg->setSnap(m_lineOverlay->getSnapX(), m_lineOverlay->getSnapY());
if (dlg->exec() == QDialog::Accepted) {
m_lineOverlay->setSnapEnabled(true);
m_lineOverlay->setSnapX(dlg->getSnapX());
m_lineOverlay->setSnapY(dlg->getSnapY());
icon.addFile(QString::fromStdString(g_iconGridOn), QSize(), QIcon::Normal,
QIcon::On);
setIconFromString(ui.btnSnapToGrid,g_iconGridOn, QIcon::Normal, QIcon::On);
} else {
// Uncheck - the user clicked cancel
ui.btnSnapToGrid->setChecked(false);
m_lineOverlay->setSnapEnabled(false);
icon.addFile(QString::fromStdString(g_iconGrid), QSize(), QIcon::Normal,
QIcon::Off);
setIconFromString(ui.btnSnapToGrid,g_iconGrid, QIcon::Normal, QIcon::Off);
}
} else {
m_lineOverlay->setSnapEnabled(false);
icon.addFile(QString::fromStdString(g_iconGrid), QSize(), QIcon::Normal,
QIcon::Off);
setIconFromString(ui.btnSnapToGrid,g_iconGrid, QIcon::Normal, QIcon::Off);
}
ui.btnSnapToGrid->setIcon(icon);
}

//------------------------------------------------------------------------------
Expand All @@ -1003,21 +1010,16 @@ void SliceViewer::RebinMode_toggled(bool checked) {
m_actionRefreshRebin->setEnabled(checked);
m_rebinMode = checked;

QIcon icon;
if (!m_rebinMode) {
icon.addFile(QString::fromStdString(g_iconRebin), QSize(), QIcon::Normal,
QIcon::Off);
ui.btnRebinMode->setIcon(icon);
setIconFromString(ui.btnRebinMode,g_iconRebin, QIcon::Normal, QIcon::Off);
// uncheck auto-rebin
ui.btnAutoRebin->setChecked(false);
// Remove the overlay WS
this->m_overlayWS.reset();
this->m_data->setOverlayWorkspace(m_overlayWS);
this->updateDisplay();
} else {
icon.addFile(QString::fromStdString(g_iconRebinOn), QSize(), QIcon::Normal,
QIcon::On);
ui.btnRebinMode->setIcon(icon);
setIconFromString(ui.btnRebinMode,g_iconRebinOn, QIcon::Normal, QIcon::On);
// Start the rebin
this->rebinParamsChanged();
}
Expand Down Expand Up @@ -2140,10 +2142,7 @@ void SliceViewer::disablePeakOverlays() {
emit showPeaksViewer(false);
m_menuPeaks->setEnabled(false);

QIcon icon;
icon.addFile(QString::fromStdString(g_iconPeakList), QSize(), QIcon::Normal,
QIcon::Off);
ui.btnPeakOverlay->setIcon(icon);
setIconFromString(ui.btnPeakOverlay,g_iconPeakList, QIcon::Normal, QIcon::Off);
ui.btnPeakOverlay->setChecked(false);
}

Expand Down Expand Up @@ -2219,11 +2218,8 @@ SliceViewer::setPeaksWorkspaces(const QStringList &list) {
updatePeakOverlaySliderWidget();
emit showPeaksViewer(true);
m_menuPeaks->setEnabled(true);

QIcon icon;
icon.addFile(QString::fromStdString(g_iconPeakList), QSize(), QIcon::Normal,
QIcon::Off);
ui.btnPeakOverlay->setIcon(icon);

setIconFromString(ui.btnPeakOverlay,g_iconPeakList, QIcon::Normal, QIcon::Off);
ui.btnPeakOverlay->setChecked(true);
return m_proxyPeaksPresenter.get();
}
Expand All @@ -2249,17 +2245,11 @@ void SliceViewer::peakOverlay_clicked() {
if (ret == MantidQt::MantidWidgets::SelectWorkspacesDialog::CustomButton) {
disablePeakOverlays();
}
QIcon icon;
if (m_peaksPresenter->size() > 0) {
icon.addFile(QString::fromStdString(g_iconPeakListOn), QSize(),
QIcon::Normal, QIcon::On);
ui.btnPeakOverlay->setIcon(icon);
setIconFromString(ui.btnPeakOverlay,g_iconPeakListOn, QIcon::Normal, QIcon::On);
ui.btnPeakOverlay->setChecked(true);
} else {

icon.addFile(QString::fromStdString(g_iconPeakList), QSize(), QIcon::Normal,
QIcon::Off);
ui.btnPeakOverlay->setIcon(icon);
setIconFromString(ui.btnPeakOverlay,g_iconPeakList, QIcon::Normal, QIcon::Off);
ui.btnPeakOverlay->setChecked(false);
}
}
Expand Down

0 comments on commit ef4def8

Please sign in to comment.