Mantid
Loading...
Searching...
No Matches
ManageUserDirectories.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
11#include <QDir>
12#include <QFileDialog>
13#include <QPointer>
14#include <QSettings>
15#include <QUrl>
16
18using namespace MantidQt::API;
19
20namespace {
21
22namespace ButtonPrefix {
23const QString DATA{"pbData"};
24const QString SCRIPT{"pbScript"};
25const QString EXTENSIONS{"pbExt"};
26} // namespace ButtonPrefix
27
28namespace ConfigKeys {
29const std::string DATASEARCH_DIRS{"datasearch.directories"};
30const std::string PYTHONSCRIPTS_DIRS{"pythonscripts.directories"};
31const std::string USERPYTHONPLUGINS_DIRS{"user.python.plugins.directories"};
32const std::string DATASEARCH_ARCHIVE{"datasearch.searcharchive"};
33const std::string DEFAULT_FACILITY{"default.facility"};
34const std::string DEFAULTSAVE_DIR{"defaultsave.directory"};
35} // namespace ConfigKeys
36
37namespace QSettingsKeys {
38const auto LastDirectory{"ManageUserSettings/last_directory"};
39} // namespace QSettingsKeys
40
41// ID for help page in docs
42const QString HELP_ID{"ManageUserDirectories"};
43
44// Current instance opened with openManageUserDirectories
45QPointer<ManageUserDirectories> CURRENTLY_OPEN_MUD;
46} // namespace
47
53 if (CURRENTLY_OPEN_MUD.isNull()) {
54 CURRENTLY_OPEN_MUD = QPointer<ManageUserDirectories>(new ManageUserDirectories);
55 CURRENTLY_OPEN_MUD->show();
56 } else {
57 CURRENTLY_OPEN_MUD->raise();
58 }
59 return CURRENTLY_OPEN_MUD;
60}
61
66ManageUserDirectories::ManageUserDirectories(QWidget *parent) : BaseClass(parent), m_saveToFile(true) {
67 setAttribute(Qt::WA_DeleteOnClose);
68 m_uiForm.setupUi(this);
69 initLayout();
70}
71
79
85
86 // Make Connections
87 connect(m_uiForm.pbHelp, SIGNAL(clicked()), this, SLOT(helpClicked()));
88 connect(m_uiForm.pbCancel, SIGNAL(clicked()), this, SLOT(cancelClicked()));
89 connect(m_uiForm.pbConfirm, SIGNAL(clicked()), this, SLOT(confirmClicked()));
90
91 connect(m_uiForm.pbDataAddDirectory, SIGNAL(clicked()), this, SLOT(addDirectory()));
92 connect(m_uiForm.pbScriptAddDirectory, SIGNAL(clicked()), this, SLOT(addDirectory()));
93 connect(m_uiForm.pbDataBrowseToDir, SIGNAL(clicked()), this, SLOT(browseToDirectory()));
94 connect(m_uiForm.pbScriptBrowseToDir, SIGNAL(clicked()), this, SLOT(browseToDirectory()));
95 connect(m_uiForm.pbExtBrowseToDir, SIGNAL(clicked()), this, SLOT(browseToDirectory()));
96 connect(m_uiForm.pbDataRemDir, SIGNAL(clicked()), this, SLOT(remDir()));
97 connect(m_uiForm.pbScriptRemDir, SIGNAL(clicked()), this, SLOT(remDir()));
98 connect(m_uiForm.pbExtRemoveDir, SIGNAL(clicked()), this, SLOT(remDir()));
99 connect(m_uiForm.pbDataMoveToTop, SIGNAL(clicked()), this, SLOT(moveToTop()));
100 connect(m_uiForm.pbDataMoveUp, SIGNAL(clicked()), this, SLOT(moveUp()));
101 connect(m_uiForm.pbScriptMoveUp, SIGNAL(clicked()), this, SLOT(moveUp()));
102 connect(m_uiForm.pbExtMoveUp, SIGNAL(clicked()), this, SLOT(moveUp()));
103 connect(m_uiForm.pbDataMoveDown, SIGNAL(clicked()), this, SLOT(moveDown()));
104 connect(m_uiForm.pbScriptMoveDown, SIGNAL(clicked()), this, SLOT(moveDown()));
105 connect(m_uiForm.pbExtMoveDown, SIGNAL(clicked()), this, SLOT(moveDown()));
106
107 connect(m_uiForm.pbSaveBrowse, SIGNAL(clicked()), this, SLOT(selectSaveDir()));
108}
109
114 auto &config = ConfigService::Instance();
115 auto populateListWidget = [&config](QListWidget *widget, const std::string &key) {
116 const auto directories = QString::fromStdString(config.getString(key)).trimmed();
117 const auto items = directories.split(";", Qt::SkipEmptyParts);
118 widget->clear();
119 widget->addItems(items);
120 };
121 // fill lists
122 populateListWidget(m_uiForm.lwDataSearchDirs, ConfigKeys::DATASEARCH_DIRS);
123 populateListWidget(m_uiForm.lwScriptSearchDirs, ConfigKeys::PYTHONSCRIPTS_DIRS);
124 populateListWidget(m_uiForm.lwExtSearchDirs, ConfigKeys::USERPYTHONPLUGINS_DIRS);
125
126 // set flag of whether to search the data archive
127 const auto archive = QString::fromStdString(config.getString(ConfigKeys::DATASEARCH_ARCHIVE)).trimmed().toLower();
128 const auto defaultFacility =
129 QString::fromStdString(config.getString(ConfigKeys::DEFAULT_FACILITY)).trimmed().toUpper();
130 m_uiForm.cbSearchArchive->addItem(QString("default facility only - ") + defaultFacility);
131 m_uiForm.cbSearchArchive->addItem("all");
132 m_uiForm.cbSearchArchive->addItem("off");
133 if (archive == "on") {
134 m_uiForm.cbSearchArchive->setCurrentIndex(0);
135 } else if (archive == "all") {
136 m_uiForm.cbSearchArchive->setCurrentIndex(1);
137 } else if (archive == "off") {
138 m_uiForm.cbSearchArchive->setCurrentIndex(2);
139 } else { // only add custom if it has been set
140 m_uiForm.cbSearchArchive->addItem("custom - " + archive.toUpper());
141 m_uiForm.cbSearchArchive->setCurrentIndex(3);
142 }
143
144 // default save directory
145 const auto saveDir = QString::fromStdString(config.getString(ConfigKeys::DEFAULTSAVE_DIR)).trimmed();
146 m_uiForm.leDefaultSave->setText(saveDir);
147}
148
153 auto &config = ConfigService::Instance();
154
155 QString newSearchArchive = m_uiForm.cbSearchArchive->currentText().toLower();
156 if (newSearchArchive == "all" || newSearchArchive == "off") {
157 // do nothing
158 } else if (newSearchArchive.startsWith("default facility only")) {
159 newSearchArchive = "on";
160 } else {
161 // the only way "custom" gets set is by using the value in ConfigService
162 // already, so just copy it
163 newSearchArchive =
164 QString::fromStdString(Mantid::Kernel::ConfigService::Instance().getString("datasearch.searcharchive"))
165 .trimmed()
166 .toLower();
167 }
168
169 // convert a path from the list to something the configservice will
170 // understand. replaces all \ with / and
171 auto toConfigPath = [](QString path) {
172 if (path.isEmpty())
173 return path;
174 auto unixStylePath = path.replace('\\', '/');
175 if (!unixStylePath.endsWith('/'))
176 unixStylePath.append('/');
177 return unixStylePath;
178 };
179 auto toConfigString = [&toConfigPath](QListWidget *itemsWidget) {
180 QStringList paths;
181 for (int i = 0; i < itemsWidget->count(); i++) {
182 const auto path = itemsWidget->item(i)->text().trimmed();
183 if (!path.isEmpty())
184 paths.append(toConfigPath(path));
185 }
186 return paths.join(";").toStdString();
187 };
188
189 // data directories
190 config.setString(ConfigKeys::DATASEARCH_ARCHIVE, newSearchArchive.toStdString());
191 config.setString(ConfigKeys::DATASEARCH_DIRS, toConfigString(m_uiForm.lwDataSearchDirs));
192 config.setString(ConfigKeys::DEFAULTSAVE_DIR, toConfigPath(m_uiForm.leDefaultSave->text().trimmed()).toStdString());
193 // python directories
194 config.setString(ConfigKeys::PYTHONSCRIPTS_DIRS, toConfigString(m_uiForm.lwScriptSearchDirs));
195 config.setString(ConfigKeys::USERPYTHONPLUGINS_DIRS, toConfigString(m_uiForm.lwExtSearchDirs));
196
197 if (m_saveToFile)
198 config.saveConfig(config.getUserFilename());
199}
200
206QListWidget *ManageUserDirectories::listWidget(QObject *sender) {
207 const auto objectName = sender->objectName();
208 if (objectName.contains(ButtonPrefix::DATA)) {
209 return m_uiForm.lwDataSearchDirs;
210 } else if (objectName.contains(ButtonPrefix::SCRIPT)) {
211 return m_uiForm.lwScriptSearchDirs;
212 } else if (objectName.contains(ButtonPrefix::EXTENSIONS)) {
213 return m_uiForm.lwExtSearchDirs;
214 } else {
215 return nullptr;
216 }
217}
218
221
224
228 this->close();
229}
230
236 QLineEdit *input(nullptr);
237
238 if (m_uiForm.tabWidget->currentWidget() == m_uiForm.tabDataSearch) {
239 input = m_uiForm.leDirectoryPath;
240 } else if (m_uiForm.tabWidget->currentWidget() == m_uiForm.tabPythonDirectories) {
241 input = m_uiForm.leDirectoryPathPython;
242 }
243
244 if (input && input->text() != "") {
245 listWidget(sender())->addItem(input->text());
246 input->clear();
247 }
248}
249
254 QSettings settings;
255 const auto lastDirectory = settings.value(QSettingsKeys::LastDirectory, "").toString();
256
257 const auto newDir = QFileDialog::getExistingDirectory(this, tr("Select New Data Directory"), lastDirectory,
258 QFileDialog::ShowDirsOnly);
259
260 if (!newDir.isEmpty()) {
261 MantidQt::MantidWidgets::QSettingsChangeAware(settings).setValue(QSettingsKeys::LastDirectory, newDir);
262 listWidget(sender())->addItem(newDir);
263 }
264}
265
268 QList<QListWidgetItem *> selected = listWidget(sender())->selectedItems();
269 for (auto &i : selected) {
270 delete i;
271 }
272}
273
275void ManageUserDirectories::moveItem(bool toTop, int offset) {
276 QListWidget *list = listWidget(sender());
277 QList<QListWidgetItem *> selected = list->selectedItems();
278 for (auto &i : selected) {
279 int index = list->row(i);
280 int count = list->count();
281 int newIndex = 0;
282 if (toTop) {
283 newIndex = 0;
284 } else {
285 newIndex = index + offset;
286 }
287 if (newIndex >= 0 && newIndex < count) {
288 QListWidgetItem *move = list->takeItem(index);
289 list->insertItem(newIndex, move);
290 }
291 list->setCurrentItem(i);
292 }
293}
294
297
300
303
306 QSettings settings;
307 auto lastDirectory = m_uiForm.leDefaultSave->text().trimmed();
308 if (lastDirectory.isEmpty())
309 lastDirectory = settings.value(QSettingsKeys::LastDirectory, "").toString();
310
311 const auto newDir = QFileDialog::getExistingDirectory(this, tr("Select New Default Save Directory"), lastDirectory,
312 QFileDialog::ShowDirsOnly);
313
314 if (!newDir.isEmpty()) {
315 auto path = QDir::cleanPath(newDir);
316 if (!path.endsWith(QDir::separator())) {
317 path += QDir::separator();
318 }
319 MantidQt::MantidWidgets::QSettingsChangeAware(settings).setValue(QSettingsKeys::LastDirectory, path);
320 m_uiForm.leDefaultSave->setText(path);
321 }
322}
std::map< DeltaEMode::Type, std::string > index
int count
counter
Definition Matrix.cpp:37
static void showCustomInterface(const QString &name, const QString &area=QString(), const QString &section=QString())
Access and update the user directory settings within the Mantid config service.
void saveProperties()
Save the current contents of the widgets back to the main config.
void confirmClicked()
Persist the properties to the config store and close the dialog.
void loadProperties()
Load config properties into the form widgets.
void moveItem(bool toTop, int offset)
Move an item in the list based on the sender of the signal and a specified direction.
void browseToDirectory()
Browse to find a new directory.
void selectSaveDir()
Find an existing directory to be used for the save directory path.
void enableSaveToFile(bool enabled)
Control if the config service changes are persisted to the user file.
void moveDown()
Lower an item down in the list based on the sender of the signal.
static ManageUserDirectories * openManageUserDirectories()
Show the default dialog or raise the existing one if it exists.
QListWidget * listWidget(QObject *object)
Return the QListWidget related to the given sender.
virtual void initLayout() final
Create the UI layout, fill the widgets and connect the relevant signals.
void moveUp()
Raise an item up in the list based on the sender of the signal.
ManageUserDirectories(QWidget *parent=nullptr)
Constructor.
void moveToTop()
Raise an item to the top in the list based on the sender of the signal.
void helpClicked()
Show the help for ManageUserDirectories.
void cancelClicked()
Close the dialog without saving the configuration.
void addDirectory()
Handle the add directory button to take the text from a text box based on the signal sender to the co...
void remDir()
Remove a directory from the list based on the sender.
A QSettings facade that applies only effective changes.
bool setValue(QString const &key, QVariant const &value)
Set a value only when the effective stored value differs.
Mantid::Kernel::SingletonHolder< ConfigServiceImpl > ConfigService