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 +
10#include <QDir>
11#include <QFileDialog>
12#include <QPointer>
13#include <QSettings>
14#include <QUrl>
15
17using namespace MantidQt::API;
18
19namespace {
20
21namespace ButtonPrefix {
22const QString DATA{"pbData"};
23const QString SCRIPT{"pbScript"};
24const QString EXTENSIONS{"pbExt"};
25} // namespace ButtonPrefix
26
27namespace ConfigKeys {
28const std::string DATASEARCH_DIRS{"datasearch.directories"};
29const std::string PYTHONSCRIPTS_DIRS{"pythonscripts.directories"};
30const std::string USERPYTHONPLUGINS_DIRS{"user.python.plugins.directories"};
31const std::string DATASEARCH_ARCHIVE{"datasearch.searcharchive"};
32const std::string DEFAULT_FACILITY{"default.facility"};
33const std::string DEFAULTSAVE_DIR{"defaultsave.directory"};
34} // namespace ConfigKeys
35
36namespace QSettingsKeys {
37const auto LastDirectory{"ManageUserSettings/last_directory"};
38} // namespace QSettingsKeys
39
40// ID for help page in docs
41const QString HELP_ID{"ManageUserDirectories"};
42
43// Current instance opened with openManageUserDirectories
44QPointer<ManageUserDirectories> CURRENTLY_OPEN_MUD;
45} // namespace
46
52 if (CURRENTLY_OPEN_MUD.isNull()) {
53 CURRENTLY_OPEN_MUD = QPointer<ManageUserDirectories>(new ManageUserDirectories);
54 CURRENTLY_OPEN_MUD->show();
55 } else {
56 CURRENTLY_OPEN_MUD->raise();
57 }
58 return CURRENTLY_OPEN_MUD;
59}
60
65ManageUserDirectories::ManageUserDirectories(QWidget *parent) : BaseClass(parent), m_saveToFile(true) {
66 setAttribute(Qt::WA_DeleteOnClose);
67 m_uiForm.setupUi(this);
68 initLayout();
69}
70
78
84
85 // Make Connections
86 connect(m_uiForm.pbHelp, SIGNAL(clicked()), this, SLOT(helpClicked()));
87 connect(m_uiForm.pbCancel, SIGNAL(clicked()), this, SLOT(cancelClicked()));
88 connect(m_uiForm.pbConfirm, SIGNAL(clicked()), this, SLOT(confirmClicked()));
89
90 connect(m_uiForm.pbDataAddDirectory, SIGNAL(clicked()), this, SLOT(addDirectory()));
91 connect(m_uiForm.pbScriptAddDirectory, SIGNAL(clicked()), this, SLOT(addDirectory()));
92 connect(m_uiForm.pbDataBrowseToDir, SIGNAL(clicked()), this, SLOT(browseToDirectory()));
93 connect(m_uiForm.pbScriptBrowseToDir, SIGNAL(clicked()), this, SLOT(browseToDirectory()));
94 connect(m_uiForm.pbExtBrowseToDir, SIGNAL(clicked()), this, SLOT(browseToDirectory()));
95 connect(m_uiForm.pbDataRemDir, SIGNAL(clicked()), this, SLOT(remDir()));
96 connect(m_uiForm.pbScriptRemDir, SIGNAL(clicked()), this, SLOT(remDir()));
97 connect(m_uiForm.pbExtRemoveDir, SIGNAL(clicked()), this, SLOT(remDir()));
98 connect(m_uiForm.pbDataMoveUp, SIGNAL(clicked()), this, SLOT(moveUp()));
99 connect(m_uiForm.pbScriptMoveUp, SIGNAL(clicked()), this, SLOT(moveUp()));
100 connect(m_uiForm.pbExtMoveUp, SIGNAL(clicked()), this, SLOT(moveUp()));
101 connect(m_uiForm.pbDataMoveDown, SIGNAL(clicked()), this, SLOT(moveDown()));
102 connect(m_uiForm.pbScriptMoveDown, SIGNAL(clicked()), this, SLOT(moveDown()));
103 connect(m_uiForm.pbExtMoveDown, SIGNAL(clicked()), this, SLOT(moveDown()));
104
105 connect(m_uiForm.pbSaveBrowse, SIGNAL(clicked()), this, SLOT(selectSaveDir()));
106}
107
112 auto &config = ConfigService::Instance();
113 auto populateListWidget = [&config](QListWidget *widget, const std::string &key) {
114 const auto directories = QString::fromStdString(config.getString(key)).trimmed();
115 const auto items = directories.split(";", Qt::SkipEmptyParts);
116 widget->clear();
117 widget->addItems(items);
118 };
119 // fill lists
120 populateListWidget(m_uiForm.lwDataSearchDirs, ConfigKeys::DATASEARCH_DIRS);
121 populateListWidget(m_uiForm.lwScriptSearchDirs, ConfigKeys::PYTHONSCRIPTS_DIRS);
122 populateListWidget(m_uiForm.lwExtSearchDirs, ConfigKeys::USERPYTHONPLUGINS_DIRS);
123
124 // set flag of whether to search the data archive
125 const auto archive = QString::fromStdString(config.getString(ConfigKeys::DATASEARCH_ARCHIVE)).trimmed().toLower();
126 const auto defaultFacility =
127 QString::fromStdString(config.getString(ConfigKeys::DEFAULT_FACILITY)).trimmed().toUpper();
128 m_uiForm.cbSearchArchive->addItem(QString("default facility only - ") + defaultFacility);
129 m_uiForm.cbSearchArchive->addItem("all");
130 m_uiForm.cbSearchArchive->addItem("off");
131 if (archive == "on") {
132 m_uiForm.cbSearchArchive->setCurrentIndex(0);
133 } else if (archive == "all") {
134 m_uiForm.cbSearchArchive->setCurrentIndex(1);
135 } else if (archive == "off") {
136 m_uiForm.cbSearchArchive->setCurrentIndex(2);
137 } else { // only add custom if it has been set
138 m_uiForm.cbSearchArchive->addItem("custom - " + archive.toUpper());
139 m_uiForm.cbSearchArchive->setCurrentIndex(3);
140 }
141
142 // default save directory
143 const auto saveDir = QString::fromStdString(config.getString(ConfigKeys::DEFAULTSAVE_DIR)).trimmed();
144 m_uiForm.leDefaultSave->setText(saveDir);
145}
146
151 auto &config = ConfigService::Instance();
152
153 QString newSearchArchive = m_uiForm.cbSearchArchive->currentText().toLower();
154 if (newSearchArchive == "all" || newSearchArchive == "off") {
155 // do nothing
156 } else if (newSearchArchive.startsWith("default facility only")) {
157 newSearchArchive = "on";
158 } else {
159 // the only way "custom" gets set is by using the value in ConfigService
160 // already, so just copy it
161 newSearchArchive =
162 QString::fromStdString(Mantid::Kernel::ConfigService::Instance().getString("datasearch.searcharchive"))
163 .trimmed()
164 .toLower();
165 }
166
167 // convert a path from the list to something the configservice will
168 // understand. replaces all \ with / and
169 auto toConfigPath = [](QString path) {
170 if (path.isEmpty())
171 return path;
172 auto unixStylePath = path.replace('\\', '/');
173 if (!unixStylePath.endsWith('/'))
174 unixStylePath.append('/');
175 return unixStylePath;
176 };
177 auto toConfigString = [&toConfigPath](QListWidget *itemsWidget) {
178 QStringList paths;
179 for (int i = 0; i < itemsWidget->count(); i++) {
180 const auto path = itemsWidget->item(i)->text().trimmed();
181 if (!path.isEmpty())
182 paths.append(toConfigPath(path));
183 }
184 return paths.join(";").toStdString();
185 };
186
187 // data directories
188 config.setString(ConfigKeys::DATASEARCH_ARCHIVE, newSearchArchive.toStdString());
189 config.setString(ConfigKeys::DATASEARCH_DIRS, toConfigString(m_uiForm.lwDataSearchDirs));
190 config.setString(ConfigKeys::DEFAULTSAVE_DIR, toConfigPath(m_uiForm.leDefaultSave->text().trimmed()).toStdString());
191 // python directories
192 config.setString(ConfigKeys::PYTHONSCRIPTS_DIRS, toConfigString(m_uiForm.lwScriptSearchDirs));
193 config.setString(ConfigKeys::USERPYTHONPLUGINS_DIRS, toConfigString(m_uiForm.lwExtSearchDirs));
194
195 if (m_saveToFile)
196 config.saveConfig(config.getUserFilename());
197}
198
204QListWidget *ManageUserDirectories::listWidget(QObject *sender) {
205 const auto objectName = sender->objectName();
206 if (objectName.contains(ButtonPrefix::DATA)) {
207 return m_uiForm.lwDataSearchDirs;
208 } else if (objectName.contains(ButtonPrefix::SCRIPT)) {
209 return m_uiForm.lwScriptSearchDirs;
210 } else if (objectName.contains(ButtonPrefix::EXTENSIONS)) {
211 return m_uiForm.lwExtSearchDirs;
212 } else {
213 return nullptr;
214 }
215}
216
219
222
226 this->close();
227}
228
234 QLineEdit *input(nullptr);
235
236 if (m_uiForm.tabWidget->currentWidget() == m_uiForm.tabDataSearch) {
237 input = m_uiForm.leDirectoryPath;
238 } else if (m_uiForm.tabWidget->currentWidget() == m_uiForm.tabPythonDirectories) {
239 input = m_uiForm.leDirectoryPathPython;
240 }
241
242 if (input && input->text() != "") {
243 listWidget(sender())->addItem(input->text());
244 input->clear();
245 }
246}
247
252 QSettings settings;
253 const auto lastDirectory = settings.value(QSettingsKeys::LastDirectory, "").toString();
254
255 const auto newDir = QFileDialog::getExistingDirectory(this, tr("Select New Data Directory"), lastDirectory,
256 QFileDialog::ShowDirsOnly);
257
258 if (!newDir.isEmpty()) {
259 settings.setValue(QSettingsKeys::LastDirectory, newDir);
260 listWidget(sender())->addItem(newDir);
261 }
262}
263
266 QList<QListWidgetItem *> selected = listWidget(sender())->selectedItems();
267 for (auto &i : selected) {
268 delete i;
269 }
270}
271
274 QListWidget *list = listWidget(sender());
275 QList<QListWidgetItem *> selected = list->selectedItems();
276 for (auto &i : selected) {
277 int index = list->row(i);
278 if (index != 0) {
279 QListWidgetItem *move = list->takeItem(index);
280 list->insertItem(index - 1, move);
281 }
282 list->setCurrentItem(i);
283 }
284}
285
288 QListWidget *list = listWidget(sender());
289 int count = list->count();
290 QList<QListWidgetItem *> selected = list->selectedItems();
291 for (auto &i : selected) {
292 int index = list->row(i);
293 if (index != (count - 1)) {
294 QListWidgetItem *move = list->takeItem(index);
295 list->insertItem(index + 1, move);
296 }
297 list->setCurrentItem(i);
298 }
299}
300
303 QSettings settings;
304 auto lastDirectory = m_uiForm.leDefaultSave->text().trimmed();
305 if (lastDirectory.isEmpty())
306 lastDirectory = settings.value(QSettingsKeys::LastDirectory, "").toString();
307
308 const auto newDir = QFileDialog::getExistingDirectory(this, tr("Select New Default Save Directory"), lastDirectory,
309 QFileDialog::ShowDirsOnly);
310
311 if (!newDir.isEmpty()) {
312 const auto path = newDir + QDir::separator();
313 settings.setValue(QSettingsKeys::LastDirectory, path);
314 m_uiForm.leDefaultSave->setText(path);
315 }
316}
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
int count
counter
Definition: Matrix.cpp:37
static void showCustomInterface(const QString &name, const QString &area=QString(), const QString &section=QString())
Definition: HelpWindow.cpp:94
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 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.
virtual void initLayout()
Create the UI layout, fill the widgets and connect the relevant signals.
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.
void moveUp()
Raise an item up in the list based on the sender of the signal.
ManageUserDirectories(QWidget *parent=nullptr)
Constructor.
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.
Manage the lifetime of a class intended to be a singleton.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...