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.pbDataMoveToTop, SIGNAL(clicked()), this, SLOT(moveToTop()));
99 connect(m_uiForm.pbDataMoveUp, SIGNAL(clicked()), this, SLOT(moveUp()));
100 connect(m_uiForm.pbScriptMoveUp, SIGNAL(clicked()), this, SLOT(moveUp()));
101 connect(m_uiForm.pbExtMoveUp, SIGNAL(clicked()), this, SLOT(moveUp()));
102 connect(m_uiForm.pbDataMoveDown, SIGNAL(clicked()), this, SLOT(moveDown()));
103 connect(m_uiForm.pbScriptMoveDown, SIGNAL(clicked()), this, SLOT(moveDown()));
104 connect(m_uiForm.pbExtMoveDown, SIGNAL(clicked()), this, SLOT(moveDown()));
105
106 connect(m_uiForm.pbSaveBrowse, SIGNAL(clicked()), this, SLOT(selectSaveDir()));
107}
108
113 auto &config = ConfigService::Instance();
114 auto populateListWidget = [&config](QListWidget *widget, const std::string &key) {
115 const auto directories = QString::fromStdString(config.getString(key)).trimmed();
116 const auto items = directories.split(";", Qt::SkipEmptyParts);
117 widget->clear();
118 widget->addItems(items);
119 };
120 // fill lists
121 populateListWidget(m_uiForm.lwDataSearchDirs, ConfigKeys::DATASEARCH_DIRS);
122 populateListWidget(m_uiForm.lwScriptSearchDirs, ConfigKeys::PYTHONSCRIPTS_DIRS);
123 populateListWidget(m_uiForm.lwExtSearchDirs, ConfigKeys::USERPYTHONPLUGINS_DIRS);
124
125 // set flag of whether to search the data archive
126 const auto archive = QString::fromStdString(config.getString(ConfigKeys::DATASEARCH_ARCHIVE)).trimmed().toLower();
127 const auto defaultFacility =
128 QString::fromStdString(config.getString(ConfigKeys::DEFAULT_FACILITY)).trimmed().toUpper();
129 m_uiForm.cbSearchArchive->addItem(QString("default facility only - ") + defaultFacility);
130 m_uiForm.cbSearchArchive->addItem("all");
131 m_uiForm.cbSearchArchive->addItem("off");
132 if (archive == "on") {
133 m_uiForm.cbSearchArchive->setCurrentIndex(0);
134 } else if (archive == "all") {
135 m_uiForm.cbSearchArchive->setCurrentIndex(1);
136 } else if (archive == "off") {
137 m_uiForm.cbSearchArchive->setCurrentIndex(2);
138 } else { // only add custom if it has been set
139 m_uiForm.cbSearchArchive->addItem("custom - " + archive.toUpper());
140 m_uiForm.cbSearchArchive->setCurrentIndex(3);
141 }
142
143 // default save directory
144 const auto saveDir = QString::fromStdString(config.getString(ConfigKeys::DEFAULTSAVE_DIR)).trimmed();
145 m_uiForm.leDefaultSave->setText(saveDir);
146}
147
152 auto &config = ConfigService::Instance();
153
154 QString newSearchArchive = m_uiForm.cbSearchArchive->currentText().toLower();
155 if (newSearchArchive == "all" || newSearchArchive == "off") {
156 // do nothing
157 } else if (newSearchArchive.startsWith("default facility only")) {
158 newSearchArchive = "on";
159 } else {
160 // the only way "custom" gets set is by using the value in ConfigService
161 // already, so just copy it
162 newSearchArchive =
163 QString::fromStdString(Mantid::Kernel::ConfigService::Instance().getString("datasearch.searcharchive"))
164 .trimmed()
165 .toLower();
166 }
167
168 // convert a path from the list to something the configservice will
169 // understand. replaces all \ with / and
170 auto toConfigPath = [](QString path) {
171 if (path.isEmpty())
172 return path;
173 auto unixStylePath = path.replace('\\', '/');
174 if (!unixStylePath.endsWith('/'))
175 unixStylePath.append('/');
176 return unixStylePath;
177 };
178 auto toConfigString = [&toConfigPath](QListWidget *itemsWidget) {
179 QStringList paths;
180 for (int i = 0; i < itemsWidget->count(); i++) {
181 const auto path = itemsWidget->item(i)->text().trimmed();
182 if (!path.isEmpty())
183 paths.append(toConfigPath(path));
184 }
185 return paths.join(";").toStdString();
186 };
187
188 // data directories
189 config.setString(ConfigKeys::DATASEARCH_ARCHIVE, newSearchArchive.toStdString());
190 config.setString(ConfigKeys::DATASEARCH_DIRS, toConfigString(m_uiForm.lwDataSearchDirs));
191 config.setString(ConfigKeys::DEFAULTSAVE_DIR, toConfigPath(m_uiForm.leDefaultSave->text().trimmed()).toStdString());
192 // python directories
193 config.setString(ConfigKeys::PYTHONSCRIPTS_DIRS, toConfigString(m_uiForm.lwScriptSearchDirs));
194 config.setString(ConfigKeys::USERPYTHONPLUGINS_DIRS, toConfigString(m_uiForm.lwExtSearchDirs));
195
196 if (m_saveToFile)
197 config.saveConfig(config.getUserFilename());
198}
199
205QListWidget *ManageUserDirectories::listWidget(QObject *sender) {
206 const auto objectName = sender->objectName();
207 if (objectName.contains(ButtonPrefix::DATA)) {
208 return m_uiForm.lwDataSearchDirs;
209 } else if (objectName.contains(ButtonPrefix::SCRIPT)) {
210 return m_uiForm.lwScriptSearchDirs;
211 } else if (objectName.contains(ButtonPrefix::EXTENSIONS)) {
212 return m_uiForm.lwExtSearchDirs;
213 } else {
214 return nullptr;
215 }
216}
217
220
223
227 this->close();
228}
229
235 QLineEdit *input(nullptr);
236
237 if (m_uiForm.tabWidget->currentWidget() == m_uiForm.tabDataSearch) {
238 input = m_uiForm.leDirectoryPath;
239 } else if (m_uiForm.tabWidget->currentWidget() == m_uiForm.tabPythonDirectories) {
240 input = m_uiForm.leDirectoryPathPython;
241 }
242
243 if (input && input->text() != "") {
244 listWidget(sender())->addItem(input->text());
245 input->clear();
246 }
247}
248
253 QSettings settings;
254 const auto lastDirectory = settings.value(QSettingsKeys::LastDirectory, "").toString();
255
256 const auto newDir = QFileDialog::getExistingDirectory(this, tr("Select New Data Directory"), lastDirectory,
257 QFileDialog::ShowDirsOnly);
258
259 if (!newDir.isEmpty()) {
260 settings.setValue(QSettingsKeys::LastDirectory, newDir);
261 listWidget(sender())->addItem(newDir);
262 }
263}
264
267 QList<QListWidgetItem *> selected = listWidget(sender())->selectedItems();
268 for (auto &i : selected) {
269 delete i;
270 }
271}
272
274void ManageUserDirectories::moveItem(bool toTop, int offset) {
275 QListWidget *list = listWidget(sender());
276 QList<QListWidgetItem *> selected = list->selectedItems();
277 for (auto &i : selected) {
278 int index = list->row(i);
279 int count = list->count();
280 int newIndex = 0;
281 if (toTop) {
282 newIndex = 0;
283 } else {
284 newIndex = index + offset;
285 }
286 if (newIndex >= 0 && newIndex < count) {
287 QListWidgetItem *move = list->takeItem(index);
288 list->insertItem(newIndex, move);
289 }
290 list->setCurrentItem(i);
291 }
292}
293
296
299
302
305 QSettings settings;
306 auto lastDirectory = m_uiForm.leDefaultSave->text().trimmed();
307 if (lastDirectory.isEmpty())
308 lastDirectory = settings.value(QSettingsKeys::LastDirectory, "").toString();
309
310 const auto newDir = QFileDialog::getExistingDirectory(this, tr("Select New Default Save Directory"), lastDirectory,
311 QFileDialog::ShowDirsOnly);
312
313 if (!newDir.isEmpty()) {
314 const auto path = newDir + QDir::separator();
315 settings.setValue(QSettingsKeys::LastDirectory, path);
316 m_uiForm.leDefaultSave->setText(path);
317 }
318}
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.
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 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.
Mantid::Kernel::SingletonHolder< ConfigServiceImpl > ConfigService