From ef4def8d28c47c76f7ccbaba57960490ef8d5b8d Mon Sep 17 00:00:00 2001 From: Nick Draper Date: Tue, 30 Jun 2015 13:40:37 +0100 Subject: [PATCH] refactor icon state changing code 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 --- .../SelectWorkspacesDialog.h | 2 +- .../inc/MantidQtSliceViewer/SliceViewer.h | 4 + .../MantidQt/SliceViewer/src/SliceViewer.cpp | 96 +++++++++---------- 3 files changed, 48 insertions(+), 54 deletions(-) diff --git a/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SelectWorkspacesDialog.h b/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SelectWorkspacesDialog.h index 4c3bc0eda441..477d71183dac 100644 --- a/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SelectWorkspacesDialog.h +++ b/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/SelectWorkspacesDialog.h @@ -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 = ""); diff --git a/Code/Mantid/MantidQt/SliceViewer/inc/MantidQtSliceViewer/SliceViewer.h b/Code/Mantid/MantidQt/SliceViewer/inc/MantidQtSliceViewer/SliceViewer.h index 9a0497b745ff..d47541c39dcf 100644 --- a/Code/Mantid/MantidQt/SliceViewer/inc/MantidQtSliceViewer/SliceViewer.h +++ b/Code/Mantid/MantidQt/SliceViewer/inc/MantidQtSliceViewer/SliceViewer.h @@ -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(); diff --git a/Code/Mantid/MantidQt/SliceViewer/src/SliceViewer.cpp b/Code/Mantid/MantidQt/SliceViewer/src/SliceViewer.cpp index 8dd715ab7fab..586a874a925f 100644 --- a/Code/Mantid/MantidQt/SliceViewer/src/SliceViewer.cpp +++ b/Code/Mantid/MantidQt/SliceViewer/src/SliceViewer.cpp @@ -231,8 +231,35 @@ void SliceViewer::saveSettings() { static_cast(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 ------------------------- @@ -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); @@ -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); @@ -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" @@ -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); } @@ -966,8 +979,6 @@ 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()); @@ -975,21 +986,17 @@ void SliceViewer::SnapToGrid_toggled(bool checked) { 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); } //------------------------------------------------------------------------------ @@ -1003,11 +1010,8 @@ 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 @@ -1015,9 +1019,7 @@ void SliceViewer::RebinMode_toggled(bool checked) { 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(); } @@ -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); } @@ -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(); } @@ -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); } }